2 * Copyright (c) 2011, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <aversive/queue.h>
30 #include <aversive/endian.h>
40 #include "xbee_neighbor.h"
41 #include "xbee_stats.h"
43 #include "xbee_proto.h"
46 /* return -1 if the frame is invalid */
47 static int xbee_proto_parse_atresp(struct xbee_dev *dev, void *buf,
50 struct xbee_atresp_hdr *atresp_hdr;
52 dev->stats.rx_atresp++;
54 if (len < sizeof(struct xbee_hdr) + sizeof(struct xbee_atresp_hdr)) {
55 dev->stats.rx_frame_too_small++;
59 atresp_hdr = buf + sizeof(struct xbee_hdr);
61 /* bad status, but let the frame continue */
62 if (atresp_hdr->status != 0)
63 dev->stats.rx_atresp_error++;
68 /* return -1 if the frame is invalid */
69 static int xbee_proto_parse_rmt_atresp(struct xbee_dev *dev, void *buf,
72 struct xbee_rmt_atresp_hdr *rmt_atresp_hdr;
74 dev->stats.rx_rmt_atresp++;
76 if (len < sizeof(struct xbee_hdr) + sizeof(struct xbee_rmt_atresp_hdr)) {
77 dev->stats.rx_frame_too_small++;
81 rmt_atresp_hdr = buf + sizeof(struct xbee_hdr);
83 /* bad status, but let the frame continue */
84 if (rmt_atresp_hdr->status != 0)
85 dev->stats.rx_rmt_atresp_error++;
90 /* return -1 if the frame is invalid */
91 static int xbee_proto_parse_xmit_status(struct xbee_dev *dev, void *buf,
94 struct xbee_xmit_status_hdr *xmit_status_hdr;
96 dev->stats.rx_xmit_status++;
98 if (len < sizeof(struct xbee_hdr) + sizeof(struct xbee_xmit_status_hdr)) {
99 dev->stats.rx_frame_too_small++;
103 xmit_status_hdr = buf + sizeof(struct xbee_hdr);
104 dev->stats.tx_xmit_retries += xmit_status_hdr->xmit_retry_cnt;
106 /* bad status, but let the frame continue */
107 if (xmit_status_hdr->delivery_status != 0)
108 dev->stats.rx_xmit_status_error++;
113 /* parse the frame stored in the device: return 0 if the frame is
114 * valid, else a negative value */
115 static int xbee_proto_parse_frame(struct xbee_dev *dev)
117 void *buf = dev->frame;
118 uint8_t len = dev->frame_len;
120 struct xbee_hdr *hdr = buf;
123 int channel = XBEE_DEFAULT_CHANNEL;
125 dev->stats.rx_frame++;
128 case XBEE_TYPE_MODEM_STATUS:
130 case XBEE_TYPE_EXPL_RECV:
131 hdrlen = sizeof(struct xbee_hdr) - 1; /* no frame ID */
134 hdrlen = sizeof(struct xbee_hdr);
138 /* check frame len */
139 if (len < (hdrlen + 1)) {
140 dev->stats.rx_frame_too_small++;
141 fprintf(stderr, "Frame too small\n");
145 /* validate the cksum */
146 for (i = 3; i < (len - 1); i++)
147 cksum += ((uint8_t *)buf)[i];
148 cksum = 0xff - cksum;
149 if (cksum != ((uint8_t *)buf)[len-1]) {
150 fprintf(stderr, "Invalid cksum\n");
151 dev->stats.rx_invalid_cksum++;
157 case XBEE_TYPE_MODEM_STATUS:
158 dev->stats.rx_modem_status++;
159 channel = XBEE_DEFAULT_CHANNEL;
161 case XBEE_TYPE_ATRESP:
162 if (xbee_proto_parse_atresp(dev, buf, len) < 0)
166 case XBEE_TYPE_RMT_ATRESP:
167 if (xbee_proto_parse_rmt_atresp(dev, buf, len) < 0)
171 case XBEE_TYPE_XMIT_STATUS:
172 if (xbee_proto_parse_xmit_status(dev, buf, len) < 0)
177 dev->stats.rx_data++;
178 channel = XBEE_DEFAULT_CHANNEL;
180 case XBEE_TYPE_EXPL_RECV:
181 dev->stats.rx_expl_data++;
182 channel = XBEE_DEFAULT_CHANNEL;
184 case XBEE_TYPE_NODE_ID:
185 dev->stats.rx_node_id++;
186 channel = hdr->id; //XXX
188 /* invalid commands */
189 case XBEE_TYPE_ATCMD:
190 case XBEE_TYPE_ATCMD_Q:
192 case XBEE_TYPE_EXPL_XMIT:
193 case XBEE_TYPE_RMT_ATCMD:
195 dev->stats.rx_invalid_type++;
199 /* fallback to default channel if not registered */
200 if (channel < 0 || channel >= XBEE_MAX_CHANNEL ||
201 dev->channel[channel].registered == 0)
202 channel = XBEE_DEFAULT_CHANNEL;
204 /* execute the callback if any */
205 if (dev->channel[channel].rx_cb != NULL)
206 dev->channel[channel].rx_cb(dev, channel, hdr->type,
209 dev->channel[channel].arg);
214 int xbee_proto_xmit(struct xbee_dev *dev, uint8_t channel_id, uint8_t type,
215 void *buf, unsigned len)
221 /* there is no empty message, so return an error */
225 /* prepare an iovec to avoid a copy: prepend a header to the
226 * buffer and append a checksum */
227 hdr.delimiter = XBEE_DELIMITER;
228 hdr.len = htons(len + 2);
232 if (channel_id < 0 || channel_id >= XBEE_MAX_CHANNEL ||
233 dev->channel[channel_id].registered == 0) {
234 dev->stats.tx_invalid_channel ++;
238 /* calculate the cksum */
241 for (i = 0; i < len; i++)
242 cksum += ((uint8_t *)buf)[i];
243 cksum = 0xff - cksum;
244 dev->stats.tx_frame ++;
246 /* some additional checks before sending */
249 case XBEE_TYPE_ATCMD:
251 dev->stats.tx_atcmd ++;
253 case XBEE_TYPE_ATCMD_Q:
254 dev->stats.tx_atcmd_q ++;
257 dev->stats.tx_data ++;
259 case XBEE_TYPE_EXPL_XMIT:
260 dev->stats.tx_expl_data ++;
262 case XBEE_TYPE_RMT_ATCMD:
263 dev->stats.tx_rmt_atcmd ++;
266 /* invalid commands */
267 case XBEE_TYPE_XMIT_STATUS:
268 case XBEE_TYPE_MODEM_STATUS:
269 case XBEE_TYPE_ATRESP:
271 case XBEE_TYPE_EXPL_RECV:
272 case XBEE_TYPE_NODE_ID:
273 case XBEE_TYPE_RMT_ATRESP:
275 dev->stats.tx_invalid_type ++;
276 fprintf(stderr, "unhandled xmit type=%x\n", hdr.type);
280 /* send the frame on the wire */
281 fwrite((uint8_t *)&hdr, 1, sizeof(hdr), dev->file);
282 fwrite((uint8_t *)buf, 1, len, dev->file);
283 fwrite(&cksum, 1, 1, dev->file);
288 void xbee_proto_rx(struct xbee_dev *dev)
291 struct xbee_hdr *hdr = (struct xbee_hdr *)dev->frame;
297 c = fgetc(dev->file);
301 /* frame too long XXX stats */
302 if (dev->frame_len >= XBEE_MAX_FRAME_LEN) {
307 if (dev->frame_len == 0 && c != XBEE_DELIMITER)
310 dev->frame[dev->frame_len++] = c;
312 /* not enough data to read len */
313 if (dev->frame_len < sizeof(*hdr))
316 framelen = ntohs(hdr->len);
317 framelen += 4; /* 1 for delimiter, 2 for len, 1 for cksum */
319 /* frame too long XXX stats */
320 if (framelen >= XBEE_MAX_FRAME_LEN) {
325 /* not enough data */
326 if (dev->frame_len < framelen)
328 if (xbee_proto_parse_frame(dev) < 0)