11 #include "../src/meshlink.h"
14 static volatile bool b_responded = false;
15 static volatile bool b_closed = false;
16 static volatile size_t a_poll_cb_len;
18 static void a_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
22 if(len == 5 && !memcmp(data, "Hello", 5)) {
26 set_sync_flag(channel->priv, true);
30 static void b_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
31 // Send one message back, then close the channel.
33 meshlink_channel_send(mesh, channel, data, len);
36 meshlink_channel_close(mesh, channel);
39 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
49 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
52 meshlink_set_channel_accept_cb(mesh, NULL);
53 meshlink_set_channel_receive_cb(mesh, channel, b_receive_cb);
56 b_receive_cb(mesh, channel, data, len);
62 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
65 meshlink_set_channel_poll_cb(mesh, channel, NULL);
66 set_sync_flag(channel->priv, true);
69 static void poll_cb2(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
74 meshlink_set_channel_poll_cb(mesh, channel, NULL);
75 set_sync_flag(channel->priv, true);
79 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
81 meshlink_handle_t *a, *b;
82 open_meshlink_pair(&a, &b, "channels-cornercases");
86 meshlink_set_channel_accept_cb(a, reject_cb);
87 meshlink_set_channel_accept_cb(b, accept_cb);
89 // Open a channel from a to b before starting the mesh.
91 meshlink_node_t *nb = meshlink_get_node(a, "b");
94 struct sync_flag channel_opened = {.flag = false};
96 meshlink_channel_t *channel = meshlink_channel_open(a, nb, 7, a_receive_cb, &channel_opened, 0);
99 meshlink_set_channel_poll_cb(a, channel, poll_cb);
101 // Start MeshLink and wait for the channel to become connected.
102 start_meshlink_pair(a, b);
104 assert(wait_sync_flag(&channel_opened, 20));
106 // Re-initialize everything
107 meshlink_channel_close(a, channel);
108 close_meshlink_pair(a, b);
111 channel_opened.flag = false;
112 open_meshlink_pair(&a, &b, "channels-cornercases");
114 meshlink_set_channel_accept_cb(a, reject_cb);
115 meshlink_set_channel_accept_cb(b, accept_cb);
117 start_meshlink_pair(a, b);
119 // Create a channel to b
120 nb = meshlink_get_node(a, "b");
123 channel = meshlink_channel_open(a, nb, 7, a_receive_cb, &channel_opened, 0);
125 meshlink_set_channel_poll_cb(a, channel, poll_cb);
127 assert(wait_sync_flag(&channel_opened, 20));
129 assert(!b_responded);
132 // Send a message to b
134 struct sync_flag channel_closed = {.flag = false};
135 channel->priv = &channel_closed;
137 meshlink_channel_send(a, channel, "Hello", 5);
139 assert(wait_sync_flag(&channel_closed, 20));
143 // Try to create a second channel
145 struct sync_flag channel_polled = {.flag = false};
147 meshlink_channel_t *channel2 = meshlink_channel_open(a, nb, 7, a_receive_cb, &channel_polled, 0);
149 meshlink_set_channel_poll_cb(a, channel2, poll_cb2);
151 assert(wait_sync_flag(&channel_polled, 20));
153 assert(0 == a_poll_cb_len);
155 meshlink_channel_close(a, channel);
156 meshlink_channel_close(a, channel2);
157 close_meshlink_pair(a, b);