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>
31 #include <aversive/wait.h>
32 #include <aversive/error.h>
45 #include <scheduler.h>
46 #include <clock_time.h>
51 #include "xbee_neighbor.h"
52 #include "xbee_atcmd.h"
53 #include "xbee_stats.h"
55 #include "xbee_proto.h"
60 #include "spi_servo.h"
63 struct xbeeboard xbeeboard;
64 volatile uint16_t global_ms;
65 struct callout_manager cm;
67 #define TIMEOUT_MS 1000
69 /* global xbee device */
70 struct xbee_dev *xbee_dev;
73 //static struct event stdin_read_event, xbee_read_event;
80 int xbee_cmdline_input_enabled = 1;
82 static struct xbee_ctx xbee_ctx[XBEE_MAX_CHANNEL];
84 static void hexdump(const char *title, const void *buf, unsigned int len)
86 unsigned int i, out, ofs;
87 const unsigned char *data = buf;
89 char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
91 printf_P(PSTR("%s at [%p], len=%d\r\n"), title, data, len);
94 /* format 1 line in the buffer, then use printk to print them */
95 out = snprintf_P(line, LINE_LEN, PSTR("%08X"), ofs);
96 for (i=0; ofs+i < len && i<16; i++)
97 out += snprintf_P(line+out, LINE_LEN - out,
101 out += snprintf(line+out, LINE_LEN - out, " ");
102 for (i=0; ofs < len && i<16; i++, ofs++) {
103 unsigned char c = data[ofs];
104 if (!isascii(c) || !isprint(c))
106 out += snprintf_P(line+out,
110 printf_P(PSTR("%s\r\n"), line);
114 static int parse_xmit_status(struct xbee_ctx *ctx,
115 struct xbee_xmit_status_hdr *frame, unsigned len)
118 printf_P(PSTR("no context\r\n"));
122 /* see if it matches a xmit query (atcmd_query must be NULL) */
123 if (ctx->atcmd_query[0] != '\0') {
124 printf_P(PSTR("invalid response 2\r\n"));
128 /* XXX use defines for these values */
129 if (frame->delivery_status == 0x00)
130 printf_P(PSTR("Success\r\n"));
131 else if (frame->delivery_status == 0x01)
132 printf_P(PSTR("MAC ACK Failure\r\n"));
133 else if (frame->delivery_status == 0x15)
134 printf_P(PSTR("Invalid destination endpoint\r\n"));
135 else if (frame->delivery_status == 0x21)
136 printf_P(PSTR("Network ACK Failure\r\n"));
137 else if (frame->delivery_status == 0x25)
138 printf_P(PSTR("Route Not Found\r\n"));
143 static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
147 struct xbee_atcmd_pgm *cmd_pgm;
148 struct xbee_atcmd cmd;
154 } __attribute__((packed)) *result;
157 printf_P(PSTR("no context\r\n"));
161 /* get AT command from frame */
162 memcpy(atcmd_str, &frame->cmd, 2);
165 /* see if it matches query */
166 if (memcmp(atcmd_str, ctx->atcmd_query, 2)) {
167 printf_P(PSTR("invalid response <%c%c><%s><%s>\r\n"),
169 (frame->cmd >> 8) & 0xFF,
170 atcmd_str, ctx->atcmd_query);
174 /* see if it exists */
175 cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
176 if (cmd_pgm == NULL) {
177 printf_P(PSTR("unknown response\r\n"));
180 memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
183 if (frame->status == 1) {
184 printf_P(PSTR("Status is error\r\n"));
187 else if (frame->status == 2) {
188 printf_P(PSTR("Invalid command\r\n"));
191 else if (frame->status == 3) {
192 printf_P(PSTR("Invalid parameter\r\n"));
195 else if (frame->status != 0) {
196 printf_P(PSTR("Unknown status error %d\r\n"), frame->status);
201 if (ctx->func != NULL)
202 ctx->func(frame, len, ctx->arg);
205 result = (void *)frame->data;
206 len -= offsetof(struct xbee_atresp_hdr, data);
207 if (cmd.flags & XBEE_ATCMD_F_PARAM_U8 && len == sizeof(uint8_t))
208 printf_P(PSTR("<%s> is 0x%x\r\n"), atcmd_str, result->u8);
209 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U16 && len == sizeof(uint16_t))
210 printf_P(PSTR("<%s> is 0x%x\r\n"),
213 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U32 && len == sizeof(uint32_t))
214 printf_P(PSTR("<%s> is 0x%"PRIx32"\r\n"),
217 else if (cmd.flags & XBEE_ATCMD_F_PARAM_S16 && len == sizeof(int16_t))
218 printf_P(PSTR("<%s> is %d\r\n"), atcmd_str, ntohs(result->s16));
220 printf_P(PSTR("no data, status ok\r\n"));
222 hexdump("atcmd answer", frame->data, len);
229 int xbee_recv_data(struct xbee_recv_hdr *recvframe, unsigned len)
231 int datalen = len - sizeof(*recvframe);
232 struct rc_proto_hdr *rch = (struct rc_proto_hdr *) &recvframe->data;
234 if (datalen < sizeof(struct rc_proto_hdr))
238 case RC_PROTO_TYPE_CHANNEL:
239 if (datalen != sizeof(struct rc_proto_channel))
242 case RC_PROTO_TYPE_RANGE: {
243 struct rc_proto_range *rcr =
244 (struct rc_proto_range *) recvframe->data;
246 if (datalen != sizeof(struct rc_proto_range))
249 if (rcr->power_level >= MAX_POWER_LEVEL)
252 rc_proto_rx_range(rcr->power_level);
263 /* socat /dev/ttyUSB0,raw,echo=0,b115200 /dev/ttyACM1,raw,echo=0,b115200 */
264 void xbee_rx(struct xbee_dev *dev, int channel, int type,
265 void *frame, unsigned len, void *opaque)
267 struct xbee_ctx *ctx = opaque;
268 int do_hexdump = xbee_hexdump;
271 printf_P(PSTR("type=0x%x, channel=%d, ctx=%p\r\n"),
274 /* if ctx is !NULL, it is an answer to a query */
276 /* XXX only delete timeout if answer matched */
277 xbee_unload_timeout(ctx);
278 if (xbee_debug && ctx->atcmd_query)
279 printf_P(PSTR("Received answer to query <%c%c>\r\n"),
280 ctx->atcmd_query[0], ctx->atcmd_query[1]);
283 /* some additional checks before sending */
285 case XBEE_TYPE_MODEM_STATUS: {
286 printf_P(PSTR("Received Modem Status frame\r\n"));
290 case XBEE_TYPE_RMT_ATRESP: {
294 #if BYTE_ORDER == LITTLE_ENDIAN
303 memcpy(&addr, frame, sizeof(addr));
304 addr.u64 = ntohll(addr.u64);
305 printf_P(PSTR("from remote address %"PRIx32"%"PRIx32"\r\n"),
306 addr.u32.high, addr.u32.low);
308 /* this answer contains an atcmd answer at offset 10 */
309 if (dump_atcmd(ctx, frame + 10, len - 10) < 0)
313 case XBEE_TYPE_ATRESP: {
314 if (dump_atcmd(ctx, frame, len) < 0)
319 case XBEE_TYPE_XMIT_STATUS: {
320 if (parse_xmit_status(ctx, frame, len) < 0)
325 case XBEE_TYPE_RECV: {
326 if (xbee_recv_data(frame, len) < 0)
331 case XBEE_TYPE_ATCMD:
332 case XBEE_TYPE_ATCMD_Q:
334 case XBEE_TYPE_EXPL_XMIT:
335 case XBEE_TYPE_RMT_ATCMD:
336 case XBEE_TYPE_EXPL_RECV:
337 case XBEE_TYPE_NODE_ID:
339 printf_P(PSTR("Invalid frame\r\n"));
345 hexdump("undecoded rx frame", frame, len);
347 /* restart command line if it was a blocking query */
349 xbee_unregister_channel(dev, channel);
350 if (ctx->foreground) {
352 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
357 static int xbeeapp_send(struct xbee_ctx *ctx, int type, void *buf, unsigned len,
363 if (len > XBEE_MAX_FRAME_LEN) {
364 printf_P(PSTR("frame too large\r\n"));
368 /* register a channel */
369 channel = xbee_register_channel(xbee_dev, XBEE_CHANNEL_ANY,
372 printf_P(PSTR("cannot send: no free channel\r\n"));
376 /* copy context in the static struct table (avoiding a malloc) */
377 memcpy(&xbee_ctx[channel], ctx, sizeof(*ctx));
378 ctx = &xbee_ctx[channel];
379 xbee_set_opaque(xbee_dev, channel, ctx);
382 printf_P(PSTR("send frame channel=%d type=0x%x len=%d\r\n"),
385 hexdump("xmit frame", buf, len);
387 /* transmit the frame on this channel */
388 ret = xbee_proto_xmit(xbee_dev, channel, type, buf,
391 printf_P(PSTR("cannot send\r\n"));
392 xbee_unregister_channel(xbee_dev, channel);
396 ctx->channel = channel;
397 xbee_load_timeout(ctx); /* load a timeout event */
399 /* suspend command line until we have answer or timeout */
402 rdline_stop(&xbeeboard.rdl); /* don't display prompt when return */
403 xbee_stdin_disable(); /* unload file descriptor polling */
409 /* send an AT command with parameters filled by caller. Disable
410 * command line until we get the answer or until a timeout occurs */
411 int xbeeapp_send_atcmd(const char *atcmd_str,
412 void *param, unsigned param_len, int foreground,
413 int (*func)(void *frame, unsigned len, void *arg), void *arg)
417 struct xbee_atcmd_hdr atcmd;
418 char buf[XBEE_MAX_FRAME_LEN];
419 } __attribute__((packed)) frame;
421 memset(&ctx, 0, sizeof(ctx));
422 ctx.atcmd_query[0] = atcmd_str[0];
423 ctx.atcmd_query[1] = atcmd_str[1];
427 memcpy(&frame.atcmd.cmd, atcmd_str, 2);
428 memcpy(&frame.buf, param, param_len);
430 if (xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &frame,
431 sizeof(struct xbee_atcmd_hdr) +
432 param_len, foreground) < 0) {
439 int xbeeapp_send_msg(uint64_t addr, void *data,
440 unsigned data_len, int foreground)
444 struct xbee_xmit_hdr xmit;
445 char buf[XBEE_MAX_FRAME_LEN];
446 } __attribute__((packed)) frame;
448 memset(&ctx, 0, sizeof(ctx));
449 ctx.atcmd_query[0] = '\0';
451 frame.xmit.dstaddr = htonll(addr);
452 frame.xmit.reserved = htons(0xFFFE);
453 frame.xmit.bcast_radius = 0;
455 memcpy(&frame.buf, data, data_len);
457 if (xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &frame,
458 sizeof(struct xbee_xmit_hdr) +
459 data_len, foreground) < 0) {
466 void xbee_stdin_enable(void)
468 xbee_cmdline_input_enabled = 1;
471 void xbee_stdin_disable(void)
473 xbee_cmdline_input_enabled = 0;
476 static void evt_timeout(struct callout_manager *cm, struct callout *clt,
479 struct xbee_ctx *ctx = arg;
481 printf_P(PSTR("Timeout\r\n"));
483 /* restart command line */
485 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
488 xbee_unregister_channel(xbee_dev, ctx->channel);
491 void xbee_load_timeout(struct xbee_ctx *ctx)
493 callout_reset(&cm, &ctx->timeout, TIMEOUT_MS, SINGLE, evt_timeout, ctx);
496 void xbee_unload_timeout(struct xbee_ctx *ctx)
498 callout_stop(&cm, &ctx->timeout);
501 void bootloader(void)
504 #define BOOTLOADER_ADDR 0x3f000
505 if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
506 printf_P(PSTR("Bootloader is not present\r\n"));
510 /* ... very specific :( */
528 __asm__ __volatile__ ("ldi r31,0xf8\n");
529 __asm__ __volatile__ ("ldi r30,0x00\n");
530 __asm__ __volatile__ ("eijmp\n");
534 void xbee_mainloop(void)
542 /* from xbee to cmdline */
543 c = xbee_dev_recv(NULL);
545 cmdline_dev_send((uint8_t)c, NULL);
547 /* from cmdline to xbee */
548 c = cmdline_dev_recv(NULL);
549 if (c == 4) { /* CTRL-d */
551 rdline_newline(&xbeeboard.rdl,
556 xbee_dev_send((uint8_t)c, NULL);
558 /* echo on cmdline */
559 cmdline_dev_send((uint8_t)c, NULL);
563 if (xbee_cmdline_input_enabled)
565 xbee_proto_rx(xbee_dev);
569 CDC_Device_USBTask(&VirtualSerial1_CDC_Interface);
570 CDC_Device_USBTask(&VirtualSerial2_CDC_Interface);
576 /* return time in milliseconds on unsigned 16 bits */
577 static uint16_t get_time_ms(void)
582 static void do_led_blink(struct callout_manager *cm,
583 struct callout *clt, void *dummy)
585 static uint8_t a = 0;
591 LEDs_SetAllLEDs(0xff);
598 static void increment_ms(void *dummy)
603 static void main_timer_interrupt(void)
605 static uint8_t cpt = 0;
608 if ((cpt & 0x3) == 0)
609 scheduler_interrupt();
612 /** Main program entry point. This routine contains the overall program flow, including initial
613 * setup of all components and the main program loop.
626 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
631 fdevopen(cmdline_dev_send, cmdline_dev_recv);
632 xbee_file = fdevopen(xbee_dev_send, xbee_dev_recv);
635 timer0_register_OV_intr(main_timer_interrupt);
637 scheduler_add_periodical_event_priority(increment_ms, NULL,
638 1000L / SCHEDULER_UNIT,
642 /* in usb mode, it's done in usb callback */
643 printf_P(PSTR("\r\n"));
644 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
646 callout_manager_init(&cm, get_time_ms);
647 callout_reset(&cm, &t1, 500, PERIODICAL, do_led_blink, NULL);
649 /* initialize libxbee */
656 /* open xbee device */
657 if (xbee_open(xbee_dev, xbee_file) < 0)
660 /* register default channel with a callback */
661 if (xbee_register_channel(xbee_dev, XBEE_DEFAULT_CHANNEL,
662 xbee_rx, NULL) < 0) {
663 fprintf(stderr, "cannot register default channel\n");