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

remove boilerplate code to write on ubus

parent 5091d65b
No related branches found
No related tags found
No related merge requests found
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include <limits.h> #include <limits.h>
#include <libubox/list.h> #include <libubox/list.h>
#include <libubus.h>
#include "libmobile.h" #include "libmobile.h"
...@@ -31,7 +30,9 @@ int list_apn_profiles(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -31,7 +30,9 @@ int list_apn_profiles(struct ubus_context *ctx, struct ubus_object *obj,
struct blob_attr *msg) struct blob_attr *msg)
{ {
struct json_object *apn_profiles = get_apn_profiles_json(); struct json_object *apn_profiles = get_apn_profiles_json();
struct blob_buf bb;
write_to_ubus(apn_profiles, ctx, req);
/*struct blob_buf bb;
memset(&bb, 0, sizeof(struct blob_buf)); memset(&bb, 0, sizeof(struct blob_buf));
blob_buf_init(&bb, 0); blob_buf_init(&bb, 0);
...@@ -48,7 +49,7 @@ int list_apn_profiles(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -48,7 +49,7 @@ int list_apn_profiles(struct ubus_context *ctx, struct ubus_object *obj,
ubus_send_reply(ctx, req, bb.head); ubus_send_reply(ctx, req, bb.head);
json_object_put(apn_profiles); json_object_put(apn_profiles);
blob_buf_free(&bb); blob_buf_free(&bb);*/
return 0; return 0;
} }
...@@ -71,15 +72,10 @@ int delete_apn_profile(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -71,15 +72,10 @@ int delete_apn_profile(struct ubus_context *ctx, struct ubus_object *obj,
char *response = _delete_apn(idx); char *response = _delete_apn(idx);
struct json_object *parsed_response = json_tokener_parse(response); struct json_object *parsed_response = json_tokener_parse(response);
struct blob_buf bb;
write_to_ubus(parsed_response, ctx, req);
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);
json_object_put(apn_profiles); json_object_put(apn_profiles);
free(response);
} }
return 0; return 0;
...@@ -91,12 +87,11 @@ int set_apn_profile(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -91,12 +87,11 @@ int set_apn_profile(struct ubus_context *ctx, struct ubus_object *obj,
{ {
struct json_object *apn_profiles = get_apn_profiles_json(); struct json_object *apn_profiles = get_apn_profiles_json();
struct blob_attr *tb[__APN_MAX]; struct blob_attr *tb[__APN_MAX];
char apn[1024]; //STR_MAX or something from limits.h char apn[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));
apn[0] = '\0';
strncpy(apn, (char *)blobmsg_data(tb[APN]), 1023); strncpy(apn, (char *)blobmsg_data(tb[APN]), 1023);
printf("apn %s\n", apn); printf("apn %s\n", apn);
...@@ -107,14 +102,8 @@ int set_apn_profile(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -107,14 +102,8 @@ int set_apn_profile(struct ubus_context *ctx, struct ubus_object *obj,
char *response = _set_apn_profile(idx); char *response = _set_apn_profile(idx);
struct json_object *parsed_response = json_tokener_parse(response); struct json_object *parsed_response = json_tokener_parse(response);
struct blob_buf bb;
memset(&bb, 0, sizeof(struct blob_buf)); write_to_ubus(parsed_response, ctx, req);
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: fail:
free(response); free(response);
json_object_put(apn_profiles); json_object_put(apn_profiles);
...@@ -134,14 +123,8 @@ int create_apn_profile(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -134,14 +123,8 @@ int create_apn_profile(struct ubus_context *ctx, struct ubus_object *obj,
strncpy(apn, (char *)blobmsg_data(tb[APN]), 1023); strncpy(apn, (char *)blobmsg_data(tb[APN]), 1023);
char *response = _create_apn_profile(apn); char *response = _create_apn_profile(apn);
struct json_object *parsed_response = json_tokener_parse(response); struct json_object *parsed_response = json_tokener_parse(response);
struct blob_buf bb;
memset(&bb, 0, sizeof(struct blob_buf)); write_to_ubus(parsed_response, ctx, req);
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);
free(response); free(response);
return 0; return 0;
...@@ -153,14 +136,8 @@ int show_current_apn(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -153,14 +136,8 @@ int show_current_apn(struct ubus_context *ctx, struct ubus_object *obj,
{ {
char *wan_apn = get_request("wan_apn"); char *wan_apn = get_request("wan_apn");
struct json_object *parsed_response = json_tokener_parse(wan_apn); struct json_object *parsed_response = json_tokener_parse(wan_apn);
struct blob_buf bb;
memset(&bb, 0, sizeof(struct blob_buf)); write_to_ubus(parsed_response, ctx, req);
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);
free(wan_apn); free(wan_apn);
return 0; return 0;
} }
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include <limits.h> #include <limits.h>
#include <libubox/list.h> #include <libubox/list.h>
#include <libubus.h>
#include "libmobile.h" #include "libmobile.h"
...@@ -27,12 +26,9 @@ int signal_strength(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -27,12 +26,9 @@ int signal_strength(struct ubus_context *ctx, struct ubus_object *obj,
char *response = get_request("rssi"); char *response = get_request("rssi");
struct json_object *parsed_response = json_tokener_parse(response); struct json_object *parsed_response = json_tokener_parse(response);
memset(&bb, 0, sizeof(struct blob_buf)); write_to_ubus(parsed_response, ctx, req);
blob_buf_init(&bb, 0); free(response);
bb = json_to_blob(parsed_response, bb);
ubus_send_reply(ctx, req, bb.head);
blob_buf_free(&bb);
json_object_put(parsed_response);
return 0; return 0;
} }
......
...@@ -12,9 +12,9 @@ ...@@ -12,9 +12,9 @@
#include <limits.h> #include <limits.h>
#include <libubox/list.h> #include <libubox/list.h>
#include <libubus.h>
#include "libmobile.h" #include "libmobile.h"
#include "common.h"
struct ubus_context *ctx; struct ubus_context *ctx;
...@@ -38,19 +38,6 @@ const struct blobmsg_policy pin_policy[__PIN_MAX] = { ...@@ -38,19 +38,6 @@ const struct blobmsg_policy pin_policy[__PIN_MAX] = {
[PIN] = {.name = "pin", .type = BLOBMSG_TYPE_STRING}, [PIN] = {.name = "pin", .type = BLOBMSG_TYPE_STRING},
}; };
int print_response(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req)
{
struct blob_buf bb;
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);
return 0;
}
int isdigits(const char *pin) int isdigits(const char *pin)
{ {
while (*pin) { while (*pin) {
...@@ -103,6 +90,7 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -103,6 +90,7 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
json_object_object_get_ex(parsed_response, "pin_status", &rv); json_object_object_get_ex(parsed_response, "pin_status", &rv);
if (!json_object_get_int(rv)) { if (!json_object_get_int(rv)) {
free(response);
response = _enable_pin(current_pin); response = _enable_pin(current_pin);
json_object_put(parsed_response); json_object_put(parsed_response);
parsed_response = json_tokener_parse(response); parsed_response = json_tokener_parse(response);
...@@ -113,6 +101,7 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -113,6 +101,7 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
} }
} }
free(response);
response = _set_pin(current_pin, new_pin); response = _set_pin(current_pin, new_pin);
json_object_put(parsed_response); json_object_put(parsed_response);
parsed_response = json_tokener_parse(response); parsed_response = json_tokener_parse(response);
...@@ -123,8 +112,9 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -123,8 +112,9 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
} }
print: print:
print_response(parsed_response, ctx, req); write_to_ubus(parsed_response, ctx, req);
fail: fail:
free(response);
return 0; return 0;
} }
...@@ -162,8 +152,9 @@ int disable_pin(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -162,8 +152,9 @@ int disable_pin(struct ubus_context *ctx, struct ubus_object *obj,
} }
print: print:
print_response(parsed_response, ctx, req); write_to_ubus(parsed_response, ctx, req);
fail: fail:
free(response);
return 0; return 0;
} }
...@@ -202,11 +193,13 @@ int enable_pin(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -202,11 +193,13 @@ int enable_pin(struct ubus_context *ctx, struct ubus_object *obj,
} }
print: print:
print_response(parsed_response, ctx, req); write_to_ubus(parsed_response, ctx, req);
fail: fail:
free(response);
return 0; return 0;
} }
/* does not work as intended, could technicly disable/enable and record response, then do the opposite, could have unwanted consequences? */
int verify_pin(struct ubus_context *ctx, struct ubus_object *obj, int verify_pin(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)
...@@ -235,8 +228,9 @@ int verify_pin(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -235,8 +228,9 @@ int verify_pin(struct ubus_context *ctx, struct ubus_object *obj,
json_object_put(parsed_response); json_object_put(parsed_response);
parsed_response = json_tokener_parse(response); parsed_response = json_tokener_parse(response);
print_response(parsed_response, ctx, req); write_to_ubus(parsed_response, ctx, req);
fail: fail:
free(response);
return 0; return 0;
} }
...@@ -247,7 +241,7 @@ int remaining_tries(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -247,7 +241,7 @@ int remaining_tries(struct ubus_context *ctx, struct ubus_object *obj,
char *response = get_request("pinnumber"); char *response = get_request("pinnumber");
struct json_object *parsed_response = json_tokener_parse(response); struct json_object *parsed_response = json_tokener_parse(response);
print_response(parsed_response, ctx, req); write_to_ubus(parsed_response, ctx, req);
return 0; return 0;
} }
...@@ -255,7 +249,7 @@ struct ubus_method dongle_object_methods[] = { ...@@ -255,7 +249,7 @@ struct ubus_method dongle_object_methods[] = {
UBUS_METHOD("set_pin", set_pin, set_pin_policy), UBUS_METHOD("set_pin", set_pin, set_pin_policy),
UBUS_METHOD("disable_pin", disable_pin, pin_policy), UBUS_METHOD("disable_pin", disable_pin, pin_policy),
UBUS_METHOD("enable_pin", enable_pin, pin_policy), UBUS_METHOD("enable_pin", enable_pin, pin_policy),
UBUS_METHOD("verify_pin", verify_pin, pin_policy), //UBUS_METHOD("verify_pin", verify_pin, pin_policy),
UBUS_METHOD_NOARG("remaining_tries", remaining_tries), UBUS_METHOD_NOARG("remaining_tries", remaining_tries),
}; };
......
...@@ -5,6 +5,19 @@ struct string { ...@@ -5,6 +5,19 @@ struct string {
size_t len; size_t len;
}; };
int write_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req)
{
struct blob_buf bb;
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);
return 0;
}
void curl_cleaner(CURLcode *curl) void curl_cleaner(CURLcode *curl)
{ {
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
......
...@@ -4,7 +4,9 @@ ...@@ -4,7 +4,9 @@
#include <json-c/json.h> #include <json-c/json.h>
#include <string.h> #include <string.h>
#include <libubox/blobmsg.h> #include <libubox/blobmsg.h>
#include <libubus.h>
int write_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req);
void curl_cleaner(CURLcode *curl); void curl_cleaner(CURLcode *curl);
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);
struct json_object *get_apn_profiles_json(void); struct json_object *get_apn_profiles_json(void);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment