Revision 1612
Updated basic receive function and wrote handler dispatch function. The latter doesn't compile, but most of this directory doesn't.
branches/wireless/code/projects/libwireless/wireless_receive.c | ||
---|---|---|
60 | 60 |
extern uint8_t basic_buf_last; // end of last packet in basic buffer |
61 | 61 |
extern uint8_t other_buf_first; // beginning of first packet in other buffer |
62 | 62 |
extern uint8_t other_buf_last; // end of last packet in other buffer |
63 |
|
|
64 |
|
|
65 |
// the receive functions |
|
63 |
extern void (*handlers[])(void); // array of pointers to normal-priority packet handlers |
|
66 | 64 |
|
67 | 65 |
/** |
68 | 66 |
* @addtogroup wireless Wireless |
... | ... | |
98 | 96 |
**/ |
99 | 97 |
int8_t wl_get_basic(char *data, uint8_t length) { |
100 | 98 |
uint8_t buf_pos = basic_buf_first; // start at beginning of first (oldest) basic packet |
101 |
uint8_t data_length = xbee_basic_buf[buf_pos]; // get packet length |
|
102 |
uint8_t packet_num; // number of packet (may eventually be used to weed out duplicates) |
|
99 |
uint8_t data_length = xbee_basic_buf[buf_pos % PACKET_BUFFER_SIZE]; // get packet length |
|
103 | 100 |
|
104 |
if (data_length > length) // not enough room for packet in destination
|
|
101 |
if (data_length - 3 > length) // not enough room for data in destination
|
|
105 | 102 |
return WL_ERROR_TOO_SMALL; |
106 |
|
|
103 |
|
|
104 |
// packet has not fully arrived yet |
|
105 |
if (buf_pos + data_length > basic_buf_last // length goes farther than end of buffer |
|
106 |
&& !(buf_pos >= basic_buf_last && buf_pos + data_length > buf_pos)) |
|
107 |
/* basic test is Beginning + Length > Buffer End means packet isn't all there yet |
|
108 |
if end counter overflows, a complete packet's end may still be greater than the buffer end index, |
|
109 |
but such a packet can be read from the buffer |
|
110 |
*/ |
|
111 |
return WL_SUCCESS; // no data written |
|
112 |
|
|
107 | 113 |
buf_pos++; |
108 |
packet_num = xbee_basic_buf[buf_pos]; // get packet number |
|
109 |
buf_pos++; |
|
110 |
buf_pos++; // ignore the group code. it will always be the same. |
|
114 |
buf_pos++; // ignore the source ID |
|
111 | 115 |
|
112 |
memcpy(data, xbee_basic_buf + buf_pos, data_length); // get the data |
|
116 |
// copy data to destination, byte by byte to account for circular buffer |
|
117 |
// don't copy the 3 header bytes. nobody wants those. |
|
118 |
for (; buf_pos < basic_buf_first + data_length; buf_pos++) |
|
119 |
memcpy(data + buf_pos - 3, xbee_basic_buf + (buf_pos % PACKET_BUFFER_SIZE), 1); |
|
120 |
|
|
113 | 121 |
basic_buf_first += data_length; // "free up" this packet's buffer space |
114 | 122 |
|
115 |
return 0;
|
|
123 |
return data_length;
|
|
116 | 124 |
} |
117 | 125 |
|
118 | 126 |
/* |
... | ... | |
121 | 129 |
* @return error codes (TBD) |
122 | 130 |
*/ |
123 | 131 |
int8_t wl_dispatch(void) { |
132 |
uint8_t buf_pos = other_buf_first; // current position in buffer |
|
133 |
uint8_t group; // group of packet under consideration |
|
134 |
void (*handler)(uint8_t *data, uint8_t length, uint8_t source); // handler for group of packet under consideration |
|
135 |
uint8_t length = xbee_other_buf[buf_pos % PACKET_BUFFER_SIZE]; // length of packet under consideration |
|
136 |
uint8_t source; // robot ID of sender of packet under consideration |
|
124 | 137 |
|
125 |
return 0; |
|
138 |
// consider packets one at a time until buffer contains no complete packets |
|
139 |
while (buf_pos + length <= other_buf_last // length goes farther than end of buffer |
|
140 |
|| (buf_pos >= other_buf_last && buf_pos + length > buf_pos)) { |
|
141 |
/* basic test is Beginning + Length > Buffer End means packet isn't all there yet |
|
142 |
if end counter overflows, a complete packet's end may still be greater than the buffer end index, |
|
143 |
but such a packet can be read from the buffer |
|
144 |
*/ |
|
145 |
buf_pos++; |
|
146 |
group = xbee_other_buf[buf_pos]; // get group from packet |
|
147 |
if (group >= WL_NUM_PACKET_GROUPS) // invalid group number |
|
148 |
buf_pos += length - 1 + 1; // go to first byte of next packet |
|
149 |
else { // valid group number |
|
150 |
handler = handlers[group].FUNC; // get function from handler array |
|
151 |
buf_pos++; |
|
152 |
buf_pos++; // ignore 1st byte of ID (will be all 0s) |
|
153 |
source = xbee_other_buf[buf_pos]; // get sender ID from packet |
|
154 |
buf_pos++; // now index of beginning of packet data (mod PACKET_BUFFER_SIZE) |
|
155 |
|
|
156 |
if (handler) // check whether group is handled |
|
157 |
(*handler)(xbee_other_buf + (buf_pos & PACKET_BUFFER_SIZE), length - 4, source); |
|
158 |
|
|
159 |
// go to next packet |
|
160 |
buf_pos += length - 4 + 1; // go to first byte of next packet |
|
161 |
} |
|
162 |
|
|
163 |
length = xbee_other_buf[buf_pos & PACKET_BUFFER_SIZE]; // get length of next packet |
|
164 |
} |
|
165 |
|
|
166 |
return WL_SUCCESS; |
|
126 | 167 |
} |
127 | 168 |
|
128 | 169 |
|
branches/wireless/code/projects/libwireless/wireless.c | ||
---|---|---|
44 | 44 |
* @{ |
45 | 45 |
**/ |
46 | 46 |
|
47 |
static uint8_t init_flag = INIT_NO; |
|
48 |
static PacketGroupHandler wl_packet_handlers[MAX_PACKET_GROUPS]; |
|
47 |
/* holds pointers to all normal-priority packet handlers |
|
48 |
group handler located at index of group number |
|
49 |
group numbers with handlers undefined are NULL |
|
50 |
*/ |
|
51 |
void (*handlers[WL_NUM_PACKET_GROUPS])(void); |
|
49 | 52 |
|
50 | 53 |
/** |
51 | 54 |
* initialization function: |
Also available in: Unified diff