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/pgmspace.h>
30 #include <aversive/queue.h>
31 #include <aversive/endian.h>
41 #include "xbee_neighbor.h"
42 #include "xbee_stats.h"
44 #include "xbee_proto.h"
47 /* return -1 if the frame is invalid */
48 static int xbee_proto_parse_atresp(struct xbee_dev *dev, void *buf,
51 struct xbee_atresp_hdr *atresp_hdr;
53 dev->stats.rx_atresp++;
55 if (len < sizeof(struct xbee_hdr) + sizeof(struct xbee_atresp_hdr)) {
56 dev->stats.rx_frame_too_small++;
60 atresp_hdr = buf + sizeof(struct xbee_hdr);
62 /* bad status, but let the frame continue */
63 if (atresp_hdr->status != 0)
64 dev->stats.rx_atresp_error++;
69 /* return -1 if the frame is invalid */
70 static int xbee_proto_parse_rmt_atresp(struct xbee_dev *dev, void *buf,
73 struct xbee_rmt_atresp_hdr *rmt_atresp_hdr;
75 dev->stats.rx_rmt_atresp++;
77 if (len < sizeof(struct xbee_hdr) + sizeof(struct xbee_rmt_atresp_hdr)) {
78 dev->stats.rx_frame_too_small++;
82 rmt_atresp_hdr = buf + sizeof(struct xbee_hdr);
84 /* bad status, but let the frame continue */
85 if (rmt_atresp_hdr->status != 0)
86 dev->stats.rx_rmt_atresp_error++;
91 /* return -1 if the frame is invalid */
92 static int xbee_proto_parse_xmit_status(struct xbee_dev *dev, void *buf,
95 struct xbee_xmit_status_hdr *xmit_status_hdr;
97 dev->stats.rx_xmit_status++;
99 if (len < sizeof(struct xbee_hdr) + sizeof(struct xbee_xmit_status_hdr)) {
100 dev->stats.rx_frame_too_small++;
104 xmit_status_hdr = buf + sizeof(struct xbee_hdr);
105 dev->stats.tx_xmit_retries += xmit_status_hdr->xmit_retry_cnt;
107 /* bad status, but let the frame continue */
108 if (xmit_status_hdr->delivery_status != 0)
109 dev->stats.rx_xmit_status_error++;
114 /* parse the frame stored in the device: return 0 if the frame is
115 * valid, else a negative value */
116 static int xbee_proto_parse_frame(struct xbee_dev *dev)
118 void *buf = dev->frame;
119 uint8_t len = dev->frame_len;
121 struct xbee_hdr *hdr = buf;
124 int channel = XBEE_DEFAULT_CHANNEL;
126 dev->stats.rx_frame++;
129 case XBEE_TYPE_MODEM_STATUS:
131 case XBEE_TYPE_EXPL_RECV:
132 hdrlen = sizeof(struct xbee_hdr) - 1; /* no frame ID */
135 hdrlen = sizeof(struct xbee_hdr);
139 /* check frame len */
140 if (len < (hdrlen + 1)) {
141 dev->stats.rx_frame_too_small++;
142 fprintf_P(stderr, PSTR("Frame too small\r\n"));
146 /* validate the cksum */
147 for (i = 3; i < (len - 1); i++)
148 cksum += ((uint8_t *)buf)[i];
149 cksum = 0xff - cksum;
150 if (cksum != ((uint8_t *)buf)[len-1]) {
151 fprintf_P(stderr, PSTR("Invalid cksum\r\n"));
152 dev->stats.rx_invalid_cksum++;
158 case XBEE_TYPE_MODEM_STATUS:
159 dev->stats.rx_modem_status++;
160 channel = XBEE_DEFAULT_CHANNEL;
162 case XBEE_TYPE_ATRESP:
163 if (xbee_proto_parse_atresp(dev, buf, len) < 0)
167 case XBEE_TYPE_RMT_ATRESP:
168 if (xbee_proto_parse_rmt_atresp(dev, buf, len) < 0)
172 case XBEE_TYPE_XMIT_STATUS:
173 if (xbee_proto_parse_xmit_status(dev, buf, len) < 0)
178 dev->stats.rx_data++;
179 channel = XBEE_DEFAULT_CHANNEL;
181 case XBEE_TYPE_EXPL_RECV:
182 dev->stats.rx_expl_data++;
183 channel = XBEE_DEFAULT_CHANNEL;
185 case XBEE_TYPE_NODE_ID:
186 dev->stats.rx_node_id++;
187 channel = hdr->id; //XXX
189 /* invalid commands */
190 case XBEE_TYPE_ATCMD:
191 case XBEE_TYPE_ATCMD_Q:
193 case XBEE_TYPE_EXPL_XMIT:
194 case XBEE_TYPE_RMT_ATCMD:
196 dev->stats.rx_invalid_type++;
200 /* fallback to default channel if not registered */
201 if (channel < 0 || channel >= XBEE_MAX_CHANNEL ||
202 dev->channel[channel].registered == 0)
203 channel = XBEE_DEFAULT_CHANNEL;
205 /* execute the callback if any */
206 if (dev->channel[channel].rx_cb != NULL)
207 dev->channel[channel].rx_cb(dev, channel, hdr->type,
210 dev->channel[channel].arg);
215 int xbee_proto_xmit(struct xbee_dev *dev, uint8_t channel_id, uint8_t type,
216 void *buf, unsigned len)
222 /* there is no empty message, so return an error */
226 /* prepare an iovec to avoid a copy: prepend a header to the
227 * buffer and append a checksum */
228 hdr.delimiter = XBEE_DELIMITER;
229 hdr.len = htons(len + 2);
233 if (channel_id >= XBEE_MAX_CHANNEL ||
234 dev->channel[channel_id].registered == 0) {
235 dev->stats.tx_invalid_channel ++;
239 /* calculate the cksum */
242 for (i = 0; i < len; i++)
243 cksum += ((uint8_t *)buf)[i];
244 cksum = 0xff - cksum;
245 dev->stats.tx_frame ++;
247 /* some additional checks before sending */
250 case XBEE_TYPE_ATCMD:
252 dev->stats.tx_atcmd ++;
254 case XBEE_TYPE_ATCMD_Q:
255 dev->stats.tx_atcmd_q ++;
258 dev->stats.tx_data ++;
260 case XBEE_TYPE_EXPL_XMIT:
261 dev->stats.tx_expl_data ++;
263 case XBEE_TYPE_RMT_ATCMD:
264 dev->stats.tx_rmt_atcmd ++;
267 /* invalid commands */
268 case XBEE_TYPE_XMIT_STATUS:
269 case XBEE_TYPE_MODEM_STATUS:
270 case XBEE_TYPE_ATRESP:
272 case XBEE_TYPE_EXPL_RECV:
273 case XBEE_TYPE_NODE_ID:
274 case XBEE_TYPE_RMT_ATRESP:
276 dev->stats.tx_invalid_type ++;
277 fprintf_P(stderr, PSTR("unhandled xmit type=%x\r\n"),
282 /* send the frame on the wire */
283 fwrite((uint8_t *)&hdr, 1, sizeof(hdr), dev->file);
284 fwrite((uint8_t *)buf, 1, len, dev->file);
285 fwrite(&cksum, 1, 1, dev->file);
290 void xbee_proto_rx(struct xbee_dev *dev)
293 struct xbee_hdr *hdr = (struct xbee_hdr *)dev->frame;
299 c = fgetc(dev->file);
303 /* frame too long XXX stats */
304 if (dev->frame_len >= XBEE_MAX_FRAME_LEN) {
309 if (dev->frame_len == 0 && c != XBEE_DELIMITER)
312 dev->frame[dev->frame_len++] = c;
314 /* not enough data to read len */
315 if (dev->frame_len < sizeof(*hdr))
318 framelen = ntohs(hdr->len);
319 framelen += 4; /* 1 for delimiter, 2 for len, 1 for cksum */
321 /* frame too long XXX stats */
322 if (framelen >= XBEE_MAX_FRAME_LEN) {
327 /* not enough data */
328 if (dev->frame_len < framelen)
330 if (xbee_proto_parse_frame(dev) < 0) {