[asterisk-commits] branch oej/metermaids-trunk r11847 - in /team/oej/metermaids-trunk: ./ channe...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Sun Mar 5 05:04:18 MST 2006


Author: oej
Date: Sun Mar  5 06:04:14 2006
New Revision: 11847

URL: http://svn.digium.com/view/asterisk?rev=11847&view=rev
Log:
Adding metermaids to trunk (old branch did not work any more for some reason)

Modified:
    team/oej/metermaids-trunk/   (props changed)
    team/oej/metermaids-trunk/channels/chan_local.c
    team/oej/metermaids-trunk/include/asterisk/features.h
    team/oej/metermaids-trunk/res/res_features.c

Propchange: team/oej/metermaids-trunk/
------------------------------------------------------------------------------
    svnmerge-integrated = /trunk:1-10648

Modified: team/oej/metermaids-trunk/channels/chan_local.c
URL: http://svn.digium.com/view/asterisk/team/oej/metermaids-trunk/channels/chan_local.c?rev=11847&r1=11846&r2=11847&view=diff
==============================================================================
--- team/oej/metermaids-trunk/channels/chan_local.c (original)
+++ team/oej/metermaids-trunk/channels/chan_local.c Sun Mar  5 06:04:14 2006
@@ -60,9 +60,13 @@
 #include "asterisk/musiconhold.h"
 #include "asterisk/manager.h"
 #include "asterisk/stringfields.h"
+#include "asterisk/devicestate.h"
+#include "asterisk/features.h"
 
 static const char desc[] = "Local Proxy Channel";
 static const char tdesc[] = "Local Proxy Channel Driver";
+
+static int watchid;	/* Metermaid watcher ID assigned by res_features */
 
 static int usecnt =0;
 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
@@ -96,6 +100,7 @@
 	.indicate = local_indicate,
 	.fixup = local_fixup,
 	.send_html = local_sendhtml,
+	.devicestate = local_devicestate,
 };
 
 struct local_pvt {
@@ -114,6 +119,37 @@
 };
 
 static AST_LIST_HEAD_STATIC(locals, local_pvt);
+
+/*! \brief Watcher callback for extension state changes in res_features */
+void local_watcher(char *exten, char *context) {
+	if (option_debug > 1)
+		ast_log(LOG_DEBUG, "Got notification of state change for %s@%s\n", exten, context);
+	ast_device_state_changed("local/%s@%s", exten, context);
+	return;
+}
+
+/*! ! \brief Adds devicestate to extensions (for parking mostly) 
+*/
+static int local_devicestate(void *data)
+{
+	char *exten;
+	char *context;
+
+	int res = AST_DEVICE_INVALID;
+		
+	exten = ast_strdupa(data);
+	if ((context  = strchr(exten, '@'))) {
+		*context = '\0';
+		context = context + 1;
+	}
+	if (option_debug > 2)
+		ast_log(LOG_DEBUG, "Checking if extension %s@%s exists (devicestate)\n", exten, context);
+	res = ast_exists_extension(NULL, context, exten, 1, NULL);
+	if (!res)
+		return AST_EXTENSION_UNAVAILABLE;
+	else
+		return AST_EXTENSION_NOT_INUSE;
+}
 
 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f, struct ast_channel *us)
 {
@@ -573,6 +609,9 @@
 		ast_log(LOG_ERROR, "Unable to register channel class 'Local'\n");
 		return -1;
 	}
+
+	/* Register watcher for parking lots in res_features */
+	watchid = ast_park_metermaid_add(&local_watcher, NULL);
 	ast_cli_register(&cli_show_locals);
 	return 0;
 }
@@ -590,6 +629,7 @@
 
 	/* First, take us out of the channel loop */
 	ast_cli_unregister(&cli_show_locals);
+	ast_park_metermaid_remove(watchid);
 	ast_channel_unregister(&local_tech);
 	if (!AST_LIST_LOCK(&locals)) {
 		/* Hangup all interfaces if they have an owner */

Modified: team/oej/metermaids-trunk/include/asterisk/features.h
URL: http://svn.digium.com/view/asterisk/team/oej/metermaids-trunk/include/asterisk/features.h?rev=11847&r1=11846&r2=11847&view=diff
==============================================================================
--- team/oej/metermaids-trunk/include/asterisk/features.h (original)
+++ team/oej/metermaids-trunk/include/asterisk/features.h Sun Mar  5 06:04:14 2006
@@ -92,4 +92,17 @@
     \param feature the ast_call_feature object which was registered before*/
 extern void ast_unregister_feature(struct ast_call_feature *feature);
 
+
+/*! \brief Add parking watcher (metermaid) to list. These will be notified when we create
+ 	or remove parking extensions in the dial plan 
+		
+ */
+int ast_park_metermaid_add(void (*maid)(char *exten, char *context), char *context);
+
+/*! Remove parking watcher 
+	\param id	Watcher ID returned by ast_park_metermaid_add()
+	\return -1 on error (ID not found), otherwise 0
+*/
+int ast_park_metermaid_remove(int id);
+
 #endif /* _AST_FEATURES_H */

Modified: team/oej/metermaids-trunk/res/res_features.c
URL: http://svn.digium.com/view/asterisk/team/oej/metermaids-trunk/res/res_features.c?rev=11847&r1=11846&r2=11847&view=diff
==============================================================================
--- team/oej/metermaids-trunk/res/res_features.c (original)
+++ team/oej/metermaids-trunk/res/res_features.c Sun Mar  5 06:04:14 2006
@@ -73,6 +73,18 @@
 
 #define AST_MAX_WATCHERS 256
 
+/*! List of meter maids (parking lot watchers) */
+struct features_parkwatch {
+	void (*callback)(char *exten, char *context);	/*! Callback for notification */
+	int id;						/*! Meter maid ID */
+	char *context;					/*! Watched context */
+	struct features_parkwatch *next;		/*! Next in this simple list */
+};
+
+/*! Metermaid ID */
+struct features_parkwatch *metermaids;
+int metermaidid = 0;
+
 static char *parkedcall = "ParkedCall";
 
 static int parkingtime = DEFAULT_PARK_TIME; 		/*!< No more than 45 seconds parked before you do something with them */
@@ -80,6 +92,7 @@
 static char parking_con_dial[AST_MAX_EXTENSION];	/*!< Context for dialback for parking (KLUDGE) */
 static char parking_ext[AST_MAX_EXTENSION];		/*!< Extension you type to park the call */
 static char pickup_ext[AST_MAX_EXTENSION];		/*!< Call pickup extension */
+static int parkaddhints = 0;				/*!< Add parking hints to dial plan automatically */
 static int parking_start;				/*!< First available extension for parking */
 static int parking_stop;				/*!< Last available extension for parking */
 
@@ -141,6 +154,8 @@
 AST_MUTEX_DEFINE_STATIC(parking_lock);
 
 static pthread_t parking_thread;
+
+STANDARD_LOCAL_USER;
 
 LOCAL_USER_DECL;
 
@@ -252,8 +267,89 @@
 	return adsi_print(chan, message, justify, 1);
 }
 
-/*! \brief Park a call 
- 	We put the user in the parking list, then wake up the parking thread to be sure it looks
+/*! Add parking watcher (metermaid) to list. These will be notified when we create
+ 	or remove parking extensions in the dial plan 
+ */
+int ast_park_metermaid_add(void (*maid)(char *exten, char *context), char *context)
+{
+	struct features_parkwatch *newmaid;
+	struct features_parkwatch *maids = metermaids;
+
+	newmaid = malloc(sizeof(struct features_parkwatch));
+	if (!newmaid) {
+		ast_log(LOG_ERROR, "Can't allocate parking watcher, out of memory.\n");
+		return -1;
+	}
+	memset(newmaid, 0, sizeof(struct features_parkwatch));
+
+	/* Spool till end of list */
+	while(maids && maids->next) {
+		maids = maids->next;
+	}
+
+	newmaid->callback = maid;
+	if (context)
+		newmaid->context = strdup(context);
+	newmaid->id = metermaidid;
+
+	/* Generate new ID */
+	metermaidid++;
+
+	/* Link the new object to the list */
+	if (maids)
+		maids->next = newmaid;
+	else
+		metermaids = newmaid;
+	if (option_debug > 1)
+		ast_log(LOG_DEBUG, "Added metermaid # %d\n", metermaidid);
+	return metermaidid;
+}
+
+/*! Remove parking watcher */
+int ast_park_metermaid_remove(int id)
+{
+	struct features_parkwatch *maids = metermaids;
+	struct features_parkwatch *prev = NULL;
+	struct features_parkwatch *kill = NULL;
+
+	while (maids && !kill) {
+		if (maids->id == id) {
+			if (prev) {
+				prev->next = maids->next;
+			} else {
+				metermaids = maids->next;
+			}
+			kill = maids;
+		}
+		prev = maids;
+		maids = maids->next;
+	}
+	if (!kill)
+		return -1;		/* Did not find id */
+	if (kill->context)
+		free(kill->context);
+	free(kill);
+	return 0;
+}
+
+/*! Notify metermaids that we've changed an extension */
+static void notify_metermaids(char *exten, char *context)
+{
+	struct features_parkwatch *maid = metermaids;
+	if (!maid)
+		return;
+	while (maid) {
+		if (!maid->context || !strcmp(context, maid->context))
+			maid->callback(exten, context);
+		maid = maid->next;
+	}
+	if (option_debug > 3)
+		ast_log(LOG_DEBUG, "Notification of state change to metermaids %s@%s\n", exten, context);
+	return;
+}
+
+/*! \brief Park a call
+	\note We put the user in the parking list, then wake up the parking thread to be sure it looks
 	after these channels too */
 int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeout, int *extout)
 {
@@ -261,7 +357,7 @@
 	int i,x,parking_range;
 	char exten[AST_MAX_EXTENSION];
 	struct ast_context *con;
-	
+
 	if (!(pu = ast_calloc(1, sizeof(*pu)))) {
 		return -1;
 	}
@@ -362,7 +458,9 @@
 	}
 	if (con) {
 		snprintf(exten, sizeof(exten), "%d", x);
-		ast_add_extension2(con, 1, exten, 1, NULL, NULL, parkedcall, strdup(exten), FREE, registrar);
+	
+		if (ast_add_extension2(con, 1, exten, 1, NULL, NULL, parkedcall, strdup(exten), FREE, registrar) == 0)
+			notify_metermaids(exten, parking_con);	/* Notify watchers */
 	}
 	if (peer) 
 		ast_say_digits(peer, pu->parkingnum, "", peer->language);
@@ -1545,6 +1643,8 @@
 					snprintf(exten, sizeof(exten), "%d", pt->parkingnum);
 					if (ast_context_remove_extension2(con, exten, 1, NULL))
 						ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
+					else
+						notify_metermaids(exten, parking_con);	/* Notify watchers */
 				} else
 					ast_log(LOG_WARNING, "Whoa, no parking context?\n");
 				free(pt);
@@ -1587,6 +1687,8 @@
 								snprintf(exten, sizeof(exten), "%d", pt->parkingnum);
 								if (ast_context_remove_extension2(con, exten, 1, NULL))
 									ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
+								else
+									notify_metermaids(exten, parking_con);
 							} else
 								ast_log(LOG_WARNING, "Whoa, no parking context?\n");
 							free(pt);
@@ -1694,6 +1796,8 @@
 			snprintf(exten, sizeof(exten), "%d", pu->parkingnum);
 			if (ast_context_remove_extension2(con, exten, 1, NULL))
 				ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
+			else
+				notify_metermaids(exten, parking_con);
 		} else
 			ast_log(LOG_WARNING, "Whoa, no parking context?\n");
 
@@ -1957,9 +2061,22 @@
 	return res;
 }
 
+static void park_add_hints(char *context, int start, int stop)
+{
+	int numext;
+	char device[AST_MAX_EXTENSION];
+	char exten[10];
+	for (numext = start; numext <= stop; numext++) {
+		snprintf(exten, sizeof(exten), "%d", numext);
+		snprintf(device, sizeof(device), "Local/%s@%s", exten, context);
+		ast_add_extension(context, 1, exten, PRIORITY_HINT, NULL, NULL, device, NULL, NULL, registrar);
+	}
+}
+
 static int load_config(void) 
 {
 	int start = 0, end = 0;
+	int res;
 	struct ast_context *con = NULL;
 	struct ast_config *cfg = NULL;
 	struct ast_variable *var = NULL;
@@ -1982,6 +2099,7 @@
 	parking_start = 701;
 	parking_stop = 750;
 	parkfindnext = 0;
+	parkaddhints = 0;
 
 	transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
 	featuredigittimeout = DEFAULT_FEATURE_DIGIT_TIMEOUT;
@@ -2009,6 +2127,8 @@
 				}
 			} else if (!strcasecmp(var->name, "findslot")) {
 				parkfindnext = (!strcasecmp(var->value, "next"));
+			} else if (!strcasecmp(var->name, "parkhints")) {
+				parkaddhints = ast_true(var->value);
 			} else if (!strcasecmp(var->name, "adsipark")) {
 				adsipark = ast_true(var->value);
 			} else if (!strcasecmp(var->name, "transferdigittimeout")) {
@@ -2122,7 +2242,8 @@
 
 	/* Remove the old parking extension */
 	if (!ast_strlen_zero(old_parking_con) && (con = ast_context_find(old_parking_con)))	{
-		ast_context_remove_extension2(con, old_parking_ext, 1, registrar);
+		if(!ast_context_remove_extension2(con, old_parking_ext, 1, registrar))
+			notify_metermaids(old_parking_ext, old_parking_con);
 		ast_log(LOG_DEBUG, "Removed old parking extension %s@%s\n", old_parking_ext, old_parking_con);
 	}
 	
@@ -2132,7 +2253,12 @@
 			return -1;
 		}
 	}
-	return ast_add_extension2(con, 1, ast_parking_ext(), 1, NULL, NULL, parkcall, strdup(""), FREE, registrar);
+	res = ast_add_extension2(con, 1, ast_parking_ext(), 1, NULL, NULL, parkcall, strdup(""), FREE, registrar);
+	if (parkaddhints)
+		park_add_hints(parking_con, parking_start, parking_stop);
+	if (!res)
+		notify_metermaids(ast_parking_ext(), parking_con);
+	return res;
 }
 
 int reload(void) {



More information about the asterisk-commits mailing list