summaryrefslogtreecommitdiff
path: root/AudioManagerDaemon/src/CAmNodeStateCommunicatorCAPI.cpp
blob: 700c9c3de261a5d2b4c775039a193e133374f7da (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
 * SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2012, BMW AG
 *
 * This file is part of GENIVI Project AudioManager.
 *
 * Contributions are licensed to the GENIVI Alliance under one or more
 * Contribution License Agreements.
 *
 * \copyright
 * 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/.
 *
 *
 * \author Aleksandar Donchev, aleksander.donchev@partner.bmw.de BMW 2013
 *
 * \file CAmNodeStateCommunicatorCAPI.cpp
 * For further information see http://www.genivi.org/.
 *
 */

#include <assert.h>
#include <string>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <functional>
#include <memory>
#include "config.h"
#include "CAmDltWrapper.h"
#include "CAmNodeStateCommunicatorCAPI.h"
#include "CAmControlSender.h"
#include <v1_0/org/genivi/NodeStateManager/LifeCycleConsumerProxy.hpp>


namespace am
{

#define LIFECYCLE_SERVICE_INTERFACE NSM_BUS_INTERFACE ".LifeCycleConsumer"

const char * CAmNodeStateCommunicatorCAPI::DEFAULT_DOMAIN_STRING = "local";

const char * CAmNodeStateCommunicatorCAPI::CLIENT_INSTANCE_STRING = NSM_BUS_INTERFACE;
const char * CAmNodeStateCommunicatorCAPI::CLIENT_INTERFACE_STRING = NSM_INTERFACE;

const char * CAmNodeStateCommunicatorCAPI::LIFECYCLE_SERVICE_INSTANCE_STRING = DBUS_SERVICE_PREFIX;
const char * CAmNodeStateCommunicatorCAPI::LIFECYCLE_SERVICE_INTERFACE_STRING = LIFECYCLE_SERVICE_INTERFACE;

const char * CAmNodeStateCommunicatorCAPI::OBJECT_NAME = DBUS_SERVICE_OBJECT_PATH;
const char * CAmNodeStateCommunicatorCAPI::BUS_NAME = LIFECYCLE_SERVICE_INTERFACE "_" DBUS_SERVICE_PREFIX;


#define IF_NOT_AVAILABLE_RETURN(error) \
if(!mIsServiceAvailable) { logError(__PRETTY_FUNCTION__, "Node State Manager not available yet"); return error; }

/**
 * Retrieves the value from given attribute wrapper.
 */
template <typename TValueReturnType, class TValueClass> am_Error_e getAttributeValue(CommonAPI::Attribute<TValueClass>* attribute, TValueReturnType & resultValue)
{
	CommonAPI::CallStatus status;
	typename CommonAPI::Attribute<TValueClass>::ValueType value;
	attribute->getValue(status, value);
	std::cout << std::endl << "CallStatus : " << static_cast<int>(status) << std::endl;
	if( CommonAPI::CallStatus::SUCCESS == status)
	{
		resultValue = static_cast<TValueReturnType>(value);
		return E_OK;
	}
	return E_UNKNOWN;
}


CAmNodeStateCommunicatorCAPI::CAmNodeStateCommunicatorCAPI(CAmCommonAPIWrapper* iCAPIWrapper) :
		CAmNodeStateCommunicator(),
		mpCAPIWrapper(iCAPIWrapper),
		mIsServiceAvailable(false)
{
    assert(mpCAPIWrapper);
    logInfo("CAmNodeStateCommunicatorCAPI::CAmNodeStateCommunicatorCAPI started");

    //Gets the factory pointer and build a proxy object
    mNSMProxy = iCAPIWrapper->buildProxy<am_nodestatemanager::ConsumerProxy>( CAmNodeStateCommunicatorCAPI::DEFAULT_DOMAIN_STRING,
			   	   	   	   	   	   	   	   	   	   	   	 	 	 	 	 	  CAmNodeStateCommunicatorCAPI::CLIENT_INSTANCE_STRING);
    //Makes subscriptions to the following 3 events
    mNSMProxy->getNodeStateEvent().subscribe(
    		std::bind(&CAmNodeStateCommunicatorCAPI::onNodeStateEvent, this, std::placeholders::_1)
    );
     mNSMProxy->getNodeApplicationModeEvent().subscribe(
    		std::bind(&CAmNodeStateCommunicatorCAPI::onNodeApplicationModeEvent, this, std::placeholders::_1)
    );
     mNSMProxy->getSessionStateChangedEvent().subscribe(
    		std::bind(&CAmNodeStateCommunicatorCAPI::onSessionStateChangedEvent, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)
    );
    mNSMProxy->getProxyStatusEvent().subscribe(std::bind(&CAmNodeStateCommunicatorCAPI::onServiceStatusEvent,this,std::placeholders::_1));
    //Instantiates the concrete stub implementation
    mNSMStub = std::make_shared<CAmNodeStateCommunicatorCAPI::CAmNodeStateCommunicatorServiceImpl>(this);

    //Registers the service
    if(!iCAPIWrapper->registerService(mNSMStub,CAmNodeStateCommunicatorCAPI::DEFAULT_DOMAIN_STRING,CAmNodeStateCommunicatorCAPI::LIFECYCLE_SERVICE_INSTANCE_STRING))
    	logError("AudioManager can't register service");
}

CAmNodeStateCommunicatorCAPI::~CAmNodeStateCommunicatorCAPI()
{
	mNSMProxy.reset();
	mpCAPIWrapper->unregisterService(CAmNodeStateCommunicatorCAPI::DEFAULT_DOMAIN_STRING,
									 CAmNodeStateCommunicatorCAPI::LIFECYCLE_SERVICE_INTERFACE_STRING,
									 CAmNodeStateCommunicatorCAPI::LIFECYCLE_SERVICE_INSTANCE_STRING);
	mNSMStub->setDelegate(NULL);
	mNSMStub.reset();
	mpCAPIWrapper = NULL;
}

bool CAmNodeStateCommunicatorCAPI::isServiceAvailable()
{
    return mIsServiceAvailable;
}

/** retrieves the actual restart reason
 *
 * @param restartReason
 * @return E_OK on success
 */
am_Error_e CAmNodeStateCommunicatorCAPI::nsmGetRestartReasonProperty(NsmRestartReason_e& restartReason)
{
	//Check the service via the proxy object is available
	IF_NOT_AVAILABLE_RETURN(E_NOT_POSSIBLE)
	//Get the attribute
	int32_t value;
	CommonAPI::CallStatus status;
	mNSMProxy->getRestartReasonAttribute().getValue(status,value);
	if (status!=CommonAPI::CallStatus::SUCCESS)
		return (E_UNKNOWN);
	restartReason=static_cast<NsmRestartReason_e>(value);
	return (E_OK);
}

/** retrieves the actual shutdown reason
 *
 * @param ShutdownReason
 * @return E_OK on success
 */
am_Error_e CAmNodeStateCommunicatorCAPI::nsmGetShutdownReasonProperty(NsmShutdownReason_e& ShutdownReason)
{
	//Check the service via the proxy object is available
	IF_NOT_AVAILABLE_RETURN(E_NOT_POSSIBLE)
	//Get the attribute
	int32_t value;
	CommonAPI::CallStatus status;
	mNSMProxy->getShutdownReasonAttribute().getValue(status,value);
	if (status!=CommonAPI::CallStatus::SUCCESS)
		return (E_UNKNOWN);
	ShutdownReason=static_cast<NsmShutdownReason_e>(value);
	return (E_OK);
}

/** retrieves the actual running reason
 *
 * @param nsmRunningReason
 * @return E_OK on success
 */
am_Error_e CAmNodeStateCommunicatorCAPI::nsmGetRunningReasonProperty(NsmRunningReason_e& nsmRunningReason)
{
	//Check the service via the proxy object is available
	IF_NOT_AVAILABLE_RETURN(E_NOT_POSSIBLE)
	//Get the attribute
	int32_t value;
	CommonAPI::CallStatus status;
	mNSMProxy->getWakeUpReasonAttribute().getValue(status,value);
	if (status!=CommonAPI::CallStatus::SUCCESS)
		return (E_UNKNOWN);
	nsmRunningReason=static_cast<NsmRunningReason_e>(value);
	return (E_OK);
}

/** gets the node state
 *
 * @param nsmNodeState
 * @return NsmErrorStatus_Ok on success
 */
NsmErrorStatus_e CAmNodeStateCommunicatorCAPI::nsmGetNodeState(NsmNodeState_e& nsmNodeState)
{
	//Check the service via the proxy object is available
	IF_NOT_AVAILABLE_RETURN(NsmErrorStatus_Error)

	CommonAPI::CallStatus callStatus;
	int32_t tmpNodeState = 0, errorCode = 0;
	mNSMProxy->GetNodeState(callStatus, tmpNodeState, errorCode);
	if( CommonAPI::CallStatus::SUCCESS == callStatus )
	{
		nsmNodeState = static_cast<NsmNodeState_e>(tmpNodeState);
		return (static_cast<NsmErrorStatus_e>(errorCode));
	}
	return NsmErrorStatus_Error;
}

/** gets the session state for a session and seatID
 *
 * @param sessionName the name of the session
 * @param seatID the seatID
 * @param sessionState
 * @return NsmErrorStatus_Ok on success
 */
NsmErrorStatus_e CAmNodeStateCommunicatorCAPI::nsmGetSessionState(const std::string& sessionName, const NsmSeat_e& seatID, NsmSessionState_e& sessionState)
{
	//Check the service via the proxy object is available
	IF_NOT_AVAILABLE_RETURN(NsmErrorStatus_Error)

	CommonAPI::CallStatus callStatus;
	int32_t tmpSessionState = 0 , errorCode = 0;
	mNSMProxy->GetSessionState(sessionName,seatID,callStatus, tmpSessionState, errorCode);

	if( CommonAPI::CallStatus::SUCCESS == callStatus)
	{
		sessionState = static_cast<NsmSessionState_e>(tmpSessionState);
		return (static_cast<NsmErrorStatus_e>(errorCode));
	}
	return NsmErrorStatus_Error;
}

/** gets the application mode
 *
 * @param applicationMode
 * @return NsmErrorStatus_Ok on success
 */
NsmErrorStatus_e CAmNodeStateCommunicatorCAPI::nsmGetApplicationMode(NsmApplicationMode_e& applicationMode)
{
	//Check the service via the proxy object is available
	IF_NOT_AVAILABLE_RETURN(NsmErrorStatus_Error)

	CommonAPI::CallStatus callStatus;
	int32_t tmpAppMode = 0 , errorCode = 0;
	mNSMProxy->GetApplicationMode(callStatus, tmpAppMode, errorCode);
	if( CommonAPI::CallStatus::SUCCESS == callStatus)
	{
		applicationMode = static_cast<NsmApplicationMode_e>(tmpAppMode);
		return (static_cast<NsmErrorStatus_e>(errorCode));
	}
	return NsmErrorStatus_Dbus;
}

/** this function registers the AudioManager as shutdown client at the NSM
 *  for more information check the Nodestatemanager
 * @param shutdownMode the shutdownmode you wish to set
 * @param timeoutMs the timeout you need to have
 * @return NsmErrorStatus_Ok on success
 */
NsmErrorStatus_e CAmNodeStateCommunicatorCAPI::nsmRegisterShutdownClient(const uint32_t shutdownMode, const uint32_t timeoutMs)
{
	//Check the service via the proxy object is available
	IF_NOT_AVAILABLE_RETURN(NsmErrorStatus_Error)

	CommonAPI::CallStatus callStatus;
	int32_t errorCode = 0;
	std::string objName = std::string(CAmNodeStateCommunicatorCAPI::OBJECT_NAME);
	std::string busName = std::string(CAmNodeStateCommunicatorCAPI::BUS_NAME);
	mNSMProxy->RegisterShutdownClient(busName, objName, shutdownMode, timeoutMs, callStatus, errorCode);
	if( CommonAPI::CallStatus::SUCCESS == callStatus)
		return (static_cast<NsmErrorStatus_e>(errorCode));
	return NsmErrorStatus_Dbus;

}

/** this function unregisters the AudioManager as shutdown client at the NSM
 *
 * @param shutdownMode
 * @return NsmErrorStatus_Ok on success
 */
NsmErrorStatus_e CAmNodeStateCommunicatorCAPI::nsmUnRegisterShutdownClient(const uint32_t shutdownMode)
{
	//Check the service via the proxy object is available
	IF_NOT_AVAILABLE_RETURN(NsmErrorStatus_Error)

	CommonAPI::CallStatus callStatus;
	int32_t errorCode = 0;
	std::string objName = std::string(CAmNodeStateCommunicatorCAPI::OBJECT_NAME);
	std::string busName = std::string(CAmNodeStateCommunicatorCAPI::BUS_NAME);
	mNSMProxy->UnRegisterShutdownClient(busName, objName, shutdownMode, callStatus, errorCode);
	if( CommonAPI::CallStatus::SUCCESS == callStatus)
		return (static_cast<NsmErrorStatus_e>(errorCode));
	return NsmErrorStatus_Dbus;
}

/** returns the interface version
 *
 * @param version
 * @return E_OK on success
 */
am_Error_e CAmNodeStateCommunicatorCAPI::nsmGetInterfaceVersion(uint32_t& version)
{
	//Check the service via the proxy object is available
	IF_NOT_AVAILABLE_RETURN(E_NOT_POSSIBLE)

	CommonAPI::CallStatus callStatus;
	mNSMProxy->GetInterfaceVersion(callStatus, version);
	if( CommonAPI::CallStatus::SUCCESS == callStatus)
		return E_OK;
	return E_UNKNOWN;
}

/** sends out the Lifecycle request complete message
 *
 * @param RequestId
 * @param status
 * @return NsmErrorStatus_Ok on success
 */
NsmErrorStatus_e CAmNodeStateCommunicatorCAPI::nsmSendLifecycleRequestComplete(const uint32_t RequestId, const NsmErrorStatus_e status)
{
	//Check the service via the proxy object is available
	IF_NOT_AVAILABLE_RETURN(NsmErrorStatus_Error)

	CommonAPI::CallStatus callStatus;
	int32_t errorCode = 0;
	mNSMProxy->LifecycleRequestComplete(RequestId, status, callStatus, errorCode);
	if( CommonAPI::CallStatus::SUCCESS == callStatus)
	{
		return (static_cast<NsmErrorStatus_e>(errorCode));
	}
	return NsmErrorStatus_Dbus;
}

/** notification handler for changed node state
 *
 * @param nodeState
 * @return none
 */
void CAmNodeStateCommunicatorCAPI::onNodeStateEvent(const int32_t nodeState)
{
	logInfo(__PRETTY_FUNCTION__, " got signal NodeState, with nodeState",nodeState);
	assert(mpControlSender);
	mpControlSender->hookSystemNodeStateChanged(static_cast<NsmNodeState_e>(nodeState));
}

/** notification handler for changed node application mode
 *
 * @param nodeApplicationMode
 * @return none
 */
void CAmNodeStateCommunicatorCAPI::onNodeApplicationModeEvent(const int32_t nodeApplicationMode)
{
	logInfo(__PRETTY_FUNCTION__, " got signal nodeApplicationMode, with applicationMode",nodeApplicationMode);
    assert(mpControlSender);
    mpControlSender->hookSystemNodeApplicationModeChanged(static_cast<NsmApplicationMode_e>(nodeApplicationMode));
}

/** notification handler for changed session state
 *
 * @param sessionName
 * @param seatID
 * @param sessionState
 * @return none
 */
void CAmNodeStateCommunicatorCAPI::onSessionStateChangedEvent(const std::string & sessionName, const int32_t seatID, const int32_t sessionState)
{
    logInfo(__PRETTY_FUNCTION__, " got signal sessionStateChanged, with session",sessionName,"seatID=",seatID,"sessionState",sessionState);
    assert(mpControlSender);
    mpControlSender->hookSystemSessionStateChanged(sessionName, static_cast<NsmSeat_e>(seatID), static_cast<NsmSessionState_e>(sessionState));
}

void CAmNodeStateCommunicatorCAPI::onServiceStatusEvent(const CommonAPI::AvailabilityStatus& serviceStatus)
{
    std::stringstream  avail;
    avail  << "(" << static_cast<int>(serviceStatus) << ")";

    logInfo("Service Status of the NSM changed to ", avail.str());
    std::cout << std::endl << "Service Status of the NSM changed to " << avail.str();
    mIsServiceAvailable = (serviceStatus==CommonAPI::AvailabilityStatus::AVAILABLE);
}

/** implements the service part, which is invoked from the node state manager
 *
 * @param sessionName
 * @param seatID
 * @param sessionState
 * @return none
 */
void CAmNodeStateCommunicatorCAPI::cbReceivedLifecycleRequest(uint32_t Request, uint32_t RequestId, int32_t& ErrorCode)
{
    assert(mpControlSender);
    ErrorCode = mpControlSender->hookSystemLifecycleRequest(Request, RequestId);
}

} /* namespace am */