rework status handling, use a blob_buf as storage
authorFelix Fietkau <nbd@openwrt.org>
Sun, 17 Feb 2013 18:16:55 +0000 (19:16 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 17 Feb 2013 18:16:55 +0000 (19:16 +0100)
commands-dms.c
commands-nas.c
commands-wds.c
commands.c

index d9f23c1713bc41b0baea25ba28916a576f89691d..483dea0250ab264b6b530273a630198ea68e56ae 100644 (file)
@@ -26,20 +26,14 @@ static void cmd_dms_get_pin_status_cb(struct qmi_dev *qmi, struct qmi_request *r
 
        qmi_parse_dms_uim_get_pin_status_response(msg, &res);
        if (res.set.pin1_status) {
-               printf("pin1_status=%s\n"
-                          "pin1_verify_tries=%d\n"
-                          "pin1_unblock_tries=%d\n",
-                          get_pin_status(res.data.pin1_status.current_status),
-                          res.data.pin1_status.verify_retries_left,
-                          res.data.pin1_status.unblock_retries_left);
+               blobmsg_add_string(&status, "pin1_status", get_pin_status(res.data.pin1_status.current_status));
+               blobmsg_add_u32(&status, "pin1_verify_tries", (int32_t) res.data.pin1_status.verify_retries_left);
+               blobmsg_add_u32(&status, "pin1_unblock_tries", (int32_t) res.data.pin1_status.unblock_retries_left);
        }
        if (res.set.pin2_status) {
-               printf("pin2_status=%s\n"
-                          "pin2_verify_tries=%d\n"
-                          "pin2_unblock_tries=%d\n",
-                          get_pin_status(res.data.pin2_status.current_status),
-                          res.data.pin2_status.verify_retries_left,
-                          res.data.pin2_status.unblock_retries_left);
+               blobmsg_add_string(&status, "pin2_status", get_pin_status(res.data.pin2_status.current_status));
+               blobmsg_add_u32(&status, "pin2_verify_tries", (int32_t) res.data.pin2_status.verify_retries_left);
+               blobmsg_add_u32(&status, "pin2_unblock_tries", (int32_t) res.data.pin2_status.unblock_retries_left);
        }
 }
 
@@ -50,16 +44,7 @@ cmd_dms_get_pin_status_prepare(struct qmi_dev *qmi, struct qmi_request *req, str
        return QMI_CMD_REQUEST;
 }
 
-static void cmd_dms_verify_pin1_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
-{
-       if (!msg) {
-               printf("failed\n");
-               return;
-       }
-
-       printf("ok\n");
-}
-
+#define cmd_dms_verify_pin1_cb no_cb
 static enum qmi_cmd_result
 cmd_dms_verify_pin1_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
 {
@@ -73,7 +58,7 @@ cmd_dms_verify_pin1_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct
        return QMI_CMD_REQUEST;
 }
 
-#define cmd_dms_verify_pin2_cb cmd_dms_verify_pin1_cb
+#define cmd_dms_verify_pin2_cb no_cb
 static enum qmi_cmd_result
 cmd_dms_verify_pin2_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
 {
index eacf99475f7d1e83ac0e41bf2e0ad6a7870fa60d..09624c4846f7d60ad9664c401a525aaa061d6066 100644 (file)
@@ -35,7 +35,7 @@ cmd_nas_set_network_modes_prepare(struct qmi_dev *qmi, struct qmi_request *req,
                }
 
                if (!found) {
-                       fprintf(stderr, "Invalid network mode '%s'\n", word);
+                       blobmsg_add_string(&status, "error", "Invalid network mode");
                        return QMI_CMD_EXIT;
                }
        }
@@ -61,32 +61,31 @@ cmd_nas_get_signal_info_cb(struct qmi_dev *qmi, struct qmi_request *req, struct
 
        qmi_parse_nas_get_signal_info_response(msg, &res);
 
-       if (res.set.cdma_signal_strength)
-               printf("cdma_rssi=%d\ncdma_ecio=%d\n",
-                       res.data.cdma_signal_strength.rssi,
-                       res.data.cdma_signal_strength.ecio);
+       if (res.set.cdma_signal_strength) {
+               blobmsg_add_u32(&status, "rssi", (int32_t) res.data.cdma_signal_strength.rssi);
+               blobmsg_add_u32(&status, "ecio", (int32_t) res.data.cdma_signal_strength.ecio);
+       }
 
-       if (res.set.hdr_signal_strength)
-               printf("hdr_rssi=%d\nhdr_ecio=%d\nhdr_io=%d\n",
-                       res.data.hdr_signal_strength.rssi,
-                       res.data.hdr_signal_strength.ecio,
-                       res.data.hdr_signal_strength.io);
+       if (res.set.hdr_signal_strength) {
+               blobmsg_add_u32(&status, "rssi", (int32_t) res.data.hdr_signal_strength.rssi);
+               blobmsg_add_u32(&status, "ecio", (int32_t) res.data.hdr_signal_strength.ecio);
+               blobmsg_add_u32(&status, "io", res.data.hdr_signal_strength.io);
+       }
 
        if (res.set.gsm_signal_strength)
-               printf("gsm_rssi=%d\n", res.data.gsm_signal_strength);
-
-       if (res.set.wcdma_signal_strength)
-               printf("wcdma_rssi=%d\nwcdma_ecio=%d\n",
-                       res.data.wcdma_signal_strength.rssi,
-                       res.data.wcdma_signal_strength.ecio);
+               blobmsg_add_u32(&status, "signal", (int32_t) res.data.gsm_signal_strength);
 
-       if (res.set.lte_signal_strength)
-               printf("lte_rssi=%d\nlte_rsrq=%d\nlte_rsrp=%d\nlte_snr=%d\n",
-                       res.data.lte_signal_strength.rssi,
-                       res.data.lte_signal_strength.rsrq,
-                       res.data.lte_signal_strength.rsrp,
-                       res.data.lte_signal_strength.snr);
+       if (res.set.wcdma_signal_strength) {
+               blobmsg_add_u32(&status, "rssi", (int32_t) res.data.wcdma_signal_strength.rssi);
+               blobmsg_add_u32(&status, "ecio", (int32_t) res.data.wcdma_signal_strength.ecio);
+       }
 
+       if (res.set.lte_signal_strength) {
+               blobmsg_add_u32(&status, "rssi", (int32_t) res.data.lte_signal_strength.rssi);
+               blobmsg_add_u32(&status, "rsrq", (int32_t) res.data.lte_signal_strength.rsrq);
+               blobmsg_add_u32(&status, "rsrp", (int32_t) res.data.lte_signal_strength.rsrp);
+               blobmsg_add_u32(&status, "snr", (int32_t) res.data.lte_signal_strength.snr);
+       }
 }
 
 static enum qmi_cmd_result
index a5c4dcbfd37abd8bbd9aebb2700cdae59947f55f..777411488a755e2eab35450a094f423aa6ee69b0 100644 (file)
@@ -26,7 +26,7 @@ cmd_wds_set_auth_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qm
                return QMI_CMD_DONE;
        }
 
-       fprintf(stderr, "Invalid auth mode (valid: pap, chap, both, none)\n");
+       blobmsg_add_string(&status, "error", "Invalid auth mode (valid: pap, chap, both, none)");
        return QMI_CMD_EXIT;
 }
 
index c0c058451a57869560206da9bdd9fab927b89b6b..044760e7f7dd7530b00814ef4f9925d1ee01cc59 100644 (file)
@@ -3,9 +3,14 @@
 #include <strings.h>
 #include <stdlib.h>
 #include <unistd.h>
+
+#include <libubox/blobmsg.h>
+
 #include "uqmi.h"
 #include "commands.h"
 
+static struct blob_buf status;
+
 static void no_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
 {
 }
@@ -13,19 +18,13 @@ static void no_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *
 static void cmd_version_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
 {
        struct qmi_ctl_get_version_info_response res;
+       char name_buf[16];
        int i;
 
-       if (!msg) {
-               printf("Request version failed: %d\n", req->ret);
-               return;
-       }
-
        qmi_parse_ctl_get_version_info_response(msg, &res);
-
-       printf("Found %d: services:\n", res.data.service_list_n);
        for (i = 0; i < res.data.service_list_n; i++) {
-               printf("Service %d, version: %d.%d\n",
-                       res.data.service_list[i].service,
+               sprintf(name_buf, "service_%d", res.data.service_list[i].service);
+               blobmsg_printf(&status, name_buf, "%d,%d",
                        res.data.service_list[i].major_version,
                        res.data.service_list[i].minor_version);
        }
@@ -123,6 +122,26 @@ void uqmi_add_command(char *arg, int cmd)
        cmds[idx].arg = optarg;
 }
 
+static void uqmi_print_result(struct blob_attr *data)
+{
+       struct blob_attr *cur;
+       int rem;
+
+       blob_for_each_attr(cur, data, rem) {
+               switch (blobmsg_type(cur)) {
+               case BLOBMSG_TYPE_STRING:
+                       printf("%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
+                       break;
+               case BLOBMSG_TYPE_INT32:
+                       printf("%s=%d\n", blobmsg_name(cur), (int32_t) blobmsg_get_u32(cur));
+                       break;
+               case BLOBMSG_TYPE_INT8:
+                       printf("%s=%s\n", blobmsg_name(cur), blobmsg_get_u8(cur) ? "true" : "false");
+                       break;
+               }
+       }
+}
+
 static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
 {
        static char buf[2048];
@@ -132,28 +151,32 @@ static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
        for (i = 0; i < n_cmds; i++) {
                enum qmi_cmd_result res;
                bool cmd_option = cmds[i].handler->type == CMD_TYPE_OPTION;
+               bool do_break = false;
 
                if (cmd_option != option)
                        continue;
 
+               blob_buf_init(&status, 0);
                if (cmds[i].handler->type > QMI_SERVICE_CTL &&
                    qmi_service_connect(qmi, cmds[i].handler->type, -1)) {
-                       fprintf(stderr, "Error in command '%s': failed to connect to service\n",
-                               cmds[i].handler->name);
-                       return false;
+                       blobmsg_printf(&status, "error", "failed to connect to service");
+                       res = QMI_CMD_EXIT;
+               } else {
+                       res = cmds[i].handler->prepare(qmi, &req, (void *) buf, cmds[i].arg);
                }
-               res = cmds[i].handler->prepare(qmi, &req, (void *) buf, cmds[i].arg);
-               switch(res) {
-               case QMI_CMD_REQUEST:
+
+               if (res == QMI_CMD_REQUEST) {
                        qmi_request_start(qmi, &req, (void *) buf, cmds[i].handler->cb);
-                       qmi_request_wait(qmi, &req);
-                       break;
-               case QMI_CMD_EXIT:
-                       return false;
-               case QMI_CMD_DONE:
-               default:
-                       continue;
+                       req.no_error_cb = true;
+                       if (qmi_request_wait(qmi, &req))
+                               blobmsg_add_string(&status, "error", qmi_get_error_str(req.ret));
+               } else if (res == QMI_CMD_EXIT) {
+                       do_break = true;
                }
+
+               uqmi_print_result(status.head);
+               if (do_break)
+                       return false;
        }
        return true;
 }