static void req_data_cb(struct ubus_request *req, int type, struct blob_attr *data) { struct blob_attr **attr; if (req->raw_data_cb) req->raw_data_cb(req, type, data); if (!req->data_cb) return; attr = ubus_parse_msg(data); req->data_cb(req, type, attr[UBUS_ATTR_DATA]); }
static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg) { struct ubus_object *obj = req->priv; struct blob_attr **attrbuf = ubus_parse_msg(msg); if (!attrbuf[UBUS_ATTR_OBJID]) return; obj->id = 0; if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type) obj->type->id = 0; avl_delete(&req->ctx->objects, &obj->avl); }
static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg) { struct ubus_object *obj = req->priv; struct blob_attr **attrbuf = ubus_parse_msg(msg); if (!attrbuf[UBUS_ATTR_OBJID]) return; obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]); if (attrbuf[UBUS_ATTR_OBJTYPE]) obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]); obj->avl.key = &obj->id; avl_insert(&req->ctx->objects, &obj->avl); }
static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr, struct ubus_object *obj, struct blob_attr **attrbuf) { printf("-> [obj] ubus_process_invoke\n"); struct ubus_request_data req = { .fd = -1, }; int method; int ret; bool no_reply = false; if (!obj) { ret = UBUS_STATUS_NOT_FOUND; goto send; } if (!attrbuf[UBUS_ATTR_METHOD]) { ret = UBUS_STATUS_INVALID_ARGUMENT; goto send; } if (attrbuf[UBUS_ATTR_NO_REPLY]) no_reply = blob_get_int8(attrbuf[UBUS_ATTR_NO_REPLY]); req.peer = hdr->peer; req.seq = hdr->seq; req.object = obj->id; if (attrbuf[UBUS_ATTR_USER] && attrbuf[UBUS_ATTR_GROUP]) { req.acl.user = blobmsg_get_string(attrbuf[UBUS_ATTR_USER]); req.acl.group = blobmsg_get_string(attrbuf[UBUS_ATTR_GROUP]); req.acl.object = obj->name; } for (method = 0; method < obj->n_methods; method++) if (!obj->methods[method].name || !strcmp(obj->methods[method].name, blob_data(attrbuf[UBUS_ATTR_METHOD]))) goto found; /* not found */ ret = UBUS_STATUS_METHOD_NOT_FOUND; goto send; found: ret = obj->methods[method].handler(ctx, obj, &req, blob_data(attrbuf[UBUS_ATTR_METHOD]), attrbuf[UBUS_ATTR_DATA]); if (req.deferred || no_reply) return; send: ubus_complete_deferred_request(ctx, &req, ret); } void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf) { void (*cb)(struct ubus_context *, struct ubus_msghdr *, struct ubus_object *, struct blob_attr **); struct ubus_msghdr *hdr = &buf->hdr; struct blob_attr **attrbuf; struct ubus_object *obj; uint32_t objid; void *prev_data = NULL; attrbuf = ubus_parse_msg(buf->data); if (!attrbuf[UBUS_ATTR_OBJID]) return; objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]); obj = avl_find_element(&ctx->objects, &objid, obj, avl); switch (hdr->type) { case UBUS_MSG_INVOKE: cb = ubus_process_invoke; break; case UBUS_MSG_UNSUBSCRIBE: cb = ubus_process_unsubscribe; break; case UBUS_MSG_NOTIFY: cb = ubus_process_notify; break; default: return; } if (buf == &ctx->msgbuf) { prev_data = buf->data; buf->data = NULL; } cb(ctx, hdr, obj, attrbuf); if (prev_data) { if (buf->data) free(prev_data); else buf->data = prev_data; } }