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 #include "net/ethernode.h"
00043 #include "net/uip_arch.h"
00044 #include "net/uip-fw.h"
00045 #include "ether.h"
00046
00047 #include "dev/radio.h"
00048 #include "net/netstack.h"
00049 #include "net/packetbuf.h"
00050
00051 #include "node.h"
00052
00053 #include "lib/random.h"
00054
00055 #include <stdio.h>
00056 #include <sys/select.h>
00057 #include <unistd.h>
00058
00059 #include "net/ethernode.h"
00060
00061 #define BUF ((uip_tcpip_hdr *)&uip_buf[HDR_LEN])
00062
00063 #define PRINTF(...)
00064
00065
00066 struct {
00067 u8_t id;
00068 int timer;
00069 u8_t seqno;
00070 } state;
00071
00072 #define TYPE_DATA 1
00073 #define TYPE_ACK 2
00074
00075 struct hdr {
00076 u8_t type;
00077 u8_t src;
00078 u8_t dest;
00079 u8_t seqno;
00080 };
00081
00082 static int radio_is_on = 1;
00083
00084
00085 static int
00086 ethernode_on(void)
00087 {
00088 radio_is_on = 1;
00089 ether_set_radio_status(radio_is_on);
00090
00091 return 1;
00092 }
00093
00094 static int
00095 ethernode_safe_off(void)
00096 {
00097 radio_is_on = 0;
00098 ether_set_radio_status(radio_is_on);
00099 return 1;
00100 }
00101
00102 static int
00103 channel_clear(void)
00104 {
00105 return 1;
00106 }
00107
00108 static int
00109 receiving(void)
00110 {
00111 return 0;
00112 }
00113
00114 static int
00115 pending(void)
00116 {
00117 return 0;
00118 }
00119
00120
00121
00122 #define HDR_LEN UIP_LLH_LEN
00123
00124 #define ID_BROADCAST 0x80
00125
00126 PROCESS(ethernode_process, "Ethernode");
00127
00128 static u8_t
00129 do_send(u8_t type, u8_t dest, struct hdr *hdr, int len)
00130 {
00131
00132 hdr->type = type;
00133 hdr->src = state.id;
00134
00135 hdr->dest = dest;
00136
00137 hdr->seqno = state.seqno;
00138
00139 ++state.seqno;
00140
00141 PRINTF("ether_send len %d\n", len);
00142 return ether_send((char *)hdr, len);
00143
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153 void
00154 ethernode_init(int port)
00155 {
00156 ether_client_init(port);
00157
00158 do {
00159 state.id = random_rand() & 0x7f;
00160 } while(state.id == ID_BROADCAST);
00161
00162 state.seqno = 0;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172 int
00173 ethernode_poll(void)
00174 {
00175 return ether_client_poll();
00176 }
00177
00178 int
00179 ethernode_read(void *buf, unsigned short bufsize)
00180 {
00181 int len;
00182 u8_t tmpbuf[2048];
00183 struct hdr *hdr = (struct hdr *)tmpbuf;
00184
00185 len = ether_client_read(tmpbuf, sizeof(tmpbuf));
00186 if(len == 0) {
00187 return 0;
00188 }
00189
00190 if(radio_is_on == 0) {
00191
00192 return 0;
00193 }
00194
00195
00196 switch(hdr->type) {
00197 case TYPE_DATA:
00198 if(hdr->dest == state.id ||
00199 hdr->dest == ID_BROADCAST) {
00200 memcpy(buf, tmpbuf + HDR_LEN, bufsize);
00201 return len - HDR_LEN;
00202 }
00203 break;
00204 case TYPE_ACK:
00205 printf("<%d>: Received ack packet from %d\n", state.id, hdr->src);
00206 break;
00207 default:
00208 printf("<%d>: Received unknown packet type %d from %d\n", state.id, hdr->type, hdr->src);
00209 break;
00210 }
00211
00212 return 0;
00213
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 u8_t
00228 ethernode_send(void)
00229 {
00230 int len;
00231 static char tmpbuf[2048];
00232 struct hdr *hdr = (struct hdr *)tmpbuf;
00233 u8_t dest;
00234 struct timeval tv;
00235
00236 if(uip_len > sizeof(tmpbuf)) {
00237 PRINTF(("Ethernode_send: too large uip_len %d\n", uip_len));
00238 return UIP_FW_TOOLARGE;
00239 }
00240
00241 memcpy(&tmpbuf[HDR_LEN], &uip_buf[HDR_LEN], uip_len);
00242 len = uip_len + HDR_LEN;
00243
00244 dest = ID_BROADCAST;
00245 tv.tv_sec = 0;
00246 tv.tv_usec = (random_rand() % 1000);
00247 select(0, NULL, NULL, NULL, &tv);
00248
00249 do_send(TYPE_DATA, dest, hdr, len);
00250
00251 return UIP_FW_OK;
00252 }
00253
00254 static char tmpbuf[2048];
00255 static struct hdr *hdr = (struct hdr *)tmpbuf;
00256 static u8_t dest;
00257
00258 static int
00259 prepare(const void *buf, unsigned short len)
00260 {
00261 memcpy(&tmpbuf[HDR_LEN], buf, len);
00262 len = len + HDR_LEN;
00263
00264 dest = ID_BROADCAST;
00265 return len;
00266 }
00267
00268 static int
00269 transmit(unsigned short len)
00270 {
00271 do_send(TYPE_DATA, dest, hdr, len + HDR_LEN);
00272 return RADIO_TX_OK;
00273 }
00274
00275 static int
00276 send(const void *payload, unsigned short payload_len)
00277 {
00278 prepare(payload, payload_len);
00279 return transmit(payload_len);
00280 }
00281
00282 PROCESS_THREAD(ethernode_process, ev, data)
00283 {
00284 int len;
00285 PROCESS_BEGIN();
00286
00287 while(1) {
00288 process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, NULL);
00289 PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_CONTINUE);
00290
00291 len = ethernode_poll();
00292 if(len > 0) {
00293
00294 packetbuf_clear();
00295 len = ethernode_read(packetbuf_dataptr(), PACKETBUF_SIZE);
00296 if(len > 0) {
00297 packetbuf_set_datalen(len);
00298 NETSTACK_RDC.input();
00299 }
00300
00301
00302
00303
00304 }
00305 }
00306 PROCESS_END();
00307 }
00308
00309 const struct radio_driver ethernode_driver =
00310 {
00311 (int (*)(void))ethernode_init,
00312 prepare,
00313 transmit,
00314 send,
00315 ethernode_read,
00316 channel_clear,
00317 receiving,
00318 pending,
00319 ethernode_on,
00320 ethernode_safe_off,
00321 };
00322
00323