summaryrefslogtreecommitdiff
path: root/AudioManagerUtilities/include/CAmCommonAPIWrapper.h
blob: a83b5b3c799330067b4b35228173280701a09649 (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
/**
 * 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 <cassert>
#include <CommonAPI/CommonAPI.hpp>
#ifndef COMMONAPI_INTERNAL_COMPILATION
#define COMMONAPI_INTERNAL_COMPILATION
#include <CommonAPI/MainLoopContext.hpp>
#undef COMMONAPI_INTERNAL_COMPILATION
#endif
#include <CommonAPI/Utils.hpp>
#include "audiomanagerconfig.h"
#include "CAmSocketHandler.h"

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

namespace am
{

class CAmSocketHandler;

class CAmCommonAPIWrapper
{
    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;
        CommonAPI::Timeout* timeout;
    };

    CAmSocketHandler *mpSocketHandler; //!< pointer to the sockethandler

    std::shared_ptr<CommonAPI::Runtime> mRuntime;
    std::shared_ptr<CommonAPI::MainLoopContext> mContext;

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

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

protected:
    CAmCommonAPIWrapper(CAmSocketHandler* socketHandler, const std::string & applicationName = "") ;

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 Deletes the instanciated object
	*/	
	static void deleteInstance();

	/**
	* \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, const std::string & applicationName = "");


	/**
	* \brief Getter for the socket handler.
	*
	* @return Pointer to the socket handler.
	*/
	CAmSocketHandler *getSocketHandler() const { return mpSocketHandler; }

    /** 
    * \brief Deprecated method. This class is used only in single connection applications and no connectionId is needed. Instead you should use bool registerService(const std::shared_ptr<TStubImp> & shStub, const std::string & domain, const std::string & instance). 
    * 
    * 
    * Example: std::shared_ptr<ConcreteStubClass> aStub; 
    *        registerService(  aStub, "local", "com.your_company.instance_name", "service-name"); 
    * 
    * @param shStub: Shared pointer to a stub instance 
    * @param domain: A string with the domain name, usually "local" 
    * @param instance: Common-api instance string as example "com.your_company.instance_name" 
    * @param connectionId: A string connection id, which is used by CommonAPI to group applications 
    * 
    */ 
    template <class TStubImp> bool __attribute__((deprecated)) registerService(const std::shared_ptr<TStubImp> & shStub, const std::string & domain, const std::string & instance, const CommonAPI::ConnectionId_t __attribute__((__unused__)) & /*connectionId*/) 
    { 
        return mRuntime->registerService(domain, instance, shStub, mContext); 
    } 
	
	/**
	* \brief Register stub objects.
	*
	* Example: std::shared_ptr<ConcreteStubClass> aStub;
	* 		   registerService(	aStub, "local", "com.your_company.instance_name");
	*
	* @param shStub: Shared pointer to a stub instance
	* @param domain: A string with the domain name, usually "local"
	* @param instance: Common-api instance string as example "com.your_company.instance_name"
	*
	*/
	template <class TStubImp> bool registerService(const std::shared_ptr<TStubImp> & shStub, const std::string & domain, const std::string & instance)
	{
		return mRuntime->registerService(domain, instance, shStub, mContext);
	}

	/**
	* \brief Unregister stub objects.
	*
	* @param domain: A string with the domain name, usually "local"
	* @param interface: Common-api interface string as example "com.your_company.interface_name"
	* @param instance: Common-api instance string as example "com.your_company.instance_name"
	*
	*/
	bool unregisterService(const std::string &domain, const std::string &interface, const std::string &instance)
	{
		return mRuntime->unregisterService(domain, interface, instance);
	}

	/**
    * \brief Deprecated method. Instead you should use bool registerService(const std::shared_ptr<TStubImp> & shStub, const std::string & domain, const std::string & instance). 
  * 
  * Register stub objects. 
  * 
  * Example: std::shared_ptr<ConcreteStubClass> aStub; 
  *        registerService(  aStub, "local:com.your_company.interface_name:com.your_company.instance_name"); 
  * 
  * @param shStub: Shared pointer to a stub instance 
  * @param address: Complete common-api address as example "local:com.your_company.interface_name:com.your_company.instance_name" 
  * 
  */ 
  template <class TStubImp> bool __attribute__((deprecated)) registerStub(const std::shared_ptr<TStubImp> & shStub, const std::string & address) 
   { 
    std::vector<std::string> parts = CommonAPI::split(address, ':'); 
    assert(parts.size()==3); 
 
    return registerService(shStub, parts[0], parts[2]); 
   } 
 
  /** 
  * \brief Deprecated method. Instead you should use bool unregisterService(const std::string &domain, const std::string &interface, const std::string &instance). 
  * 
  * Unregister stub objects. 
  * 
  * @param address: Complete common-api address as example "local:com.your_company.interface_name:com.your_company.instance_name" 
  * 
  */ 
  bool __attribute__((deprecated)) unregisterStub(const std::string & address) 
  { 
    std::vector<std::string> parts = CommonAPI::split(address, ':'); 
    assert(parts.size()==3); 
 
    return unregisterService(parts[0], parts[1], parts[2]); 
   } 
  
    /** 
    * \brief  Deprecated method. This class is used only in single connection applications and no connectionId is needed. Instead you should use buildProxy(const std::string &domain, const std::string &instance). 
    * 
    * Example: std::shared_ptr<AProxyClass<>> aProxy = buildProxy<AProxyClass>("local", "com.your_company.instance_name", "client-name"); 
    * 
    * @param domain: A string with the domain name, usually "local" 
    * @param instance: Common-api instance string as example "com.your_company.instance_name" 
    * @param connectionId: A string connection id, which is used by CommonAPI to group applications 
    * 
    * @return A proxy object. 
    */ 
    template<template<typename ...> class ProxyClass, typename ... AttributeExtensions> 
    std::shared_ptr<ProxyClass<AttributeExtensions...>> __attribute__((deprecated)) buildProxy(const std::string &domain, const std::string &instance, const CommonAPI::ConnectionId_t __attribute__((__unused__)) & /*connectionId*/) 
    { 
            return mRuntime->buildProxy<ProxyClass>(domain, instance, mContext);
    } 
 
  /** 
	* \brief Build proxy objects.
	*
	* Example: std::shared_ptr<AProxyClass<>> aProxy = buildProxy<AProxyClass>("local", "com.your_company.instance_name");
	*
	* @param domain: A string with the domain name, usually "local"
	* @param instance: Common-api instance string as example "com.your_company.instance_name"
	*
	* @return A proxy object.
	*/
	template<template<typename ...> class ProxyClass, typename ... AttributeExtensions>
	std::shared_ptr<ProxyClass<AttributeExtensions...>> buildProxy(const std::string &domain, const std::string &instance)
	{
		return mRuntime->buildProxy<ProxyClass>(domain, instance, mContext);
	}
	
	
    /** 
    * \brief  Deprecated method. Instead you should use buildProxy(const std::string &domain, const std::string &instance). 
    * 
    * Build proxy objects. 
    * Example: std::shared_ptr<AProxyClass<>> aProxy = buildProxy<AProxyClass>("local:com.your_company.interface_name:com.your_company.instance_name"); 
    * 
    * @param address: Complete common-api address as example "local:com.your_company.interface_name:com.your_company.instance_name" 
    * 
    * @return A proxy object. 
    */ 
    template<template<typename ...> class ProxyClass, typename ... AttributeExtensions> 
    std::shared_ptr<ProxyClass<AttributeExtensions...>> __attribute__((deprecated)) buildProxy(const std::string & address) 
    { 
        std::vector<std::string> parts=CommonAPI::split(address, ':'); 
        assert(parts.size()==3); 
    
        return buildProxy<ProxyClass>(parts[0], parts[2]); 
    } 

};


//Alias
extern CAmCommonAPIWrapper* (*getCAPI)();

#ifndef AMCAPI
	#define AMCAPI getCAPI()
#endif

#ifndef AM_CAPI
	#define AM_CAPI getCAPI()
#endif

#ifndef CAPI
	#define CAPI getCAPI()
#endif


}

#endif /* COMMONAPIWRAPPER_H_ */