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
/*
** Please refer to the original CANlib documentation for
** explanations of all variables, structs and functions.
**
** This is a work in progress. Not everything in CANlib is supported.
** Use at your own risk. 
*/

#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
extern crate libc;
use libc::{c_int, c_long, c_uint, c_void, c_ulong};

// Use for canOpenChannel(). XOR this with all the values you need.
pub const canOPEN_EXCLUSIVE: u32  = 0x0008;
pub const canOPEN_REQUIRE_EXTENDED: u32 = 0x0010;
pub const canOPEN_ACCEPT_VIRTUAL: u32 = 0x0020;
pub const canOPEN_OVERRIDE_EXCLUSIVE: u32 = 0x0040;
pub const canOPEN_REQUIRE_INIT_ACCESS: u32 = 0x0080;
pub const canOPEN_NO_INIT_ACCESS: u32 = 0x0100;
pub const canOPEN_ACCEPT_LARGE_DLC: u32 = 0x0200;
pub const canOPEN_CAN_FD: u32 = 0x0400;
pub const canOPEN_CAN_FD_NONISO: u32 = 0x0800;

// Driver types
pub const canDRIVER_NORMAL: u32 = 4; // DEFAULT

// CAN bus speeds
pub const canBITRATE_1M: i32 = -1;
pub const canBITRATE_500K: i32 = -2;
pub const canBITRATE_250K: i32 = -3;
pub const canBITRATE_125K: i32 = -4;
pub const canBITRATE_100K: i32 = -5;
pub const canBITRATE_62K: i32 = -6;
pub const canBITRATE_50K: i32 = -7;
pub const canBITRATE_83K: i32 = -8;
pub const canBITRATE_10K: i32 = -9;

// CAN FD bus speeds
pub const canFD_BITRATE_500K_80P: i32 = -1000;
pub const canFD_BITRATE_1M_80P: i32 = -1001;
pub const canFD_BITRATE_2M_80P: i32 = -1002;
pub const canFD_BITRATE_4M_80P: i32 = -1003;
pub const canFD_BITRATE_8M_60P: i32 = -1004;

// Declare types
pub type canHandle = c_int;
pub type CanHandle = canHandle;

// Enum for different CAN errors
#[repr(C)]
pub enum canStatus {
    canOK                  = 0,
    canERR_PARAM           = -1,
    canERR_NOMSG           = -2,
    canERR_NOTFOUND        = -3,
    canERR_NOMEM           = -4,
    canERR_NOCHANELS       = -5,
    canERR_INTERRUPTED     = -6,
    canERR_TIMEOUT         = -7,
    canERR_NOTINITIALIZED  = -8,
    canERR_NOHANDLES       = -9,
    canERR_INVHANDLE       = -10,
    canERR_INIFILE         = -11,
    canERR_DRIVER          = -12,
    canERR_TXBUFOFL        = -13,
    canERR_RESERVED_1      = -14,
    canERR_HARDWARE        = -15,
    canERR_DYNALOAD        = -16,
    canERR_DYNALIB         = -17,
    canERR_DYNAINIT        = -18,
    canERR_NOT_SUPPORTED   = -19,
    canERR_RESERVED_5      = -20, 
    canERR_RESERVED_6      = -21, 
    canERR_RESERVED_2      = -22, 
    canERR_DRIVERLOAD      = -23,
    canERR_DRIVERFAILED    = -24,
    canERR_NOCONFIGMGR     = -25,
    canERR_NOCARD          = -26,
    canERR_RESERVED_7      = -27, 
    canERR_REGISTRY        = -28,
    canERR_LICENSE         = -29,
    canERR_INTERNAL        = -30,
    canERR_NO_ACCESS       = -31,
    canERR_NOT_IMPLEMENTED = -32,
    canERR_DEVICE_FILE     = -33,
    canERR_HOST_FILE       = -34,
    canERR_DISK            = -35,
    canERR_CRC             = -36,
    canERR_CONFIG          = -37,
    canERR_MEMO_FAIL       = -38,
    canERR_SCRIPT_FAIL     = -39,
    canERR_SCRIPT_WRONG_VERSION = -40,
    canERR_SCRIPT_TXE_CONTAINER_VERSION = -41,
    canERR_SCRIPT_TXE_CONTAINER_FORMAT = -42,
    canERR_BUFFER_TOO_SMALL = -43,
    canERR_IO_WRONG_PIN_TYPE = -44,
    canERR_IO_NOT_CONFIRMED = -45,
    canERR_IO_CONFIG_CHANGED = -46,
    canERR_IO_PENDING = -47,
    canERR_IO_NO_VALID_CONFIG = -48,
    canERR__RESERVED       = -49 
}

// CANlib functions
#[link(name = "canlib")]
extern "C" {
    pub fn canInitializeLibrary();

    pub fn canUnloadLibrary() -> canStatus;

    pub fn canOpenChannel(channel: c_int, flags: c_int) -> CanHandle;
    
    pub fn canClose(hnd: CanHandle) -> canStatus;
    
    pub fn canSetBusParams(hnd: CanHandle,
                       freq: c_long,
                       tseg1: c_uint,
                       tseg2: c_uint,
                       sjw: c_uint,
                       noSamp: c_uint,
                       syncmode: c_uint
    ) -> canStatus;
    
    pub fn canSetBusOutputControl(hnd: CanHandle, drivertype: c_uint) -> canStatus;
   
    pub fn canBusOn(hnd: CanHandle) -> canStatus;
    
    pub fn canBusOff(hnd: CanHandle) -> canStatus;
    
    pub fn canWrite(hnd: CanHandle,
                id: c_long,
                msg: *mut c_void,
                dlc: c_uint,
                flag: c_uint
    ) -> canStatus;

    pub fn canWriteSync(hnd: CanHandle, timeout: c_ulong) -> canStatus;
    
    pub fn canRead(hnd: CanHandle,
                   id: *mut c_long,
                   msg: *mut c_void,
                   dlc: *mut c_uint,
                   flag: *mut c_uint,
                   time: *mut c_ulong
    ) -> canStatus;
    
    pub fn canReadWait(hnd: CanHandle,
                       id: *mut c_long,
                       msg: *mut c_void,
                       dlc: *mut c_uint,
                       flag: *mut c_uint,
                       time: *mut c_ulong,
                       timeout: *mut c_ulong
    ) -> canStatus;

    pub fn canReadSync(hnd: CanHandle, timeout: c_ulong) -> canStatus;

    pub fn canGetNumberOfChannels(channelCount: *mut c_int) -> canStatus;
               
}