summaryrefslogtreecommitdiff
path: root/AudioManagerDaemon/src/CAmCommonAPIWrapper.cpp
blob: 28103deb6f154b67f1e8bf816fb5801742444d70 (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
/**
 *  Copyright (C) 2012, BMW AG
 *
 *  \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
 *  \author Aleksandar Donchev, aleksander.donchev@partner.bmw.de BMW 2013
 *
 *  \copyright
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
 *  including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 *  subject to the following conditions:
 *  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *  \file CAmCommonAPIWrapper.cpp
 *  For further information see http://www.genivi.org/.
 */


#include <config.h>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <stdexcept>
#include <poll.h>
#include <tuple>
#include "audiomanagertypes.h"
#include "shared/CAmSocketHandler.h"
#include "shared/CAmDltWrapper.h"
#include "shared/CAmCommonAPIWrapper.h"


namespace am
{
static CAmCommonAPIWrapper* pSingleCommonAPIInstance = NULL;


using namespace CommonAPI;

CAmCommonAPIWrapper::CAmCommonAPIWrapper(CAmSocketHandler* socketHandler):
				pCommonPrepareCallback(this,&CAmCommonAPIWrapper::commonPrepareCallback), //
		        pCommonDispatchCallback(this, &CAmCommonAPIWrapper::commonDispatchCallback), //
		        pCommonFireCallback(this, &CAmCommonAPIWrapper::commonFireCallback), //
		        pCommonCheckCallback(this, &CAmCommonAPIWrapper::commonCheckCallback), //
		        pCommonTimerCallback(this, &CAmCommonAPIWrapper::commonTimerCallback), //
		        mpSocketHandler(socketHandler), //
		        mWatchToCheck(NULL)
{
	assert(NULL!=socketHandler);
//1. Load the runtime
	std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load();
//2. Get the context and store a pointer to it
	mContext = runtime->getNewMainLoopContext();
//3. Make subscriptions
   mDispatchSourceListenerSubscription = mContext->subscribeForDispatchSources(
			std::bind(&CAmCommonAPIWrapper::registerDispatchSource, this, std::placeholders::_1, std::placeholders::_2),
			std::bind(&CAmCommonAPIWrapper::deregisterDispatchSource, this, std::placeholders::_1));
	mWatchListenerSubscription = mContext->subscribeForWatches(
			std::bind(&CAmCommonAPIWrapper::registerWatch, this, std::placeholders::_1, std::placeholders::_2),
			std::bind(&CAmCommonAPIWrapper::deregisterWatch, this, std::placeholders::_1));
	mTimeoutSourceListenerSubscription = mContext->subscribeForTimeouts(
			std::bind(&CAmCommonAPIWrapper::registerTimeout, this, std::placeholders::_1, std::placeholders::_2),
			std::bind(&CAmCommonAPIWrapper::deregisterTimeout, this, std::placeholders::_1));
//4. Create the factory
	std::shared_ptr<CommonAPI::Factory> factory = runtime->createFactory(mContext);
	assert(factory);
	logInfo(__PRETTY_FUNCTION__,"CommonAPI -> Factory created");
	mFactory = factory;
//5. Get the publisher V.2.1
//	std::shared_ptr<CommonAPI::ServicePublisher> servicePublisher = runtime->getServicePublisher();
//	assert(servicePublisher);
//	logInfo(__PRETTY_FUNCTION__,"CommonAPI -> Publisher available");
//6. Instantiate your concrete stub implementations
//	std::shared_ptr<StubImpl> theStub = std::make_shared<StubImpl>(1);
//7. Register the services
//	std::string capiAddress("local:org.genivi.audiomanager.sourcestate:de.bmw.infotainment.broadcast.ta");
//	registerStub(theStub, capiAddress);
}

CAmCommonAPIWrapper::~CAmCommonAPIWrapper()
{
	mContext->unsubscribeForDispatchSources(mDispatchSourceListenerSubscription);
	mContext->unsubscribeForWatches(mWatchListenerSubscription);
	mContext->unsubscribeForTimeouts(mTimeoutSourceListenerSubscription);
//The following objects must be released in the given order.
	mFactory.reset();
	mContext.reset();

	mpSocketHandler = NULL;
	mWatchToCheck = NULL;
}

CAmCommonAPIWrapper* CAmCommonAPIWrapper::instantiateOnce(CAmSocketHandler* socketHandler)
{
	if(NULL==pSingleCommonAPIInstance)
	{
		if(NULL==socketHandler)
			throw std::runtime_error(std::string("Expected a valid socket handler. The socket handler pointer must not be NULL."));
		else
			pSingleCommonAPIInstance = new CAmCommonAPIWrapper(socketHandler);
	}
	else
		throw std::logic_error(std::string("The singleton instance has been already instantiated. This method should be called only once."));
	return pSingleCommonAPIInstance;
}

CAmCommonAPIWrapper* CAmCommonAPIWrapper::getInstance()
{
	assert(NULL!=pSingleCommonAPIInstance);
	return pSingleCommonAPIInstance;
}

std::shared_ptr<CommonAPI::Factory> CAmCommonAPIWrapper::factory() const
{
	return mFactory;
}


std::shared_ptr<CommonAPI::Runtime> CAmCommonAPIWrapper::runtime() const
{
	return  mFactory->getRuntime();
}

bool CAmCommonAPIWrapper::commonDispatchCallback(const sh_pollHandle_t handle, void *userData)
{
    (void) handle;
    (void) userData;

    std::list<DispatchSource*>::iterator iterator(mSourcesToDispatch.begin());
    for(;iterator!=mSourcesToDispatch.end();)
    {
        DispatchSource* source = *iterator;
        if (!source->dispatch()) {
            iterator=mSourcesToDispatch.erase(iterator);
        }
        else
            iterator++;
    }
    if (!mSourcesToDispatch.empty())
        return (true);

    return false;
}

bool CAmCommonAPIWrapper::commonCheckCallback(const sh_pollHandle_t, void *)
{
    std::vector<DispatchSource*> vecDispatch=mWatchToCheck->getDependentDispatchSources();
    mSourcesToDispatch.insert(mSourcesToDispatch.end(), vecDispatch.begin(), vecDispatch.end());

    return (mWatchToCheck || !mSourcesToDispatch.empty());
}

void CAmCommonAPIWrapper::commonFireCallback(const pollfd pollfd, const sh_pollHandle_t, void *)
{
    mWatchToCheck=NULL;
    try
    {
        mWatchToCheck=mMapWatches.at(pollfd.fd);
    }
    catch (const std::out_of_range& error) {
      logInfo(__PRETTY_FUNCTION__,error.what());
      return;
    }

    mWatchToCheck->dispatch(pollfd.events);
}

void CAmCommonAPIWrapper::commonPrepareCallback(const sh_pollHandle_t, void*)
{
    for (auto dispatchSourceIterator = mRegisteredDispatchSources.begin();
                            dispatchSourceIterator != mRegisteredDispatchSources.end();
                            dispatchSourceIterator++)
    {
        int64_t dispatchTimeout(TIMEOUT_INFINITE);
        if(dispatchSourceIterator->second->prepare(dispatchTimeout))
        {
            while (dispatchSourceIterator->second->dispatch());
        }
    }
}

void CAmCommonAPIWrapper::registerDispatchSource(DispatchSource* dispatchSource, const DispatchPriority dispatchPriority)
{
    mRegisteredDispatchSources.insert({dispatchPriority, dispatchSource});
}

void CAmCommonAPIWrapper::deregisterDispatchSource(DispatchSource* dispatchSource)
{
    for(auto dispatchSourceIterator = mRegisteredDispatchSources.begin();
            dispatchSourceIterator != mRegisteredDispatchSources.end();
            dispatchSourceIterator++) {

        if(dispatchSourceIterator->second == dispatchSource) {
            mRegisteredDispatchSources.erase(dispatchSourceIterator);
            break;
        }
    }
}

void CAmCommonAPIWrapper::deregisterWatch(Watch* watch)
{
    logInfo(__PRETTY_FUNCTION__);
    for(std::map<int,Watch*>::iterator iter(mMapWatches.begin());iter!=mMapWatches.end();iter++)
    {
        if (iter->second == watch)
        {
            mMapWatches.erase(iter);
            break;
        }
    }
}

void CAmCommonAPIWrapper::registerTimeout(Timeout* timeout, const DispatchPriority)
{
    logInfo(__PRETTY_FUNCTION__);
    timespec pollTimeout;
    int64_t localTimeout = timeout->getTimeoutInterval();

    pollTimeout.tv_sec = localTimeout / 1000;
    pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000;

    //prepare handle and callback. new is eval, but there is no other choice because we need the pointer!
    sh_timerHandle_t handle;
    timerHandles myHandle({handle,timeout});
    mpListTimerhandles.push_back(myHandle);

    //add the timer to the pollLoop
    mpSocketHandler->addTimer(pollTimeout, &pCommonTimerCallback, handle, timeout);

    return;
}

void CAmCommonAPIWrapper::deregisterTimeout(Timeout* timeout)
{
    logInfo(__PRETTY_FUNCTION__);
    for( std::vector<timerHandles>::iterator iter(mpListTimerhandles.begin());iter!=mpListTimerhandles.end();iter++)
    {
        if(iter->timeout==timeout)
        {
            mpSocketHandler->removeTimer(iter->handle);
        }
    }
}

void CAmCommonAPIWrapper::registerWatch(Watch* watch, const DispatchPriority)
{
    logInfo(__PRETTY_FUNCTION__);
    pollfd pollfd_ (watch->getAssociatedFileDescriptor());
    sh_pollHandle_t handle (0);

    am_Error_e error = mpSocketHandler->addFDPoll(pollfd_.fd, pollfd_.events, &pCommonPrepareCallback, &pCommonFireCallback, &pCommonCheckCallback, &pCommonDispatchCallback, watch, handle);

    //if everything is alright, add the watch and the handle to our map so we know this relationship
    if (error == !am_Error_e::E_OK || handle == 0)
        logError(__PRETTY_FUNCTION__,"entering watch failed");

    mMapWatches.insert(std::make_pair(pollfd_.fd,watch));
}

void CAmCommonAPIWrapper::commonTimerCallback(sh_timerHandle_t handle, void *)
{
    for( std::vector<timerHandles>::iterator iter(mpListTimerhandles.begin());iter!=mpListTimerhandles.end();iter++)
    {
        if(iter->handle==handle)
        {
            iter->timeout->dispatch();
        }
    }
}

}