[asterisk-commits] rmudgett: branch rmudgett/mwi r255112 - /team/rmudgett/mwi/channels/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Mar 26 17:30:47 CDT 2010


Author: rmudgett
Date: Fri Mar 26 17:30:43 2010
New Revision: 255112

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=255112
Log:
Convert ISDN MWI to use event subscriptions.

Modified:
    team/rmudgett/mwi/channels/chan_dahdi.c
    team/rmudgett/mwi/channels/sig_pri.c
    team/rmudgett/mwi/channels/sig_pri.h

Modified: team/rmudgett/mwi/channels/chan_dahdi.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/mwi/channels/chan_dahdi.c?view=diff&rev=255112&r1=255111&r2=255112
==============================================================================
--- team/rmudgett/mwi/channels/chan_dahdi.c (original)
+++ team/rmudgett/mwi/channels/chan_dahdi.c Fri Mar 26 17:30:43 2010
@@ -10798,18 +10798,6 @@
 	char buf[1024];
 	struct pollfd *pfds=NULL;
 	int lastalloc = -1;
-#if defined(HAVE_PRI)
-#if defined(HAVE_PRI_MWI)
-	int span;
-	int idx;
-	int num_messages;
-	struct dahdi_pri *pri;
-	struct pri_party_id mailbox;
-
-	static int mbox_update_count;
-#endif	/* defined(HAVE_PRI_MWI) */
-#endif	/* defined(HAVE_PRI) */
-
 	/* This thread monitors all the frame relay interfaces which are not yet in use
 	   (and thus do not have a separate thread) indefinitely */
 	/* From here on out, we die whenever asked */
@@ -11071,51 +11059,6 @@
 			}
 		}
 		ast_mutex_unlock(&iflock);
-
-#if defined(HAVE_PRI)
-#if defined(HAVE_PRI_MWI)
-		/* Poll the voicemail mailboxes every 10 seconds or so. */
-		if (10 < ++mbox_update_count) {
-			mbox_update_count = 0;
-
-			/* Poll the voicemail mailboxes on each span. */
-			for (span = 0; span < NUM_SPANS; ++span) {
-				pri = pris + span;
-				if (!pri->pri.pri) {
-					/* Span not setup. */
-					continue;
-				}
-				/* Poll the voicemail mailboxes in this span. */
-				for (idx = 0; idx < SIG_PRI_MAX_MWI_MAILBOXES; ++idx) {
-					if (!pri->pri.mbox[idx].number) {
-						/* There are no more mailboxes on this span. */
-						break;
-					}
-					num_messages = ast_app_messagecount(pri->pri.mbox[idx].context,
-						pri->pri.mbox[idx].number, NULL);
-					if (num_messages == pri->pri.mbox[idx].last_message_count) {
-						/* The number of messages has not changed since last time. */
-						continue;
-					}
-					pri->pri.mbox[idx].last_message_count = num_messages;
-
-					/* The number of messages changed so send a MWI indication. */
-					memset(&mailbox, 0, sizeof(mailbox));
-					mailbox.number.valid = 1;
-					mailbox.number.presentation = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
-					mailbox.number.plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_UNKNOWN;
-					ast_copy_string(mailbox.number.str, pri->pri.mbox[idx].number,
-						sizeof(mailbox.number.str));
-
-					ast_mutex_lock(&pri->pri.lock);
-					pri_mwi_indicate(pri->pri.pri, &mailbox, 1 /* speech */, num_messages,
-						NULL, NULL, -1, 0);
-					ast_mutex_unlock(&pri->pri.lock);
-				}
-			}
-		}
-#endif	/* defined(HAVE_PRI_MWI) */
-#endif	/* defined(HAVE_PRI) */
 	}
 	/* Never reached */
 	return NULL;
@@ -16340,6 +16283,7 @@
 		for (j = 0; j < NUM_DCHANS; j++) {
 			dahdi_close_pri_fd(&(pris[i]), j);
 		}
+		sig_pri_stop_pri(&pris[i].pri);
 	}
 #if defined(HAVE_PRI_CCSS)
 	ast_cc_agent_unregister(&dahdi_pri_cc_agent_callbacks);

Modified: team/rmudgett/mwi/channels/sig_pri.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/mwi/channels/sig_pri.c?view=diff&rev=255112&r1=255111&r2=255112
==============================================================================
--- team/rmudgett/mwi/channels/sig_pri.c (original)
+++ team/rmudgett/mwi/channels/sig_pri.c Fri Mar 26 17:30:43 2010
@@ -4770,6 +4770,88 @@
 	return 1;
 }
 
+/*!
+ * \internal
+ * \brief Send a MWI indication to the given span.
+ * \since 1.8
+ *
+ * \param pri Asterisk D channel control structure.
+ * \param mbox_number Mailbox number
+ * \param mbox_context Mailbox context
+ * \param num_messages Number of messages waiting.
+ *
+ * \return Nothing
+ */
+static void sig_pri_send_mwi_indication(struct sig_pri_pri *pri, const char *mbox_number, const char *mbox_context, int num_messages)
+{
+	struct pri_party_id mailbox;
+
+	ast_log(LOG_DEBUG, "Send MWI indication for %s@%s num_messages:%d\n", mbox_number,
+		mbox_context, num_messages);
+
+	memset(&mailbox, 0, sizeof(mailbox));
+	mailbox.number.valid = 1;
+	mailbox.number.presentation = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
+	mailbox.number.plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_UNKNOWN;
+	ast_copy_string(mailbox.number.str, mbox_number, sizeof(mailbox.number.str));
+
+	ast_mutex_lock(&pri->lock);
+	pri_mwi_indicate(pri->pri, &mailbox, 1 /* speech */, num_messages, NULL, NULL, -1, 0);
+	ast_mutex_unlock(&pri->lock);
+}
+
+/*!
+ * \internal
+ * \brief MWI subscription event callback.
+ * \since 1.8
+ *
+ * \param event the event being passed to the subscriber
+ * \param userdata the data provider in the call to ast_event_subscribe()
+ *
+ * \return Nothing
+ */
+static void sig_pri_mwi_event_cb(const struct ast_event *event, void *userdata)
+{
+	struct sig_pri_pri *pri = userdata;
+	const char *mbox_context;
+	const char *mbox_number;
+	int num_messages;
+
+	mbox_number = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
+	if (ast_strlen_zero(mbox_number)) {
+		return;
+	}
+	mbox_context = ast_event_get_ie_str(event, AST_EVENT_IE_CONTEXT);
+	if (ast_strlen_zero(mbox_context)) {
+		return;
+	}
+	num_messages = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
+	sig_pri_send_mwi_indication(pri, mbox_number, mbox_context, num_messages);
+}
+
+/*!
+ * \brief Stop PRI span.
+ * \since 1.8
+ *
+ * \param pri Asterisk D channel control structure.
+ *
+ * \return Nothing
+ */
+void sig_pri_stop_pri(struct sig_pri_pri *pri)
+{
+#if defined(HAVE_PRI_MWI)
+	int idx;
+#endif	/* defined(HAVE_PRI_MWI) */
+
+#if defined(HAVE_PRI_MWI)
+	for (idx = 0; idx < SIG_PRI_MAX_MWI_MAILBOXES; ++idx) {
+		if (pri->mbox[idx].sub) {
+			pri->mbox[idx].sub = ast_event_unsubscribe(pri->mbox[idx].sub);
+		}
+	}
+#endif	/* defined(HAVE_PRI_MWI) */
+}
+
 int sig_pri_start_pri(struct sig_pri_pri *pri)
 {
 	int x;
@@ -4778,13 +4860,15 @@
 	char *saveptr;
 	char *mbox_number;
 	char *mbox_context;
+	struct ast_str *mwi_description = ast_str_alloca(64);
 #endif	/* defined(HAVE_PRI_MWI) */
 
 #if defined(HAVE_PRI_MWI)
 	/* Prepare the mbox[] for use. */
-	memset(pri->mbox, 0, sizeof(pri->mbox));
 	for (i = 0; i < SIG_PRI_MAX_MWI_MAILBOXES; ++i) {
-		pri->mbox[i].last_message_count = -1;
+		if (pri->mbox[i].sub) {
+			pri->mbox[i].sub = ast_event_unsubscribe(pri->mbox[i].sub);
+		}
 	}
 
 	/*
@@ -4807,12 +4891,26 @@
 			/* There is no mailbox number.  Skip it. */
 			continue;
 		}
+		if (ast_strlen_zero(mbox_context)) {
+			/* There was no context so use the default. */
+			mbox_context = "default";
+		}
 
 		/* Fill the mbox[] element. */
+		ast_str_set(&mwi_description, -1, "%s span %d[%d] MWI mailbox %s@%s",
+			sig_pri_cc_type_name, pri->span, i, mbox_number, mbox_context);
+		pri->mbox[i].sub = ast_event_subscribe(AST_EVENT_MWI, sig_pri_mwi_event_cb,
+			ast_str_buffer(mwi_description), pri,
+			AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mbox_number,
+			AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, mbox_context,
+			AST_EVENT_IE_END);
+		if (!pri->mbox[i].sub) {
+			ast_log(LOG_ERROR, "%s span %d could not subscribe to MWI events for %s@%s.",
+				sig_pri_cc_type_name, pri->span, mbox_number, mbox_context);
+			continue;
+		}
 		pri->mbox[i].number = mbox_number;
-		if (!ast_strlen_zero(mbox_context)) {
-			pri->mbox[i].context = mbox_context;
-		}
+		pri->mbox[i].context = mbox_context;
 		++i;
 	}
 #endif	/* defined(HAVE_PRI_MWI) */
@@ -4896,6 +4994,23 @@
 		ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
 		return -1;
 	}
+
+#if defined(HAVE_PRI_MWI)
+	/* Send the initial MWI indications for this span. */
+	for (i = 0; i < SIG_PRI_MAX_MWI_MAILBOXES; ++i) {
+		int num_messages;
+
+		if (!pri->mbox[i].sub) {
+			/* There are no more mailboxes on this span. */
+			break;
+		}
+		num_messages = ast_app_messagecount(pri->mbox[i].context, pri->mbox[i].number,
+			NULL);
+		sig_pri_send_mwi_indication(pri, pri->mbox[i].number, pri->mbox[i].context,
+			num_messages);
+	}
+#endif	/* defined(HAVE_PRI_MWI) */
+
 	return 0;
 }
 

Modified: team/rmudgett/mwi/channels/sig_pri.h
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/mwi/channels/sig_pri.h?view=diff&rev=255112&r1=255111&r2=255112
==============================================================================
--- team/rmudgett/mwi/channels/sig_pri.h (original)
+++ team/rmudgett/mwi/channels/sig_pri.h Fri Mar 26 17:30:43 2010
@@ -27,6 +27,7 @@
 
 #include "asterisk/channel.h"
 #include "asterisk/frame.h"
+#include "asterisk/event.h"
 #include "asterisk/ccss.h"
 #include <libpri.h>
 #include <dahdi/user.h>
@@ -246,17 +247,14 @@
 
 struct sig_pri_mbox {
 	/*!
-	 * \brief Mailbox number
+	 * \brief MWI mailbox event subscription.
 	 * \note NULL if mailbox not configured.
 	 */
+	struct ast_event_sub *sub;
+	/*! \brief Mailbox number */
 	const char *number;
-	/*!
-	 * \brief Mailbox context.
-	 * \note NULL if default.
-	 */
+	/*! \brief Mailbox context. */
 	const char *context;
-	/*! \brief Number of messages in mailbox from last poll. */
-	int last_message_count;
 };
 #endif	/* defined(HAVE_PRI_MWI) */
 
@@ -392,6 +390,7 @@
  * functions should handle it normally (generate inband DTMF) */
 int sig_pri_digit_begin(struct sig_pri_chan *pvt, struct ast_channel *ast, char digit);
 
+void sig_pri_stop_pri(struct sig_pri_pri *pri);
 int sig_pri_start_pri(struct sig_pri_pri *pri);
 
 void sig_pri_chan_alarm_notify(struct sig_pri_chan *p, int noalarm);




More information about the asterisk-commits mailing list