bbf: add a header file for libbbf_api

This commit is contained in:
Amin Ben Ramdhane 2021-12-13 17:43:55 +01:00
parent ed63695485
commit 65939b90aa
13 changed files with 1447 additions and 578 deletions

132
README.md
View file

@ -234,140 +234,20 @@ In this function, there are two functions that need to be defined:
> Note3: you can use [bbf_test plugin](./test/bbf_test/bbf_test.c) as a reference in order to develop any new object/leaf/browse.
## BBF API
## LIBBBF API
`libbbf_api` is a library which contains the source code of all API functions (UCI, Ubus, JSON, CLI and memory management). these API are used for GET/SET/ADD/Delete/Operate calls which can be called in internal or external packages.
The most used one are as follow:
All APIs exposed by libbbf_api are presented in this header file [libbbf_api.h](./include/libbbf_api.h).
#### 1. dmuci_get_option_value_string: execute the uci get value
## LIBBBF UBUS
```bash
int dmuci_get_option_value_string(char *package, char *section, char *option, char **value)
```
**Argument:**
- **package:** package name
- **section:** section name
- **option:** option name
- **value:** the value of the returned option
`Libbbf_ubus` is a library that provides APIs to expose the datamodel constructed with the help of libbbf API over the ubus directly.
#### 2. dmuci_get_value_by_section_string: execute the uci get value
```bash
int dmuci_get_value_by_section_string(struct uci_section *s, char *option, char **value)
```
**Argument:**
- **section:** section name
- **option:** option name
- **value:** the value of the returned option
#### 3. uci_foreach_sections: browse all sections by package and section type
```bash
#define uci_foreach_sections(package, stype, section)
```
**Argument:**
- **package:** package name
- **stype:** section type to browse
- **section:** return section pointer for each loop iteration
#### 4. dmubus_call: execute the ubus call
```bash
int dmubus_call(char *obj, char *method, struct ubus_arg u_args[], int u_args_size, json_object **req_res)
```
**Argument:**
- **obj:** ubus obj
- **method:** ubus method
- **u_args:** ubus arguments
- **u_args_size:** number of ubus arguments
- **req_res:** the json message of the ubus call
#### 5. dmubus_call_set: set the ubus call
```bash
int dmubus_call_set(char *obj, char *method, struct ubus_arg u_args[], int u_args_size);
```
**Argument:**
- **obj:** ubus obj
- **method:** ubus method
- **u_args: ubus** arguments
- **u_args_size:** number of ubus arguments
#### 6. handle_instance: allow to retrieve/attribute the instances from uci config sections
```bash
char *handle_instance(struct dmctx *dmctx, DMNODE *parent_node, struct uci_section *s, char *inst_opt, char *alias_opt);
```
**Argument:**
- **dmctx:** the current dmctx struct passed when calling this object
- **parent_node:** the current node struct passed when calling this object
- **s:** the uci section used to get the instance
- **inst_opt:** the option name of the instance number used for this object
- **alias_opt:** the option name of the instance alias used for this object
#### 7. handle_instance_without_section: allow to attribute instances with constant values
```bash
char *handle_instance_without_section(struct dmctx *dmctx, DMNODE *parent_node, int inst_nbr);
```
**Argument:** the current dmctx struct passed when calling this object
- **dmctx:** the current dmctx struct passed when calling this object
- **parent_node:** the current node struct passed when calling this object
- **inst_nbr:** the instance to attribute for this object
#### 8. DM_LINK_INST_OBJ: link the instance to the data model tree
```bash
int DM_LINK_INST_OBJ(struct dmctx *dmctx, DMNODE *parent_node, void *data, char *instance)
```
**Argument:**
- **dmctx:** the current dmctx struct passed when calling this object
- **parent_node:** the current node struct passed when calling this object
- **data:** the data transmitted for the next sub object and parameters that can be uci section, json object, or any type of data
- **instance:** the current instance used for this object
All APIs exposed by libbbf_ubus are presented in this header file [libbbf_ubus.h](./include/libbbf_ubus.h).
> Note1: For other funtions, please refer to dmuci, dmubus, dmjson, dmcommon and dmmem (.c and .h) files in the [link](https://dev.iopsys.eu/iopsys/bbf/-/tree/devel/libbbf_api)
> Note2: When developing a new parameters/features in the Data Model, it's highly recommended to use the memory management functions of `libbbf_api` allocate and free because it's freed at the end of each RPCs.
The list of memory management functions of `libbbf_api` are:
```bash
dmmalloc(x)
dmcalloc(n, x)
dmrealloc(x, n)
dmstrdup(x)
dmasprintf(s, format, ...)
dmastrcat(s, b, m)
dmfree(x)
```
> Note3: There are several APIs that have been deprecated and replaced with new ones. the table below summarizes them
| Deprecated API | New API |
| -------------------------------------- | -------------------------------------------------- |
| handle_update_instance | handle_instance or handle_instance_without_section |
| update_instance_alias | handle_instance |
| update_instance_without_section | handle_instance_without_section |
| update_instance | Not Used |
| get_last_instance_bbfdm | Not Used |
| get_last_instance | find_max_instance |
| get_last_instance_lev2_bbfdm_dmmap_opt | Not Used |
| get_last_instance_lev2_bbfdm | Not Used |
| is_section_unnamed | Not Used |
| delete_sections_save_next_sections | Not Used |
| update_dmmap_sections | Not Used |
| check_browse_section | Not Used |
| dmuci_delete_by_section_unnamed | dmuci_delete_by_section |
| dmuci_delete_by_section_unnamed_bbfdm | dmuci_delete_by_section |
> Note: Anyone wants to check out libbbf_api or libbbf_ubus APIs and how to use them, all documentation will be available in their header files [libbbf_api.h](./include/libbbf_api.h) and [libbbf_ubus.h](./include/libbbf_ubus.h).
## BBFDM Vendor

View file

@ -6,7 +6,8 @@ libbbf_api_la_SOURCES = \
../libbbf_api/dmjson.c \
../libbbf_api/dmuci.c \
../libbbf_api/dmcommon.c \
../libbbf_api/dmmem.c
../libbbf_api/dmmem.c \
../libbbf_api/dmapi.c
libbbf_api_la_CFLAGS = \
$(AM_CFLAGS) \

View file

@ -634,10 +634,10 @@ static char *uci_get_value(json_object *mapping_obj, int json_version, char *ref
} else {
char uci_type[32] = {0};
snprintf(uci_type, sizeof(uci_type), "@%s[%d]", json_object_get_string(type), instance ? atoi(instance)-1 : 0);
value = bbf_uci_get_value(json_object_get_string(path), json_object_get_string(file), uci_type, json_object_get_string(option_name));
value = dmuci_get_value_by_path(json_object_get_string(path), json_object_get_string(file), uci_type, json_object_get_string(option_name));
}
} else if (file && section_name && option_name) {
value = bbf_uci_get_value(json_object_get_string(path), json_object_get_string(file), json_object_get_string(section_name), json_object_get_string(option_name));
value = dmuci_get_value_by_path(json_object_get_string(path), json_object_get_string(file), json_object_get_string(section_name), json_object_get_string(option_name));
}
if (strstr(refparam, "Alias") && value[0] == '\0')
@ -1170,9 +1170,9 @@ static void uci_set_value(json_object *mapping_obj, int json_version, char *refp
char uci_type[32] = {0};
snprintf(uci_type, sizeof(uci_type), "@%s[%d]", json_object_get_string(type), instance ? atoi(instance)-1 : 0);
bbf_uci_set_value(json_object_get_string(path), json_object_get_string(file), uci_type, json_object_get_string(option_name), value);
dmuci_set_value_by_path(json_object_get_string(path), json_object_get_string(file), uci_type, json_object_get_string(option_name), value);
} else if (file && section_name && option_name) {
bbf_uci_set_value(json_object_get_string(path), json_object_get_string(file), json_object_get_string(section_name), json_object_get_string(option_name), value);
dmuci_set_value_by_path(json_object_get_string(path), json_object_get_string(file), json_object_get_string(section_name), json_object_get_string(option_name), value);
}
}

701
include/libbbf_api.h Normal file
View file

@ -0,0 +1,701 @@
/*
* Copyright (C) 2021 IOPSYS Software Solutions AB
*
* Author: Amin Ben Ramdhane <amin.benramdhane@pivasoftware.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
/**
* \file libbbf_api.h
*
* This Library provides APIs for UCI, UBUS, JSON and memory management.
*/
#ifndef __LIBBBF_API_H__
#define __LIBBBF_API_H__
#include <uci.h>
#include <libubox/list.h>
#include <json-c/json.h>
#include "libbbf_api/dmapi.h"
/*******************
*
* BBF UCI API
*
******************/
#define bbf_uci_foreach_sections(package, stype, section) \
for (section = bbf_uci_walk_section(package, stype, NULL, NULL, CMP_SECTION, NULL, NULL, GET_FIRST_SECTION); \
section != NULL; \
section = bbf_uci_walk_section(package, stype, NULL, NULL, CMP_SECTION, NULL, section, GET_NEXT_SECTION))
#define bbf_uci_foreach_sections_safe(package, stype, _tmp, section) \
for (section = bbf_uci_walk_section(package, stype, NULL, NULL, CMP_SECTION, NULL, NULL, GET_FIRST_SECTION), \
_tmp = (section) ? bbf_uci_walk_section(package, stype, NULL, NULL, CMP_SECTION, NULL, section, GET_NEXT_SECTION) : NULL; \
section != NULL; \
section = _tmp, _tmp = (section) ? bbf_uci_walk_section(package, stype, NULL, NULL, CMP_SECTION, NULL, section, GET_NEXT_SECTION) : NULL)
#define bbf_uci_foreach_option_eq(package, stype, option, val, section) \
for (section = bbf_uci_walk_section(package, stype, option, val, CMP_OPTION_EQUAL, NULL, NULL, GET_FIRST_SECTION); \
section != NULL; \
section = bbf_uci_walk_section(package, stype, option, val, CMP_OPTION_EQUAL, NULL, section, GET_NEXT_SECTION))
#define bbf_uci_foreach_option_eq_safe(package, stype, option, val, _tmp, section) \
for (section = bbf_uci_walk_section(package, stype, option, val, CMP_OPTION_EQUAL, NULL, NULL, GET_FIRST_SECTION), \
_tmp = (section) ? bbf_uci_walk_section(package, stype, option, val, CMP_OPTION_EQUAL, NULL, section, GET_NEXT_SECTION) : NULL; \
section != NULL; \
section = _tmp, _tmp = (section) ? bbf_uci_walk_section(package, stype, option, val, CMP_OPTION_EQUAL, NULL, section, GET_NEXT_SECTION) : NULL)
#define section_name(s) s ? (s)->e.name : ""
#define section_type(s) s ? (s)->type : ""
#define section_config(s) s ? (s)->package->e.name : ""
/*********************************************************************//**
**
** bbf_uci_add_section
**
** This API is to add a new unnamed section under the default uci config path('/etc/config/')
**
** \param package - package name to add the section
** \param type - section type name
** \param s - pointer to store a reference to the new section in
**
** \return 0 if the operation is successful, -1 otherwise
**
**************************************************************************/
int bbf_uci_add_section(char *package, char *type, struct uci_section **s);
/*********************************************************************//**
**
** bbf_uci_delete_section
**
** This API is to delete a section or option under the default uci config path('/etc/config/')
**
** \param package - package name to delete the section
** \param type - section type name
** \param option - option name
** \param value - not used (must be removed later)
**
** \return 0 if the operation is successful, -1 otherwise
**
**************************************************************************/
int bbf_uci_delete_section(char *package, char *type, char *option, char *value);
/*********************************************************************//**
**
** bbf_uci_add_section_bbfdm
**
** This API is to add a new unnamed section under the path '/etc/bbfdm/dmmap/'
**
** \param package - package name to add the section
** \param type - section type name
** \param s - pointer to store a reference to the new section in
**
** \return 0 if the operation is successful, -1 otherwise
**
**************************************************************************/
int bbf_uci_add_section_bbfdm(char *package, char *type, struct uci_section **s);
/*********************************************************************//**
**
** bbf_uci_delete_section_bbfdm
**
** This API is to delete a section or option under the path '/etc/bbfdm/dmmap/'
**
** \param package - package name to delete the section
** \param type - section type name
** \param option - option name
** \param value - not used (must be removed later)
**
** \return 0 if the operation is successful, -1 otherwise
**
**************************************************************************/
int bbf_uci_delete_section_bbfdm(char *package, char *type, char *option, char *value);
/*********************************************************************//**
**
** bbf_uci_rename_section
**
** This API is to rename the section name
**
** \param s - pointer to the uci section to rename
** \param value - new uci section name
**
** \return 0 if the operation is successful, -1 otherwise
**
**************************************************************************/
int bbf_uci_rename_section(struct uci_section *s, char *value);
/*********************************************************************//**
**
** bbf_uci_get_value
**
** This API is to get an uci option value
**
** \param package - package name
** \param section - section name
** \param option - option name
** \param value - pointer to the option value
**
** \return 0 if the operation is successful, -1 otherwise
**
**************************************************************************/
int bbf_uci_get_value(char *package, char *section, char *option, char **value);
/*********************************************************************//**
**
** bbf_uci_set_value
**
** This API is to set an uci option value
**
** NOTE: the option will be created if it does not exist
**
** \param package - package name
** \param section - section name
** \param option - option name
** \param value - value to set to the option
**
** \return 0 if the operation is successful, -1 otherwise
**
**************************************************************************/
int bbf_uci_set_value(char *package, char *section, char *option, char *value);
/*********************************************************************//**
**
** bbf_uci_get_value_by_section
**
** This API is to get an uci option value from the section pointer of uci context
**
** \param s - section pointer of uci context
** \param option - option name
** \param value - pointer to the option value
**
** \return 0 if the operation is successful, -1 otherwise
**
**************************************************************************/
int bbf_uci_get_value_by_section(struct uci_section *s, char *option, char **value);
/*********************************************************************//**
**
** bbf_uci_get_value_by_section_fallback_def
**
** This API is to get an uci option value from the section pointer of uci context and
** return the default value if uci option value is empty
**
** \param s - section pointer of uci context
** \param option - option name
** \param default_value - default value to return if the uci option value is empty
**
** \return uci option value if the value is not empty, empty otherwise
**
**************************************************************************/
char *bbf_uci_get_value_by_section_fallback_def(struct uci_section *s, char *option, char *default_value);
/*********************************************************************//**
**
** bbf_uci_set_value_by_section
**
** This API is to set an uci option value from the section pointer of uci context
**
** \param s - section pointer of uci context
** \param option - option name
** \param value - value to set to the option
**
** \return 0 if the operation is successful, -1 otherwise
**
**************************************************************************/
int bbf_uci_set_value_by_section(struct uci_section *s, char *option, char *value);
/*********************************************************************//**
**
** bbf_uci_delete_section_by_section
**
** This API is to delete a section or option from the section pointer of uci context
**
** \param s - section pointer of uci context
** \param option - option name
** \param value - not used (must be removed later)
**
** \return 0 if the operation is successful, -1 otherwise
**
**************************************************************************/
int bbf_uci_delete_section_by_section(struct uci_section *s, char *option, char *value);
struct uci_section *bbf_uci_walk_section(char *package, char *type, void *arg1, void *arg2, int cmp, int (*filter)(struct uci_section *s, void *value), struct uci_section *prev_section, int walk);
/*******************
*
* BBF UBUS API
*
******************/
/*********************************************************************//**
**
** bbf_ubus_call
**
** This API is to get the json output of ubus call
**
** \param obj - ubus object name
** \param method - ubus method name
** \param u_args - ubus arguments
** \param u_args_size - number of ubus arguments
** \param req_res - pointer to the json object message. it could be NULL if the json object is not found
**
** \return 0 if the operation is successful, -1 otherwise
**
**************************************************************************/
int bbf_ubus_call(char *obj, char *method, struct ubus_arg u_args[], int u_args_size, json_object **req_res);
/*********************************************************************//**
**
** bbf_ubus_call_set
**
** This API is to execute the ubus call without getting the json output
**
** \param obj - ubus object name
** \param method - ubus method name
** \param u_args - ubus arguments
** \param u_args_size - number of ubus arguments
**
** \return 0 if the operation is successful, -1 otherwise
**
**************************************************************************/
int bbf_ubus_call_set(char *obj, char *method, struct ubus_arg u_args[], int u_args_size);
/*******************
*
* BBF MEMORY MANAGEMENT API
*
******************/
/*********************************************************************//**
**
** bbf_malloc
**
** This API is to allocate the requested memory using malloc().
**
** \param size - number of bytes to allocate
**
** \return pointer to the allocated memory if the operation is successful, NULL otherwise
**
**************************************************************************/
void *bbf_malloc(size_t size);
/*********************************************************************//**
**
** bbf_calloc
**
** This API is to allocate the requested memory using calloc().
**
** \param nitems - number of elements to allocate
** \param size - size of elements
**
** \return pointer to the allocated memory if the operation is successful, NULL otherwise
**
**************************************************************************/
void *bbf_calloc(int nitems, size_t size);
/*********************************************************************//**
**
** bbf_realloc
**
** This API is to reallocate the memory using realloc().
**
** \param ptr - pointer to current buffer than needs reallocating
** \param size - number of bytes to reallocate
**
** \return pointer to the reallocated memory if the operation is successful, NULL otherwise
**
**************************************************************************/
void *bbf_realloc(void *ptr, size_t size);
/*********************************************************************//**
**
** bbf_strdup
**
** This API is to copy a specific number of bytes from a string using malloc() and memcpy().
**
** NOTE: This function treats a NULL input string, as a NULL output
**
** \param ptr - pointer to buffer containing string to copy
**
** \return pointer to the new string if the operation is successful, NULL otherwise
**
**************************************************************************/
char *bbf_strdup(const char *ptr);
/*******************
*
* BBF API
*
******************/
/*********************************************************************//**
**
** bbf_synchronise_config_sections_with_dmmap
**
** This API is to synchronise uci sections under the '/etc/config/' path with dmmap uci sections under the '/etc/bbfdm/dmmap/' path
**
** NOTE: bbf_free_config_sections_list should be called to free
** the allocated resources used in this API.
**
** \param package - package name
** \param section_type - section type name
** \param dmmap_package - dmmap package name
** \param dup_list - pointer to the list of all sections
**
**************************************************************************/
void bbf_synchronise_config_sections_with_dmmap(char *package, char *section_type, char *dmmap_package, struct list_head *dup_list);
/*********************************************************************//**
**
** bbf_free_config_sections_list
**
** This API is to free the allocated resources used in bbf_synchronise_config_sections_with_dmmap function
**
** \param dup_list - pointer to the list of all sections
**
**************************************************************************/
void bbf_free_config_sections_list(struct list_head *dup_list);
/*********************************************************************//**
**
** bbf_handle_instance
**
** This API is to allow to retrieve/attribute the instances number/alias from uci config sections depending of the request and the instance mode
**
** \param dmctx - pointer to the bbf context
** \param parent_node - pointer to the parent node of the object
** \param s - pointer to the dmmap section
** \param inst_opt - instance option name
** \param alias_opt - alias option name
**
** \return pointer to the instance value if the operation is successful, empty otherwise
**
**************************************************************************/
char *bbf_handle_instance(struct dmctx *dmctx, struct dmnode *parent_node, struct uci_section *s, char *inst_opt, char *alias_opt);
/*********************************************************************//**
**
** bbf_link_instance_object
**
** This API is to link the instance to the data model tree
**
** \param ctx - pointer to the bbf context
** \param parent_node - pointer to the parent node of the object
** \param data - pointer to the data passed to the sub object and parameters
** \param instance - the current instance linked to the object
**
** \return 0 if the operation is successful, -1 otherwise
**
**************************************************************************/
int bbf_link_instance_object(struct dmctx *ctx, struct dmnode *parent_node, void *data, char *instance);
/*********************************************************************//**
**
** bbf_get_number_of_entries
**
** This API is to get the number of entries for multi-intance object
**
** \param ctx - bbf context
** \param data - the data passed from the parent object
** \param instance - instance number
** \param browseinstobj - pointer the browse function linked to the object that wants to obtain the instance number
**
** \return number of entries if the operation is successful, 0 otherwise
**
**************************************************************************/
int bbf_get_number_of_entries(struct dmctx *ctx, void *data, char *instance, int (*browseinstobj)(struct dmctx *ctx, struct dmnode *node, void *data, char *instance));
/*********************************************************************//**
**
** bbf_convert_string_to_bool
**
** This API is to convert string to bool value
**
** \param str - pointer to string to convert
** \param b - pointer to bool value
**
** \return bool value if the operation is successful, false otherwise
**
**************************************************************************/
int bbf_convert_string_to_bool(char *str, bool *b);
/*********************************************************************//**
**
** bbf_find_dmmap_section
**
** This API is to find the dmmap section based on the section name of uci config
**
** \param dmmap_package - dmmap package name
** \param section_type - section type
** \param section_name - section name to find
** \param dmmap_section - pointer to the dmmap section, it should be 'NULL' if the section is not found
**
**************************************************************************/
void bbf_find_dmmap_section(char *dmmap_package, char *section_type, char *section_name, struct uci_section **dmmap_section);
/*********************************************************************//**
**
** bbf_find_dmmap_section_by_option
**
** This API is to find the dmmap section based on option_name and option_value
**
** \param dmmap_package - dmmap package name
** \param section_type - section type
** \param option_name - option name
** \param option_value - option value
** \param dmmap_section - pointer to the dmmap section, it should be 'NULL' if the section is not found
**
**************************************************************************/
void bbf_find_dmmap_section_by_option(char *dmmap_package, char *section_type, char *option_name, char *option_value, struct uci_section **dmmap_section);
/*********************************************************************//**
**
** bbf_validate_string
**
** This API is to validate a string value
**
** \param value - pointer to the value to validate
** \param min_length - minimum length allowed, -1 meaning there is no limit
** \param max_length - maximum length allowed, -1 meaning there is no limit
** \param enumeration - pointer to an array of strings to validate the string value based on it, NULL meaning there is no enumeration
** \param pattern - pointer to an array of patterns to validate the string value based on it, NULL meaning there is no pattern
**
** \return 0 if the string value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_string(char *value, int min_length, int max_length, char *enumeration[], char *pattern[]);
/*********************************************************************//**
**
** bbf_validate_boolean
**
** This API is to validate a bool value
**
** \param value - pointer to the value to validate
**
** \return 0 if the bool value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_boolean(char *value);
/*********************************************************************//**
**
** bbf_validate_unsignedInt
**
** This API is to validate an unsigned int value
**
** \param value - pointer to the value to validate
** \param r_args - array of allowed range, 'RANGE_ARGS{{NULL,NULL}}' meaning there is no range
** \param r_args_size - number of allowed range
**
** \return 0 if the unsigned int value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_unsignedInt(char *value, struct range_args r_args[], int r_args_size);
/*********************************************************************//**
**
** bbf_validate_int
**
** This API is to validate a int value
**
** \param value - pointer to the value to validate
** \param r_args - array of allowed range, 'RANGE_ARGS{{NULL,NULL}}' meaning there is no range
** \param r_args_size - number of allowed range
**
** \return 0 if the int value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_int(char *value, struct range_args r_args[], int r_args_size);
/*********************************************************************//**
**
** bbf_validate_unsignedLong
**
** This API is to validate a unsigned long value
**
** \param value - pointer to the value to validate
** \param r_args - array of allowed range, 'RANGE_ARGS{{NULL,NULL}}' meaning there is no range
** \param r_args_size - number of allowed range
**
** \return 0 if the unsigned long value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_unsignedLong(char *value, struct range_args r_args[], int r_args_size);
/*********************************************************************//**
**
** bbf_validate_long
**
** This API is to validate a long value
**
** \param value - pointer to the value to validate
** \param r_args - array of allowed range, 'RANGE_ARGS{{NULL,NULL}}' meaning there is no range
** \param r_args_size - number of allowed range
**
** \return 0 if the long value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_long(char *value, struct range_args r_args[], int r_args_size);
/*********************************************************************//**
**
** bbf_validate_dateTime
**
** This API is to validate a date time value
**
** \param value - pointer to the value to validate
**
** \return 0 if the date time value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_dateTime(char *value);
/*********************************************************************//**
**
** bbf_validate_hexBinary
**
** This API is to validate a hexbinary value
**
** \param value - pointer to the value to validate
** \param r_args - array of allowed range, 'RANGE_ARGS{{NULL,NULL}}' meaning there is no range
** \param r_args_size - number of allowed range
**
** \return 0 if the hexbinary value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_hexBinary(char *value, struct range_args r_args[], int r_args_size);
/*********************************************************************//**
**
** bbf_validate_string_list
**
** This API is to validate a list of string value
**
** \param value - pointer to the value to validate
** \param min_item - minimum item allowed in the list, -1 meaning there is no limit
** \param max_item - maximum item allowed in the list, -1 meaning there is no limit
** \param max_size - maximum length allowed in the list, -1 meaning there is no limit
** \param min - minimum length allowed for each string value, -1 meaning there is no limit
** \param max - maximum length allowed for each string value, -1 meaning there is no limit
** \param enumeration - pointer to an array of strings to validate the string value based on it, NULL meaning there is no enumeration
** \param pattern - pointer to an array of patterns to validate the string value based on it, NULL meaning there is no pattern
**
** \return 0 if the list of string value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_string_list(char *value, int min_item, int max_item, int max_size, int min, int max, char *enumeration[], char *pattern[]);
/*********************************************************************//**
**
** bbf_validate_unsignedInt_list
**
** This API is to validate a list of unsigned int value
**
** \param value - pointer to the value to validate
** \param min_item - minimum item allowed in the list, -1 meaning there is no limit
** \param max_item - maximum item allowed in the list, -1 meaning there is no limit
** \param max_size - maximum length allowed in the list, -1 meaning there is no limit
** \param r_args - array of allowed range, 'RANGE_ARGS{{NULL,NULL}}' meaning there is no range
** \param r_args_size - number of allowed range
**
** \return 0 if the list of unsigned int value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_unsignedInt_list(char *value, int min_item, int max_item, int max_size, struct range_args r_args[], int r_args_size);
/*********************************************************************//**
**
** bbf_validate_int_list
**
** This API is to validate a list of int value
**
** \param value - pointer to the value to validate
** \param min_item - minimum item allowed in the list, -1 meaning there is no limit
** \param max_item - maximum item allowed in the list, -1 meaning there is no limit
** \param max_size - maximum length allowed in the list, -1 meaning there is no limit
** \param r_args - array of allowed range, 'RANGE_ARGS{{NULL,NULL}}' meaning there is no range
** \param r_args_size - number of allowed range
**
** \return 0 if the list of int value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_int_list(char *value, int min_item, int max_item, int max_size, struct range_args r_args[], int r_args_size);
/*********************************************************************//**
**
** bbf_validate_unsignedLong_list
**
** This API is to validate a list of unsigned long value
**
** \param value - pointer to the value to validate
** \param min_item - minimum item allowed in the list, -1 meaning there is no limit
** \param max_item - maximum item allowed in the list, -1 meaning there is no limit
** \param max_size - maximum length allowed in the list, -1 meaning there is no limit
** \param r_args - array of allowed range, 'RANGE_ARGS{{NULL,NULL}}' meaning there is no range
** \param r_args_size - number of allowed range
**
** \return 0 if the list of unsigned long value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_unsignedLong_list(char *value, int min_item, int max_item, int max_size, struct range_args r_args[], int r_args_size);
/*********************************************************************//**
**
** bbf_validate_long_list
**
** This API is to validate a list of long value
**
** \param value - pointer to the value to validate
** \param min_item - minimum item allowed in the list, -1 meaning there is no limit
** \param max_item - maximum item allowed in the list, -1 meaning there is no limit
** \param max_size - maximum length allowed in the list, -1 meaning there is no limit
** \param r_args - array of allowed range, 'RANGE_ARGS{{NULL,NULL}}' meaning there is no range
** \param r_args_size - number of allowed range
**
** \return 0 if the list of long value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_long_list(char *value, int min_item, int max_item, int max_size, struct range_args r_args[], int r_args_size);
/*********************************************************************//**
**
** bbf_validate_hexBinary_list
**
** This API is to validate a list of hexBinary value
**
** \param value - pointer to the value to validate
** \param min_item - minimum item allowed in the list, -1 meaning there is no limit
** \param max_item - maximum item allowed in the list, -1 meaning there is no limit
** \param max_size - maximum length allowed in the list, -1 meaning there is no limit
** \param r_args - array of allowed range, 'RANGE_ARGS{{NULL,NULL}}' meaning there is no range
** \param r_args_size - number of allowed range
**
** \return 0 if the list of hexBinary value is valid, -1 otherwise
**
**************************************************************************/
int bbf_validate_hexBinary_list(char *value, int min_item, int max_item, int max_size, struct range_args r_args[], int r_args_size);
#endif //__LIBBBF_API_H__

249
libbbf_api/dmapi.c Normal file
View file

@ -0,0 +1,249 @@
/*
* Copyright (C) 2021 IOPSYS Software Solutions AB
*
* Author: Amin Ben Ramdhane <amin.benramdhane@pivasoftware.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "dmcommon.h"
/**
*
* BBF UCI API
*
*/
int bbf_uci_add_section(char *package, char *type, struct uci_section **s)
{
return dmuci_add_section(package, type, s);
}
int bbf_uci_delete_section(char *package, char *type, char *option, char *value)
{
return dmuci_delete(package, type, option, value);
}
int bbf_uci_add_section_bbfdm(char *package, char *type, struct uci_section **s)
{
return dmuci_add_section_bbfdm(package, type, s);
}
int bbf_uci_delete_section_bbfdm(char *package, char *type, char *option, char *value)
{
return dmuci_delete_bbfdm(package, type, option, value);
}
int bbf_uci_rename_section(struct uci_section *s, char *value)
{
return dmuci_rename_section_by_section(s, value);
}
int bbf_uci_get_value(char *package, char *section, char *option, char **value)
{
return dmuci_get_option_value_string(package, section, option, value);
}
int bbf_uci_set_value(char *package, char *section, char *option, char *value)
{
return dmuci_set_value(package, section, option, value);
}
int bbf_uci_get_value_by_section(struct uci_section *s, char *option, char **value)
{
return dmuci_get_value_by_section_string(s, option, value);
}
char *bbf_uci_get_value_by_section_fallback_def(struct uci_section *s, char *option, char *default_value)
{
return dmuci_get_value_by_section_fallback_def(s, option, default_value);
}
int bbf_uci_set_value_by_section(struct uci_section *s, char *option, char *value)
{
return dmuci_set_value_by_section(s, option, value);
}
int bbf_uci_delete_section_by_section(struct uci_section *s, char *option, char *value)
{
return dmuci_delete_by_section(s, option, value);
}
struct uci_section *bbf_uci_walk_section(char *package, char *type, void *arg1, void *arg2, int cmp, int (*filter)(struct uci_section *s, void *value), struct uci_section *prev_section, int walk)
{
return dmuci_walk_section(package, type, arg1, arg2, cmp, filter, prev_section, walk);
}
/**
*
* BBF UBUS API
*
*/
int bbf_ubus_call(char *obj, char *method, struct ubus_arg u_args[], int u_args_size, json_object **req_res)
{
return dmubus_call(obj, method, u_args, u_args_size, req_res);
}
int bbf_ubus_call_set(char *obj, char *method, struct ubus_arg u_args[], int u_args_size)
{
return dmubus_call_set(obj, method, u_args, u_args_size);
}
/**
*
* BBF MEMORY MANAGEMENT API
*
*/
void *bbf_malloc(size_t size)
{
return dmmalloc(size);
}
void *bbf_calloc(int nitems, size_t size)
{
return dmcalloc(nitems, size);
}
void *bbf_realloc(void *ptr, size_t size)
{
return dmrealloc(ptr, size);
}
char *bbf_strdup(const char *ptr)
{
return dmstrdup(ptr);
}
/**
*
* BBF API
*
*/
void bbf_synchronise_config_sections_with_dmmap(char *package, char *section_type, char *dmmap_package, struct list_head *dup_list)
{
return synchronize_specific_config_sections_with_dmmap(package, section_type, dmmap_package, dup_list);
}
void bbf_free_config_sections_list(struct list_head *dup_list)
{
return free_dmmap_config_dup_list(dup_list);
}
char *bbf_handle_instance(struct dmctx *dmctx, DMNODE *parent_node, struct uci_section *s, char *inst_opt, char *alias_opt)
{
return handle_instance(dmctx, parent_node, s, inst_opt, alias_opt);
}
int bbf_link_instance_object(struct dmctx *dmctx, DMNODE *parent_node, void *data, char *instance)
{
return DM_LINK_INST_OBJ(dmctx, parent_node, data, instance);
}
int bbf_get_number_of_entries(struct dmctx *ctx, void *data, char *instance, int (*browseinstobj)(struct dmctx *ctx, struct dmnode *node, void *data, char *instance))
{
return get_number_of_entries(ctx, data, instance, browseinstobj);
}
int bbf_convert_string_to_bool(char *str, bool *b)
{
return string_to_bool(str, b);
}
void bbf_find_dmmap_section(char *dmmap_package, char *section_type, char *section_name, struct uci_section **dmmap_section)
{
return get_dmmap_section_of_config_section(dmmap_package, section_type, section_name, dmmap_section);
}
void bbf_find_dmmap_section_by_option(char *dmmap_package, char *section_type, char *option_name, char *option_value, struct uci_section **dmmap_section)
{
return get_dmmap_section_of_config_section_eq(dmmap_package, section_type, option_name, option_value, dmmap_section);
}
int bbf_validate_string(char *value, int min_length, int max_length, char *enumeration[], char *pattern[])
{
return dm_validate_string(value, min_length, max_length, enumeration, pattern);
}
int bbf_validate_boolean(char *value)
{
return dm_validate_boolean(value);
}
int bbf_validate_unsignedInt(char *value, struct range_args r_args[], int r_args_size)
{
return dm_validate_unsignedInt(value, r_args, r_args_size);
}
int bbf_validate_int(char *value, struct range_args r_args[], int r_args_size)
{
return dm_validate_int(value, r_args, r_args_size);
}
int bbf_validate_unsignedLong(char *value, struct range_args r_args[], int r_args_size)
{
return dm_validate_unsignedLong(value, r_args, r_args_size);
}
int bbf_validate_long(char *value, struct range_args r_args[], int r_args_size)
{
return dm_validate_long(value, r_args, r_args_size);
}
int bbf_validate_dateTime(char *value)
{
return dm_validate_dateTime(value);
}
int bbf_validate_hexBinary(char *value, struct range_args r_args[], int r_args_size)
{
return dm_validate_hexBinary(value, r_args, r_args_size);
}
int bbf_validate_string_list(char *value, int min_item, int max_item, int max_size, int min, int max, char *enumeration[], char *pattern[])
{
return dm_validate_string_list(value, min_item,max_item, max_size, min, max, enumeration, pattern);
}
int bbf_validate_unsignedInt_list(char *value, int min_item, int max_item, int max_size, struct range_args r_args[], int r_args_size)
{
return dm_validate_unsignedInt_list(value, min_item, max_item, max_size, r_args, r_args_size);
}
int bbf_validate_int_list(char *value, int min_item, int max_item, int max_size, struct range_args r_args[], int r_args_size)
{
return dm_validate_int_list(value, min_item, max_item, max_size, r_args, r_args_size);
}
int bbf_validate_unsignedLong_list(char *value, int min_item, int max_item, int max_size, struct range_args r_args[], int r_args_size)
{
return dm_validate_unsignedLong_list(value, min_item, max_item, max_size, r_args, r_args_size);
}
int bbf_validate_long_list(char *value, int min_item, int max_item, int max_size, struct range_args r_args[], int r_args_size)
{
return dm_validate_long_list(value, min_item, max_item, max_size, r_args, r_args_size);
}
int bbf_validate_hexBinary_list(char *value, int min_item, int max_item, int max_size, struct range_args r_args[], int r_args_size)
{
return dm_validate_hexBinary_list(value, min_item, max_item, max_size, r_args, r_args_size);
}

470
libbbf_api/dmapi.h Normal file
View file

@ -0,0 +1,470 @@
/*
* Copyright (C) 2021 IOPSYS Software Solutions AB
*
* Author: Amin Ben Ramdhane <amin.benramdhane@pivasoftware.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#ifndef __DMAPI_H__
#define __DMAPI_H__
#include <uci.h>
#include <libubox/list.h>
#include <json-c/json.h>
extern struct dm_permession_s DMREAD;
extern struct dm_permession_s DMWRITE;
extern struct dm_permession_s DMSYNC;
extern struct dm_permession_s DMASYNC;
extern char *DMT_TYPE[];
extern int bbfdatamodel_type;
#ifdef UNDEF
#undef UNDEF
#endif
#define UNDEF -1
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif
#ifndef FREE
#define FREE(x) do { if(x) {free(x); x = NULL;} } while (0)
#endif
#ifndef BBF_ATTR_UNUSED
#define BBF_ATTR_UNUSED(x) (void)(x)
#endif
#define DM_STRNCPY(DST, SRC, SIZE) \
do { \
strncpy(DST, SRC, SIZE - 1); \
DST[SIZE-1] = '\0'; \
} while(0)
#define UBUS_ARGS (struct ubus_arg[])
#define RANGE_ARGS (struct range_args[])
#define LIST_KEY (const char *[])
#define DMPARAM_ARGS \
struct dmctx *dmctx, \
struct dmnode *node, \
char *lastname, \
struct dm_permession_s *permission, \
int type, \
int (*get_cmd)(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value), \
int (*set_cmd)(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action), \
void *data, \
char *instance
#define DMOBJECT_ARGS \
struct dmctx *dmctx, \
struct dmnode *node, \
struct dm_permession_s *permission, \
int (*addobj)(char *refparam, struct dmctx *ctx, void *data, char **instance), \
int (*delobj)(char *refparam, struct dmctx *ctx, void *data, char *instance, unsigned char del_action), \
int (*get_linker)(char *refparam, struct dmctx *dmctx, void *data, char *instance, char **linker), \
void *data, \
char *instance
struct dmnode;
struct dmctx;
struct dm_dynamic_obj {
struct dm_obj_s **nextobj;
int idx_type;
};
struct dm_dynamic_leaf {
struct dm_leaf_s **nextleaf;
int idx_type;
};
struct dm_permession_s {
char *val;
char *(*get_permission)(char *refparam, struct dmctx *dmctx, void *data, char *instance);
};
struct dm_notif_s {
char *val;
char *(*get_notif)(char *refparam, struct dmctx *dmctx, void *data, char *instance);
};
typedef struct dm_leaf_s {
/* PARAM, permission, type, getvalue, setvalue, bbfdm_type, version(7)*/
char *parameter;
struct dm_permession_s *permission;
int type;
int (*getvalue)(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value);
int (*setvalue)(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action);
int bbfdm_type;
char version[10];
} DMLEAF;
typedef struct dm_obj_s {
/* OBJ, permission, addobj, delobj, checkdep, browseinstobj, nextdynamicobj, dynamicleaf, nextobj, leaf, linker, bbfdm_type, uniqueKeys, version(14)*/
char *obj;
struct dm_permession_s *permission;
int (*addobj)(char *refparam, struct dmctx *ctx, void *data, char **instance);
int (*delobj)(char *refparam, struct dmctx *ctx, void *data, char *instance, unsigned char del_action);
char *checkdep;
int (*browseinstobj)(struct dmctx *dmctx, struct dmnode *node, void *data, char *instance);
struct dm_dynamic_obj *nextdynamicobj;
struct dm_dynamic_leaf *dynamicleaf;
struct dm_obj_s *nextobj;
struct dm_leaf_s *leaf;
int (*get_linker)(char *refparam, struct dmctx *dmctx, void *data, char *instance, char **linker);
int bbfdm_type;
const char **unique_keys;
char version[10];
} DMOBJ;
struct set_tmp {
struct list_head list;
char *name;
char *value;
};
struct param_fault {
struct list_head list;
char *name;
int fault;
};
struct dm_parameter {
struct list_head list;
char *name;
char *data;
char *type;
char *additional_data;
};
struct dmctx
{
bool stop;
bool match;
int (*method_param)(DMPARAM_ARGS);
int (*method_obj)(DMOBJECT_ARGS);
int (*checkobj)(DMOBJECT_ARGS);
int (*checkleaf)(DMOBJECT_ARGS);
struct list_head list_parameter;
struct list_head set_list_tmp;
struct list_head list_fault_param;
struct list_head list_json_parameter;
DMOBJ *dm_entryobj;
bool nextlevel;
int faultcode;
int setaction;
char *in_param;
char *in_value;
char *addobj_instance;
char *linker;
char *linker_param;
char *dm_version;
unsigned int alias_register;
unsigned int nbrof_instance;
unsigned int instance_mode;
unsigned char inparam_isparam;
unsigned char findparam;
char *inst_buf[16];
unsigned int end_session_flag;
bool isgetschema;
bool iscommand;
bool isevent;
bool isinfo;
};
typedef struct dmnode {
DMOBJ *obj;
struct dmnode *parent;
char *current_object;
void *prev_data;
char *prev_instance;
unsigned char instance_level;
unsigned char matched;
unsigned char is_instanceobj;
unsigned char browse_type;
int max_instance;
int num_of_entries;
} DMNODE;
typedef struct dm_map_obj {
char *path;
struct dm_obj_s *root_obj;
struct dm_leaf_s *root_leaf;
} DM_MAP_OBJ;
typedef struct dm_map_vendor {
char *vendor;
struct dm_map_obj *vendor_obj;
} DM_MAP_VENDOR;
typedef struct dm_map_vendor_exclude {
char *vendor;
char **vendor_obj;
} DM_MAP_VENDOR_EXCLUDE;
enum operate_ret_status {
CMD_SUCCESS,
CMD_INVALID_ARGUMENTS,
CMD_FAIL,
CMD_NOT_FOUND,
__STATUS_MAX,
};
enum deprecated_operate_ret_status {
SUCCESS,
UBUS_INVALID_ARGUMENTS,
FAIL,
};
typedef struct {
const char **in;
const char **out;
} operation_args;
typedef struct {
const char **param;
} event_args;
typedef enum operate_ret_status opr_ret_t;
typedef opr_ret_t (*operation) (struct dmctx *dmctx, char *p, json_object *input);
typedef struct dm_map_operate {
char *path;
operation operate;
char *type; // sync or async
operation_args args;
} DM_MAP_OPERATE __attribute__ ((deprecated));
enum set_value_action {
VALUECHECK,
VALUESET
};
enum del_action_enum {
DEL_INST,
DEL_ALL
};
enum browse_type_enum {
BROWSE_NORMAL,
BROWSE_FIND_MAX_INST,
BROWSE_NUM_OF_ENTRIES
};
enum {
CMD_GET_VALUE,
CMD_GET_NAME,
CMD_SET_VALUE,
CMD_ADD_OBJECT,
CMD_DEL_OBJECT,
CMD_USP_OPERATE,
CMD_USP_LIST_OPERATE,
CMD_USP_LIST_EVENT,
CMD_GET_SCHEMA,
CMD_GET_INSTANCES,
CMD_EXTERNAL_COMMAND
};
enum usp_fault_code_enum {
USP_FAULT_GENERAL_FAILURE = 7000, // general failure
USP_FAULT_MESSAGE_NOT_UNDERSTOOD = 7001, // message was not understood
USP_FAULT_REQUEST_DENIED = 7002, // Cannot or will not process message
USP_FAULT_INTERNAL_ERROR = 7003, // Message failed due to an internal error
USP_FAULT_INVALID_ARGUMENT = 7004, // invalid values in the request elements
USP_FAULT_RESOURCES_EXCEEDED = 7005, // Message failed due to memory or processing limitations
USP_FAULT_PERMISSION_DENIED = 7006, // Source endpoint does not have authorisation to use this message
USP_FAULT_INVALID_CONFIGURATION = 7007, // invalid or unstable state
// ParamError codes
USP_FAULT_INVALID_PATH_SYNTAX = 7008, // Requested path was invalid or a reference was invalid
USP_FAULT_PARAM_ACTION_FAILED = 7009, // Parameter failed to update for a general reason described in an err_msg element.
USP_FAULT_UNSUPPORTED_PARAM = 7010, // Requested Path Name associated with this ParamError did not match any instantiated parameters
USP_FAULT_INVALID_TYPE = 7011, // Unable to convert string value to correct data type
USP_FAULT_INVALID_VALUE = 7012, // Out of range or invalid enumeration
USP_FAULT_PARAM_READ_ONLY = 7013, // Attempted to write to a read only parameter
USP_FAULT_VALUE_CONFLICT = 7014, // Requested value would result in an invalid configuration
USP_FAULT_CRUD_FAILURE = 7015, // General failure to perform the CRUD operation
USP_FAULT_OBJECT_DOES_NOT_EXIST = 7016, // Requested object instance does not exist
USP_FAULT_CREATION_FAILURE = 7017, // General failure to create the object
USP_FAULT_NOT_A_TABLE = 7018, // The requested pathname was expected to be a multi-instance object, but wasn't
USP_FAULT_OBJECT_NOT_CREATABLE = 7019, // Attempted to create an object which was non-creatable (for non-writable multi-instance objects)
USP_FAULT_SET_FAILURE = 7020, // General failure to set a parameter
USP_FAULT_REQUIRED_PARAM_FAILED = 7021, // The CRUD operation failed because a required parameter failed to update
USP_FAULT_COMMAND_FAILURE = 7022, // Command failed to operate
USP_FAULT_COMMAND_CANCELLED = 7023, // Command failed to complete because it was cancelled
USP_FAULT_OBJECT_NOT_DELETABLE = 7024, // Attempted to delete an object which was non-deletable, or object failed to be deleted
USP_FAULT_UNIQUE_KEY_CONFLICT = 7025, // unique keys would conflict
USP_FAULT_INVALID_PATH = 7026, // Path is not present in the data model schema
// Brokered USP Record Errors
USP_FAULT_RECORD_NOT_PARSED = 7100, // Record could not be parsed
USP_FAULT_SECURE_SESS_REQUIRED = 7101, // A secure session must be started before pasing any records
USP_FAULT_SECURE_SESS_NOT_SUPPORTED = 7102, // Secure session is not supported by this endpoint
USP_FAULT_SEG_NOT_SUPPORTED = 7103, // Segmentation and reassembly is not supported by this endpoint
USP_FAULT_RECORD_FIELD_INVALID = 7104, // A USP record field was invalid
};
enum fault_code_enum {
FAULT_9000 = 9000,// Method not supported
FAULT_9001,// Request denied
FAULT_9002,// Internal error
FAULT_9003,// Invalid arguments
FAULT_9004,// Resources exceeded
FAULT_9005,// Invalid parameter name
FAULT_9006,// Invalid parameter type
FAULT_9007,// Invalid parameter value
FAULT_9008,// Attempt to set a non-writable parameter
FAULT_9009,// Notification request rejected
FAULT_9010,// Download failure
FAULT_9011,// Upload failure
FAULT_9012,// File transfer server authentication failure
FAULT_9013,// Unsupported protocol for file transfer
FAULT_9014,// Download failure: unable to join multicast group
FAULT_9015,// Download failure: unable to contact file server
FAULT_9016,// Download failure: unable to access file
FAULT_9017,// Download failure: unable to complete download
FAULT_9018,// Download failure: file corrupted
FAULT_9019,// Download failure: file authentication failure
FAULT_9020,// Download failure: unable to complete download
FAULT_9021,// Cancelation of file transfer not permitted
FAULT_9022,// Invalid UUID format
FAULT_9023,// Unknown Execution Environment
FAULT_9024,// Disabled Execution Environment
FAULT_9025,// Diployment Unit to Execution environment mismatch
FAULT_9026,// Duplicate Deployment Unit
FAULT_9027,// System Ressources Exceeded
FAULT_9028,// Unknown Deployment Unit
FAULT_9029,// Invalid Deployment Unit State
FAULT_9030,// Invalid Deployment Unit Update: Downgrade not permitted
FAULT_9031,// Invalid Deployment Unit Update: Version not specified
FAULT_9032,// Invalid Deployment Unit Update: Version already exist
__FAULT_MAX
};
enum {
INSTANCE_UPDATE_NUMBER,
INSTANCE_UPDATE_ALIAS
};
enum instance_mode {
INSTANCE_MODE_NUMBER,
INSTANCE_MODE_ALIAS
};
enum bbf_end_session_enum {
BBF_END_SESSION_REBOOT = 1,
BBF_END_SESSION_EXTERNAL_ACTION = 1<<1,
BBF_END_SESSION_RELOAD = 1<<2,
BBF_END_SESSION_FACTORY_RESET = 1<<3,
BBF_END_SESSION_IPPING_DIAGNOSTIC = 1<<4,
BBF_END_SESSION_DOWNLOAD_DIAGNOSTIC = 1<<5,
BBF_END_SESSION_UPLOAD_DIAGNOSTIC = 1<<6,
BBF_END_SESSION_X_FACTORY_RESET_SOFT = 1<<7,
BBF_END_SESSION_NSLOOKUP_DIAGNOSTIC = 1<<8,
BBF_END_SESSION_TRACEROUTE_DIAGNOSTIC = 1<<9,
BBF_END_SESSION_UDPECHO_DIAGNOSTIC = 1<<10,
BBF_END_SESSION_SERVERSELECTION_DIAGNOSTIC = 1<<11
};
enum dm_browse_enum {
DM_ERROR = -1,
DM_OK = 0,
DM_STOP = 1
};
enum dmt_type_enum {
DMT_STRING,
DMT_UNINT,
DMT_INT,
DMT_UNLONG,
DMT_LONG,
DMT_BOOL,
DMT_TIME,
DMT_HEXBIN,
DMT_BASE64,
DMT_COMMAND,
DMT_EVENT,
};
enum amd_version_enum {
AMD_1 = 1,
AMD_2,
AMD_3,
AMD_4,
AMD_5,
};
enum bbfdm_type_enum {
BBFDM_BOTH,
BBFDM_CWMP,
BBFDM_USP,
BBFDM_NONE
};
enum {
INDX_JSON_MOUNT,
INDX_LIBRARY_MOUNT,
INDX_VENDOR_MOUNT,
__INDX_DYNAMIC_MAX
};
enum dm_uci_cmp {
CMP_SECTION,
CMP_OPTION_EQUAL,
CMP_OPTION_REGEX,
CMP_OPTION_CONTAINING,
CMP_OPTION_CONT_WORD,
CMP_LIST_CONTAINING,
CMP_FILTER_FUNC
};
enum dm_uci_walk {
GET_FIRST_SECTION,
GET_NEXT_SECTION
};
enum ubus_arg_type {
String,
Integer,
Boolean,
Table
};
struct ubus_arg {
const char *key;
const char *val;
enum ubus_arg_type type;
};
struct range_args {
const char *min;
const char *max;
};
struct dmmap_dup
{
struct list_head list;
struct uci_section *config_section;
struct uci_section *dmmap_section;
};
#endif //__DMAPI_H__

View file

@ -24,406 +24,10 @@
#include <json-c/json.h>
#include "dmuci.h"
#include "dmmem.h"
#ifdef UNDEF
#undef UNDEF
#endif
#define UNDEF -1
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif
#ifndef FREE
#define FREE(x) do { if(x) {free(x); x = NULL;} } while (0)
#endif
#ifndef BBF_ATTR_UNUSED
#define BBF_ATTR_UNUSED(x) (void)(x)
#endif
#include "dmapi.h"
#define DEFAULT_DMVERSION "2.15"
#define DM_STRNCPY(DST, SRC, SIZE) \
do { \
strncpy(DST, SRC, SIZE - 1); \
DST[SIZE-1] = '\0'; \
} while(0)
extern struct dm_permession_s DMREAD;
extern struct dm_permession_s DMWRITE;
extern struct dm_permession_s DMSYNC;
extern struct dm_permession_s DMASYNC;
extern char *DMT_TYPE[];
extern int bbfdatamodel_type;
#define DMPARAM_ARGS \
struct dmctx *dmctx, \
struct dmnode *node, \
char *lastname, \
struct dm_permession_s *permission, \
int type, \
int (*get_cmd)(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value), \
int (*set_cmd)(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action), \
void *data, \
char *instance
#define DMOBJECT_ARGS \
struct dmctx *dmctx, \
struct dmnode *node, \
struct dm_permession_s *permission, \
int (*addobj)(char *refparam, struct dmctx *ctx, void *data, char **instance), \
int (*delobj)(char *refparam, struct dmctx *ctx, void *data, char *instance, unsigned char del_action), \
int (*get_linker)(char *refparam, struct dmctx *dmctx, void *data, char *instance, char **linker), \
void *data, \
char *instance
struct dmnode;
struct dmctx;
struct dm_dynamic_obj {
struct dm_obj_s **nextobj;
int idx_type;
};
struct dm_dynamic_leaf {
struct dm_leaf_s **nextleaf;
int idx_type;
};
struct dm_permession_s {
char *val;
char *(*get_permission)(char *refparam, struct dmctx *dmctx, void *data, char *instance);
};
struct dm_notif_s {
char *val;
char *(*get_notif)(char *refparam, struct dmctx *dmctx, void *data, char *instance);
};
typedef struct dm_leaf_s {
/* PARAM, permission, type, getvalue, setvalue, bbfdm_type, version(7)*/
char *parameter;
struct dm_permession_s *permission;
int type;
int (*getvalue)(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value);
int (*setvalue)(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action);
int bbfdm_type;
char version[10];
} DMLEAF;
typedef struct dm_obj_s {
/* OBJ, permission, addobj, delobj, checkdep, browseinstobj, nextdynamicobj, dynamicleaf, nextobj, leaf, linker, bbfdm_type, uniqueKeys, version(14)*/
char *obj;
struct dm_permession_s *permission;
int (*addobj)(char *refparam, struct dmctx *ctx, void *data, char **instance);
int (*delobj)(char *refparam, struct dmctx *ctx, void *data, char *instance, unsigned char del_action);
char *checkdep;
int (*browseinstobj)(struct dmctx *dmctx, struct dmnode *node, void *data, char *instance);
struct dm_dynamic_obj *nextdynamicobj;
struct dm_dynamic_leaf *dynamicleaf;
struct dm_obj_s *nextobj;
struct dm_leaf_s *leaf;
int (*get_linker)(char *refparam, struct dmctx *dmctx, void *data, char *instance, char **linker);
int bbfdm_type;
const char **unique_keys;
char version[10];
} DMOBJ;
struct set_tmp {
struct list_head list;
char *name;
char *value;
};
struct param_fault {
struct list_head list;
char *name;
int fault;
};
struct dm_parameter {
struct list_head list;
char *name;
char *data;
char *type;
char *additional_data;
};
struct dmctx
{
bool stop;
bool match;
int (*method_param)(DMPARAM_ARGS);
int (*method_obj)(DMOBJECT_ARGS);
int (*checkobj)(DMOBJECT_ARGS);
int (*checkleaf)(DMOBJECT_ARGS);
struct list_head list_parameter;
struct list_head set_list_tmp;
struct list_head list_fault_param;
struct list_head list_json_parameter;
DMOBJ *dm_entryobj;
bool nextlevel;
int faultcode;
int setaction;
char *in_param;
char *in_value;
char *addobj_instance;
char *linker;
char *linker_param;
char *dm_version;
unsigned int alias_register;
unsigned int nbrof_instance;
unsigned int instance_mode;
unsigned char inparam_isparam;
unsigned char findparam;
char *inst_buf[16];
unsigned int end_session_flag;
bool isgetschema;
bool iscommand;
bool isevent;
bool isinfo;
};
typedef struct dmnode {
DMOBJ *obj;
struct dmnode *parent;
char *current_object;
void *prev_data;
char *prev_instance;
unsigned char instance_level;
unsigned char matched;
unsigned char is_instanceobj;
unsigned char browse_type;
int max_instance;
int num_of_entries;
} DMNODE;
typedef struct dm_map_obj {
char *path;
struct dm_obj_s *root_obj;
struct dm_leaf_s *root_leaf;
} DM_MAP_OBJ;
typedef struct dm_map_vendor {
char *vendor;
struct dm_map_obj *vendor_obj;
} DM_MAP_VENDOR;
typedef struct dm_map_vendor_exclude {
char *vendor;
char **vendor_obj;
} DM_MAP_VENDOR_EXCLUDE;
enum operate_ret_status {
CMD_SUCCESS,
CMD_INVALID_ARGUMENTS,
CMD_FAIL,
CMD_NOT_FOUND,
__STATUS_MAX,
};
enum deprecated_operate_ret_status {
SUCCESS,
UBUS_INVALID_ARGUMENTS,
FAIL,
};
typedef struct {
const char **in;
const char **out;
} operation_args;
typedef struct {
const char **param;
} event_args;
typedef enum operate_ret_status opr_ret_t;
typedef opr_ret_t (*operation) (struct dmctx *dmctx, char *p, json_object *input);
typedef struct dm_map_operate {
char *path;
operation operate;
char *type; // sync or async
operation_args args;
} DM_MAP_OPERATE __attribute__ ((deprecated));
enum set_value_action {
VALUECHECK,
VALUESET
};
enum del_action_enum {
DEL_INST,
DEL_ALL
};
enum browse_type_enum {
BROWSE_NORMAL,
BROWSE_FIND_MAX_INST,
BROWSE_NUM_OF_ENTRIES
};
enum {
CMD_GET_VALUE,
CMD_GET_NAME,
CMD_SET_VALUE,
CMD_ADD_OBJECT,
CMD_DEL_OBJECT,
CMD_USP_OPERATE,
CMD_USP_LIST_OPERATE,
CMD_USP_LIST_EVENT,
CMD_GET_SCHEMA,
CMD_GET_INSTANCES,
CMD_EXTERNAL_COMMAND
};
enum usp_fault_code_enum {
USP_FAULT_GENERAL_FAILURE = 7000, // general failure
USP_FAULT_MESSAGE_NOT_UNDERSTOOD = 7001, // message was not understood
USP_FAULT_REQUEST_DENIED = 7002, // Cannot or will not process message
USP_FAULT_INTERNAL_ERROR = 7003, // Message failed due to an internal error
USP_FAULT_INVALID_ARGUMENT = 7004, // invalid values in the request elements
USP_FAULT_RESOURCES_EXCEEDED = 7005, // Message failed due to memory or processing limitations
USP_FAULT_PERMISSION_DENIED = 7006, // Source endpoint does not have authorisation to use this message
USP_FAULT_INVALID_CONFIGURATION = 7007, // invalid or unstable state
// ParamError codes
USP_FAULT_INVALID_PATH_SYNTAX = 7008, // Requested path was invalid or a reference was invalid
USP_FAULT_PARAM_ACTION_FAILED = 7009, // Parameter failed to update for a general reason described in an err_msg element.
USP_FAULT_UNSUPPORTED_PARAM = 7010, // Requested Path Name associated with this ParamError did not match any instantiated parameters
USP_FAULT_INVALID_TYPE = 7011, // Unable to convert string value to correct data type
USP_FAULT_INVALID_VALUE = 7012, // Out of range or invalid enumeration
USP_FAULT_PARAM_READ_ONLY = 7013, // Attempted to write to a read only parameter
USP_FAULT_VALUE_CONFLICT = 7014, // Requested value would result in an invalid configuration
USP_FAULT_CRUD_FAILURE = 7015, // General failure to perform the CRUD operation
USP_FAULT_OBJECT_DOES_NOT_EXIST = 7016, // Requested object instance does not exist
USP_FAULT_CREATION_FAILURE = 7017, // General failure to create the object
USP_FAULT_NOT_A_TABLE = 7018, // The requested pathname was expected to be a multi-instance object, but wasn't
USP_FAULT_OBJECT_NOT_CREATABLE = 7019, // Attempted to create an object which was non-creatable (for non-writable multi-instance objects)
USP_FAULT_SET_FAILURE = 7020, // General failure to set a parameter
USP_FAULT_REQUIRED_PARAM_FAILED = 7021, // The CRUD operation failed because a required parameter failed to update
USP_FAULT_COMMAND_FAILURE = 7022, // Command failed to operate
USP_FAULT_COMMAND_CANCELLED = 7023, // Command failed to complete because it was cancelled
USP_FAULT_OBJECT_NOT_DELETABLE = 7024, // Attempted to delete an object which was non-deletable, or object failed to be deleted
USP_FAULT_UNIQUE_KEY_CONFLICT = 7025, // unique keys would conflict
USP_FAULT_INVALID_PATH = 7026, // Path is not present in the data model schema
// Brokered USP Record Errors
USP_FAULT_RECORD_NOT_PARSED = 7100, // Record could not be parsed
USP_FAULT_SECURE_SESS_REQUIRED = 7101, // A secure session must be started before pasing any records
USP_FAULT_SECURE_SESS_NOT_SUPPORTED = 7102, // Secure session is not supported by this endpoint
USP_FAULT_SEG_NOT_SUPPORTED = 7103, // Segmentation and reassembly is not supported by this endpoint
USP_FAULT_RECORD_FIELD_INVALID = 7104, // A USP record field was invalid
};
enum fault_code_enum {
FAULT_9000 = 9000,// Method not supported
FAULT_9001,// Request denied
FAULT_9002,// Internal error
FAULT_9003,// Invalid arguments
FAULT_9004,// Resources exceeded
FAULT_9005,// Invalid parameter name
FAULT_9006,// Invalid parameter type
FAULT_9007,// Invalid parameter value
FAULT_9008,// Attempt to set a non-writable parameter
FAULT_9009,// Notification request rejected
FAULT_9010,// Download failure
FAULT_9011,// Upload failure
FAULT_9012,// File transfer server authentication failure
FAULT_9013,// Unsupported protocol for file transfer
FAULT_9014,// Download failure: unable to join multicast group
FAULT_9015,// Download failure: unable to contact file server
FAULT_9016,// Download failure: unable to access file
FAULT_9017,// Download failure: unable to complete download
FAULT_9018,// Download failure: file corrupted
FAULT_9019,// Download failure: file authentication failure
FAULT_9020,// Download failure: unable to complete download
FAULT_9021,// Cancelation of file transfer not permitted
FAULT_9022,// Invalid UUID format
FAULT_9023,// Unknown Execution Environment
FAULT_9024,// Disabled Execution Environment
FAULT_9025,// Diployment Unit to Execution environment mismatch
FAULT_9026,// Duplicate Deployment Unit
FAULT_9027,// System Ressources Exceeded
FAULT_9028,// Unknown Deployment Unit
FAULT_9029,// Invalid Deployment Unit State
FAULT_9030,// Invalid Deployment Unit Update: Downgrade not permitted
FAULT_9031,// Invalid Deployment Unit Update: Version not specified
FAULT_9032,// Invalid Deployment Unit Update: Version already exist
__FAULT_MAX
};
enum {
INSTANCE_UPDATE_NUMBER,
INSTANCE_UPDATE_ALIAS
};
enum instance_mode {
INSTANCE_MODE_NUMBER,
INSTANCE_MODE_ALIAS
};
enum bbf_end_session_enum {
BBF_END_SESSION_REBOOT = 1,
BBF_END_SESSION_EXTERNAL_ACTION = 1<<1,
BBF_END_SESSION_RELOAD = 1<<2,
BBF_END_SESSION_FACTORY_RESET = 1<<3,
BBF_END_SESSION_IPPING_DIAGNOSTIC = 1<<4,
BBF_END_SESSION_DOWNLOAD_DIAGNOSTIC = 1<<5,
BBF_END_SESSION_UPLOAD_DIAGNOSTIC = 1<<6,
BBF_END_SESSION_X_FACTORY_RESET_SOFT = 1<<7,
BBF_END_SESSION_NSLOOKUP_DIAGNOSTIC = 1<<8,
BBF_END_SESSION_TRACEROUTE_DIAGNOSTIC = 1<<9,
BBF_END_SESSION_UDPECHO_DIAGNOSTIC = 1<<10,
BBF_END_SESSION_SERVERSELECTION_DIAGNOSTIC = 1<<11
};
enum dm_browse_enum {
DM_ERROR = -1,
DM_OK = 0,
DM_STOP = 1
};
enum dmt_type_enum {
DMT_STRING,
DMT_UNINT,
DMT_INT,
DMT_UNLONG,
DMT_LONG,
DMT_BOOL,
DMT_TIME,
DMT_HEXBIN,
DMT_BASE64,
DMT_COMMAND,
DMT_EVENT,
};
enum amd_version_enum {
AMD_1 = 1,
AMD_2,
AMD_3,
AMD_4,
AMD_5,
};
enum bbfdm_type_enum {
BBFDM_BOTH,
BBFDM_CWMP,
BBFDM_USP,
BBFDM_NONE
};
enum {
INDX_JSON_MOUNT,
INDX_LIBRARY_MOUNT,
INDX_VENDOR_MOUNT,
__INDX_DYNAMIC_MAX
};
int get_number_of_entries(struct dmctx *ctx, void *data, char *instance, int (*browseinstobj)(struct dmctx *ctx, struct dmnode *node, void *data, char *instance));
char *handle_instance(struct dmctx *dmctx, DMNODE *parent_node, struct uci_section *s, char *inst_opt, char *alias_opt);
char *handle_instance_without_section(struct dmctx *dmctx, DMNODE *parent_node, int inst_nbr);

View file

@ -125,7 +125,6 @@ extern char *PowerState[];
#define DHCP_LEASES_FILE "/tmp/dhcp.leases"
#define DHCP_CLIENT_OPTIONS_FILE "/var/dhcp.client.options"
#define DMMAP "dmmap"
#define RANGE_ARGS (struct range_args[])
#define LIST_KEY (const char *[])
#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
@ -189,18 +188,6 @@ enum fs_size_type_enum {
if ((dir = opendir(path)) == NULL) return 0; \
while ((ent = readdir (dir)) != NULL) \
struct range_args {
const char *min;
const char *max;
};
struct dmmap_dup
{
struct list_head list;
struct uci_section *config_section;
struct uci_section *dmmap_section;
};
struct dmmap_sect {
struct list_head list;
char *section_name;

View file

@ -110,13 +110,19 @@ const char *file, const char *func, int line,
struct list_head *mem_list, const char *s
)
{
if (s == NULL)
return NULL;
size_t len = strlen(s) + 1;
#ifdef WITH_MEMTRACK
void *new = __dmmalloc(file, func, line, mem_list, len);
#else
void *new = __dmmalloc(mem_list, len);
#endif /*WITH_MEMTRACK*/
if (new == NULL) return NULL;
if (new == NULL)
return NULL;
return (char *) memcpy(new, s, len);
}
#endif /*WITH_MEMLEACKSEC*/

View file

@ -17,21 +17,7 @@
#include <json-c/json.h>
#include <libubus.h>
#include <time.h>
#define UBUS_ARGS (struct ubus_arg[])
enum ubus_arg_type {
String,
Integer,
Boolean,
Table
};
struct ubus_arg {
const char *key;
const char *val;
enum ubus_arg_type type;
};
#include "dmapi.h"
int dmubus_call(char *obj, char *method, struct ubus_arg u_args[], int u_args_size, json_object **req_res);
int dmubus_call_set(char *obj, char *method, struct ubus_arg u_args[], int u_args_size);

View file

@ -857,7 +857,7 @@ void commit_and_free_uci_ctx_bbfdm(char *dmmap_config)
uci_ctx_bbfdm = NULL;
}
char *bbf_uci_get_value(char *path, char *package, char *section, char *option)
char *dmuci_get_value_by_path(char *path, char *package, char *section, char *option)
{
struct uci_option *o;
char *val = "";
@ -879,7 +879,7 @@ char *bbf_uci_get_value(char *path, char *package, char *section, char *option)
return val;
}
char *bbf_uci_set_value(char *path, char *package, char *section, char *option, char *value)
char *dmuci_set_value_by_path(char *path, char *package, char *section, char *option, char *value)
{
struct uci_context *save_uci_ctx = NULL;
struct uci_ptr ptr = {0};

View file

@ -33,21 +33,6 @@
#define VARSTATE_CONFDIR "/var/state/"
#define VARSTATE_SAVEDIR "/tmp/.bbfdm_var"
enum dm_uci_cmp {
CMP_SECTION,
CMP_OPTION_EQUAL,
CMP_OPTION_REGEX,
CMP_OPTION_CONTAINING,
CMP_OPTION_CONT_WORD,
CMP_LIST_CONTAINING,
CMP_FILTER_FUNC
};
enum dm_uci_walk {
GET_FIRST_SECTION,
GET_NEXT_SECTION
};
struct package_change
{
struct list_head list;
@ -390,8 +375,8 @@ int db_get_value_string(char *package, char *section, char *option, char **value
int dmuci_get_option_value_string_varstate(char *package, char *section, char *option, char **value);
int dmuci_set_value_varstate(char *package, char *section, char *option, char *value);
char *bbf_uci_get_value(char *path, char *package, char *section, char *option);
char *bbf_uci_set_value(char *path, char *package, char *section, char *option, char *value);
char *dmuci_get_value_by_path(char *path, char *package, char *section, char *option);
char *dmuci_set_value_by_path(char *path, char *package, char *section, char *option, char *value);
#endif

View file

@ -577,7 +577,7 @@ def cprintGetSetValue(getvalue, setvalue, mappingparam, instance, typeparam, par
get_value += "\n"
get_value += " snprintf(uci_type, sizeof(uci_type), \"@%s[%s]\", instance ? atoi(instance)-1 : 0);\n" % (
res2, "%d")
get_value += " *value = bbf_uci_get_value(\"%s\", \"%s\", uci_type, \"%s\");" % (
get_value += " *value = dmuci_get_value_by_path(\"%s\", \"%s\", uci_type, \"%s\");" % (
res6, res1, res5)
elif instance == "TRUE" and res7 is not None:
get_value += " char *linker = dmstrdup(*value);\n"
@ -586,7 +586,7 @@ def cprintGetSetValue(getvalue, setvalue, mappingparam, instance, typeparam, par
get_value += " if (*value == NULL)\n"
get_value += " *value = \"\";"
elif res6 is not None:
get_value += " *value = bbf_uci_get_value(\"%s\", \"%s\", \"%s\", \"%s\");" % (
get_value += " *value = dmuci_get_value_by_path(\"%s\", \"%s\", \"%s\", \"%s\");" % (
res6, res1, res3, res5)
elif instance == "TRUE":
get_value += " dmuci_get_value_by_section_string(((struct dmmap_dup *)data)->config_section, \"%s\", value);" % res5