2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ///////////////////////////////////////////////////////////////////////////////
18 /// \ingroup tag_NSFramework
23 ///////////////////////////////////////////////////////////////////////////////
24 #include <native_service/frameworkunified_framework_if.h>
25 #include <native_service/ns_message_center_if.h>
26 #include <other_service/strlcpy.h>
27 #include "frameworkunified_framework_core.h"
28 #include "ns_mc_internal.h"
36 extern __thread HANDLE responseWaitQ;
38 //////////////////////////////////////////
39 // Function : FrameworkunifiedMcOpenSender
40 //////////////////////////////////////////
41 HANDLE FrameworkunifiedMcOpenSender(HANDLE hApp, PCSTR pName) {
42 MsgQInfo *pMsgQ = NULL;
44 if ((frameworkunifiedCheckValidAppHandle(hApp)) && (NULL != pName)) {
45 UI_32 l_ui32SrvNameLen = static_cast<UI_32>(strlen(pName));
48 * CHAR cSessionName[MAX_QUEUE_NAME_SIZE] cannot be determined to be 20 bytes including the termination NULL.
50 if ((l_ui32SrvNameLen < MAX_NAME_SIZE_APP) && (l_ui32SrvNameLen > 0)) {
51 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
52 pMsgQ = reinterpret_cast<MsgQInfo *>(malloc(sizeof(MsgQInfo)));
54 if (NULL != pMsgQ) { // LCOV_EXCL_BR_LINE 5:malloc's error case.
55 errno = EOK; // flush previous errors, if any.
56 memset(pMsgQ, 0, sizeof(MsgQInfo));
57 if (NULL == (pMsgQ->hMsgQ = McOpenSender(pName))) {
58 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__,
59 "Error::errno:%d, hApp:0x%p, %s failed to McOpenSender",
64 pMsgQ->checkCode = MSGQ_CHECK_CODE;
65 pMsgQ->self = pthread_self();
66 strlcpy(pMsgQ->cSrcName, pApp->cAppName, sizeof(pMsgQ->cSrcName));
67 strlcpy(pMsgQ->cMsgQName, pName, sizeof(pMsgQ->cMsgQName));
72 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
73 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__,
74 "Invalid param. Name:%s, Following must be true: %d > len of passed name(%u) > 0.",
75 pName, MAX_NAME_SIZE_APP, l_ui32SrvNameLen);
79 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__,
80 "Invalid param. hApp:0x%p, name:0x%p",
81 hApp, pName); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
84 return static_cast<HANDLE>(pMsgQ);
88 //////////////////////////////////////////
89 // Function : FrameworkunifiedMcClose
90 //////////////////////////////////////////
91 EFrameworkunifiedStatus FrameworkunifiedMcClose(HANDLE hService) {
92 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
93 if (frameworkunifiedCheckValidMsgQ(hService)) {
94 MsgQInfo *pMsgQ = reinterpret_cast<MsgQInfo *>(hService);
96 if (NULL != pMsgQ->hMsgQ) {
97 eStatus = McClose(pMsgQ->hMsgQ);
102 hService = NULL; // This has no effect because hService is not passed as reference.
104 eStatus = eFrameworkunifiedStatusInvldHandle;
110 //////////////////////////////////////////
111 // Function : FrameworkunifiedSendMsg
112 //////////////////////////////////////////
113 EFrameworkunifiedStatus FrameworkunifiedSendMsg(HANDLE hService, UI_32 iCmd, UI_32 length, PCVOID data) {
114 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
115 if (frameworkunifiedCheckValidMsgQ(hService)) {
116 MsgQInfo *pMsgQ = reinterpret_cast<MsgQInfo *>(hService);
118 if ((NULL != pMsgQ->hMsgQ) && (0 != std::strlen(pMsgQ->cSrcName))) {
119 eStatus = McSendWithSession(pMsgQ->hMsgQ, pMsgQ->cSrcName, iCmd, length, data, pMsgQ->sessionId);
121 eStatus = eFrameworkunifiedStatusInvldHandle;
124 eStatus = eFrameworkunifiedStatusNullPointer;
126 // check if McSendWithSession failed with error destination msg queue full
127 // log the information
128 if (eFrameworkunifiedStatusMsgQFull == eStatus) {
129 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error!! Message queue full of receiver '%s' while sending CmdId = 0x%X",
130 FrameworkunifiedGetSessionName(hService), iCmd);
136 //////////////////////////////////////////
137 // Function : FrameworkunifiedSendPriorityMsg
138 //////////////////////////////////////////
139 EFrameworkunifiedStatus FrameworkunifiedSendPriorityMsg(HANDLE hService, UI_32 iCmd, UI_32 length, PCVOID data) {
140 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
141 if (frameworkunifiedCheckValidMsgQ(hService)) {
142 MsgQInfo *pMsgQ = reinterpret_cast<MsgQInfo *>(hService);
143 eStatus = McSendWithPriority(pMsgQ->hMsgQ, pMsgQ->cSrcName, iCmd,
144 length, data, eFrameworkunifiedMsgPrioEmergency, pMsgQ->sessionId);
146 eStatus = eFrameworkunifiedStatusNullPointer;
152 //////////////////////////////////////////
153 // Function: FrameworkunifiedSendSelf
154 //////////////////////////////////////////
155 EFrameworkunifiedStatus FrameworkunifiedSendSelf(HANDLE hApp, UI_32 iCmd, UI_32 length, PCVOID data) {
156 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
157 if (frameworkunifiedCheckValidAppHandle(hApp)) {
158 CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast< CFrameworkunifiedFrameworkApp * >(hApp);
159 eStatus = McSend(pApp->hAppSndMsgQ, &pApp->cAppName[ 0 ], iCmd, length, data);
161 eStatus = eFrameworkunifiedStatusNullPointer;
166 //////////////////////////////////////////
167 // Function : FrameworkunifiedInvokeSync
168 //////////////////////////////////////////
169 EFrameworkunifiedStatus FrameworkunifiedInvokeSync(HANDLE hService, UI_32 iCmd, UI_32 msgLenght, PCVOID msgData, UI_32 responseLength,
170 PVOID responseData, UI_32 *receivedLength) {
171 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
173 if (frameworkunifiedCheckValidMsgQ(hService)) {
174 if (NULL != receivedLength) {
175 MsgQInfo *pMsgQ = static_cast<MsgQInfo *>(hService);
177 if (responseWaitQ == NULL) {
178 char mc_invoker_name[MAX_QUEUE_NAME_SIZE];
179 InvokerName invokerName;
180 // LCOV_EXCL_BR_START 11:except branch
181 McCreateInvokerName(pMsgQ->cSrcName, pMsgQ->sessionId, mc_invoker_name, sizeof(mc_invoker_name));
183 invokerName = mc_invoker_name;
185 responseWaitQ = McOpenSyncReceiver(invokerName.c_str());
186 if (NULL == responseWaitQ) {
187 eStatus = eFrameworkunifiedStatusFail;
191 if (eStatus == eFrameworkunifiedStatusOK) {
192 eStatus = McInvokeSync(pMsgQ->hMsgQ, pMsgQ->cSrcName, iCmd, msgLenght, msgData, pMsgQ->sessionId,
193 responseWaitQ, responseLength, responseData, receivedLength);
196 eStatus = eFrameworkunifiedStatusInvldParam;
199 eStatus = eFrameworkunifiedStatusNullPointer;
205 //////////////////////////////////////////
206 // Function : frameworkunifiedSendSyncResponse
207 //////////////////////////////////////////
208 EFrameworkunifiedStatus frameworkunifiedSendSyncResponse(HANDLE hApp, UI_32 iCmd, UI_32 seq_id, EFrameworkunifiedStatus retValue, UI_32 length, PCVOID data) {
209 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
211 if (frameworkunifiedCheckValidAppHandle(hApp)) {
212 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
213 HANDLE responseSendQ;
215 char mc_invoker_name[MAX_QUEUE_NAME_SIZE];
216 InvokerName invokerName;
217 PVOID rcv_buf = static_cast<PVOID>(pApp->uiMsgRcvBuffer);
218 McCreateInvokerName(McGetMsgSrc(rcv_buf), mcGetMsgSsessionId(rcv_buf), mc_invoker_name, sizeof(mc_invoker_name));
219 invokerName = mc_invoker_name;
221 InvokeResponseQTable::iterator s_iterator = pApp->invokeResQTable.find(invokerName);
222 if (s_iterator != pApp->invokeResQTable.end()) {
223 responseSendQ = s_iterator->second;
225 responseSendQ = McOpenSyncSender(invokerName.c_str()); // LCOV_EXCL_BR_LINE 11:except branch
228 * FrameworkunifiedSendSyncResponse() uses McOpenSyncSender() to create a synchronous communication response queue handle,
229 * but there is no process to release the queue handle after the response.
231 if (NULL == responseSendQ) {
232 eStatus = eFrameworkunifiedStatusFail;
234 pApp->invokeResQTable[invokerName] = responseSendQ; // LCOV_EXCL_BR_LINE 11:except branch
238 if (eStatus == eFrameworkunifiedStatusOK) {
239 eStatus = McSendSyncResponse(responseSendQ, pApp->cAppName, iCmd, seq_id, retValue, length, data);
242 eStatus = eFrameworkunifiedStatusNullPointer;
247 //////////////////////////////////////////
248 // Function : FrameworkunifiedDispatchProcess
249 //////////////////////////////////////////
250 EFrameworkunifiedStatus FrameworkunifiedSetSyncResponseData(HANDLE hApp, PVOID data, UI_32 size) {
251 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
255 * When the response data is 0 bytes, it can be transmitted as SYNC data.
257 if (frameworkunifiedCheckValidAppHandle(hApp) && data != NULL) {
258 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
260 pApp->responsedata.set(data, size); // LCOV_EXCL_BR_LINE 11:except branch
262 catch(std::bad_alloc) {
263 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __PRETTY_FUNCTION__, "bad_alloc");
264 eStatus = eFrameworkunifiedStatusFail;
267 eStatus = eFrameworkunifiedStatusNullPointer;
270 return eStatus; // LCOV_EXCL_BR_LINE 11:except branch
273 //////////////////////////////////////////
274 // Function : FrameworkunifiedGetMsgLength
275 //////////////////////////////////////////
276 UI_32 FrameworkunifiedGetMsgLength(HANDLE hApp) {
277 if (frameworkunifiedCheckValidAppHandle(hApp)) {
278 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
279 return McGetLength(static_cast<PVOID>(pApp->uiMsgRcvBuffer));
285 //////////////////////////////////////////
286 // Function : FrameworkunifiedGetMsgProtocol
287 //////////////////////////////////////////
288 UI_32 FrameworkunifiedGetMsgProtocol(HANDLE hApp) {
289 if (frameworkunifiedCheckValidAppHandle(hApp)) {
290 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
291 return static_cast<UI_32>(pApp->uiProtocolCmd);
297 //////////////////////////////////////////
298 // Function : FrameworkunifiedGetMsgDataOfSize
299 //////////////////////////////////////////
300 EFrameworkunifiedStatus FrameworkunifiedGetMsgDataOfSize(HANDLE hApp, PVOID pData, UI_32 uiSize, ESMRetrieveTypes eRetrieveMethod) {
301 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
302 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
304 if (frameworkunifiedCheckValidAppHandle(hApp)) {
307 * The behavior is the same as when "eSMRRetain" is set without getting an error when the ESMRetrieveTypes is not appropriate.
309 if (eSMRRelease == eRetrieveMethod) {
310 eStatus = McGetDataOfSize(static_cast<PVOID>(pApp->uiMsgRcvBuffer), pData, uiSize);
312 eStatus = McGetDataOfSizeWithSMRetain(static_cast<PVOID>(pApp->uiMsgRcvBuffer), pData, uiSize);
315 eStatus = eFrameworkunifiedStatusNullPointer;
322 //////////////////////////////////////////
323 // Function : FrameworkunifiedGetDataPointer
324 //////////////////////////////////////////
325 EFrameworkunifiedStatus FrameworkunifiedGetDataPointer(HANDLE hApp, void **datap) {
326 if (frameworkunifiedCheckValidAppHandle(hApp)) {
328 return eFrameworkunifiedStatusInvldParam;
331 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
333 if ((dt = McGetDataPointer(reinterpret_cast<PVOID>(pApp->uiMsgRcvBuffer))) == NULL) {
334 return eFrameworkunifiedStatusInvldBufSize;
338 return eFrameworkunifiedStatusOK;
340 return eFrameworkunifiedStatusInvldHandle;
344 //////////////////////////////////////////
345 // Function : FrameworkunifiedGetMsgSrc
346 //////////////////////////////////////////
347 PCSTR FrameworkunifiedGetMsgSrc(HANDLE hApp) {
348 if (frameworkunifiedCheckValidAppHandle(hApp)) {
349 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
350 return (reinterpret_cast<PCSTR>(pApp->cMsgSrcName));
356 //////////////////////////////////////////
357 // Function : FrameworkunifiedGetMsgNotification
358 //////////////////////////////////////////
359 PCSTR FrameworkunifiedGetMsgNotification(HANDLE hApp) {
360 if (frameworkunifiedCheckValidAppHandle(hApp)) {
361 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
362 return (reinterpret_cast<PCSTR>(pApp->cSystemInfo));
368 //////////////////////////////////////////
369 // Function : FrameworkunifiedClearMsgData
370 //////////////////////////////////////////
371 EFrameworkunifiedStatus FrameworkunifiedClearMsgData(HANDLE hApp) {
372 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
373 if (frameworkunifiedCheckValidAppHandle(hApp)) {
374 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
375 eStatus = McClearData(static_cast<PVOID>(pApp->uiMsgRcvBuffer));
377 eStatus = eFrameworkunifiedStatusNullPointer;
383 //////////////////////////////////////////
384 // Function : FrameworkunifiedGetDataUSID
385 //////////////////////////////////////////
386 TMemID FrameworkunifiedGetDataUSID(HANDLE hApp) {
387 if (frameworkunifiedCheckValidAppHandle(hApp)) {
388 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
389 return McGetDataUSID(static_cast<PVOID>(pApp->uiMsgRcvBuffer));
395 //////////////////////////////////////////
396 // Function : FrameworkunifiedForwardMessage
397 //////////////////////////////////////////
398 EFrameworkunifiedStatus FrameworkunifiedForwardMessage(HANDLE hApp, HANDLE hChildQ, UI_32 iCmd) {
399 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldBuf;
401 if (frameworkunifiedCheckValidAppHandle(hApp) && hChildQ) {
402 CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast< CFrameworkunifiedFrameworkApp * >(hApp);
404 eStatus = McForward(hChildQ, &pApp->cAppName[ 0 ], iCmd, FrameworkunifiedGetDataUSID(hApp));
406 eStatus = eFrameworkunifiedStatusInvldHandle;
413 //////////////////////////////////////////
414 // Function : FrameworkunifiedGetSystemInfo
415 //////////////////////////////////////////
416 EFrameworkunifiedStatus FrameworkunifiedGetSystemInfo(HANDLE hApp, PVOID pSystemInfo) {
417 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldBuf;
419 if (frameworkunifiedCheckValidAppHandle(hApp) && pSystemInfo != NULL) { // LCOV_EXCL_BR_LINE 11:except branch
420 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
422 eStatus = McGetSysInfoData(pApp->uiMsgRcvBuffer, pSystemInfo);
423 // memcpy(pSystemInfo, pApp->cSystemInfo, _countof(pApp->cSystemInfo));
424 // eStatus = eFrameworkunifiedStatusOK;
430 //////////////////////////////////////////
431 // Function : frameworkunifiedGetIsTypeOfSync
432 //////////////////////////////////////////
433 BOOL frameworkunifiedGetIsTypeOfSync(HANDLE hApp) {
434 if (frameworkunifiedCheckValidAppHandle(hApp)) {
435 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
436 return mcGetIsTypeOfSync(pApp->uiMsgRcvBuffer);
442 ////////////////////////////////////////////////////////////////////////////////////////////
443 /// FrameworkunifiedGetLastNotification
444 /// Returns the last notification string that was received.
447 /// HANDLE - Application framework handle
449 /// \return pSourceName
450 /// PCSTR - the last notification name that has been received.
452 /// \see FrameworkunifiedMcOpenSender, FrameworkunifiedMcClose, FrameworkunifiedSendMsg, FrameworkunifiedGetMsgLength, FrameworkunifiedGetMsgDataOfSize,
453 /// FrameworkunifiedClearMsgData, FrameworkunifiedGetDataUSID, FrameworkunifiedForwardMessage, FrameworkunifiedGetSystemInfo
455 ////////////////////////////////////////////////////////////////////////////////////////////
456 PCSTR FrameworkunifiedGetLastNotification(HANDLE hApp) {
457 if (frameworkunifiedCheckValidAppHandle(hApp)) {
458 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
459 return static_cast<PCSTR>(pApp->cSystemInfo);
466 EFrameworkunifiedStatus FrameworkunifiedSendResponse(HANDLE hApp, UI_32 iCmd, UI_32 length, PCVOID data) {
467 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
468 if (frameworkunifiedCheckValidAppHandle(hApp)) {
469 HANDLE hSession = FrameworkunifiedGetCurrentSessionHandle(hApp);
473 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedSendMsg(hSession, iCmd, length, data))) {
474 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
475 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __PRETTY_FUNCTION__,
476 "Response send Failed Status:0x%x ", eStatus);
479 FRAMEWORKUNIFIEDLOG0(ZONE_NS_INFO, __PRETTY_FUNCTION__, "Response sent ");
486 EFrameworkunifiedStatus FrameworkunifiedSendRequest(HANDLE hApp, PCSTR pServerName, UI_32 uiSessionId, UI_32 iCmd,
487 UI_32 length, PCVOID data) {
488 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
489 if (frameworkunifiedCheckValidAppHandle(hApp)) {
490 HANDLE hSession = FrameworkunifiedGetSessionHandle(hApp, pServerName, uiSessionId);
493 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedSendMsg(hSession, iCmd, length, data))) {
494 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __PRETTY_FUNCTION__,
495 "Response send Failed Status:0x%x ", eStatus);
497 FRAMEWORKUNIFIEDLOG0(ZONE_NS_INFO, __PRETTY_FUNCTION__, "Response sent ");