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

rearrange libmobile.c to remove a lot of duplicate code, small error fix

parent 2cd054b2
Branches
No related tags found
No related merge requests found
*.o
.*
dongle_apn
dongle_pin
dongle_network
ipkg*
......@@ -58,23 +58,17 @@ int delete_apn_profile(struct ubus_context *ctx, struct ubus_object *obj,
{
struct json_object *apn_profiles = get_apn_profiles_json();
struct blob_attr *tb[__APN_MAX];
char name[1024]; //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));
name[0] = '\0';
strncpy(name, (char *)blobmsg_data(tb[APN]), 1023);
printf("name to remove: %s\n", name);
idx = get_apn_profile_idx(apn_profiles, name);
if (idx >= 0) {
char response[1024];
int rv;
rv = _delete_apn(idx, response);
if (rv < 0)
return -1;
char *response = _delete_apn(idx);
struct json_object *parsed_response = json_tokener_parse(response);
struct blob_buf bb;
......@@ -157,7 +151,7 @@ int show_current_apn(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
char *wan_apn = get_current_wan_apn();
char *wan_apn = get_request("wan_apn");
struct json_object *parsed_response = json_tokener_parse(wan_apn);
struct blob_buf bb;
......
......@@ -24,16 +24,16 @@ int signal_strength(struct ubus_context *ctx, struct ubus_object *obj,
{
struct blob_buf bb;
char * response = get_csv("rssi");
char * response = get_request("rssi");
struct json_object *parsed_response = json_tokener_parse(response);
print:
memset(&bb, 0, sizeof(struct blob_buf));
blob_buf_init(&bb, 0);
bb = json_to_blob(parsed_response, bb);
ubus_send_reply(ctx, req, bb.head);
blob_buf_free(&bb);
json_object_put(parsed_response);
fail:
return 0;
}
......
......@@ -38,7 +38,7 @@ const struct blobmsg_policy pin_policy[__PIN_MAX] = {
[PIN] = {.name = "pin", .type = BLOBMSG_TYPE_STRING},
};
int print_response(struct json_object *parsed_response, ubus_context *ctx, ubus_request_data *req)
int print_response(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req)
{
struct blob_buf bb;
......@@ -81,7 +81,6 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_buf bb;
struct blob_attr *tb[__SET_PIN_MAX];
char new_pin[10] = {0}, current_pin[10] = {0};
......@@ -98,7 +97,7 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
validate_pin_format(current_pin);
printf("new: %s, old: %s\n", new_pin, current_pin);
char *response = get_csv("pin_status");
char *response = get_request("pin_status");
struct json_object *parsed_response = json_tokener_parse(response);
struct json_object *rv;
......@@ -144,7 +143,7 @@ int disable_pin(struct ubus_context *ctx, struct ubus_object *obj,
strncpy(pin, (char *)blobmsg_data(tb[PIN]), 9);
validate_pin_format(pin);
char *response = get_csv("pin_status");
char *response = get_request("pin_status");
struct json_object *parsed_response = json_tokener_parse(response);
struct json_object *rv;
......@@ -172,7 +171,6 @@ int enable_pin(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_buf bb;
struct blob_attr *tb[__PIN_MAX];
char pin[10] = {0};
......@@ -185,7 +183,7 @@ int enable_pin(struct ubus_context *ctx, struct ubus_object *obj,
strncpy(pin, (char *)blobmsg_data(tb[PIN]), 9);
validate_pin_format(pin);
char *response = get_csv("pin_status");
char *response = get_request("pin_status");
struct json_object *parsed_response = json_tokener_parse(response);
struct json_object *rv;
......@@ -213,7 +211,6 @@ int verify_pin(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_buf bb;
struct blob_attr *tb[__PIN_MAX];
char pin[10] = {0};
......@@ -225,7 +222,7 @@ int verify_pin(struct ubus_context *ctx, struct ubus_object *obj,
strncpy(pin, (char *)blobmsg_data(tb[PIN]), 9);
validate_pin_format(pin);
char *response = get_csv("pin_status");
char *response = get_request("pin_status");
struct json_object *parsed_response = json_tokener_parse(response);
struct json_object *rv;
......@@ -248,8 +245,7 @@ int remaining_tries(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_buf bb;
char *response = get_csv("pinnumber");
char *response = get_request("pinnumber");
struct json_object *parsed_response = json_tokener_parse(response);
print_response(parsed_response, ctx, req);
return 0;
......
......@@ -30,28 +30,10 @@ size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp)
struct json_object *get_apn_profiles_json(void)
{
CURL *curl;
CURLcode res;
struct string str;
str.ptr = calloc(1, 1);
str.len = 0;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.1/goform/goform_get_cmd_process?isTest=false&cmd=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&multi_data=1");
curl_easy_setopt(curl, CURLOPT_REFERER, "http://192.168.0.1/index.html");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str);
res = curl_easy_perform(curl);
if (res) {
printf("errored when performing curl, %s\n", curl_easy_strerror(res));
goto fail;
}
}
struct json_object *apn_profiles = json_tokener_parse(str.ptr);
char *values = "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";
const char *response = get_request(values);
struct json_object *apn_profiles = json_tokener_parse(response);
fail:
curl_cleaner(curl);
return apn_profiles;
}
......@@ -106,43 +88,16 @@ int get_apn_profiles_len(void)
return -1;
}
int _delete_apn(int idx, char *response)
char *_delete_apn(int idx)
{
CURL *curl;
CURLcode res;
char post_query[1024];
struct string str;
char query[1024] = {0};
str.ptr = calloc(1, 1);
str.len = 0;
printf("index we are attempting to remove: %d\n", idx);
post_query[0] = '\0';
strncpy(post_query, "isTest=false&apn_action=delete&apn_mode=manual&index=", 1023);
sprintf(post_query + strlen(post_query), "%d", idx);
strncat(post_query + strlen(post_query), "&goformId=APN_PROC_EX", 1023);
printf("post_query: %s\n", post_query);
curl = curl_easy_init();
if (curl) {
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");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_query);
query[0] = '\0';
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);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str);
res = curl_easy_perform(curl);
if (res) {
printf("errored when performing curl, %s\n", curl_easy_strerror(res));
goto fail;
}
}
printf("%s\n", str.ptr);
strncpy(response, str.ptr, 1023);
return 0;
fail:
return -1;
return post_request(query);
}
int get_apn_profile_idx(struct json_object *apn_profiles, char *name)
......@@ -160,194 +115,89 @@ int get_apn_profile_idx(struct json_object *apn_profiles, char *name)
return -1;
}
char *get_current_wan_apn(void)
char *_create_apn_profile(char *apn)
{
CURL *curl;
CURLcode res;
struct string str;
str.ptr = calloc(1, 1);
str.len = 0;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.1/goform/goform_get_cmd_process?isTest=false&cmd=wan_apn&multi_data=1");
curl_easy_setopt(curl, CURLOPT_REFERER, "http://192.168.0.1/index.html");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str);
res = curl_easy_perform(curl);
if (res) {
printf("errored when performing curl, %s\n", curl_easy_strerror(res));
goto fail;
}
}
return str.ptr;
fail:
return NULL;
char query[1024] = {0};
strncpy(query, "isTest=false&goformId=APN_PROC_EX&apn_action=save&apn_mode=manual&profile_name=", 1023);
strncat(query + strlen(query), apn, 1023);
strncat(query + strlen(query), "&wan_dial=*99%23&apn_select=manual&pdp_type=IP&pdp_select=auto&pdp_addr=&index=", 1023);
sprintf(query + strlen(query), "%d", get_apn_profiles_len());
strncat(query + strlen(query), "&wan_apn=", 1023);
strncat(query + strlen(query), apn, 1023);
strncat(query + strlen(query), "&ppp_auth_mode=none&ppp_username=&ppp_passwd=&dns_mode=auto&prefer_dns_manual=&standby_dns_manual=", 1023);
printf("query: %s\n", query);
return post_request(query);
}
char *_create_apn_profile(char *apn)
char *_set_apn_profile(int idx)
{
CURL *curl;
CURLcode res;
char post_query[1024];
struct string str;
str.ptr = calloc(1, 1);
str.len = 0;
char query[1024] = {0};
post_query[0] = '\0';
strncpy(post_query, "isTest=false&goformId=APN_PROC_EX&apn_action=save&apn_mode=manual&profile_name=", 1023);
strncat(post_query + strlen(post_query), apn, 1023);
strncat(post_query + strlen(post_query), "&wan_dial=*99%23&apn_select=manual&pdp_type=IP&pdp_select=auto&pdp_addr=&index=", 1023);
sprintf(post_query + strlen(post_query), "%d", get_apn_profiles_len());
strncat(post_query + strlen(post_query), "&wan_apn=", 1023);
strncat(post_query + strlen(post_query), apn, 1023);
strncat(post_query + strlen(post_query), "&ppp_auth_mode=none&ppp_username=&ppp_passwd=&dns_mode=auto&prefer_dns_manual=&standby_dns_manual=", 1023);
printf("post_query: %s\n", post_query);
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);
curl = curl_easy_init();
if (curl) {
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");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_query);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str);
res = curl_easy_perform(curl);
if (res) {
printf("errored when performing curl, %s\n", curl_easy_strerror(res));
goto fail;
}
}
return str.ptr;
fail:
return NULL;
return post_request(query);
}
char *_set_apn_profile(int idx)
{
CURL *curl;
CURLcode res;
char post_query[1024];
struct string str;
str.ptr = calloc(1, 1);
str.len = 0;
post_query[0] = '\0';
strncpy(post_query, "isTest=false&goformId=APN_PROC_EX&apn_mode=manual&apn_action=set_default&set_default_flag=1&pdp_type=IP&index=", 1023);
sprintf(post_query + strlen(post_query), "%d", idx);
printf("post_query: %s\n", post_query);
char *_enable_pin(char *pin)
{
char query[1024] = {0};
curl = curl_easy_init();
if (curl) {
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");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_query);
strncpy(query, "goformId=ENABLE_PIN&OldPinNumber=", 1023);
strncat(query + strlen(query), pin, 1023);
strncat(query + strlen(query), "&pin_save_flag=0&isTest=false", 1023);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str);
res = curl_easy_perform(curl);
if (res) {
printf("errored when performing curl, %s\n", curl_easy_strerror(res));
goto fail;
}
}
return str.ptr;
fail:
return NULL;
return post_request(query);
}
char *get_csv(char *csv)
char *_set_pin(char *current_pin, char *new_pin)
{
CURL *curl;
CURLcode res;
struct string str;
char get_query[1024];
str.ptr = calloc(1, 1);
str.len = 0;
curl = curl_easy_init();
char query[1024] = {0};
strncpy(get_query, "http://192.168.0.1/goform/goform_get_cmd_process?isTest=false&cmd=", 1023);
strncat(get_query + strlen(get_query), csv, 1023);
strncat(get_query, "&multi_data=1", 1023);
strncpy(query, "goformId=ENABLE_PIN&OldPinNumber=", 1023);
strncat(query + strlen(query), current_pin, 1023);
strncat(query + strlen(query), "&NewPinNumber=", 1023);
strncat(query + strlen(query), new_pin, 1023);
strncat(query + strlen(query), "&pin_save_flag=0&isTest=false", 1023);
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, get_query);
curl_easy_setopt(curl, CURLOPT_REFERER, "http://192.168.0.1/index.html");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str);
res = curl_easy_perform(curl);
if (res) {
printf("errored when performing curl, %s\n", curl_easy_strerror(res));
goto fail;
}
}
return str.ptr;
fail:
return NULL;
return post_request(query);
}
char *_enable_pin(char *pin)
char *_disable_pin(char *pin)
{
CURL *curl;
CURLcode res;
struct string str;
char post_query[1024];
char query[1024] = {0};
str.ptr = calloc(1, 1);
str.len = 0;
curl = curl_easy_init();
strncpy(query, "goformId=DISABLE_PIN&OldPinNumber=", 1023);
strncat(query + strlen(query), pin, 1023);
strncat(query + strlen(query), "&pin_save_flag=0&isTest=false", 1023);
strncpy(post_query, "goformId=ENABLE_PIN&OldPinNumber=", 1023);
strncat(post_query + strlen(post_query), pin, 1023);
strncat(post_query + strlen(post_query), "&pin_save_flag=0&isTest=false", 1023);
if (curl) {
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");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_query);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str);
res = curl_easy_perform(curl);
if (res)
{
printf("errored when performing curl, %s\n", curl_easy_strerror(res));
goto fail;
}
}
return str.ptr;
fail:
return NULL;
return post_request(query);
}
char *_set_pin(char *current_pin, char *new_pin)
char *post_request(char *query)
{
CURL *curl;
CURLcode res;
struct string str;
char post_query[1024];
str.ptr = calloc(1, 1);
str.len = 0;
curl = curl_easy_init();
strncpy(post_query, "goformId=ENABLE_PIN&OldPinNumber=", 1023);
strncat(post_query + strlen(post_query), current_pin, 1023);
strncat(post_query + strlen(post_query), "&NewPinNumber=", 1023);
strncat(post_query + strlen(post_query), new_pin, 1023);
strncat(post_query + strlen(post_query), "&pin_save_flag=0&isTest=false", 1023);
if (curl) {
if (curl)
{
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");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_query);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str);
res = curl_easy_perform(curl);
if (res) {
if (res)
{
printf("errored when performing curl, %s\n", curl_easy_strerror(res));
goto fail;
}
......@@ -357,30 +207,29 @@ fail:
return NULL;
}
char *_disable_pin(char *pin)
char *get_request(char *vars)
{
CURL *curl;
CURLcode res;
struct string str;
char post_query[1024] = {0};
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);
str.ptr = calloc(1, 1);
str.len = 0;
curl = curl_easy_init();
strncpy(post_query, "goformId=DISABLE_PIN&OldPinNumber=", 1023);
strncat(post_query + strlen(post_query), pin, 1023);
strncat(post_query + strlen(post_query), "&pin_save_flag=0&isTest=false", 1023);
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.1/goform/goform_set_cmd_process");
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, query);
curl_easy_setopt(curl, CURLOPT_REFERER, "http://192.168.0.1/index.html");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_query);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str);
res = curl_easy_perform(curl);
if (res) {
if (res)
{
printf("errored when performing curl, %s\n", curl_easy_strerror(res));
goto fail;
}
......
......@@ -10,13 +10,14 @@ size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp);
struct json_object *get_apn_profiles_json(void);
struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb);
int get_apn_profiles_len(void);
int _delete_apn(int idx, char *response);
char *_delete_apn(int idx);
int get_apn_profile_idx(struct json_object *apn_profiles, char *name);
char *get_current_wan_apn(void);
char *_create_apn_profile(char *apn);
char *_set_apn_profile(int idx);
char *get_csv(char *csv);
char *_enable_pin(char *pin);
char *_set_pin(char *current_pin, char *new_pin);
char *_disable_pin(char *pin);
char *post_request(char *query);
char *get_request(char *vars);
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment