Skip to content

Commit

Permalink
fix SO_BROADCAST
Browse files Browse the repository at this point in the history
  • Loading branch information
pschichtel committed Feb 18, 2024
1 parent 58d01a4 commit d3753cb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.nio.ByteBuffer;

import static org.junit.jupiter.api.Assertions.*;
import static tel.schich.javacan.CanSocketOptions.SO_BROADCAST;
import static tel.schich.javacan.TestHelper.assertByteBufferEquals;
import static tel.schich.javacan.TestHelper.directBufferOf;
import static tel.schich.javacan.test.CanTestHelper.CAN_INTERFACE;
Expand Down Expand Up @@ -66,16 +67,17 @@ void testLoopback() throws Exception {

@Test
void testOptions() throws Exception {
// TODO figure broadcast out
// try (final J1939CanChannel socket = CanChannels.newJ1939Channel()) {
// socket.bind(CAN_INTERFACE, J1939CanChannel.NO_NAME, J1939CanChannel.NO_PGN, J1939CanChannel.IDLE_ADDR);
// //assertFalse(socket.getOption(SO_BROADCAST), "Broadcasts are disabled by default");
// socket.setOption(SO_BROADCAST, true);
// socket.connect(CAN_INTERFACE, 1L, 1, J1939CanChannel.NO_ADDR);
//
J1939Address source = new J1939Address(CAN_INTERFACE, J1939Address.NO_NAME, J1939Address.NO_PGN, J1939Address.IDLE_ADDR);
J1939Address destination = new J1939Address(CAN_INTERFACE, J1939Address.NO_NAME, J1939Address.NO_PGN, J1939Address.NO_ADDR);
try (final J1939CanChannel socket = CanChannels.newJ1939Channel()) {
socket.bind(source);
assertFalse(socket.getOption(SO_BROADCAST), "Broadcasts are disabled by default");
socket.setOption(SO_BROADCAST, true);
socket.connect(destination);

// assertTrue(socket.getOption(SO_BROADCAST), "Broadcasts can be enabled");
// socket.setOption(SO_BROADCAST, false);
// assertFalse(socket.getOption(SO_BROADCAST), "Broadcasts can be disable again");
// }
socket.setOption(SO_BROADCAST, false);
assertFalse(socket.getOption(SO_BROADCAST), "Broadcasts can be disable again");
}
}
}
8 changes: 4 additions & 4 deletions core/src/main/c/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,18 @@ int is_blocking(int sock) {
return (flags & O_NONBLOCK) == 0 ? 1 : 0;
}

int set_boolean_opt(int sock, int opt, bool enable) {
int set_boolean_opt(int sock, int level, int opt, bool enable) {
int enabled = enable ? 1 : 0;
socklen_t len = sizeof(enabled);

return setsockopt(sock, SOL_CAN_RAW, opt, &enabled, len);
return setsockopt(sock, level, opt, &enabled, len);
}

int get_boolean_opt(int sock, int opt) {
int get_boolean_opt(int sock, int level, int opt) {
int enabled;
socklen_t len = sizeof(enabled);

int result = getsockopt(sock, SOL_CAN_RAW, opt, &enabled, &len);
int result = getsockopt(sock, level, opt, &enabled, &len);
if (result == -1) {
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/c/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ int set_timeout(int, int, uint64_t, uint64_t);
int get_timeout(int, int, uint64_t*);
int set_blocking_mode(int, bool);
int is_blocking(int);
int set_boolean_opt(int sock, int opt, bool enable);
int get_boolean_opt(int sock, int opt);
int set_boolean_opt(int sock, int level, int opt, bool enable);
int get_boolean_opt(int sock, int level, int opt);
short poll_single(int, short, int);
void throw_native_exception(JNIEnv *env, char *msg);

Expand Down
20 changes: 10 additions & 10 deletions core/src/main/c/javacan_socketcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,15 @@ JNIEXPORT jint JNICALL Java_tel_schich_javacan_SocketCAN_getReceiveBufferSize(JN
}

JNIEXPORT jint JNICALL Java_tel_schich_javacan_SocketCAN_setBroadcast(JNIEnv *env, jclass clazz, jint sock, jboolean enable) {
jint result = set_boolean_opt(sock, SO_BROADCAST, enable);
jint result = set_boolean_opt(sock, SOL_SOCKET, SO_BROADCAST, enable);
if (result) {
throw_native_exception(env, "Unable to enable broadcast");
}
return result;
}

JNIEXPORT jint JNICALL Java_tel_schich_javacan_SocketCAN_getBroadcast(JNIEnv *env, jclass clazz, jint sock) {
int result = get_boolean_opt(sock, SO_BROADCAST);
int result = get_boolean_opt(sock, SOL_SOCKET, SO_BROADCAST);
if (result) {
throw_native_exception(env, "Unable to get broadcast state");
}
Expand Down Expand Up @@ -271,63 +271,63 @@ JNIEXPORT jobject JNICALL Java_tel_schich_javacan_SocketCAN_getFilters(JNIEnv *e
}

JNIEXPORT jint JNICALL Java_tel_schich_javacan_SocketCAN_setLoopback(JNIEnv *env, jclass clazz, jint sock, jboolean enable) {
jint result = set_boolean_opt(sock, CAN_RAW_LOOPBACK, enable);
jint result = set_boolean_opt(sock, SOL_CAN_RAW, CAN_RAW_LOOPBACK, enable);
if (result == -1) {
throw_native_exception(env, "Unable to set loopback state");
}
return result;
}

JNIEXPORT jint JNICALL Java_tel_schich_javacan_SocketCAN_getLoopback(JNIEnv *env, jclass clazz, jint sock) {
jint result = get_boolean_opt(sock, CAN_RAW_LOOPBACK);
jint result = get_boolean_opt(sock, SOL_CAN_RAW, CAN_RAW_LOOPBACK);
if (result == -1) {
throw_native_exception(env, "Unable to get loopback state");
}
return result;
}

JNIEXPORT jint JNICALL Java_tel_schich_javacan_SocketCAN_setReceiveOwnMessages(JNIEnv *env, jclass clazz, jint sock, jboolean enable) {
jint result = set_boolean_opt(sock, CAN_RAW_RECV_OWN_MSGS, enable);
jint result = set_boolean_opt(sock, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, enable);
if (result == -1) {
throw_native_exception(env, "Unable to set receive own messages state");
}
return result;
}

JNIEXPORT jint JNICALL Java_tel_schich_javacan_SocketCAN_getReceiveOwnMessages(JNIEnv *env, jclass clazz, jint sock) {
jint result = get_boolean_opt(sock, CAN_RAW_RECV_OWN_MSGS);
jint result = get_boolean_opt(sock, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS);
if (result == -1) {
throw_native_exception(env, "Unable to get receive own messages state");
}
return result;
}

JNIEXPORT jint JNICALL Java_tel_schich_javacan_SocketCAN_setJoinFilters(JNIEnv *env, jclass clazz, jint sock, jboolean enable) {
jint result = set_boolean_opt(sock, CAN_RAW_JOIN_FILTERS, enable);
jint result = set_boolean_opt(sock, SOL_CAN_RAW, CAN_RAW_JOIN_FILTERS, enable);
if (result == -1) {
throw_native_exception(env, "Unable to set the filter joining mode");
}
return result;
}

JNIEXPORT jint JNICALL Java_tel_schich_javacan_SocketCAN_getJoinFilters(JNIEnv *env, jclass clazz, jint sock) {
jint result = get_boolean_opt(sock, CAN_RAW_JOIN_FILTERS);
jint result = get_boolean_opt(sock, SOL_CAN_RAW, CAN_RAW_JOIN_FILTERS);
if (result == -1) {
throw_native_exception(env, "Unable to get the filter joining mode");
}
return result;
}

JNIEXPORT jint JNICALL Java_tel_schich_javacan_SocketCAN_setAllowFDFrames(JNIEnv *env, jclass clazz, jint sock, jboolean enable) {
jint result = set_boolean_opt(sock, CAN_RAW_FD_FRAMES, enable);
jint result = set_boolean_opt(sock, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, enable);
if (result == -1) {
throw_native_exception(env, "Unable to set FD frame support");
}
return result;
}

JNIEXPORT jint JNICALL Java_tel_schich_javacan_SocketCAN_getAllowFDFrames(JNIEnv *env, jclass clazz, jint sock) {
jint result = get_boolean_opt(sock, CAN_RAW_FD_FRAMES);
jint result = get_boolean_opt(sock, SOL_CAN_RAW, CAN_RAW_FD_FRAMES);
if (result == -1) {
throw_native_exception(env, "Unable to get FD frame support");
}
Expand Down

0 comments on commit d3753cb

Please sign in to comment.