Amxb_Ubus  3.3.1
Ambiorix Ubus API
amxb_ubus.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 #if !defined(__AMXB_UBUS_H__)
55 #define __AMXB_UBUS_H__
56 
57 #ifdef __cplusplus
58 extern "C"
59 {
60 #endif
61 
62 #include <libubus.h>
63 #include <libubox/blob.h>
64 #include <libubox/uloop.h>
65 #include <libubox/usock.h>
66 #include <libubox/list.h>
67 
68 #include <amxc/amxc_macros.h>
69 #include <amxc/amxc.h>
70 #include <amxp/amxp.h>
71 
72 #include <amxd/amxd_dm.h>
73 #include <amxd/amxd_object.h>
74 #include <amxd/amxd_function.h>
75 #include <amxd/amxd_path.h>
76 
77 #include <amxb/amxb.h>
78 #include <amxb/amxb_be_intf.h>
79 
80 typedef struct _amxb_ubus_sub {
81  amxc_htable_it_t it;
82  struct ubus_subscriber sub;
83  amxp_timer_t* reactivate;
85 
86 typedef struct _amxb_ubus {
87  struct ubus_context* ubus_ctx;
88  amxc_htable_t subscribers; // one for each object
89  struct blob_buf b;
90  amxp_signal_mngr_t* sigmngr;
91  amxc_llist_t registered_objs;
92  struct ubus_event_handler watcher;
93  struct ubus_event_handler wait_watcher;
94  amxc_llist_t pending_reqs;
95  amxd_dm_t* dm;
97 
98 typedef struct _amxb_ubus_object {
99  amxc_llist_it_t it;
100  char* ubus_path;
101  struct ubus_object ubus_obj;
104 
105 typedef struct _amxb_ubus_request {
106  struct ubus_request* ubus_req;
107  uint32_t id;
108  amxc_var_t args;
109  amxb_request_t* request;
110  bool converted;
112 
113 amxb_be_info_t* amxb_be_info(void);
114 
115 int PRIVATE amxb_ubus_parse_blob(amxc_var_t* var,
116  struct blob_attr* attr,
117  bool table);
118 
119 int PRIVATE amxb_ubus_parse_blob_table(amxc_var_t* var,
120  struct blob_attr* attr,
121  int len);
122 
123 int PRIVATE amxb_ubus_parse_blob_array(amxc_var_t* var,
124  struct blob_attr* attr,
125  int len);
126 
127 int PRIVATE amxb_ubus_format_blob(amxc_var_t* data,
128  const char* key,
129  struct blob_buf* b);
130 
131 int PRIVATE amxb_ubus_format_blob_table(const amxc_htable_t* table,
132  struct blob_buf* b);
133 
134 int PRIVATE amxb_ubus_format_blob_array(const amxc_llist_t* list,
135  struct blob_buf* b);
136 
137 int PRIVATE amxb_ubus_get_longest_path(amxb_ubus_t* amxb_ubus_ctx,
138  amxd_path_t* path,
139  amxc_string_t* rel_path);
140 
141 void PRIVATE amxb_ubus_result_data(struct ubus_request* req,
142  int type,
143  struct blob_attr* msg);
144 
145 int PRIVATE amxb_ubus_invoke_base(amxb_ubus_t* amxb_ubus_ctx,
146  const char* object,
147  amxc_var_t* args,
148  amxb_request_t* request,
149  uint32_t* id);
150 
151 void* PRIVATE amxb_ubus_connect(const char* host,
152  const char* port,
153  const char* path,
154  amxp_signal_mngr_t* sigmngr);
155 
156 int PRIVATE amxb_ubus_disconnect(void* ctx);
157 
158 int PRIVATE amxb_ubus_get_fd(void* ctx);
159 
160 int PRIVATE amxb_ubus_read(void* ctx);
161 
162 void PRIVATE amxb_ubus_free(void* ctx);
163 
164 int PRIVATE amxb_ubus_invoke(void* const ctx,
165  amxb_invoke_t* invoke_ctx,
166  amxc_var_t* args,
167  amxb_request_t* request,
168  int timeout);
169 
170 int PRIVATE amxb_ubus_async_invoke(void* const ctx,
171  amxb_invoke_t* invoke_ctx,
172  amxc_var_t* args,
173  amxb_request_t* request);
174 
175 int PRIVATE amxb_ubus_wait_request(void* const ctx,
176  amxb_request_t* request,
177  int timeout);
178 
179 int PRIVATE amxb_ubus_close_request(void* const ctx,
180  amxb_request_t* request);
181 
182 int PRIVATE amxb_ubus_subscribe(void* const ctx,
183  const char* object);
184 
185 int PRIVATE amxb_ubus_unsubscribe(void* const ctx,
186  const char* object);
187 
188 int PRIVATE amxb_ubus_register(void* const ctx,
189  amxd_dm_t* const dm);
190 
191 void PRIVATE amxb_ubus_cancel_requests(amxb_ubus_t* amxb_ubus_ctx);
192 
193 void PRIVATE amxb_ubus_obj_it_free(amxc_llist_it_t* it);
194 
195 int PRIVATE amxb_ubus_list(void* const bus_ctx,
196  const char* object,
197  uint32_t flags,
198  uint32_t access,
199  amxb_request_t* request);
200 
201 const amxc_var_t* amxb_ubus_get_config_option(const char* name);
202 
203 #ifdef __cplusplus
204 }
205 #endif
206 
207 #endif // __AMXB_UBUS_H__
208 
void PRIVATE amxb_ubus_obj_it_free(amxc_llist_it_t *it)
int PRIVATE amxb_ubus_disconnect(void *ctx)
Definition: amxb_ubus.c:262
int PRIVATE amxb_ubus_wait_request(void *const ctx, amxb_request_t *request, int timeout)
int PRIVATE amxb_ubus_close_request(void *const ctx, amxb_request_t *request)
amxb_be_info_t * amxb_be_info(void)
Definition: amxb_ubus.c:442
int PRIVATE amxb_ubus_format_blob_array(const amxc_llist_t *list, struct blob_buf *b)
int PRIVATE amxb_ubus_parse_blob(amxc_var_t *var, struct blob_attr *attr, bool table)
void PRIVATE amxb_ubus_free(void *ctx)
Definition: amxb_ubus.c:316
int PRIVATE amxb_ubus_invoke_base(amxb_ubus_t *amxb_ubus_ctx, const char *object, amxc_var_t *args, amxb_request_t *request, uint32_t *id)
int PRIVATE amxb_ubus_format_blob_table(const amxc_htable_t *table, struct blob_buf *b)
struct _amxb_ubus amxb_ubus_t
int PRIVATE amxb_ubus_invoke(void *const ctx, amxb_invoke_t *invoke_ctx, amxc_var_t *args, amxb_request_t *request, int timeout)
int PRIVATE amxb_ubus_list(void *const bus_ctx, const char *object, uint32_t flags, uint32_t access, amxb_request_t *request)
int PRIVATE amxb_ubus_read(void *ctx)
Definition: amxb_ubus.c:295
int PRIVATE amxb_ubus_get_fd(void *ctx)
Definition: amxb_ubus.c:282
const amxc_var_t * amxb_ubus_get_config_option(const char *name)
Definition: amxb_ubus.c:218
int PRIVATE amxb_ubus_format_blob(amxc_var_t *data, const char *key, struct blob_buf *b)
int PRIVATE amxb_ubus_unsubscribe(void *const ctx, const char *object)
int PRIVATE amxb_ubus_subscribe(void *const ctx, const char *object)
int PRIVATE amxb_ubus_register(void *const ctx, amxd_dm_t *const dm)
void *PRIVATE amxb_ubus_connect(const char *host, const char *port, const char *path, amxp_signal_mngr_t *sigmngr)
Definition: amxb_ubus.c:222
int PRIVATE amxb_ubus_parse_blob_array(amxc_var_t *var, struct blob_attr *attr, int len)
struct _amxb_ubus_request amxb_ubus_request_t
int PRIVATE amxb_ubus_parse_blob_table(amxc_var_t *var, struct blob_attr *attr, int len)
int PRIVATE amxb_ubus_async_invoke(void *const ctx, amxb_invoke_t *invoke_ctx, amxc_var_t *args, amxb_request_t *request)
void PRIVATE amxb_ubus_cancel_requests(amxb_ubus_t *amxb_ubus_ctx)
struct _amxb_ubus_sub amxb_ubus_sub_t
struct _amxb_ubus_object amxb_ubus_object_t
int PRIVATE amxb_ubus_get_longest_path(amxb_ubus_t *amxb_ubus_ctx, amxd_path_t *path, amxc_string_t *rel_path)
void PRIVATE amxb_ubus_result_data(struct ubus_request *req, int type, struct blob_attr *msg)
struct ubus_object ubus_obj
Definition: amxb_ubus.h:101
amxc_llist_it_t it
Definition: amxb_ubus.h:99
amxb_ubus_t * amxb_ubus_ctx
Definition: amxb_ubus.h:102
struct ubus_request * ubus_req
Definition: amxb_ubus.h:106
amxb_request_t * request
Definition: amxb_ubus.h:109
amxc_var_t args
Definition: amxb_ubus.h:108
amxp_timer_t * reactivate
Definition: amxb_ubus.h:83
amxc_htable_it_t it
Definition: amxb_ubus.h:81
struct ubus_subscriber sub
Definition: amxb_ubus.h:82
struct ubus_event_handler wait_watcher
Definition: amxb_ubus.h:93
amxc_htable_t subscribers
Definition: amxb_ubus.h:88
amxc_llist_t pending_reqs
Definition: amxb_ubus.h:94
struct ubus_event_handler watcher
Definition: amxb_ubus.h:92
amxc_llist_t registered_objs
Definition: amxb_ubus.h:91
struct blob_buf b
Definition: amxb_ubus.h:89
struct ubus_context * ubus_ctx
Definition: amxb_ubus.h:87
amxp_signal_mngr_t * sigmngr
Definition: amxb_ubus.h:90
amxd_dm_t * dm
Definition: amxb_ubus.h:95
static amxp_signal_mngr_t * sigmngr
static amxb_bus_ctx_t * bus_ctx
static amxd_dm_t dm