libamxb  4.8.2
Bus Agnostic C API
dummy_be.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 #include <stdlib.h>
55 #include <string.h>
56 
57 #include <amxc/amxc.h>
58 #include <amxp/amxp.h>
59 #include <amxd/amxd_dm.h>
60 #include <amxd/amxd_object.h>
61 #include <amxd/amxd_object_function.h>
62 #include <amxb/amxb_be.h>
63 #include <amxo/amxo.h>
64 
65 #include "dummy_be.h"
66 
67 #include <amxc/amxc_macros.h>
68 static amxd_dm_t remote_dm;
69 static amxo_parser_t parser;
71 
72 static void* amxb_dummy_connect(UNUSED const char* host,
73  UNUSED const char* port,
74  UNUSED const char* path,
75  UNUSED amxp_signal_mngr_t* sigmngr) {
76 
77  amxd_dm_init(&remote_dm);
78  amxo_parser_init(&parser);
79  return &remote_dm;
80 }
81 
82 static int amxb_dummy_disconnect(UNUSED void* ctx) {
83  return 0;
84 }
85 
86 static int amxb_dummy_invoke(UNUSED void* const ctx,
87  amxb_invoke_t* invoke_ctx,
88  amxc_var_t* args,
89  amxb_request_t* request,
90  UNUSED int timeout) {
91 
92  amxc_var_t empty_args;
93  amxc_var_t* return_value = NULL;
94  amxd_object_t* obj = NULL;
95  int rv = 0;
96 
97  amxc_var_init(&empty_args);
98  amxc_var_set_type(&empty_args, AMXC_VAR_ID_HTABLE);
99  amxc_var_set_type(request->result, AMXC_VAR_ID_LIST);
100 
101  if(args == NULL) {
102  args = &empty_args;
103  }
104 
105  return_value = amxc_var_add_new(request->result);
106  obj = amxd_dm_findf(&remote_dm, invoke_ctx->object);
107  when_null_status(obj, exit, rv = AMXB_ERROR_BUS_NOT_FOUND);
108 
109  rv = amxd_object_invoke_function(obj, invoke_ctx->method, args, return_value);
110 
111 exit:
112  amxc_var_clean(&empty_args);
113  return rv;
114 }
115 
116 static void amxb_dummy_free(UNUSED void* ctx) {
117  amxo_parser_clean(&parser);
118  amxd_dm_clean(&remote_dm);
119 }
120 
121 static int amxb_dummy_register(UNUSED void* const ctx,
122  UNUSED amxd_dm_t* const dm) {
123  return 0;
124 }
125 
126 static bool amxb_dummy_has(UNUSED void* const ctx,
127  const char* object) {
128  amxd_object_t* obj = amxd_dm_findf(&remote_dm, "%s", object);
129 
130  return obj != NULL;
131 }
132 
133 static uint32_t amxb_dummy_capabilities(UNUSED void* const ctx) {
134  return caps;
135 }
136 
139  .disconnect = amxb_dummy_disconnect,
140  .get_fd = NULL,
141  .read = NULL,
142  .invoke = amxb_dummy_invoke,
143  .async_invoke = NULL,
144  .wait_request = NULL,
145  .close_request = NULL,
146  .subscribe = NULL,
147  .unsubscribe = NULL,
148  .free = amxb_dummy_free,
149  .register_dm = amxb_dummy_register,
150  .has = amxb_dummy_has,
151  .capabilities = amxb_dummy_capabilities,
152  .name = "dummy",
153  .size = sizeof(amxb_be_funcs_t),
154 };
155 
157  .major = 2,
158  .minor = 0,
159  .build = -1
160 };
161 
163  .major = 2,
164  .minor = -1,
165  .build = -1
166 };
167 
169  .major = 0,
170  .minor = 0,
171  .build = 0,
172 };
173 
176  .max_supported = &sup_max_lib_version,
177  .be_version = &dummy_be_version,
178  .name = "dummy",
179  .description = "AMXB Dummy Backend for testing",
180  .funcs = &amxb_dummy_impl,
181 };
182 
185 }
186 
189 }
190 
191 int test_load_dummy_remote(const char* odl) {
192  amxd_object_t* root_obj = amxd_dm_get_root(&remote_dm);
193 
194  return amxo_parser_parse_file(&parser, odl, root_obj);
195 }
196 
197 void test_set_dummy_caps(uint32_t dummy_caps) {
198  caps = dummy_caps;
199 }
Ambiorix bus agnostic API header file.
#define AMXB_BE_DISCOVER_RESOLVE
Definition: amxb_be_intf.h:344
#define AMXB_BE_DISCOVER
Definition: amxb_be_intf.h:343
#define AMXB_BE_DISCOVER_DESCRIBE
Definition: amxb_be_intf.h:341
#define AMXB_BE_DISCOVER_LIST
Definition: amxb_be_intf.h:342
#define AMXB_ERROR_BUS_NOT_FOUND
Definition: amxb_error.h:154
struct _amxb_be_funcs amxb_be_funcs_t
Definition: amxb_types.h:79
static amxd_dm_t remote_dm
Definition: dummy_be.c:68
void test_set_dummy_caps(uint32_t dummy_caps)
Definition: dummy_be.c:197
static void amxb_dummy_free(UNUSED void *ctx)
Definition: dummy_be.c:116
amxb_be_info_t amxb_dummy_be_info
Definition: dummy_be.c:174
static amxo_parser_t parser
Definition: dummy_be.c:69
static int amxb_dummy_disconnect(UNUSED void *ctx)
Definition: dummy_be.c:82
static uint32_t caps
Definition: dummy_be.c:70
static uint32_t amxb_dummy_capabilities(UNUSED void *const ctx)
Definition: dummy_be.c:133
static amxb_be_funcs_t amxb_dummy_impl
Definition: dummy_be.c:137
static int amxb_dummy_register(UNUSED void *const ctx, UNUSED amxd_dm_t *const dm)
Definition: dummy_be.c:121
int test_unregister_dummy_be(void)
Definition: dummy_be.c:187
static bool amxb_dummy_has(UNUSED void *const ctx, const char *object)
Definition: dummy_be.c:126
static amxb_version_t dummy_be_version
Definition: dummy_be.c:168
static int amxb_dummy_invoke(UNUSED void *const ctx, amxb_invoke_t *invoke_ctx, amxc_var_t *args, amxb_request_t *request, UNUSED int timeout)
Definition: dummy_be.c:86
int test_load_dummy_remote(const char *odl)
Definition: dummy_be.c:191
int test_register_dummy_be(void)
Definition: dummy_be.c:183
static amxb_version_t sup_max_lib_version
Definition: dummy_be.c:162
static void * amxb_dummy_connect(UNUSED const char *host, UNUSED const char *port, UNUSED const char *path, UNUSED amxp_signal_mngr_t *sigmngr)
Definition: dummy_be.c:72
static amxb_version_t sup_min_lib_version
Definition: dummy_be.c:156
int amxb_be_unregister(amxb_be_funcs_t *const funcs)
Unregisters a backend interface.
int amxb_be_register(amxb_be_funcs_t *const funcs)
Registers backend interface.
The back-end interface structure.
amxb_be_connect_fn_t connect
const amxb_version_t * min_supported
Definition: amxb_types.h:105
char * method
Definition: amxb_types.h:153
char * object
Definition: amxb_types.h:151
A request structure.
Definition: amxb_types.h:138
amxc_var_t * result
Definition: amxb_types.h:140
int32_t major
Definition: amxb_types.h:99
static amxd_dm_t dm
Definition: test_amxb_e2e.c:85