diff --git a/dongle_apn.c b/dongle_apn.c index 70027cbf9d4810ee4d144896b93b70f62228f675..37bd07019e597acc9adbffdb220e2d3746ea7126 100644 --- a/dongle_apn.c +++ b/dongle_apn.c @@ -40,27 +40,17 @@ int delete_apn_profile(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(); - struct json_object *apn_profiles = json_tokener_parse(response); struct blob_attr *tb[__APN_MAX]; char name[1024] = {0}; //what is max available name length in dongle? - int idx; blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg)); strncpy(name, (char *)blobmsg_data(tb[APN]), 1023); - printf("name to remove: %s\n", name); - - idx = apn_profile_idx(apn_profiles, name); - - if (idx >= 0) { - char *response = mobile_delete_apn(idx); - struct json_object *parsed_response = json_tokener_parse(response); - write_to_ubus(parsed_response, ctx, req); - json_object_put(apn_profiles); - free(response); - } + char *response = mobile_delete_apn(name); + struct json_object *parsed_response = json_tokener_parse(response); + write_to_ubus(parsed_response, ctx, req); + free(response); return 0; } @@ -71,15 +61,14 @@ int set_apn_profile(struct ubus_context *ctx, struct ubus_object *obj, char *response = mobile_get_apn_profiles(); struct json_object *apn_profiles = json_tokener_parse(response); struct blob_attr *tb[__APN_MAX]; - char apn[1024] = {0}; //STR_MAX or something from limits.h + char name[1024] = {0}; //STR_MAX or something from limits.h int idx; blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg)); + strncpy(name, (char *)blobmsg_data(tb[name]), 1023); + printf("name %s\n", name); - strncpy(apn, (char *)blobmsg_data(tb[APN]), 1023); - printf("apn %s\n", apn); - - idx = apn_profile_idx(apn_profiles, apn); + idx = apn_profile_idx(apn_profiles, name); if (idx < 0) goto fail; @@ -99,13 +88,12 @@ int create_apn_profile(struct ubus_context *ctx, struct ubus_object *obj, struct blob_attr *msg) { struct blob_attr *tb[__APN_MAX]; - char apn[1024]; //STR_MAX or something from limits.h + char name[1024] = {0}; //STR_MAX or something from limits.h blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg)); - apn[0] = '\0'; - strncpy(apn, (char *)blobmsg_data(tb[APN]), 1023); - char *response = mobile_create_apn_profile(apn); + strncpy(name, (char *)blobmsg_data(tb[name]), 1023); + char *response = mobile_create_apn_profile(name); struct json_object *parsed_response = json_tokener_parse(response); write_to_ubus(parsed_response, ctx, req); diff --git a/libmobile.c b/libmobile.c index 5a771534be195cafe81604be88722852ca78a063..7e52a0c9939e5e6b8ebcc9693ca01bef5c922821 100644 --- a/libmobile.c +++ b/libmobile.c @@ -101,12 +101,16 @@ int get_apn_profiles_len(void) json_object_object_foreach(apn_profiles, key, val) { if (strlen(json_object_get_string(val)) > 0) len++; - else - return len; + else { + goto finished; + } } json_object_put(apn_profiles); return -1; +finished: + json_object_put(apn_profiles); + return len; } char *mobile_connect_network(void) @@ -125,15 +129,26 @@ char *mobile_disconnect_network(void) return mobile_post_request(query); } -char *mobile_delete_apn(int idx) +char *mobile_delete_apn(char *name) { + char *apn_profiles = mobile_get_apn_profiles(); + struct json_object *apn_profiles_json = json_tokener_parse(apn_profiles); char query[1024] = {0}; + int idx = apn_profile_idx(apn_profiles_json, name); + + free(apn_profiles); + if (idx < 0) + goto fail; strncpy(query, "isTest=false&apn_action=delete&apn_mode=manual&index=", 1023); sprintf(query + strlen(query), "%d", idx); strncat(query + strlen(query), "&goformId=APN_PROC_EX", 1023); + json_object_put(apn_profiles_json); return mobile_post_request(query); +fail: + json_object_put(apn_profiles_json); + return NULL; } char *mobile_enable_roaming(void) diff --git a/libmobile.h b/libmobile.h index 5fab753b8eb90f21762acc9d6fee03b8bbeb0334..8979ef32b4638131097f4bcba2772b11782a6035 100644 --- a/libmobile.h +++ b/libmobile.h @@ -6,16 +6,137 @@ #include <libubox/blobmsg.h> #include <libubus.h> + +/** + * 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 + * + * Free's the curl environment. + * + * Parameters: + * curl - The curl easy handle variable. + */ void curl_cleaner(CURLcode *curl); + +/** + * Function: write_func + * + * The callback function from performing the curl command, response from server will be parsed here. + * + * Parameters: + * buffer - Contains chunk of response from server. + * size - Size (byte) of type in buffer array. + * nmemb - Number of members in buffer. + * userp - Pointer to area allocated for storing results from server. + * + * Returns: + * Number of bytes managed (size*nmemb). + */ 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 + * + * Finds the index of a given profile name from all available profiles. + * + * Parameters: + * apn_profiles - json_object pointer to apn_profiles (gotten from a previous call to <mobile_get_apn_profiles>) + * name - Name of the APN index for which will be searched. + * + * Returns: + * Index of the APN on success. + * -1 on failure. + */ + int apn_profile_idx(struct json_object *apn_profiles, char *name); + +/** + * Function: get_apn_profiles_len + * + * Finds the number of APN profiles available. + * + * Returns: + * Number of APN profiles available on success. + * -1 on failure. + */ int get_apn_profiles_len(void); +/** + * Function: mobile_connect_network + * + * Connects the dongle's network. + * + * Returns: + * A string containing the servers response (a json string containing {"result": "success"/"failure"}), indicating whether the call was successful or not. + * NULL on failure. + */ char *mobile_connect_network(void); +/** + * Function: mobile_disconnect_network + * + * Disconnects the dongle's network. + * + * Returns: + * A string containing the servers response (a json string containing {"result": "success"/"failure"}), indicating whether the call was successful or not. + * NULL on failure. + */ char *mobile_disconnect_network(void); -char *mobile_delete_apn(int idx); + +/** + * Function: mobile_delete_apn + * + * Deletes an existing APN profile from the APN list. + * + * Parameters: + * name - The name of the APN profile to be removed. + * + * Returns: + * A string containing the servers response (a json string containing {"result": "success"/"failure"}), indicating whether the call was successful or not. + * NULL on failure. + */ +char *mobile_delete_apn(char *name); + +/** + * Function: mobile_enable_roaming + * + * Enables the roaming option on the dongle. + * + * Parameters: + * name - The name of the APN profile to be removed. + * + * Returns: + * A string containing the servers response (a json string containing {"result": "success"/"failure"}), indicating whether the call was successful or not. + * NULL on failure. + */ char *mobile_enable_roaming(void); char *mobile_disable_roaming(void); char *mobile_get_roam_status(void);