6 static struct ubus_context
*ctx
= NULL
;
10 static const struct ubus_signature main_object_sig
[] = {
11 UBUS_METHOD_START("add_device"),
12 UBUS_FIELD(STRING
, "name"),
15 UBUS_METHOD_START("del_device"),
16 UBUS_FIELD(STRING
, "name"),
20 static struct ubus_object_type main_object_type
=
21 UBUS_OBJECT_TYPE("netifd", main_object_sig
);
29 static const struct blobmsg_policy dev_policy
[] = {
30 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
31 [DEV_FORCE
] = { .name
= "force", .type
= BLOBMSG_TYPE_INT8
},
34 static int netifd_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
35 struct ubus_request_data
*req
, const char *method
,
36 struct blob_attr
*msg
)
39 struct blob_attr
*tb
[DEV_LAST
];
40 bool add
= !strncmp(method
, "add", 3);
42 blobmsg_parse(dev_policy
, ARRAY_SIZE(dev_policy
), tb
, blob_data(msg
), blob_len(msg
));
45 return UBUS_STATUS_INVALID_ARGUMENT
;
47 dev
= get_device(blobmsg_data(tb
[DEV_NAME
]), false);
49 return UBUS_STATUS_NOT_FOUND
;
51 if (!add
|| (tb
[DEV_FORCE
] && blobmsg_get_u8(tb
[DEV_FORCE
])))
52 set_device_present(dev
, add
);
54 check_device_state(dev
);
59 static struct ubus_method main_object_methods
[] = {
60 { .name
= "add_device", .handler
= netifd_handle_device
},
61 { .name
= "del_device", .handler
= netifd_handle_device
},
64 static struct ubus_object main_object
= {
65 .name
= "network.interface",
66 .type
= &main_object_type
,
67 .methods
= main_object_methods
,
68 .n_methods
= ARRAY_SIZE(main_object_methods
),
71 int netifd_ubus_init(const char *path
)
75 ctx
= ubus_connect(path
);
79 DPRINTF("connected as %08x\n", ctx
->local_id
);
83 ret
= ubus_add_object(ctx
, &main_object
);
85 fprintf(stderr
, "Failed to publish object: %s\n", ubus_strerror(ret
));
90 void netifd_ubus_done(void)
96 /* per-interface object */
97 static const struct ubus_signature iface_object_sig
[] = {
98 UBUS_METHOD_START("up"),
101 UBUS_METHOD_START("down"),
105 static struct ubus_object_type iface_object_type
=
106 UBUS_OBJECT_TYPE("netifd_iface", iface_object_sig
);
109 static int netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
110 struct ubus_request_data
*req
, const char *method
,
111 struct blob_attr
*msg
)
113 struct interface
*iface
;
115 iface
= container_of(obj
, struct interface
, ubus
);
116 set_interface_up(iface
);
121 static int netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
122 struct ubus_request_data
*req
, const char *method
,
123 struct blob_attr
*msg
)
125 struct interface
*iface
;
127 iface
= container_of(obj
, struct interface
, ubus
);
128 set_interface_down(iface
);
133 static struct ubus_method iface_object_methods
[] = {
134 { .name
= "up", .handler
= netifd_handle_up
},
135 { .name
= "down", .handler
= netifd_handle_down
},
139 void netifd_ubus_add_interface(struct interface
*iface
)
141 struct ubus_object
*obj
= &iface
->ubus
;
144 name
= malloc(strlen(main_object
.name
) + strlen(iface
->name
) + 2);
148 sprintf(name
, "%s.%s", main_object
.name
, iface
->name
);
150 obj
->type
= &iface_object_type
;
151 obj
->methods
= iface_object_methods
;
152 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
153 if (ubus_add_object(ctx
, &iface
->ubus
)) {
154 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
160 void netifd_ubus_remove_interface(struct interface
*iface
)
162 if (!iface
->ubus
.name
)
165 ubus_remove_object(ctx
, &iface
->ubus
);
166 free((void *) iface
->ubus
.name
);