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
//! Low-level FFI function declarations for `LibTSCAN`
//!
//! This module contains the raw C function bindings.
//! All functions use `extern "system"` which maps to `__stdcall` on Windows.
use crate::types::*;
use std::os::raw::c_char;
#[link(name = "libTSCAN")]
extern "system" {
// ========================================================================
// Library Initialization
// ========================================================================
/// Initialize the `LibTSCAN` library
///
/// # Parameters
/// - `AEnableFIFO`: Enable FIFO mode for message reception
/// - `AEnableErrorFrame`: Enable error frame reception
/// - `AUseHWTime`: Use hardware timestamp
///
/// # Safety
/// Must be called before any other `LibTSCAN` functions.
/// Should only be called once per process.
pub fn initialize_lib_tscan(AEnableFIFO: bool, AEnableErrorFrame: bool, AUseHWTime: bool);
/// Finalize the `LibTSCAN` library
///
/// # Safety
/// Should be called when done using `LibTSCAN`.
/// No `LibTSCAN` functions should be called after this.
pub fn finalize_lib_tscan();
// ========================================================================
// Device Discovery and Connection
// ========================================================================
/// Scan for available `TSMaster` devices
///
/// # Parameters
/// - `ADeviceCount`: Output parameter for device count
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_scan_devices(ADeviceCount: *mut u32) -> u32;
/// Get device information
///
/// # Parameters
/// - `ADeviceIndex`: Device index (0-based)
/// - `AFManufacturer`: Output pointer to manufacturer string
/// - `AFProduct`: Output pointer to product string
/// - `AFSerial`: Output pointer to serial number string
///
/// # Returns
/// 0 on success, error code otherwise
///
/// # Safety
/// Returned strings are owned by the library. Do not free them.
pub fn tscan_get_device_info(
ADeviceIndex: u32,
AFManufacturer: *mut *const c_char,
AFProduct: *mut *const c_char,
AFSerial: *mut *const c_char,
) -> u32;
/// Connect to a device
///
/// # Parameters
/// - `ADeviceSerial`: Device serial number (NULL for default device)
/// - `AHandle`: Output parameter for device handle
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_connect(ADeviceSerial: *const c_char, AHandle: *mut usize) -> u32;
/// Disconnect from a device by handle
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_disconnect_by_handle(ADeviceHandle: usize) -> u32;
/// Disconnect all devices
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_disconnect_all_devices() -> u32;
// ========================================================================
// Hardware Capability Query
// ========================================================================
/// Get CAN channel count
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `AChnCount`: Output parameter for channel count
/// - `AIsFDCAN`: Output parameter indicating CAN-FD support
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_get_can_channel_count(
ADeviceHandle: usize,
AChnCount: *mut s32,
AIsFDCAN: *mut bool,
) -> u32;
/// Get device type
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `ADeviceType`: Output parameter for device type
/// - `ADeviceName`: Output pointer to device name string
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_get_device_type(
ADeviceHandle: usize,
ADeviceType: *mut s32,
ADeviceName: *mut *const c_char,
) -> u32;
// ========================================================================
// Channel Configuration
// ========================================================================
/// Configure CAN channel baudrate
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `AChnIdx`: Channel index (0-based)
/// - `ARateKbps`: Baudrate in kbps (e.g., 500.0 for 500 kbps)
/// - `A120OhmConnected`: 1 if 120Ω termination is connected, 0 otherwise
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_config_can_by_baudrate(
ADeviceHandle: usize,
AChnIdx: u32,
ARateKbps: f64,
A120OhmConnected: u32,
) -> u32;
/// Configure CAN-FD channel baudrate
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `AChnIdx`: Channel index (0-based)
/// - `AArbRateKbps`: Arbitration phase baudrate in kbps
/// - `ADataRateKbps`: Data phase baudrate in kbps
/// - `AControllerType`: Controller type (ISO/Non-ISO)
/// - `AControllerMode`: Controller mode (Normal/ACKOff/Restricted)
/// - `A120OhmConnected`: 1 if 120Ω termination is connected, 0 otherwise
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_config_canfd_by_baudrate(
ADeviceHandle: usize,
AChnIdx: s32,
AArbRateKbps: f64,
ADataRateKbps: f64,
AControllerType: TLIBCANFDControllerType,
AControllerMode: TLIBCANFDControllerMode,
A120OhmConnected: s32,
) -> u32;
// ========================================================================
// Message Transmission
// ========================================================================
/// Transmit CAN message synchronously (blocking)
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `ACAN`: Pointer to CAN message
/// - `ATimeoutMS`: Timeout in milliseconds
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_transmit_can_sync(
ADeviceHandle: usize,
ACAN: *const TLIBCAN,
ATimeoutMS: u32,
) -> u32;
/// Transmit CAN message asynchronously (non-blocking)
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `ACAN`: Pointer to CAN message
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_transmit_can_async(ADeviceHandle: usize, ACAN: *const TLIBCAN) -> u32;
/// Transmit CAN-FD message synchronously (blocking)
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `ACANFD`: Pointer to CAN-FD message
/// - `ATimeoutMS`: Timeout in milliseconds
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_transmit_canfd_sync(
ADeviceHandle: usize,
ACANFD: *const TLIBCANFD,
ATimeoutMS: u32,
) -> u32;
/// Transmit CAN-FD message asynchronously (non-blocking)
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `ACANFD`: Pointer to CAN-FD message
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_transmit_canfd_async(ADeviceHandle: usize, ACANFD: *const TLIBCANFD) -> u32;
// ========================================================================
// Message Reception
// ========================================================================
/// Receive CAN messages from FIFO
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `ACANBuffers`: Pointer to buffer for received messages
/// - `ACANBufferSize`: Input: buffer size, Output: actual message count
/// - `AChn`: Channel index (0-based)
/// - `ARXTX`: 0=RX only, 1=RX+TX
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tsfifo_receive_can_msgs(
ADeviceHandle: usize,
ACANBuffers: *mut TLIBCAN,
ACANBufferSize: *mut s32,
AChn: u8,
ARXTX: u8,
) -> u32;
/// Receive CAN-FD messages from FIFO
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `ACANBuffers`: Pointer to buffer for received messages
/// - `ACANBufferSize`: Input: buffer size, Output: actual message count
/// - `AChn`: Channel index (0-based)
/// - `ARXTX`: 0=RX only, 1=RX+TX
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tsfifo_receive_canfd_msgs(
ADeviceHandle: usize,
ACANBuffers: *mut TLIBCANFD,
ACANBufferSize: *mut s32,
AChn: u8,
ARXTX: u8,
) -> u32;
/// Clear CAN receive buffers
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `AIdxChn`: Channel index (0-based)
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tsfifo_clear_can_receive_buffers(ADeviceHandle: usize, AIdxChn: s32) -> u32;
/// Clear CAN-FD receive buffers
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `AIdxChn`: Channel index (0-based)
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tsfifo_clear_canfd_receive_buffers(ADeviceHandle: usize, AIdxChn: s32) -> u32;
/// Read CAN buffer frame count
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `AIdxChn`: Channel index (0-based)
/// - `ACount`: Output parameter for frame count
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tsfifo_read_can_buffer_frame_count(
ADeviceHandle: usize,
AIdxChn: s32,
ACount: *mut s32,
) -> s32;
// ========================================================================
// Event Callbacks (Optional)
// ========================================================================
/// Register CAN message receive callback
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `ACallback`: Callback function
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_register_event_can(ADeviceHandle: usize, ACallback: TCANQueueEvent_Win32) -> u32;
/// Unregister CAN message receive callback
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `ACallback`: Callback function
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_unregister_event_can(ADeviceHandle: usize, ACallback: TCANQueueEvent_Win32)
-> u32;
/// Register CAN-FD message receive callback
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `ACallback`: Callback function
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_register_event_canfd(
ADeviceHandle: usize,
ACallback: TCANFDQueueEvent_Win32,
) -> u32;
/// Unregister CAN-FD message receive callback
///
/// # Parameters
/// - `ADeviceHandle`: Device handle
/// - `ACallback`: Callback function
///
/// # Returns
/// 0 on success, error code otherwise
pub fn tscan_unregister_event_canfd(
ADeviceHandle: usize,
ACallback: TCANFDQueueEvent_Win32,
) -> u32;
// ========================================================================
// Error Handling
// ========================================================================
/// Get error description
///
/// # Parameters
/// - `ACode`: Error code
/// - `ADesc`: Output pointer to error description string
///
/// # Returns
/// 0 on success, error code otherwise
///
/// # Safety
/// Returned string is owned by the library. Do not free it.
pub fn tscan_get_error_description(ACode: u32, ADesc: *mut *const c_char) -> u32;
}