{"id":307,"date":"2025-11-02T20:22:28","date_gmt":"2025-11-02T12:22:28","guid":{"rendered":"https:\/\/www.garfield-blog.top\/?p=307"},"modified":"2025-11-02T20:22:28","modified_gmt":"2025-11-02T12:22:28","slug":"%e3%80%90%e6%ba%90%e7%a0%81%e8%a7%a3%e6%9e%90%e3%80%91ubus3-examples%e5%8e%9f%e7%90%86%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.garfield-blog.top\/?p=307","title":{"rendered":"\u3010\u6e90\u7801\u89e3\u6790\u3011ubus(3) examples\u539f\u7406\u5206\u6790"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u672c\u6587\u5206\u6790\u7684\u662fubus\u6e90\u7801\u4e2d\u7684\u5b98\u65b9demo\uff0c\u89c1\u6e90\u7801\u76ee\u5f55\u7684.\/ubus\/examples\/client.c<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">client\u6574\u4f53\u6d41\u7a0b<\/h2>\n\n\n\n<p>\u6574\u4f53\u6d41\u7a0b\u4e0a\uff0c<code>ubus<\/code>\u7684\u8fd0\u884c\u9700\u8981\u4e09\u90e8\u5206<code>ubusd<\/code>\u5b88\u62a4\u8fdb\u7a0b\u3001<code>server<\/code>\u7aef\u548c<code>client<\/code>\u7aef\uff0c\u672c\u6587\u5bf9\u4e8e<code>client<\/code>\u7aef\u7684\u5b9e\u73b0\u548c<code>client<\/code>\u7aef\u4e0e\u5b88\u62a4\u8fdb\u7a0b\u548c<code>server<\/code>\u7aef\u7684\u4ea4\u4e92\u8fdb\u884c\u5206\u6790<\/p>\n\n\n\n<p><strong>\u51fd\u6570\u8c03\u7528\u6808<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>main\n    -&gt;uloop_init\n    -&gt;ubus_connect\n        \/\/\u8fd9\u91cc\u521b\u5efa\u4e86\u4e00\u4e2astruct ubus_context\u53d8\u91cf\uff0c\u6ce8\u518c\u4e86\u5404\u4e2a\u56de\u8c03\n        \/\/\u5c06\u5404\u79cd\u8bf7\u6c42\u6dfb\u52a0\u5230\u5bf9\u5e94\u7684\u5404\u4e2a\u5b9e\u8df5\u94fe\u8868\u548c\u4e00\u4e2aavl\u6811\u4e2d\n        -&gt;ubus_connect_ctx\n    \/\/\u4f7f\u7528uloop\u76d1\u542c\u521a\u624d\u521b\u5efa\u7684ctx-&gt;sock.cb\uff0c\u5728\u5176\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\u4e2d\u53bb\u5904\u7406\u5b88\u62a4\u8fdb\u7a0b\u53d1\u6765\u7684\u6d88\u606f\n    -&gt;ubus_add_uloop\n    -&gt;client_main\n        \/\/\u5206\u522b\u6f14\u793a\u4e86ubus\u7684\u540c\u6b65\u6d88\u606f\u548c\u5f02\u6b65\u6d88\u606f\n        -&gt;ubus_invoke(\"watch\");\n        -&gt;ubus_invoke_async(\"hello\");<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u9700\u8981\u7279\u522b\u7684\u5bf9<code>ubus_connect_ctx<\/code>\u51fd\u6570\u5c55\u5f00\u89e3\u8bfb\uff0c\u8fd9\u4e2a\u51fd\u6570\u4e2d\u6267\u884c\u4e86\u5173\u952e\u7684\u521d\u59cb\u5316\u52a8\u4f5c<\/p>\n\n\n\n<p><strong>ubus_connect_ctx\u51fd\u6570<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e3a\u4e86\u65b9\u4fbf\u9605\u8bfb\uff0c\u6b64\u5904\u7701\u7565\u4e86\u90e8\u5206\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ ubus\/libubus.c\nint ubus_connect_ctx(struct ubus_context *ctx, const char *path)\n{\n    uloop_init();\n    memset(ctx, 0, sizeof(*ctx));\n\n    ctx-&gt;sock.fd = -1;\n    \/\/\u8bbe\u7f6e\u76d1\u542csocket\u7684\u56de\u8c03\uff0c\u5728\u76d1\u542c\u5230socket\u6d3b\u8dc3\u540e\u7acb\u523b\u6267\u884c\n    ctx-&gt;sock.cb = ubus_handle_data;\n    \/\/\u8bbe\u7f6eubus\u8fde\u63a5\u65ad\u5f00\u65f6\u7684\u56de\u8c03\n    ctx-&gt;connection_lost = ubus_default_connection_lost;\n    \/\/\u8bbe\u7f6e\u5904\u7406\u6d88\u606f\u961f\u5217\u7684\u56de\u8c03\uff0c\u4e00\u4e9b\u6d88\u606f\u53ef\u80fd\u6ca1\u6709\u88ab\u53ca\u65f6\u5904\u7406\uff0c\u5c31\u4f1a\u88ab\u653e\u7684\u6d88\u606f\u5904\u7406\u961f\u5217\u4e2d\n    ctx-&gt;pending_timer.cb = ubus_process_pending_msg;\n\n    \/\/UBUS_MSG_CHUNK_SIZE\u662f65536\n    ctx-&gt;msgbuf.data = calloc(1, UBUS_MSG_CHUNK_SIZE);\n    ctx-&gt;msgbuf_data_len = UBUS_MSG_CHUNK_SIZE;\n\n    \/\/\u5b58\u50a8\u5ba2\u6237\u7aef\u53d1\u8d77\u7684\u3001\u6b63\u5728\u7b49\u5f85\u54cd\u5e94\u7684 ubus \u8bf7\u6c42\n    INIT_LIST_HEAD(&amp;ctx-&gt;requests);\n    \/\/\u6682\u5b58\u90a3\u4e9b\u56e0\u4e3a\u5f53\u524d\u8c03\u7528\u6808\u6df1\u5ea6\u6216\u5176\u4ed6\u539f\u56e0\u4e0d\u80fd\u7acb\u5373\u5904\u7406\u7684\u4f20\u5165 ubus \u6d88\u606f\n    INIT_LIST_HEAD(&amp;ctx-&gt;pending);\n    \/\/\u7ba1\u7406\u90a3\u4e9b\u9700\u8981\u81ea\u52a8\u91cd\u65b0\u8ba2\u9605\u7684\u4e8b\u4ef6\u8ba2\u9605\u8005\n    INIT_LIST_HEAD(&amp;ctx-&gt;auto_subscribers);\n    \/\/\u5b58\u50a8\u6b64 ubus \u4e0a\u4e0b\u6587\u6ce8\u518c\u7684\u672c\u5730 ubus \u5bf9\u8c61\n    avl_init(&amp;ctx-&gt;objects, ubus_cmp_id, false, NULL);\n    \/* path\u542b\u4e49\u662f\u5b88\u62a4\u8fdb\u7a0b\u4e0eclient\u7aef\u901a\u4fe1\u4f7f\u7528\u7684socket\u53ef\u4ee5\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u8f93\u5165\u53c2\u6570\u6765\u6307\u5b9a\n     * \u5982\u679c\u6ca1\u6709\u6307\u5b9a\u3002\u90a3\u4e48ctx-&gt;sock.fd\u4f7f\u7528\u9ed8\u8ba4\u503cUBUS_UNIX_SOCKET\n     * UBUS_UNIX_SOCKET\u5728\u7f16\u8bd1\u65f6\u6307\u5b9a\uff0c\u901a\u5e38\u662f\/var\/run\/ubus\/ubus.sock\n     *\/\n    ubus_reconnect(ctx, path);\n\n    return 0;\n}\n\n\/\/\u8fd9\u91cc\u5c55\u5f00ubus_reconnect\u6838\u5fc3\u5185\u5bb9\n-&gt;ubus_reconnect\n    \/\/UBUS_UNIX_SOCKET\u901a\u5e38\u662fvar\/run\/ubus\/ubus.sock\n    if (!path)\n        path = UBUS_UNIX_SOCKET;\n    ctx-&gt;sock.fd = usock(USOCK_UNIX, path, NULL);<\/code><\/pre>\n\n\n\n<p><strong>\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p><code>ubus_connect_ctx<\/code>\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u521b\u5efa\u65b0\u7684<code>ubus<\/code>\u5bf9\u8c61<code>ctx<\/code>\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u8fdb\u884c\u5b58\u50a8\u3002\u8fd9\u91cc\u4e3b\u8981\u662f\u521b\u5efa<code>ctx<\/code>\u5bf9\u5e94\u7684\u53e5\u67c4\u3001\u56de\u8c03\u5e76\u5c06\u4e8b\u4ef6\u6ce8\u518c\u5230\u5bf9\u5e94\u7684\u94fe\u8868\u548c\u4e00\u4e2a<code>avl<\/code>\u6811\u4e2d<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">client\u7aef\/server\u7aef\u4e0eubus\u5b88\u62a4\u8fdb\u7a0b\u901a\u4fe1<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b88\u62a4\u8fdb\u7a0b\u63a5\u6536client\u7aef\/server\u7aef\u6d88\u606f<\/h3>\n\n\n\n<p><code>ubus<\/code>\u4e2d\u7684<code>client<\/code>\u7aef\u6216<code>server<\/code>\u7aef\u4e0e\u5b88\u62a4\u8fdb\u7a0b<code>ubusd<\/code>\u8fdb\u7a0b\u7684\u901a\u4fe1\u662f\u4f9d\u9760\u4e8e<code>socket<\/code>\u53e5\u67c4\uff0c\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570\u6307\u5b9a\u4f7f\u7528\u4ec0\u4e48\u53e5\u67c4\uff0c\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u90a3\u4e48\u5c31\u4f1a\u4f7f\u7528<code>cmake<\/code>\u4e2d\u6307\u5b9a\u9ed8\u8ba4\u53e5\u67c4\u8def\u5f84\uff0c<code>cmake<\/code>\u4e2d\u4f1a\u4e3a<code>UBUS_UNIX_SOCKET<\/code>\u5b8f\u53bb\u8d4b\u503c<\/p>\n\n\n\n<p><strong>\u8fdb\u7a0b\u95f4\u901a\u4fe1socket\u521d\u59cb\u5316\u6d41\u7a0b<\/strong><\/p>\n\n\n\n<p>\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u5c31\u4f1a\u4f7f\u7528<code>UBUS_UNIX_SOCKET<\/code>\u4e2d\u5b58\u50a8\u7684\u53e5\u67c4\u6765\u901a\u4fe1<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-&gt;ubus_connect\n    -&gt;ubus_connect_ctx\n        \/\/\u8bbe\u7f6e\u76d1\u542csocket\u7684\u56de\u8c03\n        ctx-&gt;sock.cb = ubus_handle_data;\n            -&gt;ubus_reconnect\n            \/\/UBUS_UNIX_SOCKET\u4f1a\u88ab\u9ed8\u8ba4\u914d\u7f6e\u6210\/var\/run\/ubus\/ubus.sock\n            ctx-&gt;sock.fd = usock(USOCK_UNIX, UBUS_UNIX_SOCKET, NULL);<\/code><\/pre>\n\n\n\n<p><strong>\u901a\u4fe1\u8fc7\u7a0b\u51fd\u6570\u8c03\u7528\u6808<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u8fd9\u91cc\u6dfb\u52a0\u4e00\u4e2aubus\u5bf9\u8c61,\u6dfb\u52a0ubus\u5bf9\u8c61\u7684\u52a8\u4f5c\u65e0\u6cd5\u7531client\u7aef\u81ea\u5df1\u5b8c\u6210\uff0c\u53ea\u80fd\u53d1\u6d88\u606f\u7ed9ubusd\u5b88\u62a4\u8fdb\u7a0b\n-&gt;ubus_add_object\n    -&gt;ubus_start_request\n        -&gt;__ubus_start_request\n            -&gt;ubus_send_msg\n                \/\/\u8fd9\u91cc\u4f7f\u7528sendmsg\u51fd\u6570\u901a\u8fc7ctx-&gt;sock.fd\u53bb\u53d1\u9001\u6d88\u606f\u7ed9\u5b88\u62a4\u8fdb\u7a0b\n                -&gt;sendmsg(fd, &amp;msghdr, 0)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">client\u7aef\/server\u7aef\u63a5\u6536\u5b88\u62a4\u8fdb\u7a0b\u6d88\u606f<\/h3>\n\n\n\n<p><code>main<\/code>\u4e2d\u4f1a\u4f7f\u7528<code>ubus_add_uloop<\/code>\u76d1\u542c<code>ctx-&gt;sock.fd<\/code>\uff0c\u5e76\u5728\u56de\u8c03\u51fd\u6570<code>ubus_handle_data<\/code>\u4e2d\u53bb\u5904\u7406\u5b88\u62a4\u8fdb\u7a0b\u53d1\u9001\u7684\u6d88\u606f<\/p>\n\n\n\n<p><strong>ubus_handle_data\u51fd\u6570<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u6b64\u5904\u6e90\u7801\u9664\u6ce8\u91ca\u5916\u672a\u4f5c\u589e\u5220<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ ubus\/libubus-io.c\nvoid __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)\n{\n    struct ubus_context *ctx = container_of(u, struct ubus_context, sock);\n    int recv_fd = -1;\n\n    while (1) {\n        if (!ctx-&gt;stack_depth)\n            ctx-&gt;pending_timer.cb(&amp;ctx-&gt;pending_timer);\n\n        if (!get_next_msg(ctx, &amp;recv_fd))\n            break;\n        ubus_process_msg(ctx, &amp;ctx-&gt;msgbuf, recv_fd);\n        if (uloop_cancelling() || ctx-&gt;cancel_poll)\n            break;\n    }\n\n    if (!ctx-&gt;stack_depth)\n        ctx-&gt;pending_timer.cb(&amp;ctx-&gt;pending_timer);\n\n    if (u-&gt;eof)\n        ctx-&gt;connection_lost(ctx);\n}<\/code><\/pre>\n\n\n\n<p><strong>\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\u51fd\u6570\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e2a<code>while(1)<\/code>\u7684\u5faa\u73af\uff0c\u5728\u5faa\u73af\u4e2d\u4f1a\u5904\u7406\u6240\u6709\u7684\u4e0e\u5b88\u62a4\u8fdb\u7a0b\u901a\u4fe1\u4f7f\u7528\u7684\u53e5\u67c4\u53d1\u9001\u7684\u6d88\u606f\uff0c\u76f4\u5230\u5b8c\u5168\u5904\u7406\u7ed3\u675f<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5404\u4e2aDEMO\u6d4b\u8bd5\u6a21\u5757<\/h2>\n\n\n\n<p>\u5728<code>ubus<\/code>\u7684<code>examples<\/code>\u76ee\u5f55\u4e0b\u6709\u6570\u4e2a\u4ee5<code>test_<\/code>\u5f00\u5934\u7684\u51fd\u6570\uff0c\u7ec4\u6210\u4e86\u8fd9\u4e2a\u6f14\u793a<code>demo<\/code><\/p>\n\n\n\n<p><code>client<\/code>\u7aef\u7684\u6f14\u793a\u4e2d\uff0c\u6700\u91cd\u8981\u7684\u662f<code>client_main<\/code>\u51fd\u6570<\/p>\n\n\n\n<p><strong>client_main\u51fd\u6570<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e3a\u4e86\u65b9\u4fbf\u9605\u8bfb\uff0c\u6b64\u5904\u7701\u7565\u4e86\u90e8\u5206\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ ubus\/examples\/client.c\nstatic void client_main(void)\n{\n    static struct ubus_request req;\n    uint32_t id;\n\n    ubus_add_object(ctx, &amp;test_client_object);\n\n    ubus_lookup_id(ctx, \"test\", &amp;id);\n\n    blob_buf_init(&amp;b, 0);\n    blobmsg_add_u32(&amp;b, \"id\", test_client_object.id);\n    ubus_invoke(ctx, id, \"watch\", b.head, NULL, 0, 3000);\n    test_client_notify_cb(&amp;notify_timer);\n\n    blob_buf_init(&amp;b, 0);\n    blobmsg_add_string(&amp;b, \"msg\", \"blah\");\n    ubus_invoke_async(ctx, id, \"hello\", b.head, &amp;req);\n    req.fd_cb = test_client_fd_cb;\n    req.complete_cb = test_client_complete_cb;\n    ubus_complete_request_async(ctx, &amp;req);\n\n    uloop_timeout_set(&amp;count_timer, 2000);\n\n    uloop_run();\n}<\/code><\/pre>\n\n\n\n<p><strong>\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p><code>client<\/code>\u7aef\u7684\u6f14\u793a\u4e3b\u8981\u4ece\u8fd9\u4e2a\u51fd\u6570\u5f00\u59cb\uff0c\u51fd\u6570\u4e2d\u5b9e\u73b0\u4e86\u53d1\u8d77\u4e86\u4e00\u4e2a\u540c\u6b65\u8bf7\u6c42<code>watch<\/code>\u548c\u4e00\u4e2a\u5f02\u6b65\u8bf7\u6c42<code>hello<\/code><\/p>\n\n\n\n<p>\u9996\u5148\u662f\u901a\u8fc7<code>ubus_add_object<\/code>\u628a<code>test_client_object<\/code>\u6ce8\u518c\u5230<code>ubus<\/code>\u4e2d\uff0c\u7136\u540e\u901a\u8fc7<code>ubus_lookup_id<\/code>\u627e\u5230<code>\"test\"<\/code>\u5bf9\u5e94\u7684<code>id<\/code>\uff0c\u7136\u540e\u8c03\u7528\u4e86<code>\"test\"<\/code>\u7684<code>\"watch\"<\/code>\u65b9\u6cd5\u548c<code>hello<\/code>\u65b9\u6cd5\u3002<code>test<\/code>\u548c<code>hello<\/code>\u5b9e\u9645\u4e0a\u662f\u5728<code>server<\/code>\u7aef\u4e2d\u58f0\u660e\u7684<code>ubus<\/code>\u63a5\u53e3<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">test_watch\u65b9\u6cd5<\/h3>\n\n\n\n<p><code>watch<\/code>\u65b9\u6cd5\u76f8\u5bf9\u7b80\u5355\uff0c\u662f\u4e00\u4e2a\u5355\u5411\u7684\u540c\u6b65\u6d88\u606f\uff0c\u6d88\u606f\u4e2d\u643a\u5e26\u4e86\u4e00\u4e2a<code>test_client_object<\/code>\u5bf9\u8c61\u7684<code>id<\/code>\uff0c\u8bf7\u6c42<code>server<\/code>\u7aef\u8ba2\u9605\u8fd9\u4e2a<code>test_client_object<\/code>\u5bf9\u8c61\u3002\u8ba2\u9605\u540e\u5982\u679c\u6709\u8c03\u7528 <code>ubus_notify<\/code> \u51fd\u6570\u6765\u53d1\u51fa\u4e00\u4e2a\u4e8b\u4ef6\/\u901a\u77e5\u65f6\uff0c<code>ubusd<\/code>\u4f1a\u5c06\u8fd9\u4e2a\u901a\u77e5\u8f6c\u53d1\u7ed9\u6240\u6709\u8ba2\u9605\u4e86\u8be5\u5bf9\u8c61\u7684\u8ba2\u9605\u8005<\/p>\n\n\n\n<p><strong>\u8ba2\u9605\u8005\u4f1a\u6ce8\u518c\u4e24\u4e2a\u56de\u8c03\u51fd\u6570<code>cb<\/code>\u548c<code>remove_cb<\/code><\/strong>\uff0c\u5206\u522b\u662f\u6536\u5230\u8ba2\u9605\u4e8b\u4ef6\u53d1\u751f(<code>cb<\/code>)\u548c\u8ba2\u9605\u5bf9\u8c61\u53d6\u6d88\u8ba2\u9605(<code>remove_cb<\/code>)\uff0c\u8ba2\u9605\u4e8b\u4ef6\u662f\u7531<strong>\u88ab\u8ba2\u9605\u5bf9\u8c61<\/strong>\u901a\u8fc7<code>ubus_notify<\/code>\u53d1\u9001\u6765\u89e6\u53d1\u7684<\/p>\n\n\n\n<p><strong>client\u7aef<\/strong><\/p>\n\n\n\n<p>\u5c01\u88c5<code>ubus<\/code>\u6d88\u606f\uff0c\u5c06 <code>test_client_object.id<\/code>\u5c01\u88c5\u5728<code>id<\/code>\u5b57\u6bb5\u4e2d\u53d1\u9001<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ubus\/examples\/client.c:client_main\nblob_buf_init(&amp;b, 0);\nblobmsg_add_u32(&amp;b, \"id\", test_client_object.id);\n\/\/ubus_invoke\u51fd\u6570\u4f1a\u7b49\u5f85watch\u65b9\u6cd5\u7ed3\u675f\u5e76\u8fd4\u56de\uff0c\u53c2\u6570\u4e2d\u76843000\u5c31\u662f\u8d85\u65f6\u65f6\u95f43000ms\nubus_invoke(ctx, id, \"watch\", b.head, NULL, 0, 3000);\n\/\/\u4e00\u76f4\u5faa\u73af\u7684\u901a\u8fc7ubus_notify\u53d1\u9001test_client_object\u4e8b\u4ef6\ntest_client_notify_cb(&amp;notify_timer);<\/code><\/pre>\n\n\n\n<p>\u518d<code>client<\/code>\u7aef\u4e2d\u6ce8\u518c\u4e86\u4e00\u4e2a<code>ubus<\/code>\u5bf9\u8c61\u548c\u5176\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u5c06\u8fd9\u4e2a\u5bf9\u8c61\u7684<code>id<\/code>\u53d1\u9001\u7ed9<code>server<\/code>\u7aef\uff0c\u8bf7\u6c42<code>server<\/code>\u7aef\u8ba2\u9605\u8fd9\u4e2a<code>ubus<\/code>\u5bf9\u8c61<\/p>\n\n\n\n<p><strong>\u53d1\u8d77test_client_object\u4e8b\u4ef6<\/strong><\/p>\n\n\n\n<p>\u5728\u6ce8\u518c<code>test_client_object<\/code>\u4e4b\u540e\uff0c<code>client<\/code>\u7aef\u4f1a\u5728<code>test_client_notify_cb<\/code>\u51fd\u6570\u4e2d\u901a\u8fc7<code>ubus_notify<\/code>\u53d1\u9001<code>test_client_object<\/code>\u4e8b\u4ef6\uff0c\u8fd9\u4f1a\u89e6\u53d1\u6ce8\u518c\u8005(<code>server<\/code>)\u7684\u56de\u8c03\u51fd\u6570<\/p>\n\n\n\n<p><strong>server\u7aef<\/strong><\/p>\n\n\n\n<p>\u4ece\u89e3\u6790<code>ubus<\/code>\u6d88\u606f\u7136\u540e\u4ece\u4e2d\u8bfb\u53d6<code>WATCH_ID<\/code>\u8fd9\u4e2a\u5b57\u6bb5\uff0c\u7136\u540e\u901a\u8fc7<code>ubus_subscribe<\/code>\u6ce8\u518c\u5bf9\u5e94\u7684<code>ubus<\/code>\u5bf9\u8c61<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u6b64\u5904\u6e90\u7801\u9664\u6ce8\u91ca\u5916\u672a\u4f5c\u589e\u5220<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ ubus\/examples\/server.c\nstatic int test_watch(struct ubus_context *ctx, struct ubus_object *obj,\n              struct ubus_request_data *req, const char *method,\n              struct blob_attr *msg)\n{\n    struct blob_attr *tb&#91;__WATCH_MAX];\n    int ret;\n\n    blobmsg_parse(watch_policy, __WATCH_MAX, tb, blob_data(msg), blob_len(msg));\n    if (!tb&#91;WATCH_ID])\n        return UBUS_STATUS_INVALID_ARGUMENT;\n\n    \/\/\u8bbe\u7f6e\u6ce8\u518cubus\u5bf9\u8c61\u7684\u5378\u8f7d\u56de\u8c03\n    test_event.remove_cb = test_handle_remove;\n    \/\/\u8bbe\u7f6e\u6ce8\u518cubus\u5bf9\u8c61\u7684\u89e6\u53d1\u56de\u8c03\n    test_event.cb = test_notify;\n    \/\/\u6ce8\u518cclient\u7aef\u53d1\u6765\u7684ubus\u5bf9\u8c61\n    ret = ubus_subscribe(ctx, &amp;test_event, blobmsg_get_u32(tb&#91;WATCH_ID]));\n    fprintf(stderr, \"Watching object %08x: %s\\n\", blobmsg_get_u32(tb&#91;WATCH_ID]), ubus_strerror(ret));\n    return ret;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p><code>watch<\/code>\u65b9\u6cd5\u7684\u5b9e\u73b0\u4e3b\u8981\u662f\u63a5\u6536\u4f20\u5165\u7684\u6d88\u606f\uff0c\u4ece\u5176\u4e2d\u89e3\u6790\u51fa<code>ubus<\/code>\u5bf9\u8c61\u7684<code>id<\/code>\uff0c\u5411<code>ubusd<\/code>\u6ce8\u518c\u8fd9\u4e2a<code>ubus<\/code>\u5bf9\u8c61\u5e76\u8bbe\u7f6e\u5bf9\u5e94\u7684\u89e6\u53d1\u4e8b\u4ef6\u56de\u8c03\u548c\u53d6\u6d88\u8ba2\u9605\u4e8b\u4ef6\u7684\u56de\u8c03<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">test_hello\u65b9\u6cd5<\/h3>\n\n\n\n<p><code>hello<\/code>\u65b9\u6cd5\u662f\u53e6\u4e00\u4e2a<code>client<\/code>\u7aef\u548c<code>server<\/code>\u7aef\u7684\u901a\u4fe1\u5b9e\u4f8b<code>demo<\/code>\uff0c\u76f8\u5bf9\u6765\u8bf4\u590d\u6742\u4e86\u5f88\u591a<\/p>\n\n\n\n<p><strong><code>hello<\/code>\u65b9\u6cd5\u5b9e\u73b0<code>client<\/code>\u7aef\u548c<code>server<\/code>\u7aef\u7684\u901a\u4fe1\u4f7f\u7528\u7684\u662f<code>ubus_invoke_async<\/code>\u548c<code>ubus_request_set_fd<\/code>\u3002<\/strong>\u9996\u5148\u5728<code>client<\/code>\u7aef\u901a\u8fc7<code>ubus_invoke_async<\/code>\u53bb\u8c03\u7528<code>hello<\/code>\u65b9\u6cd5\u540c\u65f6\u4f1a\u6ce8\u518c\u4e00\u4e2a\u540d\u5b57\u53eb<code>fd_cb<\/code>\u7684\u56de\u8c03\u51fd\u6570\uff0c\u5728\u8fd9\u91cc\u901a\u8fc7<code>ustream<\/code>\u5e93\u6765\u8fbe\u5230\u5bf9\u4f20\u5165\u7684\u7ba1\u9053\u8bfb\u7aef\u7684\u5f02\u6b65<code>IO<\/code>\u3002\u7136\u540e\u5728<code>server<\/code>\u7aef\u521b\u5efa\u7ba1\u9053\u5c06\u7ba1\u9053\u8bfb\u7aef\u901a\u8fc7<code>ubus_request_set_fd<\/code>\u53d1\u9001\u5230<code>client<\/code>\u7aef\uff0c\u5728<code>server<\/code>\u7aef\u5411\u7ba1\u9053\u5199\u7aef\u5199\u5165\u6570\u636e\u3002\u8fd9\u91cc\u4f7f\u7528\u7ba1\u9053\u53ef\u80fd\u662f\u4e3a\u4e86\u907f\u514d\u8d44\u6e90\u7ade\u4e89\u548c\u7701\u7565\u52a0\u9501\u7684\u6b65\u9aa4<\/p>\n\n\n\n<p><strong>client\u7aef<\/strong><\/p>\n\n\n\n<p><code>clinent<\/code>\u7aef\u4e3b\u8981\u7684\u903b\u8f91\u662f\u53d1\u9001\u4e00\u4e2a\u5f02\u6b65\u7684<code>ubus<\/code>\u6d88\u606f\uff0c\u8c03\u7528<code>hello<\/code>\u65b9\u6cd5\u3002\u5e76\u6ce8\u518c\u4e86\u4e24\u4e2a\u56de\u8c03\u5206\u522b\u5728<code>server<\/code>\u7aef\u54cd\u5e94\u7684\u53e5\u67c4\u548c\u5f02\u6b65\u8bf7\u6c42\u5b8c\u6210\u7684\u56de\u8c03<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ubus\/examples\/client.c:client_main\nblob_buf_init(&amp;b, 0);\nblobmsg_add_string(&amp;b, \"msg\", \"blah\");\n\/\/\u53d1\u8d77\u4e00\u4e2aubus\u5f02\u6b65\u8bf7\u6c42\nubus_invoke_async(ctx, id, \"hello\", b.head, &amp;req);\n\/\/server\u7aef\u53d1\u9001\u7684\u6d88\u606f\u5904\u7406\u51fd\u6570\nreq.fd_cb = test_client_fd_cb;\n\/\/\u5f02\u6b65\u8bf7\u6c42\u5b8c\u6210\u7684\u56de\u8c03\u51fd\u6570\nreq.complete_cb = test_client_complete_cb;\n\/\/\u7ed3\u675f\u5f02\u6b65\u8bf7\u6c42\nubus_complete_request_async(ctx, &amp;req);<\/code><\/pre>\n\n\n\n<p><strong>\u6d88\u606f\u5904\u7406<\/strong><\/p>\n\n\n\n<p>\u7531\u4e8e<code>ubus<\/code>\u5f02\u6b65\u8c03\u7528\u4e2d\uff0c<code>client<\/code>\u7aef\u548c<code>server<\/code>\u7aef\u4e4b\u95f4\u7684\u6d88\u606f\u901a\u4fe1\u4e3b\u8981\u662f\u9760\u8bfb\u5199\u7ba1\u9053\u6765\u5b9e\u73b0\uff0c\u6240\u4ee5<code>client<\/code>\u7aef\u5904\u7406<code>server<\/code>\u7aef\u7684\u6d88\u606f\u4e5f\u57fa\u672c\u4e0a\u662f\u56f4\u7ed5\u7740\u5904\u7406\u7ba1\u9053\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6765\u8fdb\u884c\u7684\u3002\u4e3b\u8981\u6d89\u53ca\u5230\u4e24\u4e2a\u51fd\u6570<code>test_client_fd_cb<\/code>\u548c<code>test_client_fd_data_cb<\/code>\uff0c\u5206\u522b\u662f\u4f7f\u7528<code>ustream<\/code>\u6765\u5c06\u7ba1\u9053\u8bfb\u7aef\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8f6c\u5316\u4e3a<code>ustream_fd<\/code>\u7136\u540e\u518d\u53bb\u8bfb\u53d6\u5176\u4e2d\u7684\u5185\u5bb9<\/p>\n\n\n\n<p><strong>\u5bf9\u5e94\u6e90\u4ee3\u7801<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u4e3a\u4e86\u65b9\u4fbf\u9605\u8bfb\uff0c\u6b64\u5904\u7701\u7565\u4e86\u90e8\u5206\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>static void test_client_fd_data_cb(struct ustream *s, int bytes)\n{\n    char *data, *sep;\n    int len;\n\n    data = ustream_get_read_buf(s, &amp;len);\n    sep = strchr(data, '\\n');\n\n    *sep = 0;\n    fprintf(stderr, \"Got line: %s\\n\", data);\n    ustream_consume(s, sep + 1 - data);\n}\n\nstatic void test_client_fd_cb(struct ubus_request *req, int fd)\n{\n    static struct ustream_fd test_fd;\n\n    fprintf(stderr, \"Got fd from the server, watching...\\n\");\n\n    test_fd.stream.notify_read = test_client_fd_data_cb;\n    ustream_fd_init(&amp;test_fd, fd);\n}<\/code><\/pre>\n\n\n\n<p><strong>server\u7aef<\/strong><\/p>\n\n\n\n<p><code>server<\/code>\u7aef\u7684\u5904\u7406\u4e3b\u8981\u662f\u901a\u8fc7<code>uloop<\/code>\u8bbe\u7f6e\u5faa\u73af\uff0c\u7136\u540e\u5411\u7ba1\u9053\u7684\u5199\u7aef\u5199\u5165\u6570\u636e\uff0c\u5728<code>client<\/code>\u7aef\u8bfb\u53d6\u7ba1\u9053\u7684\u8bfb\u7aef\uff0c<strong>\u6d41\u7a0b\u89c1\u4e0b\u56fe<\/strong>\u3002\u5173\u4e8e\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u4f7f\u7528\u7ba1\u9053\u5728\u4e24\u4e2a\u8fdb\u7a0b\u89c1\u901a\u4fe1\uff0c\u8fd9\u90e8\u5206\u7684\u5e95\u5c42\u5b9e\u73b0\u9700\u8981\u5206\u6790<code>ubusd<\/code>\u6e90\u7801\uff0c\u8fd9\u91cc\u4e0d\u505a\u5c55\u5f00<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/11\/\u7ba1\u9053\u901a\u4fe1-1024x336.jpg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"336\" data-original=\"https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/11\/\u7ba1\u9053\u901a\u4fe1-1024x336.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-308\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u7b14\u8005\u6ce8\uff1a\u672c\u6587\u5206\u6790\u7684\u662fubus\u6e90\u7801\u4e2d\u7684\u5b98\u65b9demo\uff0c\u89c1\u6e90\u7801\u76ee\u5f55\u7684.\/ubus\/examples\/client.c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-307","post","type-post","status-publish","format-standard","hentry","category-source-code-analysis"],"_links":{"self":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=307"}],"version-history":[{"count":1,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/307\/revisions"}],"predecessor-version":[{"id":309,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/307\/revisions\/309"}],"wp:attachment":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}