Fix printing of service versions
[project/uqmi.git] / commands.c
index 43b9ca9..8c06503 100644 (file)
@@ -5,11 +5,13 @@
 #include <unistd.h>
 
 #include <libubox/blobmsg.h>
+#include <libubox/blobmsg_json.h>
 
 #include "uqmi.h"
 #include "commands.h"
 
 static struct blob_buf status;
+bool single_line = false;
 
 static void no_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
 {
@@ -18,16 +20,20 @@ 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;
+       void *c;
        char name_buf[16];
        int i;
 
        qmi_parse_ctl_get_version_info_response(msg, &res);
+
+       c = blobmsg_open_table(&status, NULL);
        for (i = 0; i < res.data.service_list_n; i++) {
                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);
        }
+       blobmsg_close_table(&status, c);
 }
 
 static enum qmi_cmd_result
@@ -125,22 +131,17 @@ void uqmi_add_command(char *arg, int cmd)
 
 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;
-               }
-       }
+       char *str;
+
+       if (!blob_len(data))
+               return;
+
+       str = blobmsg_format_json_indent(blob_data(data), false, single_line ? -1 : 0);
+       if (!str)
+               return;
+
+       printf("%s\n", str);
+       free(str);
 }
 
 static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
@@ -160,7 +161,7 @@ static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
                blob_buf_init(&status, 0);
                if (cmds[i].handler->type > QMI_SERVICE_CTL &&
                    qmi_service_connect(qmi, cmds[i].handler->type, -1)) {
-                       blobmsg_printf(&status, "error", "failed to connect to service");
+                       uqmi_add_error("Failed to connect to service");
                        res = QMI_CMD_EXIT;
                } else {
                        res = cmds[i].handler->prepare(qmi, &req, (void *) buf, cmds[i].arg);
@@ -169,8 +170,10 @@ static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
                if (res == QMI_CMD_REQUEST) {
                        qmi_request_start(qmi, &req, (void *) buf, cmds[i].handler->cb);
                        req.no_error_cb = true;
-                       if (qmi_request_wait(qmi, &req))
-                               blobmsg_add_string(&status, "error", qmi_get_error_str(req.ret));
+                       if (qmi_request_wait(qmi, &req)) {
+                               uqmi_add_error(qmi_get_error_str(req.ret));
+                               do_break = true;
+                       }
                } else if (res == QMI_CMD_EXIT) {
                        do_break = true;
                }
@@ -182,11 +185,22 @@ static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
        return true;
 }
 
-void uqmi_run_commands(struct qmi_dev *qmi)
+int uqmi_add_error(const char *msg)
 {
-       if (__uqmi_run_commands(qmi, true))
-               __uqmi_run_commands(qmi, false);
+       blobmsg_add_string(&status, NULL, msg);
+       return QMI_CMD_EXIT;
+}
+
+bool uqmi_run_commands(struct qmi_dev *qmi)
+{
+       bool ret;
+
+       ret = __uqmi_run_commands(qmi, true) &&
+             __uqmi_run_commands(qmi, false);
+
        free(cmds);
        cmds = NULL;
        n_cmds = 0;
+
+       return ret;
 }