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_HMI_CWORD77_Controller
21 ////////////////////////////////////////////////////////////////////////////////
23 #include <native_service/frameworkunified_service_protocol.h>
24 #include <native_service/frameworkunified_framework_if.h>
25 #include <native_service/frameworkunified__CWORD77__service_if.h>
26 #include <native_service/ns__CWORD77__data_pool_if.h>
27 #include <other_service/strlcpy.h>
33 #include "ns__CWORD77__data_pool_internal.h"
34 #include "ns__CWORD77__common_internal.h"
36 #define NS__CWORD77__MAX_SESSION_RETRY_COUNT 10
38 //////////////////////////////////////////
39 /// Function : CFrameworkunified_CWORD77_Service
40 //////////////////////////////////////////
41 CFrameworkunified_CWORD77_Service::CFrameworkunified_CWORD77_Service()
46 m_bServiceAvailable(FALSE),
47 m_ui8SessionRetryCount(0),
48 m_hApp(NULL) { // LCOV_EXCL_BR_LINE 11:except branch
49 m_SessTypeToSessObj.clear();
51 //////////////////////////////////////////
52 /// Function : ~CFrameworkunified_CWORD77_Service
53 //////////////////////////////////////////
54 CFrameworkunified_CWORD77_Service::~CFrameworkunified_CWORD77_Service() { // LCOV_EXCL_BR_LINE 11:except branch
55 std::map<UI_32, CFrameworkunified_CWORD77_Session *>::iterator l_iter;
56 l_iter = m_SessTypeToSessObj.begin();
57 while (l_iter != m_SessTypeToSessObj.end()) {
58 CFrameworkunified_CWORD77_Session *l_pTemp = l_iter->second;
59 DELETEPTR(l_pTemp); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns__CWORD77__common.h"
63 //////////////////////////////////////////
64 /// Function : SetResponseCallback
65 //////////////////////////////////////////
66 void CFrameworkunified_CWORD77_Service::SetResponseCallback(ResponseServiceTo_CWORD77_ cbResponse) {
67 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
68 m_cbResponse = cbResponse;
69 if (m_cbResponse == NULL) {
70 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __PRETTY_FUNCTION__, "Call back function ptr is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
72 std::map<UI_32, CFrameworkunified_CWORD77_Session *>::iterator l_iter;
73 l_iter = m_SessTypeToSessObj.begin();
74 while (l_iter != m_SessTypeToSessObj.end()) {
75 if (l_iter->second != NULL) {
76 l_iter->second->SetResponseCallback(m_cbResponse);
78 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Object ptr for session type[%d] is NULL ", l_iter->first);
82 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
84 //////////////////////////////////////////
85 /// Function : SetSession
86 //////////////////////////////////////////
87 void CFrameworkunified_CWORD77_Service::SetSession(CFrameworkunified_CWORD77_Session *f_pSessionObj) {
88 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
89 m_SessTypeToSessObj[f_pSessionObj->GetSessionType()] = f_pSessionObj;
90 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
92 //////////////////////////////////////////
93 /// Function : SendMessageToSession
94 //////////////////////////////////////////
95 void CFrameworkunified_CWORD77_Service::SendMessageToSession(UI_32 f_uiSessionType, UI_32 f_uiSrvProtocol) {
96 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");
97 std::map<UI_32, CFrameworkunified_CWORD77_Session *>::iterator l_iter;
98 l_iter = m_SessTypeToSessObj.begin();
99 l_iter = m_SessTypeToSessObj.find(f_uiSessionType);
100 if (l_iter != m_SessTypeToSessObj.end()) {
101 if (l_iter->second != NULL) {
102 if (eFrameworkunifiedStatusSessionErr == l_iter->second->HandleSessionMessage(f_uiSrvProtocol)) {
103 if (m_bServiceAvailable) {
104 if (NS__CWORD77__MAX_SESSION_RETRY_COUNT < m_ui8SessionRetryCount) {
105 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "Unable to send cmd 0x%X."
106 " But, service is available. Open session req max limit reached [%s].",
107 f_uiSrvProtocol, m_cServiceName.c_str());
108 // TODO(framework_unified): what to do in case of max limit reached?
110 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "Unable to send cmd 0x%X."
111 " But, service is available. Re-sending open session req [%s].",
112 f_uiSrvProtocol, m_cServiceName.c_str());
113 // send open session request
114 OpenSessionRetry(f_uiSessionType);
115 m_ui8SessionRetryCount++;
118 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "Unable to send cmd 0x%X."
119 " Service not yet available.", f_uiSrvProtocol);
122 // ignore error, already printed by HandleSessionMessage
125 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Object ptr for session type[%d] is NULL ", l_iter->first);
128 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Object is not there in Map for session type %d ", f_uiSessionType);
130 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-");
132 //////////////////////////////////////////
133 /// Function : OpenSession
134 //////////////////////////////////////////
135 void CFrameworkunified_CWORD77_Service::OpenSession(HANDLE f_hApp, const std::string &f_sServiceName) {
136 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __PRETTY_FUNCTION__, "+"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
137 m_cServiceName = f_sServiceName;
138 std::map<UI_32, CFrameworkunified_CWORD77_Session *>::iterator l_iter;
139 l_iter = m_SessTypeToSessObj.begin();
140 while (l_iter != m_SessTypeToSessObj.end()) {
141 if (l_iter->second != NULL) {
142 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, " Sent open session request for session %d ", l_iter->first); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
143 l_iter->second->OpenSession(f_hApp, f_sServiceName); // LCOV_EXCL_BR_LINE 11:except branch
145 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Object ptr for session type[%d] is NULL ", l_iter->first);
149 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __PRETTY_FUNCTION__, "-"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
151 //////////////////////////////////////////
152 /// Function : CloseSession
153 //////////////////////////////////////////
154 void CFrameworkunified_CWORD77_Service::CloseSession(HANDLE f_hApp) {
155 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
156 std::map<UI_32, CFrameworkunified_CWORD77_Session *>::iterator l_iter;
157 l_iter = m_SessTypeToSessObj.begin();
158 while (l_iter != m_SessTypeToSessObj.end()) {
159 if (l_iter->second != NULL) {
160 l_iter->second->CloseSession(f_hApp);
162 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Object ptr for session type[%d] is NULL ", l_iter->first);
166 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
168 //////////////////////////////////////////
169 /// Function : OpenSessionAcks
170 //////////////////////////////////////////
171 void CFrameworkunified_CWORD77_Service::OpenSessionAcks(HANDLE f_hApp) {
172 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");
173 OpenSessionAck tOpenSessAck;
175 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
176 m_ui8SessionRetryCount = 0;
178 if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedGetMsgDataOfSize(f_hApp, &tOpenSessAck, sizeof(tOpenSessAck)))) {
179 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " FrameworkunifiedGetMsgDataOfSize Failed"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
180 l_eStatus = eFrameworkunifiedStatusFail;
182 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
183 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __PRETTY_FUNCTION__, "/***********************************************************************/");
184 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_,
186 "Open session ACK obtained for service: [%s] with session type: [%d]",
187 tOpenSessAck.cSessionName != 0 ? tOpenSessAck.cSessionName : NULL,
188 tOpenSessAck.sessionType);
189 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __PRETTY_FUNCTION__, "/***********************************************************************/");
191 UI_32 l_uisessionType = tOpenSessAck.sessionType;
193 std::map<UI_32, CFrameworkunified_CWORD77_Session *>::iterator l_iter;
194 l_iter = m_SessTypeToSessObj.begin();
195 while (l_iter != m_SessTypeToSessObj.end()) {
196 if (l_iter->second != NULL) {
197 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __PRETTY_FUNCTION__, "Get session type = %d", l_iter->second->GetSessionType()); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
198 if (l_uisessionType == l_iter->second->GetSessionType()) {
199 l_iter->second->OpenSessionAcknowledge(f_hApp);
201 if (m_cbSessionACK != NULL) {
202 (VOID)m_cbSessionACK(f_hApp, m_cServiceName, tOpenSessAck.cSessionName, tOpenSessAck.sessionType,
203 tOpenSessAck.eStatus);
206 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR,
208 "Unexpected Session_Type from service [%s]. Obtained Session_Type: [%d] != Expected Session_Type: [%d]",
209 tOpenSessAck.cSessionName != 0 ? tOpenSessAck.cSessionName : NULL,
211 l_iter->second->GetSessionType()); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
214 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Object ptr for session type[%d] is NULL ", l_iter->first);
218 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
220 //////////////////////////////////////////
221 /// Function : OpenServiceOnAvailability
222 //////////////////////////////////////////
223 EFrameworkunifiedStatus CFrameworkunified_CWORD77_Service::OpenServiceOnAvailability(HANDLE f_hApp, const std::string &f_sServiceName) {
224 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
225 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
227 m_bServiceAvailable = TRUE;
228 m_ui8SessionRetryCount = 0;
229 AttachOpenSessionAckCallback(f_hApp);
230 OpenSession(f_hApp, f_sServiceName);
231 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
234 //////////////////////////////////////////
235 /// Function : CloseServiceOnUnavailability
236 //////////////////////////////////////////
237 EFrameworkunifiedStatus CFrameworkunified_CWORD77_Service::CloseServiceOnUnavailability(HANDLE f_hApp) {
238 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
239 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
240 m_bServiceAvailable = FALSE;
241 m_ui8SessionRetryCount = 0;
242 CloseSession(f_hApp);
243 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
247 //////////////////////////////////////////
248 /// Function : AddNotification
249 //////////////////////////////////////////
250 void CFrameworkunified_CWORD77_Service::AddNotification(PCSTR f_pNotification) {
251 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
252 FrameworkunifiedNotificationCallbackHandler tNotifyStruct = {};
255 * Defining a NULL for argument f_pNotification results in a segmentation fault.
257 strlcpy(tNotifyStruct.cNotification, f_pNotification, sizeof(tNotifyStruct.cNotification)); // LCOV_EXCL_BR_LINE 11:except branch
258 tNotifyStruct.callBack = CCallback< CFrameworkunified_CWORD77_Service, &CFrameworkunified_CWORD77_Service::OnServiceNtf >::set(this); // LCOV_EXCL_BR_LINE 11:except branch
259 m_vServiceNotifications.push_back(tNotifyStruct); // LCOV_EXCL_BR_LINE 11:except branch
260 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
263 //////////////////////////////////////////
264 /// Function : SubscribeNotifications
265 //////////////////////////////////////////
266 EFrameworkunifiedStatus CFrameworkunified_CWORD77_Service::SubscribeNotifications(HANDLE f_hApp) {
267 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
268 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
269 size_t Size = m_vServiceNotifications.size();
270 for (size_t i = 0; i < Size; i++) {
271 if (m_vServiceNotifications[i].callBack != NULL) {
272 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "Before subscribing notifications"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
273 if (eFrameworkunifiedStatusOK != FrameworkunifiedSubscribeNotificationWithCallback(f_hApp, m_vServiceNotifications[i].cNotification,
274 m_vServiceNotifications[i].callBack)) {
275 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedSubscribeNotificationWithCallback fail"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
279 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
282 //////////////////////////////////////////
283 /// Function : UnSubscribeNotifications
284 //////////////////////////////////////////
285 EFrameworkunifiedStatus CFrameworkunified_CWORD77_Service::UnSubscribeNotifications(HANDLE f_hApp) {
286 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");
287 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
288 size_t Size = m_vServiceNotifications.size();
289 for (size_t i = 0; i < Size; i++) {
290 if (eFrameworkunifiedStatusOK != FrameworkunifiedUnsubscribeNotificationWithCallback(f_hApp, m_vServiceNotifications[i].cNotification)) {
291 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedUnsubscribeNotificationWithCallback fail");
294 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-");
298 //////////////////////////////////////////
299 /// Function : OnServiceNtf
300 //////////////////////////////////////////
301 EFrameworkunifiedStatus CFrameworkunified_CWORD77_Service::OnServiceNtf(HANDLE hApp) {
302 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");
303 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
305 // CHAR l_cNotificationName[MAX_SYS_INFO_SIZE] = {};
306 PCSTR l_cNotificationName = FrameworkunifiedGetLastNotification(hApp);
307 // retrieve notification name
308 // FrameworkunifiedGetSystemInfo(hApp, l_cNotificationName);
310 UI_32 l_uiLength = FrameworkunifiedGetMsgLength(hApp);
311 if (l_uiLength != 0) {
312 std::vector<char> data;
313 data.resize(l_uiLength);
314 if (eFrameworkunifiedStatusOK == (l_eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, &data[0], l_uiLength))) {
315 SetRespNotfnDataIn_CWORD77_DataPool(l_cNotificationName, static_cast<UI_32>(data.size()), &data[0]);
317 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " FrameworkunifiedGetMsgDataOfSize Error, "
318 "Resp Notfn Data not set in _CWORD77_ data pool, status: 0x%x", l_eStatus);
321 SetRespNotfnDataIn_CWORD77_DataPool(l_cNotificationName, 0, NULL);
324 m_cbResponse(hApp, NOTIFICATION, m_cServiceName);
326 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-");
331 //////////////////////////////////////////
332 /// Function : OpenSessionRetry
333 //////////////////////////////////////////
334 void CFrameworkunified_CWORD77_Service::OpenSessionRetry(UI_32 f_ui32SessionType) {
335 FRAMEWORKUNIFIEDLOG(ZONE_NS_FUNC, __FUNCTION__, "+");
336 if (!m_cServiceName.empty() && (NULL != m_hApp)) {
337 std::map<UI_32, CFrameworkunified_CWORD77_Session *>::iterator l_iter;
338 l_iter = m_SessTypeToSessObj.find(f_ui32SessionType);
339 if (l_iter != m_SessTypeToSessObj.end()) {
340 if (NULL != l_iter->second) {
341 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "Re-sending open session req for session type %d ", l_iter->first);
342 l_iter->second->OpenSession(m_hApp, m_cServiceName);
344 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Object ptr for session type[%d] is NULL ", l_iter->first);
348 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Can't retry for open session req. Service name: [%s], hApp:[%p].",
349 m_cServiceName.c_str(),
352 FRAMEWORKUNIFIEDLOG(ZONE_NS_FUNC, __FUNCTION__, "-");
355 //////////////////////////////////////////
356 /// Function : Set_CWORD77_OpenSessionACK
357 //////////////////////////////////////////
358 void CFrameworkunified_CWORD77_Service::Set_CWORD77_OpenSessionACK(SessionAckTo_CWORD77_ cbResponse) {
359 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
361 m_cbSessionACK = cbResponse;
362 if (NULL == m_cbSessionACK) {
363 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __PRETTY_FUNCTION__, "Call back function ptr is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
366 FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"