Init basesystem source codes.
[staging/basesystem.git] / systemservice / interface_unified / library / src / ss_pwrsvc_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_PowerServiceIf
19 /// \brief    This file supports the Power Service module interface.
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22 #include "system_service/ss_power_service_if.h"
23 #include <string.h>
24 #include <native_service/frameworkunified_application.h>
25 #include <native_service/frameworkunified_framework_if.h>
26 #include <native_service/frameworkunified_types.h>
27 #include <native_service/frameworkunified_framework_types.h>
28 #include "system_service/ss_power_service.h"
29 #include "system_service/ss_power_service_protocol.h"
30 #include "system_service/ss_power_service_notifications.h"
31 #include "system_service/ss_system_if.h"
32 #include "system_service/ss_services.h"
33 #include "system_service/ss_string_maps.h"
34 #include "system_service/ss_templates.h"
35 #include "ss_power_if_interfaceunifiedlog.h"
36
37 HANDLE OpenPowerService(HANDLE f_hApp) {
38   EFrameworkunifiedStatus l_eStatus;
39   HANDLE l_hService = NULL;
40   if (NULL == f_hApp) {
41     l_eStatus = eFrameworkunifiedStatusInvldHandle;
42     LOG_ERROR("NULL == f_App");
43   } else if (NULL == (l_hService = FrameworkunifiedOpenService(f_hApp, SERVICE_POWER))) {
44     l_eStatus = eFrameworkunifiedStatusNullPointer;
45     LOG_ERROR("FrameworkunifiedOpenService(f_hApp, SERVICE_POWER)");
46   }
47   return l_hService;
48 }
49
50 EFrameworkunifiedStatus ClosePowerService(HANDLE f_hApp, HANDLE f_hService) {
51   EFrameworkunifiedStatus l_eStatus;
52   if (NULL == f_hApp) {
53     l_eStatus = eFrameworkunifiedStatusInvldHandle;
54     LOG_ERROR("NULL == f_App");
55   } else if (NULL == f_hService) {
56     l_eStatus = eFrameworkunifiedStatusInvldHandle;
57     LOG_ERROR("NULL == f_hService");
58   } else {
59     l_eStatus = FrameworkunifiedCloseService(f_hApp, f_hService);
60     LOG_STATUS(l_eStatus, "FrameworkunifiedCloseService(f_hApp, f_hService)");
61     f_hService = NULL;
62   }
63   return l_eStatus;
64 }
65
66 ///////////////////////////////////////////////////////////////////////////////////////////////////
67 /// Session Related Requests
68 /// Any client can also use FrameworkunifiedOpenSession and FrameworkunifiedOpenSessionWithData.
69 /// In that case, the PVOID data passed as a parameter should be MM_OpenSessionRequestIf
70 /// to open a session with the Power Service.
71 /// The client can also use the below API for Opening a session with Power Service.
72 ///////////////////////////////////////////////////////////////////////////////////////////////////
73
74 EFrameworkunifiedStatus PwrServiceOpenSessionRequest(HANDLE f_hService,
75                                         EPWR_SESSION_TYPE f_eSessionType) {
76   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
77   EFrameworkunifiedStatus l_eStatus;
78
79   if (NULL == f_hService) {
80     l_eStatus = eFrameworkunifiedStatusInvldHandle;
81     LOG_ERROR("NULL == f_hService");
82   } else if (epsstUNKNOWN == f_eSessionType) {
83     l_eStatus = eFrameworkunifiedStatusInvldParam;
84     LOG_ERROR("epsstUNKNOWN == f_eSessionType");
85   } else {
86     l_eStatus = FrameworkunifiedOpenSessionWithData(f_hService, &f_eSessionType,
87                                        sizeof(f_eSessionType));
88     LOG_STATUS(l_eStatus, "FrameworkunifiedOpenSessionWithData()");
89   }
90   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
91   return l_eStatus;
92 }
93
94 EFrameworkunifiedStatus PwrServiceCloseSessionRequest(HANDLE f_hService, HANDLE f_hSession) {
95   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
96   EFrameworkunifiedStatus l_eStatus;
97
98   if (NULL == f_hService) {
99     l_eStatus = eFrameworkunifiedStatusInvldHandle;
100     LOG_ERROR("NULL == f_hService");
101   } else if (NULL == f_hSession) {
102     l_eStatus = eFrameworkunifiedStatusInvldHandle;
103     LOG_ERROR("NULL == f_hSession");
104   } else {
105     CALL_AND_LOG_STATUS(FrameworkunifiedCloseSession(f_hService, f_hSession));
106   }
107
108   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
109   return l_eStatus;
110 }
111
112 EFrameworkunifiedStatus PwrServiceDecodeOpenSessionResponse(HANDLE f_hApp,
113                                                HANDLE& f_hSession) {  // NOLINT (runtime/references)
114   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
115   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
116
117   if (NULL == f_hApp) {
118     l_eStatus = eFrameworkunifiedStatusInvldHandle;
119     LOG_ERROR("NULL == f_App");
120   } else {
121     if (NULL == (f_hSession = FrameworkunifiedGetOpenSessionHandle(f_hApp))) {
122       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
123              " Error: FrameworkunifiedGetOpenSessionHandle('%s') returned NULL",
124              FrameworkunifiedGetAppName(f_hApp));
125       l_eStatus = eFrameworkunifiedStatusNullPointer;
126     }
127   }
128   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
129   return l_eStatus;
130 }
131
132 EFrameworkunifiedStatus PwrServiceSetVoltageState(HANDLE f_hSession,
133                                      EPWR_VOLTAGE_STATE_TYPE f_eVoltage_state) {
134   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
135   EFrameworkunifiedStatus l_eStatus;
136   if (NULL == f_hSession) {
137     l_eStatus = eFrameworkunifiedStatusInvldHandle;
138     LOG_ERROR("NULL == f_hSession");
139   } else if (epsvsINVALID == f_eVoltage_state) {
140     l_eStatus = eFrameworkunifiedStatusInvldParam;
141     LOG_ERROR("epsvsINVALID == f_eVoltage_state");
142   } else {
143     Pwr_ServiceSetInterface l_tServiceSetIf;
144     std::memset(&l_tServiceSetIf, 0, sizeof(l_tServiceSetIf));
145     l_tServiceSetIf.data.voltage.state = f_eVoltage_state;
146     l_eStatus = FrameworkunifiedSendMsg(f_hSession, SS_POWER_VOLTAGE_STATE,
147                            sizeof(l_tServiceSetIf), (PVOID) &l_tServiceSetIf);
148     LOG_STATUS(l_eStatus, "FrameworkunifiedSendMsg(SS_POWER_VOLTAGE_STATE)");
149   }
150   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
151   return l_eStatus;
152 }
153
154 EFrameworkunifiedStatus PwrServiceSetCrankState(HANDLE f_hSession,
155                                    EPWR_CRANK_STATE_TYPE f_eCrank_state) {
156   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
157   EFrameworkunifiedStatus l_eStatus;
158
159   if (NULL == f_hSession) {
160     l_eStatus = eFrameworkunifiedStatusInvldHandle;
161     LOG_ERROR("NULL == f_hSession");
162   } else if (epscsINVALID == f_eCrank_state) {
163     l_eStatus = eFrameworkunifiedStatusInvldParam;
164     LOG_ERROR("epsvsINVALID == f_eCrank_state");
165   } else {
166     Pwr_ServiceSetInterface l_tServiceSetIf;
167     std::memset(&l_tServiceSetIf, 0, sizeof(l_tServiceSetIf));
168     l_tServiceSetIf.data.crank.state = f_eCrank_state;
169     l_eStatus = FrameworkunifiedSendMsg(f_hSession, SS_POWER_CRANK_STATE,
170                            sizeof(l_tServiceSetIf), (PVOID) &l_tServiceSetIf);
171     LOG_STATUS(l_eStatus, "FrameworkunifiedSendMsg(SS_POWER_CRANK_STATE)");
172   }
173   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
174   return l_eStatus;
175 }
176
177 EFrameworkunifiedStatus PwrServiceSendShutdownRequest(
178     HANDLE f_hSession, EPWR_SHUTDOWN_REQUEST_MSG_STRUCT &f_eState) {  // NOLINT (runtime/references)
179   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
180   EFrameworkunifiedStatus l_eStatus;
181   if (NULL == f_hSession) {
182     l_eStatus = eFrameworkunifiedStatusInvldHandle;
183     LOG_ERROR("NULL == f_hSession");
184   } else {
185     Pwr_ServiceSetInterface l_tServiceSetIf;
186     std::memset(&l_tServiceSetIf, 0, sizeof(l_tServiceSetIf));
187     l_tServiceSetIf.data.shutdownRequestMsg = f_eState;
188     l_eStatus = FrameworkunifiedSendMsg(f_hSession, SS_POWER_SHUTDOWN_REQUEST_MSG,
189                            sizeof(l_tServiceSetIf), &l_tServiceSetIf);
190
191     LOG_STATUS(l_eStatus, "FrameworkunifiedSendMsg(SS_POWER_SHUTDOWN_REQUEST_MSG)");
192   }
193
194   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
195   return l_eStatus;
196 }  // End of EFrameworkunifiedStatus PwrServiceSendShutdownRequest(
197
198 EFrameworkunifiedStatus PwrServiceSendSetShutdownPopupRequest(
199     HANDLE f_hSession, EPWR_SHUTDOWN_POPUP_TYPE f_eShutdownPopup) {
200   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
201   EFrameworkunifiedStatus l_eStatus;
202   if (NULL == f_hSession) {
203     l_eStatus = eFrameworkunifiedStatusInvldHandle;
204     LOG_ERROR("NULL == f_hSession");
205   } else {
206     Pwr_ServiceSetInterface l_tServiceSetIf;
207     std::memset(&l_tServiceSetIf, 0, sizeof(l_tServiceSetIf));
208     switch (f_eShutdownPopup) {
209       case epsspPowerSave1:
210       case epsspPowerSave2:
211       case epsspPowerSave3:
212       case epsspPowerSaveClr:
213       case epsspLowVoltage1:
214       case epsspLowVoltage2:
215       case epsspLowVoltage3:
216       case epsspLowVoltageClr:
217       case epsspBattCouplingSW1:
218       case epsspBattCouplingSW2:
219       case epsspBattCouplingSW3:
220       case epsspBattCouplingSWClr:
221       case epsspAbnormalTemp_1st:
222       case epsspAbnormalTemp_Clr:
223       case epsspLimpHome_1st:
224       case epsspLimpHome_2nd:
225       case epsspLimpHome_3rd:
226       case epsspLimpHome_Clr:
227       case epsspProdMd_1st:
228       case epsspProdMd_Clr:
229       case epsspTransMd_1st:
230       case epsspTransMd_Clr:
231       case epsspAllClr:
232         l_tServiceSetIf.data.shutdownPopup.shutdownPopupEvent =
233             f_eShutdownPopup;
234         l_eStatus = FrameworkunifiedSendMsg(f_hSession,
235                                SS_POWER_PUBLISH_SHUTDOWN_CONDITION_REQ,
236                                sizeof(l_tServiceSetIf), &l_tServiceSetIf);
237         LOG_STATUS(l_eStatus,
238                    "FrameworkunifiedSendMsg(SS_POWER_PUBLISH_SHUTDOWN_CONDITION_REQ)");
239         break;
240
241       default:
242         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
243                " Error: Unknown 'ShutdownPopup' value: 0x%X/%d",
244                f_eShutdownPopup, f_eShutdownPopup);
245         l_eStatus = eFrameworkunifiedStatusInvldParam;
246         break;
247     }
248   }
249   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
250   return l_eStatus;
251 }  // End of EFrameworkunifiedStatus PwrServiceSendSetShutdownPopupRequest( HANDLE f_hSession,
252
253 EFrameworkunifiedStatus PwrServiceSendStartupConfirmationMsgRequest(
254     HANDLE f_hSession, EPWR_SC_MSG_STRUCT f_eState) {
255   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
256   EFrameworkunifiedStatus l_eStatus;
257   if (NULL == f_hSession) {
258     l_eStatus = eFrameworkunifiedStatusInvldHandle;
259     LOG_ERROR("NULL == f_hSession");
260   } else {
261     Pwr_ServiceSetInterface l_tServiceSetIf;
262     std::memset(&l_tServiceSetIf, 0, sizeof(l_tServiceSetIf));
263     l_tServiceSetIf.data.startupConfirmationMsg = f_eState;
264     l_eStatus = FrameworkunifiedSendMsg(f_hSession, SS_POWER_FWD_START_CONFIRMATION_MSG_REQ,
265                            sizeof(l_tServiceSetIf), &l_tServiceSetIf);
266
267     LOG_STATUS(l_eStatus, "FrameworkunifiedSendMsg(SS_POWER_FWD_START_CONFIRMATION_MSG_REQ)");
268   }
269   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
270   return l_eStatus;
271 }  // End of EFrameworkunifiedStatus PwrServiceSendStartupConfirmationMsgRequest ( HANDLE f_hSession,
272
273 EFrameworkunifiedStatus PwrServiceSystemModeInfoRequest(HANDLE f_hSession) {
274   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
275   EFrameworkunifiedStatus l_eStatus;
276   if (NULL == f_hSession) {
277     l_eStatus = eFrameworkunifiedStatusInvldHandle;
278     LOG_ERROR("NULL == f_hSession");
279   } else {
280     l_eStatus = FrameworkunifiedSendMsg(f_hSession, SS_POWER_SYSTEM_MODE_INFO_REQ, 0, NULL);
281     LOG_STATUS(l_eStatus, "FrameworkunifiedSendMsg(SS_POWER_SYSTEM_MODE_INFO_REQ)");
282   }
283   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
284   return l_eStatus;
285 }
286
287 EFrameworkunifiedStatus PwrServiceSendInitCompReport(HANDLE f_hSession) {
288   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
289   EFrameworkunifiedStatus l_eStatus;
290   if (NULL == f_hSession) {
291     l_eStatus = eFrameworkunifiedStatusInvldHandle;
292     LOG_ERROR("NULL == f_hSession");
293   } else {
294     l_eStatus = FrameworkunifiedSendMsg(f_hSession, SS_POWER_INITCOMP_REP, 0, NULL);
295     LOG_STATUS(l_eStatus, "FrameworkunifiedSendMsg(SS_POWER_INITCOMP_REP)");
296   }
297   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
298   return l_eStatus;
299 }
300
301 EFrameworkunifiedStatus PwrServiceSendPowerRequest(
302     HANDLE f_hSession, EPWR_POWER_REQUEST_MSG_STRUCT_WITH_UMCR &f_eState) {  // NOLINT (runtime/references)
303   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
304   EFrameworkunifiedStatus l_eStatus;
305   if (NULL == f_hSession) {
306     l_eStatus = eFrameworkunifiedStatusInvldHandle;
307     LOG_ERROR("NULL == f_hSession");
308   } else {
309     Pwr_ServiceSetInterface l_tServiceSetIf;
310     std::memset(&l_tServiceSetIf, 0, sizeof(l_tServiceSetIf));
311     l_tServiceSetIf.data.powerRequestMsg = f_eState;
312     l_eStatus = FrameworkunifiedSendMsg(f_hSession, SS_POWER_POWER_REQUEST_MSG,
313                            sizeof(l_tServiceSetIf), (PVOID) &l_tServiceSetIf);
314
315     char l_cBuf[200] = { 0 };
316     snprintf(
317         l_cBuf,
318         sizeof(l_cBuf),
319         "FrameworkunifiedSendMsg(SS_POWER_POWER_REQUEST_MSG(%s, %s))",
320         GetStr(l_tServiceSetIf.data.powerRequestMsg.userMode).c_str(),
321         GetStr(l_tServiceSetIf.data.powerRequestMsg.userModeChangeReason).c_str());
322     LOG_STATUS(l_eStatus, l_cBuf);
323   }
324   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
325   return l_eStatus;
326 }  // End of EFrameworkunifiedStatus PwrServiceSendPowerRequest( HANDLE f_hSession
327
328 EFrameworkunifiedStatus PwrServiceSendHeartBeatRequest(HANDLE f_hSession,
329                                           EPWR_HB_REQ_MSG_STRUCT *f_eHbReqMsg) {
330   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
331   EFrameworkunifiedStatus l_eStatus;
332   if (NULL == f_hSession) {
333     l_eStatus = eFrameworkunifiedStatusInvldHandle;
334     LOG_ERROR("NULL == f_hSession");
335   } else {
336     l_eStatus = FrameworkunifiedSendMsg(f_hSession, SS_POWER_HEARTBEAT_REQ,
337                            sizeof(EPWR_HB_REQ_MSG_STRUCT),
338                            reinterpret_cast<void *>(f_eHbReqMsg));
339     LOG_STATUS(l_eStatus, "FrameworkunifiedSendMsg(SS_POWER_HEARTBEAT_REQ)");
340   }
341   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");
342   return l_eStatus;
343 }  // LCOV_EXCL_BR_LINE 10:Because the last line