]> pere.pagekite.me Git - homepage.git/blob - RoBIOS/wl-ping-1.0.c
Generated.
[homepage.git] / RoBIOS / wl-ping-1.0.c
1 /*
2 * Author: Petter Reinholdtsen <pere@td.org.uit.no>
3 * Date: 1999-10-22
4
5 * Broadcast "Ping <time> <sender>" every second, and reply to every
6 * broadcast with the same message, substituting "Ping" with "Resp".
7 * When reciveing a "Resp" package, make a note on the sender, the
8 * originator and the time differerence. Print statistics when done.
9
10 */
11
12 #include "eyebot.h"
13 #include <stdio.h>
14 #include <string.h>
15
16 const char *PREFIX = "Ping";
17 const char *RESPONSE ="Resp";
18
19
20 enum {
21 MSGSIZE = 30
22 };
23
24 void
25 empty_recive_buffer(void)
26 {
27 char msgbuffer[MSGSIZE];
28 char from;
29 int len;
30 while (RADIOCheck())
31 RADIORecv(&from, &len, &msgbuffer[0]);
32 }
33
34 int
35 main(int argc, char *argv[])
36 {
37 int delay = 200; /* 2 second */
38 int nexttime = 0;
39
40 int sumsent = 0, sumrecv = 0, sumtimeused = 0;
41 int mintimeused = 10000, maxtimeused = 0;
42
43 /* disable input/output buffer */
44 setvbuf (stdout,NULL,_IONBF,0);
45 setvbuf (stdin,NULL,_IONBF,0);
46 LCDClear();
47 LCDMode(SCROLLING|NOCURSOR);
48
49
50 LCDPutString("Wireless ping\n");
51 LCDPutString("----------------");
52 LCDPutString("RADIO init");
53 RADIOInit();
54 LCDPutString(" ok\n");
55
56 LCDMenu("", "", "", "END");
57
58 empty_recive_buffer();
59
60 while (KEY4 != KEYRead())
61 {
62 int now;
63 while (RADIOCheck())
64 { /* Received messages, reply at once */
65 char msgbuffer[MSGSIZE];
66 char from;
67 int len;
68 now = OSGetCount();
69 RADIORecv(&from, &len, &msgbuffer[0]);
70 msgbuffer[len] = 0; /* zero terminate string */
71
72 /* Ping broadcast ? */
73 if (0 == strncmp(PREFIX, msgbuffer, strlen(PREFIX)))
74 {
75 /* Make message a response, and send it back */
76 memmove(&msgbuffer[0], RESPONSE, strlen(RESPONSE));
77 if (0 != RADIOSend(from, len, &msgbuffer[0]))
78 printf("Bad send on resp\n");
79 }
80
81 /* Ping response ? */
82 if (0 == strncmp(RESPONSE, msgbuffer, strlen(RESPONSE)))
83 {
84 int then, timeused, originator;
85 sscanf(&msgbuffer[0]+sizeof(RESPONSE), "%d %d", &then, &originator);
86 timeused = now - then;
87
88 if (from != originator) {
89
90 sumrecv++;
91 sumtimeused += timeused;
92 mintimeused = mintimeused > timeused ? timeused : mintimeused;
93 maxtimeused = maxtimeused < timeused ? timeused : maxtimeused;
94
95 printf("Ping (%d:%d) %3d\n", from, originator, timeused);
96 }
97 }
98 }
99
100 now = OSGetCount();
101
102 if (now >= nexttime)
103 { /* Time to send new broadcast */
104 char msgbuffer[MSGSIZE+1];
105 static int firsttime = 1;
106
107 sprintf(msgbuffer, "%s %06d %02d", PREFIX, now, OSMachineID());
108 if (0 != RADIOSend(BROADCAST, strlen(msgbuffer), msgbuffer))
109 printf("Bad send on bc\n");
110
111 if (firsttime)
112 {
113 printf("Packet size %ld\n", strlen(msgbuffer));
114 firsttime = 0;
115 }
116
117 sumsent++;
118 nexttime = now + delay;
119 }
120 }
121
122 printf("----------------");
123 printf("S %2d R %2d", sumsent, sumrecv);
124 if (0 != sumsent)
125 printf(" %2d%%", sumrecv*100/sumsent);
126 printf("\n");
127 if (0 != sumrecv)
128 printf("Time %3d<%3d<%4d\n",
129 mintimeused, sumtimeused / sumrecv, maxtimeused);
130
131 RADIOTerm();
132
133 return 0;
134 }
135