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, NULL, 0);
99 channel->priv = &channel_opened;
100 meshlink_set_channel_poll_cb(a, channel, poll_cb);
102 // Start MeshLink and wait for the channel to become connected.
103 start_meshlink_pair(a, b);
105 assert(wait_sync_flag(&channel_opened, 20));
107 // Re-initialize everything
108 meshlink_channel_close(a, channel);
109 close_meshlink_pair(a, b);
112 channel_opened.flag = false;
113 open_meshlink_pair(&a, &b, "channels-cornercases");
115 meshlink_set_channel_accept_cb(a, reject_cb);
116 meshlink_set_channel_accept_cb(b, accept_cb);
118 start_meshlink_pair(a, b);
120 // Create a channel to b
121 nb = meshlink_get_node(a, "b");
124 channel = meshlink_channel_open(a, nb, 7, a_receive_cb, NULL, 0);
126 channel->priv = &channel_opened;
127 meshlink_set_channel_poll_cb(a, channel, poll_cb);
129 assert(wait_sync_flag(&channel_opened, 20));
131 assert(!b_responded);
134 // Send a message to b
136 struct sync_flag channel_closed = {.flag = false};
137 channel->priv = &channel_closed;
139 meshlink_channel_send(a, channel, "Hello", 5);
141 assert(wait_sync_flag(&channel_closed, 20));
145 // Try to create a second channel
147 struct sync_flag channel_polled = {.flag = false};
149 meshlink_channel_t *channel2 = meshlink_channel_open(a, nb, 7, a_receive_cb, NULL, 0);
151 channel2->priv = &channel_polled;
152 meshlink_set_channel_poll_cb(a, channel2, poll_cb2);
154 assert(wait_sync_flag(&channel_polled, 20));
156 assert(0 == a_poll_cb_len);
158 meshlink_channel_close(a, channel);
159 meshlink_channel_close(a, channel2);
160 close_meshlink_pair(a, b);