From 948c7b714137842d3e48a8ce2b6c8f30761ccb85 Mon Sep 17 00:00:00 2001
From: Jakob Olsson <jakobols@kth.se>
Date: Wed, 16 May 2018 10:30:12 +0200
Subject: [PATCH] fix to compile and other minor stuff
---
libmobile.c | 113 ++++++++++++++++++----------------------------------
1 file changed, 39 insertions(+), 74 deletions(-)
diff --git a/libmobile.c b/libmobile.c
index e856f1e..252e338 100644
--- a/libmobile.c
+++ b/libmobile.c
@@ -23,11 +23,10 @@ static int apn_profile_idx(struct json_object *apn_profiles, char *name);
static int get_apn_profiles_len(void);
static char *lexer(char **input, char *delimiter);
static char *get_query_wrapper(char *vars);
-static CURLcode *perform_post_request(curl, query, str);
-static CURLcode *perform_post_request(curl, query, str);
-static struct json_object *perform_post_request(char *query);
-static struct json_object *perform_get_request(char *vars);
-static struct json_object *parse_apn_profiles(char *apn_profiles);
+static CURLcode perform_post_request(CURL *curl, char *query, struct string *str);
+static CURLcode perform_get_request(CURL *curl, char *query, struct string *str);
+static struct json_object *prepare_request(char *query, int option);
+static struct json_object *parse_apn_profiles(struct json_object *apn_profiles);
/**
* Function: curl_cleaner
@@ -120,36 +119,28 @@ static int apn_profile_idx(struct json_object *apn_profiles, char *name)
*/
static int get_apn_profiles_len(void)
{
- char *response = mobile_get_apn_profiles();
+ struct json_object *apn_profiles = mobile_get_apn_profiles();
- if (!response) {
- printf("no response!\n");
+ if (!apn_profiles) {
+ printf("Failed to get apn profiles from server!\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(parsed_response, key, val) {
- if (strlen(json_object_get_string(val)) > 0)
+ json_object_object_foreach(apn_profiles, key, val) {
+ int val_type = json_object_get_type(val);
+
+ if (val_type != json_type_string)
len++;
else
goto success;
}
- json_object_put(parsed_response);
-free_response:
- free(response);
-fail:
- return -1;
success:
- json_object_put(parsed_response);
- free(response);
+ json_object_put(apn_profiles);
return len;
+fail:
+ return -1;
}
/**
@@ -233,7 +224,7 @@ static char *get_query_wrapper(char *vars)
* Returns:
* The CURLcode indicating the success or failure of the curl execution.
*/
-static CURLcode *perform_post_request(curl, query, str)
+static CURLcode perform_post_request(CURL *curl, char *query, struct string *str)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.1/goform/goform_set_cmd_process");
curl_easy_setopt(curl, CURLOPT_REFERER, "http://192.168.0.1/index.html");
@@ -257,7 +248,7 @@ static CURLcode *perform_post_request(curl, query, str)
* Returns:
* The CURLcode indicating the success or failure of the curl execution.
*/
-static CURLcode *perform_get_request(CURL *curl, char *query, struct string *str)
+static CURLcode perform_get_request(CURL *curl, char *query, struct string *str)
{
curl_easy_setopt(curl, CURLOPT_URL, query);
curl_easy_setopt(curl, CURLOPT_REFERER, "http://192.168.0.1/index.html");
@@ -310,7 +301,7 @@ static struct json_object *prepare_request(char *query, int option)
struct json_object *parsed_response = json_tokener_parse(str.ptr);
if (!parsed_response) {
- DEBUG("No valid JSON, failed parsing!\n");
+ printf("No valid JSON, failed parsing!\n");
goto fail_json;
}
@@ -335,21 +326,20 @@ fail:
* and transforms them into a more easily read and worked with JSON format.
*
* Parameters:
- * apn_profiles - A character string containing the APN profiles.
+ * apn_profiles - A json_object pointer containing the original apn configuration returned by the dongle.
*
* Returns:
- * The newly generated JSON on success.
+ * A json_object pointer to apn configurations in a more representative and easier to use format.
* NULL on failure.
*/
-static struct json_object *parse_apn_profiles(json_object *apn_profiles)
+static struct json_object *parse_apn_profiles(struct json_object *apn_profiles)
{
struct json_object *parsed_profiles = json_object_new_object();
int apn_counter = 0;
char *field_names[13] = {"profile_name", "apn_name", "mode", "wan_dial", "ppp_auth_mode", "ppp_username", "ppp_passwd", "pdp", "pdpd_select", "pdp_addr", "dns_mode", "prefer_dns_manual", "standby_dns_manual"};
- int rv;
- json_object_object_foreach(apn_profiles_json, key, val) {
+ json_object_object_foreach(apn_profiles, key, val) {
char *apn_string = json_object_get_string(val);
if (strlen(apn_string) <= 0)
@@ -371,15 +361,9 @@ static struct json_object *parse_apn_profiles(json_object *apn_profiles)
json_object_put(apn_profile);
apn_counter++;
}
- goto finished;
-free_objects:
- json_object_put(apn_profiles_json);
- json_object_put(parsed_profiles);
-fail:
- return NULL;
finished:
- json_object_put(apn_profiles_json);
+ json_object_put(apn_profiles);
return parsed_profiles;
}
@@ -396,37 +380,28 @@ struct json_object *mobile_disconnect_network(void)
struct json_object *mobile_delete_apn(char *name)
{
- char *response = mobile_get_apn_profiles();
+ struct json_object *apn_profiles = mobile_get_apn_profiles();
- if (!response) {
+ if (!apn_profiles) {
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);
+ int idx = apn_profile_idx(apn_profiles, name);
if (idx < 0) {
printf("APN not found in list!\n");
- goto free_all;
+ goto fail_idx;
}
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(parsed_response);
- free(response);
+ json_object_put(apn_profiles);
return prepare_request(query, POST);
-free_all:
- json_object_put(parsed_response);
-free_response:
- free(response);
+fail_idx:
+ json_object_put(apn_profiles);
fail:
return NULL;
}
@@ -473,13 +448,13 @@ struct json_object *mobile_get_modem_state(void)
struct json_object *mobile_get_apn_profiles(void)
{
- struct json_object *parsed_response = prepare_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", GET);
+ struct json_object *apn_profiles = prepare_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", GET);
- if (!parsed_response) {
+ if (!apn_profiles) {
printf("Error getting profiles!\n");
goto fail;
}
- return parse_apn_profiles(parsed_response);
+ return parse_apn_profiles(apn_profiles);
fail:
return NULL;
}
@@ -501,37 +476,27 @@ struct json_object *mobile_create_apn_profile(char *name)
struct json_object *mobile_set_apn_profile(char *name)
{
- char *response = mobile_get_apn_profiles();
+ struct json_object *apn_profiles = mobile_get_apn_profiles();
- if (!response) {
+ if (!apn_profiles) {
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);
+ int idx = apn_profile_idx(apn_profiles, name);
if (idx < 0) {
printf("couldnt find idx, no such profile!\n");
- goto free_all;
+ goto free_idx;
}
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);
+ json_object_put(apn_profiles);
return prepare_request(query, POST);
-
-free_all:
- json_object_put(parsed_response);
-free_response:
- free(response);
+free_idx:
+ json_object_put(apn_profiles);
fail:
return NULL;
}
--
GitLab