Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / framework_unified / client / NS_MessageCenter / src / ns_message_center.c
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 ///////////////////////////////////////////////////////////////////////////////
18 /// \ingroup  tag_NSMessageCenter
19 /// \brief
20 ///
21 /// APIs to Open\Close and Send\Receive on message queues and shared memory.
22 ///
23 ///////////////////////////////////////////////////////////////////////////////
24
25 #include <native_service/ns_message_center_if.h>
26 #include <ns_message_center_internal.h>
27 #include "ns_msg_queue.h"
28 #include <ns_mq_internal.h>
29 #include <ns_mc_internal.h>
30 #include <native_service/ns_msgs.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <native_service/ns_shared_mem_if.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <sys/timeb.h>
39 #include <time.h>
40 #include <errno.h>
41 #include <sys/resource.h>
42 #include <sys/syscall.h>
43 #include <sys/socket.h>
44 #include <sys/un.h>
45 #include <stddef.h>
46 #include <sys/epoll.h>
47 #include <native_service/ns_transmit_log.h>
48 #include <native_service/ns_logger_if.h>
49 #include <native_service/ns_resource_controler.h>
50 #include <native_service/nsfw_message.h>
51 #include <limits.h>
52
53 #include <other_service/strlcpy.h>
54
55 #define assert_static(e) \
56   do { \
57     enum { assert_static__ = 1/(e) }; \
58   } while (0)
59
60 #define MC_INVOKE_SYNC_TIMEOUT (60 * 1000)
61
62 typedef struct {
63   UI_32 seq_id;
64   EFrameworkunifiedStatus ret_val;
65   UI_32 res_size;
66 } ResponseHeader;
67
68 static unsigned int mcSeed[4];
69
70 HANDLE McOpenReceiverNotBlocked(PCSTR name) {
71   return OpenReceiverNotBlocked(name);
72 }
73
74 HANDLE McOpenReceiver(PCSTR name) {
75   return OpenReceiver(name);
76 }
77
78 HANDLE McOpenSyncReceiver(PCSTR name) {
79   return openSyncReceiver(name);
80 }
81
82
83 HANDLE McOpenSender(PCSTR name) {
84   return OpenSender(name);
85 }
86
87 HANDLE McOpenSenderNotBlocked(PCSTR name) {
88   return OpenSender(name);
89 }
90
91 HANDLE McOpenSyncSender(PCSTR name) {
92   return openSyncSender(name);
93 }
94
95 static int mcSetMonitorName(char *str, PCSTR name) {
96   int len;
97   if (str == NULL) {
98     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "str is NULL");
99     return -1;
100   }
101   *str = '\0';
102   len = sprintf(str + 1, "monitorS_%s", name != 0 ? name : NULL);
103   return len + 1;
104 }
105
106 static int OpenMonitor(PCSTR name) {
107   int monitor_sock = -1;
108   int monitor_sa_len;
109   struct sockaddr_un monitor_sa_un;
110
111   if ((monitor_sock = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)) < 0) {
112     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "socket:%s", strerror(errno));
113     goto error;
114   }
115
116   memset(&monitor_sa_un, 0, sizeof(monitor_sa_un));
117   monitor_sa_un.sun_family = AF_UNIX;
118   monitor_sa_len = mcSetMonitorName(monitor_sa_un.sun_path, name);
119   monitor_sa_len += (int)offsetof(struct sockaddr_un, sun_path);
120
121   if (bind(monitor_sock, (struct sockaddr *)&monitor_sa_un, (socklen_t)monitor_sa_len) < 0) {
122     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "bind:%s:%s", name, strerror(errno));  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
123     goto error;
124   }
125
126   if (listen(monitor_sock, SOMAXCONN) < 0) {
127     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "listen:%s", strerror(errno));  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
128     goto error;
129   }
130   return monitor_sock;
131
132 error:
133   if (monitor_sock >= 0) {
134     close(monitor_sock);
135   }
136   return -1;
137 }
138
139 int mcOpenMonitor(PCSTR name) {
140   long monitor_sock;
141
142   if (frameworkunifiedAcquireResouce(FRAMEWORKUNIFIED_RES_ABNMLMON, name, &monitor_sock) < 0) {
143     if ((monitor_sock = OpenMonitor(name)) != -1) {
144       if (frameworkunifiedRegistResouce(FRAMEWORKUNIFIED_RES_ABNMLMON, name, monitor_sock, 1) < 0) {
145         close(monitor_sock);
146         monitor_sock = -1;
147       }
148     }
149   }
150
151   return (int)monitor_sock;
152 }
153
154 int mcCloseMonitor(PCSTR name) {
155   long monitor_sock;
156
157   if (frameworkunifiedGetResource(FRAMEWORKUNIFIED_RES_ABNMLMON, name, &monitor_sock) < 0) {
158     return -1;
159   }
160
161   frameworkunifiedUnregistResouce(FRAMEWORKUNIFIED_RES_ABNMLMON, name);
162   close((int)monitor_sock);
163
164   return 0;
165 }
166
167 static int mcSetClientName(char *str, PCSTR serverName, PCSTR clientName) {
168   int len;
169   if (str == NULL) {
170     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "str is NULL");
171     return -1;
172   }
173   *str = '\0';
174   len = sprintf(str + 1, "monitorC_%s:%s", serverName != 0 ? serverName : NULL, clientName != 0 ? clientName : NULL);
175   return len + 1;
176 }
177
178 static int ConnectMonitor(PCSTR serverName, PCSTR clientName) {
179   int client_sock = -1;
180   int client_sa_len, monitor_sa_len;
181   struct sockaddr_un client_sa_un, monitor_sa_un;
182
183   if ((client_sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0) {  // LCOV_EXCL_BR_LINE 5: socket's error case
184     // LCOV_EXCL_START 5:socket's error case
185     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
186     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "socket:%s", strerror(errno));
187     return -1;
188     // LCOV_EXCL_STOP
189   }
190
191   memset(&client_sa_un, 0, sizeof(client_sa_un));
192   client_sa_un.sun_family = AF_UNIX;
193   client_sa_len = mcSetClientName(client_sa_un.sun_path, serverName, clientName);
194   client_sa_len += (int)offsetof(struct sockaddr_un, sun_path);
195
196   if (bind(client_sock, (struct sockaddr *)&client_sa_un, (socklen_t)client_sa_len) < 0) {
197     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "bind:%s:%s", client_sa_un.sun_path + 1, strerror(errno));
198     goto error;
199   }
200
201   memset(&monitor_sa_un, 0, sizeof(monitor_sa_un));
202   monitor_sa_un.sun_family = AF_UNIX;
203   monitor_sa_len = mcSetMonitorName(monitor_sa_un.sun_path, serverName);
204   monitor_sa_len += (int)offsetof(struct sockaddr_un, sun_path);
205
206   if (connect(client_sock, (struct sockaddr *)&monitor_sa_un, (socklen_t)monitor_sa_len) < 0) {
207     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "connect:%s", strerror(errno));
208     goto error;
209   }
210   return client_sock;
211
212 error:
213   close(client_sock);
214   return -1;
215 }
216
217 int mcSetConnectKey(char *buf, size_t size, PCSTR serverName, PCSTR clientName) {
218   int len = snprintf(buf, size, "%s:%s", serverName != 0 ? serverName : NULL, clientName != 0 ? clientName : NULL);
219   if (buf == NULL) {
220     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "buf is NULL");
221     return -1;
222   }
223   buf[size - 1] = '\0';
224   return len;
225 }
226
227 int mcConnectMonitor(PCSTR serverName, PCSTR clientName) {
228   long client_sock;
229   char connect_key[32];
230
231   mcSetConnectKey(connect_key, sizeof(connect_key), serverName, clientName);
232
233   if (frameworkunifiedAcquireResouce(FRAMEWORKUNIFIED_RES_ABNMLMON, connect_key, &client_sock) < 0) {
234     if ((client_sock = ConnectMonitor(serverName, clientName)) != -1) {
235       if (frameworkunifiedRegistResouce(FRAMEWORKUNIFIED_RES_ABNMLMON, connect_key, client_sock, 1) < 0) {
236         close(client_sock);
237         client_sock = -1;
238       }
239     }
240   }
241
242   return (int)client_sock;
243 }
244
245 int mcCloseConnectMonitor(PCSTR serverName, PCSTR clientName) {  // LCOV_EXCL_START 8: dead code
246   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
247   long client_sock;
248   char connect_key[32];
249
250   mcSetConnectKey(connect_key, sizeof(connect_key), serverName, clientName);
251
252   if (frameworkunifiedGetResource(FRAMEWORKUNIFIED_RES_ABNMLMON, connect_key, &client_sock) == -1) {
253     return -1;
254   }
255
256   if (frameworkunifiedReleaseResouce(FRAMEWORKUNIFIED_RES_ABNMLMON, connect_key) == 0) {
257     frameworkunifiedUnregistResouce(FRAMEWORKUNIFIED_RES_ABNMLMON, connect_key);
258     close((int)client_sock);
259   }
260
261   return 0;
262 }
263 // LCOV_EXCL_STOP
264 static void mcGetClientName(const char *str, char *client_name, size_t size, int pathlen) {
265   size_t len = size - 1;
266   size_t str_len;
267   if (str == NULL) {
268     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "str is NULL");
269     return;
270   }
271   char *p_div = strchr(str + 1, ':');
272   if (p_div == NULL) {
273     return;
274   }
275   str_len = (size_t)(pathlen - (p_div - str + 1));
276   if (len > str_len) {
277     len = str_len;
278   }
279   if (client_name == NULL) {
280     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "client_name is NULL");
281     return;
282   }
283   strncpy(client_name, p_div + 1, len);
284   client_name[len] = '\0';
285 }
286
287 int mcAcceptMonitor(PCSTR serverName, int listenMonitorFd) {
288   int accept_sock = -1;
289   struct sockaddr_un client_sa_un;
290   socklen_t client_sa_len = sizeof(client_sa_un);
291   char client_name[16];
292   char connect_key[32];
293
294   if ((accept_sock = accept4(listenMonitorFd, (struct sockaddr *)&client_sa_un, &client_sa_len, SOCK_CLOEXEC)) < 0) {
295     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "accept:%s", strerror(errno));
296     return -1;
297   }
298
299   mcGetClientName(client_sa_un.sun_path, client_name, sizeof(client_name), (int)(client_sa_len - offsetof(struct sockaddr_un,
300                                                                                                           sun_path)));
301   mcSetConnectKey(connect_key, sizeof(connect_key), serverName, client_name);
302
303   frameworkunifiedRegistResouce(FRAMEWORKUNIFIED_RES_ABNMLMON, connect_key, accept_sock, 0);
304   return accept_sock;
305 }
306
307 int mcGetAcceptMonitor(PCSTR serverName, PCSTR clientName) {
308   long accept_sock;
309   char connect_key[32];
310
311   mcSetConnectKey(connect_key, sizeof(connect_key), serverName, clientName);
312
313   if (frameworkunifiedAcquireResouce(FRAMEWORKUNIFIED_RES_ABNMLMON, connect_key, &accept_sock) < 0) {
314     return -1;
315   }
316
317   return (int)accept_sock;
318 }
319
320 int mcGetClientNameFromConnectKey(PCSTR connectKey, PCHAR clientName, size_t len) {
321   char *p_div = strchr(connectKey, ':');
322   if (p_div == NULL) {
323     return -1;
324   }
325
326   strncpy(clientName, p_div + 1, len);
327   clientName[len - 1] = '\0';
328   return 0;
329 }
330
331 int mcGetServerNameFromConnectKey(PCSTR connectKey, PCHAR serverName, size_t len) {
332   char *p_div = strchr(connectKey, ':');
333   size_t min_len;
334
335   if (p_div == NULL) {
336     return -1;
337   }
338
339   min_len = ((len - 1) < (p_div - connectKey)) ? len - 1 : (size_t)(p_div - connectKey);
340   strncpy(serverName, connectKey, min_len);
341   serverName[min_len] = '\0';
342   return 0;
343 }
344
345 /////////////////////////////////////////
346 HANDLE McOpenSenderChild(PCSTR name, pthread_t childid) {
347   return OpenSenderChild(name, childid);
348 }
349
350 EFrameworkunifiedStatus McJoinChild(HANDLE hChildApp) {
351   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
352
353   if (INVALID_HANDLE != hChildApp) {  // LCOV_EXCL_BR_LINE 6: impossible to confirm because hChildApp checked is passed in the FrameworkunifiedDestroyChildThread function of the frameworkunified_multithreading.cpp
354     eStatus = JoinChild(hChildApp);
355   } else {
356     // LCOV_EXCL_START 6: impossible to confirm because hChildApp checked is passed in the FrameworkunifiedDestroyChildThread function of the frameworkunified_multithreading.cpp
357     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
358     eStatus = eFrameworkunifiedStatusInvldHandle;
359     // LCOV_EXCL_STOP
360   }
361
362   return eStatus;
363 }
364
365 EFrameworkunifiedStatus McGetChildThreadPriority(HANDLE hChildApp, PSI_32 threadPrio) {
366   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
367
368   if (INVALID_HANDLE != hChildApp) {
369     eStatus = GetChildThreadPriority(hChildApp, threadPrio);
370   } else {
371     eStatus = eFrameworkunifiedStatusInvldHandle;
372   }
373
374   return eStatus;
375 }
376
377 /// endhack...
378 /////////////////////////////////////////
379
380 static EFrameworkunifiedStatus mcReceiveInternal(HANDLE hMessage, PSTR source, UI_32 *cmd,  UI_32 *sessionid, UI_32 length,
381                                     PVOID data) {
382   PSSystemMsgHeader sysHdr = NULL;
383
384   // check if handle is valid
385   if (hMessage == INVALID_HANDLE) {
386     return eFrameworkunifiedStatusInvldHandle;
387   }
388
389   // check if buffer is null
390   if (data == NULL) {
391     return eFrameworkunifiedStatusInvldBuf;
392   }
393
394   // check for invalid length.
395   if (length <= 0) {
396     return eFrameworkunifiedStatusInvldBuf;
397   }
398
399   // check if the source is null
400   if (source == NULL) {
401     return eFrameworkunifiedStatusInvldQName;
402   }
403
404   if (cmd == NULL) {
405     return eFrameworkunifiedStatusInvldParam;
406   }
407
408   if (-1 != ReceiveMessage(hMessage, length, data)) {
409     sysHdr = (PSSystemMsgHeader)data;
410     if (strlen(sysHdr->info.source) < MAX_QUEUE_NAME_SIZE) {
411       *cmd = sysHdr->info.command;
412       if (sessionid) {
413         *sessionid = sysHdr->info.sessionid;
414       }
415       strlcpy(source, sysHdr->info.source, MAX_NAME_SIZE_APP);
416       return eFrameworkunifiedStatusOK;
417     } else {
418       return eFrameworkunifiedStatusInvldBuf;
419     }
420   } else {
421     return TranslateError(errno);
422   }
423
424   return eFrameworkunifiedStatusFail;
425 }
426
427 EFrameworkunifiedStatus McReceive(HANDLE hMessage, PSTR source, UI_32 *cmd, UI_32 length, PVOID data) {
428   return mcReceiveInternal(hMessage, source, cmd, NULL, length, data);
429 }
430
431 EFrameworkunifiedStatus McReceiveWithSession(HANDLE hMessage, PSTR source, UI_32 *cmd,  UI_32 *sessionid, UI_32 length, PVOID data) {
432   if (sessionid == NULL) {
433     return eFrameworkunifiedStatusInvldParam;
434   }
435   return mcReceiveInternal(hMessage, source, cmd, sessionid, length, data);
436 }
437
438 static EFrameworkunifiedStatus mcSendInternal(HANDLE hMessage, PCSTR source, UI_32 cmd, PCSTR sysInfo, UI_32 length, PCVOID data,
439                                  EFrameworkunifiedMessagePriorties priority, UI_32 sessionid, BOOL is_sync) {
440   assert_static(_NSFW_MSG_LEN_ == sizeof(SSystemMsgHeader));
441   assert_static(_NSFW_SYSINFO_SIZE_ == MAX_SYS_INFO_SIZE);
442   assert_static(_NSFW_SYSINFO_FLAG_ == MC_EMSGT_SYSINFO_MASK);
443
444   // check if handle is valid
445   if (mqCheckValidHandle(hMessage) == FALSE) {
446     return eFrameworkunifiedStatusInvldHandle;
447   }
448
449   // check if buffer is null
450   if (data == NULL && length > 0) {
451     return eFrameworkunifiedStatusInvldBuf;
452   }
453
454   // check if the source is null
455   if (source == NULL) {
456     return eFrameworkunifiedStatusInvldQName;
457   }
458
459   // check if the source length exceeds than expected
460   /**
461    * @todo
462    * Although a send message queue handle can be created using a 19-byte message queue name, messages cannot be sent using the created send message queue handle.
463    * Because "/" is added to the queue name in the send message queue handle, a judgment error of MAX_QUEUE_NAME_SIZE (20 bytes) occurs during transmission and transmission cannot be performed.
464    */
465   if (strlen(source) >= MAX_QUEUE_NAME_SIZE) {
466     return eFrameworkunifiedStatusInvldQName;
467   }
468
469   // notification name size should be less than MAX_SYS_INFO_SIZE
470   // data length check
471   UI_32 max_length = 0;
472   if (NULL != sysInfo) {
473     if (strlen(sysInfo) >= MAX_SYS_INFO_SIZE) {
474       return eFrameworkunifiedStatusInvldParam;
475     }
476     // If there is an sysInfo, the maximum send data length is the maximum send data length of UI_32 minus (header + sysInfo size).
477     max_length = 0xFFFFFFFF - sizeof(SSystemMsgHeader) - sizeof(TSysMsgSystemInfo);
478   } else {
479     // If there is no sysInfo, the maximum send data length is the maximum value of UI_32 minus the headers.
480     max_length = 0xFFFFFFFF - sizeof(SSystemMsgHeader);
481   }
482
483   if (length > max_length) {
484     return eFrameworkunifiedStatusInvldParam;
485   }
486   EFrameworkunifiedStatus rtnStatus = eFrameworkunifiedStatusOK;
487
488   UI_8 sndBuf[ MAX_QUEUE_MSG_SIZE ];
489   SQhandle *sndHndl = (SQhandle *)hMessage;
490   SSystemMsgHeader *msgHdr = (SSystemMsgHeader *)sndBuf;
491   UI_32 send_length = 0;
492   UI_32 offset = sizeof(SSystemMsgHeader);
493
494   if (NULL == sndHndl->q_name) {
495     return eFrameworkunifiedStatusInvldHandle;
496   }
497
498   // check if the dest length exceeds than expected
499   if (strlen(sndHndl->q_name) >= MAX_QUEUE_NAME_SIZE) {
500     return eFrameworkunifiedStatusInvldHandle;
501   }
502
503   // compose header
504   strcpy(msgHdr->info.source, source);
505   msgHdr->info.length = length;  // this is length of the messages data...
506   msgHdr->info.command = cmd;
507   msgHdr->info.sessionid = sessionid;
508
509   msgHdr->eType = (UI_32)MC_EMSGT_VALID_TOKEN;
510   msgHdr->memId = 0;  // not set...
511   if (NULL != sysInfo) {
512     msgHdr->eType |= MC_EMSGT_SYSINFO_MASK;
513     strcpy((char *)(sndBuf + offset), sysInfo);
514     offset += (UI_32)sizeof(TSysMsgSystemInfo);
515   }
516   if (is_sync) {
517     msgHdr->eType |= MC_EMSGT_SYNC_MASK;
518   }
519
520   // Build buffer to send...
521
522   // copy to buffer
523   /*
524    * @todo
525    * Do not throw an error even if the data size of the argument is set to 0.
526    * When "length + offset" is MAX_QUEUE_MSG_SIZE (4096) or more, the length becomes 0 in the process that is executed cannot be checked.
527    * Sizeof(SSystemMsgHeader) is 40, sizeof(TSysMsgSystemInfo) is 64, maximal. offset is 104, min. is 40
528    */
529   if ((length + offset) >= MAX_QUEUE_MSG_SIZE) {
530     // set the shared mem info in header....
531     msgHdr->eType |= MC_EMSGT_SHM_MASK;
532
533     // send data to shm
534     msgHdr->memId = SetDataToShared(data, length, source, sndHndl->q_name);
535
536     if (BAD_MEM_ID != msgHdr->memId) {
537       // update the send length
538       send_length += offset;
539     } else {
540       rtnStatus = eFrameworkunifiedStatusErrOther;
541     }
542   } else {  // was less than the max
543     // update the send length
544     send_length += offset;
545
546     /**
547      * @todo
548      *  If UINT_MAX (4294967295) is specified in the iLength of FrameworkunifiedNPPublishNotification,
549      *  the comparison by (length + offset) >= MAX_QUEUE_MSG_SIZE fails to compare correctly because of an overflow,
550      *  and the comparison is judged to be less than the max.
551      *  Once in the else block, the data is treated as a sysHeader block of up to 4096 bytes,
552      *  and the memcpy is performed again without size checking, resulting in a segmentation fault.
553      *  Maximum message length that can be sent by FrameworkunifiedNPPublishNotification must be specified and the maximum message length must be checked.
554      */
555     // copy the msg to send buffer
556     memcpy(&sndBuf[0] + offset, data, length);
557
558     // update the send length
559     send_length += length;
560   }
561
562   if (eFrameworkunifiedStatusOK == rtnStatus) {
563     rtnStatus = SendMessageWithPriority(hMessage, send_length, (PVOID)&sndBuf[0], priority);
564   }
565
566   if (rtnStatus != eFrameworkunifiedStatusOK) {
567     // remove the shared mem since this failed.
568     McClearData(msgHdr);
569   }
570
571   return rtnStatus;
572 }
573
574 EFrameworkunifiedStatus McSendPrioWithSysInfo(HANDLE hMessage, PCSTR source, UI_32 cmd, PCSTR sysInfo, UI_32 length, PCVOID data,
575                                  EFrameworkunifiedMessagePriorties priority, UI_32 sessionid) {
576   if (NULL == sysInfo) {
577     return eFrameworkunifiedStatusInvldParam;
578   }
579   return mcSendInternal(hMessage, source, cmd, sysInfo, length, data, priority, sessionid, FALSE);
580 }
581
582 EFrameworkunifiedStatus McSendWithSysInfo(HANDLE hMessage, PCSTR source, UI_32 cmd, PCSTR sysInfo, UI_32 length, PCVOID data,
583                              UI_32 sessionid) {
584   if (NULL == sysInfo) {
585     return eFrameworkunifiedStatusInvldParam;
586   }
587   return mcSendInternal(hMessage, source, cmd, sysInfo, length, data, eFrameworkunifiedMsgPrioNormal, sessionid, FALSE);
588 }
589
590 EFrameworkunifiedStatus McSend(HANDLE hMessage, PCSTR source, UI_32 cmd, UI_32 length, PCVOID data) {
591   return mcSendInternal(hMessage, source, cmd, NULL, length, data, eFrameworkunifiedMsgPrioNormal, 0, FALSE);
592 }
593
594 EFrameworkunifiedStatus McSendWithPriority(HANDLE hMessage, PCSTR source, UI_32 cmd, UI_32 length, PCVOID data,
595                               EFrameworkunifiedMessagePriorties priority,  UI_32 sessionid) {
596   return mcSendInternal(hMessage, source, cmd, NULL, length, data, priority, sessionid, FALSE);
597 }
598
599 EFrameworkunifiedStatus McSendWithSession(HANDLE hMessage, PCSTR source, UI_32 cmd, UI_32 length, PCVOID data, UI_32 sessionid) {
600   return mcSendInternal(hMessage, source, cmd, NULL, length, data, eFrameworkunifiedMsgPrioNormal, sessionid, FALSE);
601 }
602
603 EFrameworkunifiedStatus McSendSyncResponse(HANDLE hMessage, PCSTR source, UI_32 cmd, UI_32 seq_id, EFrameworkunifiedStatus ret_val, UI_32 length,
604                               PCVOID data) {
605   EFrameworkunifiedStatus eStatus;
606   ResponseHeader *resHdr;
607   UI_8 *res_data;
608
609   if ((UINT_MAX - sizeof(ResponseHeader)) < length) {
610     return eFrameworkunifiedStatusInvldBufSize;
611   }
612
613   // check if buffer is null
614   if (data == NULL && length > 0) {
615     return eFrameworkunifiedStatusInvldBuf;
616   }
617
618   if ((res_data = calloc(length + sizeof(ResponseHeader), 1)) == NULL) {  // LCOV_EXCL_BR_LINE 5: calloc's error case
619     // LCOV_EXCL_START 5: calloc's error case
620     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
621     eStatus = eFrameworkunifiedStatusFail;
622     goto exit;
623     // LCOV_EXCL_STOP
624   }
625
626   resHdr = (ResponseHeader *)res_data;
627   resHdr->seq_id = seq_id;
628   resHdr->ret_val = ret_val;
629   resHdr->res_size = length;
630
631   memcpy(res_data + sizeof(ResponseHeader), data, length);
632
633   eStatus = mcSendInternal(hMessage, source, cmd, NULL, (UI_32)(length + sizeof(ResponseHeader)), res_data, eFrameworkunifiedMsgPrioNormal, 0,
634                            TRUE);
635
636 exit:
637   free(res_data);
638   res_data = NULL;
639   resHdr = NULL;
640
641   return eStatus;
642 }
643
644 /*
645  * Xorshift
646  */
647 static inline unsigned int xor128(void) {
648   unsigned int t;
649   t = (mcSeed[0] ^ (mcSeed[0] << 11));
650   mcSeed[0] = mcSeed[1];
651   mcSeed[1] = mcSeed[2];
652   mcSeed[2] = mcSeed[3];
653   return (mcSeed[3] = (mcSeed[3] ^ (mcSeed[3] >> 19)) ^ (t ^ (t >> 8)));
654 }
655
656 static inline char *ultoa(unsigned long value, char *buf, int radix) {
657   const static char c[] = "0123456789abcdef";
658   char b[65];
659   char *p = b + sizeof(b);
660
661   *--p = '\0';
662   do {
663     *--p = c[value % (unsigned long)radix];
664     value /= (unsigned long)radix;
665   } while (value);
666   if (buf == NULL) {
667     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "buf is NULL");
668     return NULL;
669   }
670   strcpy(buf, p);
671
672   return buf;
673 }
674
675 EFrameworkunifiedStatus McInvokeSync(HANDLE hMessage, PCSTR source, UI_32 cmd, UI_32 msgLength, PCVOID msgData, UI_32 sessionid,
676                         HANDLE hRcvMessage, UI_32 responseLength, PVOID responseData, UI_32 *receivedLength) {
677   EFrameworkunifiedStatus eStatus;
678   TSysMsgSystemInfo sysInfo;
679   UI_32 seq_id;
680   ResponseHeader *resHdr;
681   UI_8 *rcv_buf = NULL;
682   CHAR resSource[MAX_NAME_SIZE_APP];
683   int efd = -1;
684   struct epoll_event events[2];
685   struct epoll_event ev;
686   int connect_sock = -1;
687   SQhandle *sndHndl = (SQhandle *)hMessage;
688
689   if (mqCheckValidHandle(hMessage) == FALSE || mqCheckValidHandle(hRcvMessage) == FALSE || (responseLength != 0 &&
690                                                                                             responseData == NULL)) {
691     eStatus = eFrameworkunifiedStatusInvldHandle;
692
693     /**
694      * @todo
695      * The system performs rcv_buf free without NULL determination.
696      */
697     goto exit;
698   }
699
700   if (NULL == receivedLength) {
701     eStatus = eFrameworkunifiedStatusInvldParam;
702     goto exit;
703   }
704   if ((rcv_buf = malloc(MAX_QUEUE_MSG_SIZE)) == NULL) {  // LCOV_EXCL_BR_LINE 5: malloc's error case.
705     // LCOV_EXCL_START 5: malloc's error case.
706     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
707     eStatus = eFrameworkunifiedStatusFail;
708     goto exit;
709     // LCOV_EXCL_STOP
710   }
711
712   if (mcSeed[0] == 0) {
713     int fd;
714     if ((fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC)) < 0) {
715       eStatus = eFrameworkunifiedStatusFail;
716       goto exit;
717     }
718
719     if (read(fd, &mcSeed, sizeof(mcSeed)) < 0) {
720       close(fd);
721       eStatus = eFrameworkunifiedStatusFail;
722       goto exit;
723     }
724     close(fd);
725   }
726   seq_id = xor128();
727
728   sysInfo[0] = '0';
729   sysInfo[1] = 'x';
730   ultoa(seq_id, &sysInfo[2], 16);
731
732   Flush(hRcvMessage);
733
734   eStatus = mcSendInternal(hMessage, source, cmd, sysInfo, msgLength, msgData, eFrameworkunifiedMsgPrioNormal, sessionid, TRUE);
735   if (eStatus != eFrameworkunifiedStatusOK) {
736     goto exit;
737   }
738
739   if ((connect_sock = mcConnectMonitor(sndHndl->q_name + 1, source)) < 0) {
740     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "connect:%s", strerror(errno));
741     eStatus = eFrameworkunifiedStatusFail;
742     goto exit;
743   }
744
745   if ((efd = epoll_create1(EPOLL_CLOEXEC)) < 0) {  // LCOV_EXCL_BR_LINE 5: epoll_create1's error case.
746     // LCOV_EXCL_START 5: epoll_create1's error case.
747     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
748     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "epoll_create1:%s", strerror(errno));
749     eStatus = eFrameworkunifiedStatusFail;
750     goto exit;
751     // LCOV_EXCL_STOP
752   }
753   ev.events = EPOLLIN;
754   ev.data.fd = McGetQueueFD(hRcvMessage);
755   if (epoll_ctl(efd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0) {  // LCOV_EXCL_BR_LINE 5: epoll_ctl's error case.
756     // LCOV_EXCL_START 5: epoll_ctl's error case.
757     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
758     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "epoll_ctl:%s", strerror(errno));
759     eStatus = eFrameworkunifiedStatusFail;
760     goto exit;
761     // LCOV_EXCL_STOP
762   }
763   ev.events = EPOLLRDHUP;
764   ev.data.fd = connect_sock;
765   if (epoll_ctl(efd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0) {  // LCOV_EXCL_BR_LINE 5: epoll_ctl's error case.
766     // LCOV_EXCL_START 5: epoll_ctl's error case.
767     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
768     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "epoll_ctl:%s", strerror(errno));
769     eStatus = eFrameworkunifiedStatusFail;
770     goto exit;
771     // LCOV_EXCL_STOP
772   }
773
774   while (1) {
775     int nfds, n;
776
777     nfds = epoll_wait(efd, events, 2, MC_INVOKE_SYNC_TIMEOUT);
778     if (nfds == -1) {  // LCOV_EXCL_BR_LINE 5: epoll_wait's error case.
779       // LCOV_EXCL_START 5: epoll_wait's error case.
780       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
781       if (errno == EINTR) {
782         continue;
783       }
784       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "epoll_wait:%s", strerror(errno));
785       eStatus = eFrameworkunifiedStatusFail;
786       goto exit;
787       // LCOV_EXCL_STOP
788     } else if (nfds == 0) {  // LCOV_EXCL_BR_LINE 5: epoll_wait return value equal 0.
789       // LCOV_EXCL_START 5: epoll_wait return value equal 0.
790       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
791       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "epoll_wait:time out");
792       eStatus = eFrameworkunifiedStatusAccessError;
793       goto exit;
794       // LCOV_EXCL_STOP
795     }
796
797     for (n = 0; n < nfds; n++) {
798       if (events[n].data.fd == connect_sock) {
799         /*
800          * connection fail
801          */
802         char connect_key[32];
803         mcSetConnectKey(connect_key, sizeof(connect_key), sndHndl->q_name + 1, source);
804
805         if (epoll_ctl(efd, EPOLL_CTL_DEL, connect_sock, events) < 0) {
806           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "epoll_ctl:%s", strerror(errno));
807         }
808         if (close(connect_sock) < 0) {
809           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "close:%s", strerror(errno));
810         }
811         frameworkunifiedUnregistResouce(FRAMEWORKUNIFIED_RES_ABNMLMON, connect_key);
812
813         if ((connect_sock = mcConnectMonitor(sndHndl->q_name + 1, source)) < 0) {
814           eStatus = eFrameworkunifiedStatusBadConnection;
815           goto exit;
816         }
817         ev.events = EPOLLRDHUP;
818         ev.data.fd = connect_sock;
819         if (epoll_ctl(efd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0) {
820           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "epoll_ctl:%s", strerror(errno));
821           eStatus = eFrameworkunifiedStatusFail;
822           goto exit;
823         }
824       } else {
825         /*
826          * response receive
827          */
828         if ((eStatus = McReceive(hRcvMessage, resSource, &cmd, MAX_QUEUE_MSG_SIZE, rcv_buf)) != eFrameworkunifiedStatusOK) {
829           goto exit;
830         }
831
832         PSSystemMsgHeader sysHdr = (PSSystemMsgHeader)rcv_buf;
833         if (MC_EMSGT_IS_SHM(sysHdr->eType)) {
834           UI_32 shmLength = GetLengthOfDataFromShared(sysHdr->memId);
835           TMemID memId = sysHdr->memId;
836
837           if (shmLength == 0) {
838             eStatus = eFrameworkunifiedStatusErrOther;
839             goto exit;
840           }
841
842           free(rcv_buf);
843           if ((rcv_buf = calloc(shmLength, 1)) == NULL) {
844             eStatus = eFrameworkunifiedStatusFail;
845             goto exit;
846           }
847
848           if ((eStatus = GetDataFromShared(memId, rcv_buf, shmLength)) != eFrameworkunifiedStatusOK) {
849             eStatus = eFrameworkunifiedStatusErrOther;
850             goto exit;
851           }
852
853           DiscardDataFromShared(memId);
854           resHdr = (ResponseHeader *)rcv_buf;
855         } else {
856           resHdr = (ResponseHeader *)(rcv_buf + sizeof(SSystemMsgHeader));
857         }
858
859         if (resHdr->seq_id == seq_id) {
860           if (resHdr->res_size > 0 && responseLength > 0)
861             memcpy(responseData, (const void *)((char *)resHdr + sizeof(ResponseHeader)),
862                    resHdr->res_size > responseLength ? responseLength : resHdr->res_size);
863
864           *receivedLength = resHdr->res_size;
865           eStatus = resHdr->ret_val;
866           goto exit;  // success
867         }
868       }
869     }
870   }
871
872 exit:
873   free(rcv_buf);
874   rcv_buf = NULL;
875   resHdr = NULL;
876
877   if (efd >= 0) {
878     close(efd);
879   }
880
881   return eStatus;
882 }
883
884 EFrameworkunifiedStatus McCreateInvokerName(PCSTR source, UI_32 sessionid, PSTR invokerName, UI_32 size) {
885   if (source == NULL || invokerName == NULL) {
886     return eFrameworkunifiedStatusInvldParam;
887   }
888
889   // When the size of the synchronous communication message queue name storage area is 0.
890   if (0 == size) {
891     return eFrameworkunifiedStatusInvldParam;
892   }
893   snprintf(invokerName, size - 1, "%s_r0", source);
894   return eFrameworkunifiedStatusOK;
895 }
896
897 EFrameworkunifiedStatus McForward(HANDLE hMessage, PCSTR source, UI_32 iCmd, TMemID USID) {
898   // check if handle is valid
899   if (mqCheckValidHandle(hMessage) == FALSE) {
900     return eFrameworkunifiedStatusInvldHandle;
901   }
902
903   // check if the source is null
904   if (source == NULL) {
905     return eFrameworkunifiedStatusInvldQName;
906   }
907
908   // check the shared memory id
909   if (USID == BAD_MEM_ID) {
910     return eFrameworkunifiedStatusErrOther;
911   }
912
913   // check if the source length exceeds than expected
914   if (strlen(source) >= MAX_QUEUE_NAME_SIZE) {
915     return eFrameworkunifiedStatusInvldQName;
916   }
917
918   UI_8 sndBuf[ MAX_QUEUE_MSG_SIZE ] = {};
919   SQhandle *sndHndl = (SQhandle *)hMessage;
920   SSystemMsgHeader msgHdr = {};
921
922   // check if the dest length exceeds than expected
923   if (strlen(sndHndl->q_name) >= MAX_QUEUE_NAME_SIZE) {
924     return eFrameworkunifiedStatusInvldHandle;
925   }
926
927   // compose header
928   strlcpy(msgHdr.info.source, source, sizeof(msgHdr.info.source));
929   msgHdr.info.length = GetLengthOfDataFromShared(USID);  // this is length of the messages data...
930   msgHdr.info.command = iCmd;
931   msgHdr.eType = (UI_32)(MC_EMSGT_VALID_TOKEN | MC_EMSGT_SHM_MASK);
932   msgHdr.memId = USID;
933   msgHdr.info.sessionid = 0;
934
935   // copy to buffer
936   UI_8 offset = sizeof(SSystemMsgHeader);
937   // write header to send buffer
938   memcpy(&sndBuf[0], &msgHdr, offset);
939
940   return SendMessage(hMessage, MAX_QUEUE_MSG_SIZE, (PVOID)&sndBuf[0]);
941 }
942
943 PCSTR McGetMsgSrc(PVOID data) {
944   // check if buffer is null
945   if (data == NULL) {
946     return NULL;  // The data is invalid return zero length.
947   }
948
949   PSSystemMsgHeader sysHdr = (PSSystemMsgHeader)data;
950   return (PCSTR)sysHdr->info.source;
951 }
952
953 UI_32 mcGetMsgSsessionId(PVOID data) {
954   // check if buffer is null
955   if (data == NULL) {
956     return 0;  // The data is invalid return zero.
957   }
958
959   PSSystemMsgHeader sysHdr = (PSSystemMsgHeader)data;
960   return sysHdr->info.sessionid;
961 }
962
963 UI_32 McGetLength(PVOID data) {
964   // check if buffer is null
965   if (data == NULL) {
966     return 0;  // The data is invalid return zero length.
967   }
968
969   PSSystemMsgHeader sysHdr = (PSSystemMsgHeader)data;
970   return sysHdr->info.length;
971 }
972
973 PVOID McGetDataPointer(PVOID data) {
974   if (data == NULL) {
975     return NULL;
976   }
977
978   PSSystemMsgHeader sysHdr = (PSSystemMsgHeader)data;
979   int offset = sizeof(SSystemMsgHeader);
980
981   if (MC_EMSGT_HAS_SYSINFO(sysHdr->eType)) {
982     offset += (int)sizeof(TSysMsgSystemInfo);
983   }
984
985   return (PVOID)((long)data + offset);
986 }
987
988 EFrameworkunifiedStatus McGetSysInfoData(PVOID data, PVOID to) {
989   // check if buffers are null
990   if (data == NULL || to == NULL) {
991     return eFrameworkunifiedStatusInvldBuf;
992   }
993
994   PSSystemMsgHeader sysHdr = (PSSystemMsgHeader)data;
995   if (MC_EMSGT_VALID((int)sysHdr->eType)) {
996     if (MC_EMSGT_HAS_SYSINFO(sysHdr->eType)) {
997       strlcpy(to, (char *)data + sizeof(SSystemMsgHeader), MAX_SYS_INFO_SIZE);
998     } else {
999       *(char *)to = '\0';
1000     }
1001     return eFrameworkunifiedStatusOK;
1002   }
1003
1004   return eFrameworkunifiedStatusInvldHandle;
1005 }
1006
1007 EFrameworkunifiedStatus McGetDataOfSize(PVOID data, PVOID to, UI_32 uiSize) {
1008   if (McGetLength(data) > uiSize) {
1009     return eFrameworkunifiedStatusInvldBufSize;
1010   }
1011   return GetData(data, to, FALSE);
1012 }
1013
1014 EFrameworkunifiedStatus McGetDataOfSizeWithSMRetain(PVOID data, PVOID to, UI_32 uiSize) {
1015   if (McGetLength(data) > uiSize) {
1016     return eFrameworkunifiedStatusInvldBufSize;
1017   }
1018   return GetData(data, to, TRUE);
1019 }
1020
1021 BOOL mcGetIsTypeOfSync(PVOID data) {
1022   if (data == NULL) {
1023     return FALSE;
1024   }
1025
1026   PSSystemMsgHeader sysHdr = (PSSystemMsgHeader)data;
1027
1028   if (MC_EMSGT_VALID((int)sysHdr->eType)) {
1029     if (MC_EMSGT_IS_SYNC(sysHdr->eType)) {
1030       return TRUE;
1031     }
1032   }
1033
1034   return FALSE;
1035 }
1036
1037 EFrameworkunifiedStatus McClearData(PVOID data) {
1038   // check if buffers are null
1039   if (data == NULL) {
1040     return eFrameworkunifiedStatusInvldBuf;
1041   }
1042
1043   PSSystemMsgHeader sysHdr = (PSSystemMsgHeader)data;
1044
1045   if (MC_EMSGT_VALID((int)sysHdr->eType)) {
1046     if (MC_EMSGT_IS_SHM(sysHdr->eType)) {
1047       return DiscardDataFromShared(sysHdr->memId);
1048     } else {
1049       return eFrameworkunifiedStatusOK;
1050     }
1051   }
1052
1053   return eFrameworkunifiedStatusInvldParam;
1054 }
1055
1056 EFrameworkunifiedStatus McClose(HANDLE hMessage) {
1057   EFrameworkunifiedStatus rtnStatus = eFrameworkunifiedStatusInvldHandle;
1058
1059   if (NULL != hMessage) {
1060     EQType qType = GetQueueType(hMessage);
1061
1062     if (eQTypeSender == qType) {
1063       rtnStatus = CloseSender(hMessage);
1064     } else if (eQTypeReveiver == qType) {
1065       rtnStatus = CloseReceiver(hMessage);
1066     }
1067   }
1068
1069   return rtnStatus;
1070 }
1071
1072
1073 void McFlushReceiver(HANDLE hMessage) {
1074   if (hMessage != INVALID_HANDLE) {
1075     // proxy to the real function in msg queue
1076     Flush(hMessage);
1077   }
1078 }
1079
1080 TMemID McGetDataUSID(PVOID pData) {
1081   // check if buffer is null
1082   if (pData == NULL) {
1083     return BAD_MEM_ID;
1084   }
1085
1086   PSSystemMsgHeader sysHdr = (PSSystemMsgHeader)pData;
1087
1088   if (MC_EMSGT_VALID((int)sysHdr->eType)) {
1089     if (MC_EMSGT_IS_SHM(sysHdr->eType)) {
1090       return sysHdr->memId;
1091     } else {
1092       return BAD_MEM_ID;
1093     }
1094   }
1095
1096   return 0;
1097 }
1098
1099 ////////////////////////////////////////////////////////////////////////////////////////////
1100 /// GetData
1101 ////////////////////////////////////////////////////////////////////////////////////////////
1102 EFrameworkunifiedStatus GetData(PVOID data, PVOID to, BOOL retain) {
1103   EFrameworkunifiedStatus rtnStatus = eFrameworkunifiedStatusInvldHandle;
1104
1105   // check if buffers are null
1106   if (data == NULL || to == NULL) {
1107     return eFrameworkunifiedStatusInvldBuf;
1108   }
1109
1110   PSSystemMsgHeader sysHdr = (PSSystemMsgHeader)data;
1111   int offset = sizeof(SSystemMsgHeader);
1112
1113   if (MC_EMSGT_VALID((int)sysHdr->eType)) {
1114     if (MC_EMSGT_HAS_SYSINFO(sysHdr->eType)) {
1115       offset += (int)sizeof(TSysMsgSystemInfo);
1116     }
1117
1118     if (MC_EMSGT_IS_SHM(sysHdr->eType)) {
1119       // need to get the message from shared memory
1120
1121       // get the length based on the memId
1122       UI_32 shmLength = GetLengthOfDataFromShared(sysHdr->memId);
1123       /*
1124        * todo
1125        * When a eFrameworkunifiedStatusInvldID error occurs in GetDataFromShared, the abnormal values are also returned in eFrameworkunifiedStatusErrOther.
1126        */
1127       if (0 != shmLength && eFrameworkunifiedStatusOK == GetDataFromShared(sysHdr->memId, to, shmLength)) {
1128         if (!retain) {
1129           // now that the data has been retrieved we may now discard it.
1130           McClearData(data);
1131         }
1132         rtnStatus = eFrameworkunifiedStatusOK;
1133       } else {
1134         rtnStatus = eFrameworkunifiedStatusErrOther;
1135       }
1136     } else {
1137       // memcpy the data to the buffer provided.
1138       memcpy(to, (PVOID)((long)data + offset), sysHdr->info.length);
1139       rtnStatus = eFrameworkunifiedStatusOK;
1140     }
1141   } else {
1142     rtnStatus = eFrameworkunifiedStatusInvldHndlType;
1143   }
1144
1145   return rtnStatus;
1146 }
1147
1148 PCSTR McGetQueueName(HANDLE hMessage) {
1149   PCSTR name = NULL;
1150
1151   if (NULL != hMessage) {
1152     name = GetQueueName(hMessage);
1153   }
1154
1155   return name;
1156 }
1157
1158 int McGetQueueFD(HANDLE hMessage) {
1159   int fd = -1;
1160
1161   if (NULL != hMessage) {
1162     fd = GetQueueFD(hMessage);
1163   }
1164
1165   return fd;
1166 }
1167
1168 EFrameworkunifiedStatus McTranslateError(int error) {
1169   return TranslateError(error);
1170 }
1171
1172 EFrameworkunifiedStatus McZcSetParam(HANDLE handle, UI_32 cmd, UI_32 length) {
1173   if (handle == NULL) {
1174     return eFrameworkunifiedStatusInvldHandle;
1175   }
1176
1177   SQhandle *sndHndl = (SQhandle *)handle;
1178   SSystemMsgHeader *msgHdr = (SSystemMsgHeader *)(sndHndl->sendbuf);
1179
1180   // QueueType checking
1181   EQType qType = GetQueueType(handle);
1182   if (eQTypeSender != qType) {
1183     return eFrameworkunifiedStatusInvldHndlType;
1184   }
1185
1186   if (mqCheckValidHandle(handle) == FALSE) {
1187     return eFrameworkunifiedStatusInvldHandle;
1188   }
1189   if (sndHndl->sendbuf == NULL) {
1190     return eFrameworkunifiedStatusInvldBuf;
1191   }
1192   if (length > (MAX_QUEUE_MSG_SIZE - sizeof(SSystemMsgHeader))) {
1193     return eFrameworkunifiedStatusInvldBufSize;
1194   }
1195
1196   msgHdr->info.command = cmd;
1197   msgHdr->info.length = length;
1198   return eFrameworkunifiedStatusOK;
1199 }
1200
1201 PVOID McZcGetBuf(HANDLE handle) {
1202   if (handle == NULL) {
1203     return NULL;
1204   }
1205
1206   SQhandle *sndHndl = (SQhandle *)handle;
1207
1208   // QueueType checking
1209   EQType qType = GetQueueType(handle);
1210   if (eQTypeSender != qType) {
1211     return NULL;
1212   }
1213
1214   if (mqCheckValidHandle(handle) == FALSE) {
1215     return NULL;
1216   }
1217   if (sndHndl->sendbuf == NULL) {
1218     return NULL;
1219   }
1220   return (char *)(sndHndl->sendbuf) + sizeof(SSystemMsgHeader);
1221 }
1222
1223 EFrameworkunifiedStatus McZcSend(HANDLE hMessage) {
1224   if (hMessage == NULL) {
1225     return eFrameworkunifiedStatusInvldHandle;
1226   }
1227
1228   // QueueType checking
1229   EQType qType = GetQueueType(hMessage);
1230   if (eQTypeSender != qType) {
1231     return eFrameworkunifiedStatusInvldHndlType;
1232   }
1233
1234   SQhandle *sndHndl = (SQhandle *)hMessage;
1235
1236   SSystemMsgHeader *msgHdr = (SSystemMsgHeader *)(sndHndl->sendbuf);
1237
1238   if (mqCheckValidHandle(hMessage) == FALSE) {
1239     return eFrameworkunifiedStatusInvldHandle;
1240   }
1241   if (sndHndl->sendbuf == NULL) {
1242     return eFrameworkunifiedStatusInvldBuf;
1243   }
1244
1245   return SendMessageWithPriority(hMessage, (UI_32)(msgHdr->info.length + sizeof(SSystemMsgHeader)), sndHndl->sendbuf,
1246                                  eFrameworkunifiedMsgPrioNormal);
1247 }
1248
1249 HANDLE McZcOpenSender(PCSTR source) {
1250   HANDLE handle = openSenderZc(source);
1251
1252   if (handle != NULL) {
1253     SQhandle *sndHndl = (SQhandle *)handle;
1254     SSystemMsgHeader *msgHdr = (SSystemMsgHeader *)(sndHndl->sendbuf);
1255
1256     strlcpy(msgHdr->info.source, source, sizeof(msgHdr->info.source));
1257     msgHdr->info.command = 0;
1258     msgHdr->info.length = 0;
1259     msgHdr->info.sessionid = 0;
1260     msgHdr->eType = (UI_32)MC_EMSGT_VALID_TOKEN;
1261     msgHdr->memId = 0;
1262   }
1263
1264   return handle;
1265 }
1266
1267 EFrameworkunifiedStatus McZcClose(HANDLE handle) {
1268   return CloseSender(handle);
1269 }