Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / peripheralservice / communication / server / src / threads / Thread_Common.cpp
1 /*
2  * Copyright (c) 2019-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 #include <stdint.h>
18 #include <boost/atomic.hpp>
19 #include <pthread.h>
20
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>
25 #include <can_hal.h>
26 #include "communication_communicationlog.h"
27 #include "communication_cid.h"
28 #include "Thread_Common.h"
29
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;
33
34 void CommonInit(void) {
35   pthread_mutex_lock(&g_sender_mutex);
36   g_map_sender.clear();
37   pthread_mutex_unlock(&g_sender_mutex);
38 }
39
40 HANDLE CommonFindSender(HANDLE h_app, std::string s) {
41   HANDLE ret = NULL;
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);
45
46   pthread_mutex_lock(&g_sender_mutex);
47
48   if (it != g_map_sender.end()) {
49     ret = g_map_sender[key];
50     goto cleanup;
51   }
52
53   ret = FrameworkunifiedMcOpenSender(h_app, s.c_str());
54   if (ret) {
55     g_map_sender[key] = ret;
56   } else {
57     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedMcOpenSender Error(to:%s)", s.c_str());
58   }
59
60 cleanup:
61   pthread_mutex_unlock(&g_sender_mutex);
62   return ret;
63 }
64
65 void CommSetAvailabilityCurrent(int32_t current) {
66   availabilityCurrent |= current;
67 }
68
69 void CommClrAvailabilityCurrent(int32_t current) {
70   availabilityCurrent &= (~current);
71 }
72
73 BOOL CommGetAvailabilityCurrent(int32_t current) {
74   return !(!(availabilityCurrent & current));
75 }
76
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;
82   UI_16 z = ZONE_ERR;
83
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);
89   return e_status;
90 }
91
92 EFrameworkunifiedStatus CommonCanHalErrorNotify(HANDLE h_app) {
93   char msg[CANHAL_ERROR_MESSAGE_LEN] = {0};
94   EFrameworkunifiedStatus err = eFrameworkunifiedStatusOK;
95
96   err = FrameworkunifiedGetMsgDataOfSize(h_app, &msg, sizeof(msg), eSMRRelease);
97   if (err != eFrameworkunifiedStatusOK) {
98     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "FrameworkunifiedGetMsgDataOfSize is failed, err=%d", err);
99   }
100
101   if (err == eFrameworkunifiedStatusInvldBufSize) {
102     FrameworkunifiedClearMsgData(h_app);
103     return eFrameworkunifiedStatusFail;
104   } else if (err != eFrameworkunifiedStatusOK) {
105     return eFrameworkunifiedStatusFail;
106   }
107   FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "can_hal error : %s", msg);
108   return err;
109 }
110
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";
116   UI_16 z = ZONE_ERR;
117
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)
124     return e_status;
125
126   e_status = open_func(h_app);
127   if (e_status != eFrameworkunifiedStatusOK)
128     return e_status;
129
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)
137     return e_status;
138
139   return e_status;
140 }
141
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";
148   UI_16 z = ZONE_ERR;
149
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)
156     return e_status;
157
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)
164     return e_status;
165
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)
172     return e_status;
173
174   e_status = close_func(h_app);
175   if (e_status != eFrameworkunifiedStatusOK){
176     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: close_func() failed");
177     return e_status;
178   }
179
180   return e_status;
181 }
182