diff --git a/Makefile b/Makefile index 7d29767a37047f8c3c3c2359547a7bda9dea73da..f38c1cead4eec51bfc5403eee26af12a7a90ee53 100644 --- a/Makefile +++ b/Makefile @@ -21,10 +21,10 @@ libmobile1_hilink: ${HLOBJS} libmobile2_hilink: ${HLOBJS} ${CC} ${HLOBJS} -shared -o libmobile_hilink.so -HOBJS = common_hilink.o -HSRCS = common_hilink.c -common_hilink: ${HOBJS} - ${CC} -c ${CFLAGS} ${HSRCS} -o ${HOBJS} +COBJS = common.o +CSRCS = common.c +common: ${COBJS} + ${CC} -c ${CFLAGS} ${CSRCS} -o ${COBJS} STOBJS = stack_operations.o STSRCS = stack_operations.c @@ -34,7 +34,7 @@ stack_operations: ${STOBJS} DIOBJS = dongle_infrastructure.o DISRCS = dongle_infrastructure.c dongle_infrastructure: ${DIOBJS} - ${CC} -c ${CWFLAGS} ${DISRCS} ${ZCOBJS} ${STOBJS} -o ${DIOBJS} -L . ${LIBS} + ${CC} -c ${CWFLAGS} ${DISRCS} ${STOBJS} ${COBJS} -o ${DIOBJS} -L . ${LIBS} dongle: dongle.o ${CC} ${CWFLAGS} dongle.o ${ZCOBJS} ${DIOBJS} ${STOBJS} -o dongle -L . ${LIBS} diff --git a/common_hilink.c b/common.c similarity index 54% rename from common_hilink.c rename to common.c index 866c33f9da662f79a5f966a7262e240d9c1ab006..b0b1302b582c8f34a158c65ab501a0a7b877e84f 100644 --- a/common_hilink.c +++ b/common.c @@ -1,4 +1,56 @@ -#include "common_hilink.h" +#include "common.h" + +int print_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req) +{ + struct blob_buf bb; + + memset(&bb, 0, sizeof(bb)); + blob_buf_init(&bb, 0); + bb = json_to_blob(parsed_response, bb); + ubus_send_reply(ctx, req, bb.head); + blob_buf_free(&bb); + + json_object_put(parsed_response); + return 0; +} + +struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb) +{ + void *arr, *obj; + int i; + + json_object_object_foreach(response, key, val) { + int val_type = json_object_get_type(val); + + switch (val_type) { + case json_type_int: + blobmsg_add_u32(&bb, key, json_object_get_int(val)); + break; + case json_type_double: + blobmsg_add_double(&bb, key, json_object_get_double(val)); + break; + case json_type_string: + blobmsg_add_string(&bb, key, json_object_get_string(val)); + break; + case json_type_array: + arr = blobmsg_open_array(&bb, key); + for (i = 0; i < json_object_array_length(val); i++) { + // add open table + bb = json_to_blob(json_object_array_get_idx(val, i), bb); + // add close table + } + blobmsg_close_array(&bb, arr); + break; + case json_type_object: + obj = blobmsg_open_table(&bb, key); + bb = json_to_blob(val, bb); + blobmsg_close_table(&bb, obj); + break; + } + } + + return bb; +} //need to free tag content.. char *xml_parser(struct write_result *result, char *tag) @@ -17,23 +69,25 @@ char *xml_parser(struct write_result *result, char *tag) xpathctx_ptr = doc_ptr ? xmlXPathNewContext(doc_ptr) : NULL; xpathobj_ptr = xpathctx_ptr ? xmlXPathEvalExpression((xmlChar *)tag, xpathctx_ptr) : NULL; - if (xpathobj_ptr) { + if (xpathobj_ptr) + { int n; xmlNodeSetPtr nodeset_ptr = xpathobj_ptr->nodesetval; - for (n = 0; nodeset_ptr && n < xmlXPathNodeSetGetLength(nodeset_ptr); n++) { + for (n = 0; nodeset_ptr && n < xmlXPathNodeSetGetLength(nodeset_ptr); n++) + { //if (DEBUG) //printf("n value: %d\n", n); xmlNodePtr node_ptr = nodeset_ptr->nodeTab[n]; if (node_ptr->type != XML_ATTRIBUTE_NODE && node_ptr->type != XML_ELEMENT_NODE && node_ptr->type != XML_CDATA_SECTION_NODE) continue; - if (node_ptr->children) { + if (node_ptr->children) + { //tag_content = (char*) node_ptr->children->content; strcpy(tag_content, (char *)node_ptr->children->content); if (DEBUG) printf("tag content: %s\n", tag_content); break; } - } } @@ -48,7 +102,6 @@ char *xml_parser(struct write_result *result, char *tag) leave: return NULL; - } void xml_to_json(xmlNode *anode, json_object *jobj) @@ -56,19 +109,24 @@ void xml_to_json(xmlNode *anode, json_object *jobj) xmlNodePtr cur_node = NULL; json_object *cur_jstr = NULL; - for (cur_node = anode; cur_node; cur_node = cur_node->next) { - if (DEBUG_COMMON) { + for (cur_node = anode; cur_node; cur_node = cur_node->next) + { + if (DEBUG_COMMON) + { printf("child address: %p\n", cur_node); printf("next addres: %p\n", cur_node->next); printf("root child content: %s\n", (uint8_t *)xmlNodeGetContent(cur_node)); } - if (cur_node->type == XML_ELEMENT_NODE) { - if (xmlChildElementCount(cur_node) == 0) { + if (cur_node->type == XML_ELEMENT_NODE) + { + if (xmlChildElementCount(cur_node) == 0) + { cur_jstr = json_object_new_string((char *)xmlNodeGetContent(cur_node)); - if (DEBUG_COMMON) { - printf ("xmlNodeGet name: %sn", (char *)cur_node->name); - printf ("xmlNodeGetContent: %sn", (char *)xmlNodeGetContent(cur_node)); + if (DEBUG_COMMON) + { + printf("xmlNodeGet name: %sn", (char *)cur_node->name); + printf("xmlNodeGetContent: %sn", (char *)xmlNodeGetContent(cur_node)); } json_object_object_add(jobj, (char *)cur_node->name, cur_jstr); @@ -79,8 +137,6 @@ void xml_to_json(xmlNode *anode, json_object *jobj) } } - - void xml_to_json_converter(struct write_result *result) { json_object *jobj; @@ -94,11 +150,8 @@ void xml_to_json_converter(struct write_result *result) jobj = json_object_new_object(); xml_to_json(origin_node, jobj); - if (DEBUG_COMMON) { - printf ("The json object created: %s\n",json_object_to_json_string(jobj)); + if (DEBUG_COMMON) + { + printf("The json object created: %s\n", json_object_to_json_string(jobj)); } - } - - - diff --git a/common_zte.h b/common.h similarity index 85% rename from common_zte.h rename to common.h index ba48c0b9edd83cc868a4f47c218ba89f3ae4152d..f9bc760b01b7d18e648ff8a35637a91efd3dfe61 100644 --- a/common_zte.h +++ b/common.h @@ -14,15 +14,20 @@ #include <libubox/list.h> #include <libubox/uloop.h> #include <ctype.h> - -#include "libmobile_zte.h" #include <json-c/json.h> #include <string.h> #include <libubox/blobmsg.h> #include <libubox/blobmsg_json.h> #include <libubus.h> +#include <libxml2/libxml/parser.h> +#include <libxml/xpath.h> +#include <libxml/xpathInternals.h> + +#include "libmobile_hilink.h" +#include "libmobile_zte.h" extern int debug; +extern char *ip_addr; extern struct ubus_context *global_ctx; #define debug_print(...) \ @@ -61,4 +66,7 @@ int print_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, * Blob buffer containing the replicated json_object. */ struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb); + +char *xml_parser(struct write_result *result, char *tag); +void xml_to_json_converter(struct write_result *result); #endif diff --git a/common_hilink.h b/common_hilink.h deleted file mode 100644 index 8f3c194ba32466d9f2a7b8d5fa79b6f424b97fd5..0000000000000000000000000000000000000000 --- a/common_hilink.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef COMMON_HILINK_H -#define COMMON_HILINK_H - -#include "libmobile_hilink.h" - - -#include <libxml2/libxml/parser.h> -#include <libxml/xpath.h> -#include <libxml/xpathInternals.h> - -#include <json-c/json.h> - -#define DEBUG_COMMON 0 - -char *xml_parser(struct write_result *result, char *tag); -void xml_to_json_converter(struct write_result *result); - - - -#endif diff --git a/common_zte.c b/common_zte.c deleted file mode 100644 index 1e42a83211149f060411e2d26dc2b21ede66707b..0000000000000000000000000000000000000000 --- a/common_zte.c +++ /dev/null @@ -1,53 +0,0 @@ -#include "common_zte.h" - -int print_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req) -{ - struct blob_buf bb; - - memset(&bb, 0, sizeof(bb)); - blob_buf_init(&bb, 0); - bb = json_to_blob(parsed_response, bb); - ubus_send_reply(ctx, req, bb.head); - blob_buf_free(&bb); - - json_object_put(parsed_response); - return 0; -} - -struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb) -{ - void *arr, *obj; - int i; - - json_object_object_foreach(response, key, val) { - int val_type = json_object_get_type(val); - - switch (val_type) { - case json_type_int: - blobmsg_add_u32(&bb, key, json_object_get_int(val)); - break; - case json_type_double: - blobmsg_add_double(&bb, key, json_object_get_double(val)); - break; - case json_type_string: - blobmsg_add_string(&bb, key, json_object_get_string(val)); - break; - case json_type_array: - arr = blobmsg_open_array(&bb, key); - for (i = 0; i < json_object_array_length(val); i++) { - // add open table - bb = json_to_blob(json_object_array_get_idx(val, i), bb); - // add close table - } - blobmsg_close_array(&bb, arr); - break; - case json_type_object: - obj = blobmsg_open_table(&bb, key); - bb = json_to_blob(val, bb); - blobmsg_close_table(&bb, obj); - break; - } - } - - return bb; -} diff --git a/dongle.c b/dongle.c index c362b91a7ab3836415388ac3cc090b8d8619e398..90ea905939898e80dc6262e35e40d6b2e3ee7410 100644 --- a/dongle.c +++ b/dongle.c @@ -1,6 +1,6 @@ #include <getopt.h> -#include "common_zte.h" +#include "common.h" #include "dongle_infrastructure.h"