AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
canal.h
1/*
2MIT License
3
4Copyright (c) 2000-2023 Åke Hedman, Grodans Paradis AB
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25// clang-format off
27
28#ifndef ___CANAL_H___
29#define ___CANAL_H___
30
31
32// This was needed to compile as a service - Don't know why???
33#ifndef EXPORT
34#define EXPORT
35#endif
36
37
38
39#ifdef WIN32
40#ifndef WINAPI
41#define WINAPI __stdcall
42#endif
43#else
44
45#endif
46
47
48#define CAN_MAX_STANDARD_ID 0x7ff
49#define CAN_MAX_EXTENDED_ID 0x1fffffff
50
51
52// CANAL version
53// =============
54// This value is steped for each new version when
55// the i/f is changed. This will hopefully not
56// happen but probably will.
57
58#define CANAL_MAIN_VERSION 1
59#define CANAL_MINOR_VERSION 0
60#define CANAL_SUB_VERSION 14
61
62
63// Canal Levels
64#define CANAL_LEVEL_STANDARD 1
65#define CANAL_LEVEL_USES_TCPIP 2
66
67// VSCP daemon version information positions
68#define POS_VSCPD_MAJOR_VERSION 1
69#define POS_VSCPD_MINOR_VERSION 2
70#define POS_VSCPD_SUB_VERSION 3
71
72
73// VSCP Daemon client Open types
74#define CANAL_COMMAND_OPEN_VSCP_LEVEL1 1 // VSCP Level I channel (CAN)
75#define CANAL_COMMAND_OPEN_VSCP_LEVEL2 2 // VSCP Level II channel
76#define CANAL_COMMAND_OPEN_VSCP_CONTROL 3 // Daemon Control channel
77
79#define COMMAND_FAILURE 0
80#define COMMAND_SUCCESS 1
81
83
84#define PACKAGE_ACK 0
85#define PACKAGE_NACK 1
86
87#define PACKAGE_TIMEOUT -1
88#define PACKAGE_UNKNOWN -1
89
90#define PACKAGE_FAILURE 0
91#define PACKAGE_SUCCESS 1
92
93#ifdef __cplusplus
94extern "C" {
95#endif
96
97
99
106typedef struct structCanalMsg {
107 unsigned long flags; // CAN message flags
108 unsigned long obid; // Used by driver for channel info etc.
109 unsigned long id; // CAN id (11-bit or 29-bit)
110 unsigned char sizeData; // Data size 0-8
111 unsigned char data[8]; // CAN Data
112 unsigned long timestamp; // Relative time stamp for package in microseconds
113} canalMsg;
114
115typedef canalMsg * PCANALMSG;
116
117
124typedef struct structCanalStatistics {
125 unsigned long cntReceiveFrames; // # of receive frames
126 unsigned long cntTransmitFrames; // # of transmitted frames
127 unsigned long cntReceiveData; // # of received data bytes
128 unsigned long cntTransmitData; // # of transmitted data bytes
129 unsigned long cntOverruns; // # of overruns
130 unsigned long cntBusWarnings; // # of bys warnings
131 unsigned long cntBusOff; // # of bus off's
132} canalStatistics;
133
134typedef canalStatistics *PCANALSTATISTICS;
135
142typedef struct structCanalStatus {
143 unsigned long channel_status; // Current state for channel
144 unsigned long lasterrorcode; // Last error code
145 unsigned long lasterrorsubcode; // Last error subcode
146 char lasterrorstr[80]; // Last error string
147} canalStatus;
148
149
150typedef canalStatus * PCANALSTATUS;
151
152
153// This is the define for the received callback method
154#ifdef WIN32
155typedef void ( __stdcall * LPFNDLL_CANAL_RECEIVE_CALLBACK) ( canalMsg *pMsg );
156#endif
157
158// CAN driver open handle
159typedef long CANHANDLE;
160
161
162// * * * Prototypes
163
172#ifdef WIN32
173long WINAPI EXPORT CanalOpen( const char *pDevice, unsigned long flags );
174#else
175long CanalOpen( const char *pDevice, unsigned long flags );
176#endif
177
178
185#ifdef WIN32
186int WINAPI EXPORT CanalClose( long handle );
187#else
188int CanalClose( long handle );
189#endif
190
196#ifdef WIN32
197unsigned long WINAPI EXPORT CanalGetLevel( long handle );
198#else
199unsigned long CanalGetLevel( long handle );
200#endif
201
212#ifdef WIN32
213int WINAPI EXPORT CanalSend( long handle, const PCANALMSG pCanalMsg );
214#else
215int CanalSend( long handle, PCANALMSG pCanalMsg );
216#endif
217
218
230#ifdef WIN32
231int WINAPI EXPORT CanalBlockingSend( long handle, PCANALMSG pCanalMsg, unsigned long timeout );
232#else
233int CanalBlockingSend( long handle, PCANALMSG pCanalMsg, unsigned long timeout );
234#endif
235
246#ifdef WIN32
247int WINAPI EXPORT CanalReceive( long handle, PCANALMSG pCanalMsg );
248#else
249int CanalReceive( long handle, PCANALMSG pCanalMsg );
250#endif
251
252
264#ifdef WIN32
265int WINAPI EXPORT CanalBlockingReceive( long handle, PCANALMSG pCanalMsg, unsigned long timeout );
266#else
267int CanalBlockingReceive( long handle, PCANALMSG pCanalMsg, unsigned long timeout );
268#endif
269
277#ifdef WIN32
278int WINAPI EXPORT CanalDataAvailable( long handle );
279#else
280int CanalDataAvailable( long handle );
281#endif
282
290#ifdef WIN32
291int WINAPI EXPORT CanalGetStatus( long handle, PCANALSTATUS pCanalStatus );
292#else
293int CanalGetStatus( long handle, PCANALSTATUS pCanalStatus );
294#endif
295
303#ifdef WIN32
304int WINAPI EXPORT CanalGetStatistics( long handle, PCANALSTATISTICS pCanalStatistics );
305#else
306int CanalGetStatistics( long handle, PCANALSTATISTICS pCanalStatistics );
307#endif
308
316#ifdef WIN32
317int WINAPI EXPORT CanalSetFilter( long handle, unsigned long filter );
318#else
319int CanalSetFilter( long handle, unsigned long filter );
320#endif
321
329#ifdef WIN32
330int WINAPI EXPORT CanalSetMask( long handle, unsigned long mask );
331#else
332int CanalSetMask( long handle, unsigned long mask );
333#endif
334
342#ifdef WIN32
343int WINAPI EXPORT CanalSetBaudrate( long handle, unsigned long baudrate );
344#else
345int CanalSetBaudrate( long handle, unsigned long baudrate );
346#endif
347
353#ifdef WIN32
354unsigned long WINAPI EXPORT CanalGetVersion( void );
355#else
356unsigned long CanalGetVersion( void );
357#endif
358
364#ifdef WIN32
365unsigned long WINAPI EXPORT CanalGetDllVersion( void );
366#else
367unsigned long CanalGetDllVersion( void );
368#endif
369
375#ifdef WIN32
376const char * WINAPI EXPORT CanalGetVendorString( void );
377#else
378const char * CanalGetVendorString( void );
379#endif
380
384#ifdef WIN32
385const char * WINAPI EXPORT CanalGetDriverInfo( void );
386#else
387const char * CanalGetDriverInfo( void );
388#endif
389
390// * * * * Constants * * * *
391
393#define CANAL_NONBLOCK 1
394
395
396// read/write are none blocking
397
399#define CANAL_IDFLAG_STANDARD 0x00000000 // Standard message id (11-bit)
400#define CANAL_IDFLAG_EXTENDED 0x00000001 // Extended message id (29-bit)
401#define CANAL_IDFLAG_RTR 0x00000002 // RTR-Frame
402#define CANAL_IDFLAG_STATUS 0x00000004 // This package is a status indication (id holds error code)
403#define CANAL_IDFLAG_SEND 0x80000000 // Reserved for use by application software to indicate send
404
406#define CANAL_BAUD_USER 0 // User specified (In CANAL i/f DLL).
407#define CANAL_BAUD_1000 1 // 1 Mbit
408#define CANAL_BAUD_800 2 // 800 Kbit
409#define CANAL_BAUD_500 3 // 500 Kbit
410#define CANAL_BAUD_250 4 // 250 Kbit
411#define CANAL_BAUD_125 5 // 125 Kbit
412#define CANAL_BAUD_100 6 // 100 Kbit
413#define CANAL_BAUD_50 7 // 50 Kbit
414#define CANAL_BAUD_20 8 // 20 Kbit
415#define CANAL_BAUD_10 9 // 10 Kbit
416
418#define CANAL_STATUSMSG_OK 0x00 // Normal condition.
419#define CANAL_STATUSMSG_OVERRUN 0x01 // Overrun occured when sending data to CAN bus.
420#define CANAL_STATUSMSG_BUSLIGHT 0x02 // Error counter has reached 96.
421#define CANAL_STATUSMSG_BUSHEAVY 0x03 // Error counter has reached 128.
422#define CANAL_STATUSMSG_BUSOFF 0x04 // Device is in BUSOFF. CANAL_STATUS_OK is
423 // sent when returning to operational mode.
424#define CANAL_STATUSMSG_STUFF 0x20 // Stuff Error.
425#define CANAL_STATUSMSG_FORM 0x21 // Form Error.
426#define CANAL_STATUSMSG_ACK 0x23 // Ack Error.
427#define CANAL_STATUSMSG_BIT1 0x24 // Bit1 Error.
428#define CANAL_STATUSMSG_BIT0 0x25 // Bit0 Error.
429#define CANAL_STATUSMSG_CRC 0x27 // CRC Error.
430
432#define CANAL_STATUS_NONE 0x00000000
433#define CANAL_STATUS_ACTIVE 0x10000000
434#define CANAL_STATUS_PASSIVE 0x40000000
435#define CANAL_STATUS_BUS_OFF 0x80000000
436#define CANAL_STATUS_BUS_WARN 0x20000000
437#define CANAL_STATUS_PHY_FAULT 0x08000000
438#define CANAL_STATUS_PHY_H 0x04000000
439#define CANAL_STATUS_PHY_L 0x02000000
440#define CANAL_STATUS_SLEEPING 0x01000000
441#define CANAL_STATUS_STOPPED 0x00800000
442#define CANAL_STATUS_RECIVE_BUFFER_FULL 0x00400000 // Drivers buffer
443#define CANAL_STATUS_TRANSMIT_BUFFER_FULL 0x00200000 // Drivers buffer
444
446#define CANAL_ERROR_SUCCESS 0 // All is OK
447#define CANAL_ERROR_BAUDRATE 1 // Baudrate error
448#define CANAL_ERROR_BUS_OFF 2 // Bus off error
449#define CANAL_ERROR_BUS_PASSIVE 3 // Bus Passive error
450#define CANAL_ERROR_BUS_WARNING 4 // Bus warning error
451#define CANAL_ERROR_CAN_ID 5 // Invalid CAN ID
452#define CANAL_ERROR_CAN_MESSAGE 6 // Invalid CAN message
453#define CANAL_ERROR_CHANNEL 7 // Invalid channel
454#define CANAL_ERROR_FIFO_EMPTY 8 // FIFO is empty
455#define CANAL_ERROR_FIFO_FULL 9 // FIFI is full
456#define CANAL_ERROR_FIFO_SIZE 10 // FIFO size error
457#define CANAL_ERROR_FIFO_WAIT 11
458#define CANAL_ERROR_GENERIC 12 // Generic error
459#define CANAL_ERROR_HARDWARE 13 // Hardware error
460#define CANAL_ERROR_INIT_FAIL 14 // Initialization failed
461#define CANAL_ERROR_INIT_MISSING 15
462#define CANAL_ERROR_INIT_READY 16
463#define CANAL_ERROR_NOT_SUPPORTED 17 // Not supported
464#define CANAL_ERROR_OVERRUN 18 // Overrun
465#define CANAL_ERROR_RCV_EMPTY 19 // Receive buffer empty
466#define CANAL_ERROR_REGISTER 20 // Register value error
467#define CANAL_ERROR_TRM_FULL 21
468#define CANAL_ERROR_ERRFRM_STUFF 22 // Errorframe: stuff error detected
469#define CANAL_ERROR_ERRFRM_FORM 23 // Errorframe: form error detected
470#define CANAL_ERROR_ERRFRM_ACK 24 // Errorframe: acknowledge error
471#define CANAL_ERROR_ERRFRM_BIT1 25 // Errorframe: bit 1 error
472#define CANAL_ERROR_ERRFRM_BIT0 26 // Errorframe: bit 0 error
473#define CANAL_ERROR_ERRFRM_CRC 27 // Errorframe: CRC error
474#define CANAL_ERROR_LIBRARY 28 // Unable to load library
475#define CANAL_ERROR_PROCADDRESS 29 // Unable get library proc address
476#define CANAL_ERROR_ONLY_ONE_INSTANCE 30 // Only one instance allowed
477#define CANAL_ERROR_SUB_DRIVER 31 // Problem with sub driver call
478#define CANAL_ERROR_TIMEOUT 32 // Blocking call timeout
479#define CANAL_ERROR_NOT_OPEN 33 // The device is not open.
480#define CANAL_ERROR_PARAMETER 34 // A parameter is invalid.
481#define CANAL_ERROR_MEMORY 35 // Memory exhausted.
482#define CANAL_ERROR_INTERNAL 36 // Some kind of internal program error
483#define CANAL_ERROR_COMMUNICATION 37 // Some kind of communication error
484#define CANAL_ERROR_USER 38 // Login error
485
486// CANAL commands sent over the pipe interface (depricated)
487#define CANAL_COMMAND_NOOP 0 // No command
488#define CANAL_COMMAND_OPEN 1 // Open channel
489#define CANAL_COMMAND_CLOSE 2 // Close channel
490#define CANAL_COMMAND_SEND 3 // Send message
491#define CANAL_COMMAND_RECEIVE 4 // Receive message
492#define CANAL_COMMAND_CHECKDATA 5 // Check if data is available
493#define CANAL_COMMAND_BAUDRATE 6 // Set Baudrate
494#define CANAL_COMMAND_STATUS 7 // Get status
495#define CANAL_COMMAND_STATISTICS 8 // Get statistics
496#define CANAL_COMMAND_FILTER 9 // Set filter
497#define CANAL_COMMAND_MASK 10 // Set mask
498#define CANAL_COMMAND_VERSION 11 // CANAL version
499#define CANAL_COMMAND_DLL_VERSION 12 // CANAL DLL version
500#define CANAL_COMMAND_VENDOR_STRING 13 // CANAL vendor string
501#define CANAL_COMMAND_LEVEL 14 // CANAL Level bitarray
502
503// CANAL responses sent over the pipe interface (depricated)
504#define CANAL_RESPONSE_NONE 0 //
505#define CANAL_RESPONSE_SUCCESS 1 // OK message
506#define CANAL_RESPONSE_ERROR 2 // ERROR message
507#define CANAL_RESPONSE_MESSAGE 3 // Response to read
508
509// CANAL error codes sent over the client interface
510// on error responses
511#define CANAL_IFERROR_GENERAL 128 // General error
512#define CANAL_IFERROR_UNKNOWN_COMMAND 129
513#define CANAL_IFERROR_CHANNEL_OPEN 130
514#define CANAL_IFERROR_CHANNEL_CLOSED 131
515#define CANAL_IFERROR_SEND_SUCCESS 132
516#define CANAL_IFERROR_SEND_MSG_ALLOCATON 133
517#define CANAL_IFERROR_BUFFER_EMPTY 134
518#define CANAL_IFERROR_BUFFER_FULL 135
519#define CANAL_IFERROR_READ_FAILURE 136
520#define CANAL_IFERROR_SEND_STORAGE 137
521
522
523// * * * TCP/IP FAST mode interface constants
524
525// FAST mode primary states
526#define CANAL_BINARY_FRAME_TYPE_VSCP 0 // VSCP event
527#define CANAL_BINARY_FRAME_TYPE_ERROR 1 // ACK/NACK/errors
528#define CANAL_BINARY_FRAME_TYPE_COMMAND 2 // Command frame
529#define CANAL_BINARY_FRAME_TYPE_CAN 3 // CAN Frame
530
531#define CANAL_BINARY_COMMAND_NOOP 0 // No operation
532#define CANAL_BINARY_COMMAND_READ 1 // Read one frame
533#define CANAL_BINARY_COMMAND_CLOSE 2 // Close communication channel
534
535// FAST error codes
536#define CANAL_BINARY_ERROR_NONE 0 // OK
537#define CANAL_BINARY_ERROR_GENERAL 1 // General error
538#define CANAL_BINARY_ERROR_TO_SMALL 2 // Packet smaller then min packet
539#define CANAL_BINARY_ERROR_FORMAT 3 // Packet have bad format
540#define CANAL_BINARY_ERROR_UNKNOW_FRAME 4 // Unknown frame type
541#define CANAL_BINARY_ERROR_MEMORY 5 // No room for event
542#define CANAL_BINARY_ERROR_NO_DATA 6 // No data available
543#define CANAL_BINARY_ERROR_INVALID_CMD 7 // Command not recognized.
544
545// Filter mask settings
546#define CANUSB_ACCEPTANCE_FILTER_ALL 0x00000000
547#define CANUSB_ACCEPTANCE_MASK_ALL 0xFFFFFFFF
548
549#ifdef __cplusplus
550}
551#endif
552
553
554#endif //___CANAL_H___
555
556
557
558// History
559// =======
560//
561// 2007-10-31 AKHE - Blocking and driver properties added
562// 2006-05-16 AKHE - Added fastmode defines.
563// 2005-08-09 AKHE - Added error info to canalStatus structure.
564
566// clang-format on