/*
 * Copyright (C) 2009-2020 OpenSIPS Solutions
 * Copyright (C) 2006 Voice Sistem SRL
 *
 * This file is part of opensips, a free SIP server.
 *
 * opensips is free software; you can redistribute it and/or modify
 * 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
 *
 * opensips 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef _DIALOG_DLG_LOAD_H_
#define _DIALOG_DLG_LOAD_H_

#include "../../lib/csv.h"
#include "dlg_cb.h"
#include "dlg_ctx.h"
#include "dlg_hash.h"
#include "dlg_handlers.h"
#include "dlg_profile.h"
#include "dlg_vals.h"
#include "../../profiling.h"
#include "../../sr_module.h"

typedef struct dlg_cell *(*get_dlg_f) (void);
typedef str *(*get_dlg_did_f) (struct dlg_cell *dlg);
typedef struct dlg_cell *(*get_dlg_by_callid_f) (const str *, int);
typedef struct dlg_cell *(*get_dlg_by_did_f) (str *, int);
typedef struct dlg_cell *(*get_dlg_by_ids_f) (unsigned int, unsigned int, int);
typedef int (*match_dialog_f) (struct sip_msg *msg, int _seq_match_mode);
typedef int (*get_direction_f) (void);

struct dlg_binds {
	register_dlgcb_f     register_dlgcb;
	create_dlg_f         create_dlg;
	get_dlg_f            get_dlg;
	add_profiles_f       add_profiles;
	search_dlg_profile_f search_profile;
	set_dlg_profile_f    set_profile;
	unset_dlg_profile_f  unset_profile;
	get_profile_size_f   get_profile_size;
	store_dlg_value_f    store_dlg_value;
	fetch_dlg_value_f    fetch_dlg_value;
	terminate_dlg_f      terminate_dlg;

	match_dialog_f       match_dialog;
	validate_dialog_f    validate_dialog;
	fix_route_dialog_f   fix_route_dialog;

	set_mod_flag_f       set_mod_flag;
	is_mod_flag_set_f    is_mod_flag_set;

	ref_dlg_f            dlg_ref;
	unref_dlg_f          dlg_unref;

	get_direction_f      get_direction;
	get_dlg_did_f        get_dlg_did;
	get_dlg_by_did_f     get_dlg_by_did;
	get_dlg_by_ids_f     get_dlg_by_ids;
	get_dlg_by_callid_f  get_dlg_by_callid;
	send_indialog_req_f  send_indialog_request;

	get_rr_param_f       get_rr_param;

	dlg_ctx_register_int_f dlg_ctx_register_int;
	dlg_ctx_register_str_f dlg_ctx_register_str;
	dlg_ctx_register_ptr_f dlg_ctx_register_ptr;

	dlg_ctx_put_int_f dlg_ctx_put_int;
	dlg_ctx_put_str_f dlg_ctx_put_str;
	dlg_ctx_put_ptr_f dlg_ctx_put_ptr;

	dlg_ctx_get_int_f dlg_ctx_get_int;
	dlg_ctx_get_str_f dlg_ctx_get_str;
	dlg_ctx_get_ptr_f dlg_ctx_get_ptr;
};


typedef int(*load_dlg_f)( struct dlg_binds *dlgb );
int load_dlg( struct dlg_binds *dlgb);


static inline int parse_create_dlg_flags(str *flags_str)
{
	/* Keep this array in sync with create_dlg_flags[] below: if you add,
	 * remove or reorder a create_dialog() flag here, make the identical
	 * change there as well.
	 */
	static const str create_dlg_flag_names[] = {
		str_init("bye-on-timeout"),
		str_init("options-ping-caller"),
		str_init("options-ping-callee"),
		str_init("reinvite-ping-caller"),
		str_init("reinvite-ping-callee"),
		str_init("end-on-race-condition"),
		str_init("auto-prack"),
		STR_NULL
	};
	static const unsigned int create_dlg_flags[] = {
		DLG_FLAG_BYEONTIMEOUT,
		DLG_FLAG_PING_CALLER,
		DLG_FLAG_PING_CALLEE,
		DLG_FLAG_REINVITE_PING_CALLER,
		DLG_FLAG_REINVITE_PING_CALLEE,
		DLG_FLAG_END_ON_RACE_CONDITION,
		DLG_FLAG_AUTOPRACK
	};
	csv_record *list, *rec;
	unsigned int flags = 0;
	int i;

	list = parse_csv_record(flags_str);
	if (!list) {
		LM_ERR("failed to parse list of create_dialog flags\n");
		return -1;
	}

	for (rec = list; rec; rec = rec->next) {
		for (i = 0; create_dlg_flag_names[i].s; i++) {
			if (str_strcasecmp(&rec->s, &create_dlg_flag_names[i]) == 0) {
				flags |= create_dlg_flags[i];
				break;
			}
		}

		if (!create_dlg_flag_names[i].s) {
			LM_ERR("unknown create_dialog flag: %.*s\n",
				rec->s.len, rec->s.s);
			free_csv_record(list);
			return -1;
		}
	}

	free_csv_record(list);
	return (int)flags;
}


static inline int load_dlg_api( struct dlg_binds *dlgb )
{
	load_dlg_f load_dlg;

	/* import the DLG auto-loading function */
	if ( !(load_dlg=(load_dlg_f)find_export("load_dlg", 0)))
		return -1;

	/* let the auto-loading function load all DLG stuff */
	if (load_dlg( dlgb )==-1)
		return -1;

	return 0;
}

#define run_dlg_api(_dlg_api, _api_func, _args...) \
	({ \
		typeof((_dlg_api)->_api_func(_args)) _dlg_api_rc; \
		profiling_proc_enter( LEVEL_SIP, #_api_func, 0); \
		_dlg_api_rc = (_dlg_api)->_api_func(_args); \
		profiling_proc_exit( LEVEL_SIP, #_api_func, (int)(intptr_t)_dlg_api_rc); \
		_dlg_api_rc; \
	})


#endif
