diff --git a/libmobile.c b/libmobile.c index 85aa72067c07cfc84d1698c04c59a4b7e7293506..d131a3d004473eadc8b4d616cf31bbcd8f56a8d5 100644 --- a/libmobile.c +++ b/libmobile.c @@ -1,3 +1,12 @@ +/** + * TODO: + * 1. Add parsing for displaying APN profiles available to make sense of them + * 2. Add more input options when creating APN profile + * 3. Seperate "set default" and "apply" APN profiles (what is the difference anyway?) + * 4. Make the APN parsed fields more dynamic + */ + + #include "libmobile.h" struct string { @@ -9,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 @@ -123,27 +133,88 @@ free_response: free(response); fail: return -1; - success: json_object_put(parsed_response); free(response); return len; } -char *mobile_connect_network(void) +/** + * 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) { - char query[1024] = {0}; + if (strlen(apn_profiles) <= 0) { + DEBUG("No APN profiles provided!\n"); + goto fail; + } + struct json_object *apn_profiles_json = json_tokener_parse(apn_profiles); - strncpy(query, "isTest=false&goformId=CONNECT_NETWORK", 1023); - return mobile_post_request(query); + if (!apn_profiles_json) { + DEBUG("Not valid json!\n"); + 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_string = json_object_get_string(val); + + if (strlen(apn_string) <= 0) + goto finished; + int i; + struct json_object *apn_profile = json_object_new_object(); + + 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_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) +{ + 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) @@ -222,6 +293,7 @@ char *mobile_get_modem_state(void) { return mobile_get_request("&sms_received_flag_flag=0&sts_received_flag_flag=0&cmd=modem_main_state%2Cpin_status%2Cloginfo%2Cnew_version_state%2Ccurrent_upgrade_state%2Cis_mandatory%2Csms_received_flag%2Csts_received_flag%2Csignalbar%2Cnetwork_type%2Cnetwork_provider%2Cppp_status%2CEX_SSID1%2Cex_wifi_status%2CEX_wifi_profile%2Cm_ssid_enable%2Csms_unread_num%2CRadioOff%2Csimcard_roam%2Clan_ipaddr%2Cstation_mac%2Cbattery_charging%2Cbattery_vol_percent%2Cbattery_pers%2Cspn_display_flag%2Cplmn_display_flag%2Cspn_name_data%2Cspn_b1_flag%2Cspn_b2_flag%2Crealtime_tx_bytes%2Crealtime_rx_bytes%2Crealtime_time%2Crealtime_tx_thrpt%2Crealtime_rx_thrpt%2Cmonthly_rx_bytes%2Cmonthly_tx_bytes%2Cmonthly_time%2Cdate_month%2Cdata_volume_limit_switch%2Cdata_volume_limit_size%2Cdata_volume_alert_percent%2Cdata_volume_limit_unit%2Croam_setting_option%2Cupg_roam_switch%2Chplmn"); } + char *mobile_get_apn_profiles(void) { return mobile_get_request("APN_config0,APN_config1,APN_config2,APN_config3,APN_config4,APN_config5,APN_config6,APN_config7,APN_config8,APN_config9,APN_config10,APN_config11,APN_config12,APN_config13,APN_config14,APN_config15,APN_config16,APN_config17,APN_config18,APN_config19"); @@ -279,7 +351,6 @@ fail: return NULL; } - char *mobile_enable_pin(char *pin) { char query[1024] = {0};