xbee_atcmd: add atcmd callback on response
authorFabrice Desclaux <serpilliere@droids-corp.org>
Mon, 20 Feb 2012 20:54:30 +0000 (21:54 +0100)
committerFabrice Desclaux <serpilliere@droids-corp.org>
Mon, 20 Feb 2012 20:54:30 +0000 (21:54 +0100)
commands.c
main.c
main.h

index be8e03c..1489cb5 100644 (file)
@@ -88,7 +88,7 @@ static void monitor_cb(int s, short event, void *arg)
        if (monitor_current == NULL)
                monitor_current = LIST_FIRST(&monitor_list);
 
-       xbeeapp_send_atcmd(monitor_current->atcmd, NULL, 0, 0);
+       xbeeapp_send_atcmd(monitor_current->atcmd, NULL, 0, 0, NULL, NULL);
        monitor_current = LIST_NEXT(monitor_current, next);
 
        evtimer_set(&monitor_event, monitor_cb, cl);
@@ -114,7 +114,7 @@ static void range_cb(int s, short event, void *arg)
        }
        range_power = ((range_power + i) & 0x7);
 
-       xbeeapp_send_atcmd("PL", &range_power, sizeof(range_power), 0);
+       xbeeapp_send_atcmd("PL", &range_power, sizeof(range_power), 0, NULL, NULL);
        snprintf(buf, sizeof(buf), "range%d", range_power);
        xbeeapp_send_msg(range_dstaddr, buf, strlen(buf), 0);
 
@@ -790,7 +790,7 @@ struct cmd_ping_result {
 /* function called when cmd_ping is parsed successfully */
 static void cmd_ping_parsed(void *parsed_result, struct cmdline *cl, void *data)
 {
-       xbeeapp_send_atcmd("VL", NULL, 0, 1);
+       xbeeapp_send_atcmd("VL", NULL, 0, 1, NULL, NULL);
 }
 
 cmdline_parse_token_string_t cmd_ping_ping =
@@ -980,7 +980,7 @@ static void cmd_read_parsed(void *parsed_result, struct cmdline *cl,
                            void *data)
 {
        struct cmd_read_result *res = parsed_result;
-       xbeeapp_send_atcmd(res->cmd->name, NULL, 0, 1);
+       xbeeapp_send_atcmd(res->cmd->name, NULL, 0, 1, NULL, NULL);
 }
 
 cmdline_parse_token_string_t cmd_read_read =
@@ -1044,7 +1044,7 @@ static void cmd_write_parsed(void *parsed_result, struct cmdline *cl,
                printf("Unknown argument type\n");
                return;
        }
-       xbeeapp_send_atcmd(res->cmd->name, param, len, 1);
+       xbeeapp_send_atcmd(res->cmd->name, param, len, 1, NULL, NULL);
 }
 
 cmdline_parse_token_string_t cmd_write_write =
diff --git a/main.c b/main.c
index 94c0f34..4847d11 100644 (file)
--- a/main.c
+++ b/main.c
@@ -243,6 +243,10 @@ static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
        /* dump frame */
        if (atcmd_frame_status(frame, len) == 0) {
 
+               /* callback */
+               if (ctx->func != NULL)
+                       ctx->func(frame, len, ctx->arg);
+
                if (len == sizeof(struct xbee_atresp_hdr))
                        xbeeapp_log(ctx->foreground, "<%s>: ok\n",
                                    ctx->atcmd_query->name);
@@ -330,7 +334,7 @@ void xbee_rx(struct xbee_dev *dev, int channel, int type,
                        if (recvlen >= strlen("range") &&
                            !strncmp((char *)recvframe->data,
                                     "range", strlen("range"))) {
-                               xbeeapp_send_atcmd("DB", NULL, 0, 0);
+                               xbeeapp_send_atcmd("DB", NULL, 0, 0, NULL, NULL);
                                on_stdout = 0;
                        }
                        hexdump(on_stdout, "rx data", recvframe->data,
@@ -415,8 +419,10 @@ static int xbeeapp_send(struct xbee_ctx *ctx, int type, void *buf, unsigned len,
 
 /* 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 xbeeapp_send_atcmd(const 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 xbee_atcmd *cmd;
@@ -441,6 +447,8 @@ int xbeeapp_send_atcmd(const char *atcmd_str, void *param, unsigned param_len,
        memset(ctx, 0, sizeof(*ctx));
        ctx->type = ATCMD;
        ctx->atcmd_query = cmd;
+       ctx->func = func;
+       ctx->arg = arg;
 
        memcpy(&frame.atcmd.cmd, atcmd_str, 2);
        memcpy(&frame.buf, param, param_len);
diff --git a/main.h b/main.h
index ec682f4..693a5c8 100644 (file)
--- a/main.h
+++ b/main.h
@@ -38,6 +38,8 @@ struct xbee_ctx {
        int have_more_command;
        int foreground;
        int channel;
+       int (*func)(void *frame, unsigned len, void *arg);
+       void *arg;
        struct event timeout;
        struct xbee_atcmd *atcmd_query;
 };
@@ -57,8 +59,10 @@ void xbeeapp_log(int always_on_stdout, const char *fmt, ...);
 
 void xbee_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 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);
 int xbeeapp_dump_config(const char *filename);