1 /*
2 * Copyright (c) 2012-2013, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
33 /*
34 * ======== MmRpc.c ========
35 */
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/ioctl.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <string.h>
46 #include <stdint.h> /* should be in linux/rpmsg_rpc.h */
47 #include <stddef.h> /* should be in linux/rpmsg_rpc.h */
51 #if defined(KERNEL_INSTALL_DIR)
53 #ifdef linux
54 #define _linux_ linux
55 #undef linux
56 #endif
57 #define linux_include(kd,m) <kd/include/linux/m.h>
58 #include linux_include(KERNEL_INSTALL_DIR,rpmsg_rpc)
59 #ifdef _linux_
60 #define linux _linux
61 #undef _linux_
62 #endif
64 #elif defined(SYSLINK_BUILDOS_QNX)
66 #include <ti/ipc/rpmsg_rpc.h>
68 #elif defined(IPC_BUILDOS_ANDROID)
69 #include <linux/rpmsg_rpc.h>
71 #else
72 #error Unsupported Operating System
73 #endif
75 #include "MmRpc.h"
78 /*
79 * ======== MmRpc_Object ========
80 */
81 typedef struct {
82 int fd; /* device file descriptor */
83 struct rppc_create_instance connect; /* connection object */
84 } MmRpc_Object;
86 /*
87 * ======== MmRpc_Params_init ========
88 */
89 void MmRpc_Params_init(MmRpc_Params *params)
90 {
91 params->reserved = 0;
92 }
94 /*
95 * ======== MmRpc_create ========
96 */
97 int MmRpc_create(const char *service, const MmRpc_Params *params,
98 MmRpc_Handle *handlePtr)
99 {
100 int status = MmRpc_S_SUCCESS;
101 MmRpc_Object * obj;
102 char cbuf[RPPC_MAX_INST_NAMELEN+16];
104 /* allocate the instance object */
105 obj = (MmRpc_Object *)calloc(1, sizeof(MmRpc_Object));
107 if (obj == NULL) {
108 status = MmRpc_E_FAIL;
109 goto leave;
110 }
112 /* open the driver */
113 sprintf(cbuf, "/dev/%s", service);
114 obj->fd = open(cbuf, O_RDWR);
116 if (obj->fd < 0) {
117 printf("MmRpc_create: Error: open failed, name=%s\n", cbuf);
118 status = MmRpc_E_FAIL;
119 goto leave;
120 }
122 strncpy(obj->connect.name, service, (RPPC_MAX_INST_NAMELEN - 1));
123 obj->connect.name[RPPC_MAX_INST_NAMELEN - 1] = '\0';
125 /* create a server instance, rebind its address to this file descriptor */
126 status = ioctl(obj->fd, RPPC_IOC_CREATE, &obj->connect);
128 if (status < 0) {
129 printf("MmRpc_create: Error: connect failed\n");
130 status = MmRpc_E_FAIL;
131 goto leave;
132 }
134 leave:
135 if (status < 0) {
136 if ((obj != NULL) && (obj->fd >= 0)) {
137 close(obj->fd);
138 }
139 if (obj != NULL) {
140 free(obj);
141 }
142 *handlePtr = NULL;
143 }
144 else {
145 *handlePtr = (MmRpc_Handle)obj;
146 }
148 return(status);
149 }
151 /*
152 * ======== MmRpc_delete ========
153 */
154 int MmRpc_delete(MmRpc_Handle *handlePtr)
155 {
156 int status = MmRpc_S_SUCCESS;
157 MmRpc_Object *obj;
159 obj = (MmRpc_Object *)(*handlePtr);
161 /* close the device */
162 if ((obj != NULL) && (obj->fd >= 0)) {
163 close(obj->fd);
164 }
166 /* free the instance object */
167 free((void *)(*handlePtr));
168 *handlePtr = NULL;
170 return(status);
171 }
173 /*
174 * ======== MmRpc_call ========
175 */
176 int MmRpc_call(MmRpc_Handle handle, MmRpc_FxnCtx *ctx, int32_t *ret)
177 {
178 int status = MmRpc_S_SUCCESS;
179 MmRpc_Object *obj = (MmRpc_Object *)handle;
180 struct rppc_function *rpfxn;
181 struct rppc_function_return reply_msg;
182 MmRpc_Param *param;
183 void *msg;
184 int len;
185 int i;
187 /* combine params and translation array into one contiguous message */
188 len = sizeof(struct rppc_function) +
189 (ctx->num_xlts * sizeof(struct rppc_param_translation));
190 msg = (void *)calloc(len, sizeof(char));
192 if (msg == NULL) {
193 printf("MmRpc_call: Error: msg alloc failed\n");
194 status = MmRpc_E_FAIL;
195 goto leave;
196 }
198 /* copy function parameters into message */
199 rpfxn = (struct rppc_function *)msg;
200 rpfxn->fxn_id = ctx->fxn_id;
201 rpfxn->num_params = ctx->num_params;
203 for (i = 0; i < ctx->num_params; i++) {
204 param = &ctx->params[i];
206 switch (param->type) {
207 case MmRpc_ParamType_Scalar:
208 rpfxn->params[i].type = RPPC_PARAM_TYPE_ATOMIC;
209 rpfxn->params[i].size = param->param.scalar.size;
210 rpfxn->params[i].data = param->param.scalar.data;
211 rpfxn->params[i].base = 0;
212 rpfxn->params[i].reserved = 0;
213 break;
215 case MmRpc_ParamType_Ptr:
216 rpfxn->params[i].type = RPPC_PARAM_TYPE_PTR;
217 rpfxn->params[i].size = param->param.ptr.size;
218 rpfxn->params[i].data = param->param.ptr.addr;
219 rpfxn->params[i].base = param->param.ptr.addr;
220 rpfxn->params[i].reserved = param->param.ptr.handle;
221 break;
223 case MmRpc_ParamType_OffPtr:
224 rpfxn->params[i].type = RPPC_PARAM_TYPE_PTR;
225 rpfxn->params[i].size = param->param.offPtr.size;
226 rpfxn->params[i].data = param->param.offPtr.base +
227 param->param.offPtr.offset;
228 rpfxn->params[i].base = param->param.offPtr.base;
229 rpfxn->params[i].reserved = param->param.offPtr.handle;
230 break;
232 default:
233 printf("MmRpc_call: Error: invalid parameter type\n");
234 status = MmRpc_E_INVALIDPARAM;
235 goto leave;
236 break;
237 }
238 }
240 /* copy offset array into message */
241 rpfxn->num_translations = ctx->num_xlts;
243 for (i = 0; i < ctx->num_xlts; i++) {
244 /* pack the pointer translation entry */
245 rpfxn->translations[i].index = ctx->xltAry[i].index;
246 rpfxn->translations[i].offset = ctx->xltAry[i].offset;
247 rpfxn->translations[i].base = ctx->xltAry[i].base;
248 rpfxn->translations[i].reserved = ctx->xltAry[i].handle;
249 }
251 /* send message for remote execution */
252 status = write(obj->fd, msg, len);
254 if (status < 0) {
255 printf("MmRpc_call: Error: write failed\n");
256 status = MmRpc_E_FAIL;
257 goto leave;
258 }
260 /* wait for return status from remote service */
261 status = read(obj->fd, &reply_msg, sizeof(struct rppc_function_return));
263 if (status < 0) {
264 printf("MmRpc_call: Error: read failed\n");
265 status = MmRpc_E_FAIL;
266 goto leave;
267 }
268 else if (status != sizeof(struct rppc_function_return)) {
269 printf("MmRpc_call: Error: reply bytes=%d, expected %d\n",
270 status, sizeof(struct rppc_function_return));
271 status = MmRpc_E_FAIL;
272 goto leave;
273 }
274 else {
275 status = MmRpc_S_SUCCESS;
276 }
278 *ret = (int32_t)reply_msg.status;
280 leave:
281 if (msg != NULL) {
282 free(msg);
283 }
285 return(status);
286 }