diff --git a/Makefile b/Makefile index bb0d1637315b88801ac2574fb8d4dd36d6ddda4e..47e3c806abff70a0378514adda94e8bb8d176b04 100644 --- a/Makefile +++ b/Makefile @@ -6,28 +6,32 @@ PAOBJS = parse_args.o PASRCS = parse_args.c HSRCS = parse_args.h -all: libmobile dongle_apn dongle_pin dongle_network +all: common libmobile dongle_apn dongle_pin dongle_network + +COBJS = common.o +CSRCS = common.c +common: ${COBJS} + ${CC} -c ${CSRCS} -o ${COBJS} MOBJS = libmobile.o MSRCS = libmobile.c - libmobile: ${MOBJS} ${CC} -c ${MSRCS} -o ${MOBJS} DAOBJS = dongle_apn.o DASRCS = dongle_apn.c dongle_apn: ${DAOBJS} - ${CC} ${CFLAGS} ${DAOBJS} ${MOBJS} -o dongle_apn ${LIBS} + ${CC} ${CFLAGS} ${DAOBJS} ${MOBJS} ${COBJS} -o dongle_apn ${LIBS} DPOBJS = dongle_pin.o DPSRCS = dongle_pin.c dongle_pin: ${DPOBJS} - ${CC} ${CFLAGS} ${DPOBJS} ${MOBJS} -o dongle_pin ${LIBS} + ${CC} ${CFLAGS} ${DPOBJS} ${MOBJS} ${COBJS} -o dongle_pin ${LIBS} DNOBJS = dongle_network.o DNSRCS = dongle_network.c dongle_network: ${DNOBJS} - ${CC} ${CFLAGS} ${DNOBJS} ${MOBJS} -o dongle_network ${LIBS} + ${CC} ${CFLAGS} ${DNOBJS} ${MOBJS} ${COBJS} -o dongle_network ${LIBS} clean: rm -f dongle_apn dongle_pin dongle_network *.o diff --git a/common.c b/common.c new file mode 100644 index 0000000000000000000000000000000000000000..72d1160e2dcfbce9215adc45bcaaefc82867f491 --- /dev/null +++ b/common.c @@ -0,0 +1,70 @@ +#include "common.h" + +int parse_and_print(char *dongle_response, struct ubus_context *ctx, struct ubus_request_data *req) +{ + if (!dongle_response) { + printf("no respose!\n"); + goto fail; + } + struct json_object *parsed_response = json_tokener_parse(dongle_response); + + if (!parsed_response) { + printf("No valid JSON, failed parsing!\n"); + goto free_response; + } + write_to_ubus(parsed_response, ctx, req); + + json_object_put(parsed_response); +free_response: + free(dongle_response); +fail: + return 0; +} + +int write_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req) +{ + struct blob_buf bb; + + memset(&bb, 0, sizeof(struct blob_buf)); + 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++) + bb = json_to_blob(json_object_array_get_idx(val, i), bb); + blobmsg_close_array(&bb, arr); + break; + case json_type_object: + obj = blobmsg_open_table(&bb, key); + bb = json_to_blob(val, bb); + blobmsg_close_array(&bb, obj); + break; + } + } + + return bb; +} \ No newline at end of file diff --git a/common.h b/common.h index cad2ff1dd5f4d85ad8ad0012a33c755e365bcbc7..b9f250c2b3b8b9318df31cf2a3149226d5f1f91f 100644 --- a/common.h +++ b/common.h @@ -1,10 +1,62 @@ #ifndef COMMON_H #define COMMON_H -#define PIPE_PATH "/tmp/appipe" #define DEBUG(message, ...) \ do \ { \ fprintf(stdout, "\n(DEBUG)\t"); \ fprintf(stdout, message, ##__VA_ARGS__);\ } while (0) + +#include <json-c/json.h> +#include <string.h> +#include <libubox/blobmsg.h> +#include <libubus.h> +/** + * Function: parse_and_print + * + * Parses a string to its corresponding JSON format, if available, then writes it on ubus through <write_to_ubus>. + * + * IMPORTANT NOTE + * Will free the input string! + * + * Parameters: + * dongle_response - A string of JSON format that should be printed on ubus. + * ctx - Ubus context containing connection. + * req - Information for from the ubus request. + * + * Returns: + * 0 On success. + * -1 On failure. + */ +int parse_and_print(char *dongle_response, struct ubus_context *ctx, struct ubus_request_data *req); + +/** + * Function: write_to_ubus + * + * Prints a json_object pointer's json structure to ubus. + * + * Parameters: + * parsed_response - A struct json_object pointer to json structure to be printed to ubus. + * ctx - Ubus context containing connection. + * req - Information for from the ubus request. + * + * Returns: + * 0 On success. + * -1 On failure. + */ +int write_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req); + +/** + * Function: json_to_blob + * + * Parses a json_object pointer to a corresponding blob buffer. + * + * Parameters: + * response - json_object pointer to be replicated in a blob buffer. + * bb - The blob buffer to hold the results. + * + * Returns: + * Blob buffer containing the replicated json_object. + */ +struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb); #endif diff --git a/dongle_apn.c b/dongle_apn.c index 59fd762c2a33ddcd2511fa2b7438bf1098158df9..6e66e91c6c00a823ef4990a4efe770c561e44bec 100644 --- a/dongle_apn.c +++ b/dongle_apn.c @@ -13,6 +13,7 @@ #include <libubox/list.h> #include "libmobile.h" +#include "common.h" struct ubus_context *ctx; diff --git a/dongle_network.c b/dongle_network.c index 6883e1b1c23346c004f757dfcff691df09324308..c4a38d7cbcd1ec90c952d43eb348bd0e540616fa 100644 --- a/dongle_network.c +++ b/dongle_network.c @@ -14,6 +14,7 @@ #include <libubox/list.h> #include "libmobile.h" +#include "common.h" struct ubus_context *ctx; @@ -21,7 +22,7 @@ int get_signal_strength(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { - char *response = mobile_get_request("rssi"); + char *response = mobile_get_rssi(); int rv; rv = parse_and_print(response, ctx, req); diff --git a/libmobile.c b/libmobile.c index ef00cd79727809a484a10c2d75acd7cea4004f48..e4fd1103564ae761a1104cccfa1c6aa6dcd178d4 100644 --- a/libmobile.c +++ b/libmobile.c @@ -5,40 +5,6 @@ struct string { size_t len; }; -int parse_and_print(char *dongle_response, struct ubus_context *ctx, struct ubus_request_data *req) -{ - if (!dongle_response) { - printf("no respose!\n"); - goto fail; - } - struct json_object *parsed_response = json_tokener_parse(dongle_response); - - if (!parsed_response) { - printf("No valid JSON, failed parsing!\n"); - goto free_response; - } - write_to_ubus(parsed_response, ctx, req); - - json_object_put(parsed_response); -free_response: - free(dongle_response); -fail: - return 0; -} - -int write_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req) -{ - struct blob_buf bb; - - memset(&bb, 0, sizeof(struct blob_buf)); - 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; -} - void curl_cleaner(CURLcode *curl) { curl_easy_cleanup(curl); @@ -62,41 +28,6 @@ size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp) return size * nmemb; } -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++) - bb = json_to_blob(json_object_array_get_idx(val, i), bb); - blobmsg_close_array(&bb, arr); - break; - case json_type_object: - obj = blobmsg_open_table(&bb, key); - bb = json_to_blob(val, bb); - blobmsg_close_array(&bb, obj); - break; - } - } - - return bb; -} - int apn_profile_idx(struct json_object *apn_profiles, char *name) { int idx = 0; diff --git a/libmobile.h b/libmobile.h index b2bddb06e6dba92521d8f03f0d448bf73d6208be..1f0db3474c5503ad876e77e02bf9a7fda05af560 100644 --- a/libmobile.h +++ b/libmobile.h @@ -3,8 +3,6 @@ #include <curl/curl.h> #include <json-c/json.h> #include <string.h> -#include <libubox/blobmsg.h> -#include <libubus.h> /*************************************************** * Libmobile - A 4G Dongle library @@ -43,24 +41,6 @@ ***************************************************/ - -int parse_and_print(char *dongle_response, struct ubus_context *ctx, struct ubus_request_data *req); - /** - * Function: write_to_ubus - * - * Prints a json_object pointer's json structure to ubus. - * - * Parameters: - * parsed_response - A struct json_object pointer to json structure to be printed to ubus. - * ctx - Ubus context containing connection. - * req - Information for from the ubus request. - * - * Returns: - * 0 On success. - * -1 On failure. - */ - int write_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req); - /** * Function: curl_cleaner * @@ -87,20 +67,6 @@ void curl_cleaner(CURLcode *curl); */ size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp); -/** - * Function: json_to_blob - * - * Parses a json_object pointer to a corresponding blob buffer. - * - * Parameters: - * response - json_object pointer to be replicated in a blob buffer. - * bb - The blob buffer to hold the results. - * - * Returns: - * Blob buffer containing the replicated json_object. - */ -struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb); - /** * Function: apn_profile_idx *