]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - packages/ti/srvmgr/omaprpc/OmapRpc.c
Add MmServiceMgr_getId() to allow retrieval of service instance id
[ipc/ipcdev.git] / packages / ti / srvmgr / omaprpc / OmapRpc.c
1 /*
2  * Copyright (c) 2012-2014, 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 #include <xdc/std.h>
34 #include <xdc/cfg/global.h>
35 #include <xdc/runtime/System.h>
36 #include <xdc/runtime/Memory.h>
38 #include <ti/sysbios/BIOS.h>
39 #include <ti/sysbios/knl/Semaphore.h>
40 #include <ti/sysbios/knl/Task.h>
42 #include <ti/grcm/RcmTypes.h>
43 #include <ti/grcm/RcmServer.h>
45 #include <string.h>
46 #include <stdlib.h>
48 #include <ti/ipc/mm/MmType.h>
49 #include <ti/ipc/rpmsg/RPMessage.h>
50 #include <ti/ipc/rpmsg/NameMap.h>
51 #include <ti/srvmgr/ServiceMgr.h>
53 #include "OmapRpc.h"
55 typedef struct OmapRpc_Object {
56     Char                     channelName[OMAPRPC_MAX_CHANNEL_NAMELEN];
57     UInt16                   dstProc;
58     UInt32                   port;
59     RPMessage_Handle         msgq;
60     UInt32                   localEndPt;
61     Task_Handle              taskHandle;
62     Bool                     shutdown;
63     Semaphore_Handle         exitSem;
64     OmapRpc_SrvDelNotifyFxn  srvDelCB;
65     RcmServer_Params         rcmParams;
66     UInt32                   numFuncs;
67     OmapRpc_FuncSignature   *funcSigs;
68 } OmapRpc_Object;
70 Int32 OmapRpc_GetSvrMgrHandle(Void *srvc, Int32 num, Int32 *params)
71 {
72     System_printf("OMAPRPC: Calling RCM Service Manager Create Function!\n");
73     return 0;
74 }
76 static Void omapRpcTask(UArg arg0, UArg arg1)
77 {
78     OmapRpc_Object *obj = (OmapRpc_Object *)arg0;
79     UInt32 local;
80     UInt32 remote;
81     Int status;
82     UInt16 len;
83     Char  * msg = NULL;
84     OmapRpc_MsgHeader *hdr = NULL;
86     msg = Memory_alloc(NULL, 512, 0, NULL); /* maximum rpmsg size is probably
87                                              * smaller, but we need to
88                                              * be cautious */
89     if (msg == NULL) {
90         System_printf("OMAPRPC: Failed to allocate msg!\n");
91         return;
92     }
94     hdr = (OmapRpc_MsgHeader *)&msg[0];
96     if (obj == NULL) {
97         System_printf("OMAPRPC: Failed to start task as arguments are NULL!\n");
98         return;
99     }
101     if (obj->msgq == NULL) {
102         System_printf("OMAPRPC: Failed to start task as MessageQ was NULL!\n");
103         return;
104     }
106     /* get the local endpoint we are to use */
107     local = obj->localEndPt;
108     System_printf("OMAPRPC: connecting from local endpoint %u to port %u\n",
109                         obj->localEndPt, obj->port);
111     NameMap_register("rpmsg-rpc", obj->channelName, obj->port);
113     System_printf("OMAPRPC: started channel on port: %d\n", obj->port);
115     while (!obj->shutdown) {
117         /* clear out the message and our message vars */
118         _memset(msg, 0, sizeof(msg));
119         len = 0;
120         remote = 0;
122         /* receive the message */
123         status = RPMessage_recv(obj->msgq, (Ptr)msg, &len, &remote,
124                                         RPMessage_FOREVER);
126         if (status == RPMessage_E_UNBLOCKED) {
127             System_printf("OMAPRPC: unblocked while waiting for messages\n");
128             continue;
129         }
131         System_printf("OMAPRPC: received msg type: %d len: %d from addr: %d\n",
132                              hdr->msgType, len, remote);
133         switch (hdr->msgType) {
134             case OmapRpc_MsgType_CREATE_INSTANCE:
135             {
136                 /* temp pointer to payload */
137                 OmapRpc_CreateInstance *create =
138                                 OmapRpc_PAYLOAD(msg, OmapRpc_CreateInstance);
139                 /* local copy of name */
140                 Char name[sizeof(create->name)];
141                 /* return structure */
142                 OmapRpc_InstanceHandle *handle =
143                                 OmapRpc_PAYLOAD(msg, OmapRpc_InstanceHandle);
144                 /* save a copy of the input structure data */
145                 _strcpy(name, create->name);
146                 /* clear out the old data */
147                 _memset(msg, 0, sizeof(msg));
149                 /* create a new instance of the service */
150                 handle->status = ServiceMgr_createService(name,
151                                                 &handle->endpointAddress);
153                 System_printf("OMAPRPC: created service instance named: %s "
154                               "(status=%d) addr: %d\n", name, handle->status,
155                                 handle->endpointAddress);
157                 hdr->msgType = OmapRpc_MsgType_INSTANCE_CREATED;
158                 hdr->msgLen  = sizeof(OmapRpc_InstanceHandle);
159                 break;
160             }
161             case OmapRpc_MsgType_DESTROY_INSTANCE:
162             {
163                 /* temp pointer to payload */
164                 OmapRpc_InstanceHandle *handle =
165                                 OmapRpc_PAYLOAD(msg, OmapRpc_InstanceHandle);
167                 /* Stash away endpointAddress for MmServiceMgr_getId to work */
168                 Task_setEnv(Task_self(), (Ptr)handle->endpointAddress);
170                 if (obj->srvDelCB != NULL) {
171                     obj->srvDelCB();
172                 }
174                 Task_setEnv(Task_self(), NULL);
176                 /* don't clear out the old data... */
177                 System_printf("OMAPRPC: destroying instance addr: %d\n",
178                                                     handle->endpointAddress);
179                 handle->status = ServiceMgr_deleteService(
180                                                     handle->endpointAddress);
181                 hdr->msgType = OmapRpc_MsgType_INSTANCE_DESTROYED;
182                 hdr->msgLen = sizeof(OmapRpc_InstanceHandle);
183                 /* leave the endpoint address alone. */
184                 break;
185             }
186             case OmapRpc_MsgType_QUERY_CHAN_INFO:
187             {
188                 OmapRpc_ChannelInfo *chanInfo =
189                                       OmapRpc_PAYLOAD(msg, OmapRpc_ChannelInfo);
191                 chanInfo->numFuncs = obj->numFuncs;
192                 System_printf("OMAPRPC: channel info query - name %s fxns %u\n",
193                                             obj->channelName, chanInfo->numFuncs);
194                 hdr->msgType = OmapRpc_MsgType_CHAN_INFO;
195                 hdr->msgLen = sizeof(OmapRpc_ChannelInfo);
196                 break;
197             }
198             case OmapRpc_MsgType_QUERY_FUNCTION:
199             {
200                 OmapRpc_QueryFunction *fxnInfo = OmapRpc_PAYLOAD(msg,
201                                                          OmapRpc_QueryFunction);
202                 System_printf("OMAPRPC: function query of type %u\n",
203                               fxnInfo->infoType);
205                 switch (fxnInfo->infoType)
206                 {
207                     case OmapRpc_InfoType_FUNC_SIGNATURE:
208                         if (fxnInfo->funcIndex < obj->numFuncs)
209                             memcpy(&fxnInfo->info.signature,
210                                    &obj->funcSigs[fxnInfo->funcIndex],
211                                    sizeof(OmapRpc_FuncSignature));
212                         break;
213                     case OmapRpc_InfoType_FUNC_PERFORMANCE:
214                         break;
215                     case OmapRpc_InfoType_NUM_CALLS:
216                         break;
217                     default:
218                         System_printf("OMAPRPC: Invalid info type!\n");
219                         break;
220                 }
221                 hdr->msgType = OmapRpc_MsgType_FUNCTION_INFO;
222                 hdr->msgLen = sizeof(OmapRpc_QueryFunction);
223                 break;
224             }
225             default:
226             {
227                 /* temp pointer to payload */
228                 OmapRpc_Error *err = OmapRpc_PAYLOAD(msg, OmapRpc_Error);
230                 /* the debugging message before we clear */
231                 System_printf("OMAPRPC: unexpected msg type: %d\n",
232                               hdr->msgType);
234                 /* clear out the old data */
235                 _memset(msg, 0, sizeof(msg));
237                 hdr->msgType = OmapRpc_MsgType_ERROR;
238                 err->endpointAddress = local;
239                 err->status = OmapRpc_ErrorType_NOT_SUPPORTED;
240                 break;
241             }
242        }
244        /* compute the length of the return message. */
245        len = sizeof(struct OmapRpc_MsgHeader) + hdr->msgLen;
247        System_printf("OMAPRPC: Replying with msg type: %d to addr: %d "
248                       " from: %d len: %u\n", hdr->msgType, remote, local, len);
250        /* send the response. All messages get responses! */
251        RPMessage_send(obj->dstProc, remote, local, msg, len);
252     }
254     System_printf("OMAPRPC: destroying channel on port: %d\n", obj->port);
255     NameMap_unregister("rpmsg-rpc", obj->channelName, obj->port);
256     if (msg != NULL) {
257        Memory_free(NULL, msg, 512);
258     }
259     /* @TODO delete any outstanding ServiceMgr instances if no disconnect
260      * was issued? */
262     Semaphore_post(obj->exitSem);
265 /*
266  *  ======== OmapRpc_createChannel ========
267  */
268 OmapRpc_Handle OmapRpc_createChannel(String channelName, UInt16 dstProc,
269         UInt32 port, RcmServer_Params *rcmParams, MmType_FxnSigTab *fxnSigTab,
270         OmapRpc_SrvDelNotifyFxn srvDelCBFunc)
272     Task_Params taskParams;
273     UInt32      func;
275     OmapRpc_Object *obj = Memory_alloc(NULL, sizeof(OmapRpc_Object), 0, NULL);
277     if (obj == NULL) {
278         System_printf("OMAPRPC: Failed to allocate memory for object!\n");
279         goto unload;
280     }
281     _memset(obj, 0, sizeof(OmapRpc_Object));
282     obj->numFuncs = fxnSigTab->count + 1;
283 #if 0
284     RcmServer_Params_init(&obj->rcmParams);
285     obj->rcmParams.priority = Thread_Priority_ABOVE_NORMAL;
286     obj->rcmParams.stackSize = 0x1000;
287     obj->rcmParams.fxns.length = obj->numFuncs;
288     obj->rcmParams.fxns.elem = Memory_alloc(NULL,
289             sizeof(RcmServer_FxnDesc) * obj->numFuncs, 0, NULL);
291     if (obj->rcmParams.fxns.elem == NULL) {
292         System_printf("OMAPRPC: Failed to allocate RCM function list!\n");
293         goto unload;
294     }
295 #else
296     memcpy(&obj->rcmParams, rcmParams, sizeof(RcmServer_Params));
297     obj->rcmParams.fxns.length = obj->numFuncs;
298     obj->rcmParams.fxns.elem = Memory_calloc(NULL, obj->numFuncs *
299             sizeof(RcmServer_FxnDesc), 0, NULL);
301     if (obj->rcmParams.fxns.elem == NULL) {
302         System_printf("OMAPRPC: Failed to allocate RCM function list!\n");
303         goto unload;
304     }
305 #endif
307     /* setup other variables... */
308     obj->shutdown = FALSE;
309     obj->dstProc = dstProc;
310     obj->port = port;
311     strncpy(obj->channelName, channelName, OMAPRPC_MAX_CHANNEL_NAMELEN-1);
312     obj->channelName[OMAPRPC_MAX_CHANNEL_NAMELEN-1]='\0';
313     obj->srvDelCB = srvDelCBFunc;
314     obj->funcSigs = Memory_alloc(NULL, obj->numFuncs *
315             sizeof(OmapRpc_FuncSignature), 0, NULL);
317     if (obj->funcSigs == NULL) {
318         System_printf("OMAPRPC: Failed to allocate signtures list!\n");
319         goto unload;
320     }
322     /* setup the RCM functions and Signatures */
323     for (func = 0; func < obj->numFuncs; func++) {
324         if (func == 0) {
325             /* assign the "first function" */
326             obj->rcmParams.fxns.elem[func].name =
327                     OmapRpc_Stringerize(OmapRpc_GetSvrMgrHandle);
328             obj->rcmParams.fxns.elem[func].addr.createFxn =
329                     (RcmServer_MsgCreateFxn)OmapRpc_GetSvrMgrHandle;
331             strncpy(obj->funcSigs[func].name, obj->rcmParams.fxns.elem[0].name,
332                     OMAPRPC_MAX_CHANNEL_NAMELEN);
333             obj->funcSigs[func].numParam = 0;
334         }
335         else {
336             /* assign the other functions */
337 /*          obj->rcmParams.fxns.elem[func].name = fxns[func-1].signature.name; */
338             obj->rcmParams.fxns.elem[func].name =
339                     rcmParams->fxns.elem[func-1].name;
340 /*          obj->rcmParams.fxns.elem[func].addr.fxn =
341                     (RcmServer_MsgFxn)fxns[func-1].function; */
342             obj->rcmParams.fxns.elem[func].addr.fxn =
343                     rcmParams->fxns.elem[func-1].addr.fxn;
345             /* copy the signature */
346 /*          memcpy(&obj->funcSigs[func], &fxns[func-1].signature,
347                     sizeof(OmapRpc_FuncSignature)); */
348             memcpy(&obj->funcSigs[func], &fxnSigTab->table[func - 1],
349                     sizeof(MmType_FxnSig));
350         }
351     }
353     ServiceMgr_init();
355     if (ServiceMgr_register(channelName, &obj->rcmParams) == TRUE) {
356         System_printf("OMAPRPC: registered channel: %s\n", obj->channelName);
358         obj->msgq = RPMessage_create(obj->port, NULL, NULL,&obj->localEndPt);
360         if (obj->msgq == NULL) {
361             goto unload;
362         }
364         Task_Params_init(&taskParams);
365         taskParams.instance->name = channelName;
366         taskParams.stackSize = 0x2000; /* must cover all proxy stack usage */
367         taskParams.priority = 1;   /* lowest priority thread */
368         taskParams.arg0 = (UArg)obj;
370         obj->exitSem = Semaphore_create(0, NULL, NULL);
372         if (obj->exitSem == NULL) {
373             goto unload;
374         }
376         obj->taskHandle = Task_create(omapRpcTask, &taskParams, NULL);
378         if (obj->taskHandle == NULL) {
379             goto unload;
380         }
381     }
382     else {
383         System_printf("OMAPRPC: FAILED to register channel: %s\n",
384                 obj->channelName);
385     }
387     System_printf("OMAPRPC: Returning Object %p\n", obj);
388     return(obj);
390 unload:
391     OmapRpc_deleteChannel(obj);
392     return(NULL);
395 Int OmapRpc_deleteChannel(OmapRpc_Handle handle)
397     OmapRpc_Object *obj = (OmapRpc_Object *)handle;
399     if (obj == NULL) {
400         return OmapRpc_E_FAIL;
401     }
403     System_printf("OMAPRPC: deleting channel %s\n", obj->channelName);
404     obj->shutdown = TRUE;
405     if (obj->msgq) {
406         RPMessage_unblock(obj->msgq);
407         if (obj->exitSem) {
408             Semaphore_pend(obj->exitSem, BIOS_WAIT_FOREVER);
409             RPMessage_delete(&obj->msgq);
410             Semaphore_delete(&obj->exitSem);
411         }
412         if (obj->taskHandle) {
413             Task_delete(&obj->taskHandle);
414         }
415     }
416     if (obj->funcSigs)
417         Memory_free(NULL, obj->funcSigs, sizeof(*obj->funcSigs)*(obj->numFuncs-1));
418     if (obj->rcmParams.fxns.elem)
419         Memory_free(NULL, obj->rcmParams.fxns.elem,
420                     sizeof(obj->rcmParams.fxns.elem[0])*obj->numFuncs);
422     Memory_free(NULL, obj, sizeof(*obj));
423     return OmapRpc_S_SUCCESS;
426 #if 0
427 /*
428  *  ======== OmapRpc_start ========
429  */
430 Int OmapRpc_start(const String name, Int port, Int aryLen,
431         OmapRpc_FuncSignature *sigAry)
433     Int status = OmapRpc_S_SUCCESS;
434     Task_Params taskParams;
435     OmapRpc_Object *obj;
437     /* create an instance */
438     obj = Memory_calloc(NULL, sizeof(OmapRpc_Object), 0, NULL);
440     if (obj == NULL) {
441         System_printf("OMAPRPC: Failed to allocate memory for object!\n");
442         status = OmapRpc_E_FAIL;
443         goto leave;
444     }
446     obj->numFuncs = aryLen + 1;
447     obj->shutdown = FALSE;
448     obj->dstProc = MultiProc_getId("HOST");
449     obj->port = port;
450     strncpy(obj->channelName, name, OMAPRPC_MAX_CHANNEL_NAMELEN-1);
451     obj->channelName[OMAPRPC_MAX_CHANNEL_NAMELEN-1]='\0';
452     obj->srvDelCB = srvDelCBFunc;
453     obj->funcSigs = Memory_alloc(NULL,
454             sizeof(OmapRpc_FuncSignature) * obj->numFuncs, 0, NULL);
456     if (obj->funcSigs == NULL) {
457         System_printf("OMAPRPC: Failed to allocate signtures list!\n");
458         goto unload;
459     }
461     /* setup the functions and signatures */
462     for (func = 0; func < obj->numFuncs; func++) {
463         if (func == 0) {
464             /* assign the "first function" */
465 /*          strncpy(obj->funcSigs[func].name, obj->rcmParams.fxns.elem[0].name,
466                     OMAPRPC_MAX_CHANNEL_NAMELEN); */
467             strncpy(obj->funcSigs[func].name,
468                     OmapRpc_Stringerize(OmapRpc_GetSvrMgrHandle),
469                     OMAPRPC_MAX_CHANNEL_NAMELEN);
470             obj->funcSigs[func].numParam = 0;
471         }
472         else {
473             /* copy the signature */
474             memcpy(&obj->funcSigs[func], &fxns[func-1].signature,
475                     sizeof(OmapRpc_FuncSignature));
476         }
477     }
479     obj->msgq = RPMessage_create(obj->port, NULL, NULL,&obj->localEndPt);
481     if (obj->msgq == NULL) {
482         goto unload;
483     }
485     Task_Params_init(&taskParams);
486     taskParams.instance->name = channelName;
487     taskParams.priority = 1;   /* Lowest priority thread */
488     taskParams.arg0 = (UArg)obj;
490     obj->exitSem = Semaphore_create(0, NULL, NULL);
492     if (obj->exitSem == NULL) {
493         goto unload;
494     }
496     obj->taskHandle = Task_create(omapRpcTask, &taskParams, NULL);
498     if (obj->taskHandle == NULL) {
499         goto unload;
500     }
502     System_printf("OMAPRPC: Returning Object %p\n", obj);
503     return(obj);
505 leave:
506     if (status < 0) {
507         OmapRpc_deleteChannel(obj);
508     }
509     return(status);
511 #endif