forked from collin80/SavvyCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanconnection.h
326 lines (272 loc) · 10.7 KB
/
canconnection.h
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
#ifndef CANCONNECTION_H
#define CANCONNECTION_H
#include <Qt>
#include <QObject>
#include "utils/lfqueue.h"
#include "can_structs.h"
#include "canbus.h"
#include "canconconst.h"
struct BusData;
class CANConnection : public QObject
{
Q_OBJECT
protected:
/**
* @brief CANConnection constructor
* @param pPort: string containing port name
* @param pDriver: string containing driver name - Really only used for SerialBus connections
* @param pType: the type of connection @ref CANCon::type
* @param pSerialSpeed: for devices with variable serial speed this is that speed.
* @param pBusSpeed: set an initial speed when opening this connection
* @param pNumBuses: the number of buses the device has
* @param pQueueLen: the length of the lock free queue to use
* @param pUseThread: if set to true, object will be execute in a dedicated thread
*/
CANConnection(QString pPort,
QString pDriver,
CANCon::type pType,
int pSerialSpeed,
int pBusSpeed,
int pNumBuses,
int pQueueLen,
bool pUseThread);
public:
/**
* @brief CANConnection destructor
*/
virtual ~CANConnection();
/**
* @brief getNumBuses
* @return returns the number of buses of the device
*/
int getNumBuses() const;
/**
* @brief getPort
* @return returns the port name of the device
*/
QString getPort();
/**
* @brief getDriver
* @return returns the name of the driver used for this device
*/
QString getDriver();
/**
* @brief getQueue
* @return the lock free queue of the device
*/
LFQueue<CANFrame>& getQueue();
/**
* @brief getType
* @return the @ref CANCon::type of the device
*/
CANCon::type getType();
/**
* @brief getStatus
* @return the @ref CANCon::status of the device (either connected or not)
*/
CANCon::status getStatus();
/**
* @brief setConsoleOutput
* @param state - set whether to send debugging info to the console or not
*/
void setConsoleOutput(bool state);
signals:
/*not implemented yet */
void error(const QString &);
void deviceInfo(int, int); //First param = driver version (or version of whatever you want), second param a status byte
//bus number, bus speed, status (bit 0 = enabled, 1 = single wire, 2 = listen only)
//3 = Use value stored for enabled, 4 = use value passed for single wire, 5 = use value passed for listen only
//6 = use value passed for speed. This allows bus status to be updated but set that some things aren't really
//being passed. Just set for things that really are being updated.
void busStatus(int, int, int);
/**
* @brief event sent when a frame matching a filter is received
*/
void targettedFrameReceived(CANFrame frame);
/**
* @brief event emitted when the CANCon::status of the connection changes (connected->not_connected or the other way round)
* @param pStatus: the new status of the device
*/
void status(CANConStatus pStatus);
/**
* @brief Event sent when device has done something worthy of debugging output.
* @param debugString: String based output to show for debugging purposes
*/
void debugOutput(QString debugString);
public slots:
/**
* @brief start the device. This calls piStarted
* @note starts the working thread if required (piStarted is called in the working thread context)
*/
void start();
/**
* @brief stop the device. This calls piStop
* @note if a working thread is used, piStop is called before exiting the working thread
*/
void stop();
/**
* @brief setBusSettings
* @param pBusIdx: the index of the bus for which settings have to be set
* @param pBus: the settings to set
* @note this calls piSetBusSettings in the working thread context (if one has been started)
*/
void setBusSettings(int pBusIdx, CANBus pBus);
/**
* @brief getBusSettings
* @param pBusIdx: the index of the bus for which settings have to be retrieved
* @param pBus: the CANBus struct to fill with information
* @return true if operation succeeds, false if pBusIdx is invalid or bus has not been configured yet
* @note this calls piGetBusSettings in the working thread context (if one has been started)
*/
bool getBusSettings(int pBusIdx, CANBus& pBus);
/**
* @brief suspends/restarts data capture
* @param pSuspend: suspends capture if true else restarts it
* @note this calls piSuspend (in the working thread context if one has been started)
* @note the caller shall not access the queue when capture is suspended,
* @note the callee is expected to flush the queue
*/
void suspend(bool pSuspend);
/**
* @brief provides device with the frame to send
* @param pFrame: the frame to send
* @return false if parameter is invalid (bus id for instance)
* @note this calls piSendFrame (in the working thread context if one has been started)
*/
bool sendFrame(const CANFrame& pFrame);
/**
* @brief provides device with a list of frames to send
* @param pFrame: the list of frames to send
* @return false if parameter is invalid (bus id for instance)
* @note this calls piSendFrameBatch (in the working thread context if one has been started)
*/
bool sendFrames(const QList<CANFrame>& pFrames);
/**
* @brief Add a new filter for the targetted frames. If a frame matches it will immediately be sent via the targettedFrameReceived signal
* @param pBusId - Which bus to bond to. -1 for any, otherwise a bitfield of buses (but 0 = first bus, etc)
* @param ID - 11 or 29 bit ID to match against
* @param mask - 11 or 29 bit mask used for filter
* @param receiver - Pointer to a QObject that wants to receive notification when filter is matched
* @return true if filter was able to be added, false otherwise.
*/
bool addTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, QObject *receiver);
/**
* @brief Try to find a matching filter in the list and remove it, no longer targetting those frames
* @param pBusId - Which bus to bond to. Doesn't have to match the call to addTargettedFrame exactly. You could disconnect just one bus for instance.
* @param ID - 11 or 29 bit ID to match against
* @param mask - 11 or 29 bit mask used for filter
* @param receiver - Pointer to a QObject that wants to receive notification when filter is matched
* @return true if filter was found and deleted, false otherwise.
*/
bool removeTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, QObject *receiver);
/**
* @brief Removes all registered filters for the passed receiver
* @param receiver - Pointer to a QObject that registered one or more filters
* @return true if filter(s) was/were found and deleted, false otherwise.
*/
bool removeAllTargettedFrames(QObject *receiver);
void debugInput(QByteArray bytes);
protected:
int mNumBuses; //protected to allow connected device to figure out how many buses are available
QVector<BusData> mBusData;
bool mConsoleOutput; //send debugging info to the console?
int mSerialSpeed;
//determine if the passed frame is part of a filter or not.
void checkTargettedFrame(CANFrame &frame);
/**
* @brief setStatus
* @param pStatus: the status to set
*/
void setStatus(CANCon::status pStatus);
/**
* @brief isConfigured
* @param pBusId
* @return true if bus is configured
*/
bool isConfigured(int pBusId);
/**
* @brief setConfigured
* @param pBusId
* @param pConfigured
* @note it is not necessary to call this function to set pBusId configured, it is enough to call @ref setBusConfig
*/
void setConfigured(int pBusId, bool pConfigured);
/**
* @brief getBusConfig
* @param pBusId
* @param pBus
* @return true if operation succeeds, false if pBusIdx is invalid or bus has not been configured yet
*/
bool getBusConfig(int pBusId, CANBus& pBus);
/**
* @brief setBusConfig
* @param pBusId: the index of the bus for which settings have to be set
* @param pBus: the settings to set
*/
void setBusConfig(int pBusId, CANBus& pBus);
/**
* @brief isCapSuspended
* @return true if the capture is suspended
*/
bool isCapSuspended();
/**
* @brief setCapSuspended
* @param pIsSuspended
*/
void setCapSuspended(bool pIsSuspended);
protected:
bool useSystemTime;
/**************************************************************/
/*********** protected interface to implement *******/
/**************************************************************/
/**
* @brief starts the device
*/
virtual void piStarted() = 0;
/**
* @brief stops the device
*/
virtual void piStop() = 0;
/**
* @brief piSetBusSettings
* @param pBusIdx: the index of the bus for which settings have to be set
* @param pBus: the settings to set
*/
virtual void piSetBusSettings(int pBusIdx, CANBus pBus) = 0;
/**
* @brief piGetBusSettings
* @param pBusIdx: the index of the bus for which settings have to be retrieved
* @param pBus: the CANBus struct to fill with information
* @return true if operation succeeds, false if pBusIdx is invalid or bus has not been configured yet
*/
virtual bool piGetBusSettings(int pBusIdx, CANBus& pBus) = 0;
/**
* @brief suspends/restarts data capture
* @param pSuspend: suspends capture if true else restarts it
* @note the caller will not access the queue when capture is suspended, so it is safe for callee to flush the queue
*/
virtual void piSuspend(bool pSuspend) = 0;
/**
* @brief provides device with the frame to send
* @param pFrame: the frame to send
* @return false if parameter is invalid (bus id for instance)
*/
virtual bool piSendFrame(const CANFrame&) = 0;
/**
* @brief provides device with a list of frames to send
* @param pFrame: the list of frames to send
* @return false if parameter is invalid (bus id for instance)
* @note implementing this function is optional
*/
virtual bool piSendFrames(const QList<CANFrame>&);
private:
LFQueue<CANFrame> mQueue;
const QString mPort;
const QString mDriver;
const CANCon::type mType;
bool mIsCapSuspended;
QAtomicInt mStatus;
bool mStarted;
QThread* mThread_p;
};
#endif // CANCONNECTION_H