#include <rdline.h>
#include <xbee.h>
+#include <xbee_rxtx.h>
#include "rc_proto.h"
#include "xbee_user.h"
int xbee_hexdump = 0;
int xbee_debug = 0;
-static void hexdump(const char *title, const void *buf, unsigned int len)
+static void __hexdump(const void *buf, unsigned int len)
{
unsigned int i, out, ofs;
const unsigned char *data = buf;
#define LINE_LEN 80
char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
- printf_P(PSTR("%s at [%p], len=%d\r\n"), title, data, len);
ofs = 0;
while (ofs < len) {
/* format 1 line in the buffer, then use printk to print them */
}
}
+static void hexdump_msg(const char *title, const struct xbee_msg *msg)
+{
+ unsigned i;
+
+ printf_P(PSTR("dump %s\r\n"), title);
+ for (i = 0; i < msg->iovlen; i++) {
+ printf_P(PSTR("iovec %d at %p, len=%d\r\n"), i,
+ msg->iov[i].buf, msg->iov[i].len);
+ __hexdump(msg->iov[i].buf, msg->iov[i].len);
+ }
+}
+
+static void hexdump(const char *title, const void *buf, unsigned int len)
+{
+ printf_P(PSTR("dump %s at [%p], len=%d\r\n"), title, buf, len);
+ __hexdump(buf, len);
+}
+
static int parse_xmit_status(struct xbee_ctx *ctx,
- struct xbee_xmit_status_hdr *frame, unsigned len)
+ struct xbee_xmit_status_hdr *frame, unsigned len)
{
(void)len;
}
static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
- unsigned len)
+ unsigned len)
{
char atcmd_str[3];
const struct xbee_atcmd *cmd_pgm;
return ret;
}
-static int xbeeapp_send(struct xbee_ctx *ctx, int type, void *buf, unsigned len,
- int foreground)
+static int xbeeapp_send(struct xbee_ctx *ctx, int type, struct xbee_msg *msg,
+ int foreground)
{
int ret;
int channel;
- if (len > XBEE_MAX_FRAME_LEN) {
- printf_P(PSTR("frame too large\r\n"));
- return -1;
- }
-
/* register a channel */
channel = xbee_register_channel(xbee_dev, XBEE_CHANNEL_ANY,
xbeeapp_rx, NULL);
xbee_set_opaque(xbee_dev, channel, ctx);
if (xbee_debug)
- printf_P(PSTR("send frame channel=%d type=0x%x len=%d\r\n"),
- channel, type, len);
+ printf_P(PSTR("send frame channel=%d type=0x%x\r\n"),
+ channel, type);
if (xbee_hexdump)
- hexdump("xmit frame", buf, len);
+ hexdump_msg("xmit frame", msg);
/* transmit the frame on this channel */
- ret = xbee_tx(xbee_dev, channel, type, buf, len);
+ ret = xbee_tx_iovec(xbee_dev, channel, type, msg);
if (ret < 0) {
printf_P(PSTR("cannot send\r\n"));
xbee_unregister_channel(xbee_dev, channel);
/* send an AT command with parameters filled by caller. Disable
* command line until we get the answer or until a timeout occurs */
-int xbeeapp_send_atcmd(const char *atcmd_str,
- void *param, unsigned param_len, int foreground,
- int (*func)(void *frame, unsigned len, void *arg), void *arg)
+int xbeeapp_send_atcmd(char *atcmd_str, void *param,
+ unsigned param_len, int foreground,
+ int (*func)(void *frame, unsigned len, void *arg), void *arg)
{
struct xbee_ctx ctx;
- struct {
- struct xbee_atcmd_hdr atcmd;
- char buf[XBEE_MAX_FRAME_LEN];
- } __attribute__((packed)) frame;
+ /* struct xbee_atcmd_hdr atcmd_hdr; -> no needed same than atcmd_str */
+ struct xbee_msg msg;
memset(&ctx, 0, sizeof(ctx));
ctx.atcmd_query[0] = atcmd_str[0];
ctx.func = func;
ctx.arg = arg;
- memcpy(&frame.atcmd.cmd, atcmd_str, 2);
- memcpy(&frame.buf, param, param_len);
+ msg.iovlen = 2;
+ msg.iov[0].buf = atcmd_str;
+ msg.iov[0].len = 2;
+ msg.iov[1].buf = param;
+ msg.iov[1].len = param_len;
- if (xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &frame,
- sizeof(struct xbee_atcmd_hdr) +
- param_len, foreground) < 0) {
+ if (xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &msg, foreground) < 0)
return -1;
- }
return 0;
}
-int xbeeapp_send_msg(uint64_t addr, void *data,
- unsigned data_len, int foreground)
+int xbeeapp_send_msg(uint64_t addr, struct xbee_msg *msg, int foreground)
{
struct xbee_ctx ctx;
- struct {
- struct xbee_xmit_hdr xmit;
- char buf[XBEE_MAX_FRAME_LEN];
- } __attribute__((packed)) frame;
+ struct xbee_xmit_hdr xmit_hdr;
+ struct xbee_msg msg2;
+ unsigned i;
+
+ if (msg->iovlen + 2 > XBEE_MSG_MAXIOV) {
+ printf_P(PSTR("too many iovecs\r\n"));
+ return -1;
+ }
+
+ xmit_hdr.dstaddr = htonll(addr);
+ xmit_hdr.reserved = htons(0xFFFE);
+ xmit_hdr.bcast_radius = 0;
+ xmit_hdr.opts = 0;
+
+ msg2.iovlen = msg->iovlen + 1;
+ msg2.iov[0].buf = &xmit_hdr;
+ msg2.iov[0].len = sizeof(xmit_hdr);
+ for (i = 0; i < msg->iovlen; i++)
+ msg2.iov[i+1] = msg->iov[i];
memset(&ctx, 0, sizeof(ctx));
ctx.atcmd_query[0] = '\0';
- frame.xmit.dstaddr = htonll(addr);
- frame.xmit.reserved = htons(0xFFFE);
- frame.xmit.bcast_radius = 0;
- frame.xmit.opts = 0;
- memcpy(&frame.buf, data, data_len);
-
- if (xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &frame,
- sizeof(struct xbee_xmit_hdr) +
- data_len, foreground) < 0) {
+ if (xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &msg2, foreground) < 0)
return -1;
- }
return 0;
}
#define _XBEE_USER_H_
#include <callout.h>
+#include <xbee_rxtx.h>
/* used for timeouts and xbee rx callback */
struct xbee_ctx {
extern int xbee_hexdump;
extern int xbee_debug;
+/* we use a specific structure to send packets. It allows to prepend some
+ * data in the frame without doing a copy. */
+struct xbeeapp_pkt {
+ char *buf;
+ unsigned len;
+ unsigned headroom;
+ unsigned tailroom;
+};
+
+/* callback registered to xbee module, called when a xbee frame is received */
int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type,
- void *frame, unsigned len, void *opaque);
-int xbeeapp_send_atcmd(const char *atcmd_str,
- void *param, unsigned param_len, int foreground,
- int (*func)(void *frame, unsigned len, void *arg),
- void *arg);
-int xbeeapp_send_msg(uint64_t addr, void *data,
- unsigned data_len, int foreground);
+ void *frame, unsigned len, void *opaque);
+
+/* Send an AT command to the xbee device. The callback function for the answer
+ * is given as a parameter */
+int xbeeapp_send_atcmd(char *atcmd_str, void *param,
+ unsigned param_len, int foreground,
+ int (*func)(void *frame, unsigned len, void *arg),
+ void *arg);
+
+/* send a message to a peer */
+int xbeeapp_send_msg(uint64_t addr, struct xbee_msg *msg, int foreground);
void xbee_stdin_enable(void);
void xbee_stdin_disable(void);