FirmwarePluginManager.cc 2.96 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
Don Gagne's avatar
Don Gagne committed
9

Don Gagne's avatar
Don Gagne committed
10 11 12 13 14

/// @file
///     @author Don Gagne <don@thegagnes.com>

#include "FirmwarePluginManager.h"
15
#include "FirmwarePlugin.h"
Don Gagne's avatar
Don Gagne committed
16

17 18
FirmwarePluginManager::FirmwarePluginManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
Don Gagne's avatar
Don Gagne committed
19 20 21 22 23 24 25 26 27 28
    , _genericFirmwarePlugin(NULL)
{

}

FirmwarePluginManager::~FirmwarePluginManager()
{
    delete _genericFirmwarePlugin;
}

29
QList<MAV_AUTOPILOT> FirmwarePluginManager::supportedFirmwareTypes(void)
30
{
31
    if (_supportedFirmwareTypes.isEmpty()) {
32
        QList<FirmwarePluginFactory*> factoryList = FirmwarePluginFactoryRegister::instance()->pluginFactories();
33
        for (int i = 0; i < factoryList.count(); i++) {
34
            _supportedFirmwareTypes.append(factoryList[i]->supportedFirmwareTypes());
35
        }
36
        _supportedFirmwareTypes.append(MAV_AUTOPILOT_GENERIC);
37
    }
38
    return _supportedFirmwareTypes;
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
QList<MAV_TYPE> FirmwarePluginManager::supportedVehicleTypes(MAV_AUTOPILOT firmwareType)
{
    QList<MAV_TYPE> vehicleTypes;

    FirmwarePluginFactory* factory = _findPluginFactory(firmwareType);

    if (factory) {
        vehicleTypes = factory->supportedVehicleTypes();
    } else if (firmwareType == MAV_AUTOPILOT_GENERIC) {
        vehicleTypes << MAV_TYPE_FIXED_WING << MAV_TYPE_QUADROTOR << MAV_TYPE_VTOL_QUADROTOR << MAV_TYPE_GROUND_ROVER << MAV_TYPE_SUBMARINE;
    } else {
        qWarning() << "Request for unknown firmware plugin factory" << firmwareType;
    }

    return vehicleTypes;
}

FirmwarePlugin* FirmwarePluginManager::firmwarePluginForAutopilot(MAV_AUTOPILOT firmwareType, MAV_TYPE vehicleType)
{
    FirmwarePluginFactory*  factory = _findPluginFactory(firmwareType);
    FirmwarePlugin*         plugin = NULL;

    if (factory) {
        plugin = factory->firmwarePluginForAutopilot(firmwareType, vehicleType);
    } else if (firmwareType != MAV_AUTOPILOT_GENERIC) {
        qWarning() << "Request for unknown firmware plugin factory" << firmwareType;
    }

    if (!plugin) {
        // Default plugin fallback
        if (!_genericFirmwarePlugin) {
            _genericFirmwarePlugin = new FirmwarePlugin;
        }
        plugin = _genericFirmwarePlugin;
    }

    return plugin;
}

FirmwarePluginFactory* FirmwarePluginManager::_findPluginFactory(MAV_AUTOPILOT firmwareType)
Don Gagne's avatar
Don Gagne committed
81
{
82 83 84 85
    QList<FirmwarePluginFactory*> factoryList = FirmwarePluginFactoryRegister::instance()->pluginFactories();

    // Find the plugin which supports this vehicle
    for (int i=0; i<factoryList.count(); i++) {
86 87 88
        FirmwarePluginFactory* factory = factoryList[i];
        if (factory->supportedFirmwareTypes().contains(firmwareType)) {
            return factory;
Don Gagne's avatar
Don Gagne committed
89 90 91
        }
    }

92
    return NULL;
Don Gagne's avatar
Don Gagne committed
93
}