libamxd  6.4.1
Data Model Manager
amxd_action_object_add_inst.c File Reference
#include <stdlib.h>
#include <string.h>
#include "amxd_priv.h"
#include <amxd/amxd_dm.h>
#include <amxd/amxd_action.h>
#include <amxd/amxd_object.h>
#include <amxd/amxd_object_expression.h>
#include "amxd_object_priv.h"
#include "amxd_assert.h"

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

static void amxd_action_add_inst_build_retval (amxd_object_t *instance, amxc_var_t *retval)
 
static amxd_status_t amxd_instance_validate_keys (amxd_object_t *instance)
 
static amxd_status_t amxd_action_add_inst (amxd_object_t *const templ, const char *name, uint32_t index, amxc_var_t *params, amxd_dm_access_t access, bool set_ro, amxc_var_t *retval)
 
static bool amxd_action_add_inst_is_created (amxd_object_t *const object, amxc_var_t *data)
 
amxd_status_t amxd_action_object_add_inst (amxd_object_t *const object, UNUSED amxd_param_t *const p, amxd_action_t reason, const amxc_var_t *const args, amxc_var_t *const retval, void *priv)
 
amxd_status_t amxd_object_add_instance (amxd_object_t **instance, amxd_object_t *templ, const char *name, uint32_t index, amxc_var_t *values)
 Data model object constructor function. More...
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 56 of file amxd_action_object_add_inst.c.

Function Documentation

◆ amxd_action_add_inst()

static amxd_status_t amxd_action_add_inst ( amxd_object_t *const  templ,
const char *  name,
uint32_t  index,
amxc_var_t *  params,
amxd_dm_access_t  access,
bool  set_ro,
amxc_var_t *  retval 
)
static

Definition at line 179 of file amxd_action_object_add_inst.c.

185  {
187  amxc_var_t templ_params;
188  amxd_object_t* instance = NULL;
189  amxp_expr_t* expr = NULL;
190 
191  amxc_var_init(&templ_params);
192  when_true_status(amxd_object_get_type(templ) != amxd_object_template,
193  exit,
195  when_true_status(amxd_object_is_attr_set(templ, amxd_oattr_private) &&
196  access != amxd_dm_access_private,
197  exit,
199  when_true_status(amxd_object_is_attr_set(templ, amxd_oattr_read_only) &&
200  !set_ro,
201  exit,
203 
204  status = amxd_object_new_instance(&instance, templ, name, index, params);
205  when_failed(status, exit);
206 
207  status = amxd_action_set_values(instance, access, true, params, retval, true);
208  if(status != amxd_status_ok) {
209  amxd_object_delete(&instance);
210  goto exit;
211  }
212 
214  if(status != amxd_status_ok) {
215  amxd_object_delete(&instance);
216  goto exit;
217  }
218 
219  amxd_action_add_inst_build_retval(instance, retval);
220 
221 exit:
222  amxc_var_clean(&templ_params);
223  amxp_expr_delete(&expr);
224  return status;
225 }
static amxd_status_t amxd_instance_validate_keys(amxd_object_t *instance)
static void amxd_action_add_inst_build_retval(amxd_object_t *instance, amxc_var_t *retval)
amxd_status_t PRIVATE amxd_action_set_values(amxd_object_t *const object, amxd_dm_access_t access, bool ro, const amxc_var_t *values, amxc_var_t *retval, bool required)
enum _amxd_status amxd_status_t
@ amxd_status_invalid_type
Definition: amxd_types.h:90
@ amxd_status_ok
Definition: amxd_types.h:78
@ amxd_status_unknown_error
Definition: amxd_types.h:79
@ amxd_status_invalid_action
Definition: amxd_types.h:89
@ amxd_oattr_read_only
Definition: amxd_types.h:199
@ amxd_oattr_private
Definition: amxd_types.h:202
@ amxd_dm_access_private
Definition: amxd_types.h:141
@ amxd_object_template
Definition: amxd_types.h:183
amxd_status_t amxd_object_new_instance(amxd_object_t **object, amxd_object_t *templ, const char *name, uint32_t index, amxc_var_t *values)
Data model object constructor function.
void amxd_object_delete(amxd_object_t **object)
Invokes the destroy handler(s) of the object.
static amxd_object_type_t amxd_object_get_type(const amxd_object_t *const object)
Returns the object type.
Definition: amxd_object.h:586
bool amxd_object_is_attr_set(const amxd_object_t *const object, const amxd_oattr_id_t attr)
Checks if an attribute is set.
Definition: amxd_object.c:348
static amxd_status_t status

◆ amxd_action_add_inst_build_retval()

static void amxd_action_add_inst_build_retval ( amxd_object_t instance,
amxc_var_t *  retval 
)
static

Definition at line 73 of file amxd_action_object_add_inst.c.

74  {
75  uint32_t index = amxd_object_get_index(instance);
76  const char* name = amxd_object_get_name(instance, AMXD_OBJECT_NAMED);
79  amxc_var_t* parameters = NULL;
80  const amxc_htable_t* ht_parameters = NULL;
81 
82  /* build the return value of the add instance action
83  the return value is a variant containing a htable
84  the variant contains this structure:
85  {
86  index : <the new index>,
87  name : <the new name>,
88  path : <the full absolute path with indeces>
89  object : <the full absolute path with names>,
90  parameters : <a hash table containig all key parameters and the values>
91  {
92  <KEY_PARAM1> : <KEY_PARAM1_VALUE>,
93  <KEY_PARAM2> : <KEY_PARAM2_VALUE>,
94  ...
95  <KEY_PARAMn> : <KEY_PARAMn_VALUE>
96  }
97  }
98  */
99  amxc_var_set_type(retval, AMXC_VAR_ID_HTABLE);
100  amxc_var_add_key(uint32_t, retval, "index", index);
101  amxc_var_add_key(cstring_t, retval, "name", name);
102  amxc_var_add_key(cstring_t, retval, "path", path);
103  amxc_var_add_key(cstring_t, retval, "object", object);
104  parameters = amxc_var_add_key(amxc_htable_t, retval, "parameters", NULL);
105 
106  /* Fetch the description of all parameters in the object and filter out
107  the key parameters.
108 
109  The parameters variant (which is an htable) will contain all parameters
110  after the call to amxd_object_describe_params.
111  Each element will have as the key the name of the parameter and the value
112  is a hash table containing all meta data of the parameter, including the
113  attributes.
114 
115  The returned description for each parameter has this format
116  <PARAM_NAME> :
117  {
118  attributes :
119  {
120  instance : <true or false>,
121  read-only : <true or false>,
122  volatile : <true or false>,
123  counter : <true or false>,
124  unique : <true or false>,
125  private : <true or false>,
126  template : <true or false>,
127  protected : <true or false>,
128  key : <true or false>,
129  persistent : <true or false>
130  },
131  value : <PARAM_VALUE>,
132  name : <PARAM_NAME>,
133  type_name : <PARAM_TYPE_NAME>,
134  type_id : <PARAM_TYPE_ID>
135  }
136  All parameters for which the "key" attribute is not set are deleted from
137  the parameters hash table (using amxc_var_delete)
138 
139  All parameters for which the "key" attribute is set are updated.
140  - the value of the parameter is taken from the description (field value)
141  - the value of the parameter is updated (this will remove the description)
142  */
144  ht_parameters = amxc_var_constcast(amxc_htable_t, parameters);
145  amxc_htable_for_each(it, ht_parameters) {
146  const char* param_name = amxc_htable_it_get_key(it);
147  amxc_var_t* param_description = amxc_var_from_htable_it(it);
148  bool is_key = GETP_BOOL(param_description, "attributes.key");
149  if(is_key) {
150  amxc_var_t* param_value = amxc_var_take_key(param_description, "value");
151  amxc_var_set_key(parameters, param_name, param_value, AMXC_VAR_FLAG_UPDATE);
152  } else {
153  amxc_var_delete(&param_description);
154  }
155  }
156  free(path);
157  free(object);
158 }
amxd_status_t amxd_object_describe_params(amxd_object_t *const object, amxc_var_t *const value, amxd_dm_access_t access)
#define AMXD_OBJECT_INDEXED
Name and path format flag - use index for instance objects.
Definition: amxd_object.h:176
#define AMXD_OBJECT_NAMED
Name and path format flag - default behavior, use name for instance objects.
Definition: amxd_object.h:164
#define AMXD_OBJECT_TERMINATE
Path format flag - when set the object path is terminated with a dot.
Definition: amxd_object.h:214
amxd_object_t amxd_status_t amxd_status_t char * amxd_object_get_path(const amxd_object_t *object, const uint32_t flags)
Get the full path of the object.
uint32_t amxd_object_get_index(const amxd_object_t *const object)
Get the index of an instance object.
Definition: amxd_object.c:265
const char * amxd_object_get_name(const amxd_object_t *const object, const uint32_t flags)
Get the name of the object (or index as a string for instance objects)
Definition: amxd_object.c:239

◆ amxd_action_add_inst_is_created()

static bool amxd_action_add_inst_is_created ( amxd_object_t *const  object,
amxc_var_t *  data 
)
static

Definition at line 227 of file amxd_action_object_add_inst.c.

228  {
229  bool retval = false;
230 
231  when_false(amxc_var_type_of(data) == AMXC_VAR_ID_HTABLE, exit);
232  when_null(GET_ARG(data, "index"), exit);
233  when_null(amxd_object_get_instance(object, NULL, GET_UINT32(data, "index")), exit);
234 
235  retval = true;
236 
237 exit:
238  return retval;
239 }
amxd_object_t * amxd_object_get_instance(const amxd_object_t *object, const char *name, uint32_t index)
Get an instance of the template object.

◆ amxd_action_object_add_inst()

amxd_status_t amxd_action_object_add_inst ( amxd_object_t *const  object,
UNUSED amxd_param_t *const  p,
amxd_action_t  reason,
const amxc_var_t *const  args,
amxc_var_t *const  retval,
void *  priv 
)

Definition at line 241 of file amxd_action_object_add_inst.c.

246  {
248  amxc_var_t* params = NULL;
249  amxc_var_t* max = (amxc_var_t*) priv;
250  uint32_t index = 0;
251  char* key = NULL;
253  bool set_ro = false;
254  bool is_created = false;
255 
256  when_null(object, exit);
257  when_null(retval, exit);
258  when_true_status(reason != action_object_add_inst,
259  exit,
261 
262  is_created = amxd_action_add_inst_is_created(object, retval);
263 
264  if(max != NULL) {
265  uint32_t instances = amxd_object_get_instance_count(object);
266  int64_t max_inst = 0;
267  instances = is_created ? instances - 1 : instances;
268  if(amxc_var_type_of(max) == AMXC_VAR_ID_CSTRING) {
269  amxc_var_t* ref_value = amxd_resolve_param_ref(object, max);
270  max = ref_value == NULL ? max : ref_value;
271  }
272  max_inst = amxc_var_dyncast(int64_t, max);
273  if((max_inst > 0) && ((int64_t) instances >= max_inst)) {
275  goto exit;
276  }
277  }
278 
279  if(is_created) {
281  goto exit;
282  }
283 
284  index = GET_UINT32(args, "index");
285  key = amxc_var_dyncast(cstring_t, GET_ARG(args, "name"));
286  params = GET_ARG(args, "parameters");
287  access = (amxd_dm_access_t) GET_UINT32(args, "access");
288  set_ro = GET_BOOL(args, "set_read_only");
289 
290  when_true_status(!amxd_action_verify_access(object, access),
291  exit,
293 
294  status = amxd_action_add_inst(object, key, index, params, access, set_ro, retval);
295 
296 exit:
297  free(key);
298  return status;
299 }
static amxd_status_t amxd_action_add_inst(amxd_object_t *const templ, const char *name, uint32_t index, amxc_var_t *params, amxd_dm_access_t access, bool set_ro, amxc_var_t *retval)
static bool amxd_action_add_inst_is_created(amxd_object_t *const object, amxc_var_t *data)
bool amxd_action_verify_access(amxd_object_t *object, amxd_dm_access_t access)
static uint32_t amxd_object_get_instance_count(const amxd_object_t *object)
amxc_var_t *PRIVATE amxd_resolve_param_ref(amxd_object_t *object, amxc_var_t *ref)
@ action_object_add_inst
Definition: amxd_types.h:123
@ amxd_status_function_not_implemented
Definition: amxd_types.h:83
@ amxd_status_object_not_found
Definition: amxd_types.h:80
@ amxd_status_invalid_value
Definition: amxd_types.h:88
enum _amxd_dm_access amxd_dm_access_t
Access level.
@ amxd_dm_access_public
Definition: amxd_types.h:136

◆ amxd_instance_validate_keys()

static amxd_status_t amxd_instance_validate_keys ( amxd_object_t instance)
static

Definition at line 160 of file amxd_action_object_add_inst.c.

160  {
162 
163  amxd_object_for_each(parameter, it, instance) {
164  amxd_param_t* param = amxc_container_of(it, amxd_param_t, it);
166  amxc_var_t value;
167  amxc_var_init(&value);
168  amxd_param_get_value(param, &value);
169  status = amxd_param_validate(param, &value);
170  amxc_var_clean(&value);
171  when_failed(status, exit);
172  }
173  }
174 
175 exit:
176  return status;
177 }
#define IS_BIT_SET(b, f)
Definition: amxd_common.h:66
amxd_status_t amxd_param_validate(amxd_param_t *const param, const amxc_var_t *const value)
uint32_t amxd_param_get_attrs(const amxd_param_t *const param)
amxd_status_t amxd_param_get_value(amxd_param_t *const param, amxc_var_t *const value)
@ amxd_pattr_key
Definition: amxd_types.h:362
#define amxd_object_for_each(type, it, object)
Helper macro for iterating object content.
Definition: amxd_object.h:113