6 static struct ubus_context
*ctx
= NULL
;
7 static struct blob_buf b
;
17 static const struct blobmsg_policy dev_policy
[] = {
18 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
19 [DEV_FORCE
] = { .name
= "force", .type
= BLOBMSG_TYPE_INT8
},
22 static int netifd_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
23 struct ubus_request_data
*req
, const char *method
,
24 struct blob_attr
*msg
)
27 struct blob_attr
*tb
[DEV_LAST
];
28 bool add
= !strncmp(method
, "add", 3);
30 blobmsg_parse(dev_policy
, ARRAY_SIZE(dev_policy
), tb
, blob_data(msg
), blob_len(msg
));
33 return UBUS_STATUS_INVALID_ARGUMENT
;
35 dev
= get_device(blobmsg_data(tb
[DEV_NAME
]), false);
37 return UBUS_STATUS_NOT_FOUND
;
39 if (!add
|| (tb
[DEV_FORCE
] && blobmsg_get_u8(tb
[DEV_FORCE
])))
40 set_device_present(dev
, add
);
42 check_device_state(dev
);
47 static struct ubus_method main_object_methods
[] = {
48 UBUS_METHOD("add_device", netifd_handle_device
, dev_policy
),
49 UBUS_METHOD("del_device", netifd_handle_device
, dev_policy
),
52 static struct ubus_object_type main_object_type
=
53 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
55 static struct ubus_object main_object
= {
56 .name
= "network.interface",
57 .type
= &main_object_type
,
58 .methods
= main_object_methods
,
59 .n_methods
= ARRAY_SIZE(main_object_methods
),
62 int netifd_ubus_init(const char *path
)
66 ctx
= ubus_connect(path
);
70 DPRINTF("connected as %08x\n", ctx
->local_id
);
74 ret
= ubus_add_object(ctx
, &main_object
);
76 fprintf(stderr
, "Failed to publish object: %s\n", ubus_strerror(ret
));
81 void netifd_ubus_done(void)
87 /* per-interface object */
89 static int netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
90 struct ubus_request_data
*req
, const char *method
,
91 struct blob_attr
*msg
)
93 struct interface
*iface
;
95 iface
= container_of(obj
, struct interface
, ubus
);
96 set_interface_up(iface
);
101 static int netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
102 struct ubus_request_data
*req
, const char *method
,
103 struct blob_attr
*msg
)
105 struct interface
*iface
;
107 iface
= container_of(obj
, struct interface
, ubus
);
108 set_interface_down(iface
);
113 static void netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
115 struct interface_error
*error
;
119 e
= blobmsg_open_array(b
, "errors");
120 list_for_each_entry(error
, &iface
->errors
, list
) {
121 e2
= blobmsg_open_table(b
, NULL
);
123 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
124 blobmsg_add_string(b
, "code", error
->code
);
125 if (error
->data
[0]) {
126 e3
= blobmsg_open_array(b
, "data");
127 for (i
= 0; error
->data
[i
]; i
++)
128 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
129 blobmsg_close_array(b
, e3
);
132 blobmsg_close_table(b
, e2
);
134 blobmsg_close_array(b
, e
);
137 static int netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
138 struct ubus_request_data
*req
, const char *method
,
139 struct blob_attr
*msg
)
141 struct interface
*iface
;
143 iface
= container_of(obj
, struct interface
, ubus
);
145 blob_buf_init(&b
, 0);
146 blobmsg_add_u8(&b
, "up", iface
->up
);
147 blobmsg_add_u8(&b
, "active", iface
->active
);
148 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
149 if (iface
->main_dev
.dev
) {
150 struct device
*dev
= iface
->main_dev
.dev
;
154 /* use a different field for virtual devices */
160 devinfo
= blobmsg_open_table(&b
, field
);
161 blobmsg_add_string(&b
, "name", dev
->ifname
);
163 if (dev
->type
->dump_status
)
164 dev
->type
->dump_status(dev
, &b
);
166 blobmsg_close_table(&b
, devinfo
);
169 if (!list_is_empty(&iface
->errors
))
170 netifd_add_interface_errors(&b
, iface
);
172 ubus_send_reply(ctx
, req
, b
.head
);
178 static struct ubus_method iface_object_methods
[] = {
179 { .name
= "up", .handler
= netifd_handle_up
},
180 { .name
= "down", .handler
= netifd_handle_down
},
181 { .name
= "status", .handler
= netifd_handle_status
},
184 static struct ubus_object_type iface_object_type
=
185 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
188 void netifd_ubus_add_interface(struct interface
*iface
)
190 struct ubus_object
*obj
= &iface
->ubus
;
193 name
= malloc(strlen(main_object
.name
) + strlen(iface
->name
) + 2);
197 sprintf(name
, "%s.%s", main_object
.name
, iface
->name
);
199 obj
->type
= &iface_object_type
;
200 obj
->methods
= iface_object_methods
;
201 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
202 if (ubus_add_object(ctx
, &iface
->ubus
)) {
203 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
209 void netifd_ubus_remove_interface(struct interface
*iface
)
211 if (!iface
->ubus
.name
)
214 ubus_remove_object(ctx
, &iface
->ubus
);
215 free((void *) iface
->ubus
.name
);