12 #include "../src/meshlink.h"
15 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
25 static struct sync_flag poll_flag;
26 static size_t poll_len;
28 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
29 meshlink_set_channel_poll_cb(mesh, channel, NULL);
31 set_sync_flag(&poll_flag, true);
34 static struct sync_flag receive_flag;
35 static size_t receive_len;
37 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
43 set_sync_flag(&receive_flag, true);
47 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
49 // Open two meshlink instances.
51 meshlink_handle_t *mesh_a, *mesh_b;
52 open_meshlink_pair(&mesh_a, &mesh_b, "channels_failure");
56 meshlink_set_channel_accept_cb(mesh_b, accept_cb);
58 // Open a channel from a to b
60 meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
63 meshlink_channel_t *channel = meshlink_channel_open(mesh_a, b, 7, receive_cb, NULL, 0);
66 meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
68 // Start both instances
70 start_meshlink_pair(mesh_a, mesh_b);
72 // Wait for the channel to be established
74 assert(wait_sync_flag(&poll_flag, 10));
75 assert(poll_len != 0);
79 // Set a very small timeout for channels to b.
81 meshlink_set_node_channel_timeout(mesh_a, b, 1);
83 // Stop mesh_b. We should get a notification that the channel has closed after a while.
85 meshlink_stop(mesh_b);
87 assert(wait_sync_flag(&receive_flag, 5));
88 assert(receive_len == 0);
90 meshlink_channel_close(mesh_a, channel);
92 // Try setting up a new channel while b is still down.
94 set_sync_flag(&poll_flag, false);
95 set_sync_flag(&receive_flag, false);
97 channel = meshlink_channel_open(mesh_a, b, 7, NULL, NULL, 0);
100 meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
102 assert(wait_sync_flag(&poll_flag, 5));
103 assert(poll_len == 0);
105 meshlink_channel_close(mesh_a, channel);
107 // Restart b and create a new channel
109 set_sync_flag(&poll_flag, false);
110 set_sync_flag(&receive_flag, false);
112 meshlink_set_node_channel_timeout(mesh_a, b, 60);
114 assert(meshlink_start(mesh_b));
116 channel = meshlink_channel_open(mesh_a, b, 7, receive_cb, NULL, 0);
117 meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
119 assert(wait_sync_flag(&poll_flag, 10));
120 assert(poll_len != 0);
122 // Close and reopen b, we should get a fast notification that the channel has been closed.
124 meshlink_close(mesh_b);
125 mesh_b = meshlink_open("channels_failure_conf.2", "b", "channels_failure", DEV_CLASS_BACKBONE);
127 assert(meshlink_start(mesh_b));
129 assert(wait_sync_flag(&receive_flag, 10));
130 assert(receive_len == 0);
134 close_meshlink_pair(mesh_a, mesh_b);