aboutsummaryrefslogtreecommitdiffstats
path: root/src/ivicore/qiviservicemanager.cpp
blob: b5de5c25dd92c7f0377044829bbca92e9f911270 (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
/****************************************************************************
**
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtIvi module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/

#include "qiviservicemanager.h"

#include "qiviproxyserviceobject_p.h"
#include "qiviservicemanager_p.h"

#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QJsonObject>
#include <QLibrary>
#include <QModelIndex>
#include <QStringList>

#define QIVI_PLUGIN_DIRECTORY "qtivi"

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(qLcIviServiceManagement, "qt.ivi.servicemanagement");

namespace qtivi_helper {
#ifdef QT_DEBUG
    static const bool loadDebug = true;
#else
    static const bool loadDebug = false;
#endif
    static const QString interfacesLiteral = QStringLiteral("interfaces");
    static const QString fileNameLiteral = QStringLiteral("fileName");
    static const QString metaDataLiteral = QStringLiteral("MetaData");
    static const QString classNameLiteral = QStringLiteral("className");
    static const QString simulationLiteral = QStringLiteral("simulation");
    static const QString debugLiteral = QStringLiteral("debug");
#ifdef Q_OS_WIN
    static const QString debugSuffixLiteral = QStringLiteral("d");
#else
    static const QString debugSuffixLiteral = QStringLiteral("_debug");
#endif

    QString backendBaseName(const QString fileName)
    {
        if (fileName.isEmpty())
            return fileName;
        const QFileInfo fi(fileName);
        //remove the library suffix
        QString baseName = fileName;
        baseName.chop(fi.suffix().count() + 1);

        //remove the configuration suffix
        if (baseName.endsWith(debugSuffixLiteral))
            baseName.chop(debugSuffixLiteral.count());

        return baseName;
    }
}

using namespace qtivi_helper;

QIviServiceManagerPrivate::QIviServiceManagerPrivate(QIviServiceManager *parent) : QObject(parent), q_ptr(parent)
{
}

QIviServiceManagerPrivate *QIviServiceManagerPrivate::get(QIviServiceManager *serviceManager)
{
    Q_ASSERT(serviceManager);
    return serviceManager->d_ptr;
}

bool QIviServiceManagerPrivate::isSimulation(const QVariantMap &metaData)
{
    QString fileName = metaData[fileNameLiteral].toString();
    return fileName.contains(QLatin1String("_simulation")) ||
            fileName.contains(QLatin1String("_simulator")) ||
            metaData[simulationLiteral].toBool();
}

QIviProxyServiceObject *QIviServiceManagerPrivate::createServiceObject(struct Backend *backend) const
{
    if (!backend)
        return nullptr;

    if (!backend->proxyServiceObject) {
        QIviServiceInterface *backendInterface = loadServiceBackendInterface(backend);
        if (backendInterface)
            backend->proxyServiceObject = new QIviProxyServiceObject(backendInterface);
    }

    if (backend->proxyServiceObject) {
        QString fileName = backend->metaData[fileNameLiteral].toString();
        if (fileName.isEmpty())
            fileName = QStringLiteral("static plugin");
        qCDebug(qLcIviServiceManagement) << "Found: " << backend->proxyServiceObject << "from: " << fileName;
        return backend->proxyServiceObject;
    }

    return nullptr;
}

QList<QIviServiceObject *> QIviServiceManagerPrivate::findServiceByInterface(const QString &interface, QIviServiceManager::SearchFlags searchFlags) const
{
    QList<QIviServiceObject*> list;
    qCDebug(qLcIviServiceManagement) << "Searching for a backend for:" << interface << "SearchFlags:" << searchFlags;

    for (Backend *backend : m_backends) {

        if (backend->metaData[interfacesLiteral].toStringList().contains(interface)) {
            bool isSimulation = QIviServiceManagerPrivate::isSimulation(backend->metaData);
            if ((searchFlags & QIviServiceManager::IncludeSimulationBackends && isSimulation) ||
                (searchFlags & QIviServiceManager::IncludeProductionBackends && !isSimulation)) {
                QIviServiceObject *serviceObject = createServiceObject(backend);
                if (serviceObject)
                    list.append(serviceObject);
            }
        }
    }

    return list;
}

void QIviServiceManagerPrivate::searchPlugins()
{
    bool found = false;
    const auto pluginDirs = QCoreApplication::libraryPaths();
    for (const QString &pluginDir : pluginDirs) {

        QString path = pluginDir + QDir::separator() + QLatin1String(QIVI_PLUGIN_DIRECTORY);
        QDir dir(path);
        //Check whether the directory exists
        if (!dir.exists())
            continue;

        const QStringList plugins = QDir(path).entryList(QDir::Files);
        for (const QString &pluginFileName : plugins) {
            if (!QLibrary::isLibrary(pluginFileName))
                continue;

            const QFileInfo info(dir, pluginFileName);
            const QString absFile = info.canonicalFilePath();
            QPluginLoader loader(absFile);

            registerBackend(absFile, loader.metaData());
            found = true;
        }
    }
    const auto staticPlugins = QPluginLoader::staticPlugins();
    for (const QStaticPlugin &plugin : staticPlugins)
        registerStaticBackend(plugin);

    if (Q_UNLIKELY(!found))
        qWarning() << "No plugins found in search path: " << QCoreApplication::libraryPaths().join(QLatin1String(":"));
}

void QIviServiceManagerPrivate::registerBackend(const QString &fileName, const QJsonObject &metaData)
{
    QVariantMap backendMetaData = metaData.value(metaDataLiteral).toVariant().toMap();

    if (Q_UNLIKELY(backendMetaData[interfacesLiteral].isNull() ||
                   backendMetaData[interfacesLiteral].toList().isEmpty())) {
        qCWarning(qLcIviServiceManagement, "PluginManager - Malformed metaData in '%s'. MetaData must contain a list of interfaces", qPrintable(fileName));
        return;
    }

    backendMetaData.insert(fileNameLiteral, fileName);

    auto *backend = new Backend;
    backend->name =  metaData.value(classNameLiteral).toString();
    backend->debug = metaData.value(debugLiteral).toBool();
    backend->metaData = backendMetaData;
    backend->interface = nullptr;
    backend->interfaceObject = nullptr;
    backend->loader = nullptr;
    backend->proxyServiceObject = nullptr;
    addBackend(backend);
}

void QIviServiceManagerPrivate::registerStaticBackend(QStaticPlugin plugin)
{
    QVariantMap backendMetaData = plugin.metaData().value(metaDataLiteral).toVariant().toMap();
    const char* pluginName = plugin.instance()->metaObject()->className();

    if (Q_UNLIKELY(backendMetaData[interfacesLiteral].isNull() ||
                   backendMetaData[interfacesLiteral].toList().isEmpty())) {
        qCWarning(qLcIviServiceManagement, "PluginManager - Malformed metaData in static plugin '%s'. MetaData must contain a list of interfaces", pluginName);
        return;
    }

    QIviServiceInterface *backendInterface = qobject_cast<QIviServiceInterface*>(plugin.instance());
    if (Q_UNLIKELY(!backendInterface))
        qCWarning(qLcIviServiceManagement, "ServiceManager::serviceObjects - failed to cast to interface from '%s'", pluginName);

    //TODO check for other metaData like name etc.

    auto *backend = new Backend;
    backend->name = plugin.metaData().value(classNameLiteral).toString();
    backend->debug = plugin.metaData().value(debugLiteral).toBool();
    backend->metaData = backendMetaData;
    backend->interface = backendInterface;
    backend->interfaceObject = nullptr;
    backend->loader = nullptr;
    backend->proxyServiceObject = nullptr;
    addBackend(backend);
}

bool QIviServiceManagerPrivate::registerBackend(QObject *serviceBackendInterface, const QStringList &interfaces, QIviServiceManager::BackendType backendType)
{
    if (interfaces.isEmpty()) {
        return false;
    }

    // Verify that the object implements the ServiceBackendInterface
    QIviServiceInterface *interface = qobject_cast<QIviServiceInterface*>(serviceBackendInterface);
    if (!interface) {
        return false;
    }

    QVariantMap metaData = QVariantMap();

    metaData.insert(interfacesLiteral, interfaces);
    if (backendType == QIviServiceManager::SimulationBackend)
        metaData.insert(simulationLiteral, true);

    auto *backend = new Backend;
    backend->name = QString::fromLocal8Bit(serviceBackendInterface->metaObject()->className());
    backend->debug = false;
    backend->metaData = metaData;
    backend->interface = interface;
    backend->interfaceObject = serviceBackendInterface;
    backend->loader = nullptr;
    backend->proxyServiceObject = nullptr;

    addBackend(backend);
    return true;
}

void QIviServiceManagerPrivate::unloadAllBackends()
{
    Q_Q(QIviServiceManager);

    q->beginResetModel();
    QMutableListIterator<Backend*> i(m_backends);
    while (i.hasNext()) {
        Backend* backend = i.next();

        //If the Interface is from a Plugin, the Plugin owns it and it will be deleted when unloading.
        //Otherwise we own the Interface and delete the Pointer.
        if (backend->loader) {
            backend->loader->unload();
            delete backend->loader;
        } else if (backend->interfaceObject) {
            delete backend->interfaceObject;
        }
        delete backend->proxyServiceObject;

        i.remove();
        delete backend;
    }
    m_backends.clear();
    q->endResetModel();

    m_interfaceNames.clear();
}

void QIviServiceManagerPrivate::addBackend(Backend *backend)
{
    Q_Q(QIviServiceManager);
    //Check whether the same plugin is already in (maybe also in a different configuration)
    //The current configuration of QtIviCore decides which configuration takes precedence

    const QString newBackendFile = backend->metaData.value(fileNameLiteral).toString();
    const QString newBackendFileBase = qtivi_helper::backendBaseName(newBackendFile);
    const QSet<QString> newInterfaces = backend->metaData.value(interfacesLiteral).toStringList().toSet();

    bool addBackend = true;
    if (!newBackendFile.isEmpty()) {
        for (int i = 0; i < m_backends.count(); i++) {
            Backend *b = m_backends[i];
            const QSet<QString> interfaces = b->metaData.value(interfacesLiteral).toStringList().toSet();
            if (interfaces == newInterfaces && b->name == backend->name) {
                const QString fileName = b->metaData.value(fileNameLiteral).toString();
                if (fileName == newBackendFile) {
                    qCDebug(qLcIviServiceManagement, "SKIPPING %s: already in the list", qPrintable(newBackendFile));
                    return;
                }

                QString base = backendBaseName(fileName);
                //check whether the plugins name are the same after removing the debug and library suffixes
                if (newBackendFileBase == base) {
                    qCInfo(qLcIviServiceManagement, "Found the same plugin in two configurations. "
                                                    "Using the '%s' configuration: %s",
                                                    qtivi_helper::loadDebug ? "debug" : "release",
                                                    qPrintable(b->debug == qtivi_helper::loadDebug ? fileName : newBackendFile));
                    if (b->debug != qtivi_helper::loadDebug) {
                        qCDebug(qLcIviServiceManagement, "REPLACING %s with %s", qPrintable(fileName), qPrintable(newBackendFile));
                        addBackend = false;
                        m_backends[i] = backend;
                        emit q->dataChanged(q->index(i, 0), q->index(i, 0));
                        delete b;
                        break;
                    } else {
                        qCDebug(qLcIviServiceManagement, "SKIPPING %s: wrong configuration", qPrintable(newBackendFile));
                        return;
                    }
                }
            }
        }
    }
    if (addBackend) {
        qCDebug(qLcIviServiceManagement, "ADDING %s", qPrintable(newBackendFile.isEmpty() ? backend->name : newBackendFile));
        q->beginInsertRows(QModelIndex(), m_backends.count(), m_backends.count());
        m_backends.append(backend);
        q->endInsertRows();
    }

    for (const QString &interface : newInterfaces)
        m_interfaceNames.insert(interface);
}

namespace {
Q_NEVER_INLINE
static QIviServiceInterface *warn(const char *what, const QPluginLoader *loader)
{
    qWarning("ServiceManager::serviceObjects - failed to %s '%s'",
             what, qPrintable(loader->fileName()));
    delete loader;
    return nullptr;
}
} // unnamed namespace

QIviServiceInterface *QIviServiceManagerPrivate::loadServiceBackendInterface(struct Backend *backend) const
{
    if (backend->interface) {
        return backend->interface;
    }

    QPluginLoader *loader = new QPluginLoader(backend->metaData[fileNameLiteral].toString());
    QObject *plugin = loader->instance();
    if (Q_UNLIKELY(!plugin))
        return warn("load", loader);

    QIviServiceInterface *backendInterface = qobject_cast<QIviServiceInterface*>(plugin);
    if (Q_UNLIKELY(!backendInterface))
        return warn("cast to interface from", loader);

    backend->interface = backendInterface;
    backend->loader = loader;
    return backend->interface;
}

/*!
    \class QIviServiceManager
    \inmodule QtIviCore
    \brief QIviServiceManager provides the Backends to QIviAbstractFeature

    QIviServiceManager is the heart of QtIvi and provides you with an easy way to detect which
    backends and interfaces are available.

    By default QIviServiceManager reads the metaData of all plugins within the "qtivi" folder
    of your plugin path. The plugin itself will be loaded once it's explicitly requested by
    the developer by using findServiceByInterface().

    The manager can distinguish between \e Production and \e Simulation backends. For the recognition
    either the filename or the metaData is used.

    Simulation Backends can set the \e simulation key in their plugin metadata to \e true or use
    one of two supported plugin postfixes (_simulation, _simulator) in the plugin filename.

    The registerService() function can be used to add Backend classes without putting them into
    a plugin.

    The service manager is a process wide singleton and can be accessed through the \l instance method.

    For more detailed information on which plugins are recognized, enable the "qt.ivi.servicemanagement"
    logging category.

    See \l {Dynamic Backend System} for more information about how backend loading works.

    \note The QIviServiceManager will only accept plugins, which match the build configuration used
    for building qtivicore. This means qtivicore "release" build, doesn't accept plugins from a "debug" build.
*/

/*!
    \enum QIviServiceManager::Roles
    \value NameRole
          The name of the backend e.g. MediaPlugin
    \value ServiceObjectRole
          The actual QIviServiceObject, which can be used to connect a frontend API to this backend.
          \note When using this role in the data() function, the backend plugin will be loaded and instantiated.
    \value InterfacesRole
          A list of interefaces the backend implements.
*/

/*!
    \enum QIviServiceManager::SearchFlag

    \value IncludeProductionBackends
          Include production backends in the search result. \sa ProductionBackend
    \value IncludeSimulationBackends
          Include simulation backends in the search result. \sa SimulationBackend
    \value IncludeAll
          Include production and simulation backends in the search result
*/

/*!
    \enum QIviServiceManager::BackendType

    \value ProductionBackend
          A backend controlling a real automotive interface (e.g. a climate control connected over the CAN bus)
    \value SimulationBackend
          A backend used for development as it's only returning simulation values and won't be deployed to the final hardware
*/

QIviServiceManager::QIviServiceManager()
    : QAbstractListModel(nullptr)
    , d_ptr(new QIviServiceManagerPrivate(this))
{
    d_ptr->searchPlugins();
}

/*!
    Returns the global service manager instance.
*/
QIviServiceManager *QIviServiceManager::instance()
{
    static auto *instance = new QIviServiceManager();
    return instance;
}

/*!
    Returns a list of backends implementing the specified \a interface.

    The \a searchFlags argument can be used to control which type of backends are included in the search result.
*/
QList<QIviServiceObject *> QIviServiceManager::findServiceByInterface(const QString &interface, SearchFlags searchFlags)
{
    Q_D(QIviServiceManager);
    return d->findServiceByInterface(interface, searchFlags);
}

/*!
    Register a backend. The provided \a serviceBackendInterface must implement the
    QIviServiceInterface else the registration will fail. \a interfaces is a list
    with interfaces (at least one) supported by the backend. The \a backendType
    indicates the type of the backend and has influence on whether the backend is
    found by the auto discovery of the Feature.

    Returns true if the backend was successfully registered else false.

    \sa QIviServiceInterface
*/
bool QIviServiceManager::registerService(QObject *serviceBackendInterface, const QStringList &interfaces, BackendType backendType)
{
    Q_D(QIviServiceManager);
    return d->registerBackend(serviceBackendInterface, interfaces, backendType);
}

/*!
    \internal

    Unloads all currently loaded backends. Commonly only used for unit testing.
*/
void QIviServiceManager::unloadAllBackends()
{
    Q_D(QIviServiceManager);
    d->unloadAllBackends();
}

/*!
    Returns true if the specified \a interface has been registered.
*/
bool QIviServiceManager::hasInterface(const QString &interface) const
{
    Q_D(const QIviServiceManager);
    return d->m_interfaceNames.contains(interface);
}

/*!
    Returns the number of rows for the given \a parent. Typically \a parent is an empty model index.

    \sa QAbstractListModel::data()
*/
int QIviServiceManager::rowCount(const QModelIndex &parent) const
{
    Q_D(const QIviServiceManager);
    return parent.isValid() ? 0 : d->m_backends.count();
}

/*!
    Returns the data for \a index and \a role.

    \sa QAbstractListModel::data()
*/
QVariant QIviServiceManager::data(const QModelIndex &index, int role) const
{
    Q_D(const QIviServiceManager);

    if (!index.isValid())
        return QVariant();

    int row = index.row();

    if (row < 0 || row >= d->m_backends.count())
        return QVariant();

    Backend *backend = d->m_backends.at(row);

    switch (role) {
    case NameRole: return backend->name;
    case ServiceObjectRole: return QVariant::fromValue(d->createServiceObject(backend));
    case InterfacesRole: return backend->metaData[interfacesLiteral];
    }

    return QVariant();
}


/*!
    \reimp
*/
QHash<int, QByteArray> QIviServiceManager::roleNames() const
{
    static QHash<int, QByteArray> roles;
    if (roles.isEmpty()) {
        roles[NameRole] = "name";
        roles[ServiceObjectRole] = "serviceObject";
        roles[InterfacesRole] = "interfaces";
    }
    return roles;
}

QT_END_NAMESPACE