cfg/cfg_ctx.h
a903fdab
 /*
  * Copyright (C) 2007 iptelorg GmbH
  *
1fff03a2
  * This file is part of Kamailio, a free SIP server.
a903fdab
  *
1fff03a2
  * Kamailio is free software; you can redistribute it and/or modify
a903fdab
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
1fff03a2
  * Kamailio is distributed in the hope that it will be useful,
a903fdab
  * 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
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
a903fdab
  *
  */
 
 #ifndef _CFG_CTX_H
 #define _CFG_CTX_H
 
 #include "../str.h"
 #include "../locking.h"
 #include "cfg.h"
 #include "cfg_struct.h"
 
e95c0775
 
 /* variable values */
 union cfg_var_value{
 	void* vp;
 	long vlong;
 	int vint;
 	str vstr;
 	unsigned char	vraw[1]; /* variable length */
 };
 
 
6d17fe5a
 /** linked list of variables with their new values. */
a903fdab
 typedef struct _cfg_changed_var {
 	cfg_group_t	*group;
 	cfg_mapping_t	*var;
 	struct _cfg_changed_var	*next;
 
ec5bc6bb
 	unsigned int	group_id; /* valid only if group_id_set==1 */
 	unsigned char	group_id_set;
7d7264e2
 	unsigned char	del_value;	/* delete the value instead of setting it */
ec5bc6bb
 
a903fdab
 	/* blob that contains the new value */
e95c0775
 	union cfg_var_value new_val; /* variable size */
a903fdab
 } cfg_changed_var_t;
 
86e99f5a
 /*! \brief callback that is called when a new group is declared */
a903fdab
 typedef void (*cfg_on_declare)(str *, cfg_def_t *);
 
86e99f5a
 /*! \brief linked list of registered contexts */
a903fdab
 typedef struct _cfg_ctx {
 	/* variables that are already changed
 	but have not been committed yet */
 	cfg_changed_var_t	*changed_first;
64146b14
 	/* lock protecting the linked-list of
a903fdab
 	changed variables */
 	gen_lock_t		lock;
 
 	/* callback that is called when a new
 	group is registered */
 	cfg_on_declare		on_declare_cb;
 
 	struct _cfg_ctx	*next;
 } cfg_ctx_t;
 
 #define CFG_CTX_LOCK(ctx)	lock_get(&(ctx)->lock)
 #define CFG_CTX_UNLOCK(ctx)	lock_release(&(ctx)->lock)
 
86e99f5a
 /*! \brief creates a new config context that is an interface to the
a903fdab
  * cfg variables with write permission */
30feb9b6
 int cfg_register_ctx(cfg_ctx_t **handle, cfg_on_declare on_declare_cb);
a903fdab
 
86e99f5a
 /*! \brief free the memory allocated for the contexts */
a903fdab
 void cfg_ctx_destroy(void);
 
86e99f5a
 /*! \brief set the value of a variable without the need of explicit commit */
6a44d0bd
 int cfg_set_now(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
a903fdab
 			void *val, unsigned int val_type);
6a44d0bd
 int cfg_set_now_int(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
 			int val);
 int cfg_set_now_string(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
 			char *val);
 int cfg_set_now_str(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
 			str *val);
a903fdab
 
7d7264e2
 /*! \brief Delete a variable from the group instance.
  * wrapper function for cfg_set_now */
 int cfg_del_now(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name);
 
a903fdab
 /* sets the value of a variable but does not commit the change */
6a44d0bd
 int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
a903fdab
 			void *val, unsigned int val_type);
6a44d0bd
 int cfg_set_delayed_int(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
 			int val);
 int cfg_set_delayed_string(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
 			char *val);
 int cfg_set_delayed_str(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
 			str *val);
a903fdab
 
7d7264e2
 /*! \brief Delete a variable from the group instance.
  * wrapper function for cfg_set_delayed */
 int cfg_del_delayed(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name);
 
86e99f5a
 /*! \brief commits the previously prepared changes within the context */
a903fdab
 int cfg_commit(cfg_ctx_t *ctx);
 
86e99f5a
 /*! \brief drops the not yet committed changes within the context */
a903fdab
 int cfg_rollback(cfg_ctx_t *ctx);
 
86e99f5a
 /*! \brief returns the value of a variable */
5d4d6ae9
 int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
a903fdab
 			void **val, unsigned int *val_type);
 
86e99f5a
 /*! \brief returns the description of a variable */
a903fdab
 int cfg_help(cfg_ctx_t *ctx, str *group_name, str *var_name,
d6b0973a
 			char **ch, unsigned int *input_type);
a903fdab
 
86e99f5a
 /*! \brief notify the drivers about the new config definition */
140137c9
 void cfg_notify_drivers(char *group_name, int group_name_len, cfg_def_t *def);
a903fdab
 
ca88d956
 /*! \brief convert the value to the requested type.
  * Do not forget the call convert_val_cleaup afterwards. */
bcf3fff9
 int convert_val(unsigned int val_type, void *val,
 			unsigned int var_type, void **new_val);
 
ca88d956
 /*! \brief cleanup function for convert_val() */
 void convert_val_cleanup(void);
 
86e99f5a
 /*! \brief initialize the handle for cfg_get_group_next() */
934bcea4
 #define cfg_get_group_init(handle) \
 	(*(handle)) = (void *)cfg_group
 
86e99f5a
 /*! \brief returns the group name and the cfg structure definition,
934bcea4
  * and moves the handle to the next group
86e99f5a
  *
934bcea4
  * Return value:
  *	0: no more group
  *	1: group exists
  *
  * can be used as follows:
  *
  * void	*handle;
  * cfg_get_group_init(&handle)
  * while (cfg_get_group_next(&handle, &name, &def)) {
  * 	...
  * }
  */
 int cfg_get_group_next(void **h,
 			str *gname, cfg_def_t **def);
 
86e99f5a
 /*! \brief Initialize the handle for cfg_diff_next()
934bcea4
  * WARNING: keeps the context lock held, do not forget
  * to release it with cfg_diff_release()
  */
 int cfg_diff_init(cfg_ctx_t *ctx,
 		void **h);
 
86e99f5a
 /*! \brief return the pending changes that have not been
934bcea4
  * committed yet
99b680c3
  * return value:
  *	1: valid value is found
  *	0: no more changed value found
  *	-1: error occured
  *
86e99f5a
  *
934bcea4
  * can be used as follows:
  *
  * void *handle;
  * if (cfg_diff_init(ctx, &handle)) return -1
99b680c3
  * while ((err = cfg_diff_next(&handle
ec5bc6bb
  *			&group_name, &group_id, &var_name,
934bcea4
  *			&old_val, &new_val
99b680c3
  *			&val_type)) > 0
934bcea4
  * ) {
  *		...
  * }
  * cfg_diff_release(ctx);
99b680c3
  * if (err) {
  *	error occured, the changes cannot be retrieved
  *	...
  * }
934bcea4
  */
 int cfg_diff_next(void **h,
ec5bc6bb
 			str *gname, unsigned int **gid, str *vname,
934bcea4
 			void **old_val, void **new_val,
 			unsigned int *val_type);
 
86e99f5a
 /*! \brief destroy the handle of cfg_diff_next() */
934bcea4
 void cfg_diff_release(cfg_ctx_t *ctx);
a903fdab
 
5d4d6ae9
 /* Add a new instance to an existing group */
 int cfg_add_group_inst(cfg_ctx_t *ctx, str *group_name, unsigned int group_id);
 
8e1d174c
 /* Delete an instance of a group */
 int cfg_del_group_inst(cfg_ctx_t *ctx, str *group_name, unsigned int group_id);
 
fc444f6f
 /* Check the existance of a group instance.
  * return value:
  *	1: exists
  *	0: does not exist
  */
 int cfg_group_inst_exists(cfg_ctx_t *ctx, str *group_name, unsigned int group_id);
 
92f85d59
 /* Apply the changes to a group instance as long as the additional variable
  * belongs to the specified group_id. *add_var_p is moved to the next additional
  * variable, and all the consumed variables are freed.
  */
 int cfg_apply_list(cfg_group_inst_t *ginst, cfg_group_t *group,
 			unsigned int group_id, cfg_add_var_t **add_var_p);
 
a903fdab
 #endif /* _CFG_CTX_H */