50 #define ROUTE_TRICKLE_INTERVAL CLOCK_SECOND * 32
51 #define ROUTE_DISCOVERY_INTERVAL CLOCK_SECOND * 4
52 #define ROUTE_TIMEOUT CLOCK_SECOND * 4
54 static struct queuebuf *queued_packet;
55 static rimeaddr_t queued_receiver;
58 static struct route_discovery_conn route_discovery;
61 static struct unicast_conn dataconn;
65 static struct trickle_conn gateway_announce_conn;
70 #define PRINTF(...) printf(__VA_ARGS__)
75 #define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
78 static rimeaddr_t gateway;
83 recv_data(
struct unicast_conn *c,
const rimeaddr_t *from)
85 struct route_entry *e;
90 source.u8[0] = BUF->srcipaddr.u8[2];
91 source.u8[1] = BUF->srcipaddr.u8[3];
93 e = route_lookup(&source);
95 route_add(&source, from, 10, 0);
103 e = route_lookup(&gateway);
111 PRINTF(
"uip-over-mesh: %d.%d: recv_data with len %d\n",
117 send_data(rimeaddr_t *
next)
119 PRINTF(
"uip-over-mesh: %d.%d: send_data with len %d\n",
122 unicast_send(&dataconn, next);
126 new_route(
struct route_discovery_conn *c,
const rimeaddr_t *to)
128 struct route_entry *rt;
131 PRINTF(
"uip-over-mesh: new route, sending queued packet\n");
133 queuebuf_to_packetbuf(queued_packet);
134 queuebuf_free(queued_packet);
135 queued_packet =
NULL;
137 rt = route_lookup(&queued_receiver);
140 send_data(&queued_receiver);
146 timedout(
struct route_discovery_conn *c)
148 PRINTF(
"uip-over-mesh: packet timed out\n");
150 PRINTF(
"uip-over-mesh: freeing queued packet\n");
151 queuebuf_free(queued_packet);
152 queued_packet =
NULL;
156 static const struct unicast_callbacks data_callbacks = { recv_data };
157 static const struct route_discovery_callbacks rdc = { new_route, timedout };
163 static uint8_t is_gateway;
166 gateway_announce_recv(
struct trickle_conn *c)
168 struct gateway_msg *msg;
170 PRINTF(
"%d.%d: gateway message: %d.%d\n",
172 msg->gateway.u8[0], msg->gateway.u8[1]);
175 uip_over_mesh_set_gateway(&msg->gateway);
181 uip_over_mesh_make_announced_gateway(
void)
183 struct gateway_msg msg;
187 PRINTF(
"%d.%d: making myself the gateway\n",
192 trickle_send(&gateway_announce_conn);
196 const static struct trickle_callbacks trickle_call = {gateway_announce_recv};
199 uip_over_mesh_init(u16_t channels)
202 PRINTF(
"Our address is %d.%d (%d.%d.%d.%d)\n",
204 uip_hostaddr.u8[0], uip_hostaddr.u8[1],
205 uip_hostaddr.u8[2], uip_hostaddr.u8[3]);
207 unicast_open(&dataconn, channels, &data_callbacks);
208 route_discovery_open(&route_discovery, ROUTE_DISCOVERY_INTERVAL,
210 trickle_open(&gateway_announce_conn, ROUTE_TRICKLE_INTERVAL, channels + 3,
215 route_set_lifetime(30);
219 uip_over_mesh_send(
void)
222 struct route_entry *rt;
234 receiver.u8[0] = BUF->destipaddr.u8[2];
235 receiver.u8[1] = BUF->destipaddr.u8[3];
238 PRINTF(
"uip_over_mesh_send: I am gateway, packet to %d.%d.%d.%d to local interface\n",
240 if(gw_netif !=
NULL) {
241 return gw_netif->
output();
245 PRINTF(
"uip_over_mesh_send: No gateway setup, dropping packet\n");
248 PRINTF(
"uip_over_mesh_send: forwarding packet to %d.%d.%d.%d towards gateway %d.%d\n",
250 gateway.u8[0], gateway.u8[1]);
255 PRINTF(
"uIP over mesh send to %d.%d with len %d\n",
256 receiver.u8[0], receiver.u8[1],
266 if(BUF->proto == UIP_PROTO_TCP) {
267 packetbuf_set_attr(PACKETBUF_ATTR_ERELIABLE, 1);
268 packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, 1);
272 rt = route_lookup(&receiver);
274 PRINTF(
"uIP over mesh no route to %d.%d\n", receiver.u8[0], receiver.u8[1]);
275 if(queued_packet ==
NULL) {
276 queued_packet = queuebuf_new_from_packetbuf();
278 route_discovery_discover(&route_discovery, &receiver, ROUTE_TIMEOUT);
280 route_discovery_discover(&route_discovery, &receiver, ROUTE_TIMEOUT);
284 send_data(&rt->nexthop);
296 uip_over_mesh_set_gateway(rimeaddr_t *gw)