Skip to content
Snippets Groups Projects
Commit c5bbbec6 authored by Jakob Olsson's avatar Jakob Olsson
Browse files

partial documentation, restructure delete_apn to more logical approach, fix minor memory leak

parent ece696c9
Branches
No related tags found
No related merge requests found
...@@ -40,27 +40,17 @@ int delete_apn_profile(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -40,27 +40,17 @@ int delete_apn_profile(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct ubus_request_data *req, const char *method,
struct blob_attr *msg) struct blob_attr *msg)
{ {
char *response = mobile_get_apn_profiles();
struct json_object *apn_profiles = json_tokener_parse(response);
struct blob_attr *tb[__APN_MAX]; struct blob_attr *tb[__APN_MAX];
char name[1024] = {0}; //what is max available name length in dongle? char name[1024] = {0}; //what is max available name length in dongle?
int idx;
blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg)); blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg));
strncpy(name, (char *)blobmsg_data(tb[APN]), 1023); strncpy(name, (char *)blobmsg_data(tb[APN]), 1023);
printf("name to remove: %s\n", name);
idx = apn_profile_idx(apn_profiles, name);
if (idx >= 0) {
char *response = mobile_delete_apn(idx);
struct json_object *parsed_response = json_tokener_parse(response);
write_to_ubus(parsed_response, ctx, req); char *response = mobile_delete_apn(name);
json_object_put(apn_profiles); struct json_object *parsed_response = json_tokener_parse(response);
free(response);
}
write_to_ubus(parsed_response, ctx, req);
free(response);
return 0; return 0;
} }
...@@ -71,15 +61,14 @@ int set_apn_profile(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -71,15 +61,14 @@ int set_apn_profile(struct ubus_context *ctx, struct ubus_object *obj,
char *response = mobile_get_apn_profiles(); char *response = mobile_get_apn_profiles();
struct json_object *apn_profiles = json_tokener_parse(response); struct json_object *apn_profiles = json_tokener_parse(response);
struct blob_attr *tb[__APN_MAX]; struct blob_attr *tb[__APN_MAX];
char apn[1024] = {0}; //STR_MAX or something from limits.h char name[1024] = {0}; //STR_MAX or something from limits.h
int idx; int idx;
blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg)); blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg));
strncpy(name, (char *)blobmsg_data(tb[name]), 1023);
printf("name %s\n", name);
strncpy(apn, (char *)blobmsg_data(tb[APN]), 1023); idx = apn_profile_idx(apn_profiles, name);
printf("apn %s\n", apn);
idx = apn_profile_idx(apn_profiles, apn);
if (idx < 0) if (idx < 0)
goto fail; goto fail;
...@@ -99,13 +88,12 @@ int create_apn_profile(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -99,13 +88,12 @@ int create_apn_profile(struct ubus_context *ctx, struct ubus_object *obj,
struct blob_attr *msg) struct blob_attr *msg)
{ {
struct blob_attr *tb[__APN_MAX]; struct blob_attr *tb[__APN_MAX];
char apn[1024]; //STR_MAX or something from limits.h char name[1024] = {0}; //STR_MAX or something from limits.h
blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg)); blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg));
apn[0] = '\0'; strncpy(name, (char *)blobmsg_data(tb[name]), 1023);
strncpy(apn, (char *)blobmsg_data(tb[APN]), 1023); char *response = mobile_create_apn_profile(name);
char *response = mobile_create_apn_profile(apn);
struct json_object *parsed_response = json_tokener_parse(response); struct json_object *parsed_response = json_tokener_parse(response);
write_to_ubus(parsed_response, ctx, req); write_to_ubus(parsed_response, ctx, req);
......
...@@ -101,12 +101,16 @@ int get_apn_profiles_len(void) ...@@ -101,12 +101,16 @@ int get_apn_profiles_len(void)
json_object_object_foreach(apn_profiles, key, val) { json_object_object_foreach(apn_profiles, key, val) {
if (strlen(json_object_get_string(val)) > 0) if (strlen(json_object_get_string(val)) > 0)
len++; len++;
else else {
return len; goto finished;
}
} }
json_object_put(apn_profiles); json_object_put(apn_profiles);
return -1; return -1;
finished:
json_object_put(apn_profiles);
return len;
} }
char *mobile_connect_network(void) char *mobile_connect_network(void)
...@@ -125,15 +129,26 @@ char *mobile_disconnect_network(void) ...@@ -125,15 +129,26 @@ char *mobile_disconnect_network(void)
return mobile_post_request(query); return mobile_post_request(query);
} }
char *mobile_delete_apn(int idx) 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 query[1024] = {0};
int idx = apn_profile_idx(apn_profiles_json, name);
free(apn_profiles);
if (idx < 0)
goto fail;
strncpy(query, "isTest=false&apn_action=delete&apn_mode=manual&index=", 1023); strncpy(query, "isTest=false&apn_action=delete&apn_mode=manual&index=", 1023);
sprintf(query + strlen(query), "%d", idx); sprintf(query + strlen(query), "%d", idx);
strncat(query + strlen(query), "&goformId=APN_PROC_EX", 1023); strncat(query + strlen(query), "&goformId=APN_PROC_EX", 1023);
json_object_put(apn_profiles_json);
return mobile_post_request(query); return mobile_post_request(query);
fail:
json_object_put(apn_profiles_json);
return NULL;
} }
char *mobile_enable_roaming(void) char *mobile_enable_roaming(void)
......
...@@ -6,16 +6,137 @@ ...@@ -6,16 +6,137 @@
#include <libubox/blobmsg.h> #include <libubox/blobmsg.h>
#include <libubus.h> #include <libubus.h>
/**
* Function: write_to_ubus
*
* Prints a json_object pointer's json structure to ubus.
*
* Parameters:
* parsed_response - A struct json_object pointer to json structure to be printed to ubus.
* ctx - Ubus context containing connection.
* req - Information for from the ubus request.
*
* Returns:
* 0 On success.
* -1 On failure.
*/
int write_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req); int write_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req);
/**
* Function: curl_cleaner
*
* Free's the curl environment.
*
* Parameters:
* curl - The curl easy handle variable.
*/
void curl_cleaner(CURLcode *curl); 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); size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp);
/**
* Function: json_to_blob
*
* Parses a json_object pointer to a corresponding blob buffer.
*
* Parameters:
* response - json_object pointer to be replicated in a blob buffer.
* bb - The blob buffer to hold the results.
*
* Returns:
* Blob buffer containing the replicated json_object.
*/
struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb); struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb);
/**
* 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 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); int get_apn_profiles_len(void);
/**
* Function: mobile_connect_network
*
* Connects the dongle's network.
*
* Returns:
* A string containing the servers response (a json string containing {"result": "success"/"failure"}), indicating whether the call was successful or not.
* NULL on failure.
*/
char *mobile_connect_network(void); char *mobile_connect_network(void);
/**
* Function: mobile_disconnect_network
*
* Disconnects the dongle's network.
*
* Returns:
* A string containing the servers response (a json string containing {"result": "success"/"failure"}), indicating whether the call was successful or not.
* NULL on failure.
*/
char *mobile_disconnect_network(void); char *mobile_disconnect_network(void);
char *mobile_delete_apn(int idx);
/**
* Function: mobile_delete_apn
*
* Deletes an existing APN profile from the APN list.
*
* Parameters:
* name - The name of the APN profile to be removed.
*
* Returns:
* A string containing the servers response (a json string containing {"result": "success"/"failure"}), indicating whether the call was successful or not.
* NULL on failure.
*/
char *mobile_delete_apn(char *name);
/**
* Function: mobile_enable_roaming
*
* Enables the roaming option on the dongle.
*
* Parameters:
* name - The name of the APN profile to be removed.
*
* Returns:
* A string containing the servers response (a json string containing {"result": "success"/"failure"}), indicating whether the call was successful or not.
* NULL on failure.
*/
char *mobile_enable_roaming(void); char *mobile_enable_roaming(void);
char *mobile_disable_roaming(void); char *mobile_disable_roaming(void);
char *mobile_get_roam_status(void); char *mobile_get_roam_status(void);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment