libamxd  6.4.1
Data Model Manager
Data Model
Collaboration diagram for Data Model:

Modules

 Data Model Actions
 

Functions

amxd_status_t amxd_dm_new (amxd_dm_t **dm)
 Instantiate a new data model. More...
 
void amxd_dm_delete (amxd_dm_t **dm)
 Deletes a data model structure. More...
 
amxd_status_t amxd_dm_init (amxd_dm_t *dm)
 Initializes a data model structure. More...
 
void amxd_dm_clean (amxd_dm_t *dm)
 Cleans a data model structure. More...
 
amxd_status_t amxd_dm_add_root_object (amxd_dm_t *const dm, amxd_object_t *const object)
 Adds an object to the root of the data model. More...
 
amxd_status_t amxd_dm_remove_root_object (amxd_dm_t *const dm, const char *name)
 Removes an object from the root of the data model. More...
 
amxd_object_tamxd_dm_get_root (amxd_dm_t *const dm)
 Fetches the root object of the data model. More...
 
amxd_object_t amxd_status_t amxd_status_t const char * amxd_dm_signal_get_path (amxd_dm_t *const dm, const amxc_var_t *const signal_data)
 Get the object path from a data model signal. More...
 
amxd_object_tamxd_dm_signal_get_object (amxd_dm_t *const dm, const amxc_var_t *const signal_data)
 Get the object from a data model using the path in the recieved signal. More...
 

Detailed Description

Function Documentation

◆ amxd_dm_add_root_object()

amxd_status_t amxd_dm_add_root_object ( amxd_dm_t *const  dm,
amxd_object_t *const  object 
)

Adds an object to the root of the data model.

A data model object can be created with amxd_object_new and added to either the data model itself using this function, or added to another object using amxd_object_add_object

Note
There is not need to keep a pointer to objects that are added to the data model, using functions amxd_object_get and amxd_object_findf it is posisble to retrieve the object pointers at any time.
Parameters
dmthe pointer to the data model structure
objectthe pointer to an object
Returns
Returns amxd_status_ok if successful.

Definition at line 418 of file amxd_dm.c.

418  {
420 
421  when_null(dm, exit);
422  when_null(object, exit);
423 
424  retval = amxd_object_add_object(&dm->object, object);
425  if(retval == 0) {
426  amxd_dm_event("dm:root-added", object, NULL, false);
427  }
428 
429 exit:
430  return retval;
431 }
void PRIVATE amxd_dm_event(const char *signal, const amxd_object_t *const object, amxc_var_t *const data, bool trigger)
Definition: amxd_dm_priv.c:550
enum _amxd_status amxd_status_t
@ amxd_status_unknown_error
Definition: amxd_types.h:79
amxd_status_t amxd_object_add_object(amxd_object_t *const parent, amxd_object_t *const child)
Adds an object in the data model tree.
Definition: amxd_object.c:207
amxd_object_t object
Definition: amxd_types.h:260
static amxd_dm_t dm

◆ amxd_dm_clean()

void amxd_dm_clean ( amxd_dm_t dm)

Cleans a data model structure.

This function frees the full data model itself. It is not needed to free all individual allocated objects that are part of the data model object hierarchy.

Parameters
dmthe pointer to the data model structure

Definition at line 365 of file amxd_dm.c.

365  {
366  when_null(dm, exit);
367 
371  amxp_sigmngr_clean(&dm->sigmngr);
372  amxc_llist_clean(&dm->mibs, amxd_dm_free_mib_object_it);
373 
374 exit:
375  return;
376 }
static void amxd_dm_free_mib_object_it(amxc_llist_it_t *it)
Definition: amxd_dm.c:87
static amxd_status_t amxd_dm_invoke_action_impl(amxc_llist_t *fns, amxd_object_t *object, amxd_param_t *param, amxd_action_t reason, const amxc_var_t *const args, amxc_var_t *const retval)
Definition: amxd_dm.c:92
void PRIVATE amxd_dm_cancel_deferred(amxd_dm_t *dm)
PRIVATE void amxd_object_clean(amxd_object_t *const object)
@ action_object_destroy
Definition: amxd_types.h:125
amxc_llist_t mibs
Definition: amxd_types.h:262
amxp_signal_mngr_t sigmngr
Definition: amxd_types.h:261
amxc_llist_t cb_fns
Definition: amxd_types.h:253

◆ amxd_dm_delete()

void amxd_dm_delete ( amxd_dm_t **  dm)

Deletes a data model structure.

This function frees the allocated memory for the data model structure and the full data model itself. It is not needed to free all individual allocated objects that are part of the data model object hierarchy.

Note
When calling this function all store references to the data model pointer become invalid.
Parameters
dma pointer to the location where the pointer to the data model is store.

Definition at line 321 of file amxd_dm.c.

321  {
322  when_null(dm, exit);
323  when_null((*dm), exit);
324 
325  amxd_dm_clean((*dm));
326 
327  free((*dm));
328  *dm = NULL;
329 
330 exit:
331  return;
332 }
void amxd_dm_clean(amxd_dm_t *dm)
Cleans a data model structure.
Definition: amxd_dm.c:365

◆ amxd_dm_get_root()

amxd_object_t* amxd_dm_get_root ( amxd_dm_t *const  dm)

Fetches the root object of the data model.

The root object has no name, it is used to add other objects to the data model using amxd_object_add_object

Parameters
dmthe pointer to the data model structure
Returns
Returns the pointer to the root object of the data model or NULL if the data model pointer (dm) is NULL.

Definition at line 456 of file amxd_dm.c.

456  {
457  amxd_object_t* object = NULL;
458 
459  when_null(dm, exit);
460  object = &dm->object;
461 
462 exit:
463  return object;
464 }

◆ amxd_dm_init()

amxd_status_t amxd_dm_init ( amxd_dm_t dm)

Initializes a data model structure.

Typically only one data model is allocated/created for an application, preferable as a static variable, and initialized with this function

Note
To build a dynamic data model, some parts are allocated on the heap. To make sure the all allocated memory is freed, it is best to clean the created data model using amxd_dm_clean.
Parameters
dmthe pointer to the data model structure
Returns
Returns amxd_status_ok if successful.

Definition at line 334 of file amxd_dm.c.

334  {
336 
337  when_null(dm, exit);
338 
339  amxd_init_base();
340 
341  when_failed(amxd_object_init(&dm->object, amxd_object_root, NULL, NULL, NULL), exit);
342  when_failed(amxp_sigmngr_init(&dm->sigmngr), exit);
343  when_failed(amxc_llist_init(&dm->mibs), exit);
344  when_failed(amxc_llist_init(&dm->deferred), exit);
345 
346  when_failed(amxp_sigmngr_add_signal(&dm->sigmngr, "dm:root-added"), exit);
347  when_failed(amxp_sigmngr_add_signal(&dm->sigmngr, "dm:root-removed"), exit);
348  when_failed(amxp_sigmngr_add_signal(&dm->sigmngr, "dm:object-added"), exit);
349  when_failed(amxp_sigmngr_add_signal(&dm->sigmngr, "dm:object-removed"), exit);
350  when_failed(amxp_sigmngr_add_signal(&dm->sigmngr, "dm:instance-added"), exit);
351  when_failed(amxp_sigmngr_add_signal(&dm->sigmngr, "dm:instance-removed"), exit);
352  when_failed(amxp_sigmngr_add_signal(&dm->sigmngr, "dm:object-changed"), exit);
353  when_failed(amxp_sigmngr_add_signal(&dm->sigmngr, "dm:periodic-inform"), exit);
354  when_failed(amxp_sigmngr_add_signal(&dm->sigmngr, "dm:mib-added"), exit);
355  when_failed(amxp_sigmngr_add_signal(&dm->sigmngr, "dm:mib-removed"), exit);
356  when_failed(amxp_sigmngr_add_signal(&dm->sigmngr, "app:start"), exit);
357  when_failed(amxp_sigmngr_add_signal(&dm->sigmngr, "app:stop"), exit);
358 
359  retval = amxd_status_ok;
360 
361 exit:
362  return retval;
363 }
void PRIVATE amxd_init_base(void)
Definition: amxd_dm_priv.c:665
PRIVATE amxd_status_t amxd_object_init(amxd_object_t *const object, const amxd_object_type_t type, const char *name, amxc_var_t *templ_params, amxc_var_t *values)
@ amxd_status_ok
Definition: amxd_types.h:78
@ amxd_object_root
Definition: amxd_types.h:178
amxc_llist_t deferred
Definition: amxd_types.h:264

◆ amxd_dm_new()

amxd_status_t amxd_dm_new ( amxd_dm_t **  dm)

Instantiate a new data model.

Allocates and initializes memory to store a data model object hierarchy. This function allocates memory from the heap,

Typically only one data model is allocated/created for an application, preferable as a static variable, see amxd_dm_init

Note
The allocated memory must be freed when not used anymore, use amxd_dm_delete to free the memory
Parameters
dma pointer to the location where the pointer to the new data model structure can be stored
Returns
Returns amxd_status_ok if successful.

Definition at line 306 of file amxd_dm.c.

306  {
308 
309  when_null(dm, exit);
310  when_not_null((*dm), exit);
311 
312  *dm = (amxd_dm_t*) calloc(1, sizeof(amxd_dm_t));
313  when_null((*dm), exit);
314 
315  retval = amxd_dm_init((*dm));
316 
317 exit:
318  return retval;
319 }
amxd_status_t amxd_dm_init(amxd_dm_t *dm)
Initializes a data model structure.
Definition: amxd_dm.c:334

◆ amxd_dm_remove_root_object()

amxd_status_t amxd_dm_remove_root_object ( amxd_dm_t *const  dm,
const char *  name 
)

Removes an object from the root of the data model.

This function removes the object from the data model root and does delete the memory allocated for the object.

After calling this function the object is not accessible anymore.

Parameters
dmthe pointer to the data model structure
namethe name of the object
Returns
Returns amxd_status_ok if successful.

Definition at line 433 of file amxd_dm.c.

433  {
435  amxd_object_t* object = NULL;
436  amxc_var_t data;
437 
438  amxc_var_init(&data);
439 
440  when_null(dm, exit);
441  when_str_empty_status(name, exit, retval = amxd_status_invalid_name);
442 
443  object = amxd_object_get_child(&dm->object, name);
444  when_null_status(object, exit, retval = amxd_status_object_not_found);
445 
446  amxd_dm_event("dm:root-removed", object, NULL, false);
447 
448  amxd_object_delete(&object);
449 
450  retval = amxd_status_ok;
451 exit:
452  amxc_var_clean(&data);
453  return retval;
454 }
@ amxd_status_invalid_name
Definition: amxd_types.h:86
@ amxd_status_object_not_found
Definition: amxd_types.h:80
amxd_object_t * amxd_object_get_child(const amxd_object_t *object, const char *name)
Get a child of the object.
void amxd_object_delete(amxd_object_t **object)
Invokes the destroy handler(s) of the object.

◆ amxd_dm_signal_get_object()

amxd_object_t* amxd_dm_signal_get_object ( amxd_dm_t *const  dm,
const amxc_var_t *const  signal_data 
)

Get the object from a data model using the path in the recieved signal.

The data model emits amxp signals (see libamxp) for different kind of events, like object added, object deleted, ...

Each of these events contain an object path, with this function the object referenced by the path in the signal is retrieved,

If you need the path to the object in the data model use amxd_dm_signal_get_path.

Note
  • For all object deleted signals, it is not possible to retrieve the object. The object will be removed and deleted when the signal is recieved.
  • To be able to get the data model signals, you need to have an event loop and connect to the signals you are interested in.
Parameters
dmthe pointer to the data model structure
signal_datathe signal data
Returns
Pointer to the data model object or NULL.

Definition at line 573 of file amxd_dm.c.

574  {
575  amxd_object_t* object = NULL;
576  const char* path = NULL;
577 
578  when_null(dm, exit);
579  when_null(signal_data, exit);
580 
581  path = amxd_dm_signal_get_path(dm, signal_data);
582  when_null(path, exit);
584  "%s",
585  path);
586 
587 exit:
588  return object;
589 }
amxd_object_t * amxd_dm_get_root(amxd_dm_t *const dm)
Fetches the root object of the data model.
Definition: amxd_dm.c:456
const char * amxd_dm_signal_get_path(amxd_dm_t *const dm, const amxc_var_t *const signal_data)
Get the object path from a data model signal.
Definition: amxd_dm.c:560
amxd_object_t * amxd_object_findf(amxd_object_t *object, const char *rel_path,...) __attribute__((format(printf
Find an object in the data model tree, starting from an object.

◆ amxd_dm_signal_get_path()

amxd_object_t amxd_status_t amxd_status_t const char* amxd_dm_signal_get_path ( amxd_dm_t *const  dm,
const amxc_var_t *const  signal_data 
)

Get the object path from a data model signal.

The data model emits amxp signals (see libamxp) for different kind of events, like object added, object deleted, ...

Each of these events contain an object path, with this function the path can be extracted from the signal.

If you need the pointer to the object in the data model use amxd_dm_signal_get_object.

Note
  • For all object deleted signals, it is not possible to retrieve the object. The object will be removed and deleted when the signal is recieved.
  • To be able to get the data model signals, you need to have an event loop and connect to the signals you are interested in.
Parameters
dmthe pointer to the data model structure
signal_datathe signal data
Returns
The path to the object, or NULL

Definition at line 560 of file amxd_dm.c.

561  {
562  amxc_var_t* object_path = NULL;
563 
564  when_null(dm, exit);
565  when_null(signal_data, exit);
566 
567  object_path = amxc_var_get_key(signal_data, "path", 0);
568 
569 exit:
570  return amxc_var_constcast(cstring_t, object_path);
571 }