-
Notifications
You must be signed in to change notification settings - Fork 31
/
net_slink.c
59 lines (46 loc) · 1.15 KB
/
net_slink.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "mesh.h"
#include "net_serial.h"
#include "ext.h"
#include "unit_test.h"
int reader(void)
{
uint8_t c1;
if(read(0, &c1, 1) <= 0) return -1;
return c1;
}
int writer(uint8_t *buf, size_t len)
{
return write(1, buf, len);
}
static uint8_t status = 0;
// exit as soon as the link is up
void link_check(link_t link)
{
LOG("CHECK %s",hashname_char(link->id));
status = link_up(link) ? 1 : 0;
}
int main(int argc, char *argv[])
{
mesh_t mesh;
net_serial_t s;
mesh = mesh_new();
fail_unless(mesh_generate(mesh));
mesh_on_discover(mesh,"auto",mesh_add); // accept anyone
mesh_on_link(mesh, "test", link_check); // testing the event being triggered
status = 0;
s = net_serial_new(mesh, NULL);
net_serial_add(s, "stdout", reader, writer, 64);
fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK); // nonblocking reads
setvbuf(stdout, NULL, _IONBF, 0); // no buffering
net_serial_send(s, "stdout", mesh_json(mesh));
LOG("entering loop");
while(net_serial_loop(s) && !status);
LOG("success!");
return 0;
}