diff --git a/libmobile.c b/libmobile.c index b46ef151138f6f82a7fcab00e7022c2c51d00514..d131a3d004473eadc8b4d616cf31bbcd8f56a8d5 100644 --- a/libmobile.c +++ b/libmobile.c @@ -18,6 +18,7 @@ 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); +struct json_object *parse_apn_profiles(char *apn_profiles); /** * Function: curl_cleaner @@ -138,6 +139,18 @@ success: return len; } +/** + * Function: parse_apn_profiles + * + * Takes a string of APN profile configurations provided by zte-mf823 (which is in an awkward, difficult to read format) + * and transforms them into a more easily read and worked with JSON format. + * + * Parameters: + * apn_profiles - A character string containing the APN profiles. + * Returns: + * The newly generated JSON on success. + * NULL on failure. + */ struct json_object *parse_apn_profiles(char *apn_profiles) { if (strlen(apn_profiles) <= 0) { @@ -151,43 +164,57 @@ struct json_object *parse_apn_profiles(char *apn_profiles) goto fail; } struct json_object *parsed_profiles = json_object_new_object(); + int apn_counter = 0; + char field_names[13] = {"profile_name", "apn_name", "mode", "dunno", "authentication", "username?", "password?", "IPdunno", "ddnunno?", "dno", "DNS_mode", "IPv_username", "IPv_password"}; + int rv; json_object_object_foreach(apn_profiles_json, key, val) { - char *apn = json_object_get_string(val); + char *apn_string = json_object_get_string(val); - if (strlen(apn) <= 0) + if (strlen(apn_string) <= 0) goto finished; + int i; + struct json_object *apn_profile = json_object_new_object(); - char fields[13] = {"profile_name", "apn_name", "mode", "dunno", "authentication", "username?", "password?", "IPdunno", "ddnunno?", "dno", "DNS_mode", "IPv_username", "IPv_password"}; - int field_counter = 0; - - + for (i = 0; i < 13; i++) { + char *field_val = strtok(apn_string, "($)"); + json_object_object_add(apn_profile, field_names[i], field_val); + } + rv = json_object_object_add(parsed_profiles, sprintf("%d", apn_counter), apn_profile); //be careful here, do we need json_object_get(apn_profile)? + json_object_put(apn_profile); + if (!rv) { + DEBUG("Error adding object!\n"); + goto free_objects; + } + apn_counter++; + //json_put what?? + printf("FREE THEM CORRECTLY HERE!\n"); + //best guess, free: apn_profile } + goto finished; -free_object: +free_objects: json_object_put(apn_profiles); json_object_put(parsed_profiles); fail: return NULL; finished: + printf("probably need to free apn_profiles_json!\n"); + //best guess, free: apn_profiles_json + json_object_put(apn_profiles); + json_object_put(parsed_profiles); return parsed_profiles; } char *mobile_connect_network(void) { - char query[1024] = {0}; - - strncpy(query, "isTest=false&goformId=CONNECT_NETWORK", 1023); - return mobile_post_request(query); + return mobile_post_request("isTest=false&goformId=CONNECT_NETWORK"); } char *mobile_disconnect_network(void) { - char query[1024] = {0}; - // removed notcallback, why have it? - strncpy(query, "isTest=false&goformId=DISCONNECT_NETWORK", 1023); - return mobile_post_request(query); + return mobile_post_request("isTest=false&goformId=DISCONNECT_NETWORK"); } char *mobile_delete_apn(char *name)