common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / systemservice / power_service / server / src / ss_power_session.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_Power
19 /// \brief    This file supports power service session management.
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22 #include "ss_power_session.h"
23 #include <system_service/ss_services.h>
24 #include <system_service/ss_templates.h>
25 #include <system_service/ss_power_service.h>
26 #include <system_service/ss_power_service_protocol.h>
27 #include <system_service/ss_power_service_local.h>
28 #include <native_service/frameworkunified_framework_if.h>
29 #include <native_service/frameworkunified_service_protocol.h>
30
31 #include "ss_power_powerservicelog.h"
32
33
34 PowerSessionHandler::PowerSessionHandler() {
35   // TODO(my_username) Auto-generated constructor stub
36 }
37
38 PowerSessionHandler::~PowerSessionHandler() {  // LCOV_EXCL_START 14 Resident process, not called by NSFW
39   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
40   // TODO(my_username) Auto-generated destructor stub
41 }
42 // LCOV_EXCL_STOP 14 Resident process, not called by NSFW
43
44 EFrameworkunifiedStatus PowerSessionHandler::CloseSession(HANDLE h_app) {
45   EFrameworkunifiedStatus l_eStatus;
46   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
47   INTERFACEUNIFIEDLOG_RECEIVED_FROM(h_app);
48
49   // find the subscriber...
50   PwSessionIter iter = m_mapSessions.find(FrameworkunifiedGetMsgSrc(h_app));
51
52   // the iter is set to the end then the subscriber is not in the map
53   if (m_mapSessions.end() == iter) {  // LCOV_EXCL_BR_LINE 11:unexpected branch
54     // LCOV_EXCL_START 11:unexpected branch
55     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
56     FRAMEWORKUNIFIEDLOG(
57         ZONE_ERR,
58         __FUNCTION__,
59         " Error: Subscriber '%s' was not found in the map; the Close " "Session Request is being ignored!",
60         FrameworkunifiedGetMsgSrc(h_app));
61     l_eStatus = eFrameworkunifiedStatusDbRecNotFound;
62   } else if (NULL == iter->second.hsession) {  // LCOV_EXCL_BR_LINE 11:unexpected branch
63     l_eStatus = eFrameworkunifiedStatusInvldHandle;
64     LOG_ERROR("Client's PwSessionInfo handle");
65     // LCOV_EXCL_STOP 11:unexpected branch
66   } else {
67     CloseSessionAck closeSessionAck;
68     closeSessionAck.sessionId = FrameworkunifiedGetSessionId(iter->second.hsession);
69     closeSessionAck.eStatus = eFrameworkunifiedStatusOK;
70
71     // Send Ack to subscriber
72     if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedSendMsg(iter->second.hsession, PROTOCOL_CLOSE_SESSION_ACK, sizeof(CloseSessionAck), (PVOID) &closeSessionAck))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.  // NOLINT[whitespace/line_length]
73       // LCOV_EXCL_START 4: NSFW error case.
74       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
75       FRAMEWORKUNIFIEDLOG(
76           ZONE_ERR,
77           __FUNCTION__,
78           " Error: FrameworkunifiedSendMsg(%s, PROTOCOL_CLOSE_SESSION_ACK) " "errored: %d/'%s'",
79           iter->second.sz_name.c_str(), l_eStatus, GetStr(l_eStatus).c_str());
80       // LCOV_EXCL_STOP 4: NSFW error case.
81     } else {
82       FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
83              " FrameworkunifiedSendMsg(%s, PROTOCOL_CLOSE_SESSION_ACK) successful",
84              iter->second.sz_name.c_str());
85       iter->second.frunning = FALSE;
86
87       //
88       // ToDo Jay 2012 November 05 1) Can't detach just basic callbacks;
89       //      client could be Supervisor or System. This has been solved
90       //      in another Power Service Change Set. Need to get all
91       //      Change Sets merged and delivered.
92       UI_32 ss_power_basic_session_ids[] = { PROTOCOL_CLOSE_SESSION_REQ };
93
94       // Detach callback : Power System Session Requests
95       if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedDetachCallbacksFromDispatcher(h_app, iter->second.sz_name.c_str(), ss_power_basic_session_ids, _countof(ss_power_basic_session_ids)))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.  // NOLINT[whitespace/line_length]
96         // LCOV_EXCL_START 4: NSFW error case.
97         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
98         LOG_ERROR("FrameworkunifiedDetachCallbacksFromDispatcher()");
99         // LCOV_EXCL_STOP 4: NSFW error case.
100       } else {
101         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
102                " FrameworkunifiedDetachCallbacksFromDispatcher() successful");
103       }
104
105       CALL_AND_LOG_STATUS(FrameworkunifiedDestroySession(h_app, iter->second.hsession));
106       iter->second.hsession = NULL;
107       m_mapSessions.erase(iter);
108     }
109   }
110   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
111   return l_eStatus;
112 }
113
114 EFrameworkunifiedStatus PowerSessionHandler::StartComplete(HANDLE h_app) {  // LCOV_EXCL_START 8: can not be called
115   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
116   EFrameworkunifiedStatus l_eStatus;
117   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
118   StartCompleteAck ack;
119
120   if (eFrameworkunifiedStatusOK != (l_eStatus = ReadMsg<StartCompleteAck>(h_app, ack))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
121     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
122     LOG_ERROR("ReadMsg()");
123   } else {
124     // find the subscriber...
125     PwSessionIter iter = m_mapSessions.find(FrameworkunifiedGetMsgSrc(h_app));
126
127     // the iter is set to the end then the subscriber is not in the map
128     if (m_mapSessions.end() != iter) {
129       iter->second.sz_servicename = ack.szServiceName;
130       iter->second.frunning = TRUE;
131
132       if (AllClientsInGroupStarted(iter->second.ui_groupid)) {
133         // NOP
134       }
135     } else {
136       FRAMEWORKUNIFIEDLOG(
137           ZONE_ERR,
138           __FUNCTION__,
139           "Subscriber: %s , was not found in the map, the close session is being ignored!",
140           FrameworkunifiedGetMsgSrc(h_app));
141     }
142   }
143
144   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
145   return l_eStatus;
146 }
147 // LCOV_EXCL_STOP 8: can not be called
148 VOID PowerSessionHandler::Print() {
149   // find the subscriber...
150   PwSessionIter iter = m_mapSessions.begin();
151   for (; iter != m_mapSessions.end(); iter++) {
152     FRAMEWORKUNIFIEDLOG(ZONE_DEBUG_DUMP, __FUNCTION__,
153            " Power Service is session-connected to '%s';  running: %s",
154            iter->second.sz_name.data(), (iter->second.frunning ? "YES" : "NO"));
155   }
156 }
157
158 // LCOV_EXCL_START 8: can not be called
159 BOOL PowerSessionHandler::WakeupComplete(RequiredModuleList & wakeup_modules) {
160   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
161   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
162   BOOL rtn = TRUE;  // set the return TRUE until we find a missing module
163   RequiredModuleListIter iter = wakeup_modules.begin();
164   for (; iter != wakeup_modules.end(); iter++) {
165     PwSessionIter psi = m_mapSessions.find(*iter);
166     if (m_mapSessions.end() == psi) {
167       rtn = FALSE;  // Module Not Found in Power Service Session Handler
168       FRAMEWORKUNIFIEDLOG(
169           ZONE_INFO,
170           __FUNCTION__,
171           "Required Wakeup: %s has not connected to Power Service, Wakeup NOT COMPLETE!",
172           iter->data());
173     } else if (FALSE == psi->second.frunning) {
174       rtn = FALSE;  // Module Found in Power Service Session Handler but hasn't started yet
175       FRAMEWORKUNIFIEDLOG(
176           ZONE_INFO,
177           __FUNCTION__,
178           "Required Wakeup: %s has connected to Power Service but is not running (FrameworkunifiedStart Response missing),"
179           " Wakeup NOT COMPLETE!",
180           iter->data());
181     }
182   }
183
184   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
185   return rtn;
186 }
187 // LCOV_EXCL_STOP 8: can not be called
188
189 BOOL PowerSessionHandler::ShutdownComplete(
190     RequiredModuleList & shutdown_modules) {
191   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
192   BOOL rtn = TRUE;  // set the return TRUE until we find a missing module
193   RequiredModuleListIter iter = shutdown_modules.begin();
194   for (; iter != shutdown_modules.end(); iter++) {
195     PwSessionIter psi = m_mapSessions.find(*iter);
196     if (m_mapSessions.end() == psi) {
197       FRAMEWORKUNIFIEDLOG(
198           ZONE_INFO,
199           __FUNCTION__,
200           "Required Shutdown: %s is not in Power Service session list, Shutdown pending!",
201           iter->data());
202     } else if (TRUE == psi->second.frunning) {
203       rtn = FALSE;  // Module Found in Power Service Session Handler but hasn't stopped yet
204       FRAMEWORKUNIFIEDLOG(
205           ZONE_INFO,
206           __FUNCTION__,
207           "Required Shutdown: %s is connected to Power Service but is running (Stop Response missing),"
208           " Shutdown NOT COMPLETE!",
209           iter->data());
210     }
211   }
212   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
213   return rtn;
214 }
215
216 EFrameworkunifiedStatus PowerSessionHandler::SendToSupervisor(UI_32 cmd, UI_32 length,
217                                                  PVOID data) {
218   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
219   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
220
221   if (NULL == data && length > 0) {  // LCOV_EXCL_BR_LINE 6: it will not be true at the other place
222     // LCOV_EXCL_START 6: it will not be true at the other place
223     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
224     LOG_ERROR("NULL == data && length > 0");
225     l_eStatus = eFrameworkunifiedStatusInvldBuf;
226     // LCOV_EXCL_STOP 6: it will not be true at the other place
227   } else {
228     // get first client from the map...
229     PwSessionIter iter = m_mapSessions.begin();
230     BOOL l_bFoundSupervisor = FALSE;
231
232     for (; iter != m_mapSessions.end() && !l_bFoundSupervisor; iter++) {
233       l_bFoundSupervisor = (epsstSUPERVISOR == iter->second.esessiontype);
234       if (l_bFoundSupervisor) {
235         l_eStatus = FrameworkunifiedSendMsg(iter->second.hsession, cmd, length, data);
236         LOG_STATUS(l_eStatus, "FrameworkunifiedSendMsg()");
237       }
238     }
239     if (!l_bFoundSupervisor) {
240       FRAMEWORKUNIFIEDLOG(
241           ZONE_ERR, __FUNCTION__,
242           "Error: Did not find a Supervisor session; did not " "send cmd %d !",
243           cmd);
244       l_eStatus = eFrameworkunifiedStatusDbRecNotFound;
245     }
246   }
247
248   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
249   return l_eStatus;
250 }
251
252 BOOL PowerSessionHandler::AllClientsInGroupStarted(UI_32 f_ungrpid) {  // LCOV_EXCL_START 8: can not be called
253   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
254   BOOL fStatus = TRUE;
255   BOOL fGrpFnd = FALSE;
256   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
257
258   PwSessionIter iter = m_mapSessions.begin();
259
260   if (iter == m_mapSessions.end())
261     fStatus = TRUE;
262
263   for (; iter != m_mapSessions.end() && fStatus; iter++) {
264     if (f_ungrpid == iter->second.ui_groupid) {
265       fGrpFnd = TRUE;
266       if (FALSE == iter->second.frunning) {
267         fStatus = FALSE;
268       }
269     }
270   }
271
272   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "- return flag: %s",
273          ((fStatus && fGrpFnd) ? "TRUE" : "FALSE"));
274   return (fStatus && fGrpFnd);
275 }
276 // LCOV_EXCL_STOP 8: can not be called