Skip to contentSkip to conversation

Add JSON output options to 'iw' for scan results

PATCH Superseded Isaev Ruslan 1 participant 1 message Mar 5, 2024 Kbuild

v5 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 Add JSON output options to 'iw' for scan results

v5 changes:
- add json print to print_ap_channel_report()
- minor refactor open_json_object()

branch: v5.19

This commit introduces the ability to output scan results from 'iw' in JSON format, similar to the 'ip' utility from the iproute2 package. The addition aims to enhance the tool's usability and scriptability by providing a structured data format option.

Two new command-line options are added:
- '-j': Outputs scan results in compact JSON format.
- '-jj': Outputs scan results in pretty-printed JSON format.

The implementation focuses on modifying the printing functions to format the output as JSON without altering the existing netlink response parsing logic. This patch demonstrates the concept with scan results, showcasing the potential for expanding JSON support to other 'iw' functionalities.

If these changes prove valuable, I am prepared to continue working on extending JSON output support throughout the tool. Feedback is welcome and appreciated.

Thank you.

---
 Makefile           |    2 +-
 iw.c               |   27 +-
 iw.h               |    1 +
 json/json_print.c  |  206 +++++
 json/json_print.h  |   57 ++
 json/json_writer.c |  296 +++++++
 json/json_writer.h |   68 ++
 json/main.c        |   28 +
 scan.c             | 2117 ++++++++++++++++++++++++++++----------------
 util.c             |  492 +++++++---
 10 files changed, 2407 insertions(+), 887 deletions(-)
 create mode 100644 json/json_print.c
 create mode 100644 json/json_print.h
 create mode 100644 json/json_writer.c
 create mode 100644 json/json_writer.h
 create mode 100644 json/main.c
Makefile +1 -1
index 2fb8db8..6cda648 100644
@@ -19,7 +19,7 @@ CFLAGS += -Werror-implicit-function-declaration -Wsign-compare -Wno-unused-param
 CFLAGS += -Wdeclaration-after-statement
 CFLAGS += $(CFLAGS_EVAL)
 
-_OBJS := $(sort $(patsubst %.c,%.o,$(wildcard *.c)))
+_OBJS := $(sort $(patsubst %.c,%.o,$(wildcard *.c) json/json_writer.c json/json_print.c))
 VERSION_OBJS := $(filter-out version.o, $(_OBJS))
 OBJS := $(VERSION_OBJS) version.o
 
iw.c +26 -1
index 35308ba..779a728 100644
@@ -24,6 +24,8 @@
 #include "nl80211.h"
 #include "iw.h"
 
+#include "json/json_print.h"
+
 /* libnl 1.x compatibility code */
 #if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30)
 static inline struct nl_handle *nl_socket_alloc(void)
@@ -44,6 +46,7 @@ static inline int nl_socket_set_buffer_size(struct nl_sock *sk,
 #endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */
 
 int iw_debug = 0;
+int iw_json = 0;
 
 static int nl80211_init(struct nl80211_state *state)
 {
@@ -181,7 +184,11 @@ static void __usage_cmd(const struct cmd *cmd, char *indent, bool full)
 static void usage_options(void)
 {
 	printf("Options:\n");
-	printf("\t--debug\t\tenable netlink debugging\n");
+	printf(""
+	"\t--debug\t\tenable netlink debugging\n"
+	"\t-j     \t\tjson output\n"
+	"\t-jj    \t\tjson pretty output\n"
+	"");
 }
 
 static const char *argv0;
@@ -592,6 +599,20 @@ int main(int argc, char **argv)
 		argv++;
 	}
 
+	if (argc > 0 && strcmp(*argv, "-j") == 0) {
+		iw_json = 1;
+		new_json_obj(1, 0); //pretty print disabled
+		argc--;
+		argv++;
+	}
+
+	if (argc > 0 && strcmp(*argv, "-jj") == 0) {
+		iw_json = 1;
+		new_json_obj(1, 1); //pretty print enabled
+		argc--;
+		argv++;
+	}
+
 	if (argc > 0 && strcmp(*argv, "--version") == 0) {
 		version();
 		return 0;
@@ -648,5 +669,9 @@ int main(int argc, char **argv)
 
 	nl80211_cleanup(&nlstate);
 
+	//close root json object and deinit jsonw
+	if(iw_json){
+		delete_json_obj();
+	}
 	return err;
 }
iw.h +1 -0
index e712c59..b97eb55 100644
@@ -179,6 +179,7 @@ struct vendor_event {
 extern const char iw_version[];
 
 extern int iw_debug;
+extern int iw_json;
 
 int handle_cmd(struct nl80211_state *state, enum id_input idby,
 	       int argc, char **argv);
json/json_print.c new file +206 -0

206 lines not on this page · raw message

json/json_print.h new file +57 -0

57 lines not on this page · raw message

json/json_writer.c new file +296 -0

296 lines not on this page · raw message

json/json_writer.h new file +68 -0

68 lines not on this page · raw message

json/main.c new file +28 -0
new file mode 100644
index 0000000..21d7cf4
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <stdint.h>
+
+#include "json_print.h"
+
+
+int main() {
+    // init json print without pretty print
+	new_json_obj(1, 0);
+	open_json_object("tx");
+	print_u64(PRINT_JSON, "bytes", NULL, 1);
+	print_u64(PRINT_JSON, "packets", NULL, 1);
+	print_u64(PRINT_JSON, "errors", NULL, 1);
+	print_u64(PRINT_JSON, "dropped", NULL, 1);
+    open_json_array("hobbies", NULL); // create object entry hobbies with array
+    print_string(PRINT_JSON, NULL, "%s", "Reading");
+    print_string(PRINT_JSON, NULL, "%s", "Hiking");
+    print_string(PRINT_JSON, NULL, "%s", "Coding");
+	close_json_array(NULL);
+	close_json_object();
+
+
+    // finish
+    delete_json_obj();
+
+    return 0;
+}
+
scan.c +1368 -749

2,274 lines not on this page · raw message

util.c +356 -136

492 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.42.0

Part of the diff is not shown here. View the raw message.