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 * avrdude -p atmega1284p -P usb -c avrispmkii -U lfuse:w:0xff:m -U hfuse:w:0x99:m -U efuse:w:0xff:m
33 #include <aversive/queue.h>
34 #include <aversive/endian.h>
35 #include <aversive/wait.h>
36 #include <aversive/error.h>
49 #include <scheduler.h>
50 #include <clock_time.h>
55 #include "xbee_neighbor.h"
56 #include "xbee_atcmd.h"
57 #include "xbee_stats.h"
59 #include "xbee_proto.h"
64 #include "spi_servo.h"
67 struct xbeeboard xbeeboard;
68 volatile uint16_t global_ms;
69 struct callout_manager cm;
71 #define TIMEOUT_MS 1000
73 /* global xbee device */
74 struct xbee_dev *xbee_dev;
77 //static struct event stdin_read_event, xbee_read_event;
84 int xbee_cmdline_input_enabled = 1;
86 static struct xbee_ctx xbee_ctx[XBEE_MAX_CHANNEL];
88 static void hexdump(const char *title, const void *buf, unsigned int len)
90 unsigned int i, out, ofs;
91 const unsigned char *data = buf;
93 char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
95 printf_P(PSTR("%s at [%p], len=%d\r\n"), title, data, len);
98 /* format 1 line in the buffer, then use printk to print them */
99 out = snprintf_P(line, LINE_LEN, PSTR("%08X"), ofs);
100 for (i=0; ofs+i < len && i<16; i++)
101 out += snprintf_P(line+out, LINE_LEN - out,
105 out += snprintf(line+out, LINE_LEN - out, " ");
106 for (i=0; ofs < len && i<16; i++, ofs++) {
107 unsigned char c = data[ofs];
108 if (!isascii(c) || !isprint(c))
110 out += snprintf_P(line+out,
114 printf_P(PSTR("%s\r\n"), line);
118 static int parse_xmit_status(struct xbee_ctx *ctx,
119 struct xbee_xmit_status_hdr *frame, unsigned len)
124 printf_P(PSTR("no context\r\n"));
128 /* see if it matches a xmit query (atcmd_query must be NULL) */
129 if (ctx->atcmd_query[0] != '\0') {
130 printf_P(PSTR("invalid response 2\r\n"));
134 /* XXX use defines for these values */
135 if (frame->delivery_status == 0x00)
136 printf_P(PSTR("Success\r\n"));
137 else if (frame->delivery_status == 0x01)
138 printf_P(PSTR("MAC ACK Failure\r\n"));
139 else if (frame->delivery_status == 0x15)
140 printf_P(PSTR("Invalid destination endpoint\r\n"));
141 else if (frame->delivery_status == 0x21)
142 printf_P(PSTR("Network ACK Failure\r\n"));
143 else if (frame->delivery_status == 0x25)
144 printf_P(PSTR("Route Not Found\r\n"));
149 static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
153 const struct xbee_atcmd *cmd_pgm;
154 struct xbee_atcmd cmd;
160 } __attribute__((packed)) *result;
163 printf_P(PSTR("no context\r\n"));
167 /* get AT command from frame */
168 memcpy(atcmd_str, &frame->cmd, 2);
171 /* see if it matches query */
172 if (memcmp(atcmd_str, ctx->atcmd_query, 2)) {
173 printf_P(PSTR("invalid response <%c%c><%s><%s>\r\n"),
175 (frame->cmd >> 8) & 0xFF,
176 atcmd_str, ctx->atcmd_query);
180 /* see if it exists */
181 cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
182 if (cmd_pgm == NULL) {
183 printf_P(PSTR("unknown response\r\n"));
186 memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
189 if (frame->status == 1) {
190 printf_P(PSTR("Status is error\r\n"));
193 else if (frame->status == 2) {
194 printf_P(PSTR("Invalid command\r\n"));
197 else if (frame->status == 3) {
198 printf_P(PSTR("Invalid parameter\r\n"));
201 else if (frame->status != 0) {
202 printf_P(PSTR("Unknown status error %d\r\n"), frame->status);
207 if (ctx->func != NULL)
208 ctx->func(frame, len, ctx->arg);
211 result = (void *)frame->data;
212 len -= offsetof(struct xbee_atresp_hdr, data);
213 if (cmd.flags & XBEE_ATCMD_F_PARAM_U8 && len == sizeof(uint8_t))
214 printf_P(PSTR("<%s> is 0x%x\r\n"), atcmd_str, result->u8);
215 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U16 && len == sizeof(uint16_t))
216 printf_P(PSTR("<%s> is 0x%x\r\n"),
219 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U32 && len == sizeof(uint32_t))
220 printf_P(PSTR("<%s> is 0x%"PRIx32"\r\n"),
223 else if (cmd.flags & XBEE_ATCMD_F_PARAM_S16 && len == sizeof(int16_t))
224 printf_P(PSTR("<%s> is %d\r\n"), atcmd_str, ntohs(result->s16));
226 printf_P(PSTR("no data, status ok\r\n"));
228 hexdump("atcmd answer", frame->data, len);
235 int xbee_recv_data(struct xbee_recv_hdr *recvframe, unsigned len)
237 unsigned int datalen;
238 struct rc_proto_hdr *rch = (struct rc_proto_hdr *) &recvframe->data;
240 if (len < sizeof(*recvframe))
243 datalen = len - sizeof(*recvframe);
244 if (datalen < sizeof(struct rc_proto_hdr))
248 case RC_PROTO_TYPE_CHANNEL: {
249 struct rc_proto_channel *rcc =
250 (struct rc_proto_channel *) recvframe->data;
252 if (datalen != sizeof(struct rc_proto_channel))
254 val = ntohs(rcc->axis[0]);
257 spi_servo_set(0, val);
260 case RC_PROTO_TYPE_RANGE: {
261 struct rc_proto_range *rcr =
262 (struct rc_proto_range *) recvframe->data;
264 if (datalen != sizeof(struct rc_proto_range))
267 if (rcr->power_level >= MAX_POWER_LEVEL)
270 rc_proto_rx_range(rcr->power_level);
281 /* socat /dev/ttyUSB0,raw,echo=0,b115200 /dev/ttyACM1,raw,echo=0,b115200 */
282 void xbee_rx(struct xbee_dev *dev, int channel, int type,
283 void *frame, unsigned len, void *opaque)
285 struct xbee_ctx *ctx = opaque;
286 int do_hexdump = xbee_hexdump;
289 printf_P(PSTR("type=0x%x, channel=%d, ctx=%p\r\n"),
292 /* if ctx is !NULL, it is an answer to a query */
294 /* XXX only delete timeout if answer matched */
295 xbee_unload_timeout(ctx);
296 if (xbee_debug && ctx->atcmd_query)
297 printf_P(PSTR("Received answer to query <%c%c>\r\n"),
298 ctx->atcmd_query[0], ctx->atcmd_query[1]);
301 /* some additional checks before sending */
303 case XBEE_TYPE_MODEM_STATUS: {
304 printf_P(PSTR("Received Modem Status frame\r\n"));
308 case XBEE_TYPE_RMT_ATRESP: {
312 #if BYTE_ORDER == LITTLE_ENDIAN
321 memcpy(&addr, frame, sizeof(addr));
322 addr.u64 = ntohll(addr.u64);
323 printf_P(PSTR("from remote address %"PRIx32"%"PRIx32"\r\n"),
324 addr.u32.high, addr.u32.low);
326 /* this answer contains an atcmd answer at offset 10 */
327 if (dump_atcmd(ctx, frame + 10, len - 10) < 0)
331 case XBEE_TYPE_ATRESP: {
332 if (dump_atcmd(ctx, frame, len) < 0)
337 case XBEE_TYPE_XMIT_STATUS: {
338 if (parse_xmit_status(ctx, frame, len) < 0)
343 case XBEE_TYPE_RECV: {
344 if (xbee_recv_data(frame, len) < 0)
349 case XBEE_TYPE_ATCMD:
350 case XBEE_TYPE_ATCMD_Q:
352 case XBEE_TYPE_EXPL_XMIT:
353 case XBEE_TYPE_RMT_ATCMD:
354 case XBEE_TYPE_EXPL_RECV:
355 case XBEE_TYPE_NODE_ID:
357 printf_P(PSTR("Invalid frame\r\n"));
363 hexdump("undecoded rx frame", frame, len);
365 /* restart command line if it was a blocking query */
367 xbee_unregister_channel(dev, channel);
368 if (ctx->foreground) {
370 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
375 static int xbeeapp_send(struct xbee_ctx *ctx, int type, void *buf, unsigned len,
381 if (len > XBEE_MAX_FRAME_LEN) {
382 printf_P(PSTR("frame too large\r\n"));
386 /* register a channel */
387 channel = xbee_register_channel(xbee_dev, XBEE_CHANNEL_ANY,
390 printf_P(PSTR("cannot send: no free channel\r\n"));
394 /* copy context in the static struct table (avoiding a malloc) */
395 memcpy(&xbee_ctx[channel], ctx, sizeof(*ctx));
396 ctx = &xbee_ctx[channel];
397 xbee_set_opaque(xbee_dev, channel, ctx);
400 printf_P(PSTR("send frame channel=%d type=0x%x len=%d\r\n"),
403 hexdump("xmit frame", buf, len);
405 /* transmit the frame on this channel */
406 ret = xbee_proto_xmit(xbee_dev, channel, type, buf,
409 printf_P(PSTR("cannot send\r\n"));
410 xbee_unregister_channel(xbee_dev, channel);
414 ctx->channel = channel;
415 xbee_load_timeout(ctx); /* load a timeout event */
417 /* suspend command line until we have answer or timeout */
420 rdline_stop(&xbeeboard.rdl); /* don't display prompt when return */
421 xbee_stdin_disable(); /* unload file descriptor polling */
427 /* send an AT command with parameters filled by caller. Disable
428 * command line until we get the answer or until a timeout occurs */
429 int xbeeapp_send_atcmd(const char *atcmd_str,
430 void *param, unsigned param_len, int foreground,
431 int (*func)(void *frame, unsigned len, void *arg), void *arg)
435 struct xbee_atcmd_hdr atcmd;
436 char buf[XBEE_MAX_FRAME_LEN];
437 } __attribute__((packed)) frame;
439 memset(&ctx, 0, sizeof(ctx));
440 ctx.atcmd_query[0] = atcmd_str[0];
441 ctx.atcmd_query[1] = atcmd_str[1];
445 memcpy(&frame.atcmd.cmd, atcmd_str, 2);
446 memcpy(&frame.buf, param, param_len);
448 if (xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &frame,
449 sizeof(struct xbee_atcmd_hdr) +
450 param_len, foreground) < 0) {
457 int xbeeapp_send_msg(uint64_t addr, void *data,
458 unsigned data_len, int foreground)
462 struct xbee_xmit_hdr xmit;
463 char buf[XBEE_MAX_FRAME_LEN];
464 } __attribute__((packed)) frame;
466 memset(&ctx, 0, sizeof(ctx));
467 ctx.atcmd_query[0] = '\0';
469 frame.xmit.dstaddr = htonll(addr);
470 frame.xmit.reserved = htons(0xFFFE);
471 frame.xmit.bcast_radius = 0;
473 memcpy(&frame.buf, data, data_len);
475 if (xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &frame,
476 sizeof(struct xbee_xmit_hdr) +
477 data_len, foreground) < 0) {
484 void xbee_stdin_enable(void)
486 xbee_cmdline_input_enabled = 1;
489 void xbee_stdin_disable(void)
491 xbee_cmdline_input_enabled = 0;
494 static void evt_timeout(struct callout_manager *cm, struct callout *clt,
497 struct xbee_ctx *ctx = arg;
502 printf_P(PSTR("Timeout\r\n"));
504 /* restart command line */
506 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
509 xbee_unregister_channel(xbee_dev, ctx->channel);
512 void xbee_load_timeout(struct xbee_ctx *ctx)
514 callout_reset(&cm, &ctx->timeout, TIMEOUT_MS, SINGLE, evt_timeout, ctx);
517 void xbee_unload_timeout(struct xbee_ctx *ctx)
519 callout_stop(&cm, &ctx->timeout);
522 void bootloader(void)
524 #define BOOTLOADER_ADDR 0x3f000
525 if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
526 printf_P(PSTR("Bootloader is not present\r\n"));
530 /* ... very specific :( */
544 /* __asm__ __volatile__ ("ldi r31,0xf8\n"); */
545 /* __asm__ __volatile__ ("ldi r30,0x00\n"); */
546 /* __asm__ __volatile__ ("eijmp\n"); */
549 void xbee_mainloop(void)
557 /* from xbee to cmdline */
558 c = xbee_dev_recv(NULL);
560 cmdline_dev_send((uint8_t)c, NULL);
562 /* from cmdline to xbee */
563 c = cmdline_dev_recv(NULL);
564 if (c == 4) { /* CTRL-d */
566 rdline_newline(&xbeeboard.rdl,
571 xbee_dev_send((uint8_t)c, NULL);
573 /* echo on cmdline */
574 cmdline_dev_send((uint8_t)c, NULL);
578 if (xbee_cmdline_input_enabled)
580 xbee_proto_rx(xbee_dev);
585 /* return time in milliseconds on unsigned 16 bits */
586 static uint16_t get_time_ms(void)
591 static void increment_ms(void *dummy)
596 if (global_ms & 0x80)
602 static void main_timer_interrupt(void)
604 static uint8_t cpt = 0;
607 if ((cpt & 0x3) == 0)
608 scheduler_interrupt();
611 /** Main program entry point. This routine contains the overall program flow, including initial
612 * setup of all components and the main program loop.
621 DDRA = 0x07; /* LEDs */
628 uart_register_rx_event(CMDLINE_UART, emergency);
630 fdevopen(cmdline_dev_send, cmdline_dev_recv);
631 xbee_file = fdevopen(xbee_dev_send, xbee_dev_recv);
634 timer0_register_OV_intr(main_timer_interrupt);
636 scheduler_add_periodical_event_priority(increment_ms, NULL,
637 1000L / SCHEDULER_UNIT,
642 printf_P(PSTR("\r\n"));
643 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
645 callout_manager_init(&cm, get_time_ms);
646 //callout_reset(&cm, &t1, 500, PERIODICAL, do_led_blink, NULL);
648 /* initialize libxbee */
655 /* open xbee device */
656 if (xbee_open(xbee_dev, xbee_file) < 0)
659 /* register default channel with a callback */
660 if (xbee_register_channel(xbee_dev, XBEE_DEFAULT_CHANNEL,
661 xbee_rx, NULL) < 0) {
662 fprintf(stderr, "cannot register default channel\n");