[asterisk-commits] rmudgett: branch rmudgett/misdn_facility r168762 - /team/rmudgett/misdn_facil...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Jan 16 12:09:26 CST 2009


Author: rmudgett
Date: Fri Jan 16 12:09:26 2009
New Revision: 168762

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=168762
Log:
*  Convert the misdn_command application to use the AST_STANDARD_APP_ARGS
macro so it will work with 1.4 and trunk level code.  Also improved
the error checking of the parameters.

Modified:
    team/rmudgett/misdn_facility/channels/chan_misdn.c

Modified: team/rmudgett/misdn_facility/channels/chan_misdn.c
URL: http://svn.digium.com/svn-view/asterisk/team/rmudgett/misdn_facility/channels/chan_misdn.c?view=diff&rev=168762&r1=168761&r2=168762
==============================================================================
--- team/rmudgett/misdn_facility/channels/chan_misdn.c (original)
+++ team/rmudgett/misdn_facility/channels/chan_misdn.c Fri Jan 16 12:09:26 2009
@@ -6453,6 +6453,7 @@
 	 * NOTE: The variable is not available until after misdn_request()
 	 * has been called.
 	 */
+	ast_channel_lock(ast);
 	chan_value = pbx_builtin_getvar_helper(ast, MISDN_CC_PEER_ID);
 	if (chan_value) {
 		int peer_id;
@@ -6462,6 +6463,7 @@
 		chan_misdn_log(3, port, " --> Found %s:%s, peer:%s\n",
 			MISDN_CC_PEER_ID, chan_value, ch->peer ? "available" : "NULL");
 	}
+	ast_channel_unlock(ast);
 
 	if (ch->record_id != -1) {
 		struct misdn_cc_record *cc_record;
@@ -7853,7 +7855,7 @@
 }
 
 static struct ast_channel_tech misdn_tech = {
-	.type = "mISDN",
+	.type = misdn_type,
 	.description = "Channel driver for mISDN Support (Bri/Pri)",
 	.capabilities = AST_FORMAT_ALAW ,
 	.requester = misdn_request,
@@ -7872,7 +7874,7 @@
 };
 
 static struct ast_channel_tech misdn_tech_wo_bridge = {
-	.type = "mISDN",
+	.type = misdn_type,
 	.description = "Channel driver for mISDN Support (Bri/Pri)",
 	.capabilities = AST_FORMAT_ALAW ,
 	.requester = misdn_request,
@@ -10572,6 +10574,16 @@
 
 
 
+#if defined(AST_MISDN_ENHANCEMENTS)
+/*! 
+* \brief misdn_command arguments container.
+*/
+AST_DEFINE_APP_ARGS_TYPE(misdn_command_args,
+	AST_APP_ARG(name);			/* Subcommand name */
+	AST_APP_ARG(arg)[10 + 1];	/* Subcommand arguments */
+);
+#endif	/* defined(AST_MISDN_ENHANCEMENTS) */
+
 /* ******************************************************************* */
 #if defined(AST_MISDN_ENHANCEMENTS)
 /*!
@@ -10579,13 +10591,12 @@
  * \brief misdn_command(cc-initialize) subcommand handler
  *
  * \param chan Asterisk channel to operate upon.
- * \param cmd_name subcommand name to use in error messages
- * \param options subcommand options string
+ * \param subcommand Arguments for the subcommand
  *
  * \retval 0 on success.
  * \retval -1 on error.
  */
-static int misdn_command_cc_initialize(struct ast_channel *chan, const char *cmd_name, char *options)
+static int misdn_command_cc_initialize(struct ast_channel *chan, struct misdn_command_args *subcommand)
 {
 	int peer_id;
 	char buf[32];
@@ -10617,16 +10628,13 @@
  * Deactivate a call completion service instance.
  *
  * \param chan Asterisk channel to operate upon.
- * \param cmd_name subcommand name to use in error messages
- * \param options subcommand options string
+ * \param subcommand Arguments for the subcommand
  *
  * \retval 0 on success.
  * \retval -1 on error.
  */
-static int misdn_command_cc_deactivate(struct ast_channel *chan, const char *cmd_name, char *options)
-{
-	char *opt;
-	char *tail;
+static int misdn_command_cc_deactivate(struct ast_channel *chan, struct misdn_command_args *subcommand)
+{
 	int record_id;
 	const char *error_str;
 	struct misdn_cc_record *cc_record;
@@ -10635,13 +10643,11 @@
 
 	static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID})\n";
 
-	tail = NULL;/* To prevent compiler warning */
-	opt = strtok_r(options, "|", &tail);
-	if (!opt || !isdigit(*opt)) {
-		ast_log(LOG_WARNING, cmd_help, misdn_command_name, cmd_name);
+	if (ast_strlen_zero(subcommand->arg[0]) || !isdigit(*subcommand->arg[0])) {
+		ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
 		return -1;
 	}
-	record_id = atoi(opt);
+	record_id = atoi(subcommand->arg[0]);
 
 	ast_mutex_lock(&misdn_cc_record_lock);
 	cc_record = misdn_cc_find_by_id(record_id);
@@ -10703,7 +10709,7 @@
 	if (error_str) {
 		if (option_verbose) {
 			ast_verbose(VERBOSE_PREFIX_1 "%s(%s) diagnostic '%s' on channel %s\n",
-				misdn_command_name, cmd_name, error_str, chan->name);
+				misdn_command_name, subcommand->name, error_str, chan->name);
 		}
 		pbx_builtin_setvar_helper(chan, MISDN_ERROR_MSG, error_str);
 	}
@@ -10726,16 +10732,13 @@
  * Set the status of User-A for a call completion service instance.
  *
  * \param chan Asterisk channel to operate upon.
- * \param cmd_name subcommand name to use in error messages
- * \param options subcommand options string
+ * \param subcommand Arguments for the subcommand
  *
  * \retval 0 on success.
  * \retval -1 on error.
  */
-static int misdn_command_cc_a_busy(struct ast_channel *chan, const char *cmd_name, char *options)
-{
-	char *opt;
-	char *tail;
+static int misdn_command_cc_a_busy(struct ast_channel *chan, struct misdn_command_args *subcommand)
+{
 	int record_id;
 	int party_a_free;
 	struct misdn_cc_record *cc_record;
@@ -10743,23 +10746,19 @@
 
 	static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<yes/no>)\n";
 
-	tail = NULL;/* To prevent compiler warning */
-	opt = strtok_r(options, "|", &tail);
-	if (!opt || !isdigit(*opt)) {
-		ast_log(LOG_WARNING, cmd_help, misdn_command_name, cmd_name);
+	if (ast_strlen_zero(subcommand->arg[0]) || !isdigit(*subcommand->arg[0])) {
+		ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
 		return -1;
 	}
-	record_id = atoi(opt);
-
-	opt = strtok_r(NULL, "|", &tail);
-	if (!opt) {
-		ast_log(LOG_WARNING, cmd_help, misdn_command_name, cmd_name);
+	record_id = atoi(subcommand->arg[0]);
+
+	if (ast_true(subcommand->arg[1])) {
+		party_a_free = 0;
+	} else if (ast_false(subcommand->arg[1])) {
+		party_a_free = 1;
+	} else {
+		ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
 		return -1;
-	}
-	if (strcasecmp(opt, "yes") == 0) {
-		party_a_free = 0;
-	} else {
-		party_a_free = 1;
 	}
 
 	ast_mutex_lock(&misdn_cc_record_lock);
@@ -10807,16 +10806,14 @@
  * Set the dialplan location to notify when User-B is free and User-A is busy.
  *
  * \param chan Asterisk channel to operate upon.
- * \param cmd_name subcommand name to use in error messages
- * \param options subcommand options string
+ * \param subcommand Arguments for the subcommand
  *
  * \retval 0 on success.
  * \retval -1 on error.
  */
-static int misdn_command_cc_b_free(struct ast_channel *chan, const char *cmd_name, char *options)
-{
-	char *opt;
-	char *tail;
+static int misdn_command_cc_b_free(struct ast_channel *chan, struct misdn_command_args *subcommand)
+{
+	unsigned index;
 	int record_id;
 	int priority;
 	char *context;
@@ -10825,34 +10822,24 @@
 
 	static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)\n";
 
-	tail = NULL;/* To prevent compiler warning */
-	opt = strtok_r(options, "|", &tail);
-	if (!opt || !isdigit(*opt)) {
-		ast_log(LOG_WARNING, cmd_help, misdn_command_name, cmd_name);
+	/* Check that all arguments are present */
+	for (index = 0; index < 4; ++index) {
+		if (ast_strlen_zero(subcommand->arg[index])) {
+			ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
+			return -1;
+		}
+	}	/* end for */
+
+	/* These must be numeric */
+	if (!isdigit(*subcommand->arg[0]) || !isdigit(*subcommand->arg[3])) {
+		ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
 		return -1;
 	}
-	record_id = atoi(opt);
-
-	opt = strtok_r(NULL, "|", &tail);
-	if (!opt) {
-		ast_log(LOG_WARNING, cmd_help, misdn_command_name, cmd_name);
-		return -1;
-	}
-	context = opt;
-
-	opt = strtok_r(NULL, "|", &tail);
-	if (!opt) {
-		ast_log(LOG_WARNING, cmd_help, misdn_command_name, cmd_name);
-		return -1;
-	}
-	exten = opt;
-
-	opt = strtok_r(NULL, "|", &tail);
-	if (!opt || !isdigit(*opt)) {
-		ast_log(LOG_WARNING, cmd_help, misdn_command_name, cmd_name);
-		return -1;
-	}
-	priority = atoi(opt);
+
+	record_id = atoi(subcommand->arg[0]);
+	context = subcommand->arg[1];
+	exten = subcommand->arg[2];
+	priority = atoi(subcommand->arg[3]);
 
 	ast_mutex_lock(&misdn_cc_record_lock);
 	cc_record = misdn_cc_find_by_id(record_id);
@@ -10890,17 +10877,15 @@
  * Set the dialplan location to notify when User-B is free and User-A is free.
  *
  * \param chan Asterisk channel to operate upon.
- * \param cmd_name subcommand name to use in error messages
- * \param options subcommand options string
+ * \param subcommand Arguments for the subcommand
  * \param request Which call-completion request message to generate.
  *
  * \retval 0 on success.
  * \retval -1 on error.
  */
-static int misdn_command_cc_request(struct ast_channel *chan, const char *cmd_name, char *options, const struct misdn_cc_request *request)
-{
-	char *opt;
-	char *tail;
+static int misdn_command_cc_request(struct ast_channel *chan, struct misdn_command_args *subcommand, const struct misdn_cc_request *request)
+{
+	unsigned index;
 	int record_id;
 	int priority;
 	char *context;
@@ -10913,34 +10898,24 @@
 
 	static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)\n";
 
-	tail = NULL;/* To prevent compiler warning */
-	opt = strtok_r(options, "|", &tail);
-	if (!opt || !isdigit(*opt)) {
-		ast_log(LOG_WARNING, cmd_help, misdn_command_name, cmd_name);
+	/* Check that all arguments are present */
+	for (index = 0; index < 4; ++index) {
+		if (ast_strlen_zero(subcommand->arg[index])) {
+			ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
+			return -1;
+		}
+	}	/* end for */
+
+	/* These must be numeric */
+	if (!isdigit(*subcommand->arg[0]) || !isdigit(*subcommand->arg[3])) {
+		ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
 		return -1;
 	}
-	record_id = atoi(opt);
-
-	opt = strtok_r(NULL, "|", &tail);
-	if (!opt) {
-		ast_log(LOG_WARNING, cmd_help, misdn_command_name, cmd_name);
-		return -1;
-	}
-	context = opt;
-
-	opt = strtok_r(NULL, "|", &tail);
-	if (!opt) {
-		ast_log(LOG_WARNING, cmd_help, misdn_command_name, cmd_name);
-		return -1;
-	}
-	exten = opt;
-
-	opt = strtok_r(NULL, "|", &tail);
-	if (!opt || !isdigit(*opt)) {
-		ast_log(LOG_WARNING, cmd_help, misdn_command_name, cmd_name);
-		return -1;
-	}
-	priority = atoi(opt);
+
+	record_id = atoi(subcommand->arg[0]);
+	context = subcommand->arg[1];
+	exten = subcommand->arg[2];
+	priority = atoi(subcommand->arg[3]);
 
 	ast_mutex_lock(&misdn_cc_record_lock);
 	cc_record = misdn_cc_find_by_id(record_id);
@@ -11062,7 +11037,7 @@
 	if (error_str) {
 		if (option_verbose) {
 			ast_verbose(VERBOSE_PREFIX_1 "%s(%s) diagnostic '%s' on channel %s\n",
-				misdn_command_name, cmd_name, error_str, chan->name);
+				misdn_command_name, subcommand->name, error_str, chan->name);
 		}
 		pbx_builtin_setvar_helper(chan, MISDN_ERROR_MSG, error_str);
 		pbx_builtin_setvar_helper(chan, MISDN_CC_STATUS, "ERROR");
@@ -11088,20 +11063,19 @@
  * Set the dialplan location to notify when User-B is free and User-A is free.
  *
  * \param chan Asterisk channel to operate upon.
- * \param cmd_name subcommand name to use in error messages
- * \param options subcommand options string
+ * \param subcommand Arguments for the subcommand
  *
  * \retval 0 on success.
  * \retval -1 on error.
  */
-static int misdn_command_ccbs_request(struct ast_channel *chan, const char *cmd_name, char *options)
+static int misdn_command_ccbs_request(struct ast_channel *chan, struct misdn_command_args *subcommand)
 {
 	static const struct misdn_cc_request request = {
 		.ptmp = Fac_CCBSRequest,
 		.ptp = Fac_CCBS_T_Request
 	};
 
-	return misdn_command_cc_request(chan, cmd_name, options, &request);
+	return misdn_command_cc_request(chan, subcommand, &request);
 }	/* end misdn_command_ccbs_request() */
 #endif	/* defined(AST_MISDN_ENHANCEMENTS) */
 
@@ -11119,20 +11093,19 @@
  * Set the dialplan location to notify when User-B is free and User-A is free.
  *
  * \param chan Asterisk channel to operate upon.
- * \param cmd_name subcommand name to use in error messages
- * \param options subcommand options string
+ * \param subcommand Arguments for the subcommand
  *
  * \retval 0 on success.
  * \retval -1 on error.
  */
-static int misdn_command_ccnr_request(struct ast_channel *chan, const char *cmd_name, char *options)
+static int misdn_command_ccnr_request(struct ast_channel *chan, struct misdn_command_args *subcommand)
 {
 	static const struct misdn_cc_request request = {
 		.ptmp = Fac_CCNRRequest,
 		.ptp = Fac_CCNR_T_Request
 	};
 
-	return misdn_command_cc_request(chan, cmd_name, options, &request);
+	return misdn_command_cc_request(chan, subcommand, &request);
 }	/* end misdn_command_ccnr_request() */
 #endif	/* defined(AST_MISDN_ENHANCEMENTS) */
 
@@ -11150,7 +11123,7 @@
 	const char *name;
 
 	/*! \brief subcommand handler */
-	int (*func)(struct ast_channel *chan, const char *cmd_name, char *options);
+	int (*func)(struct ast_channel *chan, struct misdn_command_args *subcommand);
 
 	/*! \brief TRUE if the subcommand can only be executed on mISDN channels */
 	int misdn_only;
@@ -11186,35 +11159,39 @@
 static int misdn_command_exec(struct ast_channel *chan, void *data)
 {
 	char *parse;
-	char *cmd;
-	char *options;
 	unsigned index;
-
-	if (ast_strlen_zero((char *)data)) {
-		ast_log(LOG_WARNING, "%s requires arguments\n", misdn_command_name);
+	struct misdn_command_args subcommand;
+
+	if (ast_strlen_zero((char *) data)) {
+		ast_log(LOG_ERROR, "%s requires arguments\n", misdn_command_name);
 		return -1;
 	}
 
 	ast_log(LOG_DEBUG, "%s(%s)\n", misdn_command_name, (char *) data);
 
 	parse = ast_strdupa(data);
-	options = NULL;/* To prevent compiler warning */
-	cmd = strtok_r(parse, "|", &options);
+	AST_STANDARD_APP_ARGS(subcommand, parse);
+	if (!subcommand.argc || ast_strlen_zero(subcommand.name)) {
+		ast_log(LOG_ERROR, "%s requires a subcommand\n", misdn_command_name);
+		return -1;
+	}
 
 	for (index = 0; index < ARRAY_LEN(misdn_commands); ++index) {
-		if (strcasecmp(misdn_commands[index].name, cmd) == 0) {
+		if (strcasecmp(misdn_commands[index].name, subcommand.name) == 0) {
+			strcpy(subcommand.name, misdn_commands[index].name);
 			if (misdn_commands[index].misdn_only
-				&& strcasecmp(chan->tech->type, "mISDN") != 0) {
+				&& strcasecmp(chan->tech->type, misdn_type) != 0) {
 				ast_log(LOG_WARNING,
-					"%s(%s) only makes sense with chan_misdn channels!\n",
-					misdn_command_name, misdn_commands[index].name);
+					"%s(%s) only makes sense with %s channels!\n",
+					misdn_command_name, subcommand.name, misdn_type);
 				return -1;
 			}
-			return misdn_commands[index].func(chan, misdn_commands[index].name, options);
+			return misdn_commands[index].func(chan, &subcommand);
 		}
 	}	/* end for */
 
-	ast_log(LOG_ERROR, "%s(%s) is unknown\n", misdn_command_name, cmd);
+	ast_log(LOG_WARNING, "%s(%s) subcommand is unknown\n", misdn_command_name,
+		subcommand.name);
 	return -1;
 }	/* end misdn_command_exec() */
 #endif	/* defined(AST_MISDN_ENHANCEMENTS) */
@@ -11235,8 +11212,8 @@
 
 	chan_misdn_log(0, 0, "TYPE: %s\n", chan->tech->type);
 	
-	if (strcasecmp(chan->tech->type, "mISDN")) {
-		ast_log(LOG_WARNING, "misdn_facility only makes sense with chan_misdn channels!\n");
+	if (strcasecmp(chan->tech->type, misdn_type)) {
+		ast_log(LOG_WARNING, "misdn_facility only makes sense with %s channels!\n", misdn_type);
 		return -1;
 	}
 	
@@ -11386,8 +11363,8 @@
 	int txgain = 0;
 	int change_jitter = 0;
 
-	if (strcasecmp(chan->tech->type, "mISDN")) {
-		ast_log(LOG_WARNING, "misdn_set_opt makes sense only with chan_misdn channels!\n");
+	if (strcasecmp(chan->tech->type, misdn_type)) {
+		ast_log(LOG_WARNING, "misdn_set_opt makes sense only with %s channels!\n", misdn_type);
 		return -1;
 	}
 	




More information about the asterisk-commits mailing list