multihop.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/multihop.h"
00049 #include "net/rime/route.h"
00050
00051 #include <string.h>
00052
00053 static const struct packetbuf_attrlist attributes[] =
00054 {
00055 MULTIHOP_ATTRIBUTES
00056 PACKETBUF_ATTR_LAST
00057 };
00058
00059 #define DEBUG 0
00060 #if DEBUG
00061 #include <stdio.h>
00062 #define PRINTF(...) printf(__VA_ARGS__)
00063 #else
00064 #define PRINTF(...)
00065 #endif
00066
00067
00068 void
00069 data_packet_received(struct unicast_conn *uc, const rimeaddr_t *from)
00070 {
00071 struct multihop_conn *c = (struct multihop_conn *)uc;
00072 rimeaddr_t *nexthop;
00073 rimeaddr_t sender, receiver;
00074
00075
00076
00077
00078 rimeaddr_copy(&sender, packetbuf_addr(PACKETBUF_ADDR_ESENDER));
00079 rimeaddr_copy(&receiver, packetbuf_addr(PACKETBUF_ADDR_ERECEIVER));
00080
00081 PRINTF("data_packet_received from %d.%d towards %d.%d len %d\n",
00082 from->u8[0], from->u8[1],
00083 packetbuf_addr(PACKETBUF_ADDR_ERECEIVER)->u8[0],
00084 packetbuf_addr(PACKETBUF_ADDR_ERECEIVER)->u8[1],
00085 packetbuf_datalen());
00086
00087 if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_ERECEIVER),
00088 &rimeaddr_node_addr)) {
00089 PRINTF("for us!\n");
00090 if(c->cb->recv) {
00091 c->cb->recv(c, &sender, from,
00092 packetbuf_attr(PACKETBUF_ATTR_HOPS));
00093 }
00094 } else {
00095 nexthop = NULL;
00096 if(c->cb->forward) {
00097 nexthop = c->cb->forward(c, &sender, &receiver,
00098 from, packetbuf_attr(PACKETBUF_ATTR_HOPS));
00099
00100 packetbuf_set_attr(PACKETBUF_ATTR_HOPS,
00101 packetbuf_attr(PACKETBUF_ATTR_HOPS) + 1);
00102 }
00103 if(nexthop) {
00104 PRINTF("forwarding to %d.%d\n", nexthop->u8[0], nexthop->u8[1]);
00105 unicast_send(&c->c, nexthop);
00106 }
00107 }
00108 }
00109
00110 static const struct unicast_callbacks data_callbacks = { data_packet_received };
00111
00112 void
00113 multihop_open(struct multihop_conn *c, uint16_t channel,
00114 const struct multihop_callbacks *callbacks)
00115 {
00116 unicast_open(&c->c, channel, &data_callbacks);
00117 channel_set_attributes(channel, attributes);
00118 c->cb = callbacks;
00119 }
00120
00121 void
00122 multihop_close(struct multihop_conn *c)
00123 {
00124 unicast_close(&c->c);
00125 }
00126
00127 int
00128 multihop_send(struct multihop_conn *c, const rimeaddr_t *to)
00129 {
00130 rimeaddr_t *nexthop;
00131
00132 if(c->cb->forward == NULL) {
00133 return 0;
00134 }
00135 packetbuf_compact();
00136 nexthop = c->cb->forward(c, &rimeaddr_node_addr, to, NULL, 0);
00137
00138 if(nexthop == NULL) {
00139 PRINTF("multihop_send: no route\n");
00140 return 0;
00141 } else {
00142 PRINTF("multihop_send: sending data towards %d.%d\n",
00143 nexthop->u8[0], nexthop->u8[1]);
00144 packetbuf_set_addr(PACKETBUF_ADDR_ERECEIVER, to);
00145 packetbuf_set_addr(PACKETBUF_ADDR_ESENDER, &rimeaddr_node_addr);
00146 packetbuf_set_attr(PACKETBUF_ATTR_HOPS, 1);
00147 unicast_send(&c->c, nexthop);
00148 return 1;
00149 }
00150 }
00151
00152