utcp-benchmark \
utcp-benchmark-stream
+TESTS += \
+ api_set_node_status_cb
+
if BLACKBOX_TESTS
SUBDIRS = blackbox
endif
AM_LDFLAGS = $(PTHREAD_LIBS)
check_PROGRAMS = \
+ api_set_node_status_cb \
basic \
basicpp \
blacklist \
bin_PROGRAMS = $(check_PROGRAMS)
endif
+api_set_node_status_cb_SOURCES = api_set_node_status_cb.c utils.c utils.h
+api_set_node_status_cb_LDADD = $(top_builddir)/src/libmeshlink.la
+
basic_SOURCES = basic.c utils.c utils.h
basic_LDADD = $(top_builddir)/src/libmeshlink.la
--- /dev/null
+#ifdef NDEBUG
+#undef NDEBUG
+#endif
+
+#include <assert.h>
+#include <string.h>
+
+#include "meshlink.h"
+#include "utils.h"
+
+static struct sync_flag a_reachable;
+static struct sync_flag b_reachable;
+
+static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
+ (void)mesh;
+
+ if(!reachable) {
+ return;
+ }
+
+ if(!strcmp(node->name, "a")) {
+ set_sync_flag(&a_reachable, true);
+ } else if(!strcmp(node->name, "b")) {
+ set_sync_flag(&b_reachable, true);
+ }
+}
+
+int main(void) {
+ meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
+
+ init_sync_flag(&a_reachable);
+ init_sync_flag(&b_reachable);
+
+ meshlink_handle_t *mesh1, *mesh2;
+ open_meshlink_pair_ephemeral(&mesh1, &mesh2, "api_set_node_status_cb");
+
+ // Test case #1: check that setting a valid status callback will cause it to be called
+ // when the node itself is started or stopped
+
+ meshlink_set_node_status_cb(mesh1, status_cb);
+ assert(meshlink_start(mesh1));
+ assert(wait_sync_flag(&a_reachable, 5));
+
+ // Test case #2: check that the status callback will be called when another peer is started
+
+ assert(meshlink_start(mesh2));
+ assert(wait_sync_flag(&b_reachable, 5));
+
+ // Test case #3: check that passing a NULL pointer for the mesh returns an error
+
+ meshlink_errno = MESHLINK_OK;
+ meshlink_set_node_status_cb(NULL, status_cb);
+ assert(meshlink_errno == MESHLINK_EINVAL);
+
+ // Done.
+
+ close_meshlink_pair(mesh1, mesh2);
+}
*pb = b;
}
+void open_meshlink_pair_ephemeral(meshlink_handle_t **pa, meshlink_handle_t **pb, const char *prefix) {
+ // Create two new MeshLink instances
+
+ *pa = *pb = NULL;
+
+ meshlink_handle_t *a = meshlink_open_ephemeral("a", prefix, DEV_CLASS_BACKBONE);
+ meshlink_handle_t *b = meshlink_open_ephemeral("b", prefix, DEV_CLASS_BACKBONE);
+
+ assert(a);
+ assert(b);
+
+ meshlink_enable_discovery(a, false);
+ meshlink_enable_discovery(b, false);
+
+ link_meshlink_pair(a, b);
+
+ *pa = a;
+ *pb = b;
+}
+
// Don't poll in the application thread, use a condition variable to signal when the peer is online.
static void pair_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
(void)node;
/// Create a pair of meshlink instances that are already joined together.
extern void open_meshlink_pair(meshlink_handle_t **a, meshlink_handle_t **b, const char *prefix);
+extern void open_meshlink_pair_ephemeral(meshlink_handle_t **a, meshlink_handle_t **b, const char *prefix);
/// Start a pair of meshlink instances and wait for them to connect together.
extern void start_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b);