Select Git revision
dongle_apn.c 3.37 KiB
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <limits.h>
#include <libubox/list.h>
#include "common.h"
struct ubus_context *ctx;
enum {
APN,
__APN_MAX,
};
const struct blobmsg_policy apn_policy[__APN_MAX] = {
[APN] = {.name = "apn", .type = BLOBMSG_TYPE_STRING},
};
int list_apn_profiles(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
char *response = mobile_get_apn_profiles();
struct json_object *parsed_response = parse_apn_profiles(response);
write_to_ubus(parsed_response, ctx, req);
json_object_put(parsed_response);
return 0;
//return parse_and_print(response, ctx, req);
}
int delete_apn_profile(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__APN_MAX];
char name[1024] = {0}; //what is max available name length in dongle?
blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg));
strncpy(name, (char *)blobmsg_data(tb[APN]), 1023);
char *response = mobile_delete_apn(name);
return parse_and_print(response, ctx, req);
}
int set_apn_profile(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__APN_MAX];
char name[1024] = {0}; //STR_MAX or something from limits.h
blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg));
strncpy(name, (char *)blobmsg_data(tb[APN]), 1023);
char *response = mobile_set_apn_profile(name);
return parse_and_print(response, ctx, req);
}
int create_apn_profile(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__APN_MAX];
char name[1024] = {0}; //STR_MAX or something from limits.h
blobmsg_parse(apn_policy, __APN_MAX, tb, blob_data(msg), blob_len(msg));
strncpy(name, (char *)blobmsg_data(tb[APN]), 1023);
char *response = mobile_create_apn_profile(name);
return parse_and_print(response, ctx, req);
}
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 = mobile_get_request("wan_apn");
char *response = mobile_get_wan_apn();
return parse_and_print(response, ctx, req);
}
struct ubus_method dongle_object_methods[] = {
UBUS_METHOD_NOARG("list", list_apn_profiles),
UBUS_METHOD("create", create_apn_profile, apn_policy),
UBUS_METHOD("delete", delete_apn_profile, apn_policy),
UBUS_METHOD("set_profile", set_apn_profile, apn_policy),
UBUS_METHOD_NOARG("current", show_current_apn),
};
struct ubus_object_type dongle_object_type = UBUS_OBJECT_TYPE("dongle", dongle_object_methods);
struct ubus_object dongle_object = {
.name = "dongle.apn",
.type = &dongle_object_type,
.methods = dongle_object_methods,
.n_methods = ARRAY_SIZE(dongle_object_methods),
};
void init_ubus(void)
{
ctx = ubus_connect(NULL);
if (!ctx) {
perror("ubus");
exit(1);
}
ubus_add_uloop(ctx);
}
int main(int argc, char **argv)
{
uloop_init();
init_ubus();
ubus_add_object(ctx, &dongle_object);
uloop_run();
return 0;
}