CANdevStudio
Development tool for CAN bus simulation
Loading...
Searching...
No Matches
candeviceqt.h
Go to the documentation of this file.
1#ifndef CANDEVICEQT_H_JYBV8GIQ
2#define CANDEVICEQT_H_JYBV8GIQ
3
5#include <QtSerialBus/QCanBus>
6#include <QtSerialBus/QCanBusDevice>
7#include <log.h>
8
10
11 CanDeviceQt() = default;
12
13 virtual void setParent(QObject* parent) override
14 {
15 _parent = parent;
16 }
17
18 virtual void setFramesWrittenCbk(const framesWritten_t& cb) override
19 {
20 if (_device) {
21 QObject::connect(_device, &QCanBusDevice::framesWritten, cb);
22 } else {
23 cds_error("Calling connect on null CAN device");
24 throw std::runtime_error("Calling connect on null CAN device");
25 }
26 }
27
28 virtual void setFramesReceivedCbk(const framesReceived_t& cb) override
29 {
30 if (_device) {
31 QObject::connect(_device, &QCanBusDevice::framesReceived, cb);
32 } else {
33 cds_error("Calling connect on null CAN device");
34 throw std::runtime_error("Calling connect on null CAN device");
35 }
36 }
37
38 virtual void setErrorOccurredCbk(const errorOccurred_t& cb) override
39 {
40 if (_device) {
41 QObject::connect(_device, &QCanBusDevice::errorOccurred, cb);
42 } else {
43 cds_error("Calling connect on null CAN device");
44 throw std::runtime_error("Calling connect on null CAN device");
45 }
46 }
47
48 virtual bool init(const QString& backend, const QString& iface) override
49 {
50 delete _device;
51 _device = QCanBus::instance()->createDevice(backend.toUtf8(), iface);
52
53 if (!_device) {
54 cds_error("Failed to create candevice");
55
56 return false;
57 }
58
59 if (_parent && _parent->thread()) {
60 _device->moveToThread(_parent->thread());
61 _device->setParent(_parent);
62 }
63
64 return true;
65 }
66
67 virtual bool writeFrame(const QCanBusFrame& frame) override
68 {
69 if (_device) {
70 return _device->writeFrame(frame);
71 } else {
72 cds_error("candevice is null. Call init first!");
73 throw std::runtime_error("candevice is null. Call init first!");
74 }
75
76 return false;
77 }
78
79 virtual bool connectDevice() override
80 {
81 if (_device) {
82 return _device->connectDevice();
83 } else {
84 cds_error("candevice is null. Call init first!");
85 throw std::runtime_error("candevice is null. Call init first!");
86 }
87 }
88 virtual qint64 framesAvailable() override
89 {
90 if (_device) {
91 return _device->framesAvailable();
92 } else {
93 cds_error("candevice is null. Call init first!");
94 throw std::runtime_error("candevice is null. Call init first!");
95 }
96 }
97
98 virtual QCanBusFrame readFrame() override
99 {
100 if (_device) {
101 return _device->readFrame();
102 } else {
103 cds_error("candevice is null. Call init first!");
104 throw std::runtime_error("candevice is null. Call init first!");
105 }
106 }
107
108 virtual void disconnectDevice() override
109 {
110 if (_device) {
111 return _device->disconnectDevice();
112 } else {
113 cds_error("candevice is null. Call init first!");
114 throw std::runtime_error("candevice is null. Call init first!");
115 }
116 }
117
118 virtual void clearCallbacks() override
119 {
120 if (_device) {
121 _device->disconnect();
122 } else {
123 cds_error("candevice is null. Call init first!");
124 throw std::runtime_error("candevice is null. Call init first!");
125 }
126 }
127
128 virtual void setConfigurationParameter(int key, const QVariant& value) override
129 {
130 if (_device) {
131 _device->setConfigurationParameter(key, value);
132 } else {
133 cds_error("candevice is null. Call init first!");
134 throw std::runtime_error("candevice is null. Call init first!");
135 }
136 }
137
138private:
139 QCanBusDevice* _device{ nullptr };
140 QObject* _parent{ nullptr };
141};
142
143#endif /* end of include guard: CANDEVICEQT_H_JYBV8GIQ */
#define cds_error(fmt,...)
Definition log.h:21
Definition candeviceinterface.h:8
std::function< void()> framesReceived_t
Definition candeviceinterface.h:12
std::function< void(int)> errorOccurred_t
Definition candeviceinterface.h:13
std::function< void(qint64)> framesWritten_t
Definition candeviceinterface.h:11
Definition candeviceqt.h:9
virtual QCanBusFrame readFrame() override
Definition candeviceqt.h:98
virtual void setParent(QObject *parent) override
Definition candeviceqt.h:13
virtual bool connectDevice() override
Definition candeviceqt.h:79
virtual void setErrorOccurredCbk(const errorOccurred_t &cb) override
Definition candeviceqt.h:38
virtual void disconnectDevice() override
Definition candeviceqt.h:108
virtual void setFramesWrittenCbk(const framesWritten_t &cb) override
Definition candeviceqt.h:18
virtual void setFramesReceivedCbk(const framesReceived_t &cb) override
Definition candeviceqt.h:28
virtual bool writeFrame(const QCanBusFrame &frame) override
Definition candeviceqt.h:67
virtual void clearCallbacks() override
Definition candeviceqt.h:118
virtual bool init(const QString &backend, const QString &iface) override
Definition candeviceqt.h:48
virtual void setConfigurationParameter(int key, const QVariant &value) override
Definition candeviceqt.h:128
virtual qint64 framesAvailable() override
Definition candeviceqt.h:88
CanDeviceQt()=default