From b2d8e055bbd2cc56016fd4f70c0538e84ea086e0 Mon Sep 17 00:00:00 2001
From: Moises Silva <moy@sangoma.com>
Date: Tue, 11 May 2010 15:09:22 -0400
Subject: [PATCH] freetdm: modify prototype for ftdm_span_create to accept I/O
 module name instead of pointer

---
 libs/freetdm/src/ftdm_io.c                    | 108 +++++++++++-------
 libs/freetdm/src/include/freetdm.h            |  61 +++++++++-
 libs/freetdm/src/include/private/ftdm_types.h |  14 ---
 3 files changed, 123 insertions(+), 60 deletions(-)

diff --git a/libs/freetdm/src/ftdm_io.c b/libs/freetdm/src/ftdm_io.c
index d39c26ed65..c709bced5d 100644
--- a/libs/freetdm/src/ftdm_io.c
+++ b/libs/freetdm/src/ftdm_io.c
@@ -522,17 +522,41 @@ FT_DECLARE(ftdm_status_t) ftdm_span_stop(ftdm_span_t *span)
 	return FTDM_FAIL;
 }
 
-FT_DECLARE(ftdm_status_t) ftdm_span_create(ftdm_io_interface_t *fio, ftdm_span_t **span, const char *name)
+FT_DECLARE(ftdm_status_t) ftdm_span_create(const char *iotype, const char *name, ftdm_span_t **span)
 {
 	ftdm_span_t *new_span = NULL;
+	ftdm_io_interface_t *fio = NULL;
 	ftdm_status_t status = FTDM_FAIL;
+	char buf[128] = "";
 
-	ftdm_assert(fio != NULL, "No IO provided\n");
+	ftdm_assert_return(iotype != NULL, FTDM_FAIL, "No IO type provided\n");
+	ftdm_assert_return(name != NULL, FTDM_FAIL, "No span name provided\n");
+	
+	*span = NULL;
 
 	ftdm_mutex_lock(globals.mutex);
+	if (!(fio = (ftdm_io_interface_t *) hashtable_search(globals.interface_hash, (void *)iotype))) {
+		ftdm_load_module_assume(iotype);
+		if ((fio = (ftdm_io_interface_t *) hashtable_search(globals.interface_hash, (void *)iotype))) {
+			ftdm_log(FTDM_LOG_INFO, "Auto-loaded I/O module '%s'\n", iotype);
+		}
+	}
+	ftdm_mutex_unlock(globals.mutex);
 
+	if (!fio) {
+		ftdm_log(FTDM_LOG_CRIT, "failure creating span, no such I/O type '%s'\n", iotype);
+		return FTDM_FAIL;
+	}
+
+	if (!fio->configure_span) {
+		ftdm_log(FTDM_LOG_CRIT, "failure creating span, no configure_span method for I/O type '%s'\n", iotype);
+		return FTDM_FAIL;
+	}
+
+	ftdm_mutex_lock(globals.mutex);
 	if (globals.span_index < FTDM_MAX_SPANS_INTERFACE) {
 		new_span = ftdm_calloc(sizeof(*new_span), 1);
+		
 		ftdm_assert(new_span, "allocating span failed\n");
 
 		status = ftdm_mutex_create(&new_span->mutex);
@@ -556,11 +580,11 @@ FT_DECLARE(ftdm_status_t) ftdm_span_create(ftdm_io_interface_t *fio, ftdm_span_t
 		ftdm_mutex_unlock(globals.span_mutex);
 		
 		if (!name) {
-			char buf[128] = "";
 			snprintf(buf, sizeof(buf), "span%d", new_span->span_id);
 			name = buf;
 		}
 		new_span->name = ftdm_strdup(name);
+		new_span->type = ftdm_strdup(iotype);
 		ftdm_span_add(new_span);
 		*span = new_span;
 		status = FTDM_SUCCESS;
@@ -1657,6 +1681,16 @@ FT_DECLARE(const char *) ftdm_channel_get_span_name(const ftdm_channel_t *ftdmch
 	return ftdmchan->span->name;
 }
 
+FT_DECLARE(void) ftdm_span_set_trunk_type(ftdm_span_t *span, ftdm_trunk_type_t type)
+{
+	span->trunk_type = type;
+}
+
+FT_DECLARE(ftdm_trunk_type_t) ftdm_span_get_trunk_type(const ftdm_span_t *span)
+{
+	return span->trunk_type;
+}
+
 FT_DECLARE(uint32_t) ftdm_span_get_id(const ftdm_span_t *span)
 {
 	return span->span_id;
@@ -3227,7 +3261,15 @@ static ftdm_status_t ftdm_set_channels_alarms(ftdm_span_t *span, int currindex)
 
 FT_DECLARE(ftdm_status_t) ftdm_configure_span_channels(ftdm_span_t *span, const char* str, ftdm_channel_config_t *chan_config, unsigned *configured)
 {
-	int currindex = span->chan_count;
+	int currindex;
+
+	ftdm_assert_return(span != NULL, FTDM_EINVAL, "span is null\n");
+	ftdm_assert_return(chan_config != NULL, FTDM_EINVAL, "config is null\n");
+	ftdm_assert_return(configured != NULL, FTDM_EINVAL, "configured pointer is null\n");
+	ftdm_assert_return(span->fio != NULL, FTDM_EINVAL, "span with no I/O configured\n");
+	ftdm_assert_return(span->fio->configure_span != NULL, FTDM_NOTIMPL, "span I/O with no channel configuration implemented\n");
+
+       	currindex = span->chan_count;
 	*configured = 0;
 	*configured = span->fio->configure_span(span, str, chan_config->type, chan_config->name, chan_config->number);
 	if (!*configured) {
@@ -3235,18 +3277,24 @@ FT_DECLARE(ftdm_status_t) ftdm_configure_span_channels(ftdm_span_t *span, const
 		return FTDM_FAIL;
 	}
 
-	if (ftdm_group_add_channels(span, currindex, chan_config->group_name) != FTDM_SUCCESS) {
-		ftdm_log(FTDM_LOG_ERROR, "%d:Failed to add channels to group %s\n", span->span_id, chan_config->group_name);
-		return FTDM_FAIL;
-	}
-	if (ftdm_set_channels_alarms(span, currindex) != FTDM_SUCCESS) {
-		ftdm_log(FTDM_LOG_ERROR, "%d:Failed to set channel alarms\n", span->span_id);
-		return FTDM_FAIL;
+	if (chan_config->group_name[0]) {
+		if (ftdm_group_add_channels(span, currindex, chan_config->group_name) != FTDM_SUCCESS) {
+			ftdm_log(FTDM_LOG_ERROR, "%d:Failed to add channels to group %s\n", span->span_id, chan_config->group_name);
+			return FTDM_FAIL;
+		}
 	}
+
 	if (ftdm_set_channels_gains(span, currindex, chan_config->rxgain, chan_config->txgain) != FTDM_SUCCESS) {
 		ftdm_log(FTDM_LOG_ERROR, "%d:Failed to set channel gains\n", span->span_id);
 		return FTDM_FAIL;
 	}
+
+
+	if (ftdm_set_channels_alarms(span, currindex) != FTDM_SUCCESS) {
+		ftdm_log(FTDM_LOG_ERROR, "%d:Failed to set channel alarms\n", span->span_id);
+		return FTDM_FAIL;
+	}
+
 	return FTDM_SUCCESS;
 }
 
@@ -3260,7 +3308,6 @@ static ftdm_status_t load_config(void)
 	int intparam = 0;
 	ftdm_span_t *span = NULL;
 	unsigned configured = 0, d = 0;
-	ftdm_io_interface_t *fio = NULL;
 	ftdm_analog_start_type_t tmp;
 	ftdm_size_t len = 0;
 	ftdm_channel_config_t chan_config;
@@ -3271,7 +3318,7 @@ static ftdm_status_t load_config(void)
 	if (!ftdm_config_open_file(&cfg, cfg_name)) {
 		return FTDM_FAIL;
 	}
-	
+	ftdm_log(FTDM_LOG_DEBUG, "Reading FreeTDM configuration file\n");	
 	while (ftdm_config_next_pair(&cfg, &var, &val)) {
 		if (*cfg.category == '#') {
 			if (cfg.catno != catno) {
@@ -3300,33 +3347,9 @@ static ftdm_status_t load_config(void)
 					*name++ = '\0';
 				}
 
-				ftdm_mutex_lock(globals.mutex);
-				if (!(fio = (ftdm_io_interface_t *) hashtable_search(globals.interface_hash, type))) {
-					ftdm_load_module_assume(type);
-					if ((fio = (ftdm_io_interface_t *) hashtable_search(globals.interface_hash, type))) {
-						ftdm_log(FTDM_LOG_INFO, "auto-loaded '%s'\n", type);
-					}
-				}
-				ftdm_mutex_unlock(globals.mutex);
-
-				if (!fio) {
-					ftdm_log(FTDM_LOG_CRIT, "failure creating span, no such type '%s'\n", type);
-					span = NULL;
-					continue;
-				}
-
-				if (!fio->configure_span) {
-					ftdm_log(FTDM_LOG_CRIT, "failure creating span, no configure_span method for '%s'\n", type);
-					span = NULL;
-					continue;
-				}
-
-				if (ftdm_span_create(fio, &span, name) == FTDM_SUCCESS) {
-					span->type = ftdm_strdup(type);
-					d = 0;
-
+				if (ftdm_span_create(type, name, &span) == FTDM_SUCCESS) {
 					ftdm_log(FTDM_LOG_DEBUG, "created span %d (%s) of type %s\n", span->span_id, span->name, type);
-					
+					d = 0;
 				} else {
 					ftdm_log(FTDM_LOG_CRIT, "failure creating span of type %s\n", type);
 					span = NULL;
@@ -3341,8 +3364,9 @@ static ftdm_status_t load_config(void)
 			ftdm_log(FTDM_LOG_DEBUG, "span %d [%s]=[%s]\n", span->span_id, var, val);
 			
 			if (!strcasecmp(var, "trunk_type")) {
-				span->trunk_type = ftdm_str2ftdm_trunk_type(val);
-				ftdm_log(FTDM_LOG_DEBUG, "setting trunk type to '%s'\n", ftdm_trunk_type2str(span->trunk_type)); 
+				ftdm_trunk_type_t trtype = ftdm_str2ftdm_trunk_type(val);
+				ftdm_span_set_trunk_type(span, trtype);
+				ftdm_log(FTDM_LOG_DEBUG, "setting trunk type to '%s'\n", ftdm_trunk_type2str(trtype)); 
 			} else if (!strcasecmp(var, "name")) {
 				if (!strcasecmp(val, "undef")) {
 					chan_config.name[0] = '\0';
@@ -3371,7 +3395,7 @@ static ftdm_status_t load_config(void)
 							ftdm_analog_start_type2str(span->start_type));
 				}
 				if (span->trunk_type == FTDM_TRUNK_FXO) {
-     			unsigned chans_configured = 0;
+					unsigned chans_configured = 0;
 					chan_config.type = FTDM_CHAN_TYPE_FXO;
 					if (ftdm_configure_span_channels(span, val, &chan_config, &chans_configured) == FTDM_SUCCESS) {
 						configured += chans_configured;
diff --git a/libs/freetdm/src/include/freetdm.h b/libs/freetdm/src/include/freetdm.h
index 6a1357f92b..4754edc946 100644
--- a/libs/freetdm/src/include/freetdm.h
+++ b/libs/freetdm/src/include/freetdm.h
@@ -67,7 +67,8 @@ typedef enum {
 	FTDM_MEMERR, /*!< Memory error, most likely allocation failure */
 	FTDM_TIMEOUT, /*!< Operation timed out (ie: polling on a device)*/
 	FTDM_NOTIMPL, /*!< Operation not implemented */
-	FTDM_BREAK /*!< Request the caller to perform a break (context-dependant, ie: stop getting DNIS/ANI) */
+	FTDM_BREAK, /*!< Request the caller to perform a break (context-dependant, ie: stop getting DNIS/ANI) */
+	FTDM_EINVAL /*!< Invalid argument */
 } ftdm_status_t;
 
 /*! \brief FreeTDM bool type. */
@@ -299,6 +300,23 @@ typedef enum {
 /*! \brief Move from string to ftdm_signal_event_t and viceversa */
 FTDM_STR2ENUM_P(ftdm_str2ftdm_signal_event, ftdm_signal_event2str, ftdm_signal_event_t)
 
+/*! \brief Span trunk types */
+typedef enum {
+	FTDM_TRUNK_E1,
+	FTDM_TRUNK_T1,
+	FTDM_TRUNK_J1,
+	FTDM_TRUNK_BRI,
+	FTDM_TRUNK_BRI_PTMP,
+	FTDM_TRUNK_FXO,
+	FTDM_TRUNK_FXS,
+	FTDM_TRUNK_EM,
+	FTDM_TRUNK_NONE
+} ftdm_trunk_type_t;
+#define TRUNK_STRINGS "E1", "T1", "J1", "BRI", "BRI_PTMP", "FXO", "FXS", "EM", "NONE"
+
+/*! \brief Move from string to ftdm_trunk_type_t and viceversa */
+FTDM_STR2ENUM_P(ftdm_str2ftdm_trunk_type, ftdm_trunk_type2str, ftdm_trunk_type_t)
+
 /*! \brief Basic channel configuration provided to ftdm_configure_span_channels */
 typedef struct ftdm_channel_config {    
 	char name[FTDM_MAX_NAME_STR_SZ];
@@ -809,14 +827,18 @@ FT_DECLARE(const char *) ftdm_span_get_last_error(const ftdm_span_t *span);
 /*! 
  * \brief Create a new span (not needed if you are using freetdm.conf)
  *
- * \param fio The I/O interface the span will use
- * \param span Pointer to store the create span
+ * \param iotype The I/O interface type this span will use. 
+ *               This depends on the available I/O modules 
+ *               ftmod_wanpipe = "wanpipe" (Sangoma)
+ *               ftmod_zt = "zt" (DAHDI or Zaptel)
+ *               ftmod_pika "pika" (this one is most likely broken)
  * \param name Name for the span
+ * \param span Pointer to store the create span
  *
  * \retval FTDM_SUCCESS success (the span was created)
  * \retval FTDM_FAIL failure (span was not created)
  */
-FT_DECLARE(ftdm_status_t) ftdm_span_create(ftdm_io_interface_t *fio, ftdm_span_t **span, const char *name);
+FT_DECLARE(ftdm_status_t) ftdm_span_create(const char *iotype, const char *name, ftdm_span_t **span);
 
 /*! 
  * \brief Add a new channel to a span
@@ -1144,8 +1166,39 @@ FT_DECLARE(ftdm_status_t) ftdm_conf_node_add_param(ftdm_conf_node_t *node, const
  * \return FTDM_FAIL failure
  */
 FT_DECLARE(ftdm_status_t) ftdm_conf_node_destroy(ftdm_conf_node_t *node);
+
+/*! 
+ * \brief Create and configure channels in the given span
+ *
+ * \param span The span container
+ * \param str The channel range null terminated string. "1-10", "24" etc
+ * \param chan_config The basic channel configuration for each channel within the range
+ * \param configured Pointer where the number of channels configured will be stored
+ *
+ * \return FTDM_SUCCESS success
+ * \return FTDM_FAIL failure
+ */
 FT_DECLARE(ftdm_status_t) ftdm_configure_span_channels(ftdm_span_t *span, const char *str, ftdm_channel_config_t *chan_config, unsigned *configured);
 
+/*! 
+ * \brief Set the trunk type for a span
+ *        This must be called before configuring any channels within the span
+ *
+ * \param span The span 
+ * \param type The trunk type
+ *
+ */
+FT_DECLARE(void) ftdm_span_set_trunk_type(ftdm_span_t *span, ftdm_trunk_type_t type);
+
+/*! 
+ * \brief Get the trunk type for a span
+ *
+ * \param span The span 
+ *
+ * \return The span trunk type
+ */
+FT_DECLARE(ftdm_trunk_type_t) ftdm_span_get_trunk_type(const ftdm_span_t *span);
+
 /*! 
  * \brief Return the channel identified by the provided id
  *
diff --git a/libs/freetdm/src/include/private/ftdm_types.h b/libs/freetdm/src/include/private/ftdm_types.h
index 668c3f180c..50f3280d61 100644
--- a/libs/freetdm/src/include/private/ftdm_types.h
+++ b/libs/freetdm/src/include/private/ftdm_types.h
@@ -111,20 +111,6 @@ typedef enum {
 #define TONEMAP_STRINGS "NONE", "DIAL", "RING", "BUSY", "FAIL1", "FAIL2", "FAIL3", "ATTN", "CALLWAITING-CAS", "CALLWAITING-SAS", "CALLWAITING-ACK", "INVALID"
 FTDM_STR2ENUM_P(ftdm_str2ftdm_tonemap, ftdm_tonemap2str, ftdm_tonemap_t)
 
-typedef enum {
-	FTDM_TRUNK_E1,
-	FTDM_TRUNK_T1,
-	FTDM_TRUNK_J1,
-	FTDM_TRUNK_BRI,
-	FTDM_TRUNK_BRI_PTMP,
-	FTDM_TRUNK_FXO,
-	FTDM_TRUNK_FXS,
-	FTDM_TRUNK_EM,
-	FTDM_TRUNK_NONE
-} ftdm_trunk_type_t;
-#define TRUNK_STRINGS "E1", "T1", "J1", "BRI", "BRI_PTMP", "FXO", "FXS", "EM", "NONE"
-FTDM_STR2ENUM_P(ftdm_str2ftdm_trunk_type, ftdm_trunk_type2str, ftdm_trunk_type_t)
-
 typedef enum {
 	FTDM_ANALOG_START_KEWL,
 	FTDM_ANALOG_START_LOOP,