Revision 726
Working on bayboard wireless
branches/autonomous_recharging/code/projects/libwireless/bayboardTest/main.c | ||
---|---|---|
1 | 1 |
#include <bayboard_lib.h> |
2 | 2 |
#include <wireless.h> |
3 | 3 |
#include <wl_token_ring.h> |
4 |
#include <xbee.h> |
|
5 |
#include <ctype.h> |
|
4 | 6 |
|
5 | 7 |
void test_bom_on (void) |
6 | 8 |
{ |
7 | 9 |
set_orb(0,200,0); |
10 |
delay_ms(50); |
|
8 | 11 |
} |
9 | 12 |
|
10 | 13 |
void test_bom_off(void) |
11 | 14 |
{ |
12 |
set_orb(0,0,0); |
|
15 |
set_orb(200,0,200); |
|
16 |
delay_ms(50); |
|
13 | 17 |
} |
14 | 18 |
|
15 | 19 |
int test_nothing(void) |
... | ... | |
24 | 28 |
int main(void) |
25 | 29 |
{ |
26 | 30 |
bayboard_init(ALL_ON); |
27 |
delay_ms(500); |
|
31 |
//xbee_lib_init(); |
|
32 |
|
|
33 |
set_orb(200,0,0); |
|
34 |
|
|
28 | 35 |
wl_init(); |
29 |
wl_token_ring_set_bom_functions(do_nothing, do_nothing, test_nothing); |
|
30 |
wl_token_ring_register(); |
|
36 |
if(wl_token_ring_register() < 0) { |
|
37 |
set_orb(200,0,0); |
|
38 |
return -1; |
|
39 |
} |
|
40 |
if(wl_token_ring_join() < 0) { |
|
41 |
set_orb(200,0,0); |
|
42 |
return -1; |
|
43 |
} |
|
31 | 44 |
|
32 |
if (wl_token_ring_join()) |
|
33 |
set_orb(200,0,0); |
|
34 |
else |
|
35 |
set_orb(0,200,0); |
|
45 |
set_orb(0,200,0); |
|
46 |
usb_puts("== start ==\n"); |
|
47 |
delay_ms(200); |
|
36 | 48 |
|
49 |
//bom_init(0); |
|
37 | 50 |
|
38 |
while (1) |
|
39 |
wl_do(); |
|
51 |
//int c; |
|
52 |
//char buf[20]; |
|
53 |
while (1) { |
|
54 |
wl_do(); |
|
55 |
} |
|
56 |
|
|
57 |
return 0; |
|
58 |
|
|
40 | 59 |
} |
60 |
|
|
61 |
|
branches/autonomous_recharging/code/projects/libwireless/lib/wl_token_ring.c | ||
---|---|---|
136 | 136 |
#endif |
137 | 137 |
|
138 | 138 |
static PacketGroupHandler wl_token_ring_handler = |
139 |
{WL_TOKEN_RING_GROUP, wl_token_ring_timeout_handler,
|
|
139 |
{wl_token_ring_timeout_handler, |
|
140 | 140 |
wl_token_ring_response_handler, wl_token_ring_receive_handler, |
141 |
wl_token_ring_cleanup};
|
|
141 |
wl_token_ring_cleanup, WL_TOKEN_RING_GROUP};
|
|
142 | 142 |
|
143 | 143 |
/** |
144 | 144 |
* Causes the robot to join an existing token ring, or create one |
branches/autonomous_recharging/code/projects/libwireless/lib/sensor_matrix.h | ||
---|---|---|
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 |
... | ... | |
54 | 54 |
typedef struct |
55 | 55 |
{ |
56 | 56 |
/** |
57 |
* The size of the sensor matrix. |
|
58 |
**/ |
|
59 |
int size; |
|
60 |
/** |
|
61 | 57 |
* The matrix. Each row represents the readings of one |
62 | 58 |
* robot. |
63 | 59 |
**/ |
64 | 60 |
int** matrix; |
61 |
|
|
65 | 62 |
/** |
66 | 63 |
* The element representing a robot is true if that robot |
67 | 64 |
* is in the token ring and false otherwise. |
68 | 65 |
**/ |
69 |
int* joined; |
|
66 |
char* joined; |
|
67 |
|
|
70 | 68 |
/** |
71 | 69 |
* The number of robots in the token ring. |
72 | 70 |
**/ |
73 | 71 |
int numJoined; |
72 |
|
|
73 |
/** |
|
74 |
* The size of the sensor matrix. |
|
75 |
**/ |
|
76 |
int size; |
|
74 | 77 |
} SensorMatrix; |
75 | 78 |
|
76 | 79 |
/**@brief Create a sensor matrix **/ |
branches/autonomous_recharging/code/projects/libwireless/lib/wireless.h | ||
---|---|---|
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 |
... | ... | |
34 | 34 |
|
35 | 35 |
#ifndef WIRELESS_H |
36 | 36 |
#define WIRELESS_H |
37 |
|
|
37 |
|
|
38 | 38 |
//Note: If this is raised above 16, we will need to do |
39 | 39 |
//something about frame numbers for TX Status packets. |
40 | 40 |
/** |
41 | 41 |
* The maximum number of packet groups. |
42 | 42 |
**/ |
43 |
#define WL_MAX_PACKET_GROUPS 16
|
|
43 |
#define WL_MAX_PACKET_GROUPS 4
|
|
44 | 44 |
|
45 | 45 |
/** |
46 | 46 |
* @defgroup wireless Wireless |
... | ... | |
48 | 48 |
* |
49 | 49 |
* Contains functions and definitions for dealing with wireless functionality.<br><br> |
50 | 50 |
* |
51 |
* The wireless library provides a modular method for dealing with
|
|
51 |
* The wireless library provides a modular method for dealing with |
|
52 | 52 |
* wireless packets, by allowing packet groups to be registered. |
53 | 53 |
* A packet group is a collection of packets which share a packet |
54 |
* group code. Each packet in the group also has a type. A packet
|
|
54 |
* group code. Each packet in the group also has a type. A packet |
|
55 | 55 |
* group code and type are sent with each packet. When a packet |
56 |
* with a group code registered in the wireless library is
|
|
56 |
* with a group code registered in the wireless library is |
|
57 | 57 |
* received, the corresponding event handler is called. The |
58 | 58 |
* event handler uses the packet type and other information |
59 | 59 |
* stored in the packet to respond.<br><br> |
... | ... | |
67 | 67 |
|
68 | 68 |
/** |
69 | 69 |
* @struct PacketGroupHandler |
70 |
* A PacketGroupHandler represents a packet group, and is used to
|
|
70 |
* A PacketGroupHandler represents a packet group, and is used to |
|
71 | 71 |
* register a packet group with the wireless library. It contains |
72 | 72 |
* handlers for various events which can occur related to a packet |
73 | 73 |
* group. |
... | ... | |
75 | 75 |
typedef struct |
76 | 76 |
{ |
77 | 77 |
/** |
78 |
* The group code for this packet group. This number |
|
79 |
* must be unique. The maximum number of packet groups |
|
80 |
* is defined by WL_MAX_PACKET_GROUPS. |
|
81 |
**/ |
|
82 |
unsigned int groupCode; |
|
83 |
|
|
84 |
/** |
|
85 | 78 |
* Called every half second (not in interrupt, |
86 | 79 |
* but in wl_do). |
87 | 80 |
**/ |
88 | 81 |
void (*timeout_handler) (void); |
89 |
|
|
82 |
|
|
90 | 83 |
/** |
91 | 84 |
* Called when a transmit status packet is received |
92 | 85 |
* from the XBee where the first four bits of the frame |
... | ... | |
97 | 90 |
* we did not. |
98 | 91 |
**/ |
99 | 92 |
void (*handle_response) (int frame, int received); |
100 |
|
|
93 |
|
|
101 | 94 |
/** |
102 | 95 |
* Called when we receive a packet from this group. |
103 | 96 |
* |
... | ... | |
114 | 107 |
**/ |
115 | 108 |
void (*unregister) (void); |
116 | 109 |
|
110 |
/** |
|
111 |
* The group code for this packet group. This number |
|
112 |
* must be unique. The maximum number of packet groups |
|
113 |
* is defined by WL_MAX_PACKET_GROUPS. |
|
114 |
**/ |
|
115 |
unsigned int groupCode; |
|
116 |
|
|
117 | 117 |
} PacketGroupHandler; |
118 | 118 |
|
119 | 119 |
/**@brief Initialize the wireless library **/ |
branches/autonomous_recharging/code/projects/libwireless/lib/xbee.c | ||
---|---|---|
48 | 48 |
#include <serial.h> |
49 | 49 |
#include <avr/interrupt.h> |
50 | 50 |
|
51 |
#include <ctype.h> |
|
52 |
|
|
51 | 53 |
#endif |
52 | 54 |
|
53 | 55 |
#include <stdio.h> |
... | ... | |
99 | 101 |
static int xbee_send_modify_at_command(char* command, char* value); |
100 | 102 |
|
101 | 103 |
/*Global Variables*/ |
104 |
#define MAX_XBEE_FRAME_SIZE 32 |
|
102 | 105 |
|
106 |
|
|
103 | 107 |
#ifndef ROBOT |
104 | 108 |
static char* xbee_com_port = XBEE_PORT_DEFAULT; |
105 | 109 |
static int xbee_stream; |
... | ... | |
107 | 111 |
#endif |
108 | 112 |
|
109 | 113 |
// TODO: is this a good size? |
110 |
#define XBEE_BUFFER_SIZE 256
|
|
114 |
#define XBEE_BUFFER_SIZE 128
|
|
111 | 115 |
// a buffer for data received from the XBee |
112 | 116 |
char arrival_buf[XBEE_BUFFER_SIZE]; |
113 | 117 |
// location of last unread byte in buffer |
... | ... | |
117 | 121 |
|
118 | 122 |
|
119 | 123 |
//used to store packets as they are read |
120 |
static char xbee_buf[128];
|
|
124 |
static char xbee_buf[MAX_XBEE_FRAME_SIZE];
|
|
121 | 125 |
static int currentBufPos = 0; |
122 | 126 |
|
123 | 127 |
//XBee status |
... | ... | |
127 | 131 |
static int xbee_pending_channel = XBEE_CHANNEL_DEFAULT; |
128 | 132 |
static volatile unsigned int xbee_address = 0; |
129 | 133 |
|
134 |
#ifndef ROBOT |
|
130 | 135 |
void printHex(char * s, int len) { |
131 | 136 |
int i; |
132 | 137 |
for (i = 0; i < len; i++) { |
... | ... | |
134 | 139 |
} |
135 | 140 |
printf("\n"); |
136 | 141 |
} |
142 |
#endif |
|
137 | 143 |
|
138 | 144 |
|
139 | 145 |
/*Function Implementations*/ |
... | ... | |
147 | 153 |
#ifdef FIREFLY |
148 | 154 |
SIGNAL(SIG_USART0_RECV) |
149 | 155 |
{ |
156 |
usb_puts("In USART0 interrupt\n"); |
|
150 | 157 |
char c = UDR0; |
151 | 158 |
arrival_buf[buffer_last] = c; |
152 | 159 |
int t = buffer_last + 1; |
153 | 160 |
if (t == XBEE_BUFFER_SIZE) |
154 | 161 |
t = 0; |
155 | 162 |
if (t == buffer_first) |
156 |
{
|
|
157 |
WL_DEBUG_PRINT("Out of space in buffer.\n");
|
|
158 |
}
|
|
163 |
{
|
|
164 |
WL_DEBUG_PRINT("Out of space in buffer.\n");
|
|
165 |
}
|
|
159 | 166 |
buffer_last = t; |
160 | 167 |
} |
161 | 168 |
#else |
... | ... | |
163 | 170 |
ISR(USART1_RX_vect) |
164 | 171 |
{ |
165 | 172 |
char c = UDR1; |
173 |
|
|
174 |
/* |
|
175 |
char buf[30]; |
|
176 |
char buf2[5]; |
|
177 |
buf2[0] = '\\'; |
|
178 |
if (c == '\b') { |
|
179 |
buf2[1] = 'b'; |
|
180 |
buf2[2] = '\0'; |
|
181 |
} |
|
182 |
else if (c == '\n') { |
|
183 |
buf2[1] = 'n'; |
|
184 |
buf2[2] = '\0'; |
|
185 |
} |
|
186 |
else if (c == '\r') { |
|
187 |
buf2[1] = 'r'; |
|
188 |
buf2[2] = '\0'; |
|
189 |
} |
|
190 |
else if (c == '\t') { |
|
191 |
buf2[1] = 't'; |
|
192 |
buf2[2] = '\0'; |
|
193 |
} |
|
194 |
else if (!isprint(c)) { |
|
195 |
sprintf(buf2, "0x%.2x", c); |
|
196 |
} |
|
197 |
else { |
|
198 |
buf2[0] = c; |
|
199 |
buf2[1] = '\0'; |
|
200 |
} |
|
201 |
sprintf(buf, "In USART1 interrupt [%s]\n", buf2); |
|
202 |
usb_puts(buf); |
|
203 |
*/ |
|
204 |
|
|
166 | 205 |
arrival_buf[buffer_last] = c; |
167 | 206 |
int t = buffer_last + 1; |
168 | 207 |
if (t == XBEE_BUFFER_SIZE) |
169 | 208 |
t = 0; |
170 | 209 |
if (t == buffer_first) |
171 |
{
|
|
172 |
WL_DEBUG_PRINT("Out of space in buffer.\n");
|
|
173 |
}
|
|
210 |
{
|
|
211 |
WL_DEBUG_PRINT("Out of space in buffer.\n");
|
|
212 |
}
|
|
174 | 213 |
buffer_last = t; |
175 | 214 |
} |
176 | 215 |
#else |
... | ... | |
182 | 221 |
if (t == XBEE_BUFFER_SIZE) |
183 | 222 |
t = 0; |
184 | 223 |
if (t == buffer_first) |
185 |
{
|
|
186 |
WL_DEBUG_PRINT("Out of space in buffer.\n");
|
|
187 |
}
|
|
224 |
{
|
|
225 |
WL_DEBUG_PRINT("Out of space in buffer.\n");
|
|
226 |
}
|
|
188 | 227 |
buffer_last = t; |
189 | 228 |
} |
190 | 229 |
#endif |
... | ... | |
201 | 240 |
{ |
202 | 241 |
char c; |
203 | 242 |
while (1) |
204 |
{ |
|
205 |
if (xbee_read(&c, 1) != 0) { |
|
206 |
fprintf(stderr, "xbee_read failed.\n"); |
|
207 |
return NULL; |
|
208 |
} |
|
209 |
|
|
210 |
//DEBUGGING PRINT |
|
211 |
//printf("interrupt: %c (%d)\n", c, (int)c); |
|
212 |
arrival_buf[buffer_last] = c; |
|
213 |
int t = buffer_last + 1; |
|
214 |
if (t == XBEE_BUFFER_SIZE) |
|
215 |
t = 0; |
|
216 |
if (t == buffer_first) |
|
217 |
{ |
|
218 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
219 |
} |
|
220 |
buffer_last = t; |
|
243 |
{ |
|
244 |
if (xbee_read(&c, 1) != 0) { |
|
245 |
fprintf(stderr, "xbee_read failed.\n"); |
|
246 |
return NULL; |
|
247 |
} |
|
221 | 248 |
|
222 |
usleep(1000); |
|
223 |
} |
|
249 |
//DEBUGGING PRINT |
|
250 |
//printf("interrupt: %c (%d)\n", c, (int)c); |
|
251 |
arrival_buf[buffer_last] = c; |
|
252 |
int t = buffer_last + 1; |
|
253 |
if (t == XBEE_BUFFER_SIZE) |
|
254 |
t = 0; |
|
255 |
if (t == buffer_first) |
|
256 |
{ |
|
257 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
258 |
} |
|
259 |
buffer_last = t; |
|
224 | 260 |
|
261 |
usleep(1000); |
|
262 |
} |
|
263 |
|
|
225 | 264 |
return NULL; |
226 | 265 |
} |
227 | 266 |
|
... | ... | |
252 | 291 |
printf("Connecting to port %s.\n", xbee_com_port); |
253 | 292 |
xbee_stream = open(xbee_com_port, O_RDWR); |
254 | 293 |
if (xbee_stream == -1/* || lockf(xbee_stream, F_TEST, 0) != 0*/) |
255 |
{
|
|
256 |
printf("Failed to open connection to XBee on port %s\r\n", xbee_com_port);
|
|
257 |
return -1;
|
|
258 |
} else {
|
|
294 |
{
|
|
295 |
printf("Failed to open connection to XBee on port %s\r\n", xbee_com_port);
|
|
296 |
return -1;
|
|
297 |
} else {
|
|
259 | 298 |
printf("Successfully opened connection to XBee on port %s\r\n", xbee_com_port); |
260 | 299 |
} |
261 | 300 |
|
... | ... | |
277 | 316 |
options.c_cc[VTIME] = 50; |
278 | 317 |
|
279 | 318 |
if (tcsetattr(xbee_stream, TCSANOW, &options)) |
280 |
{
|
|
281 |
fprintf(stderr, "Error setting attributes.\n");
|
|
282 |
return -1;
|
|
283 |
} else {
|
|
319 |
{
|
|
320 |
fprintf(stderr, "Error setting attributes.\n");
|
|
321 |
return -1;
|
|
322 |
} else {
|
|
284 | 323 |
//printf("Successfully set termios attributes.\n"); |
285 | 324 |
} |
286 | 325 |
|
... | ... | |
288 | 327 |
|
289 | 328 |
xbee_listen_thread = (pthread_t*)malloc(sizeof(pthread_t)); |
290 | 329 |
if (xbee_listen_thread == NULL) |
291 |
{
|
|
292 |
fprintf(stderr, "%s: Malloc failed.\n", __FUNCTION__);
|
|
293 |
return -1;
|
|
294 |
}
|
|
330 |
{
|
|
331 |
fprintf(stderr, "%s: Malloc failed.\n", __FUNCTION__);
|
|
332 |
return -1;
|
|
333 |
}
|
|
295 | 334 |
|
296 | 335 |
int ret = pthread_create(xbee_listen_thread, NULL, listen_to_xbee, NULL); |
297 | 336 |
if (ret) |
298 |
{
|
|
299 |
fprintf(stderr, "Failed to create listener thread.\r\n");
|
|
300 |
return -1;
|
|
301 |
} else {
|
|
337 |
{
|
|
338 |
fprintf(stderr, "Failed to create listener thread.\r\n");
|
|
339 |
return -1;
|
|
340 |
} else {
|
|
302 | 341 |
//printf("Successfully created listener thread.\n"); |
303 | 342 |
} |
304 | 343 |
#endif |
... | ... | |
352 | 391 |
usleep(1000); |
353 | 392 |
|
354 | 393 |
if (ret == -1) { |
355 |
//printf("xbee_get_packet(NULL) failed.\n");
|
|
356 |
return -1;
|
|
394 |
//printf("xbee_get_packet(NULL) failed.\n");
|
|
395 |
return -1;
|
|
357 | 396 |
} |
358 | 397 |
} |
359 | 398 |
|
... | ... | |
384 | 423 |
**/ |
385 | 424 |
void xbee_terminate() |
386 | 425 |
{ |
387 |
#ifndef ROBOT
|
|
426 |
#ifndef ROBOT |
|
388 | 427 |
pthread_cancel(*xbee_listen_thread); |
389 | 428 |
free(xbee_listen_thread); |
390 | 429 |
lockf(xbee_stream, F_ULOCK, 0); |
391 | 430 |
close(xbee_stream); |
392 |
#endif
|
|
431 |
#endif |
|
393 | 432 |
} |
394 | 433 |
|
395 | 434 |
/** |
... | ... | |
418 | 457 |
if (ret == size) |
419 | 458 |
return 0; |
420 | 459 |
if (ret == -1) |
421 |
{
|
|
422 |
//interrupted by system signal, probably timer interrupt.
|
|
423 |
//just try again
|
|
424 |
if (errno == 4)
|
|
425 |
{
|
|
426 |
return xbee_send(buf, size);
|
|
427 |
}
|
|
428 |
printf("Failed to write to xbee, error %i.\r\n", errno);
|
|
429 |
return -1;
|
|
430 |
}
|
|
460 |
{
|
|
461 |
//interrupted by system signal, probably timer interrupt.
|
|
462 |
//just try again
|
|
463 |
if (errno == 4)
|
|
464 |
{
|
|
465 |
return xbee_send(buf, size);
|
|
466 |
}
|
|
467 |
printf("Failed to write to xbee, error %i.\r\n", errno);
|
|
468 |
return -1;
|
|
469 |
}
|
|
431 | 470 |
|
432 | 471 |
//write was interrupted after writing ret bytes |
433 | 472 |
return xbee_send(buf + ret, size - ret); |
... | ... | |
544 | 583 |
while (curr - s < len) { |
545 | 584 |
// check if buffer is empty |
546 | 585 |
if (buffer_last != buffer_first) { |
586 |
cli(); |
|
547 | 587 |
char c = arrival_buf[buffer_first++]; |
548 | 588 |
if (buffer_first == XBEE_BUFFER_SIZE) { |
549 | 589 |
buffer_first = 0; |
550 | 590 |
} |
591 |
sei(); |
|
551 | 592 |
|
552 | 593 |
//DEBUGGING PRINT |
553 | 594 |
//printf("\t\t c is %c (%d)\n", c, (int)c); |
... | ... | |
678 | 719 |
buf[3] = command[1]; |
679 | 720 |
int valueLen = 0; |
680 | 721 |
if (value != NULL) |
681 |
{
|
|
682 |
valueLen = strlen(value);
|
|
683 |
if (valueLen > 8)
|
|
684 |
{
|
|
685 |
WL_DEBUG_PRINT("AT Command too large.\r\n");
|
|
686 |
return -1;
|
|
687 |
}
|
|
722 |
{
|
|
723 |
valueLen = strlen(value);
|
|
724 |
if (valueLen > 8)
|
|
725 |
{
|
|
726 |
WL_DEBUG_PRINT("AT Command too large.\r\n");
|
|
727 |
return -1;
|
|
728 |
}
|
|
688 | 729 |
|
689 |
for (i = 0; i < valueLen; i++) {
|
|
690 |
buf[4 + i] = value[i];
|
|
691 |
}
|
|
692 |
}
|
|
730 |
for (i = 0; i < valueLen; i++) {
|
|
731 |
buf[4 + i] = value[i];
|
|
732 |
}
|
|
733 |
}
|
|
693 | 734 |
|
694 | 735 |
return xbee_send_frame(buf, 4 + valueLen); |
695 | 736 |
} |
... | ... | |
721 | 762 |
unsigned char checksum = 0; |
722 | 763 |
|
723 | 764 |
if (len > 100) |
724 |
{
|
|
725 |
WL_DEBUG_PRINT("Packet is too large.\r\n");
|
|
726 |
return -1;
|
|
727 |
}
|
|
765 |
{
|
|
766 |
WL_DEBUG_PRINT("Packet is too large.\r\n");
|
|
767 |
return -1;
|
|
768 |
}
|
|
728 | 769 |
|
729 | 770 |
//data for sending request |
730 | 771 |
buf[0] = XBEE_FRAME_TX_REQUEST_16; |
... | ... | |
801 | 842 |
{ |
802 | 843 |
//start reading a packet with XBEE_FRAME_START |
803 | 844 |
if (currentBufPos == 0) |
804 |
{ |
|
805 |
do |
|
806 |
{ |
|
807 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
808 |
buffer_first = 0; |
|
809 |
// check if buffer is empty |
|
810 |
if (buffer_first == buffer_last) { |
|
811 |
return 0; |
|
812 |
} |
|
813 |
} while (arrival_buf[buffer_first++] != XBEE_FRAME_START); |
|
845 |
{ |
|
846 |
cli(); |
|
847 |
do |
|
848 |
{ |
|
849 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
850 |
buffer_first = 0; |
|
851 |
// check if buffer is empty |
|
852 |
if (buffer_first == buffer_last) { |
|
853 |
sei(); |
|
854 |
return 0; |
|
855 |
} |
|
856 |
} while (arrival_buf[buffer_first++] != XBEE_FRAME_START); |
|
814 | 857 |
|
815 |
if (buffer_first == XBEE_BUFFER_SIZE) { |
|
816 |
buffer_first = 0; |
|
817 |
} |
|
818 |
xbee_buf[0] = XBEE_FRAME_START; |
|
819 |
currentBufPos++; |
|
820 |
} |
|
858 |
if (buffer_first == XBEE_BUFFER_SIZE) { |
|
859 |
buffer_first = 0; |
|
860 |
} |
|
861 |
sei(); |
|
821 | 862 |
|
863 |
xbee_buf[0] = XBEE_FRAME_START; |
|
864 |
currentBufPos++; |
|
865 |
} |
|
866 |
|
|
822 | 867 |
int len = -1; |
823 | 868 |
if (currentBufPos >= 3) { |
824 | 869 |
len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8); |
825 | 870 |
} |
826 | 871 |
|
827 | 872 |
while (len == -1 //packet length has not been read yet |
828 |
|| currentBufPos < len + 4)
|
|
829 |
{
|
|
830 |
if (currentBufPos == 3)
|
|
831 |
{
|
|
832 |
len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8);
|
|
833 |
if (len > 120)
|
|
834 |
{
|
|
835 |
WL_DEBUG_PRINT("Packet too large. Probably error in XBee transmission.\n");
|
|
836 |
currentBufPos = 0;
|
|
837 |
return -1;
|
|
838 |
}
|
|
839 |
}
|
|
873 |
|| currentBufPos < len + 4)
|
|
874 |
{
|
|
875 |
if (currentBufPos == 3)
|
|
876 |
{
|
|
877 |
len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8);
|
|
878 |
if (len > 120)
|
|
879 |
{
|
|
880 |
WL_DEBUG_PRINT("Packet too large. Probably error in XBee transmission.\n");
|
|
881 |
currentBufPos = 0;
|
|
882 |
return -1;
|
|
883 |
}
|
|
884 |
}
|
|
840 | 885 |
|
841 |
// check if buffer is empty
|
|
842 |
if (buffer_first == buffer_last) {
|
|
843 |
return 0;
|
|
844 |
}
|
|
845 |
xbee_buf[currentBufPos++] = arrival_buf[buffer_first++];
|
|
846 |
if (buffer_first == XBEE_BUFFER_SIZE) {
|
|
847 |
buffer_first = 0;
|
|
848 |
}
|
|
849 |
}
|
|
886 |
// check if buffer is empty
|
|
887 |
if (buffer_first == buffer_last) {
|
|
888 |
return 0;
|
|
889 |
}
|
|
890 |
xbee_buf[currentBufPos++] = arrival_buf[buffer_first++];
|
|
891 |
if (buffer_first == XBEE_BUFFER_SIZE) {
|
|
892 |
buffer_first = 0;
|
|
893 |
}
|
|
894 |
}
|
|
850 | 895 |
|
851 | 896 |
currentBufPos = 0; |
852 | 897 |
|
853 | 898 |
if (!xbee_verify_checksum(xbee_buf, len + 4)) |
854 |
{
|
|
855 |
WL_DEBUG_PRINT("XBee checksum failed.\r\n");
|
|
856 |
return -1;
|
|
857 |
}
|
|
899 |
{
|
|
900 |
WL_DEBUG_PRINT("XBee checksum failed.\r\n");
|
|
901 |
return -1;
|
|
902 |
}
|
|
858 | 903 |
|
859 | 904 |
//we will take care of the packet |
860 | 905 |
if (xbee_handle_packet(xbee_buf + 3, len) != 0) { |
... | ... | |
881 | 926 |
void xbee_handle_status(char status) |
882 | 927 |
{ |
883 | 928 |
switch (status) |
884 |
{
|
|
929 |
{
|
|
885 | 930 |
case 0: |
886 | 931 |
WL_DEBUG_PRINT("XBee hardware reset.\r\n"); |
887 | 932 |
break; |
... | ... | |
903 | 948 |
case 6: |
904 | 949 |
WL_DEBUG_PRINT("Coordinator started.\r\n"); |
905 | 950 |
break; |
906 |
}
|
|
951 |
}
|
|
907 | 952 |
} |
908 | 953 |
|
909 | 954 |
/** |
... | ... | |
916 | 961 |
static void xbee_handle_at_command_response(char* command, char result, char* extra, int extraLen) |
917 | 962 |
{ |
918 | 963 |
if (result == 1) |
919 |
{
|
|
920 |
WL_DEBUG_PRINT("Error with AT");
|
|
921 |
WL_DEBUG_PRINT(command);
|
|
922 |
WL_DEBUG_PRINT(" packet.\r\n");
|
|
923 |
}
|
|
964 |
{
|
|
965 |
WL_DEBUG_PRINT("Error with AT");
|
|
966 |
WL_DEBUG_PRINT(command);
|
|
967 |
WL_DEBUG_PRINT(" packet.\r\n");
|
|
968 |
}
|
|
924 | 969 |
WL_DEBUG_PRINT("AT"); |
925 | 970 |
WL_DEBUG_PRINT(command); |
926 | 971 |
WL_DEBUG_PRINT(" command was successful.\r\n"); |
927 | 972 |
|
928 | 973 |
if (command[0] == 'I' && command[1] == 'D') |
929 |
{
|
|
930 |
xbee_panID = xbee_pending_panID;
|
|
931 |
WL_DEBUG_PRINT("PAN ID set to ");
|
|
932 |
WL_DEBUG_PRINT_INT(xbee_panID);
|
|
933 |
WL_DEBUG_PRINT(".\r\n");
|
|
934 |
return;
|
|
935 |
}
|
|
974 |
{
|
|
975 |
xbee_panID = xbee_pending_panID;
|
|
976 |
WL_DEBUG_PRINT("PAN ID set to ");
|
|
977 |
WL_DEBUG_PRINT_INT(xbee_panID);
|
|
978 |
WL_DEBUG_PRINT(".\r\n");
|
|
979 |
return;
|
|
980 |
}
|
|
936 | 981 |
|
937 | 982 |
if (command[0] == 'C' && command[1] == 'H') |
938 |
{
|
|
939 |
xbee_channel = xbee_pending_channel;
|
|
940 |
WL_DEBUG_PRINT("Channel set to ");
|
|
941 |
WL_DEBUG_PRINT_INT(xbee_channel);
|
|
942 |
WL_DEBUG_PRINT(".\r\n");
|
|
943 |
return;
|
|
944 |
}
|
|
983 |
{
|
|
984 |
xbee_channel = xbee_pending_channel;
|
|
985 |
WL_DEBUG_PRINT("Channel set to ");
|
|
986 |
WL_DEBUG_PRINT_INT(xbee_channel);
|
|
987 |
WL_DEBUG_PRINT(".\r\n");
|
|
988 |
return;
|
|
989 |
}
|
|
945 | 990 |
|
946 | 991 |
if (command[0] == 'M' && command[1] == 'Y' && extraLen != 0) |
947 |
{
|
|
948 |
// printf("reading xbee_address\n"); |
|
992 |
{
|
|
993 |
// printf("reading xbee_address\n");
|
|
949 | 994 |
|
950 |
xbee_address = 0;
|
|
951 |
int i;
|
|
952 |
for (i = 0; i < extraLen; i++) {
|
|
953 |
xbee_address = (xbee_address << 8) + extra[i];
|
|
954 |
}
|
|
955 |
// printf("xbee address is: %d\n", xbee_address); |
|
995 |
xbee_address = 0;
|
|
996 |
int i;
|
|
997 |
for (i = 0; i < extraLen; i++) {
|
|
998 |
xbee_address = (xbee_address << 8) + extra[i];
|
|
999 |
}
|
|
1000 |
// printf("xbee address is: %d\n", xbee_address);
|
|
956 | 1001 |
|
957 |
WL_DEBUG_PRINT("XBee address is ");
|
|
958 |
WL_DEBUG_PRINT_INT(xbee_address);
|
|
959 |
WL_DEBUG_PRINT(".\r\n");
|
|
1002 |
WL_DEBUG_PRINT("XBee address is ");
|
|
1003 |
WL_DEBUG_PRINT_INT(xbee_address);
|
|
1004 |
WL_DEBUG_PRINT(".\r\n");
|
|
960 | 1005 |
|
961 |
if (xbee_address == 0)
|
|
962 |
{
|
|
963 |
printf("XBee 16-bit address must be set using ATMY.\r\n");
|
|
964 |
WL_DEBUG_PRINT("XBee 16-bit address must be set using ATMY.\r\n");
|
|
965 |
exit(0);
|
|
966 |
}
|
|
967 |
}
|
|
1006 |
if (xbee_address == 0)
|
|
1007 |
{
|
|
1008 |
printf("XBee 16-bit address must be set using ATMY.\r\n");
|
|
1009 |
WL_DEBUG_PRINT("XBee 16-bit address must be set using ATMY.\r\n");
|
|
1010 |
exit(0);
|
|
1011 |
}
|
|
1012 |
}
|
|
968 | 1013 |
} |
969 | 1014 |
|
970 | 1015 |
/** |
... | ... | |
987 | 1032 |
|
988 | 1033 |
char command[3] = {1, 2, 3}; |
989 | 1034 |
if (len <= 0) //this should not happend |
990 |
{
|
|
991 |
WL_DEBUG_PRINT("Non-positive packet length.\r\n");
|
|
992 |
return 0;
|
|
993 |
}
|
|
1035 |
{
|
|
1036 |
WL_DEBUG_PRINT("Non-positive packet length.\r\n");
|
|
1037 |
return 0;
|
|
1038 |
}
|
|
994 | 1039 |
|
995 | 1040 |
switch ((unsigned char)packet[0]) //packet type |
996 |
{
|
|
1041 |
{
|
|
997 | 1042 |
case XBEE_FRAME_STATUS: |
998 | 1043 |
xbee_handle_status(packet[1]); |
999 | 1044 |
return 1; |
... | ... | |
1004 | 1049 |
command[2] = 0; |
1005 | 1050 |
xbee_handle_at_command_response(command, packet[4], packet + 5, len - 5); |
1006 | 1051 |
return 1; |
1007 |
}
|
|
1052 |
}
|
|
1008 | 1053 |
return 0; |
1009 | 1054 |
} |
1010 | 1055 |
|
... | ... | |
1045 | 1090 |
int xbee_set_channel(int channel) |
1046 | 1091 |
{ |
1047 | 1092 |
if (channel < 0x0B || channel > 0x1A) |
1048 |
{
|
|
1049 |
WL_DEBUG_PRINT("Channel out of range.\r\n");
|
|
1050 |
return -1;
|
|
1051 |
}
|
|
1093 |
{
|
|
1094 |
WL_DEBUG_PRINT("Channel out of range.\r\n");
|
|
1095 |
return -1;
|
|
1096 |
}
|
|
1052 | 1097 |
|
1053 | 1098 |
char s[3]; |
1054 | 1099 |
s[0] = channel & 0xFF; |
branches/autonomous_recharging/code/projects/libwireless/lib/wl_error_group.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 |
... | ... | |
45 | 45 |
int length); |
46 | 46 |
|
47 | 47 |
PacketGroupHandler wl_error_handler = |
48 |
{WL_ERROR_GROUP, NULL, |
|
49 |
wl_error_response_receive, wl_error_handle_receive, NULL}; |
|
48 |
{NULL, wl_error_response_receive, wl_error_handle_receive, NULL,WL_ERROR_GROUP}; |
|
50 | 49 |
|
51 | 50 |
void wl_error_response_receive(int frame, int received) |
52 | 51 |
{ |
... | ... | |
111 | 110 |
|
112 | 111 |
/** |
113 | 112 |
* Send an error message as a string. |
114 |
*
|
|
113 |
* |
|
115 | 114 |
* @param str the error message to send |
116 | 115 |
**/ |
117 | 116 |
void wl_error_send_string(char* str) |
branches/autonomous_recharging/code/projects/libwireless/lib/sensor_matrix.c | ||
---|---|---|
66 | 66 |
free(m); |
67 | 67 |
return NULL; |
68 | 68 |
} |
69 |
m->joined = (int*)malloc(m->size * sizeof(int));
|
|
69 |
m->joined = (char*)malloc(m->size * sizeof(int));
|
|
70 | 70 |
if (!(m->joined)) { |
71 | 71 |
WL_DEBUG_PRINT("Out of memory - allocating memory for joined.\r\n"); |
72 | 72 |
free(m->matrix); |
... | ... | |
211 | 211 |
m->size = nextSize; |
212 | 212 |
|
213 | 213 |
//expand the size of joined |
214 |
int* tempJoined = (int *)malloc(nextSize * sizeof(int));
|
|
214 |
char* tempJoined = (char *)malloc(nextSize * sizeof(int));
|
|
215 | 215 |
if (!tempJoined) |
216 | 216 |
{ |
217 | 217 |
WL_DEBUG_PRINT("Out of memory - expand matrix 3.\r\n"); |
Also available in: Unified diff