linux-gpib-rs 0.2.6

Low-level wrapper for Linux GPIB
Documentation
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
use crate::error::{GpibError, IbError};
#[cfg(feature = "nigpib")]
use crate::lowlevel::utility::Ibcnt;
#[cfg(feature = "linuxgpib")]
use crate::lowlevel::utility::{AsyncIbcntl, ThreadIbcnt, ThreadIbcntl};
use crate::status::IbStatus;
use crate::types::{
    IbEosMode, IbEvent, IbLineStatus, IbOnline, IbOption, IbSendEOI, IbTimeout, PrimaryAddress,
    SecondaryAddress,
};
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int, c_short, c_void};
use std::path::Path;

/// ibask -- query configuration (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibask.html)
pub fn ibask(ud: c_int, option: IbOption) -> Result<c_int, GpibError> {
    let option = option.as_option();
    let mut result: c_int = 0;
    let status = IbStatus::from_ibsta(unsafe {
        linux_gpib_sys::ibask(ud, option, &mut result as *mut c_int)
    });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(result)
    }
}

#[cfg(feature = "linuxgpib")]
/// ibbna -- change access board (device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibbna.html)
pub fn ibbna(ud: c_int, name: &str) -> Result<(), GpibError> {
    let name = CString::new(name)?;
    let status =
        IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibbna(ud, name.as_ptr() as *mut c_char) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibcac -- assert ATN (board)
///
/// ibcac() causes the board specified by the board descriptor ud to become active controller by asserting the ATN line. The board must be controller-in-change in order to assert ATN. If synchronous is nonzero, then the board will wait for a data byte on the bus to complete its transfer before asserting ATN. If the synchronous attempt times out, or synchronous is zero, then ATN will be asserted immediately.
///
/// It is generally not necessary to call ibcac(). It is provided for advanced users who want direct, low-level access to the GPIB bus.
///
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibcac.html)
pub fn ibcac(ud: c_int, synchronous: c_int) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibcac(ud, synchronous) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibclr -- clear device (device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibclr.html)
pub fn ibclr(ud: c_int) -> Result<(), GpibError> {
    log::debug!("ibclr({})", ud);
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibclr(ud) });
    log::debug!("ibclr({}) -> {:?}", ud, status);
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibcmd -- write command bytes (board)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibcmd.html)
pub fn ibcmd(ud: c_int, commands: &[u8]) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe {
        linux_gpib_sys::ibcmd(
            ud,
            commands.as_ptr() as *const c_void,
            commands.len().try_into()?,
        )
    });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibconfig -- change configuration (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibconfig.html)
pub fn ibconfig(ud: c_int, option: IbOption, setting: c_int) -> Result<(), GpibError> {
    let option = option.as_option();
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibconfig(ud, option, setting) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// open a device (device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibdev.html)
pub fn ibdev(
    board_index: c_int,
    primary_address: PrimaryAddress,
    secondary_address: SecondaryAddress,
    timeout: IbTimeout,
    send_eoi: IbSendEOI,
    eos: IbEosMode,
) -> Result<c_int, GpibError> {
    let ud = unsafe {
        linux_gpib_sys::ibdev(
            board_index,
            primary_address.as_pad(),
            secondary_address.as_sad(),
            timeout.as_timeout(),
            send_eoi.as_eot(),
            eos.as_mode(),
        )
    };
    log::debug!(
        "ibdev({}, {}, {}, {}, {}, {}) -> {}",
        board_index,
        primary_address,
        secondary_address,
        timeout,
        send_eoi,
        eos,
        ud
    );
    if ud >= 0 {
        Ok(ud)
    } else {
        #[cfg(feature = "linuxgpib")]
        return Err(GpibError::DriverError(
            IbStatus::current_thread_local_status(),
            IbError::current_thread_local_error()?,
        ));
        #[cfg(feature = "nigpib")]
        return Err(GpibError::DriverError(
            unsafe { IbStatus::current_global_status() },
            unsafe { IbError::current_global_error() }?,
        ));
    }
}

/// ibeos -- set end-of-string mode (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibeos.html)
pub fn ibeos(ud: c_int, eosmod: IbEosMode) -> Result<(), GpibError> {
    let eosmod = eosmod.as_mode();
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibeos(ud, eosmod) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibeot -- assert EOI with last data byte (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibeot.html)
pub fn ibeot(ud: c_int, send_eoi: IbSendEOI) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibeot(ud, send_eoi.as_eot()) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

#[cfg(feature = "linuxgpib")]
/// ibevent -- get events from event queue (board)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibevent.html)
pub fn ibevent(ud: c_int) -> Result<IbEvent, GpibError> {
    let mut event_value: c_short = 0;
    let status = IbStatus::from_ibsta(unsafe {
        linux_gpib_sys::ibevent(ud, &mut event_value as *mut c_short)
    });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(IbEvent::from_value(event_value)?)
    }
}

/// ibfind -- open a board or device (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibfind.html)
pub fn ibfind(name: &str) -> Result<c_int, GpibError> {
    let name = CString::new(name)?;
    let ud = unsafe { linux_gpib_sys::ibfind(name.as_ptr()) };
    if ud >= 0 {
        Ok(ud)
    } else {
        Err(GpibError::DriverError(
            #[cfg(feature = "linuxgpib")]
            IbStatus::current_thread_local_status(),
            #[cfg(feature = "nigpib")]
            unsafe {
                IbStatus::current_global_status()
            },
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    }
}

/// ibgts -- release ATN (board)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibgts.html)
pub fn ibgts(ud: c_int, shadow_handshake: c_int) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibgts(ud, shadow_handshake) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibist -- set individual status bit (board)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibist.html)
pub fn ibist(ud: c_int, ist: c_int) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibist(ud, ist) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// iblines -- monitor bus lines (board)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-iblines.html)
pub fn iblines(ud: c_int) -> Result<IbLineStatus, GpibError> {
    let mut line_status: c_short = 0;
    let status = IbStatus::from_ibsta(unsafe {
        linux_gpib_sys::iblines(ud, &mut line_status as *mut c_short)
    });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(IbLineStatus::from_line_status(line_status))
    }
}

/// ibln -- check if listener is present (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibln.html)
pub fn ibln(
    ud: c_int,
    primary_address: PrimaryAddress,
    secondary_address: SecondaryAddress,
) -> Result<bool, GpibError> {
    let mut found_listener: c_short = 0;
    let status = IbStatus::from_ibsta(unsafe {
        linux_gpib_sys::ibln(
            ud,
            primary_address.as_pad(),
            secondary_address.as_sad(),
            &mut found_listener as *mut c_short,
        )
    });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(found_listener != 0)
    }
}

/// ibloc -- go to local mode (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibloc.html)
pub fn ibloc(ud: c_int) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibloc(ud) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibonl -- close or reinitialize descriptor (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibonl.html)
pub fn ibonl(ud: c_int, online: IbOnline) -> Result<(), GpibError> {
    log::debug!("ibonl({}, {})", ud, online);
    let online = online.as_online();
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibonl(ud, online) });
    log::debug!("ibonl({}, {}) -> {:?}", ud, online, status);
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibpad -- set primary GPIB address (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibpad.html)
pub fn ibpad(ud: c_int, primary_address: PrimaryAddress) -> Result<(), GpibError> {
    let status =
        IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibpad(ud, primary_address.as_pad()) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibpct -- pass control (board)
///
/// ibpct() passes control to the device specified by the device descriptor ud. The device becomes the new controller-in-charge.
///
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibpct.html)
pub fn ibpct(ud: c_int) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibpct(ud) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibppc -- parallel poll configure (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibppc.html)
pub fn ibppc(ud: c_int, configuration: c_int) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibppc(ud, configuration) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// read data bytes (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibrd.html)
pub fn ibrd(ud: c_int, buffer: &mut [u8]) -> Result<(IbStatus, usize), GpibError> {
    let status = IbStatus::from_ibsta(unsafe {
        linux_gpib_sys::ibrd(
            ud,
            buffer.as_mut_ptr() as *mut c_void,
            buffer.len().try_into()?,
        )
    });
    log::debug!("ibrd({}, count = {}) -> {:?}", ud, buffer.len(), status);
    #[cfg(feature = "linuxgpib")]
    let bytes_read = ThreadIbcntl();
    #[cfg(feature = "nigpib")]
    let bytes_read = Ibcnt();
    log::debug!("bytes_read (ibcntl) = {bytes_read}.");
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        if bytes_read > buffer.len().try_into()? {
            Err(GpibError::ValueError(format!(
                "bytes_read ({}) > buffer.len() ({})",
                bytes_read,
                buffer.len(),
            )))
        } else {
            log::debug!("-> {} bytes read", bytes_read);
            Ok((status, bytes_read.try_into()?))
        }
    }
}

/// read data bytes asynchronously (board or device)
///
/// This function is unsafe because Rust will not be able to check the lifetime
/// of buffer. It needs to remain available until the asynchronous read completes.
pub unsafe fn ibrda(ud: c_int, buffer: &mut [u8]) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe {
        linux_gpib_sys::ibrda(
            ud,
            buffer.as_mut_ptr() as *mut c_void,
            buffer.len().try_into()?,
        )
    });
    log::debug!("ibrda({}) -> {:?}", ud, status);
    if status.err {
        return Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ));
    } else {
        Ok(())
    }
}

/// read data bytes to file (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibrdf.html)
pub fn ibrdf(ud: c_int, file_path: &Path) -> Result<(), GpibError> {
    let file_path = CString::new(file_path.to_str().ok_or(GpibError::ValueError(format!(
        "Unable to convert path '{:?}' to string",
        file_path
    )))?)?;
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibrdf(ud, file_path.as_ptr()) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// perform a parallel poll (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibrpp.html)
pub fn ibrpp(ud: c_int) -> Result<c_char, GpibError> {
    let mut ppoll_result: c_char = 0;
    let status = IbStatus::from_ibsta(unsafe {
        linux_gpib_sys::ibrpp(ud, &mut ppoll_result as *mut c_char)
    });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(ppoll_result)
    }
}

/// ibrsc -- request system control (board)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibrsc.html)
pub fn ibrsc(ud: c_int, request_control: c_int) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibrsc(ud, request_control) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibrsp --  read status byte / serial poll (device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibrsp.html)
pub fn ibrsp(ud: c_int) -> Result<c_char, GpibError> {
    let mut result: c_char = 0;
    let status =
        IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibrsp(ud, &mut result as *mut c_char) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(result)
    }
}

/// ibrsv -- request service (board)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibrsv.html)
pub fn ibrsv(ud: c_int, status_byte: c_int) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibrsv(ud, status_byte) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

#[cfg(feature = "linuxgpib")]
/// ibrsv2 -- request service (board)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibrsv2.html)
pub fn ibrsv2(
    ud: c_int,
    status_byte: c_int,
    new_reason_for_request: c_int,
) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe {
        linux_gpib_sys::ibrsv2(ud, status_byte, new_reason_for_request)
    });
    if status.err {
        Err(GpibError::DriverError(
            status,
            IbError::current_thread_local_error()?,
        ))
    } else {
        Ok(())
    }
}

/// ibsad -- set secondary GPIB address (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibsad.html)
pub fn ibsad(ud: c_int, secondary_address: SecondaryAddress) -> Result<(), GpibError> {
    let status =
        IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibsad(ud, secondary_address.as_sad()) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibsic -- perform interface clear (board)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibsic.html)
pub fn ibsic(ud: c_int) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibsic(ud) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

#[cfg(feature = "linuxgpib")]
/// ibspb --  obtain length of serial poll bytes queue (device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibspb.html)
pub fn ibspb(ud: c_int) -> Result<c_short, GpibError> {
    let mut result: c_short = 0;
    let status =
        IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibspb(ud, &mut result as *mut c_short) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            IbError::current_thread_local_error()?,
        ))
    } else {
        Ok(result)
    }
}

/// ibsre -- set remote enable (board)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibsre.html)
pub fn ibsre(ud: c_int, enable: c_int) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibsre(ud, enable) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibstop -- abort asynchronous i/o operation (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibstop.html)
pub fn ibstop(ud: c_int) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibstop(ud) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibtmo -- adjust io timeout (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibtmo.html)
pub fn ibtmo(ud: c_int, timeout: IbTimeout) -> Result<(), GpibError> {
    let timeout = timeout.as_timeout();
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibtmo(ud, timeout) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

/// ibtrg -- trigger device (device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibtrg.html)
pub fn ibtrg(ud: c_int) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibtrg(ud) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(())
    }
}

#[cfg(feature = "linuxgpib")]
/// ibvers -- Obtain the current linux gpib version
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibvers.html)
pub fn ibvers() -> Result<String, GpibError> {
    let mut buffer_ptr: *mut c_char = std::ptr::null_mut();
    unsafe { linux_gpib_sys::ibvers(&mut buffer_ptr as *mut *mut c_char) }
    Ok(unsafe { CStr::from_ptr(buffer_ptr) }.to_str()?.to_owned())
}

#[cfg(feature = "async-tokio")]
/// wait for event (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibwait.html)
pub async fn ibwait(ud: c_int, status_mask: IbStatus) -> Result<(IbStatus, usize), GpibError> {
    let status_mask = status_mask.as_status_mask();
    let res = tokio::task::spawn_blocking(move || {
        let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibwait(ud, status_mask) });
        if status.err {
            Err(GpibError::DriverError(
                status,
                #[cfg(feature = "linuxgpib")]
                IbError::current_async_local_error()?,
                #[cfg(feature = "nigpib")]
                unsafe { IbError::current_global_error() }?,
            ))
        } else {
            Ok((
                status,
                #[cfg(feature = "linuxgpib")]
                AsyncIbcntl().try_into()?,
                #[cfg(feature = "nigpib")]
                Ibcnt().try_into()?,
            ))
        }
    })
    .await?;
    log::debug!("ibwait({}, {}) -> {:?}", ud, status_mask, res);
    res
}

/// ibwrt -- write data bytes (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibwrt.html)
pub fn ibwrt(ud: c_int, data: &[u8]) -> Result<usize, GpibError> {
    let status = IbStatus::from_ibsta(unsafe {
        linux_gpib_sys::ibwrt(ud, data.as_ptr() as *const c_void, data.len().try_into()?)
    });
    log::debug!(
        "ibwrt({}, {:?}) -> {:?}",
        ud,
        String::from_utf8(data.to_vec())?,
        status
    );
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(
            #[cfg(feature = "linuxgpib")]
            ThreadIbcntl().try_into()?,
            #[cfg(feature = "nigpib")]
            Ibcnt().try_into()?,
        )
    }
}

/// write data bytes asynchronously (board or device)
///
/// Unsafe because the lifetime of buffer is not checked.
pub unsafe fn ibwrta(ud: c_int, data: &[u8]) -> Result<(), GpibError> {
    let status = IbStatus::from_ibsta(unsafe {
        linux_gpib_sys::ibwrta(ud, data.as_ptr() as *const c_void, data.len().try_into()?)
    });
    log::debug!("ibwrta({}, {:?}) -> {:?}", ud, data, status);
    if status.err {
        return Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ));
    } else {
        Ok(())
    }
}

/// ibwrtf -- write data bytes from file (board or device)
/// See: [Linux GPIB Reference](https://linux-gpib.sourceforge.io/doc_html/reference-function-ibwrtf.html)
pub fn ibwrtf(ud: c_int, file_path: &Path) -> Result<usize, GpibError> {
    let file_path = CString::new(file_path.to_str().ok_or(GpibError::ValueError(format!(
        "Unable to convert path '{:?}' to string",
        file_path
    )))?)?;
    let status = IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibwrtf(ud, file_path.as_ptr()) });
    if status.err {
        Err(GpibError::DriverError(
            status,
            #[cfg(feature = "linuxgpib")]
            IbError::current_thread_local_error()?,
            #[cfg(feature = "nigpib")]
            unsafe { IbError::current_global_error() }?,
        ))
    } else {
        Ok(
            #[cfg(feature = "linuxgpib")]
            ThreadIbcntl().try_into()?,
            #[cfg(feature = "nigpib")]
            Ibcnt().try_into()?,
        )
    }
}