summaryrefslogtreecommitdiff
path: root/NodeStateAccess/NodeStateConsumer_StubImpl.cpp
blob: 29f572e0c4eded7e0983d25a92a85159f0feb597 (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
/**********************************************************************************************************************
 *
 * Copyright (C) 2017 BMW AG
 *
 * Interface between NodeStateManager and IPC
 *
 * This source file is a part of the NodeStateAccess library (NSMA).
 * The architecture requires that the NodeStateManager (NSM) is independent from the CommonAPI binding.
 * Therefore, the CommonAPI communication is handled inside of this library.
 * The library offers the NSM an interface to use objects generated via CommonAPI.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 **********************************************************************************************************************/

#include "NodeStateConsumer_StubImpl.h"
#include "Watchdog.hpp"

#include <dlt.h>
#include <iostream>

DLT_IMPORT_CONTEXT(NsmaContext);

NodeStateConsumer_StubImpl::NodeStateConsumer_StubImpl(NSMA_tstObjectCallbacks* objectCallbacks)
{
   NSMA__stObjectCallbacks = objectCallbacks;
   trySetBootModeAttribute(0);
   setRestartReasonAttribute(GENIVI::NodeStateManagerTypes::NsmRestartReason_e::NsmRestartReason_NotSet);
   setShutdownReasonAttribute(GENIVI::NodeStateManagerTypes::NsmShutdownReason_e::NsmShutdownReason_NotSet);
   setWakeUpReasonAttribute(GENIVI::NodeStateManagerTypes::NsmRunningReason_e::NsmRunningReason_NotSet);
}

void NodeStateConsumer_StubImpl::GetInterfaceVersion(const std::shared_ptr<CommonAPI::ClientId> _client, GetInterfaceVersionReply_t _reply)
{
   NSMTriggerWatchdog(NsmWatchdogState_Active);
   CommonAPI::Version v = ConsumerStubDefault::getInterfaceVersion(_client);
   _reply( (v.Major << 24) | (v.Minor << 16));
   NSMUnregisterWatchdog();
}

void NodeStateConsumer_StubImpl::GetNodeState(const std::shared_ptr<CommonAPI::ClientId> __attribute__((__unused__)) _client, GetNodeStateReply_t _reply)
{
   NSMTriggerWatchdog(NsmWatchdogState_Active);
   DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: GetNodeState."));

   NsmNodeState_e enNodeState = NsmNodeState_NotSet;
   NsmErrorStatus_e nodeStateId = NSMA__stObjectCallbacks->pfGetNodeStateCb(&enNodeState);
   _reply(GENIVI::NodeStateManagerTypes::NsmNodeState_e((GENIVI::NodeStateManagerTypes::NsmNodeState_e::Literal) enNodeState),
          GENIVI::NodeStateManagerTypes::NsmErrorStatus_e((GENIVI::NodeStateManagerTypes::NsmErrorStatus_e::Literal) nodeStateId));
   NSMUnregisterWatchdog();
}

void NodeStateConsumer_StubImpl::SetSessionState(const std::shared_ptr<CommonAPI::ClientId> __attribute__((__unused__)) _client, std::string _SessionName, std::string _SessionOwner, GENIVI::NodeStateManagerTypes::NsmSeat_e _SeatID, GENIVI::NodeStateManagerTypes::NsmSessionState_e _SessionState, SetSessionStateReply_t _reply)
{
   NSMTriggerWatchdog(NsmWatchdogState_Active);
   DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: SetSessionState. SessionName:"), DLT_STRING(_SessionName.data()),
         DLT_STRING("SessionOwner:"), DLT_STRING(_SessionOwner.data()),
         DLT_STRING("SeatID:"), DLT_INT(_SeatID),
         DLT_STRING("SessionState:"), DLT_INT(_SessionState));

   _reply(GENIVI::NodeStateManagerTypes::NsmErrorStatus_e((GENIVI::NodeStateManagerTypes::NsmErrorStatus_e::Literal)
         NSMA__stObjectCallbacks->pfSetSessionStateCb(_SessionName.c_str(), _SessionOwner.c_str(), (NsmSeat_e)(int) _SeatID, (NsmSessionState_e)(int) _SessionState)));
   NSMUnregisterWatchdog();
}

void NodeStateConsumer_StubImpl::GetSessionState(const std::shared_ptr<CommonAPI::ClientId> __attribute__((__unused__)) _client, std::string _SessionName, GENIVI::NodeStateManagerTypes::NsmSeat_e _SeatID, GetSessionStateReply_t _reply)
{
   NSMTriggerWatchdog(NsmWatchdogState_Active);
   DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: GetSessionState."));
   NsmSessionState_e enSessionState = NsmSessionState_Unregistered;
   NsmErrorStatus_e errorCode = NSMA__stObjectCallbacks->pfGetSessionStateCb(_SessionName.c_str(), (NsmSeat_e)(int) _SeatID, &enSessionState);
   DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: GetSessionState. Reply:"), DLT_INT(enSessionState), DLT_INT(errorCode));
   _reply(GENIVI::NodeStateManagerTypes::NsmSessionState_e((GENIVI::NodeStateManagerTypes::NsmSessionState_e::Literal)enSessionState),
          GENIVI::NodeStateManagerTypes::NsmErrorStatus_e((GENIVI::NodeStateManagerTypes::NsmErrorStatus_e::Literal)errorCode));
   DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: GetSessionState. Reply done"));
   NSMUnregisterWatchdog();
}

void NodeStateConsumer_StubImpl::RegisterShutdownClient(const std::shared_ptr<CommonAPI::ClientId> _client, uint32_t _ShutdownMode, uint32_t _TimeoutMs, RegisterShutdownClientReply_t _reply)
{
   NSMTriggerWatchdog(NsmWatchdogState_Active);
   DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: RegisterShutdownClient. ShutdownMode:"), DLT_UINT(_ShutdownMode),
                                      DLT_STRING("TimeoutMs:"), DLT_INT(_TimeoutMs));
   std::unique_lock<std::mutex> lock(mMutex);
   /*
    *   Pass NULL because client should already be registered by onShutdownEventsSelectiveSubscriptionChanged.
    *   Just update ShutdownMode and TimeoutMs
    */
   NsmErrorStatus_e errorCode =  NSMA__stObjectCallbacks->pfRegisterLifecycleClientCb(_client->hashCode(), _ShutdownMode, _TimeoutMs);
   /* NSMA returns NsmErrorStatus_Last when client already registered -> this is the normal case here */
   errorCode = errorCode == NsmErrorStatus_Last ? NsmErrorStatus_Ok : errorCode;

   _reply(GENIVI::NodeStateManagerTypes::NsmErrorStatus_e((GENIVI::NodeStateManagerTypes::NsmErrorStatus_e::Literal) errorCode ));
   lock.unlock();
   NSMUnregisterWatchdog();
}

void NodeStateConsumer_StubImpl::UnRegisterShutdownClient(const std::shared_ptr<CommonAPI::ClientId> _client, uint32_t _ShutdownMode, UnRegisterShutdownClientReply_t _reply)
{
   NSMTriggerWatchdog(NsmWatchdogState_Active);
   DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: UnRegisterShutdownClient. ShutdownMode:"), DLT_UINT(_ShutdownMode));
   std::unique_lock<std::mutex> lock(mMutex);
   NsmErrorStatus_e enRetVal = NSMA__stObjectCallbacks->pfUnRegisterLifecycleClientCb(_client->hashCode(), _ShutdownMode);

   if(enRetVal != NsmErrorStatus_Ok)
   {
     DLT_LOG(NsmaContext, DLT_LOG_WARN, DLT_STRING("NSM: Failed to unregister lifecycle consumer."),
                                        DLT_STRING("Client hash:"),  DLT_UINT64(_client->hashCode()),
                                        DLT_STRING("Unregistered mode(s):"), DLT_INT(_ShutdownMode));
   }
   _reply(GENIVI::NodeStateManagerTypes::NsmErrorStatus_e((GENIVI::NodeStateManagerTypes::NsmErrorStatus_e::Literal)enRetVal));
   lock.unlock();
   NSMUnregisterWatchdog();
}

void NodeStateConsumer_StubImpl::RegisterSession(const std::shared_ptr<CommonAPI::ClientId> __attribute__((__unused__)) _client, std::string _SessionName, std::string _SessionOwner, GENIVI::NodeStateManagerTypes::NsmSeat_e _SeatID, GENIVI::NodeStateManagerTypes::NsmSessionState_e _SessionState, RegisterSessionReply_t _reply)
{
   NSMTriggerWatchdog(NsmWatchdogState_Active);
   DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: RegisterSession. SessionName:"), DLT_STRING(_SessionName.data()),
           DLT_STRING("SessionOwner:"), DLT_STRING(_SessionOwner.data()),
           DLT_STRING("SeatID:"), DLT_INT(_SeatID),
           DLT_STRING("SessionState:"), DLT_INT(_SessionState));
   _reply(GENIVI::NodeStateManagerTypes::NsmErrorStatus_e((GENIVI::NodeStateManagerTypes::NsmErrorStatus_e::Literal)NSMA__stObjectCallbacks->pfRegisterSessionCb(_SessionName.c_str(), _SessionOwner.c_str(), (NsmSeat_e)(int) _SeatID, (NsmSessionState_e)(int) _SessionState)));
   NSMUnregisterWatchdog();
}

void NodeStateConsumer_StubImpl::UnRegisterSession(const std::shared_ptr<CommonAPI::ClientId> __attribute__((__unused__)) _client, std::string _SessionName, std::string _SessionOwner, GENIVI::NodeStateManagerTypes::NsmSeat_e _SeatID, UnRegisterSessionReply_t _reply)
{
   NSMTriggerWatchdog(NsmWatchdogState_Active);
   DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: UnRegisterSession. SessionName:"), DLT_STRING(_SessionName.data()),
           DLT_STRING("SessionOwner:"), DLT_STRING(_SessionOwner.data()),
           DLT_STRING("SeatID:"), DLT_INT(_SeatID));
   _reply(GENIVI::NodeStateManagerTypes::NsmErrorStatus_e((GENIVI::NodeStateManagerTypes::NsmErrorStatus_e::Literal)NSMA__stObjectCallbacks->pfUnRegisterSessionCb(_SessionName.c_str(), _SessionOwner.c_str(), (NsmSeat_e)(int) _SeatID)));
   NSMUnregisterWatchdog();
}

void NodeStateConsumer_StubImpl::GetAppHealthCount(const std::shared_ptr<CommonAPI::ClientId> __attribute__((__unused__)) _client, GetAppHealthCountReply_t _reply)
{
   NSMTriggerWatchdog(NsmWatchdogState_Active);
   DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: GetAppHealthCount."));
   _reply(GENIVI::NodeStateManagerTypes::NsmErrorStatus_e((GENIVI::NodeStateManagerTypes::NsmErrorStatus_e::Literal)NSMA__stObjectCallbacks->pfGetAppHealthCountCb()));
   NSMUnregisterWatchdog();
}

void NodeStateConsumer_StubImpl::LifecycleRequestComplete(const std::shared_ptr<CommonAPI::ClientId> _client, GENIVI::NodeStateManagerTypes::NsmErrorStatus_e _Status, LifecycleRequestCompleteReply_t _reply)
{
   NSMTriggerWatchdog(NsmWatchdogState_Active);
   size_t client = _client->hashCode();
   if(_Status >= NsmErrorStatus_NotSet && _Status <= NsmErrorStatus_Last)
   {
     DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: LifecycleRequestComplete Client: "), DLT_INT64(client), DLT_STRING(" Return Value: "), DLT_STRING(ERRORSTATUS_STRING[_Status]));
     std::lock_guard<std::mutex> lock(mMutex);
     _reply(GENIVI::NodeStateManagerTypes::NsmErrorStatus_e((GENIVI::NodeStateManagerTypes::NsmErrorStatus_e::Literal)NSMA_ClientRequestFinish(_client, _Status)));
   }
   else
   {
     DLT_LOG(NsmaContext, DLT_LOG_ERROR, DLT_STRING("NSMA: LifecycleRequestComplete Client: "), DLT_INT64(client), DLT_STRING(" Invalid return value: "), DLT_INT(_Status));
   }
   NSMUnregisterWatchdog();
}

void NodeStateConsumer_StubImpl::onShutdownEventsSelectiveSubscriptionChanged(const std::shared_ptr<CommonAPI::ClientId> _client, const CommonAPI::SelectiveBroadcastSubscriptionEvent _event)
{
   NSMTriggerWatchdog(NsmWatchdogState_Active);
   std::lock_guard<std::mutex> lock(mMutex);
   if (_event == CommonAPI::SelectiveBroadcastSubscriptionEvent::SUBSCRIBED)
   {
      NSMA__stObjectCallbacks->pfRegisterLifecycleClientCb(_client->hashCode(), NSM_SHUTDOWNTYPE_NOT, 0);
   }
   else if (_event == CommonAPI::SelectiveBroadcastSubscriptionEvent::UNSUBSCRIBED)
   {
      NSMA__stObjectCallbacks->pfUnRegisterLifecycleClientCb(_client->hashCode(), NSM_SHUTDOWNTYPE_FAST | NSM_SHUTDOWNTYPE_NORMAL | NSM_SHUTDOWNTYPE_PARALLEL | NSM_SHUTDOWNTYPE_RUNUP);
   }
   NSMUnregisterWatchdog();
}