315a8e39d34a4595deb613ea70d80bb8feeac78b
[project/ubus.git] / examples / client.c
1 /*
2  * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <unistd.h>
15
16 #include "libubus.h"
17
18 static struct ubus_context *ctx;
19 static struct blob_buf b;
20
21 static struct ubus_object test_client_object = {};
22
23 static void client_main(void)
24 {
25         uint32_t id;
26         int ret;
27
28         ret = ubus_add_object(ctx, &test_client_object);
29         if (ret) {
30                 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
31                 return;
32         }
33
34         if (ubus_lookup_id(ctx, "test", &id)) {
35                 fprintf(stderr, "Failed to look up test object\n");
36                 return;
37         }
38
39         blob_buf_init(&b, 0);
40         blobmsg_add_u32(&b, "id", test_client_object.id);
41         ubus_invoke(ctx, id, "watch", b.head, NULL, 0, 3000);
42         uloop_run();
43 }
44
45 int main(int argc, char **argv)
46 {
47         const char *ubus_socket = NULL;
48         int ch;
49
50         while ((ch = getopt(argc, argv, "cs:")) != -1) {
51                 switch (ch) {
52                 case 's':
53                         ubus_socket = optarg;
54                         break;
55                 default:
56                         break;
57                 }
58         }
59
60         argc -= optind;
61         argv += optind;
62
63         uloop_init();
64
65         ctx = ubus_connect(ubus_socket);
66         if (!ctx) {
67                 fprintf(stderr, "Failed to connect to ubus\n");
68                 return -1;
69         }
70
71         ubus_add_uloop(ctx);
72
73         client_main();
74
75         ubus_free(ctx);
76         uloop_done();
77
78         return 0;
79 }