[asterisk-addons-commits] mnicholson: branch mnicholson/chan-mobile-refactor r754 - /team/mnicholson/ch...

SVN commits to the Asterisk addons project asterisk-addons-commits at lists.digium.com
Tue Jan 27 16:14:01 CST 2009


Author: mnicholson
Date: Tue Jan 27 16:14:01 2009
New Revision: 754

URL: http://svn.digium.com/svn-view/asterisk-addons?view=rev&rev=754
Log:
refactored configuration file loading

Modified:
    team/mnicholson/chan-mobile-refactor/channels/chan_mobile.c

Modified: team/mnicholson/chan-mobile-refactor/channels/chan_mobile.c
URL: http://svn.digium.com/svn-view/asterisk-addons/team/mnicholson/chan-mobile-refactor/channels/chan_mobile.c?view=diff&rev=754&r1=753&r2=754
==============================================================================
--- team/mnicholson/chan-mobile-refactor/channels/chan_mobile.c (original)
+++ team/mnicholson/chan-mobile-refactor/channels/chan_mobile.c Tue Jan 27 16:14:01 2009
@@ -1955,159 +1955,189 @@
 static int mbl_load_config(void)
 {
 
-	struct ast_config *cfg = NULL;
-	char *cat = NULL;
-	struct ast_variable *var;
-	const char *id, *address, *useadapter, *port, *context, *type, *skip, *group, *master, *nocallsetup, *aligndetect, *blackberry;
+	struct ast_config *cfg;
+	struct ast_variable *v;
+	const char *cat, *id, *address, *adapter_str, *port;
 	struct mbl_pvt *pvt;
 	struct adapter_pvt *adapter;
 	uint16_t vs;
 	struct hci_dev_req dr;
-	char nadapters = 0;
 	struct ast_flags config_flags = { 0 };
 
 	cfg = ast_config_load(MBL_CONFIG, config_flags);
 	if (!cfg)
-		return 0;
-
-	for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
-		if (!strcasecmp(var->name, "interval"))
-			discovery_interval = atoi(var->value);
-	}
-
-	/* load adapters first */
-	cat = ast_category_browse(cfg, NULL);
-	while (cat) {
+		return -1;
+
+	/* parse [general] section */
+	for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
+		if (!strcasecmp(v->name, "interval")) {
+			discovery_interval = atoi(v->value);
+		}
+	}
+
+	/* load adapters */
+	for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
 		if (!strcasecmp(cat, "adapter")) {
 			id = ast_variable_retrieve(cfg, cat, "id");
 			address = ast_variable_retrieve(cfg, cat, "address");
-			master = ast_variable_retrieve(cfg, cat, "forcemaster");
-			aligndetect = ast_variable_retrieve(cfg, cat, "alignmentdetection");
-			ast_debug(1, "Loading adapter %s %s.\n", id, address);
-			if (!ast_strlen_zero(id) && !ast_strlen_zero(address)) {
-				if ((adapter = ast_calloc(1, sizeof(*adapter)))) {
-					ast_copy_string(adapter->id, id, sizeof(adapter->id));
-					str2ba(address, &adapter->addr);
-					if (!ast_strlen_zero(aligndetect)) {
-						if (*aligndetect == 'Y' || *aligndetect == 'y')
-							adapter->alignment_detection = 1;
-					}
-					adapter->dev_id = hci_devid(address);
-					adapter->hci_socket = hci_open_dev(adapter->dev_id);
-					if (adapter->dev_id < 0 || adapter->hci_socket < 0) {
-						ast_log(LOG_ERROR, "Unable to open adapter %s. It won't be enabled.\n", adapter->id);
-						ast_free(adapter);
-					} else {
-						if ((master) && (*master)) {
-							dr.dev_id = adapter->dev_id;
-							if (hci_strtolm("master", &dr.dev_opt)) {
-								if (ioctl(adapter->hci_socket, HCISETLINKMODE, (unsigned long) &dr) < 0) {
-									ast_log(LOG_WARNING, "Unable to set adapter %s link mode to MASTER.\n", adapter->id);
-								}
+
+			if (ast_strlen_zero(id) || ast_strlen_zero(address)) {
+				ast_log(LOG_ERROR, "Skipping adapter. Missing id or address settings.\n");
+				continue;
+			}
+			
+			ast_debug(1, "Reading cinfiguration for adapter %s %s.\n", id, address);
+				
+			if (!(adapter = ast_calloc(1, sizeof(*adapter)))) {
+				ast_log(LOG_ERROR, "Skipping adapter %s. Error allocating memory.\n", id);
+				continue;
+			}
+					
+			ast_copy_string(adapter->id, id, sizeof(adapter->id));
+			str2ba(address, &adapter->addr);
+					
+			/* attempt to connect to the adapter */
+			adapter->dev_id = hci_devid(address);
+			adapter->hci_socket = hci_open_dev(adapter->dev_id);
+			if (adapter->dev_id < 0 || adapter->hci_socket < 0) {
+				ast_log(LOG_ERROR, "Skipping adapter %s. Unable to communicate with adapter.\n", adapter->id);
+				ast_free(adapter);
+				continue;
+			}
+						
+			/* check voice setting */
+			hci_read_voice_setting(adapter->hci_socket, &vs, 1000);
+			vs = htobs(vs);
+			if (vs != 0x0060) {
+				ast_log(LOG_ERROR, "Skipping adapter %s. Voice setting must be 0x0060 - see 'man hciconfig' for details.\n", adapter->id);
+				hci_close_dev(adapter->hci_socket);
+				ast_free(adapter);
+				continue;
+			}
+			
+			for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
+				if (!strcasecmp(v->name, "forcemaster")) {
+					if (ast_true(v->value)) {
+						dr.dev_id = adapter->dev_id;
+						if (hci_strtolm("master", &dr.dev_opt)) {
+							if (ioctl(adapter->hci_socket, HCISETLINKMODE, (unsigned long) &dr) < 0) {
+								ast_log(LOG_WARNING, "Unable to set adapter %s link mode to MASTER. Ignoring 'forcemaster' option.\n", adapter->id);
 							}
 						}
-						hci_read_voice_setting(adapter->hci_socket, &vs, 1000);
-						vs = htobs(vs);
-						if (vs != 0x0060) {
-							ast_log(LOG_ERROR, "Incorrect voice setting for adapter %s, it must be 0x0060 - see 'man hciconfig' for details.\n", adapter->id);
-							hci_close_dev(adapter->hci_socket);
-							ast_free(adapter);
-						} else {
-							AST_RWLIST_WRLOCK(&adapters);
-							AST_RWLIST_INSERT_HEAD(&adapters, adapter, entry);
-							AST_RWLIST_UNLOCK(&adapters);
-							nadapters++;
-						}
 					}
-				}
-			} else
-				ast_log(LOG_ERROR, "id/address missing for adapter %s. It won't be enabled.\n", cat);
-		}
-		cat = ast_category_browse(cfg, cat);
-	}
-
-	if (!nadapters) {
-		ast_log(LOG_WARNING, "***********************************************************************\n");
-		ast_log(LOG_WARNING, "No Adapters defined. Please review mobile.conf. See sample for details.\n");
-		ast_log(LOG_WARNING, "***********************************************************************\n");
+				} else if (!strcasecmp(v->name, "alignmentdetection")) {
+					adapter->alignment_detection = ast_true(v->value);
+				}
+			}
+				
+			AST_RWLIST_WRLOCK(&adapters);
+			AST_RWLIST_INSERT_HEAD(&adapters, adapter, entry);
+			AST_RWLIST_UNLOCK(&adapters);
+			ast_debug(1, "Loaded adapter %s %s.\n", adapter->id, address);
+		}
+	}
+
+	if (AST_RWLIST_EMPTY(&adapters)) {
+		ast_log(LOG_ERROR,
+			"***********************************************************************\n"
+			"No adapters could be loaded from the configuration file.\n"
+			"Please review mobile.conf. See sample for details.\n"
+			"***********************************************************************\n"
+		       );
+		ast_config_destroy(cfg);
+		return -1;
 	}
 
 	/* now load devices */
-	cat = ast_category_browse(cfg, NULL);
-	while (cat) {
+	for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
 		if (strcasecmp(cat, "general") && strcasecmp(cat, "adapter")) {
-			ast_debug(1, "Loading device %s.\n", cat);
+			ast_debug(1, "Reading configuration for device %s.\n", cat);
+
+			adapter_str = ast_variable_retrieve(cfg, cat, "adapter");
+			if(ast_strlen_zero(adapter_str)) {
+				ast_log(LOG_ERROR, "Skipping device %s. No adapter specified.\n", cat);
+				continue;
+			}
+				
+			/* find the adapter */
+			AST_RWLIST_RDLOCK(&adapters);
+			AST_RWLIST_TRAVERSE(&adapters, adapter, entry) {
+				if (!strcmp(adapter->id, adapter_str))
+					break;
+			}
+			AST_RWLIST_UNLOCK(&adapters);
+			if (!adapter) {
+				ast_log(LOG_ERROR, "Skiping device %s. Unknown adapter '%s' specified.\n", cat, adapter_str);
+				continue;
+			}
+			
 			address = ast_variable_retrieve(cfg, cat, "address");
-			useadapter = ast_variable_retrieve(cfg, cat, "adapter");
 			port = ast_variable_retrieve(cfg, cat, "port");
-			context = ast_variable_retrieve(cfg, cat, "context");
-			type = ast_variable_retrieve(cfg, cat, "type");
-			skip = ast_variable_retrieve(cfg, cat, "dtmfskip");
-			group = ast_variable_retrieve(cfg, cat, "group");
-			nocallsetup = ast_variable_retrieve(cfg, cat, "nocallsetup");
-			blackberry = ast_variable_retrieve(cfg, cat, "blackberry");
-			if (!ast_strlen_zero(address) && !ast_strlen_zero(port) && !ast_strlen_zero(useadapter)) {
-				/* find the adapter */
-				AST_RWLIST_RDLOCK(&adapters);
-				AST_RWLIST_TRAVERSE(&adapters, adapter, entry) {
-					if (!strcmp(adapter->id, useadapter))
-						break;
-				}
-				AST_RWLIST_UNLOCK(&adapters);
-				if (!adapter) {
-					ast_log(LOG_ERROR, "Device %s configured to use unknown adapter %s. It won't be enabled.\n", cat, useadapter);
-					break;
-				}
-				if ((pvt = ast_calloc(1, sizeof(*pvt)))) {
-					if (type && !strcmp(type, "headset"))
+			if (ast_strlen_zero(port) || ast_strlen_zero(address)) {
+				ast_log(LOG_ERROR, "Skipping device %s. Missing required port or address setting.\n", cat);
+				continue;
+			}
+				
+			if (!(pvt = ast_calloc(1, sizeof(*pvt)))) {
+				ast_log(LOG_ERROR, "Skipping device %s. Error allocating memory.\n", cat);
+				continue;
+			}
+
+			/* set some defaults */
+
+			pvt->type = MBL_TYPE_PHONE;
+			pvt->dtmf_skip = 200;
+			ast_copy_string(pvt->context, "default", sizeof(pvt->context));
+			
+			/* populate the pvt structure */
+			pvt->adapter = adapter;
+			ast_copy_string(pvt->id, cat, sizeof(pvt->id));
+			str2ba(address, &pvt->addr);
+			pvt->state = MBL_STATE_INIT;
+			pvt->rfcomm_socket = -1;
+			pvt->rfcomm_port = atoi(port);
+			pvt->sco_socket = -1;
+			pvt->monitor_thread = AST_PTHREADT_NULL;
+			pvt->sco_listener_thread = AST_PTHREADT_NULL;
+
+			pvt->dsp = ast_dsp_new();
+			ast_dsp_set_features(pvt->dsp, DSP_FEATURE_DIGIT_DETECT);
+			ast_dsp_set_digitmode(pvt->dsp, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
+
+			for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
+				if (!strcasecmp(v->name, "type")) {
+					if (!strcasecmp(v->value, "headset"))
 						pvt->type = MBL_TYPE_HEADSET;
 					else
 						pvt->type = MBL_TYPE_PHONE;
-
-					if (blackberry)
-						pvt->blackberry = ast_true(blackberry);
-
-					ast_copy_string(pvt->id, cat, sizeof(pvt->id));
-					str2ba(address, &pvt->addr);
-					ast_copy_string(pvt->context, S_OR(context, "default"), sizeof(pvt->context));
-					if (group)
-						pvt->group = atoi(group);	/* group 0 if invalid */
-					pvt->state = MBL_STATE_INIT;
-					pvt->rfcomm_socket = -1;
-					pvt->rfcomm_port = atoi(port);
-					pvt->sco_socket = -1;
-					pvt->monitor_thread = AST_PTHREADT_NULL;
-					pvt->sco_listener_thread = AST_PTHREADT_NULL;
-					if (!ast_strlen_zero(nocallsetup)) {
-						if ((*nocallsetup == 'y') || (*nocallsetup == 'Y')) {
-							pvt->no_callsetup = 1;
-							ast_debug(1, "Setting nocallsetup mode for device %s.\n", pvt->id);
-						}
-					}
-					pvt->dsp = ast_dsp_new();
-					if (skip) {
-						if ((pvt->dtmf_skip = atoi(skip)) == 0)
-							pvt->dtmf_skip = 200;
-					} else
+				} else if (!strcasecmp(v->name, "context")) {
+					ast_copy_string(pvt->context, v->value, sizeof(pvt->context));
+				} else if (!strcasecmp(v->name, "group")) {
+					/* group is set to 0 if invalid */
+					pvt->group = atoi(v->value);
+				} else if (!strcasecmp(v->name, "dtmfskip")) {
+					if ((pvt->dtmf_skip = atoi(v->value)) == 0)
 						pvt->dtmf_skip = 200;
-					ast_dsp_set_features(pvt->dsp, DSP_FEATURE_DIGIT_DETECT);
-					ast_dsp_set_digitmode(pvt->dsp, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
-					pvt->adapter = adapter;
-					AST_RWLIST_WRLOCK(&devices);
-					AST_RWLIST_INSERT_HEAD(&devices, pvt, entry);
-					AST_RWLIST_UNLOCK(&devices);
-				}
-			} else {
-				ast_log(LOG_ERROR, "Device %s has no address/port/adapter configured. It won't be enabled.\n", cat);
+				} else if (!strcasecmp(v->name, "nocallsetup")) {
+					pvt->no_callsetup = ast_true(v->value);
+
+					if (pvt->no_callsetup)
+						ast_debug(1, "Setting nocallsetup mode for device %s.\n", pvt->id);
+				} else if (!strcasecmp(v->name, "blackberry")) {
+					pvt->blackberry = ast_true(v->value);
+				}
 			}
-		}
-		cat = ast_category_browse(cfg, cat);
+
+			AST_RWLIST_WRLOCK(&devices);
+			AST_RWLIST_INSERT_HEAD(&devices, pvt, entry);
+			AST_RWLIST_UNLOCK(&devices);
+			ast_debug(1, "Loaded device %s.\n", pvt->id);
+		}
 	}
 
 	ast_config_destroy(cfg);
 
-	return 1;
-
+	return 0;
 }
 
 static int unload_module(void)
@@ -2179,14 +2209,14 @@
 	dev_id = hci_get_route(NULL);
 	s = hci_open_dev(dev_id);
 	if (dev_id < 0 || s < 0) {
-		ast_log(LOG_ERROR, "No Bluetooth device found. Not loading module.\n");
+		ast_log(LOG_ERROR, "No Bluetooth devices found. Not loading module.\n");
 		return AST_MODULE_LOAD_DECLINE;
 	}
 
 	hci_close_dev(s);
 
-	if (!mbl_load_config()) {
-		ast_log(LOG_ERROR, "Unable to read config file %s. Not loading module.\n", MBL_CONFIG);
+	if (mbl_load_config()) {
+		ast_log(LOG_ERROR, "Errors reading config file %s. Not loading module.\n", MBL_CONFIG);
 		return AST_MODULE_LOAD_DECLINE;
 	}
 




More information about the asterisk-addons-commits mailing list