From e68253ed889b361cdb4c8bc2d5669e9a593946df Mon Sep 17 00:00:00 2001 From: Soumya Munshi <167132989+soumyasmunshi@users.noreply.github.com> Date: Fri, 13 Dec 2024 08:59:29 -0800 Subject: [PATCH 1/2] Go CLI --- build/cli/makefile | 8 ++- go.mod | 3 + inc/em_base.h | 6 +- inc/em_cli.h | 6 +- inc/em_cli_apis.h | 27 +++++--- src/cli/a.out | Bin 8912 -> 0 bytes src/cli/em_cli.cpp | 137 ++++++++++++++++++++++++++++++++++++----- src/cli/em_cmd_cli.cpp | 2 +- src/cli/main.c | 16 ++--- src/cli/main.go | 47 ++++++++++++++ 10 files changed, 206 insertions(+), 46 deletions(-) create mode 100644 go.mod delete mode 100755 src/cli/a.out create mode 100644 src/cli/main.go diff --git a/build/cli/makefile b/build/cli/makefile index edacc1f..767c8fb 100755 --- a/build/cli/makefile +++ b/build/cli/makefile @@ -23,6 +23,7 @@ include ../makefile.inc # PROGRAM = $(INSTALLDIR)/bin/onewifi_em_cli +PROGRAM_GO = $(INSTALLDIR)/bin/cli EM_CLI_LIBRARY = $(INSTALLDIR)/lib/libemcli.so INCLUDEDIRS = \ @@ -71,7 +72,7 @@ GENERIC_OBJECTS = $(GENERIC_SOURCES:.c=.o) EMCLIOBJECTS = $(CLI_OBJECTS) $(GENERIC_OBJECTS) MAIN_OBJECT = $(MAIN_SOURCE:.c=.o) -all: $(EM_CLI_LIBRARY) $(PROGRAM) +all: $(EM_CLI_LIBRARY) $(PROGRAM) $(PROGRAM_GO) $(PROGRAM): $(MAIN_OBJECT) $(CXX) -o $@ $(MAIN_OBJECT) $(LDFLAGS) @@ -88,11 +89,14 @@ $(CLI_OBJECTS): %.o: %.cpp $(MAIN_OBJECT): %.o: %.c $(CXX) $(CFLAGS) -o $@ -c $< +$(PROGRAM_GO): + go build -o $(PROGRAM_GO) $(ONEWIFI_EM_SRC)/cli/main.go + # Clean target: "make -f Makefile.Linux clean" to remove unwanted objects and executables. # clean: - $(RM) $(MAIN_OBJECT) $(EMCLIOBJECTS) $(EM_CLI_LIBRARY) $(PROGRAM) + $(RM) $(MAIN_OBJECT) $(EMCLIOBJECTS) $(EM_CLI_LIBRARY) $(PROGRAM) $(PROGRAM_GO) # # Run target: "make -f Makefile.Linux run" to execute the application diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5144bca --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module unified-wifi-mesh/cli + +go 1.18 diff --git a/inc/em_base.h b/inc/em_base.h index aa73470..1989f1d 100644 --- a/inc/em_base.h +++ b/inc/em_base.h @@ -31,7 +31,7 @@ #define MAX_INTF_NAME_SZ 16 #define EM_MAC_STR_LEN 17 #define EM_MAX_COLS 32 -#define EM_MAX_DM_CHILDREN 32 +#define EM_MAX_DM_CHILDREN 64 #define EM_PROTO_TOUT 1 #define EM_MGR_TOUT 1 @@ -200,7 +200,7 @@ typedef char em_subdoc_data_buff_t[EM_SUBDOC_BUFF_SZ]; typedef char em_status_string_t[EM_IO_BUFF_SZ]; typedef unsigned char em_raw_data_t[EM_SUBDOC_BUFF_SZ]; -typedef int (* em_editor_callback_t)(const char *); +typedef int (* em_editor_callback_t)(char *); typedef struct { unsigned char dsap; @@ -2701,7 +2701,7 @@ typedef enum { } em_network_node_data_type_t; typedef struct em_network_node { - em_short_string_t key; + em_long_string_t key; em_network_node_data_type_t type; em_long_string_t value_str; unsigned int value_int; diff --git a/inc/em_cli.h b/inc/em_cli.h index 0c67d79..1842aa8 100644 --- a/inc/em_cli.h +++ b/inc/em_cli.h @@ -28,12 +28,14 @@ class em_cli_t { public: em_editor_callback_t m_editor_cb; - char *exec(char *in, size_t in_len, em_status_string_t out); + char *exec(char *in, size_t in_len, em_status_string_t out); + em_network_node_t *exec(char *in, size_t in_len); int init(em_editor_callback_t func); const char *get_first_cmd_str(); const char *get_next_cmd_str(const char *cmd); - em_network_node_t *get_network_tree(const char *file_name); + em_network_node_t *get_network_tree_by_file(const char *file_name); + em_network_node_t *get_network_tree(char *buff); int get_network_tree_node(cJSON *obj, em_network_node_t *root); void free_network_tree(em_network_node_t *tree); void free_network_tree_node(em_network_node_t *node); diff --git a/inc/em_cli_apis.h b/inc/em_cli_apis.h index b24aaa4..58e576b 100644 --- a/inc/em_cli_apis.h +++ b/inc/em_cli_apis.h @@ -19,16 +19,23 @@ #ifndef EM_CLI_APIS_H #define EM_CLI_APIS_H -#include "em_base.h" - +#ifdef __cplusplus extern "C" { - char *exec(char *in, size_t in_len, em_status_string_t out); - int init(em_editor_callback_t func); - const char *get_first_cmd_str(); - const char *get_next_cmd_str(const char *cmd); - em_network_node_t *get_network_tree(const char *file_name); - void free_network_tree(em_network_node_t *node); - void print_network_tree(em_network_node_t *node); -} +#endif +#include "em_base.h" + +em_network_node_t *exec(char *in, size_t in_len); +int init(em_editor_callback_t func); +const char *get_first_cmd_str(); +const char *get_next_cmd_str(const char *cmd); +em_network_node_t *get_network_tree_by_file(const char *file_name); +em_network_node_t *get_network_tree(char *buff); +void free_network_tree(em_network_node_t *node); +void print_network_tree(em_network_node_t *node); + +#ifdef __cplusplus +} // extern "C" #endif + +#endif // EM_CLI_APIS_H diff --git a/src/cli/a.out b/src/cli/a.out deleted file mode 100755 index e4ba02e6d4411d2d102aff5af56549291ad27316..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8912 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 { @@ -63,9 +65,11 @@ void em_cli_t::print_network_tree_node(em_network_node_t *node, unsigned int *pi break; case em_network_node_data_type_false: + printf("%s%s:\tfalse\n", fmt, node->key); break; case em_network_node_data_type_true: + printf("%s%s:\ttrue\n", fmt, node->key); break; case em_network_node_data_type_null: @@ -80,17 +84,79 @@ void em_cli_t::print_network_tree_node(em_network_node_t *node, unsigned int *pi break; case em_network_node_data_type_array: + printf("%s%s:", fmt, node->key); + if ((node->num_children > 0) && ((node->child[0]->type == em_network_node_data_type_array) || + (node->child[0]->type == em_network_node_data_type_obj))) { + printf("\n"); + } else if (node->num_children == 0) { + //printf("\n"); + } + break; + case em_network_node_data_type_obj: - printf("%s%s:\t\n", fmt, node->key); + if (node->key[0] != 0) { + printf("%s%s\t\n", fmt, node->key); + } break; case em_network_node_data_type_raw: break; } + + if ((node->type == em_network_node_data_type_array) && (node->num_children > 0) && + ((node->child[0]->type == em_network_node_data_type_number) || + (node->child[0]->type == em_network_node_data_type_string))) { + + snprintf(value_str, sizeof(em_long_string_t), "["); + for (i = 0; i < node->num_children; i++) { + if (node->child[0]->type == em_network_node_data_type_string) { + snprintf(tmp_str, sizeof(em_short_string_t), "%s, ", node->child[i]->value_str); + } else { + snprintf(tmp_str, sizeof(em_short_string_t), "%d, ", node->child[i]->value_int); + } + strncat(value_str, tmp_str, strlen(tmp_str)); + } - for (i = 0; i < node->num_children; i++) { - print_network_tree_node(node->child[i], pident); + value_str[strlen(value_str) - 2] = ']'; + printf("%s\n", value_str); + } else { + + if (node->type == em_network_node_data_type_array) { + if (node->num_children == 0) { + printf("["); + } else { + snprintf(value_str, sizeof(value_str), "%s[", fmt); + printf("%s\n", value_str); + } + } else if (node->type == em_network_node_data_type_obj) { + if (node->num_children == 0) { + printf("{"); + } else { + snprintf(value_str, sizeof(value_str), "%s{", fmt); + printf("%s\n", value_str); + } + } + + for (i = 0; i < node->num_children; i++) { + print_network_tree_node(node->child[i], pident); + } + if (node->type == em_network_node_data_type_array) { + if (node->num_children == 0) { + printf("]\n"); + } else { + snprintf(value_str, sizeof(value_str), "%s]", fmt); + printf("%s\n", value_str); + } + } else if (node->type == em_network_node_data_type_obj) { + if (node->num_children == 0) { + printf("}\n"); + } else { + snprintf(value_str, sizeof(value_str), "%s}", fmt); + printf("%s\n", value_str); + } + + } } ident = *pident; @@ -156,16 +222,11 @@ int em_cli_t::get_network_tree_node(cJSON *obj, em_network_node_t *root) return root->num_children; } -em_network_node_t *em_cli_t::get_network_tree(const char *file_name) +em_network_node_t *em_cli_t::get_network_tree(char *buff) { cJSON *obj = NULL; - char buff[EM_IO_BUFF_SZ]; em_network_node_t *root; - if (em_cmd_cli_t::load_params_file(file_name, buff) < 0) { - return NULL; - } - if ((obj = cJSON_Parse(buff)) == NULL) { return NULL; } @@ -177,6 +238,18 @@ em_network_node_t *em_cli_t::get_network_tree(const char *file_name) cJSON_Delete(obj); return root; + +} + +em_network_node_t *em_cli_t::get_network_tree_by_file(const char *file_name) +{ + char buff[EM_IO_BUFF_SZ]; + + if (em_cmd_cli_t::load_params_file(file_name, buff) < 0) { + return NULL; + } + + return get_network_tree(buff); } void em_cli_t::free_network_tree_node(em_network_node_t *node) @@ -324,7 +397,7 @@ em_cmd_t& em_cli_t::get_command(char *in, size_t in_len) char *em_cli_t::exec(char *in, size_t sz, em_status_string_t out) { - em_long_string_t cmd; + em_long_string_t cmd; em_status_string_t res; em_cmd_cli_t *cli_cmd; @@ -348,6 +421,31 @@ char *em_cli_t::exec(char *in, size_t sz, em_status_string_t out) return out; } +em_network_node_t *em_cli_t::exec(char *in, size_t sz) +{ + em_long_string_t cmd; + em_status_string_t res; + em_cmd_cli_t *cli_cmd; + + snprintf(cmd, sizeof(cmd), "%s", in); + cli_cmd = new em_cmd_cli_t(get_command(cmd, sz)); + + cli_cmd->init(); + + if (cli_cmd->validate() == false) { + cli_cmd->m_cmd.status_to_string(em_cmd_out_status_invalid_input, res); + } else { + if (cli_cmd->execute(res) != 0) { + cli_cmd->m_cmd.status_to_string(em_cmd_out_status_invalid_input, res); + + } + } + + delete cli_cmd; + + return get_network_tree(res); +} + int em_cli_t::init(em_editor_callback_t cb) { m_editor_cb = cb; @@ -368,9 +466,9 @@ em_cli_t *get_cli() return &g_cli; } -extern "C" char *exec(char *in, size_t in_len, em_status_string_t out) +extern "C" em_network_node_t *exec(char *in, size_t in_len) { - return g_cli.exec(in, in_len, out); + return g_cli.exec(in, in_len); } extern "C" int init(em_editor_callback_t func) @@ -388,14 +486,19 @@ extern "C" const char *get_next_cmd_str(const char *cmd) return g_cli.get_next_cmd_str(cmd); } -extern "C" em_network_node_t *get_network_tree(const char *file_name) +extern "C" void *get_network_tree_by_file(const char *file_name) +{ + return g_cli.get_network_tree_by_file(file_name); +} + +extern "C" void *get_network_tree(char *buff) { - return g_cli.get_network_tree(file_name); + return g_cli.get_network_tree(buff); } -extern "C" void free_network_tree(em_network_node_t *node) +extern "C" void free_network_tree(void *node) { - return g_cli.free_network_tree(node); + return g_cli.free_network_tree((em_network_node_t *)node); } extern "C" void print_network_tree(em_network_node_t *node) diff --git a/src/cli/em_cmd_cli.cpp b/src/cli/em_cmd_cli.cpp index 12fc533..cf254e8 100644 --- a/src/cli/em_cmd_cli.cpp +++ b/src/cli/em_cmd_cli.cpp @@ -217,7 +217,7 @@ int em_cmd_cli_t::edit_params_file(const char *filename) char buff[EM_IO_BUFF_SZ]; cJSON *obj; - m_cli.m_editor_cb(filename); + m_cli.m_editor_cb((char *)filename); if (load_params_file(filename, buff) < 0) { printf("%s:%d: Failed to load params file\n", __func__, __LINE__); diff --git a/src/cli/main.c b/src/cli/main.c index 7e85d51..bd62f01 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -46,7 +46,7 @@ const char *prompt = "<>: "; -int editor(const char *args) +int editor(char *args) { em_long_string_t cmd; @@ -59,17 +59,8 @@ int editor(const char *args) int main(int argc, const char *argv[]) { char *line = NULL; - em_status_string_t output; em_network_node_t *node; -#ifdef TEST_CLI - node = get_network_tree("./NetworkSSID.json"); - print_network_tree(node); - free_network_tree(node); - - return 0; -#endif - init(editor); while(1) { @@ -91,7 +82,10 @@ int main(int argc, const char *argv[]) add_history(line); } - printf("%s\n", exec(line, strlen(line), output)); + if ((node = exec(line, strlen(line))) != NULL) { + print_network_tree(node); + free_network_tree(node); + } } free(line); diff --git a/src/cli/main.go b/src/cli/main.go new file mode 100644 index 0000000..288f6c3 --- /dev/null +++ b/src/cli/main.go @@ -0,0 +1,47 @@ +package main + +/* +#cgo CFLAGS: -I../../inc -I../../../OneWifi/include -I../../../OneWifi/source/utils -I../../../halinterface/include +#cgo LDFLAGS: -L../../install/lib -lemcli -lcjson -lreadline +#include +#include +#include "em_cli_apis.h" + +extern int editor_func(char *s); + +static int register_editor_cb() { + return init(editor_func); +} +*/ +import "C" + +import ( + "unsafe" + "fmt" + "runtime" +) + +//export editor_func +func editor_func(*C.char) C.int { + fmt.Println("Inside Go Callnack") + return 0 +} + +func main() { + + C.register_editor_cb() + for { + prompt := C.CString("<>: ") + line := C.readline(prompt) + C.free(unsafe.Pointer(prompt)) + + node := C.exec(line, C.strlen(line)) + if node != nil { + C.print_network_tree(node) + C.free_network_tree(node) + } + + C.free(unsafe.Pointer(line)) + } + +} From e65e9dd651796ed3a4b21540d5420071bf7aa4e5 Mon Sep 17 00:00:00 2001 From: Soumya Munshi <167132989+soumyasmunshi@users.noreply.github.com> Date: Fri, 13 Dec 2024 11:58:22 -0800 Subject: [PATCH 2/2] Go CLI changes for parsing JSON --- inc/em_cli.h | 2 + inc/em_cli_apis.h | 1 + install/bin/NetworkSSID.json | 7 ++-- src/cli/em_cli.cpp | 81 ++++++++++++++++++++++++++++++++++++ src/cli/main.go | 2 +- 5 files changed, 88 insertions(+), 5 deletions(-) diff --git a/inc/em_cli.h b/inc/em_cli.h index 1842aa8..5dfff68 100644 --- a/inc/em_cli.h +++ b/inc/em_cli.h @@ -39,6 +39,8 @@ class em_cli_t { int get_network_tree_node(cJSON *obj, em_network_node_t *root); void free_network_tree(em_network_node_t *tree); void free_network_tree_node(em_network_node_t *node); + void *network_tree_to_json(em_network_node_t *root); + cJSON *network_tree_node_to_json(em_network_node_t *node, cJSON *parent); void print_network_tree(em_network_node_t *tree); void print_network_tree_node(em_network_node_t *node, unsigned int *pident); diff --git a/inc/em_cli_apis.h b/inc/em_cli_apis.h index 58e576b..08bedef 100644 --- a/inc/em_cli_apis.h +++ b/inc/em_cli_apis.h @@ -32,6 +32,7 @@ const char *get_next_cmd_str(const char *cmd); em_network_node_t *get_network_tree_by_file(const char *file_name); em_network_node_t *get_network_tree(char *buff); void free_network_tree(em_network_node_t *node); +void *network_tree_to_json(em_network_node_t *node); void print_network_tree(em_network_node_t *node); #ifdef __cplusplus diff --git a/install/bin/NetworkSSID.json b/install/bin/NetworkSSID.json index 856eaa7..1f3cc01 100644 --- a/install/bin/NetworkSSID.json +++ b/install/bin/NetworkSSID.json @@ -1,6 +1,5 @@ { - "wfa-dataelements:SetSSID": { - + "wfa-dataelements:SetSSID": { "NetworkSSIDList": [{ "SSID": "private_ssid", "PassPhrase": "test-fronthaul", @@ -46,6 +45,6 @@ "MobilityDomain": "00:01:02:03:04:05", "HaulType": ["Backhaul"] }], - "ID": "OneWifiMesh" + "ID": "OneWifiMesh" } -} +} \ No newline at end of file diff --git a/src/cli/em_cli.cpp b/src/cli/em_cli.cpp index b01c32f..6d41148 100644 --- a/src/cli/em_cli.cpp +++ b/src/cli/em_cli.cpp @@ -171,6 +171,82 @@ void em_cli_t::print_network_tree(em_network_node_t *node) print_network_tree_node(node, &ident); } +cJSON *em_cli_t::network_tree_node_to_json(em_network_node_t *node, cJSON *parent) +{ + unsigned int i; + cJSON *obj; + + switch (node->type) { + case em_network_node_data_type_invalid: + break; + + case em_network_node_data_type_false: + obj = cJSON_CreateFalse(); + break; + + case em_network_node_data_type_true: + obj = cJSON_CreateTrue(); + break; + + case em_network_node_data_type_null: + obj = cJSON_CreateNull(); + break; + + case em_network_node_data_type_number: + obj = cJSON_CreateNumber(node->value_int); + break; + + case em_network_node_data_type_string: + obj = cJSON_CreateString(node->value_str); + break; + + case em_network_node_data_type_array: + obj = cJSON_CreateArray(); + break; + + case em_network_node_data_type_obj: + obj = cJSON_CreateObject(); + break; + + case em_network_node_data_type_raw: + break; + } + + if (obj == NULL) { + printf("%s:%d: Failed to allocate JSON object\n"); + return NULL; + } + + cJSON_AddItemToObject(parent, node->key, obj); + + for (i = 0; i < node->num_children; i++) { + network_tree_node_to_json(node->child[i], obj); + } + + return obj; + +} + +void *em_cli_t::network_tree_to_json(em_network_node_t *root) +{ + cJSON *obj; + unsigned int i; + + obj = cJSON_CreateObject(); + if (obj == NULL) { + printf("%s:%d: Failed to allocate JSON object\n"); + return NULL; + } + + for (i = 0; i < root->num_children; i++) { + network_tree_node_to_json(root->child[i], obj); + } + + //printf("%s:%d: %s\n", __func__, __LINE__, cJSON_Print(obj)); + + return obj; +} + int em_cli_t::get_network_tree_node(cJSON *obj, em_network_node_t *root) { cJSON *child_obj, *tmp_obj; @@ -501,6 +577,11 @@ extern "C" void free_network_tree(void *node) return g_cli.free_network_tree((em_network_node_t *)node); } +extern "C" void *network_tree_to_json(em_network_node_t *node) +{ + return g_cli.network_tree_to_json(node); +} + extern "C" void print_network_tree(em_network_node_t *node) { return g_cli.print_network_tree(node); diff --git a/src/cli/main.go b/src/cli/main.go index 288f6c3..48b6691 100644 --- a/src/cli/main.go +++ b/src/cli/main.go @@ -18,7 +18,6 @@ import "C" import ( "unsafe" "fmt" - "runtime" ) //export editor_func @@ -38,6 +37,7 @@ func main() { node := C.exec(line, C.strlen(line)) if node != nil { C.print_network_tree(node) + //C.network_tree_to_json(node) C.free_network_tree(node) }