Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / framework_unified / client / NS__CWORD77__ServiceIf / src / frameworkunified__CWORD77__session_if.cpp
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_HMI_CWORD77_Controller
19 /// \brief
20 ///
21 ////////////////////////////////////////////////////////////////////////////////
22
23 #include <native_service/frameworkunified__CWORD77__session_if.h>
24 #include <native_service/ns__CWORD77__data_pool_if.h>
25 #include <boost/bind.hpp>
26 #include <utility>
27 #include <vector>
28 #include "ns__CWORD77__data_pool_internal.h"
29 #include "ns__CWORD77__common_internal.h"
30
31 //////////////////////////////////////////
32 /// Function : CFrameworkunified_CWORD77_Session
33 //////////////////////////////////////////
34 CFrameworkunified_CWORD77_Session::CFrameworkunified_CWORD77_Session() {
35   m_hSession = NULL;
36   m_hApp = NULL;
37   m_hService = NULL;
38   m_uiSessionType = 0;
39 }
40 //////////////////////////////////////////
41 /// Function : ~CFrameworkunified_CWORD77_Session
42 //////////////////////////////////////////
43 CFrameworkunified_CWORD77_Session::~CFrameworkunified_CWORD77_Session() {  // LCOV_EXCL_BR_LINE 11:except branch
44 }
45
46 void CFrameworkunified_CWORD77_Session::SetSessionType(const UI_32 &sessioType) {
47   m_uiSessionType = sessioType;
48 }
49
50 //////////////////////////////////////////
51 /// Function : GetSessionType
52 //////////////////////////////////////////
53 UI_32 CFrameworkunified_CWORD77_Session::GetSessionType() {
54   return m_uiSessionType;
55 }
56 //////////////////////////////////////////
57 /// Function : SetResponseCallback
58 //////////////////////////////////////////
59 void CFrameworkunified_CWORD77_Session::SetResponseCallback(ResponseServiceTo_CWORD77_ f_cbResponse) {
60   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");   // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
61   m_ResponseTo_CWORD77_ = f_cbResponse;
62   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-");   // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
63 }
64 //////////////////////////////////////////
65 /// Function : AddProtocolHandler
66 //////////////////////////////////////////
67 void CFrameworkunified_CWORD77_Session::AddProtocolHandler(UI_32 f_uiCmdId, CbFuncPtr f_cbFuncPtr) {
68   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");
69   FrameworkunifiedProtocolCallbackHandler l_tProtocolCallback;
70   l_tProtocolCallback.iCmd = f_uiCmdId;
71   l_tProtocolCallback.callBack = f_cbFuncPtr;
72   m_vSessionProtocolHanlder.push_back(l_tProtocolCallback);
73   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-");
74 }
75 //////////////////////////////////////////
76 /// Function : AddEventHandler
77 //////////////////////////////////////////
78 void CFrameworkunified_CWORD77_Session::AddEventHandler(UI_32 f_uiCmdId, CbFuncPtr f_cbFuncPtr) {
79   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");
80   FrameworkunifiedProtocolCallbackHandler l_tProtocolCallback;
81   l_tProtocolCallback.iCmd = f_uiCmdId;
82   l_tProtocolCallback.callBack = f_cbFuncPtr;
83   m_vSessionEventHandler.push_back(l_tProtocolCallback);
84   m_vEventVector.push_back(f_uiCmdId);  // Code added by Shyam
85   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-");
86 }
87 //////////////////////////////////////////
88 /// Function : AttachProtocolwithDispatcher
89 //////////////////////////////////////////
90 EFrameworkunifiedStatus CFrameworkunified_CWORD77_Session::AttachProtocolwithDispatcher(HANDLE f_hApp) {
91   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");
92   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
93   size_t l_uiSize = m_vSessionProtocolHanlder.size();
94   PCSTR l_pRequester   = FrameworkunifiedGetMsgSrc(f_hApp);
95   for (size_t i = 0; i < l_uiSize; i++) {
96     FrameworkunifiedAttachCallbackToDispatcher(f_hApp, l_pRequester, m_vSessionProtocolHanlder[i].iCmd,
97                                   m_vSessionProtocolHanlder[i].callBack, m_hSession);
98   }
99   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-");
100   return l_eStatus;
101 }
102 //////////////////////////////////////////
103 /// Function : DetachProtocolwithDispatcher
104 //////////////////////////////////////////
105 EFrameworkunifiedStatus CFrameworkunified_CWORD77_Session::DetachProtocolwithDispatcher(HANDLE f_hApp) {
106   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");
107   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
108   size_t l_uiSize = m_vSessionProtocolHanlder.size();
109   PCSTR l_pRequester   = FrameworkunifiedGetMsgSrc(f_hApp);
110   for (size_t i = 0; i < l_uiSize; i++) {
111     FrameworkunifiedDetachCallbackFromDispatcher(f_hApp, l_pRequester, m_vSessionProtocolHanlder[i].iCmd,
112                                     m_hSession);
113   }
114   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-");
115   return l_eStatus;
116 }
117 //////////////////////////////////////////
118 /// Function : AttachEventswithDispatcher
119 //////////////////////////////////////////
120 EFrameworkunifiedStatus CFrameworkunified_CWORD77_Session::AttachEventswithDispatcher(HANDLE f_hApp) {
121   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");
122   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
123   size_t l_uiSize = m_vEventVector.size();
124
125   for (size_t i = 0; i < l_uiSize; i++) {
126     l_eStatus = FrameworkunifiedRegisterEvent(m_hSession, m_vEventVector[i]);
127   }
128   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-");
129   return l_eStatus;
130 }
131 //////////////////////////////////////////
132 /// Function : AttachEventswithDispatcher
133 //////////////////////////////////////////
134 EFrameworkunifiedStatus CFrameworkunified_CWORD77_Session::RegisterEvents(HANDLE f_hApp) {
135   // Function: added by Shyam to overcome the naming conflict in registering the event
136   return AttachEventswithDispatcher(f_hApp);
137 }
138
139 //////////////////////////////////////////
140 /// Function : AttachEventHandlerswithDispatcher
141 //////////////////////////////////////////
142 EFrameworkunifiedStatus CFrameworkunified_CWORD77_Session::AttachEventHandlerswithDispatcher(HANDLE f_hApp) {
143   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");
144   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
145   size_t l_uiSize = m_vSessionEventHandler.size();
146   PCSTR l_pRequester   = FrameworkunifiedGetMsgSrc(f_hApp);
147   for (size_t i = 0; i < l_uiSize; i++) {
148     FrameworkunifiedAttachCallbackToDispatcher(f_hApp, l_pRequester, m_vSessionEventHandler[i].iCmd,
149                                   m_vSessionEventHandler[i].callBack, m_hSession);
150   }
151   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-");
152   return l_eStatus;
153 }
154 //////////////////////////////////////////
155 /// Function : DetachEventHandlerswithDispatcher
156 //////////////////////////////////////////
157 EFrameworkunifiedStatus CFrameworkunified_CWORD77_Session::DetachEventHandlerswithDispatcher(HANDLE f_hApp) {
158   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");
159   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
160   size_t l_uiSize = m_vSessionEventHandler.size();
161   PCSTR l_pRequester   = FrameworkunifiedGetMsgSrc(f_hApp);
162   for (size_t i = 0; i < l_uiSize; i++) {
163     FrameworkunifiedDetachCallbackFromDispatcher(f_hApp, l_pRequester, m_vSessionEventHandler[i].iCmd,
164                                     m_hSession);
165   }
166   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-");
167   return l_eStatus;
168 }
169
170 //////////////////////////////////////////
171 /// Function : HandleSessionMessage
172 //////////////////////////////////////////
173 EFrameworkunifiedStatus CFrameworkunified_CWORD77_Session::HandleSessionMessage(UI_32 cmdID) {
174   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");
175   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
176
177   UI_32 size = 0;
178   PVOID data = NULL;
179
180   if (NULL != m_hSession && NULL != m_hService) {
181     eStatus = GetReqDataFrom_CWORD77_DataPool(std::make_pair(cmdID, FrameworkunifiedGetSessionName(m_hService)), size, data);
182
183     if (eFrameworkunifiedStatusOK == eStatus) {
184       eStatus = FrameworkunifiedSendMsg(m_hSession, cmdID, size, data);
185       if (eFrameworkunifiedStatusOK != eStatus) {
186         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: FrameworkunifiedSendMsg() returned %d", eStatus);
187       }
188     } else if (eFrameworkunifiedStatusDbRecNotFound == eStatus) {
189       // FIXME: This is added to help, if there is any command with no data. So, that services
190       //      don't need to provide _CWORD77_ interfaces for such command.
191       //       Correct way to solve the problem is, let services provide one _CWORD77_ interface
192       //     which will write 0 length in data pool.
193       // Impact: This may have one impact that, service is expecting some data, but
194       //      _CWORD77_ sends data with 0 length because it didn't find anything in the data pool.
195       FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__,
196              "GetReqDataFrom_CWORD77_DataPool() returned %d. But, still sending cmd 0x%X to service %s with length 0.",
197              eStatus, cmdID, FrameworkunifiedGetSessionName(m_hService));
198       eStatus = FrameworkunifiedSendMsg(m_hSession, cmdID, 0, NULL);
199       if (eFrameworkunifiedStatusOK != eStatus) {
200         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: FrameworkunifiedSendMsg() returned %d", eStatus);
201       }
202       // end fixme
203     } else {
204       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__,
205              "GetReqDataFrom_CWORD77_DataPool() returned %d. Can't send cmd 0x%X to service %s.",
206              eStatus, cmdID, FrameworkunifiedGetSessionName(m_hService));
207     }
208   } else {
209     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__,
210            "Error: HandleSessionMessage: Error sending cmd 0x%X request to service."
211            " Session handle %p Service Handle %p", cmdID, m_hSession, m_hService);
212     if (NULL == m_hSession) {
213       eStatus = eFrameworkunifiedStatusSessionErr;
214     }
215   }
216
217   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "- %d", eStatus);
218   return eStatus;
219 }
220
221 //////////////////////////////////////////
222 /// Function : AttachResponseCallbacks
223 //////////////////////////////////////////
224 void CFrameworkunified_CWORD77_Session::AttachResponseCallbacks(HANDLE f_hApp) {
225   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");   // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
226   UI_32 l_uiCount = 0;
227   PCSTR l_pRequester   = FrameworkunifiedGetMsgSrc(f_hApp);
228
229   // attach response protocols
230   UI_32 const *l_uiResponseProtocol = GetResponseProtocolArray();
231   if (NULL != l_uiResponseProtocol) {
232     for (; l_uiCount < GetResponseProtocolArrayLength(); l_uiResponseProtocol++, l_uiCount++) {
233       FrameworkunifiedAttachCallbackToDispatcher(f_hApp, l_pRequester, *l_uiResponseProtocol,
234                                     boost::bind(&CFrameworkunified_CWORD77_Session::OnResponse, this, f_hApp), m_hSession);
235     }
236   } else {
237     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "No Response protocols available for session with %s",
238            FrameworkunifiedGetSessionName(m_hService));
239   }
240
241   // attach events
242   l_uiCount = 0;
243   UI_32 const *l_uiEvents = GetEventArray();
244   if (NULL != l_uiEvents) {
245     for (; l_uiCount < GetEventArrayLength(); l_uiEvents++, l_uiCount++) {
246       // attach callback
247       FrameworkunifiedAttachCallbackToDispatcher(f_hApp, l_pRequester, *l_uiEvents,
248                                     boost::bind(&CFrameworkunified_CWORD77_Session::OnResponse, this, f_hApp), m_hSession);  // LCOV_EXCL_BR_LINE 11:except branch
249       // register event
250       FrameworkunifiedRegisterEvent(m_hSession, *l_uiEvents);
251     }
252   } else {
253     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "No event protocols available for session with %s",
254       FrameworkunifiedGetSessionName(m_hService));
255   }
256
257   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "-");   // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
258 }
259
260 //////////////////////////////////////////
261 /// Function : DetachResponseCallbacks
262 //////////////////////////////////////////
263 void CFrameworkunified_CWORD77_Session::DetachResponseCallbacks(HANDLE f_hApp) {
264   if (NULL != m_hSession) {
265     UI_32 l_uiCount = 0;
266     PCSTR l_cSessionName   = FrameworkunifiedGetSessionName(m_hSession);
267     // detach response protocols
268     UI_32 const *l_uiResponseProtocol = GetResponseProtocolArray();
269     if (NULL != l_uiResponseProtocol) {
270       for (; l_uiCount < GetResponseProtocolArrayLength(); l_uiResponseProtocol++, l_uiCount++) {
271         FrameworkunifiedDetachCallbackFromDispatcher(f_hApp, l_cSessionName, *l_uiResponseProtocol, m_hSession);
272       }
273     } else {
274       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "No Response protocols available for session with %s",
275              FrameworkunifiedGetSessionName(m_hService));
276     }
277
278     // detach and unregister events
279     l_uiCount = 0;
280     UI_32 const *l_uiEvents = GetEventArray();
281     if (NULL != l_uiEvents) {
282       for (; l_uiCount < GetEventArrayLength(); l_uiEvents++, l_uiCount++) {
283         FrameworkunifiedDetachCallbackFromDispatcher(f_hApp, l_cSessionName, *l_uiEvents, m_hSession);
284         FrameworkunifiedUnRegisterEvent(m_hSession, *l_uiEvents);
285       }
286     } else {
287       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "No event protocols available for session with %s",
288         FrameworkunifiedGetSessionName(m_hService));
289     }
290   } else {
291     FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Detach not required. Session not yet available");
292   }
293 }
294
295 //////////////////////////////////////////
296 /// Function : OnResponse
297 //////////////////////////////////////////
298 EFrameworkunifiedStatus CFrameworkunified_CWORD77_Session::OnResponse(HANDLE f_hApp) {
299   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "+");
300   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
301
302   UI_32 length = FrameworkunifiedGetMsgLength(f_hApp);
303   UI_32 cmdId = FrameworkunifiedGetMsgProtocol(f_hApp);
304   PCSTR l_cMsgSourceName = FrameworkunifiedGetMsgSrc(f_hApp);
305   if (NULL != l_cMsgSourceName) {
306     if (length != 0) {
307       std::vector<char> data;
308       data.resize(length);
309       eStatus = FrameworkunifiedGetMsgDataOfSize(f_hApp, &data[0], length);
310       FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "OnResponse from %s cmdId:%d", l_cMsgSourceName, cmdId);
311       SetRespDataIn_CWORD77_DataPool(std::make_pair(cmdId, l_cMsgSourceName), static_cast<UI_32>(data.size()), &data[0]);
312     } else {
313       FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "OnResponse from %s cmdId:[%d], Value: [NULL]", l_cMsgSourceName, cmdId);
314       SetRespDataIn_CWORD77_DataPool(std::make_pair(cmdId, l_cMsgSourceName), 0, NULL);
315     }
316
317     m_ResponseTo_CWORD77_(f_hApp, RESPONSE, l_cMsgSourceName);
318   } else {
319     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Setting response value of cmd 0x%X in data pool.", cmdId);
320   }
321
322   FRAMEWORKUNIFIEDLOG(ZONE_NS__CWORD77_, __FUNCTION__, "- %d", eStatus);
323   return eStatus;
324 }
325