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"
62 struct xbeeboard xbeeboard;
64 #define TIMEOUT_MS 1000
66 /* global xbee device */
67 struct xbee_dev *xbee_dev;
70 //static struct event stdin_read_event, xbee_read_event;
77 int xbee_cmdline_input_enabled = 1;
79 static struct xbee_ctx xbee_ctx[XBEE_MAX_CHANNEL];
81 static void hexdump(const char *title, const void *buf, unsigned int len)
83 unsigned int i, out, ofs;
84 const unsigned char *data = buf;
86 char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
88 printf("%s at [%p], len=%d\n", title, data, len);
91 /* format 1 line in the buffer, then use printk to print them */
92 out = snprintf(line, LINE_LEN, "%08X", ofs);
93 for (i=0; ofs+i < len && i<16; i++)
94 out += snprintf(line+out, LINE_LEN - out, " %02X",
97 out += snprintf(line+out, LINE_LEN - out, " ");
98 for (i=0; ofs < len && i<16; i++, ofs++) {
99 unsigned char c = data[ofs];
100 if (!isascii(c) || !isprint(c))
102 out += snprintf(line+out, LINE_LEN - out, "%c", c);
104 printf("%s\n", line);
108 static int parse_xmit_status(struct xbee_ctx *ctx,
109 struct xbee_xmit_status_hdr *frame, unsigned len)
112 printf("no context\n");
116 /* see if it matches a xmit query (atcmd_query must be NULL) */
117 if (ctx->atcmd_query[0] != '\0') {
118 printf("invalid response 2\n");
122 /* XXX use defines for these values */
123 if (frame->delivery_status == 0x00)
125 else if (frame->delivery_status == 0x01)
126 printf("MAC ACK Failure\n");
127 else if (frame->delivery_status == 0x15)
128 printf("Invalid destination endpoint\n");
129 else if (frame->delivery_status == 0x21)
130 printf("Network ACK Failure\n");
131 else if (frame->delivery_status == 0x25)
132 printf("Route Not Found\n");
137 static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
141 struct xbee_atcmd_pgm *cmd_pgm;
142 struct xbee_atcmd cmd;
148 } __attribute__((packed)) *result;
151 printf("no context\n");
155 /* get AT command from frame */
156 memcpy(atcmd_str, &frame->cmd, 2);
159 /* see if it matches query */
160 if (memcmp(atcmd_str, ctx->atcmd_query, 2)) {
161 printf("invalid response <%c%c><%s><%s>\n",
163 (frame->cmd >> 8) & 0xFF,
164 atcmd_str, ctx->atcmd_query);
168 /* see if it exists */
169 cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
170 if (cmd_pgm == NULL) {
171 printf("unknown response\n");
174 memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
177 if (frame->status == 1) {
178 printf("Status is error\n");
181 else if (frame->status == 2) {
182 printf("Invalid command\n");
185 else if (frame->status == 3) {
186 printf("Invalid parameter\n");
189 else if (frame->status != 0) {
190 printf("Unknown status error %d\n", frame->status);
195 if (ctx->func != NULL)
196 ctx->func(frame, len, ctx->arg);
199 result = (void *)frame->data;
200 len -= offsetof(struct xbee_atresp_hdr, data);
201 if (cmd.flags & XBEE_ATCMD_F_PARAM_U8 && len == sizeof(uint8_t))
202 printf("<%s> is 0x%x\n", atcmd_str, result->u8);
203 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U16 && len == sizeof(uint16_t))
204 printf("<%s> is 0x%x\n", atcmd_str, ntohs(result->u16));
205 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U32 && len == sizeof(uint32_t))
206 printf("<%s> is 0x%"PRIx32"\n", atcmd_str, ntohl(result->u32));
207 else if (cmd.flags & XBEE_ATCMD_F_PARAM_S16 && len == sizeof(int16_t))
208 printf("<%s> is %d\n", atcmd_str, ntohs(result->s16));
210 printf("no data, status ok\n");
212 hexdump("atcmd answer", frame->data, len);
219 int xbee_recv_data(struct xbee_recv_hdr *recvframe, unsigned len)
221 int datalen = len - sizeof(*recvframe);
222 struct rc_proto_hdr *rch = (struct rc_proto_hdr *) &recvframe->data;
224 if (datalen < sizeof(struct rc_proto_hdr))
228 case RC_PROTO_TYPE_CHANNEL:
229 if (datalen != sizeof(struct rc_proto_channel))
232 case RC_PROTO_TYPE_RANGE: {
233 struct rc_proto_range *rcr =
234 (struct rc_proto_range *) recvframe->data;
236 if (datalen != sizeof(struct rc_proto_range))
239 if (rcr->power_level >= MAX_POWER_LEVEL)
242 rc_proto_rx_range(rcr->power_level);
253 /* socat /dev/ttyUSB0,raw,echo=0,b115200 /dev/ttyACM1,raw,echo=0,b115200 */
254 void xbee_rx(struct xbee_dev *dev, int channel, int type,
255 void *frame, unsigned len, void *opaque)
257 struct xbee_ctx *ctx = opaque;
258 int do_hexdump = xbee_hexdump;
261 printf("type=0x%x, channel=%d, ctx=%p\n", type, channel, ctx);
263 /* if ctx is !NULL, it is an answer to a query */
265 /* XXX only delete timeout if answer matched */
266 xbee_unload_timeout(ctx);
267 if (xbee_debug && ctx->atcmd_query)
268 printf("Received answer to query <%c%c>\n",
269 ctx->atcmd_query[0], ctx->atcmd_query[1]);
272 /* some additional checks before sending */
274 case XBEE_TYPE_MODEM_STATUS: {
275 printf("Received Modem Status frame\n");
279 case XBEE_TYPE_RMT_ATRESP: {
283 #if BYTE_ORDER == LITTLE_ENDIAN
292 memcpy(&addr, frame, sizeof(addr));
293 addr.u64 = ntohll(addr.u64);
294 printf("from remote address %"PRIx32"%"PRIx32"\n",
295 addr.u32.high, addr.u32.low);
297 /* this answer contains an atcmd answer at offset 10 */
298 if (dump_atcmd(ctx, frame + 10, len - 10) < 0)
302 case XBEE_TYPE_ATRESP: {
303 if (dump_atcmd(ctx, frame, len) < 0)
308 case XBEE_TYPE_XMIT_STATUS: {
309 if (parse_xmit_status(ctx, frame, len) < 0)
314 case XBEE_TYPE_RECV: {
315 if (xbee_recv_data(frame, len) < 0)
320 case XBEE_TYPE_ATCMD:
321 case XBEE_TYPE_ATCMD_Q:
323 case XBEE_TYPE_EXPL_XMIT:
324 case XBEE_TYPE_RMT_ATCMD:
325 case XBEE_TYPE_EXPL_RECV:
326 case XBEE_TYPE_NODE_ID:
328 printf("Invalid frame\n");
334 hexdump("undecoded rx frame", frame, len);
336 /* restart command line if it was a blocking query */
338 xbee_unregister_channel(dev, channel);
339 if (ctx->foreground) {
341 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
346 static int xbeeapp_send(struct xbee_ctx *ctx, int type, void *buf, unsigned len,
352 if (len > XBEE_MAX_FRAME_LEN) {
353 printf("frame too large\n");
357 /* register a channel */
358 channel = xbee_register_channel(xbee_dev, XBEE_CHANNEL_ANY,
361 printf("cannot send: no free channel\n");
365 /* copy context in the static struct table (avoiding a malloc) */
366 memcpy(&xbee_ctx[channel], ctx, sizeof(*ctx));
367 ctx = &xbee_ctx[channel];
368 xbee_set_opaque(xbee_dev, channel, ctx);
371 printf("send frame channel=%d type=0x%x len=%d\n",
374 hexdump("xmit frame", buf, len);
376 /* transmit the frame on this channel */
377 ret = xbee_proto_xmit(xbee_dev, channel, type, buf,
380 printf("cannot send\n");
381 xbee_unregister_channel(xbee_dev, channel);
385 ctx->channel = channel;
386 xbee_load_timeout(ctx); /* load a timeout event */
388 /* suspend command line until we have answer or timeout */
391 rdline_stop(&xbeeboard.rdl); /* don't display prompt when return */
392 xbee_stdin_disable(); /* unload file descriptor polling */
398 /* send an AT command with parameters filled by caller. Disable
399 * command line until we get the answer or until a timeout occurs */
400 int xbeeapp_send_atcmd(const char *atcmd_str,
401 void *param, unsigned param_len, int foreground,
402 int (*func)(void *frame, unsigned len, void *arg), void *arg)
406 struct xbee_atcmd_hdr atcmd;
407 char buf[XBEE_MAX_FRAME_LEN];
408 } __attribute__((packed)) frame;
410 memset(&ctx, 0, sizeof(ctx));
411 ctx.atcmd_query[0] = atcmd_str[0];
412 ctx.atcmd_query[1] = atcmd_str[1];
416 memcpy(&frame.atcmd.cmd, atcmd_str, 2);
417 memcpy(&frame.buf, param, param_len);
419 if (xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &frame,
420 sizeof(struct xbee_atcmd_hdr) +
421 param_len, foreground) < 0) {
428 int xbeeapp_send_msg(uint64_t addr, void *data,
429 unsigned data_len, int foreground)
433 struct xbee_xmit_hdr xmit;
434 char buf[XBEE_MAX_FRAME_LEN];
435 } __attribute__((packed)) frame;
437 memset(&ctx, 0, sizeof(ctx));
438 ctx.atcmd_query[0] = '\0';
440 frame.xmit.dstaddr = htonll(addr);
441 frame.xmit.reserved = htons(0xFFFE);
442 frame.xmit.bcast_radius = 0;
444 memcpy(&frame.buf, data, data_len);
446 if (xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &frame,
447 sizeof(struct xbee_xmit_hdr) +
448 data_len, foreground) < 0) {
455 void xbee_stdin_enable(void)
457 xbee_cmdline_input_enabled = 1;
460 void xbee_stdin_disable(void)
462 xbee_cmdline_input_enabled = 0;
465 static void evt_timeout(struct callout_manager *cm, struct callout *clt,
468 struct xbee_ctx *ctx = arg;
472 /* restart command line */
474 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
477 xbee_unregister_channel(xbee_dev, ctx->channel);
480 void xbee_load_timeout(struct xbee_ctx *ctx)
482 callout_reset(&cm, &ctx->timeout, TIMEOUT_MS, SINGLE, evt_timeout, ctx);
485 void xbee_unload_timeout(struct xbee_ctx *ctx)
487 callout_stop(&cm, &ctx->timeout);
490 void bootloader(void)
492 #define BOOTLOADER_ADDR 0x1e000
493 if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
494 printf_P(PSTR("Bootloader is not present\r\n"));
498 /* ... very specific :( */
505 __asm__ __volatile__ ("ldi r31,0xf0\n");
506 __asm__ __volatile__ ("ldi r30,0x00\n");
507 __asm__ __volatile__ ("ijmp\n");
512 void xbee_mainloop(void)
520 /* from xbee to cmdline */
521 c = CDC_Device_ReceiveByte(&VirtualSerial2_CDC_Interface);
523 CDC_Device_SendByte(&VirtualSerial1_CDC_Interface,
526 /* from cmdline to xbee */
527 c = CDC_Device_ReceiveByte(&VirtualSerial1_CDC_Interface);
528 if (c == 4) { /* CTRL-d */
530 rdline_newline(&xbeeboard.rdl,
535 CDC_Device_SendByte(&VirtualSerial2_CDC_Interface,
537 /* echo on cmdline */
538 CDC_Device_SendByte(&VirtualSerial1_CDC_Interface,
543 if (xbee_cmdline_input_enabled)
545 xbee_proto_rx(xbee_dev);
548 CDC_Device_USBTask(&VirtualSerial1_CDC_Interface);
549 CDC_Device_USBTask(&VirtualSerial2_CDC_Interface);