summaryrefslogtreecommitdiffstats
path: root/power_service/server/src/ss_power_session.cpp
blob: 20db4a9a3628eb0a2d90aa2606272a37aabedab4 (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
/*
 * @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_Power
/// \brief    This file supports power service session management.
///
///////////////////////////////////////////////////////////////////////////////
#include "ss_power_session.h"
#include <system_service/ss_services.h>
#include <system_service/ss_templates.h>
#include <system_service/ss_power_service.h>
#include <system_service/ss_power_service_protocol.h>
#include <system_service/ss_power_service_local.h>
#include <native_service/frameworkunified_framework_if.h>
#include <native_service/frameworkunified_service_protocol.h>

#include "ss_power_powerservicelog.h"


PowerSessionHandler::PowerSessionHandler() {
  // TODO(my_username) Auto-generated constructor stub
}

PowerSessionHandler::~PowerSessionHandler() {  // LCOV_EXCL_START 14 Resident process, not called by NSFW
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  // TODO(my_username) Auto-generated destructor stub
}
// LCOV_EXCL_STOP 14 Resident process, not called by NSFW

EFrameworkunifiedStatus PowerSessionHandler::CloseSession(HANDLE h_app) {
  EFrameworkunifiedStatus l_eStatus;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  INTERFACEUNIFIEDLOG_RECEIVED_FROM(h_app);

  // find the subscriber...
  PwSessionIter iter = m_mapSessions.find(FrameworkunifiedGetMsgSrc(h_app));

  // the iter is set to the end then the subscriber is not in the map
  if (m_mapSessions.end() == iter) {  // LCOV_EXCL_BR_LINE 11:unexpected branch
    // LCOV_EXCL_START 11:unexpected branch
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(
        ZONE_ERR,
        __FUNCTION__,
        " Error: Subscriber '%s' was not found in the map; the Close " "Session Request is being ignored!",
        FrameworkunifiedGetMsgSrc(h_app));
    l_eStatus = eFrameworkunifiedStatusDbRecNotFound;
  } else if (NULL == iter->second.hsession) {  // LCOV_EXCL_BR_LINE 11:unexpected branch
    l_eStatus = eFrameworkunifiedStatusInvldHandle;
    LOG_ERROR("Client's PwSessionInfo handle");
    // LCOV_EXCL_STOP 11:unexpected branch
  } else {
    CloseSessionAck closeSessionAck;
    closeSessionAck.sessionId = FrameworkunifiedGetSessionId(iter->second.hsession);
    closeSessionAck.eStatus = eFrameworkunifiedStatusOK;

    // Send Ack to subscriber
    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]
      // LCOV_EXCL_START 4: NSFW error case.
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(
          ZONE_ERR,
          __FUNCTION__,
          " Error: FrameworkunifiedSendMsg(%s, PROTOCOL_CLOSE_SESSION_ACK) " "errored: %d/'%s'",
          iter->second.sz_name.c_str(), l_eStatus, GetStr(l_eStatus).c_str());
      // LCOV_EXCL_STOP 4: NSFW error case.
    } else {
      FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
             " FrameworkunifiedSendMsg(%s, PROTOCOL_CLOSE_SESSION_ACK) successful",
             iter->second.sz_name.c_str());
      iter->second.frunning = FALSE;

      //
      // ToDo Jay 2012 November 05 1) Can't detach just basic callbacks;
      //      client could be Supervisor or System. This has been solved
      //      in another Power Service Change Set. Need to get all
      //      Change Sets merged and delivered.
      UI_32 ss_power_basic_session_ids[] = { PROTOCOL_CLOSE_SESSION_REQ };

      // Detach callback : Power System Session Requests
      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]
        // LCOV_EXCL_START 4: NSFW error case.
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        LOG_ERROR("FrameworkunifiedDetachCallbacksFromDispatcher()");
        // LCOV_EXCL_STOP 4: NSFW error case.
      } else {
        FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
               " FrameworkunifiedDetachCallbacksFromDispatcher() successful");
      }

      CALL_AND_LOG_STATUS(FrameworkunifiedDestroySession(h_app, iter->second.hsession));
      iter->second.hsession = NULL;
      m_mapSessions.erase(iter);
    }
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}

EFrameworkunifiedStatus PowerSessionHandler::StartComplete(HANDLE h_app) {  // LCOV_EXCL_START 8: can not be called
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  EFrameworkunifiedStatus l_eStatus;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  StartCompleteAck ack;

  if (eFrameworkunifiedStatusOK != (l_eStatus = ReadMsg<StartCompleteAck>(h_app, ack))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    LOG_ERROR("ReadMsg()");
  } else {
    // find the subscriber...
    PwSessionIter iter = m_mapSessions.find(FrameworkunifiedGetMsgSrc(h_app));

    // the iter is set to the end then the subscriber is not in the map
    if (m_mapSessions.end() != iter) {
      iter->second.sz_servicename = ack.szServiceName;
      iter->second.frunning = TRUE;

      if (AllClientsInGroupStarted(iter->second.ui_groupid)) {
        // NOP
      }
    } else {
      FRAMEWORKUNIFIEDLOG(
          ZONE_ERR,
          __FUNCTION__,
          "Subscriber: %s , was not found in the map, the close session is being ignored!",
          FrameworkunifiedGetMsgSrc(h_app));
    }
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}
// LCOV_EXCL_STOP 8: can not be called
VOID PowerSessionHandler::Print() {
  // find the subscriber...
  PwSessionIter iter = m_mapSessions.begin();
  for (; iter != m_mapSessions.end(); iter++) {
    FRAMEWORKUNIFIEDLOG(ZONE_DEBUG_DUMP, __FUNCTION__,
           " Power Service is session-connected to '%s';  running: %s",
           iter->second.sz_name.data(), (iter->second.frunning ? "YES" : "NO"));
  }
}

// LCOV_EXCL_START 8: can not be called
BOOL PowerSessionHandler::WakeupComplete(RequiredModuleList & wakeup_modules) {
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  BOOL rtn = TRUE;  // set the return TRUE until we find a missing module
  RequiredModuleListIter iter = wakeup_modules.begin();
  for (; iter != wakeup_modules.end(); iter++) {
    PwSessionIter psi = m_mapSessions.find(*iter);
    if (m_mapSessions.end() == psi) {
      rtn = FALSE;  // Module Not Found in Power Service Session Handler
      FRAMEWORKUNIFIEDLOG(
          ZONE_INFO,
          __FUNCTION__,
          "Required Wakeup: %s has not connected to Power Service, Wakeup NOT COMPLETE!",
          iter->data());
    } else if (FALSE == psi->second.frunning) {
      rtn = FALSE;  // Module Found in Power Service Session Handler but hasn't started yet
      FRAMEWORKUNIFIEDLOG(
          ZONE_INFO,
          __FUNCTION__,
          "Required Wakeup: %s has connected to Power Service but is not running (FrameworkunifiedStart Response missing),"
          " Wakeup NOT COMPLETE!",
          iter->data());
    }
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return rtn;
}
// LCOV_EXCL_STOP 8: can not be called

BOOL PowerSessionHandler::ShutdownComplete(
    RequiredModuleList & shutdown_modules) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  BOOL rtn = TRUE;  // set the return TRUE until we find a missing module
  RequiredModuleListIter iter = shutdown_modules.begin();
  for (; iter != shutdown_modules.end(); iter++) {
    PwSessionIter psi = m_mapSessions.find(*iter);
    if (m_mapSessions.end() == psi) {
      FRAMEWORKUNIFIEDLOG(
          ZONE_INFO,
          __FUNCTION__,
          "Required Shutdown: %s is not in Power Service session list, Shutdown pending!",
          iter->data());
    } else if (TRUE == psi->second.frunning) {
      rtn = FALSE;  // Module Found in Power Service Session Handler but hasn't stopped yet
      FRAMEWORKUNIFIEDLOG(
          ZONE_INFO,
          __FUNCTION__,
          "Required Shutdown: %s is connected to Power Service but is running (Stop Response missing),"
          " Shutdown NOT COMPLETE!",
          iter->data());
    }
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return rtn;
}

EFrameworkunifiedStatus PowerSessionHandler::SendToSupervisor(UI_32 cmd, UI_32 length,
                                                 PVOID data) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  if (NULL == data && length > 0) {  // LCOV_EXCL_BR_LINE 6: it will not be true at the other place
    // LCOV_EXCL_START 6: it will not be true at the other place
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    LOG_ERROR("NULL == data && length > 0");
    l_eStatus = eFrameworkunifiedStatusInvldBuf;
    // LCOV_EXCL_STOP 6: it will not be true at the other place
  } else {
    // get first client from the map...
    PwSessionIter iter = m_mapSessions.begin();
    BOOL l_bFoundSupervisor = FALSE;

    for (; iter != m_mapSessions.end() && !l_bFoundSupervisor; iter++) {
      l_bFoundSupervisor = (epsstSUPERVISOR == iter->second.esessiontype);
      if (l_bFoundSupervisor) {
        l_eStatus = FrameworkunifiedSendMsg(iter->second.hsession, cmd, length, data);
        LOG_STATUS(l_eStatus, "FrameworkunifiedSendMsg()");
      }
    }
    if (!l_bFoundSupervisor) {
      FRAMEWORKUNIFIEDLOG(
          ZONE_ERR, __FUNCTION__,
          "Error: Did not find a Supervisor session; did not " "send cmd %d !",
          cmd);
      l_eStatus = eFrameworkunifiedStatusDbRecNotFound;
    }
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}

BOOL PowerSessionHandler::AllClientsInGroupStarted(UI_32 f_ungrpid) {  // LCOV_EXCL_START 8: can not be called
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  BOOL fStatus = TRUE;
  BOOL fGrpFnd = FALSE;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  PwSessionIter iter = m_mapSessions.begin();

  if (iter == m_mapSessions.end())
    fStatus = TRUE;

  for (; iter != m_mapSessions.end() && fStatus; iter++) {
    if (f_ungrpid == iter->second.ui_groupid) {
      fGrpFnd = TRUE;
      if (FALSE == iter->second.frunning) {
        fStatus = FALSE;
      }
    }
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "- return flag: %s",
         ((fStatus && fGrpFnd) ? "TRUE" : "FALSE"));
  return (fStatus && fGrpFnd);
}
// LCOV_EXCL_STOP 8: can not be called