aboutsummaryrefslogtreecommitdiff
path: root/src/mesh/node.c
blob: 2ac921a5942bccffc3efee2febb9f0c80751bb99 (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <gran/mesh/node.h>

struct reg {
	struct packet pkt;
	bool busy;
};

struct node {
	struct component component;
	uint16_t x, y;

	uint64_t timestamp;

	struct component *n, *s, *e, *w, *l;

	struct reg n_in, s_in, e_in, w_in, l_in;
};

enum order {
	N, S, E, W, L
};

static inline void maybe_pick(struct reg *output[5], enum order d, struct reg *r)
{
	if (output[d] && output[d]->pkt.timestamp < r->pkt.timestamp)
		return;

	output[d] = r;
}

static stat node_clock(struct node *node)
{
	node->timestamp++;

	struct reg *output[5] = {NULL, NULL, NULL, NULL, NULL};
	struct reg *input[5] = {
		&node->n_in,
		&node->s_in,
		&node->e_in,
		&node->w_in,
		&node->l_in
	};

	uint8_t X = node->x, Y = node->y;
	for (size_t i = 0; i < 5; ++i) {
		struct reg *r = input[i];
		if (!r->busy)
			continue;

		uint16_t x, y;
		addr_mesh(r->pkt.to, &x, &y, NULL);
		if (x < X) {
			maybe_pick(output, W, r);
			continue;
		}

		if (x > X) {
			maybe_pick(output, E, r);
			continue;
		}

		if (y < Y) {
			maybe_pick(output, S, r);
			continue;
		}

		if (y > Y) {
			maybe_pick(output, N, r);
			continue;
		}

		maybe_pick(output, L, r);
	}

	struct component *target[7] = {
		node->n,
		node->s,
		node->e,
		node->w,
		node->l
	};

	for (size_t i = 0; i < 5; ++i) {
		if (!output[i])
			continue;

		if (!target[i]) {
			/* for now, should send packet back with an error or something */
			abort();
		}

		stat ret = SEND(node, target[i], output[i]->pkt);
		if (ret == EBUSY)
			continue;

		assert(ret == OK);
		output[i]->busy = false;
	}

	return OK;
}

static stat reg_receive(struct reg *r, struct packet pkt)
{
	if (r->busy)
		return EBUSY;

	r->pkt = pkt;
	r->busy = true;
	return OK;
}

static stat node_receive(struct node *node, struct component *from, struct packet pkt)
{
	if (from == node->l) {
		/* add time when packet entered network */
		pkt.timestamp = node->timestamp;
		return reg_receive(&node->l_in, pkt);
	}

	if (from == node->n)
		return reg_receive(&node->n_in, pkt);

	if (from == node->s)
		return reg_receive(&node->s_in, pkt);

	if (from == node->e)
		return reg_receive(&node->e_in, pkt);

	if (from == node->w)
		return reg_receive(&node->w_in, pkt);

	abort();
	return OK;
}

struct component *create_mesh_node(uint16_t x, uint16_t y)
{
	struct node *node = calloc(1, sizeof(struct node));
	if (!node)
		return NULL;

	node->component.receive = (receive_callback)node_receive;
	node->component.clock = (clock_callback)node_clock;
	node->x = x;
	node->y = y;
	return (struct component *)node;
}

stat mesh_node_connect(struct component *node,
		struct component *n,
		struct component *s,
		struct component *e,
		struct component *w,
		struct component *l)
{
	struct node *nod = (struct node *)node;
	nod->n = n;
	nod->s = s;
	nod->e = e;
	nod->w = w;
	nod->l = l;
	return OK;
}