This repository was archived by the owner on Jan 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGpio.java
More file actions
817 lines (748 loc) · 27.8 KB
/
Copy pathGpio.java
File metadata and controls
817 lines (748 loc) · 27.8 KB
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
/*
* Copyright (c) Steven P. Goldsmith. All rights reserved.
*/
package com.codeferm.periphery;
import static com.codeferm.periphery.Common.MAX_CHAR_ARRAY_LEN;
import static com.codeferm.periphery.Common.free;
import static com.codeferm.periphery.Common.jString;
import static com.codeferm.periphery.Common.memMove;
import org.fusesource.hawtjni.runtime.ClassFlag;
import static org.fusesource.hawtjni.runtime.FieldFlag.CONSTANT;
import org.fusesource.hawtjni.runtime.JniClass;
import org.fusesource.hawtjni.runtime.JniField;
import org.fusesource.hawtjni.runtime.JniMethod;
import org.fusesource.hawtjni.runtime.Library;
import static org.fusesource.hawtjni.runtime.MethodFlag.CONSTANT_INITIALIZER;
/**
* c-periphery GPIO wrapper methods for Linux userspace character device gpio-cdev and sysfs GPIOs.
*
* @author Steven P. Goldsmith
* @version 1.0.0
* @since 1.0.0
*/
@JniClass
public class Gpio implements AutoCloseable {
/**
* gpioPoll returned event.
*/
public static final int GPIO_POLL_EVENT = 1;
/**
* gpioPoll timeout.
*/
public static final int GPIO_POLL_TIMEOUT = 0;
/**
* Function was successful.
*/
public static final int GPIO_SUCCESS = 0;
/**
* java-periphery library.
*/
private static final Library LIBRARY = new Library("java-periphery", Gpio.class);
/**
* GPIO handle.
*/
final private long handle;
/**
* Gpio config struct.
*/
final private GpioConfig config;
/**
* Load library.
*/
static {
LIBRARY.load();
init();
}
/**
* Load constants.
*/
@JniMethod(flags = {CONSTANT_INITIALIZER})
private static native void init();
/**
* Error constants.
*/
@JniField(flags = {CONSTANT})
public static int GPIO_ERROR_ARG;
@JniField(flags = {CONSTANT})
public static int GPIO_ERROR_OPEN;
@JniField(flags = {CONSTANT})
public static int GPIO_ERROR_NOT_FOUND;
@JniField(flags = {CONSTANT})
public static int GPIO_ERROR_QUERY;
@JniField(flags = {CONSTANT})
public static int GPIO_ERROR_CONFIGURE;
@JniField(flags = {CONSTANT})
public static int GPIO_ERROR_UNSUPPORTED;
@JniField(flags = {CONSTANT})
public static int GPIO_ERROR_INVALID_OPERATION;
@JniField(flags = {CONSTANT})
public static int GPIO_ERROR_IO;
@JniField(flags = {CONSTANT})
public static int GPIO_ERROR_CLOSE;
/**
* Direction constants.
*/
@JniField(flags = {CONSTANT})
public static int GPIO_DIR_IN;
@JniField(flags = {CONSTANT})
public static int GPIO_DIR_OUT;
@JniField(flags = {CONSTANT})
public static int GPIO_DIR_OUT_LOW;
@JniField(flags = {CONSTANT})
public static int GPIO_DIR_OUT_HIGH;
/**
* Edge constants.
*/
@JniField(flags = {CONSTANT})
public static int GPIO_EDGE_NONE;
@JniField(flags = {CONSTANT})
public static int GPIO_EDGE_RISING;
@JniField(flags = {CONSTANT})
public static int GPIO_EDGE_FALLING;
@JniField(flags = {CONSTANT})
public static int GPIO_EDGE_BOTH;
/**
* Bias constants.
*/
@JniField(flags = {CONSTANT})
public static int GPIO_BIAS_DEFAULT;
@JniField(flags = {CONSTANT})
public static int GPIO_BIAS_PULL_UP;
@JniField(flags = {CONSTANT})
public static int GPIO_BIAS_PULL_DOWN;
@JniField(flags = {CONSTANT})
public static int GPIO_BIAS_DISABLE;
/**
* Drive constants.
*/
@JniField(flags = {CONSTANT})
public static int GPIO_DRIVE_DEFAULT;
@JniField(flags = {CONSTANT})
public static int GPIO_DRIVE_OPEN_DRAIN;
@JniField(flags = {CONSTANT})
public static int GPIO_DRIVE_OPEN_SOURCE;
/**
* gpio_config struct as Java object. Allow fluent style to set members.
*/
@JniClass(name = "gpio_config_t", flags = {ClassFlag.STRUCT, ClassFlag.TYPEDEF})
public static class GpioConfig {
static {
LIBRARY.load();
init();
}
@JniMethod(flags = {CONSTANT_INITIALIZER})
private static native void init();
@JniField(flags = {CONSTANT}, accessor = "sizeof(gpio_config_t)")
private static int SIZEOF;
private int direction;
private int edge;
private int bias;
private int drive;
private boolean inverted;
private long label;
public static int getSIZEOF() {
return SIZEOF;
}
public static void setSIZEOF(int SIZEOF) {
GpioConfig.SIZEOF = SIZEOF;
}
public int getDirection() {
return direction;
}
public GpioConfig setDirection(final int direction) {
this.direction = direction;
return this;
}
public int getEdge() {
return edge;
}
public GpioConfig setEdge(final int edge) {
this.edge = edge;
return this;
}
public int getBias() {
return bias;
}
public GpioConfig setBias(final int bias) {
this.bias = bias;
return this;
}
public int getDrive() {
return drive;
}
public GpioConfig setDrive(final int drive) {
this.drive = drive;
return this;
}
public boolean isInverted() {
return inverted;
}
public GpioConfig setInverted(final boolean inverted) {
this.inverted = inverted;
return this;
}
public long getLabel() {
return label;
}
public GpioConfig setLabel(final long label) {
this.label = label;
return this;
}
}
/**
* Open the character device GPIO with the specified GPIO line and direction at the specified character device GPIO chip path.
*
* @param path GPIO chip character device path.
* @param line GPIO line number.
* @param direction One of the direction values.
*/
public Gpio(final String path, final int line, final int direction) {
// Config not used
config = null;
// Allocate handle
handle = gpioNew();
if (handle == 0) {
throw new RuntimeException("Handle cannot be NULL");
}
// Open line
if (gpioOpen(handle, path, line, direction) != GPIO_SUCCESS) {
// Free handle before throwing exception
gpioFree(handle);
throw new RuntimeException(gpioErrMessage(handle));
}
}
/**
* Open the character device GPIO with the specified GPIO name and direction at the specified character device GPIO chip path
* (e.g. /dev/gpiochip0).
*
* @param path GPIO chip character device path.
* @param name GPIO line name.
* @param direction One of the direction values.
*/
public Gpio(final String path, final String name, final int direction) {
// Config not used
config = null;
// Allocate handle
handle = gpioNew();
if (handle == 0) {
throw new RuntimeException("Handle cannot be NULL");
}
// Open line
if (gpioOpenName(handle, path, name, direction) != GPIO_SUCCESS) {
// Free handle before throwing exception
gpioFree(handle);
throw new RuntimeException(gpioErrMessage(handle));
}
}
/**
* Open the character device GPIO with the specified GPIO line and configuration at the specified character device GPIO chip
* path (e.g. /dev/gpiochip0).
*
* @param path GPIO chip character device path.
* @param line GPIO line number.
* @param config Configuration struct.
*/
public Gpio(final String path, final int line, final GpioConfig config) {
this.config = config;
// Allocate handle
handle = gpioNew();
if (handle == 0) {
// Deallocate label before throwing exception
if (config.getLabel() != 0) {
free(config.getLabel());
}
throw new RuntimeException("Handle cannot be NULL");
}
// Open line
if (gpioOpenAdvanced(handle, path, line, config) != GPIO_SUCCESS) {
// Free handle before throwing exception
gpioFree(handle);
// Deallocate label before throwing exception
if (config.getLabel() != 0) {
free(config.getLabel());
}
throw new RuntimeException(gpioErrMessage(handle));
}
}
/**
* Open the character device GPIO with the specified GPIO name and configuration at the specified character device GPIO chip
* path (e.g. /dev/gpiochip0).
*
* @param path GPIO chip character device path.
* @param name GPIO line name.
* @param config Configuration struct.
*/
public Gpio(final String path, final String name, final GpioConfig config) {
this.config = config;
// Allocate handle
handle = gpioNew();
if (handle == 0) {
// Deallocate label before throwing exception
if (config.getLabel() != 0) {
free(config.getLabel());
}
throw new RuntimeException("Handle cannot be NULL");
}
// Open line
if (gpioOpenNameAdvanced(handle, path, name, config) != GPIO_SUCCESS) {
// Free handle before throwing exception
gpioFree(handle);
// Deallocate label before throwing exception
if (config.getLabel() != 0) {
free(config.getLabel());
}
throw new RuntimeException(gpioErrMessage(handle));
}
}
/**
* Open the sysfs GPIO with the specified line and direction.
*
* @param line GPIO line number.
* @param direction One of the direction values.
*/
public Gpio(final int line, final int direction) {
// Config not used
config = null;
// Allocate handle
handle = gpioNew();
if (handle == 0) {
throw new RuntimeException("Handle cannot be NULL");
}
// Open line
if (gpioOpenSysfs(handle, line, direction) != GPIO_SUCCESS) {
// Free handle before throwing exception
gpioFree(handle);
throw new RuntimeException(gpioErrMessage(handle));
}
}
/**
* Close handle, free handle and free line label if allocated.
*/
@Override
public void close() {
// Close handle
gpioClose(handle);
// Free handle
gpioFree(handle);
// Deallocate label
if (config != null && config.getLabel() != 0) {
free(config.getLabel());
}
}
/**
* Handle accessor.
*
* @return Handle.
*/
public long getHandle() {
return handle;
}
/**
* Config accessor.
*
* @return Config.
*/
public GpioConfig getConfig() {
return config;
}
/**
* Allocate a GPIO handle. Returns a valid handle on success, or NULL on failure.
*
* @return A valid handle on success, or NULL on failure.
*/
@JniMethod(accessor = "gpio_new")
public static final native long gpioNew();
/**
* Open the character device GPIO with the specified GPIO line and direction at the specified character device GPIO chip path.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param path GPIO chip character device path.
* @param line GPIO line number.
* @param direction One of the direction values.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_open")
public static native int gpioOpen(long gpio, String path, int line, int direction);
/**
* Open the character device GPIO with the specified GPIO name and direction at the specified character device GPIO chip path
* (e.g. /dev/gpiochip0).
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param path GPIO chip character device path.
* @param name GPIO line name.
* @param direction One of the direction values.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_open_name")
public static native int gpioOpenName(long gpio, String path, String name, int direction);
/**
* Open the character device GPIO with the specified GPIO line and configuration at the specified character device GPIO chip
* path (e.g. /dev/gpiochip0).
*
* gpio should be a valid pointer to an allocated GPIO handle structure. path is the GPIO chip character device path. line is
* the GPIO line number. config should be a valid pointer to a gpio_config_t structure with valid values. label can be NULL for
* a default consumer label.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param path GPIO chip character device path.
* @param line GPIO line number.
* @param config Configuration struct.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_open_advanced")
public static native int gpioOpenAdvanced(long gpio, String path, int line, GpioConfig config);
/**
* Open the character device GPIO with the specified GPIO name and configuration at the specified character device GPIO chip
* path (e.g. /dev/gpiochip0).
*
* gpio should be a valid pointer to an allocated GPIO handle structure. path is the GPIO chip character device path. name is
* the GPIO line name. config should be a valid pointer to a gpio_config_t structure with valid values. label can be NULL for a
* default consumer label.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param path GPIO chip character device path.
* @param name GPIO line name.
* @param config Configuration struct.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_open_name_advanced")
public static native int gpioOpenNameAdvanced(long gpio, String path, String name, GpioConfig config);
/**
* Open the sysfs GPIO with the specified line and direction.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param line GPIO line number.
* @param direction One of the direction values.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_open_sysfs")
public static native int gpioOpenSysfs(long gpio, int line, int direction);
/**
* Read the state of the GPIO into value.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param value Pointer to an allocated bool.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_read")
public static native int gpioRead(long gpio, boolean[] value);
/**
* Set the state of the GPIO to value.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param value True of false.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_write")
public static native int gpioWrite(long gpio, boolean value);
/**
* Poll a GPIO for the edge event configured with gpio_set_edge(). For character device GPIOs, the edge event should be consumed
* with gpio_read_event(). For sysfs GPIOs, the edge event should be consumed with gpio_read().
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param timeoutMs Positive number for a timeout in milliseconds, 0 for a non-blocking poll, or a negative number for a
* blocking poll.
* @return 1 on success (an edge event occurred), 0 on timeout, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_poll")
public static native int gpioPoll(long gpio, int timeoutMs);
/**
* Close the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_close")
public static native int gpioClose(long gpio);
/**
* Free a GPIO handle.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
*/
@JniMethod(accessor = "gpio_free")
public static native void gpioFree(long gpio);
/**
* Read the edge event that occurred with the GPIO. This method is intended for use with character device GPIOs and is
* unsupported by sysfs GPIOs.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param edge Pointer to an allocated int.
* @param timestamp Pointer to an allocated long.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_read_event")
public static native int gpioReadEvent(long gpio, int[] edge, long[] timestamp);
/**
* Poll multiple GPIOs for an edge event configured with gpio_set_edge(). For character device GPIOs, the edge event should be
* consumed with gpio_read_event(). For sysfs GPIOs, the edge event should be consumed with gpio_read().
*
* @param gpios A valid pointer to a size count array of GPIO handles opened with one of the gpio_open*() functions.
* @param count Number of gpio pointers.
* @param timeoutMs Positive number for a timeout in milliseconds, 0 for a non-blocking poll, or a negative number for a
* blocking poll.
* @param gpiosReady is an optional pointer to a size count array of bool that will be populated with true for the corresponding
* GPIO in the gpios array if an edge event occurred, or false if none occurred.
* @return Number of GPIOs for which an edge event occurred, 0 on timeout, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_poll_multiple")
public static native int gpioPollMultiple(long[] gpios, int count, int timeoutMs, boolean[] gpiosReady);
/**
* Get the configured direction of the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param direction Pointer to an allocated int.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_get_direction")
public static native int gpioGetDirection(long gpio, int[] direction);
/**
* Get the configured interrupt edge of the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param edge Pointer to an allocated int.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_get_edge")
public static native int gpioGetEdge(long gpio, int[] edge);
/**
* Get the configured line bias of the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param bias
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_get_bias")
public static native int gpioGetBias(long gpio, int[] bias);
/**
* Get the configured line drive of the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param drive
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_get_drive")
public static native int gpioGetDrive(long gpio, int[] drive);
/**
* Get the configured line inverted of the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param inverted Active low.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_get_inverted")
public static native int gpioGetInverted(long gpio, boolean[] inverted);
/**
* Set the direction of the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param direction One of the direction values.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_set_direction")
public static native int gpioSetDirection(long gpio, int direction);
/**
* Set the interrupt edge of the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param edge One of the edge values.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_set_edge")
public static native int gpioSetEdge(long gpio, int edge);
/**
* Set the bias of the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param bias One of the bias values.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_set_bias")
public static native int gpioSetBias(long gpio, int bias);
/**
* Set the drive of the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param drive One of the drive values.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_set_drive")
public static native int gpioSetDrive(long gpio, int drive);
/**
* Set the inverted value of the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param inverted Is line inverted (active low)?
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_set_inverted")
public static native int gpioSetInverted(long gpio, boolean inverted);
/**
* Return the line the GPIO handle was opened with.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @return Line the GPIO handle was opened with.
*/
@JniMethod(accessor = "gpio_line")
public static native int gpioLine(long gpio);
/**
* Return the line file descriptor of the GPIO handle.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @return Line file descriptor.
*/
@JniMethod(accessor = "gpio_fd")
public static native int gpioFd(long gpio);
/**
* Return the line name of the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param str Line name.
* @param len Length of char array.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_name")
public static native int gpioName(long gpio, byte[] str, long len);
/**
* Return the line name of the GPIO. Wraps native method and simplifies.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @return Line name.
*/
public static String gpioName(long gpio) {
var str = new byte[MAX_CHAR_ARRAY_LEN];
if (gpioName(gpio, str, str.length) < 0) {
throw new RuntimeException(gpioErrMessage(gpio));
}
return jString(str);
}
/**
* Return the line label of the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param str Line label.
* @param len Length of char array.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_label")
public static native int gpioLabel(long gpio, byte[] str, long len);
/**
* Return the line lable of the GPIO. Wraps native method and simplifies.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @return Line label.
*/
public static String gpioLabel(long gpio) {
var str = new byte[MAX_CHAR_ARRAY_LEN];
if (gpioLabel(gpio, str, str.length) < 0) {
throw new RuntimeException(gpioErrMessage(gpio));
}
return jString(str);
}
/**
* Return the GPIO chip file descriptor of the GPIO handle.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @return GPIO chip file descriptor.
*/
@JniMethod(accessor = "gpio_chip_fd")
public static native int gpioChipFd(long gpio);
/**
* Return the name of the GPIO chip associated with the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param str Name of the GPIO chip.
* @param len Length of char array.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_chip_name")
public static native int gpioChipName(long gpio, byte[] str, long len);
/**
* Return the name of the GPIO chip associated with the GPIO. Wraps native method and simplifies.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @return
*/
public static String gpioChipName(long gpio) {
var str = new byte[MAX_CHAR_ARRAY_LEN];
if (gpioChipName(gpio, str, str.length) < 0) {
throw new RuntimeException(gpioErrMessage(gpio));
}
return jString(str);
}
/**
* Return the label of the GPIO chip associated with the GPIO.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param str Label of the GPIO chip.
* @param len Length of char array.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_chip_label")
public static native int gpioChipLabel(long gpio, byte[] str, long len);
/**
* Return the label of the GPIO chip associated with the GPIO. Wraps native method and simplifies.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @return GPIO label.
*/
public static String gpioChipLabel(long gpio) {
var str = new byte[MAX_CHAR_ARRAY_LEN];
if (gpioChipLabel(gpio, str, str.length) < 0) {
throw new RuntimeException(gpioErrMessage(gpio));
}
return jString(str);
}
/**
*
* Return a string representation of the GPIO handle.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @param str String representation of the GPIO handle.
* @param len Length of char array.
* @return 0 on success, or a negative GPIO error code on failure.
*/
@JniMethod(accessor = "gpio_tostring")
public static native int gpioToString(long gpio, byte[] str, long len);
/**
* Return a string representation of the GPIO handle. Wraps native method and simplifies.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @return GPIO handle as String.
*/
public static String gpioToString(long gpio) {
var str = new byte[MAX_CHAR_ARRAY_LEN];
if (gpioToString(gpio, str, str.length) < 0) {
throw new RuntimeException(gpioErrMessage(gpio));
}
return jString(str);
}
/**
* Return the libc errno of the last failure that occurred.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @return libc errno.
*/
@JniMethod(accessor = "gpio_errno")
public static native int gpioErrNo(long gpio);
/**
* Return a human readable error message pointer of the last failure that occurred.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @return Error message pointer.
*/
@JniMethod(accessor = "gpio_errmsg")
public static native long gpioErrMsg(long gpio);
/**
* Return a human readable error message of the last failure that occurred. Converts const char * returned by gpio_errmsg to a
* Java String.
*
* @param gpio Valid pointer to an allocated GPIO handle structure.
* @return Error message.
*/
public static String gpioErrMessage(long gpio) {
var ptr = gpioErrMsg(gpio);
var str = new byte[MAX_CHAR_ARRAY_LEN];
memMove(str, ptr, str.length);
return jString(str);
}
}