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 pathLed.java
More file actions
269 lines (245 loc) · 8.16 KB
/
Copy pathLed.java
File metadata and controls
269 lines (245 loc) · 8.16 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
/*
* 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.jString;
import static com.codeferm.periphery.Common.memMove;
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 LED wrapper functions for Linux userspace sysfs LEDs.
*
* @author Steven P. Goldsmith
* @version 1.0.0
* @since 1.0.0
*/
@JniClass
public class Led implements AutoCloseable {
/**
* Function was successful.
*/
public static final int LED_SUCCESS = 0;
/**
* java-periphery library.
*/
private static final Library LIBRARY = new Library("java-periphery", Led.class);
/**
* LED handle.
*/
final private long handle;
/**
* 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 LED_ERROR_ARG;
@JniField(flags = {CONSTANT})
public static int LED_ERROR_OPEN;
@JniField(flags = {CONSTANT})
public static int LED_ERROR_QUERY;
@JniField(flags = {CONSTANT})
public static int LED_ERROR_IO;
@JniField(flags = {CONSTANT})
public static int LED_ERROR_CLOSE;
/**
* Open the sysfs LED with the specified name.
*
* @param name Led name.
*/
public Led(final String name) {
// Allocate handle
handle = ledNew();
if (handle == 0) {
throw new RuntimeException("Handle cannot be NULL");
}
// Open device
if (ledOpen(handle, name) != LED_SUCCESS) {
// Free handle before throwing exception
ledFree(handle);
throw new RuntimeException(ledErrMessage(handle));
}
}
/**
* Close and free handle.
*/
@Override
public void close() {
ledClose(handle);
ledFree(handle);
}
/**
* Handle accessor.
*
* @return Handle.
*/
public long getHandle() {
return handle;
}
/**
* Allocate an LED handle.
*
* @return A valid handle on success, or NULL on failure.
*/
@JniMethod(accessor = "led_new")
public static final native long ledNew();
/**
* Open the sysfs LED with the specified name.
*
* @param led Valid pointer to an allocated LED handle structure.
* @param name Led name.
* @return 0 on success, or a negative LED error code on failure.
*/
@JniMethod(accessor = "led_open")
public static native int ledOpen(long led, String name);
/**
* Read the state of the LED into value, where true is non-zero brightness, and false is zero brightness.
*
* @param led Valid pointer to an allocated LED handle structure.
* @param value Should be a pointer to an allocated bool.
* @return 0 on success, or a negative LED error code on failure.
*/
@JniMethod(accessor = "led_read")
public static native int ledRead(long led, boolean[] value);
/**
* Write the state of the LED to value, where true is max brightness, and false is zero brightness.
*
* @param led Valid pointer to an allocated LED handle structure.
* @param value True is max brightness, and false is zero brightness.
* @return 0 on success, or a negative LED error code on failure.
*/
@JniMethod(accessor = "led_write")
public static native int ledWrite(long led, boolean value);
/**
* Close the LED.
*
* @param led Valid pointer to an allocated LED handle structure.
* @return 0 on success, or a negative LED error code on failure.
*/
@JniMethod(accessor = "led_close")
public static native int ledClose(long led);
/**
* Free an LED handle.
*
* @param led Valid pointer to an allocated LED handle structure.
*/
@JniMethod(accessor = "led_free")
public static native void ledFree(long led);
/**
* Get the brightness of the LED.
*
* @param led Valid pointer to an allocated LED handle structure.
* @param brightness Brightness value.
* @return 0 on success, or a negative LED error code on failure.
*/
@JniMethod(accessor = "led_get_brightness")
public static native int ledGetBrightness(long led, int[] brightness);
/**
* Get the max brightness of the LED.
*
* @param led Valid pointer to an allocated LED handle structure.
* @param maxBrightness Max brightness to set.
* @return 0 on success, or a negative LED error code on failure.
*/
@JniMethod(accessor = "led_get_max_brightness")
public static native int ledGetMaxBrightness(long led, int[] maxBrightness);
/**
* Set the brightness of the LED.
*
* @param led Valid pointer to an allocated LED handle structure.
* @param brightness Brightness to set.
* @return 0 on success, or a negative LED error code on failure.
*/
@JniMethod(accessor = "led_set_brightness")
public static native int ledSetBrightness(long led, int brightness);
/**
* Return the name of the sysfs LED.
*
* @param led Valid pointer to an allocated LED handle structure.
* @param str LED name.
* @param len Length of char array.
* @return 0 on success, or a negative LED error code on failure.
*/
@JniMethod(accessor = "led_name")
public static native int ledName(long led, byte[] str, long len);
/**
* Return the name of the sysfs LED. Wraps native method and simplifies.
*
* @param led Valid pointer to an allocated LED handle structure.
* @return
*/
public static String ledName(long led) {
var str = new byte[MAX_CHAR_ARRAY_LEN];
if (ledName(led, str, str.length) < 0) {
throw new RuntimeException(ledErrMessage(led));
}
return jString(str);
}
/**
* Return a string representation of the LED handle.
*
* @param led Valid pointer to an allocated LED handle structure.
* @param str String representation of the LED handle.
* @param len Length of char array.
* @return 0 on success, or a negative LED error code on failure.
*/
@JniMethod(accessor = "led_tostring")
public static native int ledToString(long led, byte[] str, long len);
/**
* Return a string representation of the LED handle. Wraps native method and simplifies.
*
* @param led Valid pointer to an allocated LED handle structure.
* @return LED handle as String.
*/
public static String ledToString(long led) {
var str = new byte[MAX_CHAR_ARRAY_LEN];
if (ledToString(led, str, str.length) < 0) {
throw new RuntimeException(ledErrMessage(led));
}
return jString(str);
}
/**
* Return the libc errno of the last failure that occurred.
*
* @param led Valid pointer to an allocated LED handle structure.
* @return libc errno.
*/
@JniMethod(accessor = "led_errno")
public static native int ledErrNo(long led);
/**
* Return a human readable error message pointer of the last failure that occurred.
*
* @param led Valid pointer to an allocated LED handle structure.
* @return Error message pointer.
*/
@JniMethod(accessor = "led_errmsg")
public static native long ledErrMsg(long led);
/**
* Return a human readable error message of the last failure that occurred. Converts const char * returned by led_errmsg to a
* Java String.
*
* @param led Valid pointer to an allocated LED handle structure.
* @return Error message.
*/
public static String ledErrMessage(long led) {
var ptr = ledErrMsg(led);
var str = new byte[MAX_CHAR_ARRAY_LEN];
memMove(str, ptr, str.length);
return jString(str);
}
}