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_PowerServiceIf
19 /// \brief This file supports Power Service client management.
21 ///////////////////////////////////////////////////////////////////////////////
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>
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"
42 std::string szServiceName;
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 = { };
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
55 EFrameworkunifiedStatus InterfaceunifiedSystemConnectToPowerSerivce(HANDLE hApp) {
56 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
57 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
60 FrameworkunifiedNotificationCallbackHandler g_aryPower_Notif_Cbs[] = {
61 // Notifications name, Call back function
62 { szNTFY_PowerAvailability, OnPowerAvailability }, };
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 ",
75 /// Need to save the Group and Subgroup values
76 g_tPowerSession.szServiceName = FrameworkunifiedGetAppName(hApp);
78 /// Cleanup any other data members
79 g_tPowerSession.fAvailable = FALSE;
80 g_tPowerSession.hService = NULL;
81 g_tPowerSession.hSession = NULL;
85 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");
90 EFrameworkunifiedStatus OnPowerAvailability(HANDLE hApp) {
91 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
92 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
94 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
95 "Power Service Availability notification received ****");
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);
104 eStatus = OnPowerUnavailable(hApp);
107 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
108 "FrameworkunifiedAttachNotificationCallbacksToDispatcher Failed Status:0x%x ",
113 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");
117 EFrameworkunifiedStatus OnPowerAvailable(HANDLE hApp) {
118 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
120 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
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;
131 PCSTR power_name = FrameworkunifiedGetMsgSrc(hApp);
133 // opening the service
134 if (NULL == (g_tPowerSession.hService = FrameworkunifiedOpenService(hApp, power_name))) {
135 FRAMEWORKUNIFIEDLOG0(ZONE_ERR, __FUNCTION__, "Power: Failed to open service");
137 FrameworkunifiedProtocolCallbackHandler g_aryPower_Protocol_Cbs[] = {
138 // Command ID, Call back functions
139 { PROTOCOL_OPEN_SESSION_ACK, OnPowerOpenSessionAck },
140 { PROTOCOL_CLOSE_SESSION_ACK, OnPowerCloseSesionAck }, };
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,
152 sizeof(tOpenSessionReq)))) {
153 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "FrameworkunifiedOpenSession Failed Status:0x%x ",
157 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "%s has received PowerService Available.",
158 FrameworkunifiedGetAppName(hApp));
160 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
161 "FrameworkunifiedAttachCallbacksToDispatcher Failed Status:0x%x ", eStatus);
164 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
168 EFrameworkunifiedStatus OnPowerUnavailable(HANDLE hApp) {
169 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
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",
181 g_tPowerSession.hSession = NULL;
182 g_tPowerSession.hService = NULL;
183 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__,
184 "%s has received PowerService Unavailable.", FrameworkunifiedGetAppName(hApp));
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);
193 eStatus = eFrameworkunifiedStatusOK;
195 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");
200 ///////////////////////////////////////////////////////////
201 /// Function: OnHeartBeatClientOpenSessionAck
202 /// HeartBeat Service OpenSession callback
203 ///////////////////////////////////////////////////////////
204 EFrameworkunifiedStatus OnPowerOpenSessionAck(HANDLE hApp) {
205 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
206 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
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));
216 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "FrameworkunifiedGetOpenSessionHandle returned : %" PRIxPTR "",
217 (uintptr_t) g_tPowerSession.hSession);
218 g_tPowerSession.hSession = NULL;
221 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");
226 ///////////////////////////////////////////////////////////
227 /// Function: OnHeartBeatClientCloseSessionAck
228 /// HeartBeat Service CloseSession callback
229 ///////////////////////////////////////////////////////////
230 EFrameworkunifiedStatus OnPowerCloseSesionAck(HANDLE hApp) {
231 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
233 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
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());
242 g_tPowerSession.hService = NULL;
243 g_tPowerSession.hSession = NULL;
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));
251 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");