mesh.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "contiki.h"
00047 #include "net/rime.h"
00048 #include "net/rime/route.h"
00049 #include "net/rime/mesh.h"
00050
00051 #include <stddef.h>
00052
00053 #define PACKET_TIMEOUT (CLOCK_SECOND * 10)
00054
00055 #define DEBUG 0
00056 #if DEBUG
00057 #include <stdio.h>
00058 #define PRINTF(...) printf(__VA_ARGS__)
00059 #else
00060 #define PRINTF(...)
00061 #endif
00062
00063
00064 static void
00065 data_packet_received(struct multihop_conn *multihop,
00066 const rimeaddr_t *from,
00067 const rimeaddr_t *prevhop, uint8_t hops)
00068 {
00069 struct mesh_conn *c = (struct mesh_conn *)
00070 ((char *)multihop - offsetof(struct mesh_conn, multihop));
00071
00072 struct route_entry *rt;
00073
00074
00075 rt = route_lookup(from);
00076 if(rt != NULL) {
00077 route_refresh(rt);
00078 }
00079
00080 if(c->cb->recv) {
00081 c->cb->recv(c, from, hops);
00082 }
00083 }
00084
00085 static rimeaddr_t *
00086 data_packet_forward(struct multihop_conn *multihop,
00087 const rimeaddr_t *originator,
00088 const rimeaddr_t *dest,
00089 const rimeaddr_t *prevhop, uint8_t hops)
00090 {
00091 struct route_entry *rt;
00092
00093 rt = route_lookup(dest);
00094 if(rt == NULL) {
00095 return NULL;
00096 }
00097
00098 return &rt->nexthop;
00099 }
00100
00101 static void
00102 found_route(struct route_discovery_conn *rdc, const rimeaddr_t *dest)
00103 {
00104 struct mesh_conn *c = (struct mesh_conn *)
00105 ((char *)rdc - offsetof(struct mesh_conn, route_discovery_conn));
00106
00107 if(c->queued_data != NULL &&
00108 rimeaddr_cmp(dest, &c->queued_data_dest)) {
00109 queuebuf_to_packetbuf(c->queued_data);
00110 queuebuf_free(c->queued_data);
00111 c->queued_data = NULL;
00112 if(multihop_send(&c->multihop, dest)) {
00113 c->cb->sent(c);
00114 } else {
00115 c->cb->timedout(c);
00116 }
00117 }
00118 }
00119
00120 static void
00121 route_timed_out(struct route_discovery_conn *rdc)
00122 {
00123 struct mesh_conn *c = (struct mesh_conn *)
00124 ((char *)rdc - offsetof(struct mesh_conn, route_discovery_conn));
00125
00126 if(c->queued_data != NULL) {
00127 queuebuf_free(c->queued_data);
00128 c->queued_data = NULL;
00129 }
00130
00131 if(c->cb->timedout) {
00132 c->cb->timedout(c);
00133 }
00134 }
00135
00136 static const struct multihop_callbacks data_callbacks = { data_packet_received,
00137 data_packet_forward };
00138 static const struct route_discovery_callbacks route_discovery_callbacks =
00139 { found_route, route_timed_out };
00140
00141 void
00142 mesh_open(struct mesh_conn *c, uint16_t channels,
00143 const struct mesh_callbacks *callbacks)
00144 {
00145 route_init();
00146 multihop_open(&c->multihop, channels, &data_callbacks);
00147 route_discovery_open(&c->route_discovery_conn,
00148 CLOCK_SECOND * 2,
00149 channels + 1,
00150 &route_discovery_callbacks);
00151 c->cb = callbacks;
00152 }
00153
00154 void
00155 mesh_close(struct mesh_conn *c)
00156 {
00157 multihop_close(&c->multihop);
00158 route_discovery_close(&c->route_discovery_conn);
00159 }
00160
00161 int
00162 mesh_send(struct mesh_conn *c, const rimeaddr_t *to)
00163 {
00164 int could_send;
00165
00166 PRINTF("%d.%d: mesh_send to %d.%d\n",
00167 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
00168 to->u8[0], to->u8[1]);
00169
00170 could_send = multihop_send(&c->multihop, to);
00171
00172 if(!could_send) {
00173 if(c->queued_data != NULL) {
00174 queuebuf_free(c->queued_data);
00175 }
00176
00177 PRINTF("mesh_send: queueing data, sending rreq\n");
00178 c->queued_data = queuebuf_new_from_packetbuf();
00179 rimeaddr_copy(&c->queued_data_dest, to);
00180 route_discovery_discover(&c->route_discovery_conn, to,
00181 PACKET_TIMEOUT);
00182 return 0;
00183 }
00184 c->cb->sent(c);
00185 return 1;
00186 }
00187
00188