summaryrefslogtreecommitdiffstats
path: root/power_service/server/include/ss_power.h
blob: b1e3de57bb9f39dd4d1db4187c199a7ce0f9ad14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

///////////////////////////////////////////////////////////////////////////////
/// \ingroup  tag_PowerService
/// \brief    Handles Power Service Business logic
///
///////////////////////////////////////////////////////////////////////////////
#ifndef POWER_SERVICE_SERVER_INCLUDE_SS_POWER_H_
#define POWER_SERVICE_SERVER_INCLUDE_SS_POWER_H_

#include <system_service/ss_power_service.h>
#include <system_service/ss_power_service_protocol.h>
#include <system_service/ss_string_maps.h>
#include <system_service/ss_templates.h>
#include <native_service/frameworkunified_types.h>
#include <map>
#include <vector>
#include <string>
#include <exception>

#include "ss_power_session.h"
#include "ss_power_config.h"
#include "ss_power_state_machine.h"
#include "ss_power_powerservicelog.h"

typedef enum _SS_PSState {
  SS_PS_READY_TO_WAKEUP = 0x01,
  SS_PS_WAKEUP_INITIATED,
  SS_PS_WAKEUP_COMPLETE,
  SS_PS_POWER_ON_COMPLETE,
  SS_PS_POWER_OFF_INITIATED,
  SS_PS_POWER_OFF_COMPLETE,
  SS_PS_SHUTDOWN_INITIATED,
  SS_PS_SHUTDOWN_COMPLETE
  // This is not real state, used only to know total states
  ,
  SS_PS_STATE_MAX
} SS_PSState;

typedef struct {
  std::string m_cmd;
  UI_64 m_time;
  std::string m_sender;
} Ts_cmdHist;

typedef std::vector<Ts_cmdHist> cmdHist;
typedef cmdHist::iterator cmdHistIter;

class Power {
 public:
  Power();
  virtual ~Power();
  EFrameworkunifiedStatus Initialize(HANDLE h_app);
  static Power & GetInstance();

  /// Session Request Handlers
  EFrameworkunifiedStatus OnOpenSession(HANDLE h_app);
  EFrameworkunifiedStatus OnCloseSession(HANDLE h_app);

  /// Session Response\Ack Handlers
  EFrameworkunifiedStatus OnWakeUpComplete(HANDLE h_app);
  EFrameworkunifiedStatus OnShutdownComplete(HANDLE h_app);
  EFrameworkunifiedStatus OnPrintConnections(HANDLE h_app);
  EFrameworkunifiedStatus OnPrintStack(HANDLE h_app);

  /// SSM Handlers

  /// Supervisor Handlers
  EFrameworkunifiedStatus OnPowerRequestMsg(HANDLE h_app);
  EFrameworkunifiedStatus OnSetPowerState(HANDLE h_app);
  EFrameworkunifiedStatus OnSetCommWakeUp(HANDLE h_app);
  EFrameworkunifiedStatus OnSetCommSleep(HANDLE h_app);
  EFrameworkunifiedStatus OnShutdownRequestMsg(HANDLE h_app);

  EFrameworkunifiedStatus OnPowerOnOffNotification(HANDLE h_app);
  EFrameworkunifiedStatus OnStartModules(HANDLE h_app);
  EFrameworkunifiedStatus OnShutdownModules(HANDLE h_app);

  EFrameworkunifiedStatus OnHysteresisTimeout(HANDLE h_app);

  // System Handlers
  EFrameworkunifiedStatus OnSystemLaunchComplete(HANDLE h_app);
  EFrameworkunifiedStatus OnSystemShutdownComplete(HANDLE h_app);
  EFrameworkunifiedStatus OnWakeUpTimeout(HANDLE h_app);
  EFrameworkunifiedStatus OnShutdownTimeout(HANDLE h_app);

  // Get Result query from power test client
  EFrameworkunifiedStatus OnCurrentPowerStateQuery(HANDLE h_app);

  EFrameworkunifiedStatus OnSetVoltageState(HANDLE h_app);
  EFrameworkunifiedStatus OnSetCrankState(HANDLE h_app);

  EFrameworkunifiedStatus OnSystemModeInfoRequest(HANDLE h_app);
  EFrameworkunifiedStatus OnSystemModeInfoResponse(HANDLE h_app);

  EFrameworkunifiedStatus OnInitCompReport(HANDLE h_app);

  EFrameworkunifiedStatus OnSystemMgrConnectionEstablished(HANDLE h_app);

  //
  // Start Confirmation callback functions
  //
  EFrameworkunifiedStatus OnSendStartupConfirmationRequest(HANDLE h_app);
  EFrameworkunifiedStatus OnSendStartupConfirmationResponse(HANDLE h_app);

  //
  // User Mode Protocol callback functions
  //
  EFrameworkunifiedStatus OnUserModeResponse(HANDLE h_app);

  //
  // Shutdown Condition Notification Protocol function
  //
  EFrameworkunifiedStatus OnPublishShutdownPopupRequest(HANDLE h_app);

  //
  // Power Popup Notification Protocol function
  //
  EFrameworkunifiedStatus OnPublishPowerPopupRequest(HANDLE h_app);

  //
  // HeartBeat Protocol callback functions
  //
  EFrameworkunifiedStatus On_CWORD56_HeartBeatRequest(HANDLE h_app);
  EFrameworkunifiedStatus OnSM_CWORD56_HeartBeatResponse(HANDLE h_app);

  //
  // Hard Reset Protocol callback functions
  //
  EFrameworkunifiedStatus OnCpuResetRequest(HANDLE h_app);

  //
  // Remote Data Reset Protocol callback functions
  //
  EFrameworkunifiedStatus OnRemoteDataResetRequest(HANDLE h_app);
  EFrameworkunifiedStatus OnRemoteDataResetResponse(HANDLE h_app);

  cmdHist m__CWORD56_RepHist;
  cmdHistIter m__CWORD56_RepIter;
  cmdHist m_PubCmdHist;
  cmdHistIter m_PubHistIter;
  cmdHist m_ErrHist;
  cmdHistIter m_ErrHistIter;
  cmdHist m_VCmdHist;
  cmdHistIter m_VHistIter;

  void SSPowerDebugDump(HANDLE h_app);

 private:
#define LOG_POWERSERVICELOG_CNT(caseid, Cnt) \
      FRAMEWORKUNIFIEDLOG_CNT(ZONE_INFO, Cnt, 0); \
    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " case " #caseid \
        ": FRAMEWORKUNIFIEDLOG_CNT(ZONE_INFO, " #Cnt ", 0)");

#define LOG_POWERSERVICELOG_EVT(caseid, Evt, Data0) \
      FRAMEWORKUNIFIEDLOG_EVT(ZONE_INFO, Evt, 1, Data0); \
    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " case " #caseid \
        ": FRAMEWORKUNIFIEDLOG_EVT(ZONE_INFO, " #Evt ", " #Data0 ")");

#define LOG_STATUS_IF_ERRORED_PWR_SM_WITH_HIST_LOGGING(l_eStatus, pStr)          \
    if (eFrameworkunifiedStatusOK != l_eStatus) {                  \
        LOG_ERROR(pStr);                               \
        SetCmdHist(pStr, m_ErrHist, m_ErrHistIter, GetStr(static_cast<EFrameworkunifiedStatus>(l_eStatus)).c_str()); \
    }  // End of #define LOG_STATUS_IF_ERRORED(l_eStatus,pStr)

#define SS_PWR_LOG_HIST(cmd, hist, it, sender, l_eStatus) \
    if (eFrameworkunifiedStatusOK == l_eStatus) {             \
        SetCmdHist((static_cast< std::string> (cmd)), (static_cast<cmdHist&> (hist)), \
                   (static_cast<cmdHistIter&> (it)), (static_cast< std::string> (sender)));      \
    }
  //**************************************************************************
  // Counter Logging enums & typedefs                                        *
  //                                                                         *
  //  Counter Event IDs                                                      *
  typedef enum {                                                          // *
    CT_Startup_CanSignal = 0x0114,                                   // *
    CT_Startup_DiagMsg = 0x0118,                                   // *
                                                                   // *
    CT_Shutdown_CanSignal = 0x0121,                                   // *
    CT_Shutdown_Timeout = 0x0124,                                   // *
    CT_Shutdown_Temperature = 0x0125,                                   // *
    CT_Shutdown_Error = 0x0126,                                   // *
    CT_Shutdown_Voltage = 0x0127                                    // *
  } EPWR_COUNTER_EVENT_ID_TYPE;       // *
  //                                                                         *
  // End of Counter Logging enums & typedefs                                 *
  //**************************************************************************

  //**************************************************************************
  // Event Logging enums & typedefs                                          *
  //                                                                         *
  //  Event ID                                                               *
  typedef enum {                                                          // *
    // Startup Event IDs                                                   *
    Ei_StartUp_Cansignal = 0x0101,                           // *
    Ei_StartUp_HK_On = 0x0102,                           // *
    //                                0x0103 not specified              // *
    //                                0x0104 not specified              // *
    //                                 0x0105 System Svcs not responsible// *
    //                                0x0106 not specified              // *
    Ei_StartUp_ActivationLine = 0x0107,                           // *
    Ei_StartUp_DiagMsg = 0x0108,                           // *
    //                                                                     *
    // Shutdown Event IDs                                                  *
    Ei_shutdown_CANsignal = 0x0201,                           // *
    Ei_shutdown_HkOFF = 0x0202,                           // *
    //                                0x0203 not specified              // *
    //                                0x0204 not specified              // *
    Ei_shutdown_Temperature = 0x0205,                           // *
    Ei_shutdown_NetworkError = 0x0206,                           // *
    Ei_shutdown_Voltage = 0x0207                            // *
  } EPWR_LOGGING_EVENT_ID_TYPE;              // *
  //                                                                         *
  //  Ei_StartUp_CanSignal byte0 Event Detail                                *
  typedef enum {                                                          // *
    epseisuedIGNITION = 0x00,                             // *
    epseisuedHK_ON = 0x01,                             // *
    epseisuedDOOR_MODULES = 0x02,                             // *
    //                                    0x03 not specified              // *
    //                                    0x04 not specified              // *
    epseisuedPASS = 0x05,                             // *
    epseisuedIHTM = 0x06,                             // *
    epseisuedMPM = 0x07,                             // *
    epseisuedSNA = 0xFF                              // *
  } EPWR_LOGGING_EVT_DETAIL_SU_CAN_TYPE;                                  // *

  //
  // Ei_StartUp_HkOn
  typedef enum {
    epseisuedHK_ON_Key = 0x00
  } EPWR_LOGGING_EVT_DETAIL_SU_KEY_TYPE;

  //
  // Ei_StartUp_ActivationLine
  typedef enum {
    epseisuedACV_LINE = 0x00
  } EPWR_LOGGING_EVT_DETAIL_SU_LINE_TYPE;

  //                                                                         *
  //  Ei_StartUp_DiagMsg byte0 Event Detail                                  *
  typedef enum {                                                          // *
    epseisuedHU = 0x01,                                          // *
    epseisuedCAN = 0x02,                                          // *
  } EPWR_LOGGING_EVT_DETAIL_SU_DIAG_TYPE;                                 // *
  //                                                                         *
  //  Ei_ShutDown_CanSignal byte0 Event Detail                               *
  typedef enum {                                                          // *
    epseisdcsIGN_STATE = 0x00,                                          // *
    epseisdcsHK_ON = 0x01                                           // *
  } EPWR_LOGGING_EVT_DETAIL_SD_CAN_TYPE;                                  // *
  //                                                                         *
  //  Ei_ShutDown_Temperature byte0 Event Detail                             *
  typedef enum {                                                          // *
    epseisdtempECU_LOW = 0x00,                                    // *
    epseisdtempECU_HIGH = 0x01,                                    // *
  } EPWR_LOGGING_EVT_DETAIL_SD_TEMP_TYPE;                                 // *
  //                                                                         *
  //  Ei_ShutDown_NetworkError byte0 Event Detail                            *
  typedef enum {                                                          // *
    epseisdneHMI_CAN = 0x00,                                    // *
    epseisdneHU_CAN = 0x01                                     // *
  } EPWR_LOGGING_EVT_DETAIL_SD_NETWORK_ERROR_TYPE;                        // *
  //                                                                         *
  //  Ei_ShutDown_Voltage byte0 Event Detail                                 *
  typedef enum {                                                          // *
    epseisdnvoltBATTERY_CUT_OFF = 0x00,                                // *
    epseisdnvoltLIMP_HOME = 0x01,                                // *
    epseisdnvoltUNKNOWN = 0xFF                                 // *
  } EPWR_LOGGING_EVT_DETAIL_SD_VOLTAGE_TYPE;                              // *
  //                                                                         *
  // End of Event Logging enums & typedefs                                   *
  //**************************************************************************

  PowerSessionHandler m_oSessionHandler;
  PowerConfigParams m_tConfigData;
  PowerStateMachine m_oStateMachine;
  UI_32 m_aTimerIDs[15];
  wakeInfo m_WakeUpData;   /// WakeUp data received from SPM
  SS_PSState m_PowerState;
  ePowerSrvVoltageStates m_VoltageState;
  ePowerSrvCrankStates m_CrankState;
  UI_32 m_MaxShutdownTimeout;
  std::map<SS_PSState, std::string> m_PPStateStrMap;

  // Notify Module on Voltage change detected.
  EFrameworkunifiedStatus PublishVoltageStateChange(HANDLE h_app);
  EFrameworkunifiedStatus ConstructPwrStateResponse(CHAR *f_MessageResponse);
  EFrameworkunifiedStatus AddStateInformationToResponse(CHAR *f_MessageResponse);
  EFrameworkunifiedStatus AddVoltageInformationToResponse(CHAR *f_MessageResponse);
  EFrameworkunifiedStatus AddCrankInformationToResponse(CHAR *f_MessageResponse);
  EFrameworkunifiedStatus RegisterAllCallbacksAndNofitications(HANDLE h_app);
  VOID SetPowerServiceState(SS_PSState f_NewState);
  EFrameworkunifiedStatus ValidateUserModeMessage(HANDLE h_app,
      EPWR_USER_MODE_TYPE &l_eUserModeState);  // NOLINT (runtime/references)

  pthread_mutex_t pwr_hist_mutex;

  void SetCmdHist(std::string cmd,
                  cmdHist &hist,  // NOLINT (runtime/references)
                  cmdHistIter &it,  // NOLINT (runtime/references)
                  std::string sender);
};

#endif  // POWER_SERVICE_SERVER_INCLUDE_SS_POWER_H_