cfg_ctx.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_CTX_H
00028 #define _CFG_CTX_H
00029 
00030 #include "../str.h"
00031 #include "../locking.h"
00032 #include "cfg.h"
00033 #include "cfg_struct.h"
00034 
00035 
00036 /* variable values */
00037 union cfg_var_value{
00038         void* vp;
00039         long vlong;
00040         int vint;
00041         str vstr;
00042         unsigned char   vraw[1]; /* variable length */
00043 };
00044 
00045 
00047 typedef struct _cfg_changed_var {
00048         cfg_group_t     *group;
00049         cfg_mapping_t   *var;
00050         struct _cfg_changed_var *next;
00051 
00052         unsigned int    group_id; /* valid only if group_id_set==1 */
00053         unsigned char   group_id_set;
00054         unsigned char   del_value;      /* delete the value instead of setting it */
00055 
00056         /* blob that contains the new value */
00057         union cfg_var_value new_val; /* variable size */
00058 } cfg_changed_var_t;
00059 
00061 typedef void (*cfg_on_declare)(str *, cfg_def_t *);
00062 
00064 typedef struct _cfg_ctx {
00065         /* variables that are already changed
00066         but have not been committed yet */
00067         cfg_changed_var_t       *changed_first;
00068         /* lock protecting the linked-list of
00069         changed variables */
00070         gen_lock_t              lock;
00071 
00072         /* callback that is called when a new
00073         group is registered */
00074         cfg_on_declare          on_declare_cb;
00075 
00076         struct _cfg_ctx *next;
00077 } cfg_ctx_t;
00078 
00079 #define CFG_CTX_LOCK(ctx)       lock_get(&(ctx)->lock)
00080 #define CFG_CTX_UNLOCK(ctx)     lock_release(&(ctx)->lock)
00081 
00084 int cfg_register_ctx(cfg_ctx_t **handle, cfg_on_declare on_declare_cb);
00085 
00087 void cfg_ctx_destroy(void);
00088 
00090 int cfg_set_now(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
00091                         void *val, unsigned int val_type);
00092 int cfg_set_now_int(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
00093                         int val);
00094 int cfg_set_now_string(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
00095                         char *val);
00096 int cfg_set_now_str(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
00097                         str *val);
00098 
00101 int cfg_del_now(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name);
00102 
00103 /* sets the value of a variable but does not commit the change */
00104 int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
00105                         void *val, unsigned int val_type);
00106 int cfg_set_delayed_int(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
00107                         int val);
00108 int cfg_set_delayed_string(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
00109                         char *val);
00110 int cfg_set_delayed_str(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
00111                         str *val);
00112 
00115 int cfg_del_delayed(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name);
00116 
00118 int cfg_commit(cfg_ctx_t *ctx);
00119 
00121 int cfg_rollback(cfg_ctx_t *ctx);
00122 
00124 int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
00125                         void **val, unsigned int *val_type);
00126 
00128 int cfg_help(cfg_ctx_t *ctx, str *group_name, str *var_name,
00129                         char **ch, unsigned int *input_type);
00130 
00132 void cfg_notify_drivers(char *group_name, int group_name_len, cfg_def_t *def);
00133 
00136 int convert_val(unsigned int val_type, void *val,
00137                         unsigned int var_type, void **new_val);
00138 
00140 void convert_val_cleanup(void);
00141 
00143 #define cfg_get_group_init(handle) \
00144         (*(handle)) = (void *)cfg_group
00145 
00161 int cfg_get_group_next(void **h,
00162                         str *gname, cfg_def_t **def);
00163 
00168 int cfg_diff_init(cfg_ctx_t *ctx,
00169                 void **h);
00170 
00196 int cfg_diff_next(void **h,
00197                         str *gname, unsigned int **gid, str *vname,
00198                         void **old_val, void **new_val,
00199                         unsigned int *val_type);
00200 
00202 void cfg_diff_release(cfg_ctx_t *ctx);
00203 
00204 /* Add a new instance to an existing group */
00205 int cfg_add_group_inst(cfg_ctx_t *ctx, str *group_name, unsigned int group_id);
00206 
00207 /* Delete an instance of a group */
00208 int cfg_del_group_inst(cfg_ctx_t *ctx, str *group_name, unsigned int group_id);
00209 
00210 /* Check the existance of a group instance.
00211  * return value:
00212  *      1: exists
00213  *      0: does not exist
00214  */
00215 int cfg_group_inst_exists(cfg_ctx_t *ctx, str *group_name, unsigned int group_id);
00216 
00217 /* Apply the changes to a group instance as long as the additional variable
00218  * belongs to the specified group_id. *add_var_p is moved to the next additional
00219  * variable, and all the consumed variables are freed.
00220  */
00221 int cfg_apply_list(cfg_group_inst_t *ginst, cfg_group_t *group,
00222                         unsigned int group_id, cfg_add_var_t **add_var_p);
00223 
00224 #endif /* _CFG_CTX_H */