diff options
author | Janne Partala <janne.partala@bitfactor.fi> | 2015-07-22 11:24:33 +0300 |
---|---|---|
committer | Alex Blasche <alexander.blasche@theqtcompany.com> | 2015-08-19 14:52:19 +0000 |
commit | 218422886b61a6127b191768440205eb910db975 (patch) | |
tree | dbbbf925995f6a4da225a8a751fbaaf044522e22 | |
parent | 45462071b577cf64a632ab7b433c0b5f2c60e5a9 (diff) |
Skeleton for modbus implementation
Barebones or empty classes added for foundation of modbus implementation.
Uses similar of architecture with CAN implementation.
Change-Id: I5cde9a04a040de1cf11dfd9f6a9c8bc9462a9299
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
-rw-r--r-- | examples/serialbus/modbus/main.cpp | 51 | ||||
-rw-r--r-- | examples/serialbus/modbus/mainwindow.cpp | 80 | ||||
-rw-r--r-- | examples/serialbus/modbus/mainwindow.h | 69 | ||||
-rw-r--r-- | examples/serialbus/modbus/mainwindow.ui | 64 | ||||
-rw-r--r-- | examples/serialbus/modbus/modbus.pro | 12 | ||||
-rw-r--r-- | examples/serialbus/serialbus.pro | 4 | ||||
-rw-r--r-- | src/plugins/modbus/libmodbus/libmodbus.pro | 16 | ||||
-rw-r--r-- | src/plugins/modbus/libmodbus/libmodbusbackend.cpp | 46 | ||||
-rw-r--r-- | src/plugins/modbus/libmodbus/libmodbusbackend.h | 55 | ||||
-rw-r--r-- | src/plugins/modbus/libmodbus/main.cpp | 65 | ||||
-rw-r--r-- | src/plugins/modbus/libmodbus/plugin.json | 3 | ||||
-rw-r--r-- | src/plugins/modbus/modbus.pro | 3 | ||||
-rw-r--r-- | src/plugins/plugins.pro | 1 | ||||
-rw-r--r-- | src/serialbus/qmodbus.cpp | 122 | ||||
-rw-r--r-- | src/serialbus/qmodbus.h | 65 | ||||
-rw-r--r-- | src/serialbus/qmodbusdevice.cpp | 46 | ||||
-rw-r--r-- | src/serialbus/qmodbusdevice.h | 54 | ||||
-rw-r--r-- | src/serialbus/qmodbusfactory.h | 59 | ||||
-rw-r--r-- | src/serialbus/serialbus.pro | 11 |
19 files changed, 823 insertions, 3 deletions
diff --git a/examples/serialbus/modbus/main.cpp b/examples/serialbus/modbus/main.cpp new file mode 100644 index 0000000..f23b403 --- /dev/null +++ b/examples/serialbus/modbus/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the QtSerialBus module. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" +#include <QApplication> + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/examples/serialbus/modbus/mainwindow.cpp b/examples/serialbus/modbus/mainwindow.cpp new file mode 100644 index 0000000..d258744 --- /dev/null +++ b/examples/serialbus/modbus/mainwindow.cpp @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the QtSerialBus module. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" +#include "ui_mainwindow.h" + +#include <QtSerialBus> + +#include <QtCore/qbytearray.h> +#include <QtCore/qdebug.h> + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + init(); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::init() +{ + QPointer<QModBus> modBus = QModBus::instance(); + + plugins = modBus->plugins(); + for (int i = 0; i < plugins.size(); i++) + ui->pluginBox->insertItem(i, plugins.at(i)); + +} + +void MainWindow::connectDevice(int pluginIndex) +{ + QPointer<QModBus> modBus = QModBus::instance(); + + modBusDevice = modBus->createDevice(plugins.at(pluginIndex), "unused"); + + if (modBusDevice.isNull()) + return; +} diff --git a/examples/serialbus/modbus/mainwindow.h b/examples/serialbus/modbus/mainwindow.h new file mode 100644 index 0000000..0ee38fc --- /dev/null +++ b/examples/serialbus/modbus/mainwindow.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the QtSerialBus module. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> +#include <QModBusDevice> +#include <QPointer> + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private: + void init(); + void connectDevice(int pluginIndex); + + QList<QByteArray> plugins; + QPointer<QModBusDevice> modBusDevice; + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/examples/serialbus/modbus/mainwindow.ui b/examples/serialbus/modbus/mainwindow.ui new file mode 100644 index 0000000..872c176 --- /dev/null +++ b/examples/serialbus/modbus/mainwindow.ui @@ -0,0 +1,64 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>MainWindow</class> + <widget class="QMainWindow" name="MainWindow"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>300</height> + </rect> + </property> + <property name="windowTitle"> + <string>MainWindow</string> + </property> + <widget class="QWidget" name="centralWidget"> + <widget class="QComboBox" name="pluginBox"> + <property name="geometry"> + <rect> + <x>10</x> + <y>20</y> + <width>85</width> + <height>27</height> + </rect> + </property> + </widget> + <widget class="QLabel" name="label"> + <property name="geometry"> + <rect> + <x>10</x> + <y>0</y> + <width>67</width> + <height>17</height> + </rect> + </property> + <property name="text"> + <string>Plugin</string> + </property> + </widget> + </widget> + <widget class="QMenuBar" name="menuBar"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>25</height> + </rect> + </property> + </widget> + <widget class="QToolBar" name="mainToolBar"> + <attribute name="toolBarArea"> + <enum>TopToolBarArea</enum> + </attribute> + <attribute name="toolBarBreak"> + <bool>false</bool> + </attribute> + </widget> + <widget class="QStatusBar" name="statusBar"/> + </widget> + <layoutdefault spacing="6" margin="11"/> + <resources/> + <connections/> +</ui> diff --git a/examples/serialbus/modbus/modbus.pro b/examples/serialbus/modbus/modbus.pro new file mode 100644 index 0000000..640ea85 --- /dev/null +++ b/examples/serialbus/modbus/modbus.pro @@ -0,0 +1,12 @@ +QT += core gui serialbus widgets + +TARGET = modbus +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp + +HEADERS += mainwindow.h + +FORMS += mainwindow.ui diff --git a/examples/serialbus/serialbus.pro b/examples/serialbus/serialbus.pro index ad9457a..7010f4f 100644 --- a/examples/serialbus/serialbus.pro +++ b/examples/serialbus/serialbus.pro @@ -3,3 +3,7 @@ TEMPLATE = subdirs qtHaveModule(widgets) { SUBDIRS += can } + +qtHaveModule(widgets) { + SUBDIRS += modbus +} diff --git a/src/plugins/modbus/libmodbus/libmodbus.pro b/src/plugins/modbus/libmodbus/libmodbus.pro new file mode 100644 index 0000000..a9e0a2c --- /dev/null +++ b/src/plugins/modbus/libmodbus/libmodbus.pro @@ -0,0 +1,16 @@ +QT += core-private serialbus + +TARGET = qtmodbus + +PLUGIN_TYPE = modbus +PLUGIN_EXTENDS = serialbus +PLUGIN_CLASS_NAME = LibModBusPlugin +load(qt_plugin) + +HEADERS += \ + libmodbusbackend.h + +SOURCES += main.cpp \ + libmodbusbackend.cpp + +OTHER_FILES = plugin.json diff --git a/src/plugins/modbus/libmodbus/libmodbusbackend.cpp b/src/plugins/modbus/libmodbus/libmodbusbackend.cpp new file mode 100644 index 0000000..2d787d2 --- /dev/null +++ b/src/plugins/modbus/libmodbus/libmodbusbackend.cpp @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtSerialBus module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "libmodbusbackend.h" + +QT_BEGIN_NAMESPACE + +LibModBusBackend::LibModBusBackend(const QString &name) +{ + Q_UNUSED(name); +} + +QT_END_NAMESPACE diff --git a/src/plugins/modbus/libmodbus/libmodbusbackend.h b/src/plugins/modbus/libmodbus/libmodbusbackend.h new file mode 100644 index 0000000..c8dc5ce --- /dev/null +++ b/src/plugins/modbus/libmodbus/libmodbusbackend.h @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtSerialBus module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef LIBMODBUSBACKEND_H +#define LIBMODBUSBACKEND_H + +#include <QtSerialBus/qmodbusdevice.h> + +#include <QtCore/qstring.h> + +QT_BEGIN_NAMESPACE + +class LibModBusBackend : public QModBusDevice +{ + Q_OBJECT +public: + explicit LibModBusBackend(const QString &name); +}; + +QT_END_NAMESPACE + +#endif // LIBMODBUSBACKEND_H diff --git a/src/plugins/modbus/libmodbus/main.cpp b/src/plugins/modbus/libmodbus/main.cpp new file mode 100644 index 0000000..66fb220 --- /dev/null +++ b/src/plugins/modbus/libmodbus/main.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtSerialBus module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "libmodbusbackend.h" + +#include <QtSerialBus/qmodbus.h> +#include <QtSerialBus/qmodbusdevice.h> +#include <QtSerialBus/qmodbusfactory.h> + +#include <QtCore/qfile.h> +#include <QtCore/qdebug.h> + +QT_BEGIN_NAMESPACE + +class ModBusPlugin : public QObject, public QModBusFactory +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QModBusFactory" FILE "plugin.json") + Q_INTERFACES(QModBusFactory) + + +public: + QModBusDevice *createDevice(const QString &interfaceName) const + { + QModBusDevice *device = new LibModBusBackend(interfaceName); + return device; + } +}; + +QT_END_NAMESPACE + +#include "main.moc" diff --git a/src/plugins/modbus/libmodbus/plugin.json b/src/plugins/modbus/libmodbus/plugin.json new file mode 100644 index 0000000..07d7cf8 --- /dev/null +++ b/src/plugins/modbus/libmodbus/plugin.json @@ -0,0 +1,3 @@ +{ + "Key": "libmodbus" +} diff --git a/src/plugins/modbus/modbus.pro b/src/plugins/modbus/modbus.pro new file mode 100644 index 0000000..cc78c8f --- /dev/null +++ b/src/plugins/modbus/modbus.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs + +SUBDIRS += libmodbus diff --git a/src/plugins/plugins.pro b/src/plugins/plugins.pro index 9d64723..456289e 100644 --- a/src/plugins/plugins.pro +++ b/src/plugins/plugins.pro @@ -1,2 +1,3 @@ TEMPLATE = subdirs + SUBDIRS += canbus diff --git a/src/serialbus/qmodbus.cpp b/src/serialbus/qmodbus.cpp new file mode 100644 index 0000000..ae8a18f --- /dev/null +++ b/src/serialbus/qmodbus.cpp @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtSerialBus module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qmodbus.h" +#include "qmodbusfactory.h" + +#include <QtCore/qpluginloader.h> +#include <QtCore/qglobalstatic.h> + +#include <private/qfactoryloader_p.h> +#include <private/qlibrary_p.h> + +#define QModBusFactory_iid "org.qt-project.Qt.QModBusFactory" + +QT_BEGIN_NAMESPACE + +struct QModBusPrivate +{ +public: + QModBusPrivate() : factory(Q_NULLPTR), index(-1) { } + + QJsonObject meta; + QModBusFactory *factory; + int index; +}; + +Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, qFactoryLoader, + (QModBusFactory_iid, QLatin1String("/modbus"))) + +typedef QHash<QByteArray, QModBusPrivate> QModBusPluginStore; +Q_GLOBAL_STATIC(QModBusPluginStore, qModBusPlugins) + +static QModBus *globalInstance = Q_NULLPTR; + +static void loadPlugins() +{ + const QList<QJsonObject> meta = qFactoryLoader()->metaData(); + for (int i = 0; i < meta.count(); i++) { + const QJsonObject obj = meta.at(i).value(QStringLiteral("MetaData")).toObject(); + if (obj.isEmpty()) + continue; + + QModBusPrivate d; + d.index = i; + d.meta = obj; + qModBusPlugins()->insert( + obj.value(QStringLiteral("Key")).toString().toLatin1(), d); + } +} + +QModBus *QModBus::instance() +{ + if (!globalInstance) + globalInstance = new QModBus(); + return globalInstance; +} + +QList<QByteArray> QModBus::plugins() const +{ + return qModBusPlugins()->keys(); +} + +QModBusDevice *QModBus::createDevice(const QByteArray &plugin, + const QString &interfaceName) const +{ + if (!qModBusPlugins()->contains(plugin)) + return Q_NULLPTR; + + QModBusPrivate d = qModBusPlugins()->value(plugin); + if (!d.factory) { + d.factory + = qobject_cast<QModBusFactory *>(qFactoryLoader->instance(d.index)); + if (!d.factory) + return Q_NULLPTR; + + qModBusPlugins()->insert(plugin, d); + } + if (!d.factory) + return Q_NULLPTR; + + return d.factory->createDevice(interfaceName);} + +QModBus::QModBus(QObject *parent) : + QObject(parent) +{ + loadPlugins(); +} + +QT_END_NAMESPACE diff --git a/src/serialbus/qmodbus.h b/src/serialbus/qmodbus.h new file mode 100644 index 0000000..74e3e02 --- /dev/null +++ b/src/serialbus/qmodbus.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtSerialBus module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMODBUS_H +#define QMODBUS_H + +#include <QtSerialBus/qserialbusglobal.h> +#include <QtSerialBus/qmodbusdevice.h> + +#include <QtCore/qobject.h> + +QT_BEGIN_NAMESPACE + +class Q_SERIALBUS_EXPORT QModBus : public QObject +{ + Q_OBJECT +public: + static QModBus *instance(); + QList<QByteArray> plugins() const; + + QModBusDevice *createDevice(const QByteArray &plugin, + const QString &interfaceName) const; + +private: + QModBus(QObject *parent = 0); + + Q_DISABLE_COPY(QModBus) +}; + +QT_END_NAMESPACE + +#endif // QMODBUS_H diff --git a/src/serialbus/qmodbusdevice.cpp b/src/serialbus/qmodbusdevice.cpp new file mode 100644 index 0000000..ff498af --- /dev/null +++ b/src/serialbus/qmodbusdevice.cpp @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtSerialBus module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qmodbusdevice.h" + +QT_BEGIN_NAMESPACE + +QModBusDevice::QModBusDevice(QObject *parent) : + QObject(parent) +{ +} + +QT_END_NAMESPACE diff --git a/src/serialbus/qmodbusdevice.h b/src/serialbus/qmodbusdevice.h new file mode 100644 index 0000000..e3bebea --- /dev/null +++ b/src/serialbus/qmodbusdevice.h @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtSerialBus module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMODBUSDEVICE_H +#define QMODBUSDEVICE_H + +#include <QtSerialBus/qserialbusglobal.h> + +#include <QtCore/qobject.h> + +QT_BEGIN_NAMESPACE + +class Q_SERIALBUS_EXPORT QModBusDevice : public QObject +{ + Q_OBJECT +public: + explicit QModBusDevice(QObject *parent = 0); +}; + +QT_END_NAMESPACE +#endif // QMODBUSDEVICE_H diff --git a/src/serialbus/qmodbusfactory.h b/src/serialbus/qmodbusfactory.h new file mode 100644 index 0000000..ff901f7 --- /dev/null +++ b/src/serialbus/qmodbusfactory.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtSerialBus module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMODBUSFACTORY_H +#define QMODBUSFACTORY_H + +#include <QtSerialBus/qserialbusglobal.h> +#include <QtSerialBus/qmodbusdevice.h> + +#include <QtCore/qstringlist.h> + +QT_BEGIN_NAMESPACE + +class Q_SERIALBUS_EXPORT QModBusFactory +{ +public: + virtual QModBusDevice *createDevice(const QString &interfaceName) const = 0; +protected: + virtual ~QModBusFactory() {} +}; + +Q_DECLARE_INTERFACE(QModBusFactory, "org.qt-project.Qt.QModBusFactory") + +QT_END_NAMESPACE + +#endif // QMODBUSFACTORY_H diff --git a/src/serialbus/serialbus.pro b/src/serialbus/serialbus.pro index e65eb62..6ac0473 100644 --- a/src/serialbus/serialbus.pro +++ b/src/serialbus/serialbus.pro @@ -14,7 +14,10 @@ PUBLIC_HEADERS += \ qcanbusframe.h \ qcanbus.h \ qserialbusglobal.h \ - qcanbusfactory.h + qcanbusfactory.h \ + qmodbus.h \ + qmodbusdevice.h \ + qmodbusfactory.h \ PRIVATE_HEADERS += \ qcanbusdevice_p.h \ @@ -23,6 +26,8 @@ SOURCES += \ qcanbusdevice.cpp \ qcanbus.cpp \ qcanbusfactory.cpp \ - qcanbusframe.cpp + qcanbusframe.cpp \ + qmodbus.cpp \ + qmodbusdevice.cpp -HEADERS += $$PUBLIC_HEADERS $$PRIVATE_HEADERS +HEADERS += $$PUBLIC_HEADERS $$PRIVATE_HEADERS \ |