2 * Copyright (c) 2019-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.
18 #include <boost/atomic.hpp>
21 #include <native_service/frameworkunified_types.h>
22 #include <native_service/frameworkunified_framework_if.h>
23 #include <native_service/frameworkunified_timer.h>
24 #include <agl_thread.h>
26 #include "communication_communicationlog.h"
27 #include "communication_cid.h"
28 #include "Thread_Common.h"
30 static std::map<std::string, HANDLE> g_map_sender;
31 static pthread_mutex_t g_sender_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
32 static boost::atomic<int32_t> availabilityCurrent;
34 void CommonInit(void) {
35 pthread_mutex_lock(&g_sender_mutex);
37 pthread_mutex_unlock(&g_sender_mutex);
40 HANDLE CommonFindSender(HANDLE h_app, std::string s) {
42 std::string app(FrameworkunifiedGetAppName(h_app));
43 std::string key = s + app;
44 std::map<std::string, HANDLE>::iterator it = g_map_sender.find(key);
46 pthread_mutex_lock(&g_sender_mutex);
48 if (it != g_map_sender.end()) {
49 ret = g_map_sender[key];
53 ret = FrameworkunifiedMcOpenSender(h_app, s.c_str());
55 g_map_sender[key] = ret;
57 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedMcOpenSender Error(to:%s)", s.c_str());
61 pthread_mutex_unlock(&g_sender_mutex);
65 void CommSetAvailabilityCurrent(int32_t current) {
66 availabilityCurrent |= current;
69 void CommClrAvailabilityCurrent(int32_t current) {
70 availabilityCurrent &= (~current);
73 BOOL CommGetAvailabilityCurrent(int32_t current) {
74 return !(!(availabilityCurrent & current));
77 EFrameworkunifiedStatus CommonStartNotify(HANDLE h_app, PCSTR cmd) {
78 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
79 const char *result = "Unknown";
80 const char *called = "Unknown";
81 const BOOL avail = TRUE;
84 e_status = FrameworkunifiedNPPublishNotification(h_app, cmd, &avail , sizeof(avail));
85 result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
86 z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
87 called = "FrameworkunifiedNPPublishNotification";
88 FRAMEWORKUNIFIEDLOG(z, __func__, "%s(%s) %s(%d)", called, cmd, result, e_status);
92 EFrameworkunifiedStatus CommonCanHalErrorNotify(HANDLE h_app) {
93 char msg[CANHAL_ERROR_MESSAGE_LEN] = {0};
94 EFrameworkunifiedStatus err = eFrameworkunifiedStatusOK;
96 err = FrameworkunifiedGetMsgDataOfSize(h_app, &msg, sizeof(msg), eSMRRelease);
97 if (err != eFrameworkunifiedStatusOK) {
98 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "FrameworkunifiedGetMsgDataOfSize is failed, err=%d", err);
101 if (err == eFrameworkunifiedStatusInvldBufSize) {
102 FrameworkunifiedClearMsgData(h_app);
103 return eFrameworkunifiedStatusFail;
104 } else if (err != eFrameworkunifiedStatusOK) {
105 return eFrameworkunifiedStatusFail;
107 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "can_hal error : %s", msg);
111 EFrameworkunifiedStatus CommonThreadStart(HANDLE h_app, const FrameworkunifiedProtocolCallbackHandler *cb,
112 UI_32 count, PCSTR cmd, EFrameworkunifiedStatus (*open_func)(HANDLE)) {
113 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
114 const char *result = "Unknown";
115 const char *called = "Unknown";
118 e_status = FrameworkunifiedAttachCallbacksToDispatcher(h_app, FRAMEWORKUNIFIED_ANY_SOURCE, cb, count);
119 result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
120 z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
121 called = "FrameworkunifiedAttachCallbacksFromDispatcher";
122 FRAMEWORKUNIFIEDLOG(z, __func__, "%s %s(%d)", called, result, e_status);
123 if (e_status != eFrameworkunifiedStatusOK)
126 e_status = open_func(h_app);
127 if (e_status != eFrameworkunifiedStatusOK)
130 e_status = FrameworkunifiedNPRegisterNotification(h_app, cmd,
131 sizeof(EFrameworkunifiedStatus), eFrameworkunifiedStateVar);
132 result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
133 z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
134 called = "FrameworkunifiedNPRegisterNotification";
135 FRAMEWORKUNIFIEDLOG(z, __func__, "%s(%s) %s(%d)", called, cmd, result, e_status);
136 if (e_status != eFrameworkunifiedStatusOK)
142 EFrameworkunifiedStatus CommonThreadStop(HANDLE h_app, const PUI_32 cb,
143 UI_32 count, PCSTR cmd, EFrameworkunifiedStatus (*close_func)(HANDLE)) {
144 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
145 const BOOL avail = FALSE;
146 const char *result = "Unknown";
147 const char *called = "Unknown";
150 e_status = FrameworkunifiedNPPublishNotification(h_app, cmd, &avail , sizeof(avail));
151 result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
152 z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
153 called = "FrameworkunifiedNPPublishNotification";
154 FRAMEWORKUNIFIEDLOG(z, __func__, "%s(%s) %s(%d)", called, cmd, result, e_status);
155 if (e_status != eFrameworkunifiedStatusOK)
158 e_status = FrameworkunifiedNPUnRegisterNotification(h_app, cmd);
159 result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
160 z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
161 called = "FrameworkunifiedNPUnRegisterNotification";
162 FRAMEWORKUNIFIEDLOG(z, __func__, "%s(%s) %s(%d)", called, cmd, result, e_status);
163 if (e_status != eFrameworkunifiedStatusOK)
166 e_status = FrameworkunifiedDetachCallbacksFromDispatcher(h_app, FRAMEWORKUNIFIED_ANY_SOURCE, cb, count);
167 result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
168 z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
169 called = "FrameworkunifiedDetachCallbacksFromDispatcher";
170 FRAMEWORKUNIFIEDLOG(z, __func__, "%s %s(%d)", called, result, e_status);
171 if (e_status != eFrameworkunifiedStatusOK)
174 e_status = close_func(h_app);
175 if (e_status != eFrameworkunifiedStatusOK){
176 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: close_func() failed");