cfg.h

00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2007 iptelorg GmbH
00005  *
00006  * This file is part of SIP-router, a free SIP server.
00007  *
00008  * SIP-router is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * SIP-router is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * History
00023  * -------
00024  *  2007-12-03  Initial version (Miklos)
00025  */
00026 
00027 #ifndef _CFG_H
00028 #define _CFG_H
00029 
00030 #include "../str.h"
00031 
00032 /* variable type */
00033 #define CFG_VAR_UNSET           0U
00034 #define CFG_VAR_INT             1U
00035 #define CFG_VAR_STRING          2U
00036 #define CFG_VAR_STR             3U
00037 #define CFG_VAR_POINTER         4U
00038 
00039 /* number of bits required for the variable type */
00040 #define CFG_INPUT_SHIFT         3
00041 
00042 /* input type */
00043 #define CFG_INPUT_INT           (CFG_VAR_INT << CFG_INPUT_SHIFT)
00044 #define CFG_INPUT_STRING        (CFG_VAR_STRING << CFG_INPUT_SHIFT)
00045 #define CFG_INPUT_STR           (CFG_VAR_STR << CFG_INPUT_SHIFT)
00046 
00047 #define CFG_VAR_MASK(x)         ((x)&((1U<<CFG_INPUT_SHIFT)-1))
00048 #define CFG_INPUT_MASK(x)       ((x)&((1U<<(2*CFG_INPUT_SHIFT))-(1U<<CFG_INPUT_SHIFT)))
00049 
00050 /* atomic change is allowed */
00051 #define CFG_ATOMIC              (1U<<(2*CFG_INPUT_SHIFT))
00052 /* variable is read-only */
00053 #define CFG_READONLY            (1U<<(2*CFG_INPUT_SHIFT+1))
00054 /* per-child process callback needs to be called only once */
00055 #define CFG_CB_ONLY_ONCE        (1U<<(2*CFG_INPUT_SHIFT+2))
00056 
00057 typedef int (*cfg_on_change)(void *, str *, str *, void **);
00058 typedef void (*cfg_on_set_child)(str *, str *);
00059 
00060 /* strutrure to be used by the module interface */
00061 typedef struct _cfg_def {
00062         char    *name;
00063         unsigned int    type;
00064         int     min;
00065         int     max;
00066         cfg_on_change   on_change_cb;
00067         cfg_on_set_child        on_set_child_cb;
00068         char    *descr;
00069 } cfg_def_t;
00070 
00071 /* declares a new cfg group
00072  * handler is set to the memory area where the variables are stored
00073  * return value is -1 on error
00074  */
00075 int cfg_declare(char *group_name, cfg_def_t *def, void *values, int def_size,
00076                         void **handler);
00077 
00078 #define cfg_sizeof(gname) \
00079         sizeof(struct cfg_group_##gname)
00080 
00081 #define cfg_get(gname, handle, var) \
00082         ((struct cfg_group_##gname *)handle)->var
00083 
00084 /* declares a single variable with integer type */
00085 int cfg_declare_int(char *group_name, char *var_name,
00086                 int val, int min, int max, char *descr);
00087 
00088 /* declares a single variable with str type */
00089 int cfg_declare_str(char *group_name, char *var_name, char *val, char *descr);
00090 
00091 /* Add a varibale to a group instance with integer type.
00092  * The group instance is created if it does not exist.
00093  * wrapper function for new_add_var()
00094  */
00095 int cfg_ginst_var_int(char *group_name, unsigned int group_id, char *var_name,
00096                         int val);
00097 
00098 /* Add a varibale to a group instance with string type.
00099  * The group instance is created if it does not exist.
00100  * wrapper function for new_add_var()
00101  */
00102 int cfg_ginst_var_string(char *group_name, unsigned int group_id, char *var_name,
00103                         char *val);
00104 
00105 /* Create a new group instance.
00106  * wrapper function for new_add_var()
00107  */
00108 int cfg_new_ginst(char *group_name, unsigned int group_id);
00109 
00110 /* returns the handle of a cfg group */
00111 void **cfg_get_handle(char *gname);
00112 
00113 #endif /* _CFG_H */