Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go CLI #82

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions build/cli/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 = \
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module unified-wifi-mesh/cli

go 1.18
7 changes: 4 additions & 3 deletions inc/em_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
#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_MAX_E4_TABLE_CHANNEL 32

#define EM_PROTO_TOUT 1
Expand Down Expand Up @@ -201,7 +202,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;
Expand Down Expand Up @@ -2742,7 +2743,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;
Expand Down
6 changes: 4 additions & 2 deletions inc/em_cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 17 additions & 10 deletions inc/em_cli_apis.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file removed src/cli/a.out
Binary file not shown.
137 changes: 120 additions & 17 deletions src/cli/em_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,27 @@ void em_cli_t::print_network_tree_node(em_network_node_t *node, unsigned int *pi
{
unsigned int i, ident = 0;
em_long_string_t fmt = {0};
em_long_string_t value_str = {0};
em_short_string_t tmp_str;

ident = *pident;
ident++;
*pident = ident;

for (i = 0; i < ident; i++) {
strncat(fmt, "\t", strlen("\t"));
strncat(fmt, " ", strlen(" "));
}

switch (node->type) {
case em_network_node_data_type_invalid:
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:
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/cli/em_cmd_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
Expand Down
16 changes: 5 additions & 11 deletions src/cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
const char *prompt = "<<OneWifiMeshCli>>: ";


int editor(const char *args)
int editor(char *args)
{
em_long_string_t cmd;

Expand All @@ -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) {
Expand All @@ -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);
Expand Down
Loading
Loading