75c61a4255eb3372aca0fa93623e1f4ba959cc82
[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 void test_client_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
22 {
23         fprintf(stderr, "Subscribers active: %d\n", obj->has_subscribers);
24 }
25
26 static struct ubus_object test_client_object = {
27         .subscribe_cb = test_client_subscribe_cb,
28 };
29
30 static void test_client_notify_cb(struct uloop_timeout *timeout)
31 {
32         static int counter = 0;
33         int err;
34
35         blob_buf_init(&b, 0);
36         blobmsg_add_u32(&b, "counter", counter++);
37
38         err = ubus_notify(ctx, &test_client_object, "ping", b.head, 1000);
39         if (err)
40                 fprintf(stderr, "Notify failed: %s\n", ubus_strerror(err));
41
42         uloop_timeout_set(timeout, 1000);
43 }
44
45 static struct uloop_timeout notify_timer = {
46         .cb = test_client_notify_cb,
47 };
48
49 static void client_main(void)
50 {
51         uint32_t id;
52         int ret;
53
54         ret = ubus_add_object(ctx, &test_client_object);
55         if (ret) {
56                 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
57                 return;
58         }
59
60         if (ubus_lookup_id(ctx, "test", &id)) {
61                 fprintf(stderr, "Failed to look up test object\n");
62                 return;
63         }
64
65         blob_buf_init(&b, 0);
66         blobmsg_add_u32(&b, "id", test_client_object.id);
67         ubus_invoke(ctx, id, "watch", b.head, NULL, 0, 3000);
68         test_client_notify_cb(&notify_timer);
69         uloop_run();
70 }
71
72 int main(int argc, char **argv)
73 {
74         const char *ubus_socket = NULL;
75         int ch;
76
77         while ((ch = getopt(argc, argv, "cs:")) != -1) {
78                 switch (ch) {
79                 case 's':
80                         ubus_socket = optarg;
81                         break;
82                 default:
83                         break;
84                 }
85         }
86
87         argc -= optind;
88         argv += optind;
89
90         uloop_init();
91
92         ctx = ubus_connect(ubus_socket);
93         if (!ctx) {
94                 fprintf(stderr, "Failed to connect to ubus\n");
95                 return -1;
96         }
97
98         ubus_add_uloop(ctx);
99
100         client_main();
101
102         ubus_free(ctx);
103         uloop_done();
104
105         return 0;
106 }