make uqmi_add_error return QMI_CMD_EXIT
[project/uqmi.git] / commands.c
index c0c0584..4680511 100644 (file)
@@ -3,9 +3,16 @@
 #include <strings.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <strings.h>
 #include <stdlib.h>
 #include <unistd.h>
+
+#include <libubox/blobmsg.h>
+#include <libubox/blobmsg_json.h>
+
 #include "uqmi.h"
 #include "commands.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)
 {
 }
 static void no_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
 {
 }
@@ -13,19 +20,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;
 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;
 
        int i;
 
-       if (!msg) {
-               printf("Request version failed: %d\n", req->ret);
-               return;
-       }
-
        qmi_parse_ctl_get_version_info_response(msg, &res);
        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++) {
        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);
        }
                        res.data.service_list[i].major_version,
                        res.data.service_list[i].minor_version);
        }
@@ -97,6 +98,7 @@ cmd_set_client_id_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct q
 #include "commands-wds.c"
 #include "commands-dms.c"
 #include "commands-nas.c"
 #include "commands-wds.c"
 #include "commands-dms.c"
 #include "commands-nas.c"
+#include "commands-wms.c"
 
 #define __uqmi_command(_name, _optname, _arg, _type) \
        [__UQMI_COMMAND_##_name] = { \
 
 #define __uqmi_command(_name, _optname, _arg, _type) \
        [__UQMI_COMMAND_##_name] = { \
@@ -123,6 +125,21 @@ void uqmi_add_command(char *arg, int cmd)
        cmds[idx].arg = optarg;
 }
 
        cmds[idx].arg = optarg;
 }
 
+static void uqmi_print_result(struct blob_attr *data)
+{
+       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)
 {
        static char buf[2048];
 static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)
 {
        static char buf[2048];
@@ -132,37 +149,54 @@ 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;
        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;
 
 
                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)) {
                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;
+                       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);
                }
                }
-               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_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)) {
+                               uqmi_add_error(qmi_get_error_str(req.ret));
+                               do_break = true;
+                       }
+               } else if (res == QMI_CMD_EXIT) {
+                       do_break = true;
                }
                }
+
+               uqmi_print_result(status.head);
+               if (do_break)
+                       return false;
        }
        return true;
 }
 
        }
        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;
        free(cmds);
        cmds = NULL;
        n_cmds = 0;
+
+       return ret;
 }
 }