Revision 1825
Wireless: updated library to make it more readable and to reduce possible errors with acking (untested changes, but shouldn't be broken or something is very wrong).
wireless_send.c | ||
---|---|---|
99 | 99 |
packet[3] = XBEE_OPTIONS_NONE; |
100 | 100 |
packet[4] = nextframe; |
101 | 101 |
packet[5] = group; |
102 |
nextframe = (nextframe == 0xFF)?1:nextframe+1; // increment frame number |
|
103 | 102 |
|
104 |
// set scope |
|
103 |
// set scope, defaults to send to PAN
|
|
105 | 104 |
if (scope == GLOBAL) |
106 |
packet[3] &= XBEE_OPTIONS_BROADCAST_ALL_PANS;
|
|
105 |
packet[3] |= XBEE_OPTIONS_BROADCAST_ALL_PANS;
|
|
107 | 106 |
else if (scope != PAN) { |
108 | 107 |
WL_DEBUG_PRINT_P("Error - bad scope in core send function\r\n"); |
109 | 108 |
return WL_ERROR_SCOPE; |
110 | 109 |
} |
111 | 110 |
|
112 |
// set mode |
|
111 |
// set mode, defaults to RELIABLE
|
|
113 | 112 |
if (mode == FAST) { |
114 |
packet[3] &= XBEE_OPTIONS_DISABLE_RESPONSE;
|
|
113 |
packet[3] |= XBEE_OPTIONS_DISABLE_RESPONSE;
|
|
115 | 114 |
} else if (mode != RELIABLE) { |
116 | 115 |
WL_DEBUG_PRINT_P("Error - bad mode in core send function\r\n"); |
117 | 116 |
return WL_ERROR_MODE; |
... | ... | |
152 | 151 |
return ret; // no frame number |
153 | 152 |
} else if (mode == RELIABLE) { |
154 | 153 |
setack(nextframe,SENDING); // set status to SENDING |
154 |
nextframe = (nextframe == 0xFF)?1:nextframe+1; // increment frame number |
|
155 | 155 |
// save packet on sending buffer |
156 | 156 |
scope = send_buf_last; // use as ptr to send buffer |
157 | 157 |
if (send_buf_add(&scope,length+6) != WL_SUCCESS) { // add length |
158 | 158 |
WL_DEBUG_PRINT_P("Error adding length to sending buffer\r\n"); |
159 |
ret = WL_ERROR_SENDING_BUFFER_FULL;
|
|
159 |
return WL_ERROR_SENDING_BUFFER_FULL;
|
|
160 | 160 |
} |
161 |
for(mode=0;mode<6;mode++) { // add header |
|
161 |
for(mode=0;mode<6;mode++) { // add header //mode is being used as a counter to save space
|
|
162 | 162 |
if (send_buf_add(&scope,packet[mode]) != WL_SUCCESS) { |
163 | 163 |
WL_DEBUG_PRINT_P("Error adding header to sending buffer\r\n"); |
164 |
ret = WL_ERROR_SENDING_BUFFER_FULL;
|
|
164 |
return WL_ERROR_SENDING_BUFFER_FULL;
|
|
165 | 165 |
} |
166 | 166 |
} |
167 |
for(mode=0;mode<length;mode++) { // add data |
|
167 |
for(mode=0;mode<length;mode++) { // add data //mode is being used as a counter to save space
|
|
168 | 168 |
if (send_buf_add(&scope,data[mode]) != WL_SUCCESS) { |
169 | 169 |
WL_DEBUG_PRINT_P("Error adding data to sendig buffer\r\n"); |
170 |
ret = WL_ERROR_SENDING_BUFFER_FULL;
|
|
170 |
return WL_ERROR_SENDING_BUFFER_FULL;
|
|
171 | 171 |
} |
172 | 172 |
} |
173 | 173 |
if (send_buf_add(&scope,0) != WL_SUCCESS) { // add num retries=0 |
174 | 174 |
WL_DEBUG_PRINT_P("Error adding num_retries to sending buffer\r\n"); |
175 |
ret = WL_ERROR_SENDING_BUFFER_FULL;
|
|
175 |
return WL_ERROR_SENDING_BUFFER_FULL;
|
|
176 | 176 |
} |
177 | 177 |
send_buf_last = scope; |
178 | 178 |
send_buf_num_packets++; |
... | ... | |
254 | 254 |
**/ |
255 | 255 |
uint8_t wl_ack_error(void) { |
256 | 256 |
WL_DEBUG_PRINT_P("entering wl_ack_error function"); |
257 |
uint8_t val=0,i=1; |
|
257 |
uint8_t val=0,i=1;//i=1 because if i=0, then the xbee won't give an ack
|
|
258 | 258 |
|
259 |
while(1) {
|
|
260 |
if (((ack_buf[i/4])&(0x2<<(i%4))) != 0) // if the ack is in an error state
|
|
259 |
for(i = 1; i < 255; i++){
|
|
260 |
if (((ack_buf[i/4])&(0x2<<(i%4))) != 0) // if the ack is in an error state
|
|
261 | 261 |
val++; |
262 |
if (i==255) |
|
263 |
break; |
|
264 |
i++; |
|
265 |
} |
|
262 |
} |
|
266 | 263 |
WL_DEBUG_PRINT_P("|num_errors:"); |
267 | 264 |
WL_DEBUG_PRINT_INT(val); |
268 | 265 |
WL_DEBUG_PRINT_P("\r\n"); |
... | ... | |
297 | 294 |
/** |
298 | 295 |
* acknowledgement reset |
299 | 296 |
* reset the acknowledgement buffer |
297 |
* TODO: Also needs to reset packet buffer |
|
300 | 298 |
**/ |
301 | 299 |
void wl_ack_reset(void) { |
302 | 300 |
WL_DEBUG_PRINT_P("entering wl_ack_reset function\r\n"); |
... | ... | |
338 | 336 |
} |
339 | 337 |
|
340 | 338 |
|
341 |
/* ack handler */ |
|
339 |
/* ack handler |
|
340 |
* |
|
341 |
* @param num the ack number |
|
342 |
* @param val {SUCCESS,ACK_FAILURE,CCA_FAILURE,PURGED} |
|
343 |
* will be either SUCCESS or FAILURE |
|
344 |
*/ |
|
342 | 345 |
void ackhandle(uint8_t num,uint8_t val) { |
343 | 346 |
uint8_t len; |
344 | 347 |
uint8_t packets = 0; |
... | ... | |
356 | 359 |
// success |
357 | 360 |
setack(num,ACK_OK); // set status code |
358 | 361 |
// remove from buffer |
359 |
val = send_buf_first; |
|
362 |
val = send_buf_first; //val is no longer the send status |
|
363 |
|
|
360 | 364 |
while (1) { |
361 | 365 |
len = send_buf_get(&val); |
362 | 366 |
WL_DEBUG_PRINT_P("|send_packet_buf_pos:"); |
Also available in: Unified diff