diff --git a/common.c b/common.c deleted file mode 100644 index e4fcc0cc45a96686f28ad477c290131360bb87ac..0000000000000000000000000000000000000000 --- a/common.c +++ /dev/null @@ -1,70 +0,0 @@ -#include "common.h" - -int parse_and_print(char *dongle_response, struct ubus_context *ctx, struct ubus_request_data *req) -{ - if (!dongle_response) { - DEBUG("no respose!\n"); - goto fail; - } - struct json_object *parsed_response = json_tokener_parse(dongle_response); - - if (!parsed_response) { - DEBUG("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 deleted file mode 100644 index b9f250c2b3b8b9318df31cf2a3149226d5f1f91f..0000000000000000000000000000000000000000 --- a/common.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef COMMON_H -#define COMMON_H -#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 deleted file mode 100644 index a0466fa86a36ac1d7c9e255653c29869339fda10..0000000000000000000000000000000000000000 --- a/dongle_apn.c +++ /dev/null @@ -1,127 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include <stdbool.h> -#include <unistd.h> -#include <getopt.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <signal.h> -#include <time.h> -#include <limits.h> - -#include <libubox/list.h> - -#include "libmobile.h" -#include "common.h" - -struct ubus_context *ctx; - -enum { - APN, - __APN_MAX, -}; - -const struct blobmsg_policy apn_policy[__APN_MAX] = { - [APN] = {.name = "apn", .type = BLOBMSG_TYPE_STRING}, -}; - -int list_apn_profiles(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - char *response = mobile_get_apn_profiles(); - - return parse_and_print(response, ctx, req); -} - -int delete_apn_profile(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - struct blob_attr *tb[__APN_MAX]; - char name[1024] = {0}; //what is max available name length in dongle? - - blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg)); - strncpy(name, (char *)blobmsg_data(tb[APN]), 1023); - char *response = mobile_delete_apn(name); - - return parse_and_print(response, ctx, req); -} - -int set_apn_profile(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - struct blob_attr *tb[__APN_MAX]; - char name[1024] = {0}; //STR_MAX or something from limits.h - - blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg)); - strncpy(name, (char *)blobmsg_data(tb[APN]), 1023); - - char *response = mobile_set_apn_profile(name); - - return parse_and_print(response, ctx, req); -} - -int create_apn_profile(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - struct blob_attr *tb[__APN_MAX]; - char name[1024] = {0}; //STR_MAX or something from limits.h - - blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg)); - - strncpy(name, (char *)blobmsg_data(tb[APN]), 1023); - char *response = mobile_create_apn_profile(name); - - return parse_and_print(response, ctx, req); -} - -int show_current_apn(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - //char *wan_apn = mobile_get_request("wan_apn"); - char *response = mobile_get_wan_apn(); - - return parse_and_print(response, ctx, req); -} - -struct ubus_method dongle_object_methods[] = { - UBUS_METHOD_NOARG("list", list_apn_profiles), - UBUS_METHOD("create", create_apn_profile, apn_policy), - UBUS_METHOD("delete", delete_apn_profile, apn_policy), - UBUS_METHOD("set_profile", set_apn_profile, apn_policy), - UBUS_METHOD_NOARG("current", show_current_apn), -}; - -struct ubus_object_type dongle_object_type = UBUS_OBJECT_TYPE("dongle", dongle_object_methods); - -struct ubus_object dongle_object = { - .name = "dongle.apn", - .type = &dongle_object_type, - .methods = dongle_object_methods, - .n_methods = ARRAY_SIZE(dongle_object_methods), -}; - -void init_ubus(void) -{ - ctx = ubus_connect(NULL); - if (!ctx) { - perror("ubus"); - exit(1); - } - ubus_add_uloop(ctx); -} - -int main(int argc, char **argv) -{ - uloop_init(); - init_ubus(); - ubus_add_object(ctx, &dongle_object); - uloop_run(); - - return 0; -} diff --git a/dongle_network.c b/dongle_network.c deleted file mode 100644 index aa9857c240113104ca4a57747d117e19b97a87bd..0000000000000000000000000000000000000000 --- a/dongle_network.c +++ /dev/null @@ -1,120 +0,0 @@ -#include <ctype.h> -#include <stdlib.h> -#include <stdio.h> -#include <stdbool.h> -#include <unistd.h> -#include <getopt.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <signal.h> -#include <time.h> -#include <limits.h> - -#include <libubox/list.h> - -#include "libmobile.h" -#include "common.h" - -struct ubus_context *ctx; - -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_rssi(); - - return parse_and_print(response, ctx, req); -} -int connect_network(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - char *response = mobile_connect_network(); - - return parse_and_print(response, ctx, req); -} - -int disconnect(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - char *response = mobile_disconnect_network(); - - return parse_and_print(response, ctx, req); -} - -int modem_state(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - char *response = mobile_get_modem_state(); - - return parse_and_print(response, ctx, req); -} - -int enable_roaming(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - char *response = mobile_enable_roaming(); - - return parse_and_print(response, ctx, req); -} - -int disable_roaming(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - char response = *mobile_disable_roaming(); - - return parse_and_print(response, ctx, req); -} - -int roam_status(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - char *response = mobile_get_roam_status(); - - return parse_and_print(response, ctx, req); -} - -struct ubus_method dongle_object_methods[] = { - UBUS_METHOD_NOARG("signal_strength", get_signal_strength), - UBUS_METHOD_NOARG("connect", connect_network), - UBUS_METHOD_NOARG("disconnect", disconnect), - UBUS_METHOD_NOARG("modem_state", modem_state), - UBUS_METHOD_NOARG("enable_roaming", enable_roaming), - UBUS_METHOD_NOARG("disable_roaming", disable_roaming), - UBUS_METHOD_NOARG("roam_status", roam_status), -}; - -struct ubus_object_type dongle_object_type = UBUS_OBJECT_TYPE("dongle", dongle_object_methods); - -struct ubus_object dongle_object = { - .name = "dongle.network", - .type = &dongle_object_type, - .methods = dongle_object_methods, - .n_methods = ARRAY_SIZE(dongle_object_methods), -}; - -void init_ubus(void) -{ - ctx = ubus_connect(NULL); - if (!ctx) { - perror("ubus"); - exit(1); - } - ubus_add_uloop(ctx); -} - -int main(int argc, char **argv) -{ - uloop_init(); - init_ubus(); - ubus_add_object(ctx, &dongle_object); - uloop_run(); - - return 0; -} diff --git a/dongle_pin.c b/dongle_pin.c deleted file mode 100644 index 7fea2f038cd5613e245031f75a7ec9df7d15d697..0000000000000000000000000000000000000000 --- a/dongle_pin.c +++ /dev/null @@ -1,365 +0,0 @@ -#include <ctype.h> -#include <stdlib.h> -#include <stdio.h> -#include <stdbool.h> -#include <unistd.h> -#include <getopt.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <signal.h> -#include <time.h> -#include <limits.h> - -#include <libubox/list.h> - -#include "libmobile.h" -#include "common.h" - -struct ubus_context *ctx; - -enum { - NEW_PIN, - CURRENT_PIN, - __SET_PIN_MAX, -}; - -enum { - PIN, - __PIN_MAX, -}; - -const struct blobmsg_policy set_pin_policy[__SET_PIN_MAX] = { - [NEW_PIN] = {.name = "new_pin", .type = BLOBMSG_TYPE_STRING}, - [CURRENT_PIN] = {.name = "current_pin", .type = BLOBMSG_TYPE_STRING}, -}; - -const struct blobmsg_policy pin_policy[__PIN_MAX] = { - [PIN] = {.name = "pin", .type = BLOBMSG_TYPE_STRING}, -}; - -int isdigits(const char *pin) -{ - while (*pin) { - if (isdigit(*pin++) == 0) - return false; - } - return true; -} - -int validate_pin_format(char *pin) -{ - if (!isdigits(pin)) { - DEBUG("Please enter digits only!\n"); - goto fail; - } else if (strlen(pin) > 8 || strlen(pin) < 4) { - DEBUG("Please enter between 4 to 8 digits!\n"); - goto fail; - } else if (atoi(pin) == 0) { - DEBUG("0000 is not a valid pin! Lowest available is 0001\n"); - goto fail; - } - return 0; -fail: - return -1; -} - -int set_pin(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - struct blob_attr *tb[__SET_PIN_MAX]; - char new_pin[10] = {0}, current_pin[10] = {0}; - int rv; - - blobmsg_parse(set_pin_policy, __SET_PIN_MAX, tb, blob_data(msg), blob_len(msg)); - - if (!tb[NEW_PIN] && !tb[CURRENT_PIN]) { - DEBUG("Please enter both a new pin and old pin!\n"); - goto fail; - } - - strncpy(new_pin, (char *)blobmsg_data(tb[NEW_PIN]), 9); - strncpy(current_pin, (char *)blobmsg_data(tb[CURRENT_PIN]), 9); - - rv = validate_pin_format(new_pin); - if (rv > 0) { - DEBUG("invalid pin\n"); - goto fail; - } - rv = validate_pin_format(current_pin); - if (rv > 0) { - DEBUG("invalid pin\n"); - goto fail; - } - char *response = mobile_get_pin_status(); - - if (!response) { - DEBUG("error getting pin_status\n"); - goto fail; - } - struct json_object *parsed_response = json_tokener_parse(response); - - if (!parsed_response) { - DEBUG("No valid JSON, failed parsing!\n"); - goto free_response; - } - struct json_object *rv_json; - - json_object_object_get_ex(parsed_response, "pin_status", &rv_json); - if (!rv_json) { - DEBUG("no pin_status available\n"); - goto free_all; - } - - free(response); - if (!json_object_get_int(rv_json)) { - json_object_put(parsed_response); - response = mobile_enable_pin(current_pin); - if (!response) { - DEBUG("error enabling pin!\n"); - goto fail; - } - parsed_response = json_tokener_parse(response); - json_object_object_get_ex(parsed_response, "result", &rv_json); - if (!rv_json) { - DEBUG("error getting result!\n"); - goto free_all; - } - if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0) - DEBUG("Incorrect pin!\n"); - - free(response); - } - - json_object_put(parsed_response); - response = mobile_set_pin(current_pin, new_pin); - if (!response) { - DEBUG("error setting pin!\n"); - goto fail; - } - parsed_response = json_tokener_parse(response); - json_object_object_get_ex(parsed_response, "result", &rv_json); - if (!rv_json) { - DEBUG("no result available from set_pin!"); - goto free_all; - } - write_to_ubus(parsed_response, ctx, req); - -free_all: - json_object_put(parsed_response); -free_response: - free(response); -fail: - return 0; -} - -int disable_pin(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - struct blob_attr *tb[__PIN_MAX]; - char pin[10] = {0}; - int rv; - - blobmsg_parse(pin_policy, __PIN_MAX, tb, blob_data(msg), blob_len(msg)); - if (!tb[PIN]) { - DEBUG("Please enter a pin!\n"); - goto fail; - } - strncpy(pin, (char *)blobmsg_data(tb[PIN]), 9); - rv = validate_pin_format(pin); - if (rv < 0) { - DEBUG("invalid pin format!\n"); - goto fail; - } - char *response = mobile_get_pin_status(); - - if (!response) { - DEBUG("no response from get_pin_status!\n"); - goto fail; - } - struct json_object *parsed_response = json_tokener_parse(response); - - if (!parsed_response) { - DEBUG("No valid JSON, failed parsing!\n"); - goto free_response; - } - struct json_object *rv_json; - - json_object_object_get_ex(parsed_response, "pin_status", &rv_json); - if (!rv_json) { - DEBUG("no pin_status available in response!\n"); - goto free_all; - } - if (json_object_get_int(rv_json)) { - - free(response); - json_object_put(parsed_response); - response = mobile_disable_pin(pin); - if (!response) { - DEBUG("eror disabling pin!\n"); - goto fail; - } - parsed_response = json_tokener_parse(response); - json_object_object_get_ex(parsed_response, "result", &rv_json); - if (!rv_json) { - DEBUG("no result available in response!\n"); - goto free_all; - } - if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0) - DEBUG("Incorrect pin!\n"); - } else { - DEBUG("already disabled!\n"); - } - - write_to_ubus(parsed_response, ctx, req); - -free_all: - json_object_put(parsed_response); -free_response: - free(response); -fail: - return 0; -} - -int enable_pin(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - struct blob_attr *tb[__PIN_MAX]; - char pin[10] = {0}; - int rv; - - blobmsg_parse(pin_policy, __PIN_MAX, tb, blob_data(msg), blob_len(msg)); - if (!tb[PIN]) { - DEBUG("Please enter both a new pin and old pin!\n"); - goto fail; - } - strncpy(pin, (char *)blobmsg_data(tb[PIN]), 9); - rv = validate_pin_format(pin); - if (rv < 0) { - DEBUG("invalid pin format!\n"); - goto fail; - } - char *response = mobile_get_pin_status(); - - if (!response) { - DEBUG("no response from get_pin_status!\n"); - goto fail; - } - struct json_object *parsed_response = json_tokener_parse(response); - - if (!parsed_response) { - DEBUG("No valid JSON, failed parsing!\n"); - goto free_response; - } - struct json_object *rv_json; - - json_object_object_get_ex(parsed_response, "pin_status", &rv_json); - if (!rv_json) { - DEBUG("no pin_status available in response!\n"); - goto free_all; - } - if (!json_object_get_int(rv_json)) { - - free(response); - json_object_put(parsed_response); - response = mobile_enable_pin(pin); - if (!response) { - DEBUG("no response from get_pin_status!\n"); - goto fail; - } - parsed_response = json_tokener_parse(response); - json_object_object_get_ex(parsed_response, "result", &rv_json); - if (!rv_json) { - DEBUG("no result available in response!\n"); - goto free_all; - } - if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0) - DEBUG("Incorrect pin!\n"); - } else { - DEBUG("already enabled!\n"); - } - - write_to_ubus(parsed_response, ctx, req); - -free_all: - json_object_put(parsed_response); -free_response: - free(response); -fail: - return 0; -} - -int verify_pin(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - struct blob_attr *tb[__PIN_MAX]; - char pin[10] = {0}; - int rv; - - blobmsg_parse(pin_policy, __PIN_MAX, tb, blob_data(msg), blob_len(msg)); - if (!tb[PIN]) { - DEBUG("Please enter a pin\n!"); - goto fail; - } - strncpy(pin, (char *)blobmsg_data(tb[PIN]), 9); - rv = validate_pin_format(pin); - if (rv < 0) { - DEBUG("invalid pin format!\n"); - goto fail; - } - char *response = mobile_set_pin(pin, pin); - - return parse_and_print(response, ctx, req); -fail: - return -1; -} - -int remaining_tries(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - char *response = mobile_get_pinnumber(); - - return parse_and_print(response, ctx, req); -} - -struct ubus_method dongle_object_methods[] = { - UBUS_METHOD("set_pin", set_pin, set_pin_policy), - UBUS_METHOD("disable_pin", disable_pin, pin_policy), - UBUS_METHOD("enable_pin", enable_pin, pin_policy), - UBUS_METHOD("verify_pin", verify_pin, pin_policy), - UBUS_METHOD_NOARG("remaining_tries", remaining_tries), -}; - -struct ubus_object_type dongle_object_type = UBUS_OBJECT_TYPE("dongle", dongle_object_methods); - -struct ubus_object dongle_object = { - .name = "dongle.pin", - .type = &dongle_object_type, - .methods = dongle_object_methods, - .n_methods = ARRAY_SIZE(dongle_object_methods), -}; - -void init_ubus(void) -{ - ctx = ubus_connect(NULL); - if (!ctx) { - perror("ubus"); - exit(1); - } - ubus_add_uloop(ctx); -} - -int main(int argc, char **argv) -{ - uloop_init(); - init_ubus(); - ubus_add_object(ctx, &dongle_object); - uloop_run(); - - return 0; -}