root / trunk / code / projects / colonet / utilities / colonet_sniffer / colonetSniffer.cpp @ 13
History | View | Annotate | Download (5.75 KB)
1 |
/** @file colonetSniffer.cpp
|
---|---|
2 |
* @brief A packet sniffer for the colony network. Uses the colonet_wireless
|
3 |
* library
|
4 |
*
|
5 |
* @author Eugene Marinelli
|
6 |
* @date 2/13/06
|
7 |
*/
|
8 |
|
9 |
#include <iostream> |
10 |
#include <signal.h> |
11 |
#include <fcntl.h> |
12 |
#include <signal.h> |
13 |
|
14 |
#include <colonet_wireless.h> |
15 |
#include <colonet_defs.h> |
16 |
#include <set> |
17 |
#include <queue> |
18 |
#include <assert.h> |
19 |
|
20 |
typedef struct robot { |
21 |
int id;
|
22 |
} robot_t; |
23 |
|
24 |
static bool monitor_mode; |
25 |
static int cur_robot_id; |
26 |
static int last_robot_id; |
27 |
|
28 |
static queue<ColonetPacket*> pkt_queue;
|
29 |
|
30 |
pthread_cond_t monitor_cvar; |
31 |
pthread_mutex_t pkt_queue_lock; |
32 |
|
33 |
void print_robot(robot_t* robot)
|
34 |
{ |
35 |
assert(robot); |
36 |
|
37 |
if (robot->id == cur_robot_id) {
|
38 |
cout<<" * ";
|
39 |
} else if (robot->id == last_robot_id) { |
40 |
cout<<" > ";
|
41 |
} else {
|
42 |
cout<<" ";
|
43 |
} |
44 |
|
45 |
cout<<"robot "<<robot->id<<" "; |
46 |
|
47 |
cout<<endl; |
48 |
} |
49 |
|
50 |
int set_contains_id(set<void*> s, int id) |
51 |
{ |
52 |
for (set<void*>::iterator itr = s.begin(); itr != s.end(); itr++) { |
53 |
if (((robot_t*)*itr)->id == id) {
|
54 |
return 1; |
55 |
} |
56 |
} |
57 |
|
58 |
return 0; |
59 |
} |
60 |
|
61 |
void set_remove_id(set<void*>* s, int id) |
62 |
{ |
63 |
for (set<void*>::iterator itr = s->begin(); itr != s->end(); itr++) { |
64 |
if (((robot_t*)*itr)->id == id) {
|
65 |
s->erase(*itr); |
66 |
return;
|
67 |
} |
68 |
} |
69 |
} |
70 |
|
71 |
void* monitor_packets(void* args) |
72 |
{ |
73 |
static set<void*> robot_set; |
74 |
int sender_id;
|
75 |
robot_t* new_robot; |
76 |
ColonetPacket* pkt; |
77 |
int errcode;
|
78 |
|
79 |
assert(monitor_mode); |
80 |
|
81 |
pthread_cond_init(&monitor_cvar, NULL);
|
82 |
pthread_mutex_init(&pkt_queue_lock, NULL);
|
83 |
|
84 |
while (1) { |
85 |
if (pkt_queue.empty()) {
|
86 |
pthread_mutex_lock(&pkt_queue_lock); |
87 |
pthread_cond_wait(&monitor_cvar, &pkt_queue_lock); |
88 |
} |
89 |
|
90 |
assert(!pkt_queue.empty()); |
91 |
|
92 |
pkt = pkt_queue.front(); |
93 |
pkt_queue.pop(); |
94 |
|
95 |
pthread_mutex_unlock(&pkt_queue_lock); |
96 |
|
97 |
errcode = ((char*)pkt)[WL_ERROR_LOC];
|
98 |
sender_id = pkt->token_src; |
99 |
last_robot_id = pkt->token_src; |
100 |
cur_robot_id = pkt->token_dest; |
101 |
|
102 |
if (!set_contains_id(robot_set, sender_id)) {
|
103 |
assert(new_robot = (robot_t*) malloc(sizeof(robot_t)));
|
104 |
new_robot->id = sender_id; |
105 |
robot_set.insert((void*)new_robot);
|
106 |
} |
107 |
|
108 |
/* Handle errors */
|
109 |
if (pkt->token_dest == WL_ERROR_DEST) {
|
110 |
switch (errcode) {
|
111 |
case WL_ERRCODE_ROBOTDEATH: {
|
112 |
int dead_robot = ((char*)pkt)[WL_ERR_DEAD_LOC]; |
113 |
set_remove_id(&robot_set, dead_robot); |
114 |
|
115 |
for (set<void*>::iterator itr = robot_set.begin(); itr != robot_set.end(); itr++) { |
116 |
if (((robot_t*)*itr)->id > dead_robot) {
|
117 |
((robot_t*)*itr)->id--; |
118 |
} |
119 |
} |
120 |
break;
|
121 |
} |
122 |
case WL_ERRCODE_NEEDTOCHARGE:
|
123 |
fprintf(stderr, "Need to charge error received\n");
|
124 |
break;
|
125 |
case WL_ERRCODE_CHARGESTATION:
|
126 |
fprintf(stderr, "Charge station error received\n");
|
127 |
break;
|
128 |
} |
129 |
} |
130 |
|
131 |
/* Display */
|
132 |
cout<<endl; |
133 |
for (set<void*>::iterator itr = robot_set.begin(); itr != robot_set.end(); itr++) { |
134 |
print_robot((robot_t*)*itr); |
135 |
} |
136 |
|
137 |
free(pkt); |
138 |
} |
139 |
|
140 |
return NULL; |
141 |
} |
142 |
|
143 |
int msg_handler(ColonetPacket* pkt)
|
144 |
{ |
145 |
ColonetPacket* copy_pkt = (ColonetPacket*) malloc(sizeof(ColonetPacket));
|
146 |
assert(copy_pkt); |
147 |
|
148 |
memcpy(copy_pkt, pkt, sizeof(ColonetPacket));
|
149 |
|
150 |
pthread_mutex_lock(&pkt_queue_lock); |
151 |
pkt_queue.push(copy_pkt); |
152 |
pthread_mutex_unlock(&pkt_queue_lock); |
153 |
|
154 |
pthread_cond_signal(&monitor_cvar); |
155 |
|
156 |
return 0; |
157 |
} |
158 |
|
159 |
void set_xbee_pan(char* port, int pan) |
160 |
{ |
161 |
int xbee_fd = open(port, O_RDWR);
|
162 |
char inbuf[8]; |
163 |
char panbuf[16]; |
164 |
|
165 |
sprintf(panbuf, "ATID %d\r", pan);
|
166 |
|
167 |
assert(xbee_fd); |
168 |
|
169 |
write(xbee_fd, "+++", 3); |
170 |
|
171 |
printf(".");
|
172 |
fflush(stdout); |
173 |
|
174 |
read(xbee_fd, inbuf, 1);
|
175 |
while (inbuf[0] != 'O') { |
176 |
read(xbee_fd, inbuf, 1);
|
177 |
} |
178 |
|
179 |
printf(".");
|
180 |
fflush(stdout); |
181 |
|
182 |
read(xbee_fd, inbuf, 1);
|
183 |
while (inbuf[0] != 'K') { |
184 |
read(xbee_fd, inbuf, 1);
|
185 |
} |
186 |
|
187 |
printf(".");
|
188 |
fflush(stdout); |
189 |
|
190 |
read(xbee_fd, inbuf, 1);
|
191 |
while (inbuf[0] != '\r') { |
192 |
read(xbee_fd, inbuf, 1);
|
193 |
} |
194 |
|
195 |
printf(".");
|
196 |
fflush(stdout); |
197 |
|
198 |
write(xbee_fd, panbuf, strlen(panbuf)); |
199 |
|
200 |
read(xbee_fd, inbuf, 1);
|
201 |
if (inbuf[0] != 'O') { |
202 |
printf("XBEE ERROR! received %d\n", inbuf[0]); |
203 |
return;
|
204 |
} |
205 |
|
206 |
printf(".");
|
207 |
fflush(stdout); |
208 |
|
209 |
read(xbee_fd, inbuf, 1);
|
210 |
if (inbuf[0] != 'K') { |
211 |
printf("XBEE ERROR! received %d\n", inbuf[0]); |
212 |
return;
|
213 |
} |
214 |
|
215 |
printf(".");
|
216 |
fflush(stdout); |
217 |
|
218 |
read(xbee_fd, inbuf, 1);
|
219 |
if (inbuf[0] != '\r') { |
220 |
printf("XBEE ERROR! received %d\n", inbuf[0]); |
221 |
return;
|
222 |
} |
223 |
|
224 |
printf(".");
|
225 |
fflush(stdout); |
226 |
|
227 |
write(xbee_fd, "ATCN\r", 5); |
228 |
|
229 |
printf("pan id set!\n");
|
230 |
|
231 |
close(xbee_fd); |
232 |
} |
233 |
|
234 |
int main(int argc, char** argv) |
235 |
{ |
236 |
ColonetWireless* cw; |
237 |
pthread_t* wl_thr; |
238 |
pthread_t monitor_thr; |
239 |
int opt;
|
240 |
char* log_filename = NULL; |
241 |
char* port = SERIAL_PORT;
|
242 |
bool join_network = false; |
243 |
int pan = WL_DEFAULT_PAN;
|
244 |
bool ignore_token = false; |
245 |
|
246 |
while ((opt = getopt(argc, argv, "l:mhp:ja:")) != -1) { |
247 |
switch(opt) {
|
248 |
case 'm': |
249 |
monitor_mode = true;
|
250 |
cout<<"Monitoring token\n";
|
251 |
break;
|
252 |
case 'l': |
253 |
log_filename = optarg; |
254 |
break;
|
255 |
case 'h': |
256 |
cout<<"Usage: ./colonetSniffer [-l log_filename] [-m (token monitor)] [-h (help/usage)] [-a pan] [-j join nework] [-i ignore token] [-p port]\n";
|
257 |
cout<<"* latest destination\n";
|
258 |
cout<<"> latest sender\n";
|
259 |
|
260 |
return 1; |
261 |
break;
|
262 |
case 'p': |
263 |
port = optarg; |
264 |
break;
|
265 |
case 'j': |
266 |
join_network = true;
|
267 |
break;
|
268 |
case 'a': |
269 |
pan = atoi(optarg); |
270 |
break;
|
271 |
case 'i': |
272 |
ignore_token = true;
|
273 |
break;
|
274 |
} |
275 |
} |
276 |
|
277 |
if (monitor_mode) {
|
278 |
assert(!pthread_create(&monitor_thr, NULL, monitor_packets, NULL)); |
279 |
} |
280 |
|
281 |
//cout<<"Setting pan to "<<pan<<"...\n";
|
282 |
//set_xbee_pan(port, pan);
|
283 |
|
284 |
cw = new ColonetWireless(port, msg_handler, log_filename, join_network,
|
285 |
ignore_token); |
286 |
|
287 |
assert(wl_thr = cw->run_listener_thread()); |
288 |
|
289 |
pthread_join(*wl_thr, NULL);
|
290 |
free(wl_thr); |
291 |
|
292 |
return 0; |
293 |
} |