15 #include "../src/meshlink.h"
18 static volatile bool b_responded = false;
19 static volatile bool b_closed = false;
20 static volatile size_t a_poll_cb_len;
22 static void a_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
26 if(len == 5 && !memcmp(data, "Hello", 5)) {
30 set_sync_flag(channel->priv, true);
34 static void b_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
35 // Send one message back, then close the channel.
37 assert(meshlink_channel_send(mesh, channel, data, len) == (ssize_t)len);
40 meshlink_channel_close(mesh, channel);
43 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
46 meshlink_set_channel_accept_cb(mesh, NULL);
47 meshlink_set_channel_receive_cb(mesh, channel, b_receive_cb);
50 b_receive_cb(mesh, channel, data, len);
56 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
59 meshlink_set_channel_poll_cb(mesh, channel, NULL);
60 set_sync_flag(channel->priv, true);
63 static void poll_cb2(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
68 meshlink_set_channel_poll_cb(mesh, channel, NULL);
69 set_sync_flag(channel->priv, true);
73 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
75 meshlink_handle_t *a, *b;
76 open_meshlink_pair(&a, &b, "channels-cornercases");
80 meshlink_set_channel_accept_cb(b, accept_cb);
82 // Open a channel from a to b before starting the mesh.
84 meshlink_node_t *nb = meshlink_get_node(a, "b");
87 struct sync_flag channel_opened = {.flag = false};
88 init_sync_flag(&channel_opened);
90 meshlink_channel_t *channel = meshlink_channel_open(a, nb, 7, a_receive_cb, &channel_opened, 0);
93 meshlink_set_channel_poll_cb(a, channel, poll_cb);
95 // Start MeshLink and wait for the channel to become connected.
96 start_meshlink_pair(a, b);
98 assert(wait_sync_flag(&channel_opened, 20));
100 // Re-initialize everything
101 meshlink_channel_close(a, channel);
102 close_meshlink_pair(a, b);
105 channel_opened.flag = false;
106 open_meshlink_pair(&a, &b, "channels-cornercases");
108 meshlink_set_channel_accept_cb(b, accept_cb);
110 start_meshlink_pair(a, b);
112 // Create a channel to b
113 nb = meshlink_get_node(a, "b");
116 channel = meshlink_channel_open(a, nb, 7, a_receive_cb, &channel_opened, 0);
118 meshlink_set_channel_poll_cb(a, channel, poll_cb);
120 assert(wait_sync_flag(&channel_opened, 20));
122 assert(!b_responded);
125 // Send a message to b
127 struct sync_flag channel_closed = {.flag = false};
128 init_sync_flag(&channel_closed);
129 channel->priv = &channel_closed;
131 assert(meshlink_channel_send(a, channel, "Hello", 5) == 5);
132 assert(wait_sync_flag(&channel_closed, 20));
136 // Try to create a second channel
138 struct sync_flag channel_polled = {.flag = false};
139 init_sync_flag(&channel_polled);
141 meshlink_channel_t *channel2 = meshlink_channel_open(a, nb, 7, a_receive_cb, &channel_polled, 0);
143 meshlink_set_channel_poll_cb(a, channel2, poll_cb2);
145 assert(wait_sync_flag(&channel_polled, 20));
147 assert(0 == a_poll_cb_len);
149 meshlink_channel_close(a, channel);
150 meshlink_channel_close(a, channel2);
151 close_meshlink_pair(a, b);