rime.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 #define DEBUG 0
00047 #if DEBUG
00048 #include <stdio.h>
00049 #define PRINTF(...) printf(__VA_ARGS__)
00050 #else
00051 #define PRINTF(...)
00052 #endif
00053
00054 #include "net/netstack.h"
00055 #include "net/rime.h"
00056 #include "net/rime/chameleon.h"
00057 #include "net/rime/route.h"
00058 #include "net/rime/announcement.h"
00059 #include "net/rime/broadcast-announcement.h"
00060 #include "net/mac/mac.h"
00061
00062 #include "lib/list.h"
00063
00064 const struct mac_driver *rime_mac;
00065
00066 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL
00067 #define BROADCAST_ANNOUNCEMENT_CHANNEL RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL
00068 #else
00069 #define BROADCAST_ANNOUNCEMENT_CHANNEL 2
00070 #endif
00071
00072 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME
00073 #define BROADCAST_ANNOUNCEMENT_BUMP_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME
00074 #else
00075 #define BROADCAST_ANNOUNCEMENT_BUMP_TIME CLOCK_SECOND * 8
00076 #endif
00077
00078 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME
00079 #define BROADCAST_ANNOUNCEMENT_MIN_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME
00080 #else
00081 #define BROADCAST_ANNOUNCEMENT_MIN_TIME CLOCK_SECOND * 60
00082 #endif
00083
00084 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME
00085 #define BROADCAST_ANNOUNCEMENT_MAX_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME
00086 #else
00087 #define BROADCAST_ANNOUNCEMENT_MAX_TIME CLOCK_SECOND * 3600UL
00088 #endif
00089
00090
00091 LIST(sniffers);
00092
00093
00094 void
00095 rime_sniffer_add(struct rime_sniffer *s)
00096 {
00097 list_add(sniffers, s);
00098 }
00099
00100 void
00101 rime_sniffer_remove(struct rime_sniffer *s)
00102 {
00103 list_remove(sniffers, s);
00104 }
00105
00106 static void
00107 input(void)
00108 {
00109 struct rime_sniffer *s;
00110 struct channel *c;
00111
00112 RIMESTATS_ADD(rx);
00113 c = chameleon_parse();
00114
00115 for(s = list_head(sniffers); s != NULL; s = list_item_next(s)) {
00116 if(s->input_callback != NULL) {
00117 s->input_callback();
00118 }
00119 }
00120
00121 if(c != NULL) {
00122 abc_input(c);
00123 }
00124 }
00125
00126 static void
00127 init(void)
00128 {
00129 queuebuf_init();
00130 packetbuf_clear();
00131 announcement_init();
00132
00133 rime_mac = &NETSTACK_MAC;
00134 chameleon_init();
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 broadcast_announcement_init(BROADCAST_ANNOUNCEMENT_CHANNEL,
00145 BROADCAST_ANNOUNCEMENT_BUMP_TIME,
00146 BROADCAST_ANNOUNCEMENT_MIN_TIME,
00147 BROADCAST_ANNOUNCEMENT_MAX_TIME);
00148 }
00149
00150 static void
00151 packet_sent(void *ptr, int status, int num_tx)
00152 {
00153 struct channel *c = ptr;
00154
00155
00156 switch(status) {
00157 case MAC_TX_COLLISION:
00158 PRINTF("rime: collision after %d tx\n", num_tx);
00159 break;
00160 case MAC_TX_NOACK:
00161 PRINTF("rime: noack after %d tx\n", num_tx);
00162 break;
00163 case MAC_TX_OK:
00164 PRINTF("rime: sent after %d tx\n", num_tx);
00165 break;
00166 default:
00167 PRINTF("rime: error %d after %d tx\n", status, num_tx);
00168 }
00169
00170 if(status == MAC_TX_OK) {
00171 struct rime_sniffer *s;
00172
00173 for(s = list_head(sniffers); s != NULL; s = list_item_next(s)) {
00174 if(s->output_callback != NULL) {
00175 s->output_callback();
00176 }
00177 }
00178 }
00179
00180 abc_sent(c, status, num_tx);
00181 }
00182
00183 int
00184 rime_output(struct channel *c)
00185 {
00186 RIMESTATS_ADD(tx);
00187 if(chameleon_create(c)) {
00188 packetbuf_compact();
00189
00190 NETSTACK_MAC.send(packet_sent, c);
00191 return 1;
00192 }
00193 return 0;
00194 }
00195
00196 const struct network_driver rime_driver = {
00197 "Rime",
00198 init,
00199 input
00200 };
00201