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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#![cfg(windows)]

extern crate winreg;
extern crate libc;
#[macro_use]
extern crate bitflags;

use std::ffi;
use std::io;
use std::fmt;
use std::error;
use std::rc::Rc;
use std::str::Utf8Error;
use winreg::RegKey;
use winreg::enums::*;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Clone)]
pub struct Error {
    kind: ErrorKind,
}

#[derive(Copy, Clone, Debug)]
pub enum ErrorKind {
    NotFound,
    Code(i32),
    Utf8,
}

impl Error {
    pub fn kind(&self) -> ErrorKind {
        self.kind
    }

    pub fn from_code(code: i32) -> Error {
        Error { kind: ErrorKind::Code(code) }
    }
}

impl From<Utf8Error> for Error {
    fn from(_err: Utf8Error) -> Self {
        Error {
            kind: ErrorKind::Utf8,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.kind {
            ErrorKind::NotFound => write!(f, "Not found"),
            ErrorKind::Code(code) => write!(f, "error code: {}", code),
            ErrorKind::Utf8 => write!(f, "utf8 error"),
        }
    }
}

extern {
    fn j2534_load(path: *const libc::c_char) -> *mut libc::c_void;
    fn j2534_close(handle: *const libc::c_void);
    fn j2534_PassThruClose(handle: *const libc::c_void, device_id: libc::uint32_t) -> libc::int32_t;
    fn j2534_PassThruOpen(handle: *const libc::c_void, port: *const libc::c_char, device_id: *mut libc::uint32_t) -> libc::int32_t;
    fn j2534_PassThruConnect(handle: *const libc::c_void, device_id: libc::uint32_t, protocol_id: libc::uint32_t, flags: libc::uint32_t, baudrate: libc::uint32_t, channel_id: *mut libc::uint32_t) -> libc::int32_t;
    fn j2534_PassThruDisconnect(handle: *const libc::c_void, channel_id: libc::uint32_t) -> libc::int32_t;
    fn j2534_PassThruReadMsgs(handle: *const libc::c_void, channel_id: libc::uint32_t, msgs: *mut PassThruMsg, num_msgs: *mut libc::uint32_t, timeout: libc::uint32_t) -> libc::int32_t;
    fn j2534_PassThruWriteMsgs(handle: *const libc::c_void, channel_id: libc::uint32_t, msgs: *mut PassThruMsg, num_msgs: *mut libc::uint32_t, timeout: libc::uint32_t) -> libc::int32_t;
    fn j2534_PassThruStartPeriodicMsg(handle: *const libc::c_void, channel_id: libc::uint32_t, msg: *const PassThruMsg, msg_id: *mut libc::uint32_t, time_interval: libc::uint32_t) -> libc::int32_t;
    fn j2534_PassThruStopPeriodicMsg(handle: *const libc::c_void, channel_id: libc::uint32_t, msg_id: libc::uint32_t) -> libc::int32_t;
    fn j2534_PassThruStartMsgFilter(handle: *const libc::c_void, channel_id: libc::uint32_t, filter_type: libc::uint32_t, msg_mask: *const PassThruMsg, pattern_msg: *const PassThruMsg, flow_control_msg: *const PassThruMsg, filter_id: *mut libc::uint32_t) -> libc::int32_t;
    fn j2534_PassThruStopMsgFilter(handle: *const libc::c_void, channel_id: libc::uint32_t, filter_id: libc::uint32_t) -> libc::int32_t;
    fn j2534_PassThruSetProgrammingVoltage(handle: *const libc::c_void, device_id: libc::uint32_t, pin_number: libc::uint32_t, voltage: libc::uint32_t) -> libc::int32_t;
    fn j2534_PassThruReadVersion(handle: *const libc::c_void, device_id: libc::uint32_t, firmware_version: *mut libc::c_char, dll_version: *mut libc::c_char, api_version: *mut libc::c_char) -> libc::int32_t;
    fn j2534_PassThruGetLastError(handle: *const libc::c_void, error_description: *mut libc::c_char) -> libc::int32_t;
    fn j2534_PassThruIoctl(handle: *const libc::c_void, handle_id: libc::uint32_t, ioctl_id: libc::uint32_t, input: *mut libc::c_void, output: *mut libc::c_void) -> libc::int32_t;
}

// Much of the descriptions and APIs used here were taken from http://www.drewtech.com/support/passthru.html

#[derive(Copy, Clone)]
#[repr(C)]
pub struct PassThruMsg {
    pub protocol_id: u32,
    pub rx_status: u32,
    pub tx_flags: u32,
    pub timestamp: u32,
    pub data_size: u32,
    pub extra_data_index: u32,
    pub data: [u8; 4128],
}

impl PassThruMsg {
    pub fn new_raw(protocol: Protocol, rx_status: u32, tx_flags: u32, timestamp: u32, data_size: u32, extra_data_index: u32, data: [u8; 4128]) -> PassThruMsg {
        PassThruMsg {
            protocol_id: protocol as u32,
            rx_status,
            tx_flags,
            timestamp,
            data_size,
            extra_data_index,
            data,
        }
    }

    pub fn new(protocol: Protocol, rx_status: u32, tx_flags: u32, timestamp: u32, extra_data_index: u32, data: &[u8]) -> PassThruMsg {
        PassThruMsg::new_raw(protocol, rx_status, tx_flags, timestamp, data.len() as u32, extra_data_index, {
                let mut d: [u8; 4128] = [0; 4128];
                d[..data.len()].copy_from_slice(&data);
                d })
    }
}

impl Default for PassThruMsg {
    fn default() -> PassThruMsg {
        PassThruMsg {
            protocol_id: 0,
            rx_status: 0,
            tx_flags: 0,
            timestamp: 0,
            data_size: 0,
            extra_data_index: 0,
            data: [0; 4128],
        }
    }
}

/// Represents a J2534 library
pub struct Interface {
    handle: *const libc::c_void,
}

/// Represents a J2534 device created with `Interface::open`
pub struct Device {
    interface: Rc<Interface>,
    id: u32,
}

/// Represents a J2534 channel
pub struct Channel {
    device: Rc<Device>,
    id: u32,
    protocol_id: u32,
}

impl Interface {
    /// Returns a J2534 library given the path
    /// 
    /// # Arguments
    /// 
    /// * `path` - The absolute path to the J2534 shared library
    /// 
    /// # Example
    /// ```
    /// use j2534::Interface;
    /// let interface = Interface::new("C:\\j2534_driver.dll").unwrap();
    /// ```
    pub fn new(path: &str) -> Result<Interface> {
        let cstring  = ffi::CString::new(path).unwrap();
        let handle = unsafe { j2534_load(cstring.as_ptr()) };
        if handle.is_null() {
            return Err(Error{kind: ErrorKind::NotFound});
        }
        Ok(Interface{handle})
    }

    /// Returns a text description of the most recent error
    pub fn get_last_error(&self) -> Result<String> {
        let mut error: [u8; 80] = [0; 80];
        let res = unsafe { j2534_PassThruGetLastError(self.handle, error.as_mut_ptr() as *mut libc::c_char) };
        if res != 0 {
            return Err(Error::from_code(res));
        }
        
        unsafe { Ok(String::from(ffi::CStr::from_ptr(error.as_mut_ptr() as *mut libc::c_char).to_str()?)) }
    }
}

impl Drop for Interface {
    fn drop(&mut self) {
        unsafe { j2534_close(self.handle) };
    }
}

#[derive(Copy, Clone)]
pub enum Protocol {
    J1850VPW = 1,
    J1850PWM = 2,
    ISO9141 = 3,
    ISO14230 = 4,
    CAN = 5,
    ISO15765 = 6,
    SCI_A_ENGINE = 7,
    SCI_A_TRANS = 8,
    SCI_B_ENGINE = 9,
    SCI_B_TRANS = 10,
}

bitflags! {
    pub struct ConnectFlags: u32 {
        const NONE = 0;
        const CAN_29_BIT_ID = 0x100;
        const ISO9141_NO_CHECKSUM = 0x200;
        const CAN_ID_BOTH = 0x800;
        const ISO9141_K_LINE_ONLY = 0x1000;
    }
}

#[derive(Copy, Clone)]
pub enum FilterType {
    /// Allows matching messages into the receive queue. This filter type is only valid on non-ISO 15765 channels
    Pass = 1,
    /// Keeps matching messages out of the receive queue. This filter type is only valid on non-ISO 15765 channels
    Block = 2,
    /// Allows matching messages into the receive queue and defines an outgoing flow control message to support
    /// the ISO 15765-2 flow control mechanism. This filter type is only valid on ISO 15765 channels.
    FlowControl = 3,
}

#[derive(Debug)]
pub struct VersionInfo {
    pub firmware_version: String,
    pub dll_version: String,
    pub api_version: String,
}

pub const SHORT_TO_GROUND: u32= 0xFFFFFFFE;
pub const VOLTAGE_OFF: u32 = 0xFFFFFFFF;

impl Device {
    /// Creates a `Device` from the PassThru interface connected to the given port
    /// 
    /// # Arguments
    /// 
    /// * `port` - The port to search for a J2534 device
    /// 
    /// # Example
    /// ```
    /// use j2534::Interface;
    /// let interface = Interface::new("C:\\j2534_driver.dll").unwrap();
    /// let device = interface.open("COM2").unwrap();
    /// ```
    pub fn open(interface: Rc<Interface>, port: &str) -> Result<Device> {
        let s = ffi::CString::new(port).unwrap();
        let raw = s.as_ptr();
        let mut id = 0;
        
        let res = unsafe { j2534_PassThruOpen(interface.handle, raw, &mut id as *mut libc::uint32_t) };
        if res != 0 {
            return Err(Error::from_code(res));
        }

        Ok(Device {interface, id})
    }

    /// Creates a `Device` from any connected PassThru devices
    /// 
    /// # Example
    /// ```
    /// use j2534::Interface;
    /// let interface = Interface::new("C:\\j2534_driver.dll").unwrap();
    /// let device = interface.open_any().unwrap();
    /// ```
    pub fn open_any(interface: Rc<Interface>) -> Result<Device> {
        let raw = 0 as *const libc::c_void;
        let mut id = 0;
        let res = unsafe { j2534_PassThruOpen(interface.handle, raw as *const libc::c_char, &mut id as *mut libc::uint32_t) };
        if res != 0 {
            return Err(Error::from_code(res));
        }

        Ok(Device {interface, id})
    }

    /// Reads the version info of the device
    pub fn read_version(&self) -> Result<VersionInfo> {
        let mut firmware_version: [u8; 80] = [0; 80];
        let mut dll_version: [u8; 80] = [0; 80];
        let mut api_version: [u8; 80] = [0; 80];
        let res = unsafe { j2534_PassThruReadVersion(self.interface.handle, self.id, firmware_version.as_mut_ptr() as *mut libc::c_char, dll_version.as_mut_ptr() as *mut libc::c_char, api_version.as_mut_ptr() as *mut libc::c_char) };
        if res != 0 {
            return Err(Error::from_code(res));
        }
        unsafe {
            Ok(VersionInfo {
                firmware_version: String::from(ffi::CStr::from_ptr(firmware_version.as_mut_ptr() as *mut libc::c_char).to_str()?),
                api_version: String::from(ffi::CStr::from_ptr(api_version.as_mut_ptr() as *mut libc::c_char).to_str()?),
                dll_version: String::from(ffi::CStr::from_ptr(dll_version.as_mut_ptr() as *mut libc::c_char).to_str()?),
            })
        }
    }

    /// Outputs a programmable voltage on the specified J1962 connector pin.
    /// Only one pin can have a specified voltage applied at a time. The only exception: it is permissible to program pin 15 for SHORT_TO_GROUND, and another pin to a voltage level.
    /// When switching pins, the user application must disable the first voltage (VOLTAGE_OFF option) before enabling the second. 
    /// 
    /// # Arguments
    /// 
    /// * `pin_number` - The J1962 connector pin to which the PassThru device will apply the specified voltage
    /// * `voltage` - The voltage value (in millivolts) that will be applied to the specified pin
    pub fn set_programming_voltage(&self, pin_number: u32, voltage: u32) -> Result<()> {
        let res = unsafe { j2534_PassThruSetProgrammingVoltage(self.interface.handle, self.id, pin_number, voltage) };
        if res != 0 {
            return Err(Error::from_code(res));
        }
        Ok(())
    }
}

impl Drop for Device {
    fn drop(&mut self) {
        unsafe { j2534_PassThruClose(self.interface.handle, self.id) };
    }
}

impl Channel {
    /// See `Channel::connect`
    pub fn connect_raw(device: Rc<Device>, protocol: u32, flags: u32, baudrate: u32) -> Result<Channel> {
        let mut id: u32 = 0;
        let res = unsafe { j2534_PassThruConnect(device.interface.handle, device.id, protocol, flags, baudrate, &mut id as *mut libc::uint32_t) };
        if res != 0 {
            return Err(Error::from_code(res));
        }
        Ok(Channel {
            device,
            id,
            protocol_id: protocol,
        })
    }

    /// Creates a channel
    /// 
    /// # Arguments
    /// 
    /// * protocol - Protocol to use with the channel
    /// * flags - Protocol-specific flags. This is usually set to zero
    /// * baudrate - Initial baud rate for the channel
    pub fn connect(device: Rc<Device>, protocol: Protocol, flags: ConnectFlags, baudrate: u32) -> Result<Channel> {
        Self::connect_raw(device, protocol as u32, flags.bits(), baudrate)
    }

    /// Fills `msgs` with messages until timing out or until `msgs` is filled. Returns the slice of messages read.
    /// 
    /// # Arguments
    /// 
    /// * msgs - The array of messages to fill.
    /// * timeout - The amount of time in milliseconds to wait. If set to zero, reads buffered messages and returns immediately
    pub fn read_msgs<'b>(&self, msgs: &'b mut [PassThruMsg], timeout: u32) -> Result<&'b [PassThruMsg]> {
        for msg in msgs.iter_mut() {
            msg.protocol_id = self.protocol_id;
        }
        let mut num_msgs: u32 = msgs.len() as u32;
        let res = unsafe { j2534_PassThruReadMsgs(self.device.interface.handle, self.id, msgs.as_mut_ptr(), &mut num_msgs as *mut libc::uint32_t, timeout) };
        if res != 0 {
            return Err(Error::from_code(res));
        }
        Ok(&msgs[..(num_msgs as usize)])
    }

    /// Reads a single message
    pub fn read_msg(&self, timeout: u32) -> Result<PassThruMsg> {
        let mut msg = PassThruMsg {
            protocol_id: self.protocol_id,
            ..Default::default()
        };

        let mut num_msgs = 1 as u32;
        let res = unsafe { j2534_PassThruReadMsgs(self.device.interface.handle, self.id, &mut msg as *mut PassThruMsg, &mut num_msgs as *mut libc::uint32_t, timeout) };
        if res != 0 {
            return Err(Error::from_code(res));
        }
        Ok(msg)
    }

    /// Writes `msgs` to the device until all messages have been written or until the timeout has been reached. Returns the amount of message written.
    /// 
    /// # Arguments
    /// 
    /// * msgs - The array of messages to send.
    /// * timeout - The amount of time in milliseconds to wait. If set to zero, queues as many messages as possible and returns immediately.
    pub fn write_msgs(&self, msgs: &mut [PassThruMsg], timeout: u32) -> Result<usize> {
        for msg in msgs.iter_mut() {
            msg.protocol_id = self.protocol_id;
        }
        let mut num_msgs: u32 = msgs.len() as u32;
        let res = unsafe { j2534_PassThruWriteMsgs(self.device.interface.handle, self.id, msgs.as_mut_ptr(), &mut num_msgs as *mut libc::uint32_t, timeout) };
        if res != 0 {
            return Err(Error::from_code(res));
        }
        Ok(num_msgs as usize)
    }

    /// Sets up a network protocol filter to filter messages received by the PassThru device. There is a limit of ten filters per network layer protocol.
    /// The device blocks all receive frames by default when no filters are defined.
    /// http://www.drewtech.com/support/passthru/startmsgfilter.html
    pub fn start_msg_filter(&self, filter_type: FilterType, mask_msg: Option<&PassThruMsg>, pattern_msg: Option<&PassThruMsg>, flow_control_msg: Option<&PassThruMsg>) -> Result<u32> {
        let mut msg_id: u32 = 0;

        let mask_ptr = match mask_msg {
            Some(msg) => msg as *const PassThruMsg,
            None => std::ptr::null() as *const PassThruMsg,
        };

        let pattern_ptr = match pattern_msg {
            Some(msg) => msg as *const PassThruMsg,
            None => std::ptr::null() as *const PassThruMsg,
        };

        let flow_control_ptr = match flow_control_msg {
            Some(msg) => msg as *const PassThruMsg,
            None => std::ptr::null() as *const PassThruMsg,
        };

        let res = unsafe { j2534_PassThruStartMsgFilter(self.device.interface.handle, self.id, filter_type as u32, mask_ptr, pattern_ptr, flow_control_ptr, &mut msg_id as *mut libc::uint32_t) };
        if res != 0 {
            return Err(Error::from_code(res));
        }
        Ok(msg_id)
    }

    /// Removes a message filter started with `Channel::start_msg_filter`
    /// 
    /// # Arguments
    /// 
    /// * `msg_id` - The id of the message returned from `Channel::start_msg_filter`
    pub fn stop_msg_filter(&self, msg_id: u32) -> Result<()> {
        let res = unsafe { j2534_PassThruStopMsgFilter(self.device.interface.handle, self.id, msg_id) };
        if res != 0 {
            return Err(Error::from_code(res));
        }
        Ok(())
    }

    /// Repetitively transmit network protocol messages at the specified time interval over an existing logical communication channel. There is a limit of ten periodic messages per network layer protocol.
    /// Returns a handle for the periodic message used in `Channel::stop_periodic_msg`
    /// 
    /// # Arguments
    /// 
    /// * `msg` - The message to send
    /// * `time_interval` - The time in milliseconds to wait between sending messages. The acceptable range is between 5 and 65,535 milliseconds.
    pub fn start_periodic_msg(&self, msg: &PassThruMsg, time_interval: u32) -> Result<u32> {
        let mut msg_id = 0;
        let res = unsafe { j2534_PassThruStartPeriodicMsg(self.device.interface.handle, self.id, msg as *const PassThruMsg, &mut msg_id as *mut libc::uint32_t, time_interval) };
        if res != 0 {
            return Err(Error::from_code(res));
        }
        Ok(msg_id)
    }

    /// Stops a periodic mesage started with `Channel::start_periodic_msg`
    /// 
    /// # Arguments
    /// 
    /// * msg_id = the id of the periodic message returned from `Channel::start_periodiC_msg`
    pub fn stop_periodic_msg(&self, msg_id: u32) -> Result<()> {
        let res = unsafe { j2534_PassThruStopPeriodicMsg(self.device.interface.handle, self.id, msg_id) };
        if res != 0 {
            return Err(Error::from_code(res));
        }
        Ok(())
    }
}

impl Drop for Channel {
    fn drop(&mut self) {
        unsafe { j2534_PassThruDisconnect(self.device.interface.handle, self.id) };
    }
}


#[derive(Debug)]
pub struct Listing {
    pub name: String,
    pub vendor: String,
    pub path: String,
}

/// Returns a list of all installed PassThru drivers
pub fn list() -> io::Result<Vec<Listing>> {
    let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);

    let software = hklm.open_subkey("SOFTWARE")?;
    let passthru = software.open_subkey("PassThruSupport.04.04");
    if let Err(err) = passthru {
        if err.kind() == io::ErrorKind::NotFound {
            return Ok(Vec::new());
        }
        return Err(err);
    }
    let passthru = passthru.unwrap();
    let mut listings = Vec::new();

    for name in passthru.enum_keys() {
        let name = name?;
        let key = passthru.open_subkey(name)?;

        let device_name: String = key.get_value("Name")?;
        let vendor: String = key.get_value("Vendor")?;
        let path: String = key.get_value("FunctionLibrary")?;

        listings.push(Listing {name: device_name, vendor: vendor, path: path});
    }

    Ok(listings)
}