summaryrefslogtreecommitdiff
path: root/AudioManagerUtilities/include/CAmCommonAPIWrapper.h
blob: e756cc7e91f3e27bc04149ee2a2a3bdce76ae66c (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
/**
 * SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2012, BMW AG
 *
 * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
 * \author Aleksandar Donchev, aleksander.donchev@partner.bmw.de BMW 2013
 *
 * \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/.
 *
 * \file CAmCommonAPIWrapper.h
 * For further information see http://www.genivi.org/.
 */


#ifndef COMMONAPIWRAPPER_H_
#define COMMONAPIWRAPPER_H_

#include <string>
#include <list>
#include <map>
#include <queue>
#include <memory>
#include <CommonAPI/CommonAPI.h>
#include "config.h"
#include "CAmSocketHandler.h"


/**
 * A Common-API wrapper class, that loads the common-api runtime and instantiates all necessary other objects. Works with the CAmSocketHandler.
 * It is implemented as singleton and usually once instantiated at the beginning with CAmSocketHandler.
 * Example: CAmCommonAPIWrapper *pCAPIWrapper = CAmCommonAPIWrapper::instantiateOnce( aSocketHandlerPointer );
 */

namespace am
{
using namespace CommonAPI;

class CAmSocketHandler;

class CAmCommonAPIWrapper
{
public:

    virtual ~CAmCommonAPIWrapper();
	/**
	* \brief Returns an already instantiated object.
	*
	* This method should be called after the instantiateOnce(...) has been called with non null socket handler parameter.
	*
	* @return The common-api wrapper object.
	*/
	static CAmCommonAPIWrapper* getInstance();
	/**
	* \brief Creates a singleton instance attached to the provided socket handler object.
	*
	* This method should be called only once because it instantiates a single object.
	* Otherwise it will throw an exception.
	* The first call of this method with non null parameter loads the common-api and attaches it to the main loop.
	*
	* @param socketHandler: A pointer to socket handler or NULL
	*
	* @return The common-api wrapper object.
	*/
	static CAmCommonAPIWrapper* instantiateOnce(CAmSocketHandler* socketHandler);

	void registerDispatchSource(DispatchSource* dispatchSource, const DispatchPriority dispatchPriority);
	void deregisterDispatchSource(DispatchSource* dispatchSource);
	void registerWatch(Watch* watch, const DispatchPriority dispatchPriority);
	void deregisterWatch(Watch* watch);
	void registerTimeout(Timeout* timeout, const DispatchPriority dispatchPriority);
	void deregisterTimeout(Timeout* timeout);
	void wakeup();

	std::shared_ptr<CommonAPI::Factory> factory() const;
	std::shared_ptr<CommonAPI::Runtime> runtime() const;
	//Wraps the invitation to the service publisher
	template <class TStubImp> bool registerStub(const std::shared_ptr<TStubImp> & shStub, const std::string & aCommonAPIAddress)
	{
		return runtime()->getServicePublisher()->registerService(shStub, aCommonAPIAddress, factory());
	}
	bool unregisterStub(const std::string & aCommonAPIAddress)
	{
		(void)aCommonAPIAddress;
		/** Not implemented yet
			todo: Check whether the appropriate method is available and uncomment...

			return runtime()->getServicePublisher()->unregisterService(aCommonAPIAddress);
		*/
		return true;
	}


protected:
	CAmCommonAPIWrapper(CAmSocketHandler* socketHandler) ;
private:
    void commonPrepareCallback(const sh_pollHandle_t handle, void* userData);
	TAmShPollPrepare<CAmCommonAPIWrapper> pCommonPrepareCallback;

    bool commonDispatchCallback(const sh_pollHandle_t handle, void* userData);
    TAmShPollDispatch<CAmCommonAPIWrapper> pCommonDispatchCallback;

    void commonFireCallback(const pollfd pollfd, const sh_pollHandle_t, void*);
    TAmShPollFired<CAmCommonAPIWrapper> pCommonFireCallback;

    bool commonCheckCallback(const sh_pollHandle_t handle, void*);
    TAmShPollCheck<CAmCommonAPIWrapper> pCommonCheckCallback;

    void commonTimerCallback(sh_timerHandle_t handle, void* userData);
    TAmShTimerCallBack<CAmCommonAPIWrapper> pCommonTimerCallback;

     struct timerHandles
    {
        sh_timerHandle_t handle;
        Timeout* timeout;
    };
     //!< reference to the dbus instance
    CAmSocketHandler *mpSocketHandler; //!< pointer to the sockethandler

    std::shared_ptr<CommonAPI::Factory> mFactory;
    std::shared_ptr<CommonAPI::MainLoopContext> mContext;

    DispatchSourceListenerSubscription mDispatchSourceListenerSubscription;
    WatchListenerSubscription mWatchListenerSubscription;
    TimeoutSourceListenerSubscription mTimeoutSourceListenerSubscription;
    WakeupListenerSubscription mWakeupListenerSubscription;
    std::multimap<DispatchPriority, DispatchSource*> mRegisteredDispatchSources;
    std::map<int,Watch*> mMapWatches;
    Watch* mWatchToCheck;
    std::list<DispatchSource*> mSourcesToDispatch;
    std::vector<timerHandles> mpListTimerhandles;
};

#define Am_CAPI CAmCommonAPIWrapper::getInstance()

}

#endif /* COMMONAPIWRAPPER_H_ */