diff --git a/libmobile.c b/libmobile.c index 67f250a2e62cb52030061f6cf034033c9a3153d1..85aa72067c07cfc84d1698c04c59a4b7e7293506 100644 --- a/libmobile.c +++ b/libmobile.c @@ -5,12 +5,39 @@ struct string { size_t len; }; +void curl_cleaner(CURLcode *curl); +size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp); +int apn_profile_idx(struct json_object *apn_profiles, char *name); +int get_apn_profiles_len(void); + +/** + * Function: curl_cleaner + * + * Free's the curl environment. + * + * Parameters: + * curl - The curl easy handle variable. + */ void curl_cleaner(CURLcode *curl) { curl_easy_cleanup(curl); curl_global_cleanup(); } +/** + * 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) { struct string *str = (struct string *)userp; @@ -28,13 +55,26 @@ size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp) return size * nmemb; } +/** + * 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) { int idx = 0; json_object_object_foreach(apn_profiles, key, val) { char *apn_profile = json_object_get_string(val); - if (strlen(apn_profile) <= 0) + if (!apn_profile || strlen(apn_profile) <= 0) break; char *apn_name = strtok(apn_profile, "($)"); @@ -46,23 +86,47 @@ int apn_profile_idx(struct json_object *apn_profiles, char *name) return -1; } +/** + * 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) { char *response = mobile_get_apn_profiles(); - struct json_object *apn_profiles = json_tokener_parse(response); + + if (!response) { + printf("no response!\n"); + goto fail; + } + struct json_object *parsed_response = json_tokener_parse(response); + + if(!parsed_response) { + printf("no valid json to parse!"); + goto free_response; + } int len = 0; - json_object_object_foreach(apn_profiles, key, val) { + json_object_object_foreach(parsed_response, key, val) { if (strlen(json_object_get_string(val)) > 0) len++; else - goto finished; + goto success; } - json_object_put(apn_profiles); + json_object_put(parsed_response); +free_response: + free(response); +fail: return -1; -finished: - json_object_put(apn_profiles); + +success: + json_object_put(parsed_response); + free(response); return len; } @@ -84,26 +148,38 @@ char *mobile_disconnect_network(void) 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}; + char *response = mobile_get_apn_profiles(); - int idx = apn_profile_idx(apn_profiles_json, name); + if (!response) { + printf("no response!\n"); + goto fail; + } + struct json_object *parsed_response = json_tokener_parse(response); + + if(!parsed_response) { + printf("no valid json to parse!"); + goto free_response; + } + char query[1024] = {0}; + int idx = apn_profile_idx(parsed_response, name); - free(apn_profiles); if (idx < 0) { printf("APN not found in list!\n"); - goto fail; + goto free_all; } - 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); + json_object_put(parsed_response); + free(response); return mobile_post_request(query); + +free_all: + json_object_put(parsed_response); +free_response: + free(response); fail: - json_object_put(apn_profiles_json); return NULL; } @@ -168,15 +244,39 @@ char *mobile_create_apn_profile(char *name) char *mobile_set_apn_profile(char *name) { - char *apn_profiles = mobile_get_apn_profiles(); - struct json_object *apn_profiles_json = json_tokener_parse(apn_profiles); - int idx = apn_profile_idx(apn_profiles_json, name); + char *response = mobile_get_apn_profiles(); + + if (!response) { + printf("no response!\n"); + goto fail; + } + struct json_object *parsed_response = json_tokener_parse(response); + + if(!parsed_response) { + printf("no valid json to parse!"); + goto free_response; + } + int idx = apn_profile_idx(parsed_response, name); + + if (idx < 0) { + printf("couldnt find idx, no such profile!\n"); + goto free_all; + } char query[1024] = {0}; strncpy(query, "isTest=false&goformId=APN_PROC_EX&apn_mode=manual&apn_action=set_default&set_default_flag=1&pdp_type=IP&index=", 1023); sprintf(query + strlen(query), "%d", idx); + json_object_put(parsed_response); + free(response); return mobile_post_request(query); + +free_all: + json_object_put(parsed_response); +free_response: + free(response); +fail: + return NULL; } diff --git a/libmobile.h b/libmobile.h index a87f4160f308c9934c43f1df24d50bc5397d02d3..2c6531f2b77d4c3700b46aa50e80867869d67fe8 100644 --- a/libmobile.h +++ b/libmobile.h @@ -41,58 +41,6 @@ * ***************************************************/ -/** - * 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: 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 *