libubus-req: add data_cb callback handling for ubus notifications
[project/ubus.git] / libubus-req.c
index db5061c..92f80fa 100644 (file)
@@ -122,7 +122,7 @@ static void ubus_sync_req_cb(struct ubus_request *req, int ret)
 {
        req->status_msg = true;
        req->status_code = ret;
-       uloop_end();
+       req->ctx->cancel_poll = true;
 }
 
 static int64_t get_time_msec(void)
@@ -151,28 +151,26 @@ int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
 
        ctx->stack_depth++;
        while (!req->status_msg) {
-               bool cancelled = uloop_cancelled;
-
-               uloop_cancelled = false;
                if (req_timeout) {
                        timeout = time_end - get_time_msec();
                        if (timeout <= 0) {
                                ubus_set_req_status(req, UBUS_STATUS_TIMEOUT);
-                               uloop_cancelled = cancelled;
                                break;
                        }
                }
+
                ubus_poll_data(ctx, (unsigned int) timeout);
 
-               uloop_cancelled = cancelled;
                if (ctx->sock.eof) {
                        ubus_set_req_status(req, UBUS_STATUS_CONNECTION_FAILED);
+                       ctx->cancel_poll = true;
                        break;
                }
        }
+
        ctx->stack_depth--;
        if (ctx->stack_depth)
-               uloop_cancelled = true;
+               ctx->cancel_poll = true;
 
        if (req->status_msg)
                status = req->status_code;
@@ -255,6 +253,18 @@ ubus_notify_complete_cb(struct ubus_request *req, int ret)
        nreq->complete_cb(nreq, 0, 0);
 }
 
+static void
+ubus_notify_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
+{
+       struct ubus_notify_request *nreq;
+
+       nreq = container_of(req, struct ubus_notify_request, req);
+       if (!nreq->data_cb)
+               return;
+
+       nreq->data_cb(nreq, type, msg);
+}
+
 static int
 __ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
                    const char *type, struct blob_attr *msg,
@@ -280,6 +290,7 @@ __ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
        req->pending = 1;
        req->id[0] = obj->id;
        req->req.complete_cb = ubus_notify_complete_cb;
+       req->req.data_cb = ubus_notify_data_cb;
 
        return 0;
 }