rmh.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/rmh.h"
00049
00050 struct data_hdr {
00051 rimeaddr_t dest;
00052 rimeaddr_t originator;
00053 uint8_t hops;
00054 uint8_t max_rexmits;
00055 };
00056
00057 #define DEBUG 0
00058 #if DEBUG
00059 #include <stdio.h>
00060 #define PRINTF(...) printf(__VA_ARGS__)
00061 #else
00062 #define PRINTF(...)
00063 #endif
00064
00065
00066 static void
00067 received(struct runicast_conn *uc, const rimeaddr_t *from, uint8_t seqno)
00068 {
00069 struct rmh_conn *c = (struct rmh_conn *)uc;
00070 struct data_hdr *msg = packetbuf_dataptr();
00071 rimeaddr_t *nexthop;
00072
00073 PRINTF("data_packet_received from %d.%d towards %d.%d len %d\n",
00074 from->u8[0], from->u8[1],
00075 msg->dest.u8[0], msg->dest.u8[1],
00076 packetbuf_datalen());
00077
00078 if(rimeaddr_cmp(&msg->dest, &rimeaddr_node_addr)) {
00079 PRINTF("for us!\n");
00080 packetbuf_hdrreduce(sizeof(struct data_hdr));
00081 if(c->cb->recv) {
00082 c->cb->recv(c, &msg->originator, msg->hops);
00083 }
00084 } else {
00085 nexthop = NULL;
00086 if(c->cb->forward) {
00087 nexthop = c->cb->forward(c, &msg->originator,
00088 &msg->dest, from, msg->hops);
00089 }
00090 if(nexthop) {
00091 PRINTF("forwarding to %d.%d\n", nexthop->u8[0], nexthop->u8[1]);
00092 msg->hops++;
00093 runicast_send(&c->c, nexthop, c->num_rexmit);
00094 }
00095 }
00096 }
00097
00098 static void
00099 sent(struct runicast_conn *c, const rimeaddr_t *to, uint8_t retransmissions)
00100 {
00101
00102 }
00103
00104 static void
00105 timedout(struct runicast_conn *c, const rimeaddr_t *to, uint8_t retransmissions)
00106 {
00107
00108 }
00109
00110 static const struct runicast_callbacks data_callbacks = { received ,
00111 sent,
00112 timedout};
00113
00114 void
00115 rmh_open(struct rmh_conn *c, uint16_t channel,
00116 const struct rmh_callbacks *callbacks)
00117 {
00118 runicast_open(&c->c, channel, &data_callbacks);
00119 c->cb = callbacks;
00120 }
00121
00122 void
00123 rmh_close(struct rmh_conn *c)
00124 {
00125 runicast_close(&c->c);
00126 }
00127
00128 int
00129 rmh_send(struct rmh_conn *c, rimeaddr_t *to, uint8_t num_rexmit, uint8_t max_hops)
00130 {
00131 rimeaddr_t *nexthop;
00132 struct data_hdr *hdr;
00133
00134 c->num_rexmit = num_rexmit;
00135
00136 if(c->cb->forward == NULL) {
00137 return 0;
00138 }
00139
00140 nexthop = c->cb->forward(c, &rimeaddr_node_addr, to, NULL, 0);
00141 if(nexthop == NULL) {
00142 PRINTF("rmh_send: no route\n");
00143 return 0;
00144 } else {
00145 PRINTF("rmh_send: sending data\n");
00146
00147
00148 if(packetbuf_hdralloc(sizeof(struct data_hdr))) {
00149 hdr = packetbuf_hdrptr();
00150 rimeaddr_copy(&hdr->dest, to);
00151 rimeaddr_copy(&hdr->originator, &rimeaddr_node_addr);
00152 hdr->hops = 1;
00153 hdr->max_rexmits = num_rexmit;
00154 runicast_send(&c->c, nexthop, num_rexmit);
00155 }
00156 return 1;
00157 }
00158 }
00159
00160