-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubus.c
175 lines (137 loc) · 4.14 KB
/
ubus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright (C) 2024 TDT AG <[email protected]>
*
* This is free software, licensed under the GNU General Public License v2.
* See https://www.gnu.org/licenses/gpl-2.0.txt for more information.
*
*/
#include <libubus.h>
#include "interface.h"
#include "logging.h"
static struct ubus_event_handler ev;
static struct ubus_context *ctx;
static struct blob_buf b;
enum ubus_event_attr {
UBUS_EVENT_INTERFACE,
UBUS_EVENT_ACTION,
__UBUS_EVENT_MAX,
};
static const struct blobmsg_policy ubus_event_policy[__UBUS_EVENT_MAX] = {
[UBUS_EVENT_INTERFACE] = {.name = "interface", .type = BLOBMSG_TYPE_STRING},
[UBUS_EVENT_ACTION] = {.name = "action", .type = BLOBMSG_TYPE_STRING},
};
static void ubus_receive_interface_event(struct ubus_context* ctx,
struct ubus_event_handler* ev,
const char* type,
struct blob_attr* msg)
{
struct blob_attr* tb[ARRAY_SIZE(ubus_event_policy)];
const char* interface = NULL;
const char* action = NULL;
blobmsg_parse(ubus_event_policy, ARRAY_SIZE(ubus_event_policy), tb,
blob_data(msg), blob_len(msg));
if (!tb[UBUS_EVENT_INTERFACE] || !tb[UBUS_EVENT_ACTION])
return;
interface = blobmsg_get_string(tb[UBUS_EVENT_INTERFACE]);
action = blobmsg_get_string(tb[UBUS_EVENT_ACTION]);
interface_notify(interface, action);
}
static void ubus_listen_network_events(void)
{
memset(&ev, 0, sizeof(ev));
ev.cb = ubus_receive_interface_event;
ubus_register_event_handler(ctx, &ev, "network.interface");
}
enum iface_reset_attr {
IFACE_RESET_NAME,
__IFACE_RESET_MAX
};
static const struct blobmsg_policy iface_reset_policy[] = {
[IFACE_RESET_NAME] = {.name = "interface", .type = BLOBMSG_TYPE_STRING},
};
static int
ubus_iface_reset(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__IFACE_RESET_MAX];
const char *name;
blobmsg_parse(iface_reset_policy, ARRAY_SIZE(iface_reset_policy), tb,
blobmsg_data(msg), blobmsg_len(msg));
if (!tb[IFACE_RESET_NAME])
return UBUS_STATUS_INVALID_ARGUMENT;
name = blobmsg_get_string(tb[IFACE_RESET_NAME]);
LOGGING_INFO("%s: got interface name '%s'", __func__, name);
blob_buf_init(&b, 0);
ubus_send_reply(ctx, req, b.head);
return UBUS_STATUS_OK;
}
enum iface_status_attr {
IFACE_STATUS_NAME,
__IFACE_STATUS_MAX
};
static const struct blobmsg_policy iface_status_policy[] = {
[IFACE_STATUS_NAME] = {.name = "interface", .type = BLOBMSG_TYPE_STRING},
};
static int
ubus_iface_status(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__IFACE_RESET_MAX];
const char *name;
blobmsg_parse(iface_reset_policy, ARRAY_SIZE(iface_reset_policy), tb,
blobmsg_data(msg), blobmsg_len(msg));
if (!tb[IFACE_RESET_NAME])
return UBUS_STATUS_INVALID_ARGUMENT;
name = blobmsg_get_string(tb[IFACE_RESET_NAME]);
blob_buf_init(&b, 0);
interface_get_status(name, &b);
ubus_send_reply(ctx, req, b.head);
return UBUS_STATUS_OK;
}
static const struct ubus_method mwan4_methods[] = {
UBUS_METHOD("reset", ubus_iface_reset, iface_reset_policy),
UBUS_METHOD("status", ubus_iface_status, iface_status_policy),
};
static struct ubus_object_type mwan4_object_type =
UBUS_OBJECT_TYPE("mwan4", mwan4_methods);
static struct ubus_object mwan4_object = {
.name = "mwan4",
.type = &mwan4_object_type,
.methods = mwan4_methods,
.n_methods = ARRAY_SIZE(mwan4_methods),
};
int ubus_init(void)
{
int ret;
ctx = ubus_connect(NULL);
if (!ctx) {
LOGGING_ERR("%s: failed to connect to ubus", __func__);
return -1;
}
ret = ubus_add_object(ctx, &mwan4_object);
if (ret) {
LOGGING_ERR("%s: failed to add mwan4 object: %s", __func__, ubus_strerror(ret));
ubus_add_uloop(ctx);
return -2;
}
ubus_listen_network_events();
/* if (!ret) {
LOGGING_ERR("%s: could not listen to interface events", __func__);
ubus_remove_object(ctx, &mwan4_object);
ubus_add_uloop(ctx);
return -3;
}*/
ubus_add_uloop(ctx);
LOGGING_INFO("%s: ubus for mwan4 initilized", __func__);
return 0;
}
void ubus_done(void)
{
if (ctx == NULL) {
return;
}
ubus_remove_object(ctx, &mwan4_object);
ubus_free(ctx);
}