1 /*
2 * Copyright (c) 2017, 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 * ======== Server.c ========
35 *
36 */
38 /* this define must precede inclusion of any xdc header file */
39 #define Registry_CURDESC Test__Desc
40 #define MODULE_NAME "Server"
42 /* xdctools header files */
43 #include <xdc/std.h>
44 #include <xdc/runtime/Assert.h>
45 #include <xdc/runtime/Diags.h>
46 #include <xdc/runtime/Log.h>
47 #include <xdc/runtime/Registry.h>
49 #include <stdio.h>
51 /* package header files */
52 #include <ti/ipc/MessageQ.h>
53 #include <ti/ipc/MultiProc.h>
54 #include <ti/ipc/SharedRegion.h>
55 #include <ti/ipc/HeapMemMP.h>
56 #include <ti/sysbios/hal/Cache.h>
57 #include <xdc/runtime/IHeap.h>
58 #include <xdc/runtime/Memory.h>
60 #include <ti/sysbios/BIOS.h>
61 #include <ti/sysbios/knl/Task.h>
63 /* local header files */
64 #include "../shared/AppCommon.h"
66 /* module header file */
67 #include "Server.h"
69 /* module structure */
70 typedef struct {
71 UInt16 hostProcId; // host processor id
72 MessageQ_Handle slaveQue; // created locally
73 } Server_Module;
75 /* private data */
76 Registry_Desc Registry_CURDESC;
77 static Server_Module Module;
79 /*
80 * ======== Server_init ========
81 */
82 Void Server_init(Void)
83 {
84 Registry_Result result;
86 /* register with xdc.runtime to get a diags mask */
87 result = Registry_addModule(&Registry_CURDESC, MODULE_NAME);
88 Assert_isTrue(result == Registry_SUCCESS, (Assert_Id)NULL);
90 /* initialize module object state */
91 Module.hostProcId = MultiProc_getId("HOST");
92 }
94 /*
95 * ======== Server_create ========
96 */
97 Int Server_create()
98 {
99 Int status = 0;
100 MessageQ_Params msgqParams;
101 char msgqName[32];
103 /* enable some log events */
104 Diags_setMask(MODULE_NAME"+EXF");
106 /* create local message queue (inbound messages) */
107 MessageQ_Params_init(&msgqParams);
108 sprintf(msgqName, App_SlaveMsgQueName, MultiProc_getName(MultiProc_self()));
109 Module.slaveQue = MessageQ_create(msgqName, &msgqParams);
111 if (Module.slaveQue == NULL) {
112 status = -1;
113 goto leave;
114 }
116 Log_print0(Diags_INFO,"Server_create: server is ready");
118 leave:
119 Log_print1(Diags_EXIT, "<-- Server_create: %d", (IArg)status);
120 return (status);
121 }
123 /*
124 * ======== Server_exec ========
125 */
126 Int Server_exec()
127 {
128 Int status;
129 Bool running = TRUE;
130 App_Msg * msg;
131 MessageQ_QueueId queId;
132 UInt16 regionId;
133 IHeap_Handle srHeap;
134 Memory_Stats stats;
135 HeapMemMP_ExtendedStats extStats;
136 Uint32 *bigDataLocalPtr;
137 Int j;
138 UInt32 errorCount=0;
139 Int retVal;
140 bigDataLocalDesc_t bigDataLocalDesc;
142 Log_print0(Diags_ENTRY | Diags_INFO, "--> Server_exec:");
144 while (running) {
146 /* wait for inbound message */
147 status = MessageQ_get(Module.slaveQue, (MessageQ_Msg *)&msg,
148 MessageQ_FOREVER);
150 if (status < 0) {
151 goto leave;
152 }
153 Log_print1(Diags_ENTRY | Diags_INFO, "Message received...%d", msg->id);
154 switch (msg->cmd) {
155 case App_CMD_SHARED_REGION_INIT:
156 regionId = msg->regionId;
157 /* NOTE: Alternatively the Shared region can be created here by using id, base, length right here */
158 Log_print1(Diags_INFO, "Server_taskFxn: SR_1 region Id=%d",msg->regionId);
159 srHeap = (IHeap_Handle)SharedRegion_getHeap(regionId);
160 if (srHeap != NULL)
161 {
162 HeapMemMP_getStats((HeapMemMP_Handle)srHeap, &stats);
163 Log_print3(Diags_INFO, "Server_taskFxn: SR_1 heap, totalSize=%d,totalFreeSize=%d,largestFreeSize=%d", (IArg)stats.totalSize, (IArg)stats.totalFreeSize, (IArg)stats.largestFreeSize);
164 HeapMemMP_getExtendedStats((HeapMemMP_Handle)srHeap, &extStats);
165 Log_print2(Diags_INFO, "Server_taskFxn: SR_1 heap, buf=0x%x,size=%d", (IArg)extStats.buf, (IArg)extStats.size);
166 }
167 else
168 {
169 Log_print0(Diags_INFO, "Server_taskFxn: SR_1, srHeap=NULL");
170 }
171 break;
173 case App_CMD_BIGDATA:
174 #ifdef DEBUG
175 Log_print1(Diags_ENTRY | Diags_INFO, "msg->cmd=App_CMD_BIGDATA,msg->ptr=0x%x",
176 (IArg)msg->u.bigDataSharedDesc.sharedPtr);
177 #endif
179 /* Translate to local descriptor */
180 retVal = bigDataXlatetoLocalAndSync(msg->regionId,
181 &msg->u.bigDataSharedDesc, &bigDataLocalDesc);
182 if (retVal) {
183 status = -1;
184 goto leave;
185 }
186 bigDataLocalPtr = (Uint32 *)bigDataLocalDesc.localPtr;
187 #ifdef DEBUG
188 /* print message from buffer */
189 Log_print1(Diags_INFO, " Received message %d", msg->id);
190 Log_print1(Diags_INFO, " Local Pointer 0x%x", (UInt32)bigDataLocalPtr);
191 Log_print0(Diags_INFO, " First 8 bytes: ");
192 for ( j = 0; j < 8 && j < bigDataLocalDesc.size/sizeof(uint32_t); j+=4)
193 Log_print4(Diags_INFO, "0x%x, 0x%x, 0x%x, 0x%x",
194 bigDataLocalPtr[j], bigDataLocalPtr[j+1], bigDataLocalPtr[j+2], bigDataLocalPtr[j+3]);
195 Log_print0(Diags_INFO, " Last 8 bytes: ");
196 for ( j = (bigDataLocalDesc.size/sizeof(uint32_t))-8 ;
197 j < bigDataLocalDesc.size/sizeof(uint32_t); j+=4)
198 Log_print4(Diags_INFO, "0x%x, 0x%x, 0x%x, 0x%x",
199 bigDataLocalPtr[j], bigDataLocalPtr[j+1], bigDataLocalPtr[j+2], bigDataLocalPtr[j+3]);
200 #endif
201 /* Check values to see expected results */
202 for( j=0; j < bigDataLocalDesc.size/sizeof(uint32_t); j++) {
203 if ( bigDataLocalPtr[j] != (msg->id+j) ) {
204 errorCount++;
205 }
206 }
208 /* Fill new data */
209 for ( j=0; j < bigDataLocalDesc.size/sizeof(uint32_t); j++)
210 bigDataLocalPtr[j] = msg->id + 10 +j;
212 /* Translate to Shared Descriptor and Sync */
213 retVal = bigDataXlatetoGlobalAndSync(msg->regionId,
214 &bigDataLocalDesc, &msg->u.bigDataSharedDesc);
215 if (retVal) {
216 status = -1;
217 goto leave;
218 }
219 break;
221 case App_CMD_SHUTDOWN:
222 running = FALSE;
223 break;
225 default:
226 break;
227 }
229 /* process the message */
230 Log_print2(Diags_INFO, "Server_exec: processed id %d, cmd=0x%x", msg->id, msg->cmd);
232 /* send message back */
233 queId = MessageQ_getReplyQueue(msg);
234 MessageQ_put(queId, (MessageQ_Msg)msg);
235 } /* while (running) */
237 leave:
238 /* Print error count if non-zero */
239 if (errorCount) {
240 Log_print1(Diags_INFO, "Server_exec: Error Count %d", errorCount);
241 status = -1;
242 }
243 else
244 Log_print0(Diags_INFO, "Server_exec: Data check clean");
246 Log_print1(Diags_EXIT, "<-- Server_exec: %d", (IArg)status);
247 return(status);
248 }
250 /*
251 * ======== Server_delete ========
252 */
254 Int Server_delete()
255 {
256 Int status;
258 Log_print0(Diags_ENTRY, "--> Server_delete:");
260 /* delete the video message queue */
261 status = MessageQ_delete(&Module.slaveQue);
263 if (status < 0) {
264 goto leave;
265 }
267 leave:
268 if (status < 0) {
269 Log_error1("Server_finish: error=0x%x", (IArg)status);
270 }
272 /* disable log events */
273 Log_print1(Diags_EXIT, "<-- Server_delete: %d", (IArg)status);
274 Diags_setMask(MODULE_NAME"-EXF");
276 return(status);
277 }
279 /*
280 * ======== Server_exit ========
281 */
283 Void Server_exit(Void)
284 {
285 /*
286 * Note that there isn't a Registry_removeModule() yet:
287 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=315448
288 *
289 * ... but this is where we'd call it.
290 */
291 }