summaryrefslogtreecommitdiff
path: root/AudioManagerCore/src/CAmControlSender.cpp
blob: d80460deb612d5408c893b7ed8bcc3852d75de8b (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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/**
 * 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 Christian Linke, christian.linke@bmw.de BMW 2011,2012
 *
 * \file CAmControlSender.cpp
 * For further information see http://www.genivi.org/.
 *
 */

#include "CAmControlSender.h"
#include <cassert>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include "TAmPluginTemplate.h"
#include "CAmDltWrapper.h"

namespace am
{

#define REQUIRED_INTERFACE_VERSION_MAJOR 1  //!< major interface version. All versions smaller than this will be rejected
#define REQUIRED_INTERFACE_VERSION_MINOR 0 //!< minor interface version. All versions smaller than this will be rejected

CAmControlSender* CAmControlSender::mInstance=NULL;

CAmControlSender::CAmControlSender(std::string controlPluginFile,CAmSocketHandler* sockethandler) :
        receiverCallbackT(this, &CAmControlSender::receiverCallback),
        checkerCallbackT(this, &CAmControlSender::checkerCallback),
        dispatcherCallbackT(this, &CAmControlSender::dispatcherCallback),
        mPipe(),
        mlibHandle(NULL),
        mController(NULL),
        mSignal(0)
{
    assert(sockethandler);

    //Check if a folder is given, then select the first plugin
    struct stat buf;
    const char* conFile(controlPluginFile.c_str());
    stat(conFile, &buf);
    if (S_ISDIR(buf.st_mode))
    {
        std::string directoryName(controlPluginFile);
        logInfo("Searching for ControlPlugin in", directoryName);
        DIR *directory = opendir(directoryName.c_str());

        if (!directory)
        {
            logError("Error opening directory ", directoryName);
            throw std::runtime_error("Controller directory could not be openend");
        }

        // iterate content of directory
        struct dirent *itemInDirectory = 0;
        while ((itemInDirectory = readdir(directory)))
        {
            unsigned char entryType = itemInDirectory->d_type;
            std::string entryName = itemInDirectory->d_name;
            std::string fullName = directoryName + "/" + entryName;

            bool regularFile = (entryType == DT_REG || entryType == DT_LNK);
            bool sharedLibExtension = ("so" == entryName.substr(entryName.find_last_of(".") + 1));

            // Handle cases where readdir() could not determine the file type
	        if (entryType == DT_UNKNOWN) {
	            struct stat buf;

	            if (stat(fullName.c_str(), &buf)) {
	                logInfo(__PRETTY_FUNCTION__,"Failed to stat file: ", entryName, errno);
	                continue;
	            }

	            regularFile = S_ISREG(buf.st_mode);
	        }

            if (regularFile && sharedLibExtension)
            {
                controlPluginFile=directoryName + "/" + entryName;
				logInfo("Found ControlPlugin:", controlPluginFile);
				break;
            }
        }
        closedir(directory);
	
    }
    
    std::ifstream isfile(controlPluginFile.c_str());
    if (!isfile)
    {
        logError("ControlSender::ControlSender: Controller plugin not found:", controlPluginFile);
        throw std::runtime_error("Could not find controller plugin!");
    }
    else if (!controlPluginFile.empty())
    {
        mInstance=this;
        IAmControlSend* (*createFunc)();
        createFunc = getCreateFunction<IAmControlSend*()>(controlPluginFile, mlibHandle);
        assert(createFunc!=NULL);
        mController = createFunc();
        mControlPluginFile = controlPluginFile;
        //check libversion
        std::string version, cVersion(ControlVersion);
        mController->getInterfaceVersion(version);
        uint16_t minorVersion, majorVersion, cMinorVersion, cMajorVersion;
        std::istringstream(version.substr(0, 1)) >> majorVersion;
        std::istringstream(version.substr(2, 1)) >> minorVersion;
        std::istringstream(cVersion.substr(0, 1)) >> cMajorVersion;
        std::istringstream(cVersion.substr(2, 1)) >> cMinorVersion;
        
        

        if (majorVersion < cMajorVersion || ((majorVersion == cMajorVersion) && (minorVersion < cMinorVersion)))
        {
            logError("ControlSender::ControlSender: Interface Version of Controller too old, required version:",ControlVersion," Controller Version:",version,"exiting now");
            throw std::runtime_error("Interface Version of Controller too old");
        }
    }
    else
    {
        logError("ControlSender::ControlSender: No controller loaded !");
    }

    //here we need a pipe to be able to call the rundown function out of the mainloop
    if (pipe(mPipe) == -1)
    {
        logError("CAmControlSender could not create pipe!");
    }

    //add the pipe to the poll - nothing needs to be proccessed here we just need the pipe to trigger the ppoll
    short event = 0;
    sh_pollHandle_t handle;
    event |= POLLIN;
    sockethandler->addFDPoll(mPipe[0], event, NULL, &receiverCallbackT, &checkerCallbackT, &dispatcherCallbackT, NULL, handle);
}

CAmControlSender::~CAmControlSender()
{
    close(mPipe[0]);
    close(mPipe[1]);

    if (mlibHandle)
    {
        void (*destroyFunc)(IAmControlSend*);
        destroyFunc = getDestroyFunction<void(IAmControlSend*)>(mControlPluginFile, mlibHandle);
        if (destroyFunc)
        {
            destroyFunc(mController);
        }
        else
        {
            logError("CAmControlSender Dtor: destroyFunc is invalid or not found");
        }
        dlclose(mlibHandle);
    }
}

am_Error_e CAmControlSender::hookUserConnectionRequest(const am_sourceID_t sourceID, const am_sinkID_t sinkID, am_mainConnectionID_t & mainConnectionID)
{
    assert(mController);
    return (mController->hookUserConnectionRequest(sourceID, sinkID, mainConnectionID));
}

am_Error_e CAmControlSender::hookUserDisconnectionRequest(const am_mainConnectionID_t connectionID)
{
    assert(mController);
    return (mController->hookUserDisconnectionRequest(connectionID));
}

am_Error_e CAmControlSender::hookUserSetMainSinkSoundProperty(const am_sinkID_t sinkID, const am_MainSoundProperty_s & soundProperty)
{
    assert(mController);
    return (mController->hookUserSetMainSinkSoundProperty(sinkID, soundProperty));
}

am_Error_e CAmControlSender::hookUserSetMainSourceSoundProperty(const am_sourceID_t sourceID, const am_MainSoundProperty_s & soundProperty)
{
    assert(mController);
    return (mController->hookUserSetMainSourceSoundProperty(sourceID, soundProperty));
}

am_Error_e CAmControlSender::hookUserSetSystemProperty(const am_SystemProperty_s & property)
{
    assert(mController);
    return (mController->hookUserSetSystemProperty(property));
}

am_Error_e CAmControlSender::hookUserVolumeChange(const am_sinkID_t sinkID, const am_mainVolume_t newVolume)
{
    assert(mController);
    return (mController->hookUserVolumeChange(sinkID, newVolume));
}

am_Error_e CAmControlSender::hookUserVolumeStep(const am_sinkID_t sinkID, const int16_t increment)
{
    assert(mController);
    return (mController->hookUserVolumeStep(sinkID, increment));
}

am_Error_e CAmControlSender::hookUserSetSinkMuteState(const am_sinkID_t sinkID, const am_MuteState_e muteState)
{
    assert(mController);
    return (mController->hookUserSetSinkMuteState(sinkID, muteState));
}

am_Error_e CAmControlSender::hookSystemRegisterDomain(const am_Domain_s & domainData, am_domainID_t & domainID)
{
    assert(mController);
    return (mController->hookSystemRegisterDomain(domainData, domainID));
}

am_Error_e CAmControlSender::hookSystemDeregisterDomain(const am_domainID_t domainID)
{
    assert(mController);
    return (mController->hookSystemDeregisterDomain(domainID));
}

void CAmControlSender::hookSystemDomainRegistrationComplete(const am_domainID_t domainID)
{
    assert(mController);
    return (mController->hookSystemDomainRegistrationComplete(domainID));
}

am_Error_e CAmControlSender::hookSystemRegisterSink(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
{
    assert(mController);
    return (mController->hookSystemRegisterSink(sinkData, sinkID));
}

am_Error_e CAmControlSender::hookSystemDeregisterSink(const am_sinkID_t sinkID)
{
    assert(mController);
    return (mController->hookSystemDeregisterSink(sinkID));
}

am_Error_e CAmControlSender::hookSystemRegisterSource(const am_Source_s & sourceData, am_sourceID_t & sourceID)
{
    assert(mController);
    return (mController->hookSystemRegisterSource(sourceData, sourceID));
}

am_Error_e CAmControlSender::hookSystemDeregisterSource(const am_sourceID_t sourceID)
{
    assert(mController);
    return (mController->hookSystemDeregisterSource(sourceID));
}

am_Error_e CAmControlSender::hookSystemRegisterGateway(const am_Gateway_s & gatewayData, am_gatewayID_t & gatewayID)
{
    assert(mController);
    return (mController->hookSystemRegisterGateway(gatewayData, gatewayID));
}

am_Error_e CAmControlSender::hookSystemRegisterConverter(const am_Converter_s& converterData, am_converterID_t& converterID)
{
    assert(mController);
    return (mController->hookSystemRegisterConverter(converterData, converterID));
}

am_Error_e CAmControlSender::hookSystemDeregisterGateway(const am_gatewayID_t gatewayID)
{
    assert(mController);
    return (mController->hookSystemDeregisterGateway(gatewayID));
}

am_Error_e CAmControlSender::hookSystemDeregisterConverter(const am_converterID_t converterID)
{
    assert(mController);
    return (mController->hookSystemDeregisterConverter(converterID));
}

am_Error_e CAmControlSender::hookSystemRegisterCrossfader(const am_Crossfader_s & crossfaderData, am_crossfaderID_t & crossfaderID)
{
    assert(mController);
    return (mController->hookSystemRegisterCrossfader(crossfaderData, crossfaderID));
}

am_Error_e CAmControlSender::hookSystemDeregisterCrossfader(const am_crossfaderID_t crossfaderID)
{
    assert(mController);
    return (mController->hookSystemDeregisterCrossfader(crossfaderID));
}

void CAmControlSender::hookSystemSinkVolumeTick(const am_Handle_s handle, const am_sinkID_t sinkID, const am_volume_t volume)
{
    assert(mController);
    mController->hookSystemSinkVolumeTick(handle, sinkID, volume);
}

void CAmControlSender::hookSystemSourceVolumeTick(const am_Handle_s handle, const am_sourceID_t sourceID, const am_volume_t volume)
{
    assert(mController);
    mController->hookSystemSourceVolumeTick(handle, sourceID, volume);
}

void CAmControlSender::hookSystemInterruptStateChange(const am_sourceID_t sourceID, const am_InterruptState_e interruptState)
{
    assert(mController);
    mController->hookSystemInterruptStateChange(sourceID, interruptState);
}

void CAmControlSender::hookSystemSinkAvailablityStateChange(const am_sinkID_t sinkID, const am_Availability_s & availability)
{
    assert(mController);
    mController->hookSystemSinkAvailablityStateChange(sinkID, availability);
}

void CAmControlSender::hookSystemSourceAvailablityStateChange(const am_sourceID_t sourceID, const am_Availability_s & availability)
{
    assert(mController);
    mController->hookSystemSourceAvailablityStateChange(sourceID, availability);
}

void CAmControlSender::hookSystemDomainStateChange(const am_domainID_t domainID, const am_DomainState_e state)
{
    assert(mController);
    mController->hookSystemDomainStateChange(domainID, state);
}

void CAmControlSender::hookSystemReceiveEarlyData(const std::vector<am_EarlyData_s> & data)
{
    assert(mController);
    mController->hookSystemReceiveEarlyData(data);
}

void CAmControlSender::hookSystemSpeedChange(const am_speed_t speed)
{
    assert(mController);
    mController->hookSystemSpeedChange(speed);
}

void CAmControlSender::hookSystemTimingInformationChanged(const am_mainConnectionID_t mainConnectionID, const am_timeSync_t time)
{
    assert(mController);
    mController->hookSystemTimingInformationChanged(mainConnectionID, time);
}

void CAmControlSender::cbAckConnect(const am_Handle_s handle, const am_Error_e errorID)
{
    assert(mController);
    mController->cbAckConnect(handle, errorID);
}

void CAmControlSender::cbAckDisconnect(const am_Handle_s handle, const am_Error_e errorID)
{
    assert(mController);
    mController->cbAckDisconnect(handle, errorID);
}

void CAmControlSender::cbAckCrossFade(const am_Handle_s handle, const am_HotSink_e hostsink, const am_Error_e error)
{
    assert(mController);
    mController->cbAckCrossFade(handle, hostsink, error);
}

void CAmControlSender::cbAckSetSinkVolumeChange(const am_Handle_s handle, const am_volume_t volume, const am_Error_e error)
{
    assert(mController);
    mController->cbAckSetSinkVolumeChange(handle, volume, error);
}

void CAmControlSender::cbAckSetSourceVolumeChange(const am_Handle_s handle, const am_volume_t volume, const am_Error_e error)
{
    assert(mController);
    mController->cbAckSetSourceVolumeChange(handle, volume, error);
}

void CAmControlSender::cbAckSetSourceState(const am_Handle_s handle, const am_Error_e error)
{
    assert(mController);
    mController->cbAckSetSourceState(handle, error);
}

void CAmControlSender::cbAckSetSourceSoundProperty(const am_Handle_s handle, const am_Error_e error)
{
    assert(mController);
    mController->cbAckSetSourceSoundProperty(handle, error);
}

am_Error_e CAmControlSender::startupController(IAmControlReceive *controlreceiveinterface)
{
    if (!mController)
    {
        logError("ControlSender::startupController: no Controller to startup!");
        throw std::runtime_error("ControlSender::startupController: no Controller to startup! Exiting now ...");
        return (E_NON_EXISTENT);
    }
    return (mController->startupController(controlreceiveinterface));
}

void CAmControlSender::cbAckSetSinkSoundProperty(const am_Handle_s handle, const am_Error_e error)
{
    assert(mController);
    mController->cbAckSetSinkSoundProperty(handle, error);
}

void CAmControlSender::cbAckSetSinkSoundProperties(const am_Handle_s handle, const am_Error_e error)
{
    assert(mController);
    mController->cbAckSetSinkSoundProperties(handle, error);
}

void CAmControlSender::cbAckSetSourceSoundProperties(const am_Handle_s handle, const am_Error_e error)
{
    assert(mController);
    mController->cbAckSetSourceSoundProperties(handle, error);
}

void CAmControlSender::setControllerReady()
{
    assert(mController);
    mController->setControllerReady();
}

void CAmControlSender::setControllerRundown(const int16_t signal)
{
    assert(mController);
    logInfo("CAmControlSender::setControllerRundown received, signal=",signal);
    mController->setControllerRundown(signal);
}

am_Error_e am::CAmControlSender::getConnectionFormatChoice(const am_sourceID_t sourceID, const am_sinkID_t sinkID, const am_Route_s listRoute, const std::vector<am_CustomConnectionFormat_t> listPossibleConnectionFormats, std::vector<am_CustomConnectionFormat_t> & listPrioConnectionFormats)
{
    assert(mController);
    return (mController->getConnectionFormatChoice(sourceID, sinkID, listRoute, listPossibleConnectionFormats, listPrioConnectionFormats));
}

void CAmControlSender::getInterfaceVersion(std::string & version) const
{
    version = ControlVersion;
}

void CAmControlSender::confirmCommandReady(const am_Error_e error)
{
    assert(mController);
    mController->confirmCommandReady(error);
}

void CAmControlSender::confirmRoutingReady(const am_Error_e error)
{
    assert(mController);
    mController->confirmRoutingReady(error);
}

void CAmControlSender::confirmCommandRundown(const am_Error_e error)
{
    assert(mController);
    mController->confirmCommandRundown(error);
}

void CAmControlSender::confirmRoutingRundown(const am_Error_e error)
{
    assert(mController);
    mController->confirmRoutingRundown(error);
}

am_Error_e CAmControlSender::hookSystemUpdateSink(const am_sinkID_t sinkID, const am_sinkClass_t sinkClassID, const std::vector<am_SoundProperty_s>& listSoundProperties, const std::vector<am_CustomConnectionFormat_t>& listConnectionFormats, const std::vector<am_MainSoundProperty_s>& listMainSoundProperties)
{
    assert(mController);
    return (mController->hookSystemUpdateSink(sinkID,sinkClassID,listSoundProperties,listConnectionFormats,listMainSoundProperties));
}

am_Error_e CAmControlSender::hookSystemUpdateSource(const am_sourceID_t sourceID, const am_sourceClass_t sourceClassID, const std::vector<am_SoundProperty_s>& listSoundProperties, const std::vector<am_CustomConnectionFormat_t>& listConnectionFormats, const std::vector<am_MainSoundProperty_s>& listMainSoundProperties)
{
    assert(mController);
    return (mController->hookSystemUpdateSource(sourceID,sourceClassID,listSoundProperties,listConnectionFormats,listMainSoundProperties));
}

am_Error_e CAmControlSender::hookSystemUpdateGateway(const am_gatewayID_t gatewayID, const std::vector<am_CustomConnectionFormat_t>& listSourceConnectionFormats, const std::vector<am_CustomConnectionFormat_t>& listSinkConnectionFromats, const std::vector<bool>& convertionMatrix)
{
    assert(mController);
    return (mController->hookSystemUpdateGateway(gatewayID,listSourceConnectionFormats,listSinkConnectionFromats,convertionMatrix));
}

am_Error_e CAmControlSender::hookSystemUpdateConverter(const am_converterID_t converterID, const std::vector<am_CustomConnectionFormat_t>& listSourceConnectionFormats, const std::vector<am_CustomConnectionFormat_t>& listSinkConnectionFromats, const std::vector<bool>& convertionMatrix)
{
    assert(mController);
    return (mController->hookSystemUpdateConverter(converterID,listSourceConnectionFormats,listSinkConnectionFromats,convertionMatrix));
}

void CAmControlSender::cbAckSetVolume(const am_Handle_s handle, const std::vector<am_Volumes_s>& listVolumes, const am_Error_e error)
{
    assert(mController);
    mController->cbAckSetVolumes(handle,listVolumes,error);
}

void CAmControlSender::cbAckSetSinkNotificationConfiguration(const am_Handle_s handle, const am_Error_e error)
{
    assert(mController);
    mController->cbAckSetSinkNotificationConfiguration(handle,error);
}

void CAmControlSender::cbAckSetSourceNotificationConfiguration(const am_Handle_s handle, const am_Error_e error)
{
    assert(mController);
    mController->cbAckSetSourceNotificationConfiguration(handle,error);
}

void CAmControlSender::hookSinkNotificationDataChanged(const am_sinkID_t sinkID, const am_NotificationPayload_s& payload)
{
    assert(mController);
    mController->hookSinkNotificationDataChanged(sinkID,payload);
}

void CAmControlSender::hookSourceNotificationDataChanged(const am_sourceID_t sourceID, const am_NotificationPayload_s& payload)
{
    assert(mController);
    mController->hookSourceNotificationDataChanged(sourceID,payload);
}

am_Error_e CAmControlSender::hookUserSetMainSinkNotificationConfiguration(const am_sinkID_t sinkID, const am_NotificationConfiguration_s& notificationConfiguration)
{
    assert(mController);
    return (mController->hookUserSetMainSinkNotificationConfiguration(sinkID,notificationConfiguration));
}

am_Error_e CAmControlSender::hookUserSetMainSourceNotificationConfiguration(const am_sourceID_t sourceID, const am_NotificationConfiguration_s& notificationConfiguration)
{
    assert(mController);
    return (mController->hookUserSetMainSourceNotificationConfiguration(sourceID,notificationConfiguration));
}

void CAmControlSender::receiverCallback(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
{
   (void) handle;
   (void) userData;
   //get the signal number from the socket
   ssize_t result = read(pollfd.fd, &mSignal, sizeof(mSignal));
}

bool CAmControlSender::checkerCallback(const sh_pollHandle_t handle, void* userData)
{
   (void) handle;
   (void) userData;
   return (true);
}

void CAmControlSender::hookSystemSingleTimingInformationChanged(const am_connectionID_t connectionID, const am_timeSync_t time)
{
    assert(mController);
    mController->hookSystemSingleTimingInformationChanged(connectionID,time);
}

/**for testing only contructor - do not use !
 *
 */
CAmControlSender::CAmControlSender() :
    receiverCallbackT(this, &CAmControlSender::receiverCallback),
    checkerCallbackT(this, &CAmControlSender::checkerCallback),
    dispatcherCallbackT(this, &CAmControlSender::dispatcherCallback),
    mlibHandle(NULL),
    mController(NULL),
    mSignal(0)
{
    logInfo("CAmControlSender was loaded in test mode!");
    std::memset(mPipe, -1, sizeof(mPipe));
}

bool CAmControlSender::dispatcherCallback(const sh_pollHandle_t handle, void* userData)
{
   (void)handle;
   (void)userData;
   setControllerRundown(mSignal);
   return (false);
}

}