4 * Copyright (C) 2007 iptelorg GmbH
6 * This file is part of ser, a free SIP server.
8 * ser is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version
13 * For a license to use the ser software under conditions
14 * other than those described here, or to purchase support for this
15 * software, please contact iptel.org by e-mail at the following addresses:
18 * ser is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * 2007-12-03 Initial version (Miklos)
35 #include "../mem/mem.h"
36 #include "cfg_struct.h"
38 #include "cfg_script.h"
41 /* declares a new cfg group
42 * handler is set to the memory area where the variables are stored
43 * return value is -1 on error
45 int cfg_declare(char *group_name, cfg_def_t *def, void *values, int def_size,
48 int i, num, size, group_name_len;
49 cfg_mapping_t *mapping = NULL;
51 /* check the number of the variables */
52 for (num=0; def[num].name; num++);
54 mapping = (cfg_mapping_t *)pkg_malloc(sizeof(cfg_mapping_t)*num);
56 LOG(L_ERR, "ERROR: register_cfg_def(): not enough memory\n");
59 memset(mapping, 0, sizeof(cfg_mapping_t)*num);
61 /* calculate the size of the memory block that has to
62 be allocated for the cfg variables, and set the content of the
63 cfg_mapping array the same time */
64 for (i=0, size=0; i<num; i++) {
65 mapping[i].def = &(def[i]);
66 mapping[i].name_len = strlen(def[i].name);
68 /* padding depends on the type of the next variable */
69 switch (CFG_VAR_MASK(def[i].type)) {
72 size = ROUND_INT(size);
73 mapping[i].offset = size;
79 size = ROUND_POINTER(size);
80 mapping[i].offset = size;
81 size += sizeof(char *);
85 size = ROUND_POINTER(size);
86 mapping[i].offset = size;
91 LOG(L_ERR, "ERROR: register_cfg_def(): %s.%s: unsupported variable type\n",
92 group_name, def[i].name);
96 /* verify the type of the input */
97 if (CFG_INPUT_MASK(def[i].type)==0) {
98 def[i].type |= def[i].type << CFG_INPUT_SHIFT;
100 if ((CFG_INPUT_MASK(def[i].type) != CFG_VAR_MASK(def[i].type) << CFG_INPUT_SHIFT)
101 && (def[i].on_change_cb == 0)) {
102 LOG(L_ERR, "ERROR: register_cfg_def(): %s.%s: variable and input types are "
103 "different, but no callback is defined for conversion\n",
104 group_name, def[i].name);
109 if (CFG_INPUT_MASK(def[i].type) > CFG_INPUT_STR) {
110 LOG(L_ERR, "ERROR: register_cfg_def(): %s.%s: unsupported input type\n",
111 group_name, def[i].name);
116 /* minor validation */
117 if (size != def_size) {
118 LOG(L_ERR, "ERROR: register_cfg_def(): the specified size of the config "
119 "structure does not equal with the calculated size, check whether "
120 "the variable types are correctly defined!\n");
124 group_name_len = strlen(group_name);
125 /* create a new group
126 I will allocate memory in shm mem for the variables later in a single block,
127 when we know the size of all the registered groups. */
128 if (!cfg_new_group(group_name, group_name_len, num, mapping, values, size, handle))
131 /* The cfg variables are ready to use, let us set the handle
132 before passing the new definitions to the drivers.
133 We make the interface usable for the fixup functions
137 /* notify the drivers about the new config definition */
138 cfg_notify_drivers(group_name, group_name_len, def);
140 LOG(L_DBG, "DEBUG: register_cfg_def(): "
141 "new config group has been registered: '%s' (num=%d, size=%d)\n",
142 group_name, num, size);
147 if (mapping) pkg_free(mapping);
148 LOG(L_ERR, "ERROR: register_cfg_def(): Failed to register the config group: %s\n",
154 /* declares a single variable with integer type */
155 int cfg_declare_int(char *group_name, char *var_name, int val, char *descr)
157 cfg_script_var_t *var;
159 if ((var = new_cfg_script_var(group_name, var_name, CFG_VAR_INT, descr)) == NULL)
167 /* declares a single variable with str type */
168 int cfg_declare_str(char *group_name, char *var_name, char *val, char *descr)
170 cfg_script_var_t *var;
173 if ((var = new_cfg_script_var(group_name, var_name, CFG_VAR_STR, descr)) == NULL)
178 var->val.s.s = (char *)pkg_malloc(sizeof(char) * (len + 1));
180 LOG(L_ERR, "ERROR: cfg_declare_str(): not enough memory\n");
183 memcpy(var->val.s.s, val, len + 1);
184 var->val.s.len = len;