common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / systemservice / interface_unified / library / src / ss_power_client.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_PowerServiceIf
19 /// \brief    This file supports Power Service client management.
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include <inttypes.h>
24 #include <string.h>
25 #include <native_service/frameworkunified_application.h>
26 #include <native_service/frameworkunified_framework_if.h>
27 #include <native_service/frameworkunified_types.h>
28 #include <native_service/frameworkunified_framework_types.h>
29 #include <string>
30 #include "system_service/ss_power_service.h"
31 #include "system_service/ss_power_service_protocol.h"
32 #include "system_service/ss_power_service_notifications.h"
33 #include "system_service/ss_services.h"
34 #include "system_service/ss_system_if.h"
35 #include "system_service/ss_templates.h"
36 #include "ss_power_if_interfaceunifiedlog.h"
37
38 typedef struct {
39   BOOL fAvailable;
40   HANDLE hSession;
41   HANDLE hService;
42   std::string szServiceName;
43 } TPowerSession;
44
45 static EFrameworkunifiedStatus OnPowerCloseSesionAck(HANDLE hApp);
46 static EFrameworkunifiedStatus OnPowerOpenSessionAck(HANDLE hApp);
47 static EFrameworkunifiedStatus OnPowerAvailability(HANDLE hApp);
48 static EFrameworkunifiedStatus OnPowerAvailable(HANDLE hApp);
49 static EFrameworkunifiedStatus OnPowerUnavailable(HANDLE hApp);
50 static TPowerSession g_tPowerSession = { };
51
52 /// Base on the a user group affiliation we will call there start and stop methods
53 /// Note: there group and subgroup strings are empty we will call start on the every
54 /// first case of
55 EFrameworkunifiedStatus InterfaceunifiedSystemConnectToPowerSerivce(HANDLE hApp) {
56   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
57   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
58
59   // Subscriptions
60   FrameworkunifiedNotificationCallbackHandler g_aryPower_Notif_Cbs[] = {
61       // Notifications name, Call back function
62       { szNTFY_PowerAvailability, OnPowerAvailability }, };
63
64   /// Avoid opening a session to the Power
65   /// service Since it uses the framework as well
66   if (0 != strcmp(SERVICE_POWER, FrameworkunifiedGetAppName(hApp))) {
67     // Subscribe and attach call backs to notifications, to Notification Service!
68     if (eFrameworkunifiedStatusOK
69         != (eStatus = FrameworkunifiedSubscribeNotificationsWithCallback(
70             hApp, g_aryPower_Notif_Cbs, _countof(g_aryPower_Notif_Cbs)))) {
71       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
72              "FrameworkunifiedAttachNotificationCallbacksToDispatcher Failed Status:0x%x ",
73              eStatus);
74     } else {
75       /// Need to save the Group and Subgroup values
76       g_tPowerSession.szServiceName = FrameworkunifiedGetAppName(hApp);
77
78       /// Cleanup any other data members
79       g_tPowerSession.fAvailable = FALSE;
80       g_tPowerSession.hService = NULL;
81       g_tPowerSession.hSession = NULL;
82     }
83   }
84
85   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");
86
87   return eStatus;
88 }
89
90 EFrameworkunifiedStatus OnPowerAvailability(HANDLE hApp) {
91   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
92   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
93
94   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
95          "Power Service Availability notification received ****");
96
97   if (sizeof(BOOL) == FrameworkunifiedGetMsgLength(hApp)) {
98     BOOL avalibility = FALSE;
99     if (eFrameworkunifiedStatusOK
100         == (eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, &avalibility, sizeof(BOOL)))) {
101       if (TRUE == avalibility) {
102         eStatus = OnPowerAvailable(hApp);
103       } else {
104         eStatus = OnPowerUnavailable(hApp);
105       }
106     } else {
107       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
108              "FrameworkunifiedAttachNotificationCallbacksToDispatcher Failed Status:0x%x ",
109              eStatus);
110     }
111   }
112
113   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");
114   return eStatus;
115 }
116
117 EFrameworkunifiedStatus OnPowerAvailable(HANDLE hApp) {
118   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
119
120   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
121
122   // if the service is available before the close happens then close the session and service first.
123   if (NULL != g_tPowerSession.hService && NULL != g_tPowerSession.hSession) {
124     EFrameworkunifiedStatus interfaceunifiedRet = FrameworkunifiedCloseSession(g_tPowerSession.hService,
125                                         g_tPowerSession.hSession);
126     SS_ASERT(interfaceunifiedRet == eFrameworkunifiedStatusOK);
127     g_tPowerSession.hSession = NULL;
128     g_tPowerSession.hService = NULL;
129   }
130
131   PCSTR power_name = FrameworkunifiedGetMsgSrc(hApp);
132
133   // opening the service
134   if (NULL == (g_tPowerSession.hService = FrameworkunifiedOpenService(hApp, power_name))) {
135     FRAMEWORKUNIFIEDLOG0(ZONE_ERR, __FUNCTION__, "Power: Failed to open service");
136   } else {
137     FrameworkunifiedProtocolCallbackHandler g_aryPower_Protocol_Cbs[] = {
138     // Command ID,  Call back functions
139         { PROTOCOL_OPEN_SESSION_ACK, OnPowerOpenSessionAck },
140         { PROTOCOL_CLOSE_SESSION_ACK, OnPowerCloseSesionAck }, };
141
142     /// Attach the valid callback for this service
143     if (eFrameworkunifiedStatusOK
144         == (eStatus = FrameworkunifiedAttachCallbacksToDispatcher(
145             hApp, power_name, g_aryPower_Protocol_Cbs,
146             _countof(g_aryPower_Protocol_Cbs)))) {
147       EPWR_SESSION_TYPE tOpenSessionReq = epsstBASIC;
148       /// opening the session with service
149       if (eFrameworkunifiedStatusOK
150           != (eStatus = FrameworkunifiedOpenSessionWithData(g_tPowerSession.hService,
151                                                &tOpenSessionReq,
152                                                sizeof(tOpenSessionReq)))) {
153         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "FrameworkunifiedOpenSession Failed Status:0x%x ",
154                eStatus);
155       }
156
157       FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "%s has received PowerService Available.",
158              FrameworkunifiedGetAppName(hApp));
159     } else {
160       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
161              "FrameworkunifiedAttachCallbacksToDispatcher Failed Status:0x%x ", eStatus);
162     }
163   }
164   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
165   return eStatus;
166 }
167
168 EFrameworkunifiedStatus OnPowerUnavailable(HANDLE hApp) {
169   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
170
171   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
172   if (0 == strcmp(SERVICE_POWER, FrameworkunifiedGetAppName(hApp))) {
173     if ((g_tPowerSession.hSession != NULL)
174         && (g_tPowerSession.hService != NULL)) {
175       if (eFrameworkunifiedStatusOK
176           != (eStatus = FrameworkunifiedCloseSession(g_tPowerSession.hService,
177                                         g_tPowerSession.hSession))) {
178         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "FrameworkunifiedCloseSession Failed Status:0x%x",
179                eStatus);
180       }
181       g_tPowerSession.hSession = NULL;
182       g_tPowerSession.hService = NULL;
183       FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__,
184              "%s has received PowerService Unavailable.", FrameworkunifiedGetAppName(hApp));
185     } else {
186       FRAMEWORKUNIFIEDLOG(
187           ZONE_ERR,
188           __FUNCTION__,
189           "Error INVALID HANDLE [g_tPowerSession.hSession :0x%" PRIxPTR "]; [g_tPowerSession.hService :0x%" PRIxPTR "] ",  // NOLINT (whitespace/line_length)
190           (uintptr_t) g_tPowerSession.hSession, (uintptr_t) g_tPowerSession.hService);
191     }
192   } else {
193     eStatus = eFrameworkunifiedStatusOK;
194   }
195   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");
196
197   return eStatus;
198 }
199
200 ///////////////////////////////////////////////////////////
201 /// Function: OnHeartBeatClientOpenSessionAck
202 /// HeartBeat Service OpenSession callback
203 ///////////////////////////////////////////////////////////
204 EFrameworkunifiedStatus OnPowerOpenSessionAck(HANDLE hApp) {
205   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
206   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
207
208   g_tPowerSession.hSession = FrameworkunifiedGetOpenSessionHandle(hApp);
209   if (NULL != g_tPowerSession.hSession) {
210     g_tPowerSession.fAvailable = TRUE;
211     eStatus = eFrameworkunifiedStatusOK;
212     FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__,
213            "%s Session to PowerService has been successfully opened.",
214            FrameworkunifiedGetAppName(hApp));
215   } else {
216     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "FrameworkunifiedGetOpenSessionHandle returned : %" PRIxPTR "",
217            (uintptr_t) g_tPowerSession.hSession);
218     g_tPowerSession.hSession = NULL;
219   }
220
221   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");
222
223   return eStatus;
224 }
225
226 ///////////////////////////////////////////////////////////
227 /// Function: OnHeartBeatClientCloseSessionAck
228 /// HeartBeat Service CloseSession callback
229 ///////////////////////////////////////////////////////////
230 EFrameworkunifiedStatus OnPowerCloseSesionAck(HANDLE hApp) {
231   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
232
233   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
234
235   // Close Service
236   if (NULL != g_tPowerSession.hService) {
237     eStatus = FrameworkunifiedCloseService(hApp, g_tPowerSession.hService);
238     if (eFrameworkunifiedStatusOK != eStatus) {
239       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error: FrameworkunifiedCloseService() errored %d/'%s",
240              eStatus, GetStr(eStatus).c_str());
241     }
242     g_tPowerSession.hService = NULL;
243     g_tPowerSession.hSession = NULL;
244   }
245   g_tPowerSession.fAvailable = FALSE;
246   eStatus = eFrameworkunifiedStatusOK;
247   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__,
248          "%s Session to PowerService has been successfully closed.",
249          FrameworkunifiedGetAppName(hApp));
250
251   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");
252
253   return eStatus;
254 }