Revision 397
added error returns to some libwireless functions, handled in server.
trunk/code/projects/colonet/ColonetServer/colonet_wireless.cpp | ||
---|---|---|
17 | 17 |
#include <wireless.h> // Colonet wireless library. |
18 | 18 |
#include <wl_token_ring.h> |
19 | 19 |
|
20 |
#include "colonet_defs.h"
|
|
21 |
#include "colonet_wireless.h"
|
|
20 |
#include <colonet_defs.h>
|
|
21 |
#include <colonet_wireless.h>
|
|
22 | 22 |
|
23 | 23 |
/******************************* Definitions *********************************/ |
24 | 24 |
|
... | ... | |
48 | 48 |
static int log_packet(unsigned char* packet, int len); |
49 | 49 |
|
50 | 50 |
/**************************** Public functions *******************************/ |
51 |
void colonet_wl_init(char* wl_port_, MsgHandlerFunction message_handler_, char* log_filename_) {
|
|
51 |
int colonet_wl_init(char* wl_port_, MsgHandlerFunction message_handler_, char* log_filename_) {
|
|
52 | 52 |
if (log_filename_ != NULL) { |
53 | 53 |
logging_enabled = true; |
54 | 54 |
strcpy(log_filename, log_filename_); |
... | ... | |
65 | 65 |
wl_token_ring_register(); |
66 | 66 |
|
67 | 67 |
printf("Joining token ring...\n"); |
68 |
wl_token_ring_join(); |
|
68 |
if (wl_token_ring_join() != 0) { |
|
69 |
fprintf(stderr, "Failed to join token ring.\n"); |
|
70 |
return -1; |
|
71 |
} |
|
69 | 72 |
printf("Joined token ring.\n"); |
73 |
|
|
74 |
return 0; |
|
70 | 75 |
} |
71 | 76 |
|
72 | 77 |
void colonet_wl_kill_listener_thread() { |
trunk/code/projects/colonet/ColonetServer/Main.cpp | ||
---|---|---|
18 | 18 |
} |
19 | 19 |
|
20 | 20 |
if (colonet_server.start_listening() < 0) { |
21 |
fprintf(stderr, "ColonetServer.start_listening failed.\n"); |
|
21 | 22 |
return -1; |
22 | 23 |
} |
23 | 24 |
|
trunk/code/projects/colonet/ColonetServer/includes/colonet_wireless.h | ||
---|---|---|
23 | 23 |
* |
24 | 24 |
* @return new ColonetWireless object |
25 | 25 |
*/ |
26 |
void colonet_wl_init(char* wl_port, MsgHandlerFunction message_handler_, char* log_filename_);
|
|
26 |
int colonet_wl_init(char* wl_port, MsgHandlerFunction message_handler_, char* log_filename_);
|
|
27 | 27 |
|
28 | 28 |
/** @brief Spawns a thread which reads data from the hardware interface |
29 | 29 |
* with the colony (either a dongle or a robot programmed to relay data) |
trunk/code/projects/colonet/ColonetServer/ColonetServer.cpp | ||
---|---|---|
191 | 191 |
printf("Logging disabled.\n"); |
192 | 192 |
} |
193 | 193 |
|
194 |
colonet_wl_init(optionsG.wireless_port, wirelessMessageHandler, log_filename); |
|
194 |
if (colonet_wl_init(optionsG.wireless_port, wirelessMessageHandler, log_filename) != 0) { |
|
195 |
fprintf(stderr, "ERROR - colonet_wl_init failed.\n"); |
|
196 |
return -1; |
|
197 |
} |
|
195 | 198 |
|
196 | 199 |
if (colonet_wl_run_listener_thread()) { |
197 | 200 |
fprintf(stderr, "%s: colonet_wl_run_listener_thread failed.\n", __FUNCTION__); |
trunk/code/projects/colonet/ColonetServer/options.c | ||
---|---|---|
17 | 17 |
|
18 | 18 |
#include <colonet_defs.h> |
19 | 19 |
|
20 |
#include "includes/options.h"
|
|
20 |
#include <options.h>
|
|
21 | 21 |
|
22 | 22 |
ColonetServerOptionsT optionsG; |
23 | 23 |
|
trunk/code/projects/colonet/README | ||
---|---|---|
12 | 12 |
|
13 | 13 |
Part II - Server setup. |
14 | 14 |
1) Go to colony/trunk/code/projects/libwireless/lib and run "make colonet". |
15 |
2) Go to colony/trunk/code/projects/colonet/lib/colonet_wireless and run "make". |
|
16 |
3) Go to colony/trunk/code/projects/colonet/ColonetServer and run "make". |
|
17 |
4) In the same directory, run "./ColonetServer". |
|
15 |
2) Go to colony/trunk/code/projects/colonet/ColonetServer and run "make". |
|
16 |
3) In the same directory, run "./ColonetServer". |
|
18 | 17 |
Now the server is ready. |
19 | 18 |
|
20 | 19 |
Part III - Client (GUI) setup. |
... | ... | |
30 | 29 |
"Failed to write to xbee, error 9." |
31 | 30 |
The Xbee is not being recognized by the computer (assuming it is plugged in). Try unplugging it and plugging it in |
32 | 31 |
again. |
33 |
|
trunk/code/projects/libwireless/lib/wl_token_ring.c | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* Copyright (c) 2007 Colony Project |
3 |
*
|
|
3 |
* |
|
4 | 4 |
* Permission is hereby granted, free of charge, to any person |
5 | 5 |
* obtaining a copy of this software and associated documentation |
6 | 6 |
* files (the "Software"), to deal in the Software without |
... | ... | |
9 | 9 |
* copies of the Software, and to permit persons to whom the |
10 | 10 |
* Software is furnished to do so, subject to the following |
11 | 11 |
* conditions: |
12 |
*
|
|
12 |
* |
|
13 | 13 |
* The above copyright notice and this permission notice shall be |
14 | 14 |
* included in all copies or substantial portions of the Software. |
15 |
*
|
|
15 |
* |
|
16 | 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
17 | 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
18 | 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
... | ... | |
146 | 146 |
WL_DEBUG_PRINT(".\r\n"); |
147 | 147 |
return; |
148 | 148 |
} |
149 |
|
|
149 |
|
|
150 | 150 |
sensorMatrix = sensor_matrix_create(); |
151 | 151 |
//add ourselves to the sensor matrix |
152 | 152 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0); |
... | ... | |
164 | 164 |
|
165 | 165 |
/** |
166 | 166 |
* Sets the functions that are called when the BOM ought to be |
167 |
* turned on or off. This could be used for things such as
|
|
167 |
* turned on or off. This could be used for things such as |
|
168 | 168 |
* charging stations, which have multiple BOMs. |
169 | 169 |
* |
170 | 170 |
* @param on_function the function to be called when the BOM |
... | ... | |
209 | 209 |
wl_token_next_robot = -1; |
210 | 210 |
deathDelay = DEATH_DELAY; |
211 | 211 |
} |
212 |
|
|
212 |
|
|
213 | 213 |
// we may have been dropped from the ring when this is received |
214 | 214 |
if (ringState == MEMBER) |
215 | 215 |
wl_token_pass_token(); |
... | ... | |
247 | 247 |
|
248 | 248 |
/** |
249 | 249 |
* Called when the XBee tells us if a packet we sent has been received. |
250 |
*
|
|
250 |
* |
|
251 | 251 |
* @param frame the frame number assigned when the packet was sent |
252 | 252 |
* @param received 1 if the packet was received, 0 otherwise |
253 | 253 |
**/ |
... | ... | |
300 | 300 |
* if no token ring exists. The token ring uses global and robot to robot |
301 | 301 |
* packets, and does not rely on any PAN. |
302 | 302 |
**/ |
303 |
void wl_token_ring_join()
|
|
303 |
int wl_token_ring_join()
|
|
304 | 304 |
{ |
305 | 305 |
WL_DEBUG_PRINT("Joining the token ring.\r\n"); |
306 | 306 |
ringState = JOINING; |
307 | 307 |
joinDelay = DEATH_DELAY * 2; |
308 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN, |
|
309 |
NULL, 0, 0); |
|
308 |
if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN, NULL, 0, 0) != 0) { |
|
309 |
return -1; |
|
310 |
} |
|
311 |
|
|
312 |
return 0; |
|
310 | 313 |
} |
311 | 314 |
|
312 | 315 |
/** |
... | ... | |
338 | 341 |
|
339 | 342 |
/** |
340 | 343 |
* Returns the BOM reading we have for robot dest. |
341 |
*
|
|
344 |
* |
|
342 | 345 |
* @param dest the robot whose relative location is returned |
343 | 346 |
* |
344 | 347 |
* @return a BOM reading from us to robot dest, in the range |
... | ... | |
386 | 389 |
{ |
387 | 390 |
if (i == source) |
388 | 391 |
continue; |
389 |
|
|
392 |
|
|
390 | 393 |
//set the sensor information we receive |
391 | 394 |
if (j < sensorDataLength / 2 && sensorData[2 * j] == i) |
392 | 395 |
{ |
... | ... | |
419 | 422 |
{ |
420 | 423 |
ringState = NONMEMBER; |
421 | 424 |
wl_token_ring_join(); |
422 |
|
|
425 |
|
|
423 | 426 |
WL_DEBUG_PRINT("We have been removed from the ring "); |
424 | 427 |
WL_DEBUG_PRINT("and are rejoining.\r\n"); |
425 | 428 |
} |
426 |
|
|
429 |
|
|
427 | 430 |
//the person who accepted us is dead... let's ask again |
428 | 431 |
if (i == acceptor) |
429 | 432 |
{ |
... | ... | |
437 | 440 |
} |
438 | 441 |
|
439 | 442 |
wl_token_next_robot = nextRobot; |
440 |
|
|
443 |
|
|
441 | 444 |
deathDelay = get_token_distance(wl_get_xbee_id(), nextRobot) * DEATH_DELAY; |
442 |
|
|
445 |
|
|
443 | 446 |
//we have the token |
444 | 447 |
if (wl_token_next_robot == wl_get_xbee_id()) |
445 | 448 |
wl_token_get_token(); |
... | ... | |
520 | 523 |
buf[2*j + 2] = sensor_matrix_get_reading(sensorMatrix, wl_get_xbee_id(), i); |
521 | 524 |
j++; |
522 | 525 |
} |
523 |
|
|
526 |
|
|
524 | 527 |
WL_DEBUG_PRINT("Passing the token to robot "); |
525 | 528 |
WL_DEBUG_PRINT_INT(buf[0]); |
526 | 529 |
WL_DEBUG_PRINT(".\r\n"); |
... | ... | |
544 | 547 |
WL_DEBUG_PRINT("Robot "); |
545 | 548 |
WL_DEBUG_PRINT_INT(source); |
546 | 549 |
WL_DEBUG_PRINT(" has flashed its bom.\r\n"); |
547 |
|
|
550 |
|
|
548 | 551 |
bom_on_count = 0; |
549 | 552 |
|
550 |
sensor_matrix_set_reading(sensorMatrix, wl_get_xbee_id(),
|
|
553 |
sensor_matrix_set_reading(sensorMatrix, wl_get_xbee_id(), |
|
551 | 554 |
source, get_max_bom_function()); |
552 | 555 |
} |
553 | 556 |
|
... | ... | |
555 | 558 |
* This method is called when we receive the token. Upon receiving |
556 | 559 |
* the token, we must send a BOM_ON packet, flash the BOM, and send |
557 | 560 |
* the token to the next robot. |
558 |
*
|
|
561 |
* |
|
559 | 562 |
* If there is a pending request for the token, this is processed first. |
560 | 563 |
**/ |
561 | 564 |
void wl_token_get_token() |
... | ... | |
579 | 582 |
} |
580 | 583 |
return; |
581 | 584 |
} |
582 |
|
|
585 |
|
|
583 | 586 |
WL_DEBUG_PRINT("Our BOM has been flashed.\r\n"); |
584 | 587 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, |
585 | 588 |
NULL, 0, 0); |
... | ... | |
589 | 592 |
delay_ms(BOM_DELAY); |
590 | 593 |
#endif |
591 | 594 |
bom_off_function(); |
592 |
|
|
595 |
|
|
593 | 596 |
if (!sensor_matrix_get_in_ring(sensorMatrix, wl_get_xbee_id())) |
594 | 597 |
{ |
595 | 598 |
WL_DEBUG_PRINT("Removed from sensor matrix while flashing BOM.\r\n"); |
596 | 599 |
return; |
597 | 600 |
} |
598 |
|
|
601 |
|
|
599 | 602 |
wl_token_pass_token(); |
600 | 603 |
} |
601 | 604 |
|
... | ... | |
622 | 625 |
//we can only accept one request at a time |
623 | 626 |
if (accepted != -1) |
624 | 627 |
return; |
625 |
|
|
628 |
|
|
626 | 629 |
//check if we are the preceding robot in the token ring |
627 | 630 |
int i = source - 1; |
628 | 631 |
while (1) |
... | ... | |
642 | 645 |
accepted = source; |
643 | 646 |
wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN_ACCEPT, |
644 | 647 |
NULL, 0, source, TOKEN_JOIN_ACCEPT_FRAME); |
645 |
|
|
648 |
|
|
646 | 649 |
WL_DEBUG_PRINT("Accepting robot "); |
647 | 650 |
WL_DEBUG_PRINT_INT(source); |
648 | 651 |
WL_DEBUG_PRINT(" into the token ring.\r\n"); |
trunk/code/projects/libwireless/lib/wireless.h | ||
---|---|---|
135 | 135 |
void wl_send_robot_to_robot_packet(char group, char type, |
136 | 136 |
char* data, int len, int dest, char frame); |
137 | 137 |
/**@brief Send a packet to all robots **/ |
138 |
void wl_send_global_packet(char group, char type, |
|
139 |
char* data, int len, char frame); |
|
138 |
int wl_send_global_packet(char group, char type, char* data, int len, char frame); |
|
140 | 139 |
/**@brief Send a packet to all robots in our PAN **/ |
141 | 140 |
void wl_send_pan_packet(char group, char type, |
142 | 141 |
char* data, int len, char frame); |
trunk/code/projects/libwireless/lib/xbee.c | ||
---|---|---|
69 | 69 |
/*Internal Function Prototypes*/ |
70 | 70 |
|
71 | 71 |
/*I/O Functions*/ |
72 |
void xbee_send(char* buf, int size);
|
|
73 |
void xbee_send_string(char* c); |
|
72 |
static int xbee_send(char* buf, int size);
|
|
73 |
static void xbee_send_string(char* c);
|
|
74 | 74 |
|
75 | 75 |
#ifndef ROBOT |
76 | 76 |
void xbee_read(char* buf, int size); |
... | ... | |
89 | 89 |
/*API Mode Functions*/ |
90 | 90 |
|
91 | 91 |
int xbee_handle_packet(char* packet, int len); |
92 |
void xbee_handle_at_command_response(char* command, char result, |
|
93 |
char* extra, int extraLen); |
|
92 |
void xbee_handle_at_command_response(char* command, char result, char* extra, int extraLen); |
|
94 | 93 |
void xbee_handle_status(char status); |
95 | 94 |
int xbee_verify_checksum(char* packet, int len); |
96 | 95 |
char xbee_compute_checksum(char* packet, int len); |
... | ... | |
291 | 290 |
* @param buf the buffer of data to send |
292 | 291 |
* @param size the number of bytes to send |
293 | 292 |
**/ |
294 |
void xbee_send(char* buf, int size)
|
|
293 |
int xbee_send(char* buf, int size)
|
|
295 | 294 |
{ |
296 | 295 |
#ifdef ROBOT |
297 | 296 |
int i; |
... | ... | |
301 | 300 |
int ret = write(xbee_stream, buf, size); |
302 | 301 |
//success |
303 | 302 |
if (ret == size) |
304 |
return; |
|
303 |
return 0;
|
|
305 | 304 |
if (ret == -1) |
306 | 305 |
{ |
307 | 306 |
//interrupted by system signal, probably timer interrupt. |
308 | 307 |
//just try again |
309 | 308 |
if (errno == 4) |
310 | 309 |
{ |
311 |
xbee_send(buf, size); |
|
312 |
return; |
|
310 |
return xbee_send(buf, size); |
|
313 | 311 |
} |
314 | 312 |
printf("Failed to write to xbee, error %i.\r\n", errno); |
315 |
return; |
|
313 |
return -1;
|
|
316 | 314 |
} |
317 | 315 |
|
318 | 316 |
//write was interrupted after writing ret bytes |
319 | 317 |
xbee_send(buf + ret, size - ret); |
320 | 318 |
#endif |
319 |
|
|
320 |
return 0; |
|
321 | 321 |
} |
322 | 322 |
|
323 | 323 |
/** |
... | ... | |
529 | 529 |
* the XBee alerts us as to whether or not our message |
530 | 530 |
* was received. |
531 | 531 |
**/ |
532 |
void xbee_send_packet(char* packet, int len, int dest, |
|
533 |
char options, char frame) |
|
532 |
int xbee_send_packet(char* packet, int len, int dest, char options, char frame) |
|
534 | 533 |
{ |
535 | 534 |
char buf[5]; |
536 | 535 |
char prefix[3]; |
... | ... | |
540 | 539 |
if (len > 100) |
541 | 540 |
{ |
542 | 541 |
WL_DEBUG_PRINT("Packet is too large.\r\n"); |
543 |
return; |
|
542 |
return -1;
|
|
544 | 543 |
} |
545 | 544 |
|
546 | 545 |
//data for sending request |
... | ... | |
560 | 559 |
for (i = 0; i < len; i++) |
561 | 560 |
checksum += (unsigned char)packet[i]; |
562 | 561 |
checksum = 0xFF - checksum; |
563 |
xbee_send(prefix, 3); |
|
564 |
xbee_send(buf, 5); |
|
565 |
xbee_send(packet, len); |
|
566 |
xbee_send((char*)&checksum, 1); |
|
562 |
|
|
563 |
if (xbee_send(prefix, 3) != 0) { |
|
564 |
return -1; |
|
565 |
} |
|
566 |
|
|
567 |
if (xbee_send(buf, 5) != 0) { |
|
568 |
return -1; |
|
569 |
} |
|
570 |
|
|
571 |
if (xbee_send(packet, len) != 0) { |
|
572 |
return -1; |
|
573 |
} |
|
574 |
|
|
575 |
if (xbee_send((char*)&checksum, 1) != 0) { |
|
576 |
return -1; |
|
577 |
} |
|
578 |
|
|
579 |
return 0; |
|
567 | 580 |
} |
568 | 581 |
|
569 | 582 |
/** |
trunk/code/projects/libwireless/lib/wl_token_ring.h | ||
---|---|---|
55 | 55 |
void (*off_function) (void), int (*max_bom_function) (void)); |
56 | 56 |
|
57 | 57 |
/**@brief Join the token ring **/ |
58 |
void wl_token_ring_join(void);
|
|
58 |
int wl_token_ring_join(void);
|
|
59 | 59 |
/**@brief Leave the token ring **/ |
60 | 60 |
void wl_token_ring_leave(void); |
61 | 61 |
|
trunk/code/projects/libwireless/lib/xbee.h | ||
---|---|---|
80 | 80 |
/**@brief Get a packet from the XBee **/ |
81 | 81 |
int xbee_get_packet(unsigned char* packet); |
82 | 82 |
/**@brief Send a packet to the XBee **/ |
83 |
void xbee_send_packet(char* packet, int len, int dest, |
|
84 |
char options, char frame); |
|
83 |
int xbee_send_packet(char* packet, int len, int dest, char options, char frame); |
|
85 | 84 |
/**@brief Set the PAN ID for the XBee **/ |
86 | 85 |
void xbee_set_pan_id(int id); |
87 | 86 |
/**@brief Get the XBee's PAN ID **/ |
trunk/code/projects/libwireless/lib/wireless.c | ||
---|---|---|
55 | 55 |
|
56 | 56 |
//Note: the actual frame sent has group as the first four bits and |
57 | 57 |
//frame as the last four. |
58 |
void wl_send_packet(char group, char type, char* data, int len, |
|
59 |
int dest, char options, char frame); |
|
58 |
int wl_send_packet(char group, char type, char* data, int len, int dest, char options, char frame); |
|
60 | 59 |
|
61 | 60 |
/*Data Members*/ |
62 | 61 |
|
... | ... | |
261 | 260 |
* @param len the packet length in bytes |
262 | 261 |
* @param frame the frame number to see with a TX_STATUS response |
263 | 262 |
**/ |
264 |
void wl_send_global_packet(char group, char type, |
|
265 |
char* data, int len, char frame) |
|
263 |
int wl_send_global_packet(char group, char type, char* data, int len, char frame) |
|
266 | 264 |
{ |
267 |
wl_send_packet(group, type, data, len, XBEE_BROADCAST, |
|
268 |
XBEE_OPTIONS_BROADCAST_ALL_PANS, frame); |
|
265 |
return wl_send_packet(group, type, data, len, XBEE_BROADCAST, XBEE_OPTIONS_BROADCAST_ALL_PANS, frame); |
|
269 | 266 |
} |
270 | 267 |
|
271 | 268 |
/** |
... | ... | |
295 | 292 |
* @param options the options for sending the packet |
296 | 293 |
* @param frame the frame number to see with a TX_STATUS response |
297 | 294 |
**/ |
298 |
void wl_send_packet(char group, char type, char* data, int len, |
|
299 |
int dest, char options, char frame) |
|
295 |
int wl_send_packet(char group, char type, char* data, int len, int dest, char options, char frame) |
|
300 | 296 |
{ |
301 | 297 |
char buf[128]; |
302 | 298 |
int i; |
303 | 299 |
if (frame != 0) |
304 | 300 |
frame = (frame & 0x0F) | ((group & 0x0F) << 4); |
301 |
|
|
305 | 302 |
buf[0] = group; |
306 | 303 |
buf[1] = type; |
307 | 304 |
for (i = 0; i < len; i++) |
308 | 305 |
buf[2 + i] = data[i]; |
309 |
xbee_send_packet(buf, len + 2, dest, options, frame); |
|
306 |
|
|
307 |
return xbee_send_packet(buf, len + 2, dest, options, frame); |
|
310 | 308 |
} |
311 | 309 |
|
312 | 310 |
/** |
Also available in: Unified diff