libamxb  4.8.2
Bus Agnostic C API
amxb_ba_wait_for.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 #include <syslog.h>
56 #include <stdlib.h>
57 #include <amxc/amxc.h>
58 #include <amxp/amxp.h>
59 
60 #include <amxd/amxd_common.h>
61 #include <amxd/amxd_dm.h>
62 #include <amxd/amxd_object.h>
63 #include <amxd/amxd_object_event.h>
64 #include <amxd/amxd_path.h>
65 
66 #include <amxb/amxb_be_intf.h>
67 #include <amxb/amxb.h>
68 
69 #include "amxb_priv.h"
70 
71 static int32_t wait_for_objects = 0;
72 
73 static void amxb_wait_object_available(const char* const sig_name,
74  UNUSED const amxc_var_t* const data,
75  UNUSED void* const priv) {
76  amxp_signal_t* signal = NULL;
77  syslog(LOG_USER | LOG_NOTICE, "object '%s' available", sig_name + 5);
78  signal = amxp_sigmngr_find_signal(NULL, sig_name);
79  amxp_signal_delete(&signal);
80 
82  if(wait_for_objects <= 0) {
83  wait_for_objects = 0;
84  amxp_sigmngr_emit_signal(NULL, "wait:done", NULL);
85  }
86 }
87 
89  UNUSED const amxc_var_t* args,
90  void* priv) {
91  int retval = 0;
92  const amxb_be_funcs_t* fns = NULL;
93  const char* object = (const char*) priv;
94 
95  fns = bus_ctx->bus_fn;
96  if(amxb_is_valid_be_func(fns, wait_for, fns->wait_for)) {
97  retval = fns->wait_for(bus_ctx->bus_ctx, object);
98  } else {
100  }
101 
102  return retval;
103 }
104 
105 int amxb_wait_for_object(const char* object) {
106  int retval = -1;
107  amxd_path_t path;
108  char* object_path = NULL;
109  amxb_bus_ctx_t* bus_ctx = NULL;
110  amxc_string_t signal_name;
111  amxp_signal_t* signal = NULL;
112 
113  amxd_path_init(&path, NULL);
114  amxc_string_init(&signal_name, 0);
115 
116  when_str_empty(object, exit);
117 
118  amxd_path_setf(&path, true, "%s", object);
119  object_path = amxd_path_get_fixed_part(&path, AMXD_OBJECT_TERMINATE);
120  amxc_string_setf(&signal_name, "wait:%s", object_path);
121 
122  signal = amxp_sigmngr_find_signal(NULL, amxc_string_get(&signal_name, 0));
123  if(signal == NULL) {
124  retval = amxp_signal_new(NULL, &signal, amxc_string_get(&signal_name, 0));
125  when_failed(retval, exit);
126  amxp_slot_connect(NULL, amxc_string_get(&signal_name, 0), NULL,
129  }
130 
131  // When waiting for an object, make sure that the exact full path is matched
132  // remove entry from the cache and check again with full path matching
133  // full path is the fixed part (no wildcard or search parts)
134  amxb_be_cache_remove_path(object_path);
135  bus_ctx = amxb_be_who_has_impl(object_path, true);
136  if(bus_ctx != NULL) {
137  amxp_signal_emit(signal, NULL);
138  retval = 0;
139  } else {
140  retval = amxb_be_for_all_connections(amxb_wait_for_impl, NULL, (void*) object_path);
141  }
142 
143 exit:
144  free(object_path);
145  amxc_string_clean(&signal_name);
146  amxd_path_clean(&path);
147  return retval;
148 }
Ambiorix bus agnostic API header file.
static int32_t wait_for_objects
static void amxb_wait_object_available(const char *const sig_name, UNUSED const amxc_var_t *const data, UNUSED void *const priv)
static int amxb_wait_for_impl(amxb_bus_ctx_t *bus_ctx, UNUSED const amxc_var_t *args, void *priv)
Ambiorix Bus Backend Interface.
#define amxb_is_valid_be_func(ft, member, ptr)
Definition: amxb_priv.h:78
amxb_bus_ctx_t * amxb_be_who_has_impl(const char *object_path, bool full_match)
int amxb_be_for_all_connections(amxb_be_task_fn_t fn, const amxc_var_t *args, void *priv)
Calls a function on all open connections.
void amxb_be_cache_remove_path(const char *object_path)
Removes an object path and its corresponding bus context from the lookup cache.
#define AMXB_ERROR_NOT_SUPPORTED_OP
Function/operation not supported.
Definition: amxb_error.h:110
int amxb_wait_for_object(const char *object)
Checks if an object is available, if not waits for it.
The back-end interface structure.
amxb_be_wait_for_fn_t wait_for
const amxb_be_funcs_t * bus_fn
Definition: amxb_types.h:121
void * bus_ctx
Definition: amxb_types.h:122
static amxb_bus_ctx_t * bus_ctx
Definition: test_amxb_e2e.c:84