libamxb  4.8.2
Bus Agnostic C API
amxb_ba_priv.c
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 
55 #ifndef _GNU_SOURCE
56 #define _GNU_SOURCE
57 #endif
58 
59 #include <stdlib.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <string.h>
63 
64 #include <amxc/amxc.h>
65 #include <amxp/amxp.h>
66 
67 #include <amxd/amxd_dm.h>
68 #include <amxd/amxd_object.h>
69 
70 #include <amxb/amxb_be_intf.h>
71 #include <amxb/amxb.h>
72 #include <amxd/amxd_path.h>
73 
74 #include "amxb_priv.h"
75 
76 void amxb_dm_event_to_object_event(const char* const sig_name,
77  const amxc_var_t* const data,
78  void* const priv) {
79  amxc_htable_it_t* sub_hit = (amxc_htable_it_t*) priv;
80  const char* subscriber_object = amxc_htable_it_get_key(sub_hit);
81  amxb_bus_ctx_t* ctx = amxc_container_of(sub_hit->ait->array, amxb_bus_ctx_t, subscriptions);
82 
83  amxc_var_add_key(cstring_t, (amxc_var_t*) data, "notification", sig_name);
84  amxp_sigmngr_emit_signal(&ctx->sigmngr, subscriber_object, data);
85 }
86 
88  const char* obj_path) {
89  amxd_dm_t* dm = ctx->dm;
90  amxd_object_t* object = amxd_dm_findf(dm, "%s", obj_path);
91  return object;
92 }
93 
95  const char* obj_path) {
96  amxd_dm_t* dm = ctx->dm;
97  amxd_path_t path;
98  amxd_object_t* object = NULL;
99 
100  amxd_path_init(&path, NULL);
101  amxd_path_setf(&path, true, "%s", obj_path);
102  when_null(dm, exit);
103 
104  while(object == NULL && !amxc_string_is_empty(&path.path)) {
105  char* part = NULL;
106  object = amxd_dm_findf(dm, "%s", amxd_path_get(&path, AMXD_OBJECT_TERMINATE));
107  part = amxd_path_get_last(&path, true);
108  free(part);
109  }
110 
111 exit:
112  amxd_path_clean(&path);
113  return object != NULL;
114 }
115 
117  amxd_path_t* reference,
118  int timeout) {
119  int retval = amxd_status_unknown_error;
120  amxc_var_t resolved;
121  char* ref = amxd_path_get_reference_part(reference, true);
122  uint32_t index = amxd_path_get_reference_index(reference);
123  amxd_path_t ref_path;
124  amxc_var_t* real_path = NULL;
125 
126  amxd_path_init(&ref_path, ref);
127  amxc_var_init(&resolved);
128 
129  when_true(index == 0, exit);
130  when_str_empty(amxd_path_get_param(&ref_path), exit);
131  retval = amxb_get(bus_ctx, ref, 0, &resolved, timeout);
132  when_failed(retval, exit);
133 
134  when_true(amxc_htable_size(amxc_var_constcast(amxc_htable_t, GETP_ARG(&resolved, "0"))) > 1, exit);
135 
136  real_path = amxc_var_get_pathf(&resolved, AMXC_VAR_FLAG_DEFAULT,
137  "0.0.%s",
138  amxd_path_get_param(&ref_path));
139 
140  amxc_var_cast(real_path, AMXC_VAR_ID_CSV_STRING);
141  amxc_var_cast(real_path, AMXC_VAR_ID_LIST);
142 
143  if(index - 1 >= amxc_llist_size(amxc_var_constcast(amxc_llist_t, real_path))) {
144  goto exit;
145  }
146 
147  amxd_path_setf(&ref_path, true, "%s", GETI_CHAR(real_path, index - 1));
148  amxd_path_prepend(reference, amxd_path_get(&ref_path, 0));
149 
150 exit:
151  amxd_path_clean(&ref_path);
152  free(ref);
153  amxc_var_clean(&resolved);
154  return retval;
155 }
156 
159  const amxb_be_funcs_t* fns = NULL;
160 
161  when_null(bus_ctx, exit);
162  fns = bus_ctx->bus_fn;
163 
164  if(amxb_is_valid_be_func(fns, capabilities, fns->capabilities)) {
165  caps = fns->capabilities(bus_ctx->bus_ctx);
166  }
167 
168 exit:
169  return caps;
170 }
Ambiorix bus agnostic API header file.
void amxb_dm_event_to_object_event(const char *const sig_name, const amxc_var_t *const data, void *const priv)
Definition: amxb_ba_priv.c:76
uint32_t amxb_be_get_capabilities(amxb_bus_ctx_t *bus_ctx)
Definition: amxb_ba_priv.c:157
amxd_object_t * amxb_fetch_local_object(amxb_bus_ctx_t *ctx, const char *obj_path)
Definition: amxb_ba_priv.c:87
bool amxb_is_local_object(amxb_bus_ctx_t *ctx, const char *obj_path)
Definition: amxb_ba_priv.c:94
int amxb_follow_reference(amxb_bus_ctx_t *const bus_ctx, amxd_path_t *reference, int timeout)
Definition: amxb_ba_priv.c:116
Ambiorix Bus Backend Interface.
#define AMXB_BE_DISCOVER_RESOLVE
Definition: amxb_be_intf.h:344
#define AMXB_BE_DISCOVER_DESCRIBE
Definition: amxb_be_intf.h:341
#define AMXB_BE_DISCOVER_LIST
Definition: amxb_be_intf.h:342
#define amxb_is_valid_be_func(ft, member, ptr)
Definition: amxb_priv.h:78
static uint32_t caps
Definition: dummy_be.c:70
int amxb_get(amxb_bus_ctx_t *const bus_ctx, const char *object, int32_t depth, amxc_var_t *ret, int timeout)
Fetches one or more objects or a single parameter.
The back-end interface structure.
amxb_be_capabilities_fn_t capabilities
const amxb_be_funcs_t * bus_fn
Definition: amxb_types.h:121
amxd_dm_t * dm
Definition: amxb_types.h:123
amxp_signal_mngr_t sigmngr
Definition: amxb_types.h:120
void * bus_ctx
Definition: amxb_types.h:122
static amxd_dm_t dm
Definition: test_amxb_e2e.c:85
static amxb_bus_ctx_t * bus_ctx
Definition: test_amxb_e2e.c:84