45 #include <sys/types.h>
46 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
62 #include "dev/radio-sensor.h"
76 MEMB(packets,
struct ether_packet, 20000);
79 static u8_t rxbuffer[2048];
80 static clock_time_t
timer;
82 #define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
92 #define PTYPE_SENSOR 3
96 #define PTYPE_SERIAL 7
97 #define PTYPE_RADIO_STATUS 8
103 struct sensor_data sensor_data;
113 char text[NODES_TEXTLEN + SERIAL_LEN];
118 static int collisions = 1;
119 static int num_collisions = 0;
120 static int num_sent = 0;
121 static int num_received = 0;
122 static int drop_probability = 0;
123 static int num_drops = 0;
125 #include <sys/time.h>
127 static struct timeval t1;
129 static int linex, liney;
133 ether_print_stats(
void)
137 gettimeofday(&t2,
NULL);
139 time = (t2.tv_sec * 1000 + t2.tv_usec / 1000) -
140 (t1.tv_sec * 1000 + t1.tv_usec / 1000);
142 printf(
"Time: %f\n", time/1000.0);
143 printf(
"Total packets sent: %d\n", num_sent);
144 printf(
"Total collisions: %d\n", num_collisions);
145 printf(
"Total packets receptions: %d\n", num_received);
146 printf(
"Total randomly dropped packets: %d\n", num_drops);
150 ether_set_drop_probability(
double p)
152 drop_probability = p * 65536;
156 ether_set_collisions(
int c)
162 ether_set_strength(
int s)
174 ether_server_init(
void)
176 struct sockaddr_in sa;
178 gettimeofday(&t1,
NULL);
185 s = socket(AF_INET,SOCK_DGRAM,0);
188 perror(
"ether_server_init: socket");
191 memset((
char *)&sa, 0,
sizeof(sa));
193 sa.sin_family = AF_INET;
194 sa.sin_addr.s_addr = inet_addr(
"127.0.0.1");
200 if(bind(s, (
struct sockaddr *)&sa,
sizeof(sa)) < 0) {
201 printf(
"Bind to port %d\n", ETHER_PORT);
209 ether_client_init(
int port)
211 struct sockaddr_in sa;
213 sc = socket(AF_INET,SOCK_DGRAM,0);
219 memset((
char *)&sa, 0,
sizeof(sa));
221 sa.sin_family = AF_INET;
222 sa.sin_addr.s_addr = inet_addr(
"127.0.0.1");
227 if(bind(sc, (
struct sockaddr *)&sa,
sizeof(sa)) < 0) {
228 printf(
"Bind to port %d\n", port);
235 ether_client_poll(
void)
247 ret = select(sc + 1, &fdset,
NULL,
NULL, &tv);
250 perror(
"ether_client_poll: select");
256 ether_client_read(u8_t *buf,
int bufsize)
261 struct ether_hdr *hdr = (
struct ether_hdr *)rxbuffer;
269 ret = select(sc + 1, &fdset,
NULL,
NULL, &tv);
275 if(FD_ISSET(sc, &fdset)) {
276 ret = recv(sc, &rxbuffer[0],
sizeof(rxbuffer), 0);
278 perror(
"ether_client_poll: recv");
284 PRINTF(
"ether_client_read: packet truncated from %d to %d\n",
290 memcpy(buf, &rxbuffer[
sizeof(
struct ether_hdr)], len);
291 radio_sensor_signal = hdr->signal;
293 if(hdr->type == PTYPE_DATA && hdr->srcid != node.id) {
294 return len -
sizeof(
struct ether_hdr);
295 }
else if(hdr->type == PTYPE_CLOCK) {
296 node_set_time(hdr->clock);
297 }
else if(hdr->type == PTYPE_SENSOR) {
298 int strength = sensor_strength() -
299 ((hdr->srcx - node_x()) * (hdr->srcx - node_x()) +
300 (hdr->srcy - node_y()) * (hdr->srcy - node_y())) / sensor_strength();
303 sensor_input(&hdr->sensor_data, strength);
305 }
else if(hdr->type == PTYPE_SERIAL) {
306 char *ptr = hdr->text;
307 printf(
"serial input %s\n", ptr);
308 for(ptr = hdr->text; *ptr != 0; ++ptr) {
309 serial_line_input_byte(*ptr);
317 ether_server_poll(
void)
322 struct ether_hdr *hdr = (
struct ether_hdr *)rxbuffer;
336 ret = select(s + 1, &fdset,
NULL,
NULL, &tv);
340 if(FD_ISSET(s, &fdset)) {
341 ret = recv(s, &rxbuffer[0],
sizeof(rxbuffer), 0);
343 perror(
"ether_poll: read");
346 nodes_set_line(hdr->srcx, hdr->srcy, hdr->linex, hdr->liney);
349 PRINTF(
"ether_poll: read %d bytes from (%d, %d)\n",
350 ret, hdr->srcx, hdr->srcy);
351 ether_put((
char *)rxbuffer, ret, hdr->srcx, hdr->srcy);
354 nodes_set_leds(hdr->srcx, hdr->srcy, hdr->leds);
357 nodes_set_text(hdr->srcx, hdr->srcy, hdr->text);
360 nodes_done(hdr->srcid);
364 case PTYPE_RADIO_STATUS:
365 nodes_set_radio_status(hdr->srcx, hdr->srcy, hdr->radio_status);
376 ether_put(
char *data,
int len,
int x,
int y)
378 struct ether_packet *p;
382 p = (
struct ether_packet *)
memb_alloc(&packets);
388 memcpy(p->data, data, len);
399 send_packet(
char *data,
int len,
int port)
401 struct sockaddr_in sa;
403 memset((
char *)&sa, 0,
sizeof(sa));
404 sa.sin_family = AF_INET;
405 sa.sin_addr.s_addr = inet_addr(
"127.0.0.1");
408 if(sendto(s, data, len, 0, (
struct sockaddr *)&sa,
sizeof(sa)) == -1) {
409 perror(
"ether: send_packet: sendto");
416 struct ether_packet *p, *q;
417 struct ether_hdr *hdr;
427 for(i = 0; i < nodes_num(); ++i) {
429 x = nodes_node(i)->x;
430 y = nodes_node(i)->y;
431 port = nodes_node(i)->port;
440 hdr = (
struct ether_hdr *)p->data;
443 if(!(p->x == x && p->y == y) &&
445 (p->x - x) * (p->x - x) +
446 (p->y - y) * (p->y - y) <=
447 ether_strength() * ether_strength()) {
449 hdr->signal = ether_strength() * ether_strength() -
450 (p->x - x) * (p->x - x) -
451 (p->y - y) * (p->y - y);
463 ((q->x - x) * (q->x - x) +
464 (q->y - y) * (q->y - y) <=
465 ether_strength() * ether_strength())) {
488 if((
unsigned int)((rand() * 17) % 65536) >= drop_probability) {
489 send_packet(p->data, p->len, port);
509 struct ether_packet *
522 node_send_packet(
char *data,
int len)
524 struct sockaddr_in sa;
526 memset((
char *)&sa, 0,
sizeof(sa));
527 sa.sin_family = AF_INET;
528 sa.sin_addr.s_addr = inet_addr(
"127.0.0.1");
531 if(sendto(sc, data, len, 0,
532 (
struct sockaddr *)&sa,
sizeof(sa)) == -1) {
533 perror(
"ether.c node_send_packet: sendto");
538 ether_send(
char *data,
int len)
541 struct ether_hdr *hdr = (
struct ether_hdr *)tmpbuf;
544 memcpy(&tmpbuf[
sizeof(
struct ether_hdr)], data, len);
548 hdr->type = PTYPE_DATA;
550 hdr->srcid = node.id;
554 node_send_packet(tmpbuf, len +
sizeof(
struct ether_hdr));
560 ether_set_leds(
int leds)
562 struct ether_hdr hdr;
564 memset(&hdr, 0,
sizeof (hdr));
567 hdr.type = PTYPE_LEDS;
574 node_send_packet((
char *)&hdr,
sizeof(
struct ether_hdr));
579 ether_set_text(
char *text)
581 struct ether_hdr hdr;
585 hdr.type = PTYPE_TEXT;
586 strncpy(hdr.text, text, NODES_TEXTLEN);
592 node_send_packet((
char *)&hdr,
sizeof(
struct ether_hdr));
597 ether_set_radio_status(
int onoroff)
599 struct ether_hdr hdr;
603 hdr.type = PTYPE_RADIO_STATUS;
604 hdr.radio_status = onoroff;
609 node_send_packet((
char *)&hdr,
sizeof(
struct ether_hdr));
614 ether_send_sensor_data(
struct sensor_data *d,
int srcx,
int srcy,
int strength)
619 struct ether_hdr hdr;
623 for(i = 0; i < nodes_num(); ++i) {
625 x = nodes_node(i)->x;
626 y = nodes_node(i)->y;
627 port = nodes_node(i)->port;
629 if((srcx - x) * (srcx - x) +
630 (srcy - y) * (srcy - y) <=
631 strength * strength) {
635 hdr.type = PTYPE_SENSOR;
636 hdr.sensor_data = *d;
637 send_packet((
char *)&hdr,
sizeof(hdr), port);
644 ether_send_done(
void)
646 struct ether_hdr hdr;
650 hdr.type = PTYPE_DONE;
653 node_send_packet((
char *)&hdr,
sizeof(
struct ether_hdr));
658 ether_send_serial(
char *str)
660 struct ether_hdr hdr;
666 hdr.type = PTYPE_SERIAL;
668 len = strlen(str) + 1;
669 if(len >
sizeof(hdr.text)) {
670 len =
sizeof(hdr.text);
672 memcpy(&hdr.text, str, len);
677 send_packet((
char *)&hdr,
sizeof(
struct ether_hdr), nodes_base_node_port);
681 ether_set_line(
int x,
int y)
683 struct ether_hdr hdr;
691 hdr.type = PTYPE_NONE;
696 node_send_packet((
char *)&hdr,
sizeof(
struct ether_hdr));