1
2 //=============================================================================
3 // Includs
4 //=============================================================================
5
6 #include <stdio.h>
7 #include <string.h>
8
9 #include "drvMBX.h"
10 #include "apiMBX.h"
11 #include "MsOS.h"
12
13
14 //=============================================================================
15 // Compile options
16 //=============================================================================
17
18 //=============================================================================
19 // Local Defines
20 //=============================================================================
21
22 #define MAX_DMBX_CLASS 20
23 #define MAX_DMBX_NAME_LEN (MAX_MBX_PARAM_SIZE-1)
24
25 //=============================================================================
26 // Debug Macros
27 //=============================================================================
28
29 //=============================================================================
30 // Macros
31 //=============================================================================
32
33 //=============================================================================
34 // Local Variables
35 //=============================================================================
36
37 //Record Dynamic Register Mailbox Class
38 typedef struct _Dynmaic_MBX_Class
39 {
40 char name[MAX_DMBX_NAME_LEN];
41 MS_U16 mbx_class;
42 } Dynamic_MBX_Class;
43
44 typedef enum _Dynamic_MBX_Index
45 {
46 E_Dynamic_GenerateClass = 0,
47 E_Dynamic_QueryCalsss = 1,
48 } Dynamic_MBX_Index;
49
50 //=============================================================================
51 // Global Variables
52 //=============================================================================
53
54 static MS_U8 Count_DMBX_Class = 0;
55 static Dynamic_MBX_Class Tab_DMBX_Class[MAX_DMBX_CLASS];
56
57 #ifndef ANDROID
58 #define VPRINTF printf
59 #else
60 #include <sys/mman.h>
61 #include <cutils/ashmem.h>
62 #include <cutils/log.h>
63
64 #define VPRINTF //ALOGD
65 #endif
66
67
68 #if 0//def MSOS_TYPE_LINUX
69
70 MS_U32 s32APIMutexMBX;
71
72
73 #define API_MBX_InitLock() {\
74 s32APIMutexMBX = MsOS_CreateMutex(E_MSOS_FIFO, "MBX_Send_Mutex", MSOS_PROCESS_SHARED);\
75 if(s32APIMutexMBX<0)\
76 {\
77 printf("!!!Acquire Send Mutex for MBX Failed!\n");\
78 return E_MBX_UNKNOW_ERROR;\
79 }\
80 }
81
82 #define API_MBX_DeInitLock() {\
83 MsOS_DeleteMutex(s32APIMutexMBX);\
84 }
85
86 #define API_MBX_LockSend() {\
87 {\
88 MsOS_ObtainMutex(s32APIMutexMBX, MSOS_WAIT_FOREVER);\
89 }\
90 }
91
92 #define API_MBX_UnLockSend() {\
93 {\
94 MsOS_ReleaseMutex(s32APIMutexMBX);\
95 }\
96 }
97
98 #else
99 #define API_MBX_InitLock()
100 #define API_MBX_DeInitLock()
101 #define API_MBX_LockSend()
102 #define API_MBX_UnLockSend()
103 #endif
104
105
106 //=============================================================================
107 // Local Function Prototypes
108 //=============================================================================
109
_MApi_Search_ClassNum(char * name,MS_U8 * pmbx_class)110 static MBX_Result _MApi_Search_ClassNum(char *name, MS_U8 *pmbx_class)
111 {
112 MS_U16 i = 0;
113
114 *pmbx_class = 0x00;
115
116 //judge if name length exceed MAX_DMBX_NAME_LEN or NULL
117 if ((name==NULL) || (strlen(name)>(MAX_DMBX_NAME_LEN-1)))
118 return E_MBX_UNKNOW_ERROR;
119
120 //Search Dynamic Class Table
121 for (i = 0; i < Count_DMBX_Class; i++)
122 {
123 if (strncmp(Tab_DMBX_Class[i].name, name, MAX_DMBX_NAME_LEN) == 0)
124 {
125 *pmbx_class = Tab_DMBX_Class[i].mbx_class;
126 return E_MBX_SUCCESS;
127 }
128 }
129
130 return E_MBX_UNKNOW_ERROR;
131
132 }
133
134
_MApi_Conduct_GenerateClass(MBX_Msg * pMsg)135 static void _MApi_Conduct_GenerateClass(MBX_Msg *pMsg)
136 {
137 //prepare receive mailbox transmit class & name info
138 int flag_repeat = 0;
139 int i;
140 char temp_name[MAX_DMBX_NAME_LEN] = {0};
141 MS_U8 temp_class = 0;
142
143 if (pMsg->u8ParameterCount > 0)
144 {
145 temp_class = pMsg->u8Parameters[0];
146 for (i = 1; i<pMsg->u8ParameterCount; i++)
147 {
148 temp_name[i-1] = pMsg->u8Parameters[i];
149 }
150 //printf("%s, temp_class=0x%x, temp_name=%s\n", __FUNCTION__, temp_class, temp_name);
151 }
152 else
153 {
154 //printf("Error Send Message\n");
155 return;
156 }
157
158 //Search Dynamic Class Table
159 for (i = 0; i < Count_DMBX_Class; i++)
160 {
161 if (strncmp(Tab_DMBX_Class[i].name, temp_name, MAX_DMBX_NAME_LEN) == 0)
162 {
163 flag_repeat = 1;
164 break;
165 }
166 }
167
168 if (flag_repeat == 0)
169 {
170 Tab_DMBX_Class[Count_DMBX_Class].name[0] = 0; //clear name string
171
172 Tab_DMBX_Class[Count_DMBX_Class].mbx_class = temp_class;
173 strncpy(Tab_DMBX_Class[Count_DMBX_Class].name, temp_name, MAX_DMBX_NAME_LEN);
174 Count_DMBX_Class++;
175 }
176
177 MBX_Msg MB_Command;
178
179 memset((void*)&MB_Command, 0, sizeof(MBX_Msg));
180 MB_Command.eRoleID = E_MBX_CPU_MIPS;//E_MBX_CPU_R2M;//E_MBX_ROLE_CP;
181 MB_Command.eMsgType = E_MBX_MSG_TYPE_INSTANT;
182 MB_Command.u8Ctrl = 0;
183 MB_Command.u8MsgClass = E_MBX_CLASS_SECURE_WAIT; // E_MBX_CLASS_PM_NOWAIT;
184 MB_Command.u8Index = 0;//PM_CMDIDX_GLOBAL_CHIP_RESET;
185 MB_Command.u8ParameterCount = 1;
186 MB_Command.u8Parameters[0] = flag_repeat;
187
188 MApi_MBX_SendMsg(&MB_Command);
189
190 }
191
_MApi_Conduct_QueryClass(MBX_Msg * pMsg)192 static void _MApi_Conduct_QueryClass(MBX_Msg *pMsg)
193 {
194 int i;
195 char temp_name[MAX_DMBX_NAME_LEN] = {0};
196 MS_U8 temp_class = 0;
197
198 if (pMsg->u8ParameterCount > 0)
199 {
200 for (i = 1; i<pMsg->u8ParameterCount; i++)
201 {
202 temp_name[i-1] = pMsg->u8Parameters[i];
203 }
204 //printf("%s, temp_class=0x%x, temp_name=%s\n", __FUNCTION__, temp_class, temp_name);
205 }
206 else
207 {
208 //printf("Error Send Message\n");
209 return;
210 }
211
212 _MApi_Search_ClassNum(temp_name, &temp_class);
213
214 MBX_Msg MB_Command;
215
216 memset((void*)&MB_Command, 0, sizeof(MBX_Msg));
217 MB_Command.eRoleID = E_MBX_CPU_MIPS;//E_MBX_CPU_R2M;//E_MBX_ROLE_CP;
218 MB_Command.eMsgType = E_MBX_MSG_TYPE_INSTANT;
219 MB_Command.u8Ctrl = 0;
220 MB_Command.u8MsgClass = E_MBX_CLASS_SECURE_WAIT; // E_MBX_CLASS_PM_NOWAIT;
221 MB_Command.u8Index = 2;//PM_CMDIDX_GLOBAL_CHIP_RESET;
222 MB_Command.u8ParameterCount = 1;
223 MB_Command.u8Parameters[0] = temp_class;
224
225 MApi_MBX_SendMsg(&MB_Command);
226
227 }
228
229
_MApi_HandleDynamicMBX(MBX_Msg * pMsg,MS_BOOL * pbAddToQueue)230 static void _MApi_HandleDynamicMBX(MBX_Msg *pMsg, MS_BOOL *pbAddToQueue)
231 {
232
233 *pbAddToQueue = FALSE;
234
235 switch (pMsg->u8Index)
236 {
237 case E_Dynamic_GenerateClass:
238 _MApi_Conduct_GenerateClass(pMsg);
239 break;
240 case E_Dynamic_QueryCalsss:
241 _MApi_Conduct_QueryClass(pMsg);
242 break;
243 default:
244 return ;
245 }
246 }
247
248 MS_BOOL _gbFlagReg_Q = FALSE;
249 MS_BOOL _gbFlagReg_S = FALSE;
250 extern MBX_Result _MDrv_MBX_Init_(MBX_CPU_ID eHKCPU, MBX_ROLE_ID eHostRole, MS_U32 u32TimeoutMillSecs);
251
MApi_MBX_ISMsQEmpty(MBX_Class eTargetClass)252 static MS_BOOL MApi_MBX_ISMsQEmpty(MBX_Class eTargetClass)
253 {
254
255 MBX_MSGQ_Status eMsgQStatus;
256 MBX_Result ret = E_MBX_SUCCESS;
257
258 ret = MDrv_MBX_GetMsgQueueStatus(eTargetClass, &eMsgQStatus);
259
260 if (ret != E_MBX_SUCCESS)
261 return TRUE;
262
263 return (((eMsgQStatus.u32NormalMsgCount+eMsgQStatus.u32InstantMsgCount) > 0)? FALSE:TRUE);
264 }
265
266 #define Wait_MSQ_Empty(eTargetClass) { \
267 while(MApi_MBX_ISMsQEmpty(eTargetClass) == FALSE); \
268 }
269
MApi_MBX_RegisterQueryMBX(void)270 static MBX_Result MApi_MBX_RegisterQueryMBX(void)
271 {
272 MBX_Result ret = E_MBX_SUCCESS;
273
274 if (_gbFlagReg_Q == FALSE)
275 {
276 Wait_MSQ_Empty(E_MBX_CLASS_QUERY_CLASS);
277 ret = MApi_MBX_RegisterMSGWithCallBack(E_MBX_CLASS_QUERY_CLASS, 5, _MApi_HandleDynamicMBX);
278
279 if (ret == E_MBX_SUCCESS)
280 {
281 _gbFlagReg_Q = TRUE;
282 }
283 else
284 {
285 return ret;
286 }
287 }
288
289 if (_gbFlagReg_S == FALSE)
290 {
291 Wait_MSQ_Empty(E_MBX_CLASS_SECURE_WAIT);
292 ret = MApi_MBX_RegisterMSG(E_MBX_CLASS_SECURE_WAIT, 10);
293
294 if (ret == E_MBX_SUCCESS)
295 {
296 _gbFlagReg_S = TRUE;
297 }
298 else
299 {
300 return ret;
301 }
302 }
303
304 return ret;
305
306 }
307
308
309
MApi_MBX_UnRegisterQueryMBX(void)310 static MBX_Result MApi_MBX_UnRegisterQueryMBX(void)
311 {
312 MBX_Result ret = E_MBX_SUCCESS;
313
314 if (_gbFlagReg_Q == TRUE)
315 {
316 Wait_MSQ_Empty(E_MBX_CLASS_QUERY_CLASS);
317 ret = MApi_MBX_UnRegisterMSG(E_MBX_CLASS_QUERY_CLASS, TRUE);
318 if (ret == E_MBX_SUCCESS)
319 {
320 _gbFlagReg_Q = FALSE;
321 }
322 else
323 {
324 return ret;
325 }
326 }
327
328
329 if (_gbFlagReg_S == TRUE)
330 {
331 Wait_MSQ_Empty(E_MBX_CLASS_SECURE_WAIT);
332 ret = MApi_MBX_UnRegisterMSG(E_MBX_CLASS_SECURE_WAIT, TRUE);
333
334 if (ret == E_MBX_SUCCESS)
335 {
336 _gbFlagReg_S = FALSE;
337 }
338 else
339 {
340 return ret;
341 }
342 }
343
344 return ret;
345
346 }
347
348
349
MApi_MBX_Init(MBX_CPU_ID eHKCPU,MBX_ROLE_ID eHostRole,MS_U32 u32TimeoutMillSecs)350 MBX_Result MApi_MBX_Init(MBX_CPU_ID eHKCPU, MBX_ROLE_ID eHostRole, MS_U32 u32TimeoutMillSecs)
351 {
352 MBX_Result ret;
353
354 ret = _MDrv_MBX_Init_(eHKCPU, eHostRole, u32TimeoutMillSecs);
355
356 API_MBX_InitLock();
357
358 #if defined(MSOS_TYPE_NUTTX)
359 if (ret == E_MBX_SUCCESS)
360 {
361 ret = MApi_MBX_RegisterQueryMBX();
362 }
363 #endif
364
365 return ret;
366 }
367
MApi_MBX_DeInit(MS_BOOL bForceDiscardPendingMsg)368 MBX_Result MApi_MBX_DeInit(MS_BOOL bForceDiscardPendingMsg)
369 {
370 return MDrv_MBX_DeInit(bForceDiscardPendingMsg);
371 }
372
MApi_MBX_RegisterMSG(MBX_Class eMsgClass,MS_U16 u16MsgQueueSize)373 MBX_Result MApi_MBX_RegisterMSG(MBX_Class eMsgClass, MS_U16 u16MsgQueueSize)
374 {
375 return MDrv_MBX_RegisterMSG(eMsgClass, u16MsgQueueSize);
376 }
377
MApi_MBX_RegisterMSGWithCallBack(MBX_Class eMsgClass,MS_U16 u16MsgQueueSize,MBX_MAIL_ARRIVE_NOTIFY_FUNC notifier)378 MBX_Result MApi_MBX_RegisterMSGWithCallBack(MBX_Class eMsgClass, MS_U16 u16MsgQueueSize, MBX_MAIL_ARRIVE_NOTIFY_FUNC notifier)
379 {
380 return MDrv_MBX_RegisterMSGWithCallBack(eMsgClass, u16MsgQueueSize, notifier);
381 }
382
383
MApi_MBX_UnRegisterMSG(MBX_Class eMsgClass,MS_BOOL bForceDiscardMsgQueue)384 MBX_Result MApi_MBX_UnRegisterMSG(MBX_Class eMsgClass, MS_BOOL bForceDiscardMsgQueue)
385 {
386 return MDrv_MBX_UnRegisterMSG(eMsgClass, bForceDiscardMsgQueue);
387 }
388
MApi_MBX_ClearMSG(MBX_Class eMsgClass)389 MBX_Result MApi_MBX_ClearMSG(MBX_Class eMsgClass)
390 {
391 return MDrv_MBX_ClearMSG(eMsgClass);
392 }
393
MApi_MBX_SendMsg(MBX_Msg * pMsg)394 MBX_Result MApi_MBX_SendMsg(MBX_Msg *pMsg)
395 {
396 return MDrv_MBX_SendMsg(pMsg);
397 }
398
MApi_MBX_GetMsgQueueStatus(MBX_Class eTargetClass,MBX_MSGQ_Status * pMsgQueueStatus)399 MBX_Result MApi_MBX_GetMsgQueueStatus(MBX_Class eTargetClass, MBX_MSGQ_Status *pMsgQueueStatus)
400 {
401 return MDrv_MBX_GetMsgQueueStatus(eTargetClass, pMsgQueueStatus);
402 }
403
MApi_MBX_RecvMsg(MBX_Class eTargetClass,MBX_Msg * pMsg,MS_U32 u32WaitMillSecs,MS_U32 u32Flag)404 MBX_Result MApi_MBX_RecvMsg(MBX_Class eTargetClass, MBX_Msg *pMsg, MS_U32 u32WaitMillSecs, MS_U32 u32Flag)
405 {
406 return MDrv_MBX_RecvMsg(eTargetClass, pMsg, u32WaitMillSecs, u32Flag);
407 }
408
MApi_MBX_CheckMsg(MBX_Class eTargetClass,MBX_Msg * pMsg,MS_U32 u32WaitMillSecs,MS_U32 u32Flag)409 MBX_Result MApi_MBX_CheckMsg(MBX_Class eTargetClass, MBX_Msg *pMsg, MS_U32 u32WaitMillSecs, MS_U32 u32Flag)
410 {
411 return MDrv_MBX_CheckMsg(eTargetClass, pMsg, u32WaitMillSecs, u32Flag);
412 }
413
MApi_MBX_SendMsgLoopback(MBX_Msg * pMsg,MBX_ROLE_ID eSrcRoleId)414 MBX_Result MApi_MBX_SendMsgLoopback(MBX_Msg *pMsg, MBX_ROLE_ID eSrcRoleId)
415 {
416 return MDrv_MBX_SendMsgLoopback(pMsg, eSrcRoleId);
417 }
418
MApi_MBX_Enable(MS_BOOL bEnable)419 MBX_Result MApi_MBX_Enable(MS_BOOL bEnable)
420 {
421 return MDrv_MBX_Enable(bEnable);
422 }
423
MApi_MBX_RemoveLatestMsg(void)424 MBX_Result MApi_MBX_RemoveLatestMsg(void)
425 {
426 return MDrv_MBX_RemoveLatestMsg();
427 }
428
_MApi_MBX_DynamicClass(MBX_ROLE_ID eRoleID,char * name,MS_U8 * pmbx_class,Dynamic_MBX_Index eDMBX_Index)429 static MBX_Result _MApi_MBX_DynamicClass(MBX_ROLE_ID eRoleID, char *name, MS_U8 *pmbx_class, Dynamic_MBX_Index eDMBX_Index)
430 {
431 MS_U16 i;
432 MBX_Result ret = E_MBX_SUCCESS;
433
434 API_MBX_LockSend();
435
436 if (_MApi_Search_ClassNum(name, pmbx_class) == E_MBX_SUCCESS)
437 {
438 API_MBX_UnLockSend();
439 return E_MBX_SUCCESS;
440 }
441
442 MBX_Msg smsg;
443
444 Tab_DMBX_Class[Count_DMBX_Class].name[0] = 0; //clear name string
445 strncpy(Tab_DMBX_Class[Count_DMBX_Class].name, name, MAX_DMBX_NAME_LEN);
446 Tab_DMBX_Class[Count_DMBX_Class].mbx_class = E_MBX_CLASS_NUM + Count_DMBX_Class + 1;
447 //printf("\33[33m name=%s: \33[0m\n",name);
448 if(E_Dynamic_QueryCalsss==eDMBX_Index)
449 {
450
451 MApi_MBX_RegisterQueryMBX();
452
453 //printf("\33[33m %s:%s::%d\33[0m\n",__FILE__,__func__,__LINE__);
454 memset((void*)&smsg, 0, sizeof(MBX_Msg));
455 smsg.eRoleID = eRoleID;
456 smsg.eMsgType = E_MBX_MSG_TYPE_INSTANT;
457 smsg.u8Ctrl = 0;
458 smsg.u8MsgClass = E_MBX_CLASS_QUERY_CLASS;
459 smsg.u8Index = eDMBX_Index;
460 smsg.u8ParameterCount = strlen(Tab_DMBX_Class[Count_DMBX_Class].name) + 2; // string end token
461
462 smsg.u8Parameters[0] = (MS_U8) Tab_DMBX_Class[Count_DMBX_Class].mbx_class;
463 for (i = 0; i < (smsg.u8ParameterCount); i++)
464 smsg.u8Parameters[i+1] = (MS_U8) Tab_DMBX_Class[Count_DMBX_Class].name[i];
465
466
467 ret = MDrv_MBX_SendMsg(&smsg);
468 if (ret != E_MBX_SUCCESS)
469 {
470 API_MBX_UnLockSend();
471 //printf("Send Mailbox Failed, status=%d\n", ret);
472 return ret;
473 }
474
475 //printf("\33[33m %s:%s::%d\33[0m\n",__FILE__,__func__,__LINE__);
476
477 memset((void*)&smsg, 0, sizeof(MBX_Msg));
478 //ret = MDrv_MBX_RecvMsg(E_MBX_CLASS_SECURE_WAIT, &smsg, 1000, MBX_CHECK_INSTANT_MSG);
479 //if (ret != E_MBX_SUCCESS)
480 while(E_MBX_SUCCESS!=MDrv_MBX_RecvMsg(E_MBX_CLASS_SECURE_WAIT, &smsg, 1000, MBX_CHECK_INSTANT_MSG))
481 {
482 //printf("Receive Mailbox Failed, status=%d\n", ret);
483 //return ret;
484 }
485 //printf("\33[33m %s:%s::%d\33[0m\n",__FILE__,__func__,__LINE__);
486
487 MApi_MBX_UnRegisterQueryMBX();
488
489 }
490
491 //printf("\33[33m %s:%s::%d,%x\33[0m\n",__FILE__,__func__,__LINE__,eDMBX_Index);
492 switch (eDMBX_Index)
493 {
494 case E_Dynamic_GenerateClass:
495 *pmbx_class = Tab_DMBX_Class[Count_DMBX_Class].mbx_class;
496 Count_DMBX_Class++;
497 break;
498 case E_Dynamic_QueryCalsss:
499
500 //printf("\33[33m %s:%s::%d,%x,%x\33[0m\n",__FILE__,__func__,__LINE__,smsg.u8ParameterCount,smsg.u8Parameters[0]);
501 if (smsg.u8ParameterCount > 0 && smsg.u8Parameters[0] != 0)
502 {
503 Tab_DMBX_Class[Count_DMBX_Class].mbx_class = smsg.u8Parameters[0];
504 *pmbx_class = Tab_DMBX_Class[Count_DMBX_Class].mbx_class;
505 Count_DMBX_Class++;
506 }
507 break;
508 }
509
510 API_MBX_UnLockSend();
511 return ret;
512 }
513
514
MApi_MBX_QueryDynamicClass(MBX_ROLE_ID eRoleID,char * name,MS_U8 * pmbx_class)515 MBX_Result MApi_MBX_QueryDynamicClass(MBX_ROLE_ID eRoleID, char *name, MS_U8 *pmbx_class)
516 {
517 return _MApi_MBX_DynamicClass(eRoleID, name, pmbx_class, E_Dynamic_QueryCalsss);
518 }
519
MApi_MBX_GenerateDynamicClass(MBX_ROLE_ID eRoleID,char * name,MS_U8 * pmbx_class)520 MBX_Result MApi_MBX_GenerateDynamicClass(MBX_ROLE_ID eRoleID, char *name, MS_U8 *pmbx_class)
521 {
522 return _MApi_MBX_DynamicClass(eRoleID, name, pmbx_class, E_Dynamic_GenerateClass);
523 }
524
525
526
527
528
529