diff --git a/libmobile.c b/libmobile.c index 5c0df27cbbd7485645762c136f1f5c3e7c254db0..589b3fc8a8ecc9ec62e66bd33b9e497f953e27cd 100644 --- a/libmobile.c +++ b/libmobile.c @@ -3,10 +3,8 @@ * 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 + * 4. Change way to seperate profile configurations? (i.e. currently 0: .., 1: .., n: ...) */ - - #include "libmobile.h" struct string { @@ -14,11 +12,12 @@ 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); -char *lexer(char **input, char *delimiter); +static void curl_cleaner(CURLcode *curl); +static size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp); +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); /** * Function: curl_cleaner @@ -28,7 +27,7 @@ char *lexer(char **input, char *delimiter); * Parameters: * curl - The curl easy handle variable. */ -void curl_cleaner(CURLcode *curl) +static void curl_cleaner(CURLcode *curl) { curl_easy_cleanup(curl); curl_global_cleanup(); @@ -48,7 +47,7 @@ void curl_cleaner(CURLcode *curl) * Returns: * Number of bytes managed (size*nmemb). */ -size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp) +static size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp) { struct string *str = (struct string *)userp; size_t new_len = str->len + (size * nmemb); @@ -78,7 +77,7 @@ size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp) * Index of the APN on success. * -1 on failure. */ -int apn_profile_idx(struct json_object *apn_profiles, char *name) +static int apn_profile_idx(struct json_object *apn_profiles, char *name) { int idx = 0; json_object_object_foreach(apn_profiles, key, val) { @@ -105,7 +104,7 @@ int apn_profile_idx(struct json_object *apn_profiles, char *name) * Number of APN profiles available on success. * -1 on failure. */ -int get_apn_profiles_len(void) +static int get_apn_profiles_len(void) { char *response = mobile_get_apn_profiles(); @@ -157,7 +156,7 @@ success: * Will alter the input string and allocate memory for the return value! * The caller is responsible for freeing the return value. */ -char *lexer(char **input, char *delimiter) { +static char *lexer(char **input, char *delimiter) { if(strlen(*input) == 0) { printf("empty input!\n"); return NULL; @@ -440,17 +439,43 @@ fail: return NULL; } -char *mobile_get_request(char *vars) +/** + * Function: get_query_wrapper + * Wraps the input comma-separated values to the appropriate address format for a GET request. + * + * Parameters: + * vars - Char pointer pointing to comma-separated values to retreive. + * + * Returns: + * The entire query on success. + * NULL on failure. + */ +char *get_query_wrapper(char *vars) { - CURL *curl; - CURLcode res; - struct string str; + if (strlen(vars) == 0) { + printf("No GET input provided!\n"); + return NULL; + } + char query[1024] = {0}; strncpy(query, "http://192.168.0.1/goform/goform_get_cmd_process?isTest=false&cmd=", 1023); strncat(query + strlen(query), vars, 1023); strncat(query + strlen(query), "&multi_data=1", 1023); + return strdup(query); +} + +char *mobile_get_request(char *vars) +{ + CURL *curl; + CURLcode res; + struct string str; + char *query = get_query_wrapper(vars); + + if (!query) + goto fail; + str.ptr = calloc(1, 1); str.len = 0; curl = curl_easy_init(); @@ -466,6 +491,7 @@ char *mobile_get_request(char *vars) } } + free(query); curl_cleaner(curl); return str.ptr; fail: