Skip to content

Commit

Permalink
Added strerror output to GpioD exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
alex9849 committed Feb 22, 2024
1 parent 6aac9d0 commit ca45f76
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private GpioD() {
static GpioChip chipOpen(String path) {
Long ptr = c_gpiod_chip_open(path);
if (ptr == null)
throw new GpioDException("c_gpiod_chip_open failed!");
throw new GpioDException("c_gpiod_chip_open failed! (" + c_gpiod_strerror() + ")");
return new GpioChip(ptr);
}

Expand Down Expand Up @@ -57,7 +57,7 @@ static int chipGetNumLines(long chipPtr) {
static long chipGetLine(long chipPtr, int offset) {
Long linePtr = c_gpiod_chip_get_line(chipPtr, offset);
if (linePtr == null)
throw new GpioDException("c_gpiod_chip_get_line failed!");
throw new GpioDException("c_gpiod_chip_get_line failed! (" + c_gpiod_strerror() + ")");
return linePtr;
}

Expand All @@ -66,7 +66,7 @@ static long chipGetLine(long chipPtr, int offset) {
static long chipGetLine(long chipPtr, String name) {
Long linePtr = c_gpiod_chip_find_line(chipPtr, name);
if (linePtr == null)
throw new GpioDException("c_gpiod_chip_find_line failed!");
throw new GpioDException("c_gpiod_chip_find_line failed! (" + c_gpiod_strerror() + ")");
return linePtr;
}

Expand Down Expand Up @@ -129,71 +129,71 @@ static boolean lineIsOpenSource(long linePtr) {
static void lineUpdate(long linePtr) {
int result = c_gpiod_line_update(linePtr);
if (result < 0)
throw new GpioDException("c_gpiod_line_update failed: " + result);
throw new GpioDException("c_gpiod_line_update failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_update(long linePtr);

static void lineRequest(long linePtr, long lineRequestPtr, int defaultVal) {
int result = c_gpiod_line_request(linePtr, lineRequestPtr, defaultVal);
if (result < 0)
throw new GpioDException("c_gpiod_line_request failed: " + result);
throw new GpioDException("c_gpiod_line_request failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_request(long linePtr, long lineRequestPtr, int default_val);

static void lineRequestInput(long linePtr, String consumer) {
int result = c_gpiod_line_request_input(linePtr, consumer);
if (result < 0)
throw new GpioDException("c_gpiod_line_request_input failed: " + result);
throw new GpioDException("c_gpiod_line_request_input failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_request_input(long linePtr, String consumer);

static void lineRequestOutput(long linePtr, String consumer, int defaultVal) {
int result = c_gpiod_line_request_output(linePtr, consumer, defaultVal);
if (result < 0)
throw new GpioDException("c_gpiod_line_request_output failed: " + result);
throw new GpioDException("c_gpiod_line_request_output failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_request_output(long linePtr, String consumer, int default_val);

static void lineRequestRisingEdgeEvents(long linePtr, String consumer) {
int result = c_gpiod_line_request_rising_edge_events(linePtr, consumer);
if (result < 0)
throw new GpioDException("c_gpiod_line_request_rising_edge_events failed: " + result);
throw new GpioDException("c_gpiod_line_request_rising_edge_events failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_request_rising_edge_events(long linePtr, String consumer);

static void lineRequestFallingEdgeEvents(long linePtr, String consumer) {
int result = c_gpiod_line_request_falling_edge_events(linePtr, consumer);
if (result < 0)
throw new GpioDException("c_gpiod_line_request_falling_edge_events failed: " + result);
throw new GpioDException("c_gpiod_line_request_falling_edge_events failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_request_falling_edge_events(long linePtr, String consumer);

static void lineRequestBothEdgeEvents(long linePtr, String consumer) {
int result = c_gpiod_line_request_both_edges_events(linePtr, consumer);
if (result < 0)
throw new GpioDException("c_gpiod_line_request_both_edges_events failed: " + result);
throw new GpioDException("c_gpiod_line_request_both_edges_events failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_request_both_edges_events(long linePtr, String consumer);

static void lineRequestInputFlags(long linePtr, String consumer, int flags) {
int result = c_gpiod_line_request_input_flags(linePtr, consumer, flags);
if (result < 0)
throw new GpioDException("c_gpiod_line_request_input_flags failed: " + result);
throw new GpioDException("c_gpiod_line_request_input_flags failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_request_input_flags(long linePtr, String consumer, int flags);

static void lineRequestOutputFlags(long linePtr, String consumer, int flags, int defaultVal) {
int result = c_gpiod_line_request_output_flags(linePtr, consumer, flags, defaultVal);
if (result < 0)
throw new GpioDException("c_gpiod_line_request_output_flags failed: " + result);
throw new GpioDException("c_gpiod_line_request_output_flags failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_request_output_flags(long linePtr, String consumer, int flags,
Expand All @@ -202,23 +202,23 @@ private static native int c_gpiod_line_request_output_flags(long linePtr, String
static void lineRequestRisingEdgeEventsFlags(long linePtr, String consumer, int flags) {
int result = c_gpiod_line_request_rising_edge_events_flags(linePtr, consumer, flags);
if (result < 0)
throw new GpioDException("c_gpiod_line_request_rising_edge_events_flags failed: " + result);
throw new GpioDException("c_gpiod_line_request_rising_edge_events_flags failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_request_rising_edge_events_flags(long linePtr, String consumer, int flags);

static void lineRequestFallingEdgeEventsFlags(long linePtr, String consumer, int flags) {
int result = c_gpiod_line_request_falling_edge_events_flags(linePtr, consumer, flags);
if (result < 0)
throw new GpioDException("c_gpiod_line_request_falling_edge_events_flags failed: " + result);
throw new GpioDException("c_gpiod_line_request_falling_edge_events_flags failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_request_falling_edge_events_flags(long linePtr, String consumer, int flags);

static void lineRequestBothEdgeEventsFlags(long linePtr, String consumer, int flags) {
int result = c_gpiod_line_request_both_edges_events_flags(linePtr, consumer, flags);
if (result < 0)
throw new GpioDException("c_gpiod_line_request_both_edges_events_flags failed: " + result);
throw new GpioDException("c_gpiod_line_request_both_edges_events_flags failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_request_both_edges_events_flags(long linePtr, String consumer, int flags);
Expand All @@ -244,7 +244,7 @@ static boolean lineIsFree(long linePtr) {
static int lineGetValue(long linePtr) {
int result = c_gpiod_line_get_value(linePtr);
if (result < 0)
throw new GpioDException("c_gpiod_line_get_value failed: " + result);
throw new GpioDException("c_gpiod_line_get_value failed: " + result + " (" + c_gpiod_strerror() + ")");
return result;
}

Expand All @@ -253,47 +253,47 @@ static int lineGetValue(long linePtr) {
static void lineSetValue(long linePtr, int value) {
int result = c_gpiod_line_set_value(linePtr, value);
if (result < 0)
throw new GpioDException("c_gpiod_line_set_value failed: " + result);
throw new GpioDException("c_gpiod_line_set_value failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_set_value(long linePtr, int value);

static void lineSetConfig(long linePtr, LineRequest direction, int flags, int value) {
int result = c_gpiod_line_set_config(linePtr, direction.val, flags, value);
if (result < 0)
throw new GpioDException("c_gpiod_line_set_config failed: " + result);
throw new GpioDException("c_gpiod_line_set_config failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_set_config(long linePtr, int direction, int flags, int value);

static void lineSetFlags(long linePtr, int flags) {
int result = c_gpiod_line_set_flags(linePtr, flags);
if (result < 0)
throw new GpioDException("c_gpiod_line_set_flags failed: " + result);
throw new GpioDException("c_gpiod_line_set_flags failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_set_flags(long linePtr, int flags);

static void lineSetDirectionInput(long linePtr) {
int result = c_gpiod_line_set_direction_input(linePtr);
if (result < 0)
throw new GpioDException("c_gpiod_line_set_direction_input failed: " + result);
throw new GpioDException("c_gpiod_line_set_direction_input failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_set_direction_input(long linePtr);

static void lineSetDirectionOutput(long linePtr, int value) {
int result = c_gpiod_line_set_direction_output(linePtr, value);
if (result < 0)
throw new GpioDException("c_gpiod_line_set_direction_output failed: " + result);
throw new GpioDException("c_gpiod_line_set_direction_output failed: " + result + " (" + c_gpiod_strerror() + ")");
}

private static native int c_gpiod_line_set_direction_output(long linePtr, int value);

static boolean lineEventWait(long linePtr, long timeoutNs) {
int result = c_gpiod_line_event_wait(linePtr, timeoutNs);
if (result < 0)
throw new GpioDException("c_gpiod_line_event_wait failed: " + result);
throw new GpioDException("c_gpiod_line_event_wait failed: " + result + " (" + c_gpiod_strerror() + ")");
return result > 0;
}

Expand All @@ -302,7 +302,7 @@ static boolean lineEventWait(long linePtr, long timeoutNs) {
static boolean lineEventRead(long linePtr, long lineEventPtr) {
int result = c_gpiod_line_event_read(linePtr, lineEventPtr);
if (result < 0)
throw new GpioDException("c_gpiod_line_event_read failed: " + result);
throw new GpioDException("c_gpiod_line_event_read failed: " + result + " (" + c_gpiod_strerror() + ")");
return result > 0;
}

Expand All @@ -311,7 +311,7 @@ static boolean lineEventRead(long linePtr, long lineEventPtr) {
static long lineGet(String device, int offset) {
Long ptr = c_gpiod_line_get(device, offset);
if (ptr == null)
throw new GpioDException("c_gpiod_line_get failed!");
throw new GpioDException("c_gpiod_line_get failed! (" + c_gpiod_strerror() + ")");
return ptr;
}

Expand All @@ -320,7 +320,7 @@ static long lineGet(String device, int offset) {
static long lineFind(String name) {
Long ptr = gpiod_line_find(name);
if (ptr == null)
throw new GpioDException("gpiod_line_find failed!");
throw new GpioDException("gpiod_line_find failed! (" + c_gpiod_strerror() + ")");
return ptr;
}

Expand All @@ -341,7 +341,7 @@ static long lineGetChip(long linePtr) {
static long chipIterNew() {
Long ptr = gpiod_chip_iter_new();
if (ptr == null)
throw new GpioDException("gpiod_chip_iter_new failed!");
throw new GpioDException("gpiod_chip_iter_new failed! (" + c_gpiod_strerror() + ")");
return ptr;
}

Expand Down Expand Up @@ -382,7 +382,7 @@ static LineEvent lineEventGetType(long lineEventPtr) {
static long lineEventNew() {
Long ptr = c_gpiod_line_event_new();
if (ptr == null)
throw new GpioDException("c_gpiod_line_event_new failed!");
throw new GpioDException("c_gpiod_line_event_new failed! (" + c_gpiod_strerror() + ")");
return ptr;
}

Expand All @@ -399,4 +399,6 @@ static String getVersion() {
}

private static native String c_gpiod_version_string();

private static native String c_gpiod_strerror();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#include <gpiod.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include "com_pi4j_library_gpiod_internal_GpioD.h"

// Compile using:
Expand Down Expand Up @@ -615,4 +617,14 @@ JNIEXPORT void JNICALL Java_com_pi4j_library_gpiod_internal_GpioD_c_1gpiod_1line
JNIEXPORT jstring JNICALL Java_com_pi4j_library_gpiod_internal_GpioD_c_1gpiod_1version_1string
(JNIEnv* env, jclass javaClass) {
return (*env)->NewStringUTF(env, gpiod_version_string());
}

/*
* Class: com_pi4j_library_gpiod_internal_GpioD
* Method: c_gpiod_strerror
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_pi4j_library_gpiod_internal_GpioD_c_1gpiod_1strerror
(JNIEnv* env, jclass javaClass) {
return (*env)->NewStringUTF(env, strerror(errno));
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ca45f76

Please sign in to comment.