format output as json
authorFelix Fietkau <nbd@openwrt.org>
Sat, 8 Jun 2013 20:34:17 +0000 (22:34 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Sat, 8 Jun 2013 20:36:09 +0000 (22:36 +0200)
CMakeLists.txt
commands.c

index ce2e14764d6d4b1f429d84659f4b798374896874..a09865fe3a9f1f7b016eaa4ce0a8f8086a6f15b9 100644 (file)
@@ -7,7 +7,8 @@ SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
 
 SET(SOURCES main.c dev.c commands.c qmi-message.c)
 
-SET(LIBS ubox)
+FIND_LIBRARY(json json-c json)
+SET(LIBS ubox blobmsg_json ${json})
 
 IF(DEBUG_PACKET)
   ADD_DEFINITIONS(-DDEBUG_PACKET)
index 43b9ca999c8ae7f4dfa0f5bf459a3f2d07b353a2..680b75dd0b1fb9c5446eb33dd632e814f8ba1a12 100644 (file)
@@ -5,6 +5,7 @@
 #include <unistd.h>
 
 #include <libubox/blobmsg.h>
+#include <libubox/blobmsg_json.h>
 
 #include "uqmi.h"
 #include "commands.h"
@@ -125,22 +126,14 @@ 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;
+
+       str = blobmsg_format_json_indent(data, true, 0);
+       if (!str)
+               return;
+
+       printf("%s\n", str);
+       free(str);
 }
 
 static bool __uqmi_run_commands(struct qmi_dev *qmi, bool option)