2 * Copyright (c) 2014, 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.
36 #include <aversive/endian.h>
37 #include <aversive/wait.h>
38 #include <aversive/pgmspace.h>
43 #include <xbee_rxtx.h>
46 #include "xbee_user.h"
49 #define XBEE_TIMEOUT_MS 1000
50 #define XBEE_POLL_TIMER_MS 5
52 static struct xbee_ctx xbee_ctx[XBEE_MAX_CHANNEL];
54 int xbee_cmdline_input_enabled = 1;
59 static struct callout xbee_rx_poll_timer;
61 static void __hexdump(const void *buf, unsigned int len)
63 unsigned int i, out, ofs;
64 const unsigned char *data = buf;
66 char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
70 /* format 1 line in the buffer, then use printk to print them */
71 out = snprintf_P(line, LINE_LEN, PSTR("%08X"), ofs);
72 for (i=0; ofs+i < len && i<16; i++)
73 out += snprintf_P(line+out, LINE_LEN - out,
77 out += snprintf(line+out, LINE_LEN - out, " ");
78 for (i=0; ofs < len && i<16; i++, ofs++) {
79 unsigned char c = data[ofs];
80 if (!isascii(c) || !isprint(c))
82 out += snprintf_P(line+out,
86 DEBUG(E_USER_XBEE, "%s", line);
90 static void hexdump_msg(const char *title, const struct xbee_msg *msg)
94 DEBUG(E_USER_XBEE, "dump %s", title);
96 for (i = 0; i < msg->iovlen; i++) {
97 DEBUG(E_USER_XBEE, "iovec %d at %p, len=%d", i,
98 msg->iov[i].buf, msg->iov[i].len);
99 __hexdump(msg->iov[i].buf, msg->iov[i].len);
103 void hexdump(const char *title, const void *buf, unsigned int len)
105 DEBUG(E_USER_XBEE, "dump %s at [%p], len=%d", title, buf, len);
109 static int parse_xmit_status(struct xbee_ctx *ctx,
110 struct xbee_xmit_status_hdr *frame, unsigned len)
115 ERROR(E_USER_XBEE, "no context");
119 /* see if it matches a xmit query (atcmd_query must be \0) */
120 if (ctx->atcmd_query[0] != '\0') {
121 ERROR(E_USER_XBEE, "invalid response 2");
125 /* XXX use defines for these values */
126 if (frame->delivery_status == 0x00)
127 NOTICE(E_USER_XBEE, "Success");
128 else if (frame->delivery_status == 0x01)
129 WARNING(E_USER_XBEE, "MAC ACK Failure");
130 else if (frame->delivery_status == 0x15)
131 WARNING(E_USER_XBEE, "Invalid destination endpoint");
132 else if (frame->delivery_status == 0x21)
133 WARNING(E_USER_XBEE, "Network ACK Failure");
134 else if (frame->delivery_status == 0x25)
135 WARNING(E_USER_XBEE, "Route Not Found");
140 /* Write in a human readable format the content of an atcmd response frame. It
141 * assumes the frame is valid .*/
142 void atresp_to_str(char *buf, unsigned len, const struct xbee_atresp_hdr *frame)
149 } __attribute__((packed)) *result;
151 const struct xbee_atcmd *cmd_pgm;
152 struct xbee_atcmd cmd;
154 /* get AT command from frame */
155 memcpy(atcmd_str, &frame->cmd, 2);
158 /* see if it exists */
159 cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
160 if (cmd_pgm == NULL) {
161 snprintf(buf, len, "<%s> (unknown cmd)", atcmd_str);
164 memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
167 result = (void *)frame->data;
168 len -= offsetof(struct xbee_atresp_hdr, data);
169 if (cmd.flags & XBEE_ATCMD_F_PARAM_U8 && len == sizeof(uint8_t))
170 snprintf(buf, len, "<%s> is 0x%x (%d)", atcmd_str,
171 result->u8, result->u8);
172 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U16 && len == sizeof(uint16_t))
173 snprintf(buf, len, "<%s> is 0x%x (%d)", atcmd_str,
174 ntohs(result->u16), ntohs(result->u16));
175 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U32 && len == sizeof(uint32_t))
176 snprintf(buf, len, "<%s> is 0x%"PRIx32" (%"PRIu32")",
177 atcmd_str, ntohl(result->u32), ntohl(result->u32));
178 else if (cmd.flags & XBEE_ATCMD_F_PARAM_S16 && len == sizeof(int16_t))
179 snprintf(buf, len, "<%s> is %d",
180 atcmd_str, ntohs(result->s16));
182 snprintf(buf, len, "<%s> no data", atcmd_str);
185 static int parse_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
192 ERROR(E_USER_XBEE, "no context");
196 /* get AT command from frame */
197 memcpy(atcmd_str, &frame->cmd, 2);
200 /* see if it matches query */
201 if (memcmp(atcmd_str, ctx->atcmd_query, 2)) {
202 ERROR(E_USER_XBEE, "invalid response <%c%c><%s><%s>",
204 (frame->cmd >> 8) & 0xFF,
205 atcmd_str, ctx->atcmd_query);
210 if (frame->status == 1) {
211 WARNING(E_USER_XBEE, "Status is error");
214 else if (frame->status == 2) {
215 WARNING(E_USER_XBEE, "Invalid command");
218 else if (frame->status == 3) {
219 WARNING(E_USER_XBEE, "Invalid parameter");
222 else if (frame->status != 0) {
223 WARNING(E_USER_XBEE, "Unknown status error %d",
228 len -= offsetof(struct xbee_atresp_hdr, data);
230 atresp_to_str(buf, sizeof(buf), frame);
231 NOTICE(E_USER_XBEE, "status ok, len=%d, %s", len, buf);
234 hexdump("atcmd answer", frame->data, len);
240 /* main rx entry point for application */
241 int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type,
242 void *frame, unsigned len, void *opaque)
244 struct xbee_ctx *ctx = opaque;
245 int8_t ret = XBEE_USER_RETCODE_OK;
247 NOTICE(E_USER_XBEE, "type=0x%x, channel=%d, ctx=%p",
250 /* if ctx is !NULL, it is an answer to a query */
252 xbee_unload_timeout(ctx);
253 if (ctx->atcmd_query)
254 NOTICE(E_USER_XBEE, "Received answer to query <%c%c>",
255 ctx->atcmd_query[0], ctx->atcmd_query[1]);
258 /* some additional checks before sending */
260 case XBEE_TYPE_MODEM_STATUS: {
261 NOTICE(E_USER_XBEE, "Received Modem Status frame");
265 case XBEE_TYPE_RMT_ATRESP: {
269 #if BYTE_ORDER == LITTLE_ENDIAN
278 memcpy(&addr, frame, sizeof(addr));
279 addr.u64 = ntohll(addr.u64);
281 "from remote address %"PRIx32"%"PRIx32"",
282 addr.u32.high, addr.u32.low);
284 /* this answer contains an atcmd answer at offset 10 */
285 if (parse_atcmd(ctx, frame + 10, len - 10) < 0)
286 ret = XBEE_USER_RETCODE_BAD_FRAME;
290 case XBEE_TYPE_ATRESP: {
291 if (parse_atcmd(ctx, frame, len) < 0)
292 ret = XBEE_USER_RETCODE_BAD_FRAME;
297 case XBEE_TYPE_XMIT_STATUS: {
298 if (parse_xmit_status(ctx, frame, len) < 0)
299 ret = XBEE_USER_RETCODE_BAD_FRAME;
304 case XBEE_TYPE_RECV: {
305 if (rc_proto_rx(frame, len) < 0)
306 ret = XBEE_USER_RETCODE_BAD_FRAME;
311 case XBEE_TYPE_ATCMD:
312 case XBEE_TYPE_ATCMD_Q:
314 case XBEE_TYPE_EXPL_XMIT:
315 case XBEE_TYPE_RMT_ATCMD:
316 case XBEE_TYPE_EXPL_RECV:
317 case XBEE_TYPE_NODE_ID:
319 ERROR(E_USER_XBEE, "Invalid frame");
320 ret = XBEE_USER_RETCODE_BAD_FRAME;
324 WARNING(E_USER_XBEE, "undecoded rx frame");
325 hexdump("undecoded rx frame", frame, len);
329 if (ctx->rx_cb != NULL)
330 ret = ctx->rx_cb(ret, frame, len, ctx->arg);
332 /* free channel now, it implicitely frees the context too */
333 xbee_unregister_channel(dev, channel);
339 /* called with callout prio == XBEE_PRIO */
340 static int xbeeapp_send(struct xbee_ctx *ctx, int type, struct xbee_msg *msg)
345 /* register a channel */
346 channel = xbee_register_channel(xbee_dev, XBEE_CHANNEL_ANY,
349 ERROR(E_USER_XBEE, "cannot send: no free channel");
353 /* copy context in the static struct table (avoiding a malloc) */
354 memcpy(&xbee_ctx[channel], ctx, sizeof(*ctx));
355 ctx = &xbee_ctx[channel];
356 xbee_set_opaque(xbee_dev, channel, ctx);
358 NOTICE(E_USER_XBEE, "send frame channel=%d type=0x%x", channel, type);
359 hexdump_msg("xmit frame", msg);
361 /* transmit the frame on this channel */
362 ret = xbee_tx_iovec(xbee_dev, channel, type, msg);
364 ERROR(E_USER_XBEE, "cannot send");
365 xbee_unregister_channel(xbee_dev, channel);
369 ctx->channel = channel;
370 xbee_load_timeout(ctx); /* load a timeout event */
375 /* send an AT command with parameters filled by caller. param is the argument
376 * of the atcmd (if any). */
377 int xbeeapp_send_atcmd(char *atcmd_str, void *param, unsigned param_len,
378 xbee_user_rx_cb_t *rx_cb, void *arg)
381 /* struct xbee_atcmd_hdr atcmd_hdr; -> no needed same than atcmd_str */
386 memset(&ctx, 0, sizeof(ctx));
387 ctx.atcmd_query[0] = atcmd_str[0];
388 ctx.atcmd_query[1] = atcmd_str[1];
393 msg.iov[0].buf = atcmd_str;
395 msg.iov[1].buf = param;
396 msg.iov[1].len = param_len;
398 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
399 if (prio > XBEE_PRIO)
400 ERROR(E_USER_XBEE, "invalid prio = %d\n", prio);
401 ret = xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &msg);
402 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
407 int xbeeapp_send_msg(uint64_t addr, struct xbee_msg *msg,
408 xbee_user_rx_cb_t *rx_cb, void *arg)
411 struct xbee_xmit_hdr xmit_hdr;
412 struct xbee_msg msg2;
417 if (msg->iovlen + 2 > XBEE_MSG_MAXIOV) {
418 ERROR(E_USER_XBEE, "too many iovecs");
422 xmit_hdr.dstaddr = htonll(addr);
423 xmit_hdr.reserved = htons(0xFFFE);
424 xmit_hdr.bcast_radius = 0;
427 msg2.iovlen = msg->iovlen + 1;
428 msg2.iov[0].buf = &xmit_hdr;
429 msg2.iov[0].len = sizeof(xmit_hdr);
430 for (i = 0; i < msg->iovlen; i++)
431 msg2.iov[i+1] = msg->iov[i];
433 memset(&ctx, 0, sizeof(ctx));
437 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
438 if (prio > XBEE_PRIO)
439 ERROR(E_USER_XBEE, "invalid prio = %d\n", prio);
440 ret = xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &msg2);
441 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
446 static void evt_timeout(struct callout_mgr *cm, struct callout *clt,
449 struct xbee_ctx *ctx = arg;
454 WARNING(E_USER_XBEE, "Timeout");
457 if (ctx->rx_cb != NULL)
458 ctx->rx_cb(XBEE_USER_RETCODE_TIMEOUT, NULL, 0, ctx->arg);
460 /* free channel, it implicitely frees the context too */
461 xbee_unregister_channel(xbee_dev, ctx->channel);
462 callout_stop(cm, clt);
465 void xbee_load_timeout(struct xbee_ctx *ctx)
469 callout_init(&ctx->timeout, evt_timeout, ctx, XBEE_PRIO);
470 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
471 callout_schedule(&xbeeboard.intr_cm, &ctx->timeout, XBEE_TIMEOUT_MS);
472 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
475 void xbee_unload_timeout(struct xbee_ctx *ctx)
479 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
480 callout_stop(&xbeeboard.intr_cm, &ctx->timeout);
481 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
484 static void xbee_rx_poll_timer_cb(struct callout_mgr *cm, struct callout *tim,
489 callout_reschedule(cm, tim, XBEE_POLL_TIMER_MS);
492 void xbee_mainloop(void)
500 /* from xbee to cmdline */
501 c = xbee_dev_recv(NULL);
503 cmdline_dev_send((uint8_t)c, NULL);
505 /* from cmdline to xbee */
506 c = cmdline_dev_recv(NULL);
507 if (c == 4) { /* CTRL-d */
508 prio = callout_mgr_set_prio(&xbeeboard.intr_cm,
510 xbee_dev_send('A', NULL);
511 xbee_dev_send('T', NULL);
512 xbee_dev_send('C', NULL);
513 xbee_dev_send('N', NULL);
514 xbee_dev_send('\n', NULL);
515 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
517 rdline_newline(&xbeeboard.rdl,
522 xbee_dev_send((uint8_t)c, NULL);
524 /* echo on cmdline */
525 cmdline_dev_send((uint8_t)c, NULL);
529 if (xbee_cmdline_input_enabled)
531 /* xbee rx polling is done in a timer, so we can block
532 * the cmdline without loosing incoming packets */
537 void xbee_stdin_enable(void)
539 xbee_cmdline_input_enabled = 1;
542 void xbee_stdin_disable(void)
544 xbee_cmdline_input_enabled = 0;
547 void xbeeapp_init(void)
549 callout_init(&xbee_rx_poll_timer, xbee_rx_poll_timer_cb,
551 callout_schedule(&xbeeboard.intr_cm, &xbee_rx_poll_timer,