aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGES16
-rw-r--r--UPGRADE.txt13
-rw-r--r--apps/app_voicemail.c337
-rw-r--r--channels/chan_iax2.c106
-rw-r--r--channels/chan_mgcp.c38
-rw-r--r--channels/chan_sip.c153
-rw-r--r--channels/chan_zap.c36
-rw-r--r--configs/sip.conf.sample1
-rw-r--r--configs/voicemail.conf.sample13
-rw-r--r--include/asterisk.h1
-rw-r--r--include/asterisk/event.h416
-rw-r--r--include/asterisk/event_defs.h120
-rw-r--r--main/Makefile2
-rw-r--r--main/asterisk.c1
-rw-r--r--main/dial.c26
-rw-r--r--main/event.c774
-rw-r--r--res/res_eventtest.c177
17 files changed, 2064 insertions, 166 deletions
diff --git a/CHANGES b/CHANGES
index 07d7ec52b..02aebb909 100644
--- a/CHANGES
+++ b/CHANGES
@@ -53,6 +53,10 @@ SIP changes
registration data, as before.
* The SIPPEER function have new options for port address, call and pickup groups
* Added support for T.140 realtime text in SIP/RTP
+ * The "checkmwi" option has been removed from sip.conf, as it is no longer
+ required due to the restructuring of how MWI is handled. See the descriptions
+ in this file of the "pollmailboxes" and "pollfreq" options to voicemail.conf
+ for more information.
IAX2 changes
------------
@@ -79,6 +83,18 @@ Voicemail Changes
prompts within the Voicemail application by changing them in voicemail.conf
* Added the ability for the "voicemail show users" CLI command to show users
configured by the dynamic realtime configuration method.
+ * MWI (Message Waiting Indication) handling has been significantly
+ restructured internally to Asterisk. It is now totally event based
+ instead of polling based. The voicemail application will notify other
+ modules that have subscribed to MWI events when something in the mailbox
+ changes.
+ This also means that if any other entity outside of Asterisk is changing
+ the contents of mailboxes, then the voicemail application still needs to
+ poll for changes. Examples of situations that would require this option
+ are web interfaces to voicemail or an email client in the case of using
+ IMAP storage. So, two new options have been added to voicemail.conf
+ to account for this: "pollmailboxes" and "pollfreq". See the sample
+ configuration file for details.
Queue changes
-------------
diff --git a/UPGRADE.txt b/UPGRADE.txt
index 0a7d18306..2b7faf067 100644
--- a/UPGRADE.txt
+++ b/UPGRADE.txt
@@ -24,13 +24,24 @@ Core:
style' layout introduced in Asterisk 1.4 (and used by the automatic
sound file installer in the Makefile).
-Applications:
+Voicemail:
* The voicemail configuration values 'maxmessage' and 'minmessage' have
been changed to 'maxsecs' and 'minsecs' to clarify their purpose and
to make them more distinguishable from 'maxmsgs', which sets folder
size. The old variables will continue to work in this version, albeit
with a deprecation warning.
+* If you use any interface for modifying voicemail aside from the built in
+ dialplan applications, then the option "pollmailboxes" *must* be set in
+ voicemail.conf for message waiting indication (MWI) to work properly. This
+ is because Voicemail notification is now event based instead of polling
+ based. The channel drivers are no longer responsible for constantly manually
+ checking mailboxes for changes so that they can send MWI information to users.
+ Examples of situations that would require this option are web interfaces to
+ voicemail or an email client in the case of using IMAP storage.
+
+Applications:
+
* ChanIsAvail() now has a 't' option, which allows the specified device
to be queried for state without consulting the channel drivers. This
performs mostly a 'ChanExists' sort of function.
diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c
index ba2da801d..9ee82427e 100644
--- a/apps/app_voicemail.c
+++ b/apps/app_voicemail.c
@@ -104,6 +104,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/utils.h"
#include "asterisk/stringfields.h"
#include "asterisk/smdi.h"
+#include "asterisk/event.h"
#ifdef ODBC_STORAGE
#include "asterisk/res_odbc.h"
@@ -313,7 +314,8 @@ struct baseio {
unsigned char iobuf[BASEMAXINLINE];
};
-/*! Structure for linked list of users */
+/*! Structure for linked list of users
+ * Use ast_vm_user_destroy() to free one of these structures. */
struct ast_vm_user {
char context[AST_MAX_CONTEXT]; /*!< Voicemail context */
char mailbox[AST_MAX_EXTENSION]; /*!< Mailbox id, unique within vm context */
@@ -528,6 +530,42 @@ static int maxgreet;
static int skipms;
static int maxlogins;
+/*! Poll mailboxes for changes since there is something external to
+ * app_voicemail that may change them. */
+static unsigned int poll_mailboxes;
+
+/*! Polling frequency */
+static unsigned int poll_freq;
+/*! By default, poll every 30 seconds */
+#define DEFAULT_POLL_FREQ 30
+
+AST_MUTEX_DEFINE_STATIC(poll_lock);
+static ast_cond_t poll_cond = PTHREAD_COND_INITIALIZER;
+static pthread_t poll_thread = AST_PTHREADT_NULL;
+static unsigned char poll_thread_run;
+
+/*! Subscription to ... MWI event subscriptions */
+static struct ast_event_sub *mwi_sub_sub;
+/*! Subscription to ... MWI event un-subscriptions */
+static struct ast_event_sub *mwi_unsub_sub;
+
+/*!
+ * \brief An MWI subscription
+ *
+ * This is so we can keep track of which mailboxes are subscribed to.
+ * This way, we know which mailboxes to poll when the pollmailboxes
+ * option is being used.
+ */
+struct mwi_sub {
+ AST_RWLIST_ENTRY(mwi_sub) entry;
+ int old_new;
+ int old_old;
+ uint32_t uniqueid;
+ char mailbox[1];
+};
+
+static AST_RWLIST_HEAD_STATIC(mwi_subs, mwi_sub);
+
/* custom password sounds */
static char vm_password[80] = "vm-password";
static char vm_newpassword[80] = "vm-newpassword";
@@ -2258,8 +2296,10 @@ static int invent_message(struct ast_channel *chan, char *context, char *ext, in
static void free_user(struct ast_vm_user *vmu)
{
- if (ast_test_flag(vmu, VM_ALLOCED))
- free(vmu);
+ if (!ast_test_flag(vmu, VM_ALLOCED))
+ return;
+
+ free(vmu);
}
static void free_zone(struct vm_zone *z)
@@ -4002,6 +4042,35 @@ static int vm_forwardoptions(struct ast_channel *chan, struct ast_vm_user *vmu,
return cmd;
}
+static void queue_mwi_event(const char *mbox, int new, int old)
+{
+ struct ast_event *event;
+ char *mailbox;
+
+ /* Strip off @default */
+ mailbox = ast_strdupa(mbox);
+ if (strstr(mailbox, "@default"))
+ mailbox = strsep(&mailbox, "@");
+
+ if (ast_event_check_subscriber(AST_EVENT_MWI,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
+ AST_EVENT_IE_END) == AST_EVENT_SUB_NONE) {
+ return;
+ }
+
+ if (!(event = ast_event_new(AST_EVENT_MWI,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
+ AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, new,
+ AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, old,
+ AST_EVENT_IE_END))) {
+ return;
+ }
+
+ ast_event_queue_and_cache(event,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR,
+ AST_EVENT_IE_END);
+}
+
static int notify_new_message(struct ast_channel *chan, struct ast_vm_user *vmu, int msgnum, long duration, char *fmt, char *cidnum, char *cidname)
{
char todir[PATH_MAX], fn[PATH_MAX], ext_context[PATH_MAX], *stringp;
@@ -4050,6 +4119,8 @@ static int notify_new_message(struct ast_channel *chan, struct ast_vm_user *vmu,
if (ast_app_has_voicemail(ext_context, NULL))
ast_app_inboxcount(ext_context, &newmsgs, &oldmsgs);
+ queue_mwi_event(ext_context, newmsgs, oldmsgs);
+
manager_event(EVENT_FLAG_CALL, "MessageWaiting", "Mailbox: %s@%s\r\nWaiting: %d\r\nNew: %d\r\nOld: %d\r\n", vmu->mailbox, vmu->context, ast_app_has_voicemail(ext_context, NULL), newmsgs, oldmsgs);
run_externnotify(vmu->context, vmu->mailbox);
return 0;
@@ -6833,9 +6904,12 @@ out:
if (vmu)
close_mailbox(&vms, vmu);
if (valid) {
+ int new = 0, old = 0;
snprintf(ext_context, sizeof(ext_context), "%s@%s", vms.username, vmu->context);
manager_event(EVENT_FLAG_CALL, "MessageWaiting", "Mailbox: %s\r\nWaiting: %d\r\n", ext_context, has_voicemail(ext_context, NULL));
run_externnotify(vmu->context, vmu->mailbox);
+ ast_app_inboxcount(ext_context, &new, &old);
+ queue_mwi_event(ext_context, new, old);
}
#ifdef IMAP_STORAGE
/* expunge message - use UID Expunge if supported on IMAP server*/
@@ -6939,20 +7013,25 @@ static int vm_exec(struct ast_channel *chan, void *data)
static struct ast_vm_user *find_or_create(char *context, char *mbox)
{
struct ast_vm_user *vmu;
+
AST_LIST_TRAVERSE(&users, vmu, list) {
if (ast_test_flag((&globalflags), VM_SEARCH) && !strcasecmp(mbox, vmu->mailbox))
break;
if (context && (!strcasecmp(context, vmu->context)) && (!strcasecmp(mbox, vmu->mailbox)))
break;
}
+
+ if (vmu)
+ return vmu;
+
+ if (!(vmu = ast_calloc(1, sizeof(*vmu))))
+ return NULL;
+
+ ast_copy_string(vmu->context, context, sizeof(vmu->context));
+ ast_copy_string(vmu->mailbox, mbox, sizeof(vmu->mailbox));
+
+ AST_LIST_INSERT_TAIL(&users, vmu, list);
- if (!vmu) {
- if ((vmu = ast_calloc(1, sizeof(*vmu)))) {
- ast_copy_string(vmu->context, context, sizeof(vmu->context));
- ast_copy_string(vmu->mailbox, mbox, sizeof(vmu->mailbox));
- AST_LIST_INSERT_TAIL(&users, vmu, list);
- }
- }
return vmu;
}
@@ -6963,24 +7042,36 @@ static int append_mailbox(char *context, char *mbox, char *data)
char *stringp;
char *s;
struct ast_vm_user *vmu;
+ char *mailbox_full;
+ int new = 0, old = 0;
tmp = ast_strdupa(data);
- if ((vmu = find_or_create(context, mbox))) {
- populate_defaults(vmu);
-
- stringp = tmp;
- if ((s = strsep(&stringp, ",")))
- ast_copy_string(vmu->password, s, sizeof(vmu->password));
- if (stringp && (s = strsep(&stringp, ",")))
- ast_copy_string(vmu->fullname, s, sizeof(vmu->fullname));
- if (stringp && (s = strsep(&stringp, ",")))
- ast_copy_string(vmu->email, s, sizeof(vmu->email));
- if (stringp && (s = strsep(&stringp, ",")))
- ast_copy_string(vmu->pager, s, sizeof(vmu->pager));
- if (stringp && (s = strsep(&stringp, ",")))
- apply_options(vmu, s);
- }
+ if (!(vmu = find_or_create(context, mbox)))
+ return -1;
+
+ populate_defaults(vmu);
+
+ stringp = tmp;
+ if ((s = strsep(&stringp, ",")))
+ ast_copy_string(vmu->password, s, sizeof(vmu->password));
+ if (stringp && (s = strsep(&stringp, ",")))
+ ast_copy_string(vmu->fullname, s, sizeof(vmu->fullname));
+ if (stringp && (s = strsep(&stringp, ",")))
+ ast_copy_string(vmu->email, s, sizeof(vmu->email));
+ if (stringp && (s = strsep(&stringp, ",")))
+ ast_copy_string(vmu->pager, s, sizeof(vmu->pager));
+ if (stringp && (s = strsep(&stringp, ",")))
+ apply_options(vmu, s);
+
+ mailbox_full = alloca(strlen(mbox) + strlen(context) + 1);
+ strcpy(mailbox_full, mbox);
+ strcat(mailbox_full, "@");
+ strcat(mailbox_full, context);
+
+ inboxcount(mailbox_full, &new, &old);
+ queue_mwi_event(mailbox_full, new, old);
+
return 0;
}
@@ -7257,6 +7348,160 @@ static struct ast_cli_entry cli_voicemail[] = {
voicemail_show_zones_help, NULL, NULL },
};
+static void poll_subscribed_mailboxes(void)
+{
+ struct mwi_sub *mwi_sub;
+
+ AST_RWLIST_RDLOCK(&mwi_subs);
+ AST_RWLIST_TRAVERSE(&mwi_subs, mwi_sub, entry) {
+ int new = 0, old = 0;
+
+ if (ast_strlen_zero(mwi_sub->mailbox))
+ continue;
+
+ inboxcount(mwi_sub->mailbox, &new, &old);
+
+ if (new != mwi_sub->old_new || old != mwi_sub->old_old) {
+ mwi_sub->old_new = new;
+ mwi_sub->old_old = old;
+ queue_mwi_event(mwi_sub->mailbox, new, old);
+ }
+ }
+ AST_RWLIST_UNLOCK(&mwi_subs);
+}
+
+static void *mb_poll_thread(void *data)
+{
+ while (poll_thread_run) {
+ struct timespec ts = { 0, };
+ struct timeval tv;
+
+ tv = ast_tvadd(ast_tvnow(), ast_samp2tv(poll_freq, 1));
+ ts.tv_sec = tv.tv_sec;
+ ts.tv_nsec = tv.tv_usec * 1000;
+
+ ast_mutex_lock(&poll_lock);
+ ast_cond_timedwait(&poll_cond, &poll_lock, &ts);
+ ast_mutex_unlock(&poll_lock);
+
+ if (!poll_thread_run)
+ break;
+
+ poll_subscribed_mailboxes();
+ }
+
+ return NULL;
+}
+
+static void mwi_sub_destroy(struct mwi_sub *mwi_sub)
+{
+ free(mwi_sub);
+}
+
+static void mwi_unsub_event_cb(const struct ast_event *event, void *userdata)
+{
+ uint32_t uniqueid;
+ struct mwi_sub *mwi_sub;
+
+ if (ast_event_get_type(event) != AST_EVENT_UNSUB)
+ return;
+
+ if (ast_event_get_ie_uint(event, AST_EVENT_IE_EVENTTYPE) != AST_EVENT_MWI)
+ return;
+
+ uniqueid = ast_event_get_ie_uint(event, AST_EVENT_IE_UNIQUEID);
+
+ AST_RWLIST_WRLOCK(&mwi_subs);
+ AST_RWLIST_TRAVERSE_SAFE_BEGIN(&mwi_subs, mwi_sub, entry) {
+ if (mwi_sub->uniqueid == uniqueid) {
+ AST_LIST_REMOVE_CURRENT(&mwi_subs, entry);
+ break;
+ }
+ }
+ AST_RWLIST_TRAVERSE_SAFE_END
+ AST_RWLIST_UNLOCK(&mwi_subs);
+
+ if (mwi_sub)
+ mwi_sub_destroy(mwi_sub);
+}
+
+static void mwi_sub_event_cb(const struct ast_event *event, void *userdata)
+{
+ const char *mailbox;
+ uint32_t uniqueid;
+ unsigned int len;
+ struct mwi_sub *mwi_sub;
+
+ if (ast_event_get_type(event) != AST_EVENT_SUB)
+ return;
+
+ if (ast_event_get_ie_uint(event, AST_EVENT_IE_EVENTTYPE) != AST_EVENT_MWI)
+ return;
+
+ mailbox = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
+ uniqueid = ast_event_get_ie_uint(event, AST_EVENT_IE_UNIQUEID);
+
+ len = sizeof(*mwi_sub);
+ if (!ast_strlen_zero(mailbox))
+ len += strlen(mailbox);
+
+ if (!(mwi_sub = ast_calloc(1, len)))
+ return;
+
+ mwi_sub->uniqueid = uniqueid;
+ if (!ast_strlen_zero(mailbox))
+ strcpy(mwi_sub->mailbox, mailbox);
+
+ AST_RWLIST_WRLOCK(&mwi_subs);
+ AST_RWLIST_INSERT_TAIL(&mwi_subs, mwi_sub, entry);
+ AST_RWLIST_UNLOCK(&mwi_subs);
+}
+
+static void start_poll_thread(void)
+{
+ pthread_attr_t attr;
+
+ mwi_sub_sub = ast_event_subscribe(AST_EVENT_SUB, mwi_sub_event_cb, NULL,
+ AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_MWI,
+ AST_EVENT_IE_END);
+
+ mwi_unsub_sub = ast_event_subscribe(AST_EVENT_UNSUB, mwi_unsub_event_cb, NULL,
+ AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_MWI,
+ AST_EVENT_IE_END);
+
+ if (mwi_sub_sub)
+ ast_event_report_subs(mwi_sub_sub);
+
+ poll_thread_run = 1;
+
+ pthread_attr_init(&attr);
+ ast_pthread_create(&poll_thread, &attr, mb_poll_thread, NULL);
+ pthread_attr_destroy(&attr);
+}
+
+static void stop_poll_thread(void)
+{
+ poll_thread_run = 0;
+
+ if (mwi_sub_sub) {
+ ast_event_unsubscribe(mwi_sub_sub);
+ mwi_sub_sub = NULL;
+ }
+
+ if (mwi_unsub_sub) {
+ ast_event_unsubscribe(mwi_unsub_sub);
+ mwi_unsub_sub = NULL;
+ }
+
+ ast_mutex_lock(&poll_lock);
+ ast_cond_signal(&poll_cond);
+ ast_mutex_unlock(&poll_lock);
+
+ pthread_join(poll_thread, NULL);
+
+ poll_thread = AST_PTHREADT_NULL;
+}
+
static int load_config(void)
{
struct ast_vm_user *cur;
@@ -7641,6 +7886,19 @@ static int load_config(void)
if (!(val = ast_variable_retrieve(cfg, "general", "usedirectory")))
val = "no";
ast_set2_flag((&globalflags), ast_true(val), VM_DIRECFORWARD);
+
+ poll_freq = DEFAULT_POLL_FREQ;
+ if ((val = ast_variable_retrieve(cfg, "general", "pollfreq"))) {
+ if (sscanf(val, "%u", &poll_freq) != 1) {
+ poll_freq = DEFAULT_POLL_FREQ;
+ ast_log(LOG_ERROR, "'%s' is not a valid value for the pollfreq option!\n", val);
+ }
+ }
+
+ poll_mailboxes = 0;
+ if ((val = ast_variable_retrieve(cfg, "general", "pollmailboxes")))
+ poll_mailboxes = ast_true(val);
+
if ((ucfg = ast_config_load("users.conf"))) {
for (cat = ast_category_browse(ucfg, NULL); cat ; cat = ast_category_browse(ucfg, cat)) {
if (!ast_true(ast_config_option(ucfg, cat, "hasvoicemail")))
@@ -7800,6 +8058,12 @@ static int load_config(void)
}
AST_LIST_UNLOCK(&users);
ast_config_destroy(cfg);
+
+ if (poll_mailboxes && poll_thread == AST_PTHREADT_NULL)
+ start_poll_thread();
+ if (!poll_mailboxes && poll_thread != AST_PTHREADT_NULL)
+ stop_poll_thread();;
+
return 0;
} else {
AST_LIST_UNLOCK(&users);
@@ -7810,13 +8074,13 @@ static int load_config(void)
static int reload(void)
{
- return(load_config());
+ return load_config();
}
static int unload_module(void)
{
int res;
-
+
res = ast_unregister_application(app);
res |= ast_unregister_application(app2);
res |= ast_unregister_application(app3);
@@ -7827,29 +8091,32 @@ static int unload_module(void)
ast_module_user_hangup_all();
+ if (poll_thread != AST_PTHREADT_NULL)
+ stop_poll_thread();
+
return res;
}
static int load_module(void)
{
int res;
+
+ /* compute the location of the voicemail spool directory */
+ snprintf(VM_SPOOL_DIR, sizeof(VM_SPOOL_DIR), "%s/voicemail/", ast_config_AST_SPOOL_DIR);
+
+ if ((res = load_config()))
+ return res;
+
res = ast_register_application(app, vm_exec, synopsis_vm, descrip_vm);
res |= ast_register_application(app2, vm_execmain, synopsis_vmain, descrip_vmain);
res |= ast_register_application(app3, vm_box_exists, synopsis_vm_box_exists, descrip_vm_box_exists);
res |= ast_register_application(app4, vmauthenticate, synopsis_vmauthenticate, descrip_vmauthenticate);
res |= ast_custom_function_register(&mailbox_exists_acf);
if (res)
- return(res);
-
- if ((res=load_config())) {
- return(res);
- }
+ return res;
ast_cli_register_multiple(cli_voicemail, sizeof(cli_voicemail) / sizeof(struct ast_cli_entry));
- /* compute the location of the voicemail spool directory */
- snprintf(VM_SPOOL_DIR, sizeof(VM_SPOOL_DIR), "%s/voicemail/", ast_config_AST_SPOOL_DIR);
-
ast_install_vm_functions(has_voicemail, inboxcount, messagecount);
return res;
diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index b6953bcff..6b943ea58 100644
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -91,6 +91,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/netsock.h"
#include "asterisk/stringfields.h"
#include "asterisk/linkedlists.h"
+#include "asterisk/event.h"
#include "iax2.h"
#include "iax2-parser.h"
@@ -367,7 +368,9 @@ struct iax2_peer {
int pokefreqnotok; /*!< How often to check when the host has been determined to be down */
int historicms; /*!< How long recent average responses took */
int smoothing; /*!< Sample over how many units to determine historic ms */
-
+
+ struct ast_event_sub *mwi_event_sub;
+
struct ast_ha *ha;
AST_LIST_ENTRY(iax2_peer) entry;
};
@@ -883,6 +886,13 @@ static const struct ast_channel_tech iax2_tech = {
.func_channel_write = acf_channel_write,
};
+static void mwi_event_cb(const struct ast_event *event, void *userdata)
+{
+ /* The MWI subscriptions exist just so the core knows we care about those
+ * mailboxes. However, we just grab the events out of the cache when it
+ * is time to send MWI, since it is only sent with a REGACK. */
+}
+
static void insert_idle_thread(struct iax2_thread *thread)
{
if (thread->type == IAX_THREAD_TYPE_DYNAMIC) {
@@ -5546,9 +5556,38 @@ static int iax2_ack_registry(struct iax_ies *ies, struct sockaddr_in *sin, int c
return 0;
}
-static int iax2_register(char *value, int lineno)
+static int iax2_append_register(const char *hostname, const char *username,
+ const char *secret, const char *porta)
{
struct iax2_registry *reg;
+
+ if (!(reg = ast_calloc(1, sizeof(*reg))))
+ return -1;
+
+ if (ast_dnsmgr_lookup(hostname, &reg->addr.sin_addr, &reg->dnsmgr) < 0) {
+ free(reg);
+ return -1;
+ }
+
+ ast_copy_string(reg->username, username, sizeof(reg->username));
+
+ if (secret)
+ ast_copy_string(reg->secret, secret, sizeof(reg->secret));
+
+ reg->expire = -1;
+ reg->refresh = IAX_DEFAULT_REG_EXPIRE;
+ reg->addr.sin_family = AF_INET;
+ reg->addr.sin_port = porta ? htons(atoi(porta)) : htons(IAX_DEFAULT_PORTNO);
+
+ AST_LIST_LOCK(&registrations);
+ AST_LIST_INSERT_HEAD(&registrations, reg, entry);
+ AST_LIST_UNLOCK(&registrations);
+
+ return 0;
+}
+
+static int iax2_register(char *value, int lineno)
+{
char copy[256];
char *username, *hostname, *secret;
char *porta;
@@ -5556,18 +5595,21 @@ static int iax2_register(char *value, int lineno)
if (!value)
return -1;
+
ast_copy_string(copy, value, sizeof(copy));
- stringp=copy;
+ stringp = copy;
username = strsep(&stringp, "@");
hostname = strsep(&stringp, "@");
+
if (!hostname) {
ast_log(LOG_WARNING, "Format for registration is user[:secret]@host[:port] at line %d\n", lineno);
return -1;
}
- stringp=username;
+
+ stringp = username;
username = strsep(&stringp, ":");
secret = strsep(&stringp, ":");
- stringp=hostname;
+ stringp = hostname;
hostname = strsep(&stringp, ":");
porta = strsep(&stringp, ":");
@@ -5575,26 +5617,11 @@ static int iax2_register(char *value, int lineno)
ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
return -1;
}
- if (!(reg = ast_calloc(1, sizeof(*reg))))
- return -1;
- if (ast_dnsmgr_lookup(hostname, &reg->addr.sin_addr, &reg->dnsmgr) < 0) {
- free(reg);
- return -1;
- }
- ast_copy_string(reg->username, username, sizeof(reg->username));
- if (secret)
- ast_copy_string(reg->secret, secret, sizeof(reg->secret));
- reg->expire = -1;
- reg->refresh = IAX_DEFAULT_REG_EXPIRE;
- reg->addr.sin_family = AF_INET;
- reg->addr.sin_port = porta ? htons(atoi(porta)) : htons(IAX_DEFAULT_PORTNO);
- AST_LIST_LOCK(&registrations);
- AST_LIST_INSERT_HEAD(&registrations, reg, entry);
- AST_LIST_UNLOCK(&registrations);
-
- return 0;
+
+ return iax2_append_register(hostname, username, secret, porta);
}
+
static void register_peer_exten(struct iax2_peer *peer, int onoff)
{
char multi[256];
@@ -5790,13 +5817,27 @@ static int update_registry(const char *name, struct sockaddr_in *sin, int callno
iax_ie_append_short(&ied, IAX_IE_REFRESH, p->expiry);
iax_ie_append_addr(&ied, IAX_IE_APPARENT_ADDR, &p->addr);
if (!ast_strlen_zero(p->mailbox)) {
+ struct ast_event *event;
int new, old;
- ast_app_inboxcount(p->mailbox, &new, &old);
+
+ event = ast_event_get_cached(AST_EVENT_MWI,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, p->mailbox,
+ AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
+ AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
+ AST_EVENT_IE_END);
+ if (event) {
+ new = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
+ old = ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
+ ast_event_destroy(event);
+ } else /* Fall back on checking the mailbox directly */
+ ast_app_inboxcount(p->mailbox, &new, &old);
+
if (new > 255)
new = 255;
if (old > 255)
old = 255;
msgcount = (old << 8) | new;
+
iax_ie_append_short(&ied, IAX_IE_MSGCOUNT, msgcount);
}
if (ast_test_flag(p, IAX_HASCALLERID)) {
@@ -8533,9 +8574,9 @@ static struct iax2_peer *build_peer(const char *name, struct ast_variable *v, st
{
struct iax2_peer *peer = NULL;
struct ast_ha *oldha = NULL;
- int maskfound=0;
- int found=0;
- int firstpass=1;
+ int maskfound = 0;
+ int found = 0;
+ int firstpass = 1;
AST_LIST_LOCK(&peers);
if (!temponly) {
@@ -8762,8 +8803,16 @@ static struct iax2_peer *build_peer(const char *name, struct ast_variable *v, st
/* Make sure these are IPv4 addresses */
peer->addr.sin_family = AF_INET;
}
+
if (oldha)
ast_free_ha(oldha);
+
+ if (!ast_strlen_zero(peer->mailbox)) {
+ peer->mwi_event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, NULL,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, peer->mailbox,
+ AST_EVENT_IE_END);
+ }
+
return peer;
}
@@ -9065,6 +9114,9 @@ static void destroy_peer(struct iax2_peer *peer)
if (peer->dnsmgr)
ast_dnsmgr_release(peer->dnsmgr);
+ if (peer->mwi_event_sub)
+ ast_event_unsubscribe(peer->mwi_event_sub);
+
ast_string_field_free_pools(peer);
free(peer);
diff --git a/channels/chan_mgcp.c b/channels/chan_mgcp.c
index 27ca2688c..b39e1fbdd 100644
--- a/channels/chan_mgcp.c
+++ b/channels/chan_mgcp.c
@@ -76,6 +76,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/devicestate.h"
#include "asterisk/stringfields.h"
#include "asterisk/abstract_jb.h"
+#include "asterisk/event.h"
#ifndef IPTOS_MINCOST
#define IPTOS_MINCOST 0x02
@@ -320,9 +321,10 @@ struct mgcp_endpoint {
char cid_name[AST_MAX_EXTENSION]; /*!< Caller*ID name */
char lastcallerid[AST_MAX_EXTENSION]; /*!< Last Caller*ID */
char call_forward[AST_MAX_EXTENSION]; /*!< Last Caller*ID */
- char mailbox[AST_MAX_EXTENSION];
char musicclass[MAX_MUSICCLASS];
char curtone[80]; /*!< Current tone */
+ char mailbox[AST_MAX_EXTENSION];
+ struct ast_event_sub *mwi_event_sub;
ast_group_t callgroup;
ast_group_t pickupgroup;
int callwaiting;
@@ -450,9 +452,31 @@ static const struct ast_channel_tech mgcp_tech = {
.bridge = ast_rtp_bridge,
};
+static void mwi_event_cb(const struct ast_event *event, void *userdata)
+{
+ /* This module does not handle MWI in an event-based manner. However, it
+ * subscribes to MWI for each mailbox that is configured so that the core
+ * knows that we care about it. Then, chan_mgcp will get the MWI from the
+ * event cache instead of checking the mailbox directly. */
+}
+
static int has_voicemail(struct mgcp_endpoint *p)
{
- return ast_app_has_voicemail(p->mailbox, NULL);
+ int new_msgs;
+ struct ast_event *event;
+
+ event = ast_event_get_cached(AST_EVENT_MWI,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, p->mailbox,
+ AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
+ AST_EVENT_IE_END);
+
+ if (event) {
+ new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
+ ast_event_destroy(event);
+ } else
+ new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
+
+ return new_msgs;
}
static int unalloc_sub(struct mgcp_subchannel *sub)
@@ -3740,6 +3764,12 @@ static struct mgcp_gateway *build_gateway(char *cat, struct ast_variable *v)
ast_copy_string(e->language, language, sizeof(e->language));
ast_copy_string(e->musicclass, musicclass, sizeof(e->musicclass));
ast_copy_string(e->mailbox, mailbox, sizeof(e->mailbox));
+ if (!ast_strlen_zero(e->mailbox)) {
+ e->mwi_event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, NULL,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, e->mailbox,
+ AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
+ AST_EVENT_IE_END);
+ }
snprintf(e->rqnt_ident, sizeof(e->rqnt_ident), "%08lx", ast_random());
e->msgstate = -1;
e->amaflags = amaflags;
@@ -4014,6 +4044,10 @@ static void destroy_endpoint(struct mgcp_endpoint *e)
ast_mutex_destroy(&s->cx_queue_lock);
free(s);
}
+
+ if (e->mwi_event_sub)
+ ast_event_unsubscribe(e->mwi_event_sub);
+
ast_mutex_destroy(&e->lock);
ast_mutex_destroy(&e->rqnt_queue_lock);
ast_mutex_destroy(&e->cmd_queue_lock);
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index c756549b6..4c6243aaa 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -142,6 +142,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/threadstorage.h"
#include "asterisk/translate.h"
#include "asterisk/version.h"
+#include "asterisk/event.h"
#ifndef FALSE
#define FALSE 0
@@ -503,7 +504,6 @@ static const struct cfsip_options {
#define DEFAULT_VMEXTEN "asterisk"
#define DEFAULT_CALLERID "asterisk"
#define DEFAULT_NOTIFYMIME "application/simple-message-summary"
-#define DEFAULT_MWITIME 10
#define DEFAULT_ALLOWGUEST TRUE
#define DEFAULT_SRVLOOKUP FALSE /*!< Recommended setting is ON */
#define DEFAULT_COMPACTHEADERS FALSE
@@ -560,7 +560,6 @@ static int global_regattempts_max; /*!< Registration attempts before giving up *
static int global_allowguest; /*!< allow unauthenticated users/peers to connect? */
static int global_allowsubscribe; /*!< Flag for disabling ALL subscriptions, this is FALSE only if all peers are FALSE
the global setting is in globals_flags[1] */
-static int global_mwitime; /*!< Time between MWI checks for peers */
static unsigned int global_tos_sip; /*!< IP type of service for SIP packets */
static unsigned int global_tos_audio; /*!< IP type of service for audio RTP packets */
static unsigned int global_tos_video; /*!< IP type of service for video RTP packets */
@@ -1134,7 +1133,6 @@ struct sip_peer {
char useragent[256]; /*!< User agent in SIP request (saved from registration) */
struct ast_codec_pref prefs; /*!< codec prefs */
int lastmsgssent;
- time_t lastmsgcheck; /*!< Last time we checked for MWI */
unsigned int sipoptions; /*!< Supported SIP options */
struct ast_flags flags[2]; /*!< SIP_ flags */
int expire; /*!< When to expire this peer registration */
@@ -1160,6 +1158,7 @@ struct sip_peer {
struct ast_variable *chanvars; /*!< Variables to set for channel created by user */
struct sip_pvt *mwipvt; /*!< Subscription for MWI */
int autoframing;
+ struct ast_event_sub *mwi_event_sub; /*!< The MWI event subscription */
};
@@ -1291,8 +1290,7 @@ static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittyp
static void copy_request(struct sip_request *dst, const struct sip_request *src);
static void receive_message(struct sip_pvt *p, struct sip_request *req);
static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req);
-static int sip_send_mwi_to_peer(struct sip_peer *peer);
-static int does_peer_need_mwi(struct sip_peer *peer);
+static int sip_send_mwi_to_peer(struct sip_peer *peer, const struct ast_event *event, int cache_only);
/*--- Dialog management */
static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *sin,
@@ -1363,20 +1361,20 @@ static int reload_config(enum channelreloadreason reason);
static int expire_register(void *data);
static void *do_monitor(void *data);
static int restart_monitor(void);
-static int sip_send_mwi_to_peer(struct sip_peer *peer);
static void sip_destroy(struct sip_pvt *p);
static int sip_addrcmp(char *name, struct sockaddr_in *sin); /* Support for peer matching */
static int sip_refer_allocate(struct sip_pvt *p);
static void ast_quiet_chan(struct ast_channel *chan);
static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target);
-/*--- Device monitoring and Device/extension state handling */
+/*--- Device monitoring and Device/extension state/event handling */
static int cb_extensionstate(char *context, char* exten, int state, void *data);
static int sip_devicestate(void *data);
static int sip_poke_noanswer(void *data);
static int sip_poke_peer(struct sip_peer *peer);
static void sip_poke_all_peers(void);
static void sip_peer_hold(struct sip_pvt *p, int hold);
+static void mwi_event_cb(const struct ast_event *, void *);
/*--- Applications, functions, CLI and manager command helpers */
static const char *sip_nat_mode(const struct sip_pvt *p);
@@ -2600,6 +2598,11 @@ static void sip_destroy_peer(struct sip_peer *peer)
if (peer->mwipvt) /* We have an active subscription, delete it */
sip_destroy(peer->mwipvt);
+ if (peer->mwi_event_sub) {
+ ast_event_unsubscribe(peer->mwi_event_sub);
+ peer->mwi_event_sub = NULL;
+ }
+
if (peer->chanvars) {
ast_variables_destroy(peer->chanvars);
peer->chanvars = NULL;
@@ -8733,6 +8736,16 @@ static void sip_peer_hold(struct sip_pvt *p, int hold)
return;
}
+/*! \brief Receive MWI events that we have subscribed to */
+static void mwi_event_cb(const struct ast_event *event, void *userdata)
+{
+ struct sip_peer *peer = userdata;
+
+ ASTOBJ_RDLOCK(peer);
+ sip_send_mwi_to_peer(peer, event, 0);
+ ASTOBJ_UNLOCK(peer);
+}
+
/*! \brief Callback for the devicestate notification (SUBSCRIBE) support subsystem
\note If you add an "hint" priority to the extension in the dial plan,
you will get notifications on device state changes */
@@ -8860,7 +8873,7 @@ static enum check_auth_result register_verify(struct sip_pvt *p, struct sockaddr
if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri, XMIT_UNRELIABLE, ast_test_flag(req, SIP_PKT_IGNORE)))) {
sip_cancel_destroy(p);
- /* We have a succesful registration attemp with proper authentication,
+ /* We have a successful registration attempt with proper authentication,
now, update the peer */
switch (parse_register_contact(p, peer, req)) {
case PARSE_REGISTER_FAILED:
@@ -11016,7 +11029,6 @@ static int sip_show_settings(int fd, int argc, char *argv[])
ast_cli(fd, " Call limit peers only: %s\n", global_limitonpeers ? "Yes" : "No");
ast_cli(fd, " Direct RTP setup: %s\n", global_directrtpsetup ? "Yes" : "No");
ast_cli(fd, " User Agent: %s\n", global_useragent);
- ast_cli(fd, " MWI checking interval: %d secs\n", global_mwitime);
ast_cli(fd, " Reg. context: %s\n", S_OR(global_regcontext, "(not set)"));
ast_cli(fd, " Caller ID: %s\n", default_callerid);
ast_cli(fd, " From: Domain: %s\n", default_fromdomain);
@@ -15227,6 +15239,11 @@ static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req,
}
p->subscribed = MWI_NOTIFICATION;
+ if (ast_test_flag(&authpeer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY)) {
+ authpeer->mwi_event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, authpeer,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, authpeer->mailbox,
+ AST_EVENT_IE_END);
+ }
if (authpeer->mwipvt && authpeer->mwipvt != p) /* Destroy old PVT if this is a new one */
/* We only allow one subscription per peer */
sip_destroy(authpeer->mwipvt);
@@ -15273,7 +15290,7 @@ static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req,
transmit_response(p, "200 OK", req);
if (p->relatedpeer) { /* Send first notification */
ASTOBJ_WRLOCK(p->relatedpeer);
- sip_send_mwi_to_peer(p->relatedpeer);
+ sip_send_mwi_to_peer(p->relatedpeer, NULL, 0);
ASTOBJ_UNLOCK(p->relatedpeer);
}
} else {
@@ -15685,25 +15702,35 @@ static int sipsock_read(int *id, int fd, short events, void *ignore)
}
/*! \brief Send message waiting indication to alert peer that they've got voicemail */
-static int sip_send_mwi_to_peer(struct sip_peer *peer)
+static int sip_send_mwi_to_peer(struct sip_peer *peer, const struct ast_event *event, int cache_only)
{
/* Called with peerl lock, but releases it */
struct sip_pvt *p;
- int newmsgs, oldmsgs;
+ int newmsgs = 0, oldmsgs = 0;
+ struct ast_event *cache_event = NULL;
- /* Check for messages */
- ast_app_inboxcount(peer->mailbox, &newmsgs, &oldmsgs);
-
- peer->lastmsgcheck = time(NULL);
-
- /* Return now if it's the same thing we told them last time */
- if (((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs)) == peer->lastmsgssent) {
+ if (ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY) && !peer->mwipvt)
return 0;
- }
-
-
- peer->lastmsgssent = ((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs));
+ if (!event) {
+ /* Check the event cache for the mailbox info */
+ event = cache_event = ast_event_get_cached(AST_EVENT_MWI,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, peer->mailbox,
+ AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
+ AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
+ AST_EVENT_IE_END);
+ }
+
+ if (event) {
+ newmsgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
+ oldmsgs = ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
+ if (cache_event)
+ ast_event_destroy(cache_event);
+ } else if (cache_only) {
+ return 0;
+ } else /* Fall back to manually checking the mailbox */
+ ast_app_inboxcount(peer->mailbox, &newmsgs, &oldmsgs);
+
if (peer->mwipvt) {
/* Base message on subscription */
p = peer->mwipvt;
@@ -15724,30 +15751,14 @@ static int sip_send_mwi_to_peer(struct sip_peer *peer)
/* Destroy this session after 32 secs */
sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
}
+
/* Send MWI */
ast_set_flag(&p->flags[0], SIP_OUTGOING);
transmit_notify_with_mwi(p, newmsgs, oldmsgs, peer->vmexten);
- return 0;
-}
-
-/*! \brief Check whether peer needs a new MWI notification check */
-static int does_peer_need_mwi(struct sip_peer *peer)
-{
- time_t t = time(NULL);
-
- if (ast_test_flag(&peer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY) &&
- !peer->mwipvt) { /* We don't have a subscription */
- peer->lastmsgcheck = t; /* Reset timer */
- return FALSE;
- }
- if (!ast_strlen_zero(peer->mailbox) && (t - peer->lastmsgcheck) > global_mwitime)
- return TRUE;
-
- return FALSE;
+ return 0;
}
-
/*! \brief helper function for the monitoring thread */
static void check_rtp_timeout(struct sip_pvt *dialog, time_t t)
{
@@ -15823,11 +15834,7 @@ static void *do_monitor(void *data)
{
int res;
struct sip_pvt *dialog;
- struct sip_peer *peer = NULL;
time_t t;
- int fastrestart = FALSE;
- int lastpeernum = -1;
- int curpeernum;
int reloading;
/* Add an I/O event to our SIP UDP socket */
@@ -15859,7 +15866,7 @@ restartsearch:
of time since the last time we did it (when MWI is being sent, we can
get back to this point every millisecond or less)
*/
- for (dialog = dialoglist; !fastrestart && dialog; dialog = dialog->next) {
+ for (dialog = dialoglist; dialog; dialog = dialog->next) {
sip_pvt_lock(dialog);
/* Check RTP timeouts and kill calls if we have a timeout set and do not get RTP */
check_rtp_timeout(dialog, t);
@@ -15881,10 +15888,6 @@ restartsearch:
res = ast_sched_wait(sched);
if ((res < 0) || (res > 1000))
res = 1000;
-
- /* If we might need to send more mailbox notifications, don't wait long at all.*/
- if (fastrestart)
- res = 1;
res = ast_io_wait(io, res);
if (option_debug && res > 20)
ast_log(LOG_DEBUG, "chan_sip: ast_io_wait ran %d all at once\n", res);
@@ -15894,37 +15897,11 @@ restartsearch:
if (option_debug && res >= 20)
ast_log(LOG_DEBUG, "chan_sip: ast_sched_runq ran %d all at once\n", res);
}
-
- /* Send MWI notifications to peers - static and cached realtime peers */
- t = time(NULL);
- fastrestart = FALSE;
- curpeernum = 0;
- peer = NULL;
- /* Find next peer that needs mwi */
- ASTOBJ_CONTAINER_TRAVERSE(&peerl, !peer, do {
- if ((curpeernum > lastpeernum) && does_peer_need_mwi(iterator)) {
- fastrestart = TRUE;
- lastpeernum = curpeernum;
- peer = ASTOBJ_REF(iterator);
- };
- curpeernum++;
- } while (0)
- );
- /* Send MWI to the peer */
- if (peer) {
- ASTOBJ_WRLOCK(peer);
- sip_send_mwi_to_peer(peer);
- ASTOBJ_UNLOCK(peer);
- unref_peer(peer);
- } else {
- /* Reset where we come from */
- lastpeernum = -1;
- }
ast_mutex_unlock(&monlock);
}
+
/* Never reached */
return NULL;
-
}
/*! \brief Start the channel monitor thread */
@@ -16714,7 +16691,6 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, str
struct ast_flags mask[2] = {{(0)}};
char callback[256] = "";
-
if (!realtime)
/* Note we do NOT use find_peer here, to avoid realtime recursion */
/* We also use a case-sensitive comparison (unlike find_peer) so
@@ -16953,7 +16929,22 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, str
global_allowsubscribe = TRUE; /* No global ban any more */
if (!found && ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC) && !ast_test_flag(&peer->flags[0], SIP_REALTIME))
reg_source_db(peer);
+
+ /* If they didn't request that MWI is sent *only* on subscribe, go ahead and
+ * subscribe to it now. */
+ if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY) &&
+ !ast_strlen_zero(peer->mailbox)) {
+ peer->mwi_event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, peer,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, peer->mailbox,
+ AST_EVENT_IE_END);
+ /* Send MWI from the event cache only. This is so we can send initial
+ * MWI if app_voicemail got loaded before chan_sip. If it is the other
+ * way, then we will get events when app_voicemail gets loaded. */
+ sip_send_mwi_to_peer(peer, NULL, 1);
+ }
+
ASTOBJ_UNMARK(peer);
+
ast_free_ha(oldha);
if (!ast_strlen_zero(callback)) { /* build string from peer info */
char *reg_string;
@@ -17047,7 +17038,6 @@ static int reload_config(enum channelreloadreason reason)
global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
global_regattempts_max = 0;
pedanticsipchecking = DEFAULT_PEDANTIC;
- global_mwitime = DEFAULT_MWITIME;
autocreatepeer = DEFAULT_AUTOCREATEPEER;
global_autoframing = 0;
global_allowguest = DEFAULT_ALLOWGUEST;
@@ -17135,11 +17125,6 @@ static int reload_config(enum channelreloadreason reason)
ast_set2_flag(&global_flags[0], ast_true(v->value), SIP_USEREQPHONE);
} else if (!strcasecmp(v->name, "relaxdtmf")) {
global_relaxdtmf = ast_true(v->value);
- } else if (!strcasecmp(v->name, "checkmwi")) {
- if ((sscanf(v->value, "%d", &global_mwitime) != 1) || (global_mwitime < 0)) {
- ast_log(LOG_WARNING, "'%s' is not a valid MWI time setting at line %d. Using default (10).\n", v->value, v->lineno);
- global_mwitime = DEFAULT_MWITIME;
- }
} else if (!strcasecmp(v->name, "vmexten")) {
ast_copy_string(default_vmexten, v->value, sizeof(default_vmexten));
} else if (!strcasecmp(v->name, "rtptimeout")) {
diff --git a/channels/chan_zap.c b/channels/chan_zap.c
index 93b01d405..3703b40f9 100644
--- a/channels/chan_zap.c
+++ b/channels/chan_zap.c
@@ -106,6 +106,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/abstract_jb.h"
#include "asterisk/smdi.h"
#include "asterisk/astobj.h"
+#include "asterisk/event.h"
+
#define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
/*! Global jitterbuffer configuration - by default, jb is disabled */
@@ -261,6 +263,14 @@ static enum ast_bridge_result zt_bridge(struct ast_channel *c0, struct ast_chann
static int zt_sendtext(struct ast_channel *c, const char *text);
+static void mwi_event_cb(const struct ast_event *event, void *userdata)
+{
+ /* This module does not handle MWI in an event-based manner. However, it
+ * subscribes to MWI for each mailbox that is configured so that the core
+ * knows that we care about it. Then, chan_zap will get the MWI from the
+ * event cache instead of checking the mailbox directly. */
+}
+
/*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
static inline int zt_get_event(int fd)
{
@@ -601,6 +611,7 @@ static struct zt_pvt {
struct tdd_state *tdd; /*!< TDD flag */
char call_forward[AST_MAX_EXTENSION];
char mailbox[AST_MAX_EXTENSION];
+ struct ast_event_sub *mwi_event_sub;
char dialdest[256];
int onhooktime;
int msgstate;
@@ -1831,8 +1842,21 @@ static int send_cwcidspill(struct zt_pvt *p)
static int has_voicemail(struct zt_pvt *p)
{
+ int new_msgs;
+ struct ast_event *event;
- return ast_app_has_voicemail(p->mailbox, NULL);
+ event = ast_event_get_cached(AST_EVENT_MWI,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, p->mailbox,
+ AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
+ AST_EVENT_IE_END);
+
+ if (event) {
+ new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
+ ast_event_destroy(event);
+ } else
+ new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
+
+ return new_msgs;
}
static int send_callerid(struct zt_pvt *p)
@@ -2390,6 +2414,8 @@ static void destroy_zt_pvt(struct zt_pvt **pvt)
p->next->prev = p->prev;
if (p->use_smdi)
ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
+ if (p->mwi_event_sub)
+ ast_event_unsubscribe(p->mwi_event_sub);
ast_mutex_destroy(&p->lock);
free(p);
*pvt = NULL;
@@ -7191,7 +7217,7 @@ static void *do_monitor(void *data)
if (last) {
if (!last->cidspill && !last->owner && !ast_strlen_zero(last->mailbox) && (thispass - last->onhooktime > 3) &&
(last->sig & __ZT_SIG_FXO)) {
- res = ast_app_has_voicemail(last->mailbox, NULL);
+ res = has_voicemail(last);
if (last->msgstate != res) {
int x;
if (option_debug)
@@ -7872,6 +7898,12 @@ static struct zt_pvt *mkintf(int channel, struct zt_chan_conf conf, struct zt_pr
tmp->cid_ton = 0;
ast_copy_string(tmp->cid_name, conf.chan.cid_name, sizeof(tmp->cid_name));
ast_copy_string(tmp->mailbox, conf.chan.mailbox, sizeof(tmp->mailbox));
+ if (!ast_strlen_zero(tmp->mailbox)) {
+ tmp->mwi_event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, NULL,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, tmp->mailbox,
+ AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
+ AST_EVENT_IE_END);
+ }
tmp->msgstate = -1;
tmp->group = conf.chan.group;
tmp->callgroup = conf.chan.callgroup;
diff --git a/configs/sip.conf.sample b/configs/sip.conf.sample
index 45890126d..71bcbdae7 100644
--- a/configs/sip.conf.sample
+++ b/configs/sip.conf.sample
@@ -69,7 +69,6 @@ srvlookup=yes ; Enable DNS SRV lookups on outbound calls
;t1min=100 ; Minimum roundtrip time for messages to monitored hosts
; Defaults to 100 ms
;notifymimetype=text/plain ; Allow overriding of mime type in MWI NOTIFY
-;checkmwi=10 ; Default time between mailbox checks for peers
;buggymwi=no ; Cisco SIP firmware doesn't support the MWI RFC
; fully. Enable this option to not get error messages
; when sending MWI to phones with this bug.
diff --git a/configs/voicemail.conf.sample b/configs/voicemail.conf.sample
index 031628eef..e44926169 100644
--- a/configs/voicemail.conf.sample
+++ b/configs/voicemail.conf.sample
@@ -126,6 +126,19 @@ emaildateformat=%A, %B %d, %Y at %r
; You can override the default program to send e-mail if you wish, too
;
;mailcmd=/usr/sbin/sendmail -t
+;
+;pollmailboxes=no ; If mailboxes are changed anywhere outside of app_voicemail,
+; ; then this option must be enabled for MWI to work. This
+; ; enables polling mailboxes for changes. Normally, it will
+; ; expect that changes are only made when someone called in
+; ; to one of the voicemail applications.
+; ; Examples of situations that would require this option are
+; ; web interfaces to voicemail or an email client in the case
+; ; of using IMAP storage.
+;
+;pollfreq=30 ; If the "pollmailboxes" option is enabled, this option
+; ; sets the polling frequency. The default is once every
+; ; 30 seconds.
;
; Users may be located in different timezones, or may have different
; message announcements for their introductory message when they enter
diff --git a/include/asterisk.h b/include/asterisk.h
index ffd081fdf..6ec3c8452 100644
--- a/include/asterisk.h
+++ b/include/asterisk.h
@@ -84,6 +84,7 @@ int dnsmgr_init(void); /*!< Provided by dnsmgr.c */
void dnsmgr_start_refresh(void); /*!< Provided by dnsmgr.c */
int dnsmgr_reload(void); /*!< Provided by dnsmgr.c */
void threadstorage_init(void); /*!< Provided by threadstorage.c */
+void ast_event_init(void); /*!< Provided by event.c */
/* Many headers need 'ast_channel' to be defined */
struct ast_channel;
diff --git a/include/asterisk/event.h b/include/asterisk/event.h
new file mode 100644
index 000000000..18a4c706c
--- /dev/null
+++ b/include/asterisk/event.h
@@ -0,0 +1,416 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \author Russell Bryant <russell@digium.com>
+ * \brief Generic event system
+ *
+ * The purpose of this API is to provide a generic way to share events between
+ * Asterisk modules. Code can generate events, and other code can subscribe to
+ * them.
+ *
+ * Events have an associated event type, as well as information elements. The
+ * information elements are the meta data that go along with each event. For
+ * example, in the case of message waiting indication, the event type is MWI,
+ * and each MWI event containts at least three information elements: the
+ * mailbox, the number of new messages, and the number of old messages.
+ *
+ * Subscriptions to events consist of an event type and information elements,
+ * as well. Subscriptions can be to all events, or a certain subset of events.
+ * If an event type is provided, only events of that type will be sent to this
+ * subscriber. Furthermore, if information elements are supplied with the
+ * subscription, only events that contain the specified information elements
+ * with specified values will be sent to the subscriber. For example, when a
+ * SIP phone subscribes to MWI for mailbox 1234, then chan_sip can subscribe
+ * to internal Asterisk MWI events with the MAILBOX information element with
+ * a value of "1234".
+ *
+ * Another key feature of this event system is the ability to cache events.
+ * It is useful for some types of events to be able to remember the last known
+ * value. These are usually events that indicate some kind of state change.
+ * In the example of MWI, app_voicemail can instruct the event core to cache
+ * these events based on the mailbox. So, the last known MWI state of each
+ * mailbox will be cached, and other modules can retrieve this information
+ * on demand without having to poll the mailbox directly.
+ */
+
+#ifndef AST_EVENT_H
+#define AST_EVENT_H
+
+#include "asterisk/event_defs.h"
+
+/*!
+ * \brief Subscriber event callback type
+ *
+ * \param event the event being passed to the subscriber
+ * \param userdata the data provider in the call to ast_event_subscribe()
+ *
+ * \return The event callbacks do not return anything.
+ */
+typedef void (*ast_event_cb_t)(const struct ast_event *event, void *userdata);
+
+/*!
+ * \brief Subscribe to events
+ *
+ * \param event_type The type of events to subscribe to
+ * \param cb The function to be called with events
+ * \param userdata data to be passed to the event callback
+ *
+ * The rest of the arguments to this function specify additional parameters for
+ * the subscription to filter which events are passed to this subscriber. The
+ * arguments must be in sets of:
+ * \code
+ * <enum ast_event_ie_type>, [enum ast_event_ie_pltype, [payload] ]
+ * \endcode
+ * and must end with AST_EVENT_IE_END.
+ *
+ * If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed
+ * by a valid IE payload type. If the payload type specified is
+ * AST_EVENT_IE_PLTYPE_EXISTS, then the 3rd argument should not be provided.
+ * Otherwise, a payload must also be specified.
+ *
+ * \return This returns a reference to the subscription for use with
+ * un-subscribing later. If there is a failure in creating the
+ * subscription, NULL will be returned.
+ *
+ * Example usage:
+ *
+ * \code
+ * peer->mwi_event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, peer,
+ * AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, peer->mailbox,
+ * AST_EVENT_IE_END);
+ * \endcode
+ *
+ * This creates a subscription to AST_EVENT_MWI events that contain an
+ * information element, AST_EVENT_IE_MAILBOX, with the same string value
+ * contained in peer->mailbox. Also, the event callback will be passed a
+ * pointer to the peer.
+ */
+struct ast_event_sub *ast_event_subscribe(enum ast_event_type event_type,
+ ast_event_cb_t cb, void *userdata, ...);
+
+/*!
+ * \brief Un-subscribe from events
+ *
+ * \param event_sub This is the reference to the subscription returned by
+ * ast_event_subscribe.
+ *
+ * \return Nothing
+ */
+void ast_event_unsubscribe(struct ast_event_sub *event_sub);
+
+/*!
+ * \brief Check if subscribers exist
+ *
+ * \param event_type This is the type of event that the caller would like to
+ * check for subscribers to.
+ *
+ * The rest of the arguments to this function specify additional parameters for
+ * checking for subscriptions to subsets of an event type. The arguments must
+ * in sets of:
+ * \code
+ * <enum ast_event_ie_type>, [enum ast_event_ie_pltype, [payload] ]
+ * \endcode
+ * and must end with AST_EVENT_IE_END.
+ *
+ * If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed
+ * by a valid IE payload type. If the payload type specified is
+ * AST_EVENT_IE_PLTYPE_EXISTS, then the 3rd argument should not be provided.
+ * Otherwise, a payload must also be specified.
+ *
+ * \return This returns one of the values defined in the ast_event_subscriber_res
+ * enum which will indicate if subscribers exist that match the given
+ * criteria.
+ *
+ * Example usage:
+ *
+ * \code
+ * if (ast_event_check_subscriber(AST_EVENT_MWI,
+ * AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
+ * AST_EVENT_IE_END) == AST_EVENT_SUB_NONE) {
+ * return;
+ * }
+ * \endcode
+ *
+ * This example will check if there are any subscribers to MWI events for the
+ * mailbox defined in the "mailbox" variable.
+ */
+enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type event_type, ...);
+
+/*!
+ * \brief Report current subscriptions to a subscription subscriber
+ *
+ * \arg sub the subscription subscriber
+ *
+ * \return nothing
+ *
+ * This reports all of the current subscribers to a subscriber of
+ * subscribers to a specific event type. (Try saying that a few times fast).
+ *
+ * The idea here is that it is sometimes very useful for a module to know when
+ * someone subscribes to events. However, when they first subscribe, this
+ * provides that module the ability to request the event core report to them
+ * all of the subscriptions to that event type that already exist.
+ */
+void ast_event_report_subs(const struct ast_event_sub *sub);
+
+/*!
+ * \brief Create a new event
+ *
+ * \param event_type The type of event to create
+ *
+ * The rest of the arguments to this function specify information elements to
+ * add to the event. They are specified in the form:
+ * \code
+ * <enum ast_event_ie_type>, [enum ast_event_ie_pltype, [payload] ]
+ * \endcode
+ * and must end with AST_EVENT_IE_END.
+ *
+ * If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed
+ * by a valid IE payload type. The payload type, EXISTS, should not be used here
+ * because it makes no sense to do so. So, a payload must also be specified
+ * after the IE payload type.
+ *
+ * \return This returns the event that has been created. If there is an error
+ * creating the event, NULL will be returned.
+ *
+ * Example usage:
+ *
+ * \code
+ * if (!(event = ast_event_new(AST_EVENT_MWI,
+ * AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
+ * AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, new,
+ * AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, old,
+ * AST_EVENT_IE_END))) {
+ * return;
+ * }
+ * \code
+ *
+ * This creates a MWI event with 3 information elements, a mailbox which is
+ * a string, and the number of new and old messages, specified as integers.
+ */
+struct ast_event *ast_event_new(enum ast_event_type event_type, ...);
+
+/*!
+ * \brief Destroy an event
+ *
+ * \param event the event to destroy
+ *
+ * \return Nothing
+ *
+ * \note Events that have been queued should *not* be destroyed by the code that
+ * created the event. It will be automatically destroyed after being
+ * dispatched to the appropriate subscribers.
+ */
+void ast_event_destroy(struct ast_event *event);
+
+/*!
+ * \brief Queue an event
+ *
+ * \param event the event to be queued
+ *
+ * \retval zero success
+ * \retval non-zero failure
+ *
+ * This function queues an event to be dispatched to all of the appropriate
+ * subscribers. This function will not block while the event is being
+ * dispatched because a pool of event dispatching threads handle the event
+ * queue.
+ */
+int ast_event_queue(struct ast_event *event);
+
+/*!
+ * \brief Queue and cache an event
+ *
+ * \param event the event to be queued and cached
+ *
+ * The rest of the arguments to this function specify information elements to
+ * use for determining which events in the cache that this event should replace.
+ * All events in the cache that match the specified criteria will be removed from
+ * the cache and then this one will be added. The arguments are specified in
+ * the form:
+ *
+ * \code
+ * <enum ast_event_ie_type>, [enum ast_event_ie_pltype]
+ * \endcode
+ * and must end with AST_EVENT_IE_END.
+ *
+ * If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed
+ * by a valid IE payload type. If the payload type given is EXISTS, then all
+ * events that contain that information element will be removed from the cache.
+ * Otherwise, all events in the cache that contain an information element with
+ * the same value as the new event will be removed.
+ *
+ * \note If more than one IE parameter is specified, they *all* must match for
+ * the event to be removed from the cache.
+ *
+ * Example usage:
+ *
+ * \code
+ * ast_event_queue_and_cache(event,
+ * AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR,
+ * AST_EVENT_IE_END);
+ * \endcode
+ *
+ * This example queues and caches an event. Any events in the cache that have
+ * the same MAILBOX information element as this event will be removed.
+ *
+ * The purpose of caching events is so that the core can retain the last known
+ * information for events that represent some sort of state. That way, when
+ * code needs to find out the current state, it can query the cache.
+ */
+int ast_event_queue_and_cache(struct ast_event *event, ...);
+
+/*!
+ * \brief Retrieve an event from the cache
+ *
+ * \param event_type The type of event to retrieve from the cache
+ *
+ * The rest of the arguments to this function specify information elements to
+ * match for retrieving events from the cache. They are specified in the form:
+ * \code
+ * <enum ast_event_ie_type>, [enum ast_event_ie_pltype, [payload] ]
+ * \endcode
+ * and must end with AST_EVENT_IE_END.
+ *
+ * If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed
+ * by a valid IE payload type. If the payload type specified is
+ * AST_EVENT_IE_PLTYPE_EXISTS, then the 3rd argument should not be provided.
+ * Otherwise, a payload must also be specified.
+ *
+ * \return A reference to an event retrieved from the cache. If no event was
+ * found that matches the specified criteria, then NULL will be returned.
+ *
+ * \note If more than one event in the cache matches the specified criteria, only
+ * one will be returned, and it is undefined which one it will be.
+ *
+ * \note The caller of this function *must* call ast_event_destroy() on the
+ * returned event after it is done using it.
+ *
+ * Example Usage:
+ *
+ * \code
+ * event = ast_event_get_cached(AST_EVENT_MWI,
+ * AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
+ * AST_EVENT_IE_END);
+ * \endcode
+ *
+ * This example will check for an MWI event in the cache that matches the
+ * specified mailbox. This would be the way to find out the last known state
+ * of a mailbox without having to poll the mailbox directly.
+ */
+struct ast_event *ast_event_get_cached(enum ast_event_type, ...);
+
+/*!
+ * \brief Append an information element that has a string payload
+ *
+ * \param event the event that the IE will be appended to
+ * \param ie_type the type of IE to append
+ * \param str The string for the payload of the IE
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ *
+ * The pointer to the event will get updated with the new location for the event
+ * that now contains the appended information element. If the re-allocation of
+ * the memory for this event fails, it will be set to NULL.
+ */
+int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
+ const char *str);
+
+/*!
+ * \brief Append an information element that has an integer payload
+ *
+ * \param event the event that the IE will be appended to
+ * \param ie_type the type of IE to append
+ * \param data The integer for the payload of the IE
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ *
+ * The pointer to the event will get updated with the new location for the event
+ * that now contains the appended information element. If the re-allocation of
+ * the memory for this event fails, it will be set to NULL.
+ */
+int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
+ uint32_t data);
+
+/*!
+ * \brief Append an information element that has a raw payload
+ *
+ * \param event the event that the IE will be appended to
+ * \param ie_type the type of IE to append
+ * \param data A pointer to the raw data for the payload of the IE
+ * \param data_len The amount of data to copy into the payload
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ *
+ * The pointer to the event will get updated with the new location for the event
+ * that now contains the appended information element. If the re-allocation of
+ * the memory for this event fails, it will be set to NULL.
+ */
+int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
+ const void *data, size_t data_len);
+
+/*!
+ * \brief Get the value of an information element that has an integer payload
+ *
+ * \param event The event to get the IE from
+ * \param ie_type the type of information element to retrieve
+ *
+ * \return This returns the payload of the information element with the given type.
+ * However, an IE with a payload of 0, and the case where no IE is found
+ * yield the same return value.
+ */
+uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type);
+
+/*!
+ * \brief Get the value of an information element that has a string payload
+ *
+ * \param event The event to get the IE from
+ * \param ie_type the type of information element to retrieve
+ *
+ * \return This returns the payload of the information element with the given type.
+ * If the information element isn't found, NULL will be returned.
+ */
+const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type);
+
+/*!
+ * \brief Get the value of an information element that has a raw payload
+ *
+ * \param event The event to get the IE from
+ * \param ie_type the type of information element to retrieve
+ *
+ * \return This returns the payload of the information element with the given type.
+ * If the information element isn't found, NULL will be returned.
+ */
+const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type);
+
+/*!
+ * \brief Get the type for an event
+ *
+ * \param event the event to get the type for
+ *
+ * \return the event type as represented by one of the values in the
+ * ast_event_type enum
+ */
+enum ast_event_type ast_event_get_type(const struct ast_event *event);
+
+#endif /* AST_EVENT_H */
diff --git a/include/asterisk/event_defs.h b/include/asterisk/event_defs.h
new file mode 100644
index 000000000..d6023e259
--- /dev/null
+++ b/include/asterisk/event_defs.h
@@ -0,0 +1,120 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \author Russell Bryant <russell@digium.com>
+ * \brief Generic event system
+ */
+
+#ifndef AST_EVENT_DEFS_H
+#define AST_EVENT_DEFS_H
+
+/*! \brief Event types
+ * \note These values can *never* change. */
+enum ast_event_type {
+ /*! Reserved to provide the ability to subscribe to all events. A specific
+ event should never have a payload of 0. */
+ AST_EVENT_ALL = 0x00,
+ /*! This event type is reserved for use by third-party modules to create
+ custom events without having to modify this file.
+ \note There are no "custom" IE types, because IEs only have to be
+ unique to the event itself, not necessarily across all events. */
+ AST_EVENT_CUSTOM = 0x01,
+ /*! Voicemail message waiting indication */
+ AST_EVENT_MWI = 0x02,
+ /*! Someone has subscribed to events */
+ AST_EVENT_SUB = 0x03,
+ /*! Someone has unsubscribed from events */
+ AST_EVENT_UNSUB = 0x04,
+ /*! Number of event types. This should be the last event type + 1 */
+ AST_EVENT_TOTAL = 0x05,
+};
+
+/*! \brief Event Information Element types */
+enum ast_event_ie_type {
+ /*! Used to terminate the arguments to event functions */
+ AST_EVENT_IE_END = -1,
+
+ /*!
+ * \brief Number of new messages
+ * Used by: AST_EVENT_MWI
+ * Payload type: UINT
+ */
+ AST_EVENT_IE_NEWMSGS = 0x01,
+ /*!
+ * \brief Number of
+ * Used by: AST_EVENT_MWI
+ * Payload type: UINT
+ */
+ AST_EVENT_IE_OLDMSGS = 0x02,
+ /*!
+ * \brief Mailbox name (mailbox[@context])
+ * Used by: AST_EVENT_MWI
+ * Payload type: STR
+ */
+ AST_EVENT_IE_MAILBOX = 0x03,
+ /*!
+ * \brief Unique ID
+ * Used by: AST_EVENT_SUB, AST_EVENT_UNSUB
+ * Payload type: UINT
+ */
+ AST_EVENT_IE_UNIQUEID = 0x04,
+ /*!
+ * \brief Event type
+ * Used by: AST_EVENT_SUB, AST_EVENT_UNSUB
+ * Payload type: UINT
+ */
+ AST_EVENT_IE_EVENTTYPE = 0x05,
+ /*!
+ * \brief Hint that someone cares than an IE exists
+ * Used by: AST_EVENT_SUB
+ * Payload type: UINT (ast_event_ie_type)
+ */
+ AST_EVENT_IE_EXISTS = 0x06,
+};
+
+/*!
+ * \brief Payload types for event information elements
+ */
+enum ast_event_ie_pltype {
+ /*! Just check if it exists, not the value */
+ AST_EVENT_IE_PLTYPE_EXISTS,
+ /*! Unsigned Integer (Can be used for signed, too ...) */
+ AST_EVENT_IE_PLTYPE_UINT,
+ /*! String */
+ AST_EVENT_IE_PLTYPE_STR,
+};
+
+/*!
+ * \brief Results for checking for subscribers
+ *
+ * \ref ast_event_check_subscriber()
+ */
+enum ast_event_subscriber_res {
+ /*! No subscribers exist */
+ AST_EVENT_SUB_NONE,
+ /*! At least one subscriber exists */
+ AST_EVENT_SUB_EXISTS,
+};
+
+struct ast_event;
+struct ast_event_ie;
+struct ast_event_sub;
+
+#endif /* AST_EVENT_DEFS_H */
diff --git a/main/Makefile b/main/Makefile
index f552fabda..a559810fb 100644
--- a/main/Makefile
+++ b/main/Makefile
@@ -26,7 +26,7 @@ OBJS= io.o sched.o logger.o frame.o loader.o config.o channel.o \
utils.o plc.o jitterbuf.o dnsmgr.o devicestate.o \
netsock.o slinfactory.o ast_expr2.o ast_expr2f.o \
cryptostub.o sha1.o http.o fixedjitterbuf.o abstract_jb.o \
- strcompat.o threadstorage.o dial.o
+ strcompat.o threadstorage.o dial.o event.o
# we need to link in the objects statically, not as a library, because
# otherwise modules will not have them available if none of the static
diff --git a/main/asterisk.c b/main/asterisk.c
index 995b6df02..43d330603 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -2593,6 +2593,7 @@ int main(int argc, char *argv[])
ast_alaw_init();
callerid_init();
ast_builtins_init();
+ ast_event_init();
ast_utils_init();
tdd_init();
/* When Asterisk restarts after it has dropped the root privileges,
diff --git a/main/dial.c b/main/dial.c
index fc0af983c..590a2829c 100644
--- a/main/dial.c
+++ b/main/dial.c
@@ -47,23 +47,23 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
/*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
struct ast_dial {
- int num; /*! Current number to give to next dialed channel */
- enum ast_dial_result state; /*! Status of dial */
- void *options[AST_DIAL_OPTION_MAX]; /*! Global options */
- ast_dial_state_callback state_callback; /*! Status callback */
- AST_LIST_HEAD_NOLOCK(, ast_dial_channel) channels; /*! Channels being dialed */
- pthread_t thread; /*! Thread (if running in async) */
+ int num; /*!< Current number to give to next dialed channel */
+ enum ast_dial_result state; /*!< Status of dial */
+ void *options[AST_DIAL_OPTION_MAX]; /*!< Global options */
+ ast_dial_state_callback state_callback; /*!< Status callback */
+ AST_LIST_HEAD_NOLOCK(, ast_dial_channel) channels; /*!< Channels being dialed */
+ pthread_t thread; /*!< Thread (if running in async) */
};
/*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
struct ast_dial_channel {
- int num; /*! Unique number for dialed channel */
- const char *tech; /*! Technology being dialed */
- const char *device; /*! Device being dialed */
- void *options[AST_DIAL_OPTION_MAX]; /*! Channel specific options */
- int cause; /*! Cause code in case of failure */
- struct ast_channel *owner; /*! Asterisk channel */
- AST_LIST_ENTRY(ast_dial_channel) list; /*! Linked list information */
+ int num; /*!< Unique number for dialed channel */
+ const char *tech; /*!< Technology being dialed */
+ const char *device; /*!< Device being dialed */
+ void *options[AST_DIAL_OPTION_MAX]; /*!< Channel specific options */
+ int cause; /*!< Cause code in case of failure */
+ struct ast_channel *owner; /*!< Asterisk channel */
+ AST_LIST_ENTRY(ast_dial_channel) list; /*!< Linked list information */
};
/*! \brief Typedef for dial option enable */
diff --git a/main/event.c b/main/event.c
new file mode 100644
index 000000000..e36c23b93
--- /dev/null
+++ b/main/event.c
@@ -0,0 +1,774 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Internal generic event system
+ *
+ * \author Russell Bryant <russell@digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "asterisk/event.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/lock.h"
+#include "asterisk/utils.h"
+
+#define NUM_EVENT_THREADS 5
+
+struct ast_event_ie {
+ enum ast_event_ie_type ie_type:16;
+ /*! Total length of the IE payload */
+ uint16_t ie_payload_len;
+ unsigned char ie_payload[0];
+} __attribute__ ((packed));
+
+/*!
+ * \brief An event
+ *
+ * \note The format of this structure is important, and can not change, since
+ * they are sent directly over the network (via IAX2).
+ *
+ */
+struct ast_event {
+ /*! Event type */
+ enum ast_event_type type:16;
+ /*! Total length of the event */
+ uint16_t event_len:16;
+ /*! The data payload of the event, made up of information elements */
+ unsigned char payload[0];
+} __attribute__ ((packed));
+
+struct ast_event_ref {
+ struct ast_event *event;
+ AST_LIST_ENTRY(ast_event_ref) entry;
+};
+
+/*! \brief data shared between event dispatching threads */
+static struct {
+ ast_cond_t cond;
+ ast_mutex_t lock;
+ AST_LIST_HEAD_NOLOCK(, ast_event_ref) event_q;
+} event_thread = {
+ .lock = AST_MUTEX_INIT_VALUE,
+};
+
+struct ast_event_ie_val {
+ AST_LIST_ENTRY(ast_event_ie_val) entry;
+ enum ast_event_ie_type ie_type;
+ enum ast_event_ie_pltype ie_pltype;
+ union {
+ uint32_t uint;
+ const char *str;
+ } payload;
+};
+
+/*! \brief Event subscription */
+struct ast_event_sub {
+ enum ast_event_type type;
+ ast_event_cb_t cb;
+ void *userdata;
+ uint32_t uniqueid;
+ AST_LIST_HEAD_NOLOCK(, ast_event_ie_val) ie_vals;
+ AST_RWLIST_ENTRY(ast_event_sub) entry;
+};
+
+static uint32_t sub_uniqueid;
+
+/*! \brief Event subscriptions
+ * The event subscribers are indexed by which event they are subscribed to */
+static AST_RWLIST_HEAD(ast_event_sub_list, ast_event_sub) ast_event_subs[AST_EVENT_TOTAL];
+
+/*! \brief Cached events
+ * The event cache is indexed on the event type. The purpose of this is
+ * for events that express some sort of state. So, when someone first
+ * needs to know this state, it can get the last known state from the cache. */
+static AST_RWLIST_HEAD(ast_event_ref_list, ast_event_ref) ast_event_cache[AST_EVENT_TOTAL];
+
+static void ast_event_ie_val_destroy(struct ast_event_ie_val *ie_val)
+{
+ if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
+ free((void *) ie_val->payload.str);
+
+ free(ie_val);
+}
+
+enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type type, ...)
+{
+ va_list ap;
+ enum ast_event_ie_type ie_type;
+ enum ast_event_subscriber_res res = AST_EVENT_SUB_NONE;
+ struct ast_event_ie_val *ie_val, *sub_ie_val;
+ struct ast_event_sub *sub;
+ AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
+
+ if (type >= AST_EVENT_TOTAL) {
+ ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
+ return res;
+ }
+
+ va_start(ap, type);
+ for (ie_type = va_arg(ap, enum ast_event_type);
+ ie_type != AST_EVENT_IE_END;
+ ie_type = va_arg(ap, enum ast_event_type))
+ {
+ struct ast_event_ie_val *ie_val = alloca(sizeof(*ie_val));
+ memset(ie_val, 0, sizeof(*ie_val));
+ ie_val->ie_type = ie_type;
+ ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
+ if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
+ ie_val->payload.uint = va_arg(ap, uint32_t);
+ else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
+ ie_val->payload.str = ast_strdupa(va_arg(ap, const char *));
+ AST_LIST_INSERT_TAIL(&ie_vals, ie_val, entry);
+ }
+ va_end(ap);
+
+ AST_RWLIST_RDLOCK(&ast_event_subs[type]);
+ AST_RWLIST_TRAVERSE(&ast_event_subs[type], sub, entry) {
+ AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
+ AST_LIST_TRAVERSE(&sub->ie_vals, sub_ie_val, entry) {
+ if (sub_ie_val->ie_type == ie_val->ie_type)
+ break;
+ }
+ if (!sub_ie_val) {
+ if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS)
+ break;
+ continue;
+ }
+ /* The subscriber doesn't actually care what the value is */
+ if (sub_ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS)
+ continue;
+ if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
+ ie_val->payload.uint != sub_ie_val->payload.uint)
+ break;
+ if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
+ strcmp(ie_val->payload.str, sub_ie_val->payload.str))
+ break;
+ }
+ if (!ie_val)
+ break;
+ }
+ AST_RWLIST_UNLOCK(&ast_event_subs[type]);
+
+ if (sub) /* All parameters were matched */
+ return AST_EVENT_SUB_EXISTS;
+
+ AST_RWLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
+ if (!AST_LIST_EMPTY(&ast_event_subs[AST_EVENT_ALL]))
+ res = AST_EVENT_SUB_EXISTS;
+ AST_RWLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
+
+ return res;
+}
+
+/*! Send AST_EVENT_SUB events to this subscriber of ... subscriber events */
+void ast_event_report_subs(const struct ast_event_sub *event_sub)
+{
+ struct ast_event *event;
+ struct ast_event_sub *sub;
+ enum ast_event_type event_type = -1;
+ struct ast_event_ie_val *ie_val;
+
+ if (event_sub->type != AST_EVENT_SUB)
+ return;
+
+ AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
+ if (ie_val->ie_type == AST_EVENT_IE_EVENTTYPE) {
+ event_type = ie_val->payload.uint;
+ break;
+ }
+ }
+
+ if (event_type == -1)
+ return;
+
+ AST_RWLIST_RDLOCK(&ast_event_subs[event_type]);
+ AST_RWLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
+ if (event_sub == sub)
+ continue;
+
+ event = ast_event_new(AST_EVENT_SUB,
+ AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
+ AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
+ AST_EVENT_IE_END);
+
+ AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
+ switch (ie_val->ie_pltype) {
+ case AST_EVENT_IE_PLTYPE_EXISTS:
+ ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
+ break;
+ case AST_EVENT_IE_PLTYPE_UINT:
+ ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
+ break;
+ case AST_EVENT_IE_PLTYPE_STR:
+ ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
+ break;
+ }
+ if (!event)
+ break;
+ }
+
+ if (!event)
+ continue;
+
+ event_sub->cb(event, event_sub->userdata);
+
+ ast_event_destroy(event);
+ }
+ AST_RWLIST_UNLOCK(&ast_event_subs[event_type]);
+}
+
+struct ast_event_sub *ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb,
+ void *userdata, ...)
+{
+ va_list ap;
+ enum ast_event_ie_type ie_type;
+ struct ast_event_sub *sub;
+ struct ast_event *event;
+
+ if (type >= AST_EVENT_TOTAL) {
+ ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
+ return NULL;
+ }
+
+ if (!(sub = ast_calloc(1, sizeof(*sub))))
+ return NULL;
+
+ va_start(ap, userdata);
+ for (ie_type = va_arg(ap, enum ast_event_type);
+ ie_type != AST_EVENT_IE_END;
+ ie_type = va_arg(ap, enum ast_event_type))
+ {
+ struct ast_event_ie_val *ie_val;
+ if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
+ continue;
+ ie_val->ie_type = ie_type;
+ ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
+ if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
+ ie_val->payload.uint = va_arg(ap, uint32_t);
+ else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR) {
+ if (!(ie_val->payload.str = ast_strdup(va_arg(ap, const char *)))) {
+ free(ie_val);
+ continue;
+ }
+ }
+ AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
+ }
+ va_end(ap);
+
+ sub->type = type;
+ sub->cb = cb;
+ sub->userdata = userdata;
+ sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
+
+ if (ast_event_check_subscriber(AST_EVENT_SUB,
+ AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, type,
+ AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
+ struct ast_event_ie_val *ie_val;
+
+ event = ast_event_new(AST_EVENT_SUB,
+ AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
+ AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
+ AST_EVENT_IE_END);
+
+ AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
+ switch (ie_val->ie_pltype) {
+ case AST_EVENT_IE_PLTYPE_EXISTS:
+ ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
+ break;
+ case AST_EVENT_IE_PLTYPE_UINT:
+ ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
+ break;
+ case AST_EVENT_IE_PLTYPE_STR:
+ ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
+ break;
+ }
+ if (!event)
+ break;
+ }
+
+ if (event)
+ ast_event_queue(event);
+ }
+
+ AST_RWLIST_WRLOCK(&ast_event_subs[type]);
+ AST_RWLIST_INSERT_TAIL(&ast_event_subs[type], sub, entry);
+ AST_RWLIST_UNLOCK(&ast_event_subs[type]);
+
+ return sub;
+}
+
+static void ast_event_sub_destroy(struct ast_event_sub *sub)
+{
+ struct ast_event_ie_val *ie_val;
+
+ while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry)))
+ ast_event_ie_val_destroy(ie_val);
+
+ free(sub);
+}
+
+void ast_event_unsubscribe(struct ast_event_sub *sub)
+{
+ struct ast_event *event;
+
+ AST_RWLIST_WRLOCK(&ast_event_subs[sub->type]);
+ AST_LIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
+ AST_RWLIST_UNLOCK(&ast_event_subs[sub->type]);
+
+ if (ast_event_check_subscriber(AST_EVENT_UNSUB,
+ AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
+ AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
+
+ event = ast_event_new(AST_EVENT_UNSUB,
+ AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
+ AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
+ AST_EVENT_IE_END);
+
+ if (event)
+ ast_event_queue(event);
+ }
+
+ ast_event_sub_destroy(sub);
+}
+
+enum ast_event_type ast_event_get_type(const struct ast_event *event)
+{
+ return ntohs(event->type);
+}
+
+uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
+{
+ const uint32_t *ie_val;
+
+ ie_val = ast_event_get_ie_raw(event, ie_type);
+
+ return ie_val ? ntohl(*ie_val) : 0;
+}
+
+const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
+{
+ return ast_event_get_ie_raw(event, ie_type);
+}
+
+const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
+{
+ struct ast_event_ie *ie;
+ uint16_t event_len;
+
+ ie_type = ntohs(ie_type);
+ event_len = ntohs(event->event_len);
+
+ ie = ((void *) event) + sizeof(*event);
+
+ while ((((void *) ie) - ((void *) event)) < event_len) {
+ if (ie->ie_type == ie_type)
+ return ie->ie_payload;
+ ie = ((void *) ie) + sizeof(*ie) + ntohs(ie->ie_payload_len);
+ }
+
+ return NULL;
+}
+
+int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
+ const char *str)
+{
+ return ast_event_append_ie_raw(event, ie_type, str, strlen(str) + 1);
+}
+
+int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
+ uint32_t data)
+{
+ data = htonl(data);
+ return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
+}
+
+int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
+ const void *data, size_t data_len)
+{
+ struct ast_event_ie *ie;
+ unsigned int extra_len;
+ uint16_t event_len;
+
+ event_len = ntohs((*event)->event_len);
+ extra_len = sizeof(*ie) + data_len;
+
+ if (!(*event = ast_realloc(*event, event_len + extra_len)))
+ return -1;
+
+ ie = ((void *) *event) + event_len;
+ ie->ie_type = htons(ie_type);
+ ie->ie_payload_len = htons(data_len);
+ memcpy(ie->ie_payload, data, data_len);
+
+ (*event)->event_len = htons(event_len + extra_len);
+
+ return 0;
+}
+
+struct ast_event *ast_event_new(enum ast_event_type type, ...)
+{
+ va_list ap;
+ struct ast_event *event;
+ enum ast_event_type ie_type;
+ struct ast_event_ie_val *ie_val;
+ AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
+
+ /* Invalid type */
+ if (type >= AST_EVENT_TOTAL) {
+ ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
+ "type '%d'!\n", type);
+ return NULL;
+ }
+
+ va_start(ap, type);
+ for (ie_type = va_arg(ap, enum ast_event_type);
+ ie_type != AST_EVENT_IE_END;
+ ie_type = va_arg(ap, enum ast_event_type))
+ {
+ struct ast_event_ie_val *ie_val = alloca(sizeof(*ie_val));
+ memset(ie_val, 0, sizeof(*ie_val));
+ ie_val->ie_type = ie_type;
+ ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
+ if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
+ ie_val->payload.uint = va_arg(ap, uint32_t);
+ else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
+ ie_val->payload.str = ast_strdupa(va_arg(ap, const char *));
+ AST_LIST_INSERT_TAIL(&ie_vals, ie_val, entry);
+ }
+ va_end(ap);
+
+ if (!(event = ast_calloc(1, sizeof(*event))))
+ return NULL;
+
+ event->type = htons(type);
+ event->event_len = htons(sizeof(*event));
+
+ AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
+ if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
+ ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
+ else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
+ ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
+
+ if (!event)
+ break;
+ }
+
+ return event;
+}
+
+void ast_event_destroy(struct ast_event *event)
+{
+ free(event);
+}
+
+static void ast_event_ref_destroy(struct ast_event_ref *event_ref)
+{
+ ast_event_destroy(event_ref->event);
+ free(event_ref);
+}
+
+static struct ast_event *ast_event_dup(const struct ast_event *event)
+{
+ struct ast_event *dup_event;
+ uint16_t event_len;
+
+ event_len = ntohs(event->event_len);
+
+ if (!(dup_event = ast_calloc(1, event_len)))
+ return NULL;
+
+ memcpy(dup_event, event, event_len);
+
+ return dup_event;
+}
+
+struct ast_event *ast_event_get_cached(enum ast_event_type type, ...)
+{
+ va_list ap;
+ enum ast_event_ie_type ie_type;
+ struct ast_event *dup_event = NULL;
+ struct ast_event_ref *event_ref;
+ struct cache_arg {
+ AST_LIST_ENTRY(cache_arg) entry;
+ enum ast_event_ie_type ie_type;
+ enum ast_event_ie_pltype ie_pltype;
+ union {
+ uint32_t uint;
+ const char *str;
+ } payload;
+ } *cache_arg;
+ AST_LIST_HEAD_NOLOCK_STATIC(cache_args, cache_arg);
+
+ if (type >= AST_EVENT_TOTAL) {
+ ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
+ return NULL;
+ }
+
+ va_start(ap, type);
+ for (ie_type = va_arg(ap, enum ast_event_type);
+ ie_type != AST_EVENT_IE_END;
+ ie_type = va_arg(ap, enum ast_event_type))
+ {
+ cache_arg = alloca(sizeof(*cache_arg));
+ memset(cache_arg, 0, sizeof(*cache_arg));
+ cache_arg->ie_type = ie_type;
+ cache_arg->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
+ if (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
+ cache_arg->payload.uint = va_arg(ap, uint32_t);
+ else if (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
+ cache_arg->payload.str = ast_strdupa(va_arg(ap, const char *));
+ AST_LIST_INSERT_TAIL(&cache_args, cache_arg, entry);
+ }
+ va_end(ap);
+
+ if (AST_LIST_EMPTY(&cache_args)) {
+ ast_log(LOG_ERROR, "Events can not be retrieved from the cache without "
+ "specifying at least one IE type!\n");
+ return NULL;
+ }
+
+ AST_RWLIST_RDLOCK(&ast_event_cache[type]);
+ AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[type], event_ref, entry) {
+ AST_LIST_TRAVERSE(&cache_args, cache_arg, entry) {
+ if ( ! ( (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
+ (cache_arg->payload.uint ==
+ ast_event_get_ie_uint(event_ref->event, cache_arg->ie_type))) ||
+
+ (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
+ (!strcmp(cache_arg->payload.str,
+ ast_event_get_ie_str(event_ref->event, cache_arg->ie_type)))) ||
+
+ (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS &&
+ ast_event_get_ie_raw(event_ref->event, cache_arg->ie_type)) ) )
+ {
+ break;
+ }
+ }
+ if (!cache_arg) {
+ /* All parameters were matched on this cache entry, so return it */
+ dup_event = ast_event_dup(event_ref->event);
+ break;
+ }
+ }
+ AST_RWLIST_TRAVERSE_SAFE_END
+ AST_RWLIST_UNLOCK(&ast_event_cache[type]);
+
+ return dup_event;
+}
+
+/*! \brief Duplicate an event and add it to the cache
+ * \note This assumes this index in to the cache is locked */
+static int ast_event_dup_and_cache(const struct ast_event *event)
+{
+ struct ast_event *dup_event;
+ struct ast_event_ref *event_ref;
+
+ if (!(dup_event = ast_event_dup(event)))
+ return -1;
+ if (!(event_ref = ast_calloc(1, sizeof(*event_ref))))
+ return -1;
+
+ event_ref->event = dup_event;
+
+ AST_LIST_INSERT_TAIL(&ast_event_cache[ntohs(event->type)], event_ref, entry);
+
+ return 0;
+}
+
+int ast_event_queue_and_cache(struct ast_event *event, ...)
+{
+ va_list ap;
+ enum ast_event_type ie_type;
+ uint16_t host_event_type;
+ struct ast_event_ref *event_ref;
+ int res;
+ struct cache_arg {
+ AST_LIST_ENTRY(cache_arg) entry;
+ enum ast_event_ie_type ie_type;
+ enum ast_event_ie_pltype ie_pltype;
+ } *cache_arg;
+ AST_LIST_HEAD_NOLOCK_STATIC(cache_args, cache_arg);
+
+ host_event_type = ntohs(event->type);
+
+ /* Invalid type */
+ if (host_event_type >= AST_EVENT_TOTAL) {
+ ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
+ "type '%d'!\n", host_event_type);
+ return -1;
+ }
+
+ va_start(ap, event);
+ for (ie_type = va_arg(ap, enum ast_event_type);
+ ie_type != AST_EVENT_IE_END;
+ ie_type = va_arg(ap, enum ast_event_type))
+ {
+ cache_arg = alloca(sizeof(*cache_arg));
+ memset(cache_arg, 0, sizeof(*cache_arg));
+ cache_arg->ie_type = ie_type;
+ cache_arg->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
+ AST_LIST_INSERT_TAIL(&cache_args, cache_arg, entry);
+ }
+ va_end(ap);
+
+ if (AST_LIST_EMPTY(&cache_args)) {
+ ast_log(LOG_ERROR, "Events can not be cached without specifying at "
+ "least one IE type!\n");
+ return ast_event_queue(event);
+ }
+
+ AST_RWLIST_WRLOCK(&ast_event_cache[host_event_type]);
+ AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[host_event_type], event_ref, entry) {
+ AST_LIST_TRAVERSE(&cache_args, cache_arg, entry) {
+ if ( ! ( (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
+ (ast_event_get_ie_uint(event, cache_arg->ie_type) ==
+ ast_event_get_ie_uint(event_ref->event, cache_arg->ie_type))) ||
+
+ (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
+ (!strcmp(ast_event_get_ie_str(event, cache_arg->ie_type),
+ ast_event_get_ie_str(event_ref->event, cache_arg->ie_type)))) ||
+
+ (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS &&
+ ast_event_get_ie_raw(event_ref->event, cache_arg->ie_type)) ) )
+ {
+ break;
+ }
+ }
+ if (!cache_arg) {
+ /* All parameters were matched on this cache entry, so remove it */
+ AST_LIST_REMOVE_CURRENT(&ast_event_cache[host_event_type], entry);
+ ast_event_ref_destroy(event_ref);
+ }
+ }
+ AST_RWLIST_TRAVERSE_SAFE_END
+ res = ast_event_dup_and_cache(event);
+ AST_RWLIST_UNLOCK(&ast_event_cache[host_event_type]);
+
+ return (ast_event_queue(event) || res) ? -1 : 0;
+}
+
+int ast_event_queue(struct ast_event *event)
+{
+ struct ast_event_ref *event_ref;
+ uint16_t host_event_type;
+
+ host_event_type = ntohs(event->type);
+
+ /* Invalid type */
+ if (host_event_type >= AST_EVENT_TOTAL) {
+ ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
+ "type '%d'!\n", host_event_type);
+ return -1;
+ }
+
+ /* If nobody has subscribed to this event type, throw it away now */
+ if (ast_event_check_subscriber(host_event_type, AST_EVENT_IE_END)
+ == AST_EVENT_SUB_NONE) {
+ ast_event_destroy(event);
+ return 0;
+ }
+
+ if (!(event_ref = ast_calloc(1, sizeof(*event_ref))))
+ return -1;
+
+ event_ref->event = event;
+
+ ast_mutex_lock(&event_thread.lock);
+ AST_LIST_INSERT_TAIL(&event_thread.event_q, event_ref, entry);
+ ast_cond_signal(&event_thread.cond);
+ ast_mutex_unlock(&event_thread.lock);
+
+ return 0;
+}
+
+static void *ast_event_dispatcher(void *unused)
+{
+ for (;;) {
+ struct ast_event_ref *event_ref;
+ struct ast_event_sub *sub;
+ uint16_t host_event_type;
+
+ ast_mutex_lock(&event_thread.lock);
+ while (!(event_ref = AST_LIST_REMOVE_HEAD(&event_thread.event_q, entry)))
+ ast_cond_wait(&event_thread.cond, &event_thread.lock);
+ ast_mutex_unlock(&event_thread.lock);
+
+ host_event_type = ntohs(event_ref->event->type);
+
+ /* Subscribers to this specific event first */
+ AST_RWLIST_RDLOCK(&ast_event_subs[host_event_type]);
+ AST_RWLIST_TRAVERSE(&ast_event_subs[host_event_type], sub, entry) {
+ struct ast_event_ie_val *ie_val;
+ AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
+ if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS &&
+ ast_event_get_ie_raw(event_ref->event, ie_val->ie_type)) {
+ continue;
+ } else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
+ ast_event_get_ie_uint(event_ref->event, ie_val->ie_type)
+ == ie_val->payload.uint) {
+ continue;
+ } else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
+ !strcmp(ast_event_get_ie_str(event_ref->event, ie_val->ie_type),
+ ie_val->payload.str)) {
+ continue;
+ }
+ break;
+ }
+ if (ie_val)
+ continue;
+ sub->cb(event_ref->event, sub->userdata);
+ }
+ AST_RWLIST_UNLOCK(&ast_event_subs[host_event_type]);
+
+ /* Now to subscribers to all event types */
+ AST_RWLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
+ AST_RWLIST_TRAVERSE(&ast_event_subs[AST_EVENT_ALL], sub, entry)
+ sub->cb(event_ref->event, sub->userdata);
+ AST_RWLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
+
+ ast_event_ref_destroy(event_ref);
+ }
+
+ return NULL;
+}
+
+void ast_event_init(void)
+{
+ int i;
+
+ for (i = 0; i < AST_EVENT_TOTAL; i++)
+ AST_RWLIST_HEAD_INIT(&ast_event_subs[i]);
+
+ for (i = 0; i < AST_EVENT_TOTAL; i++)
+ AST_RWLIST_HEAD_INIT(&ast_event_cache[i]);
+
+ ast_cond_init(&event_thread.cond, NULL);
+
+ for (i = 0; i < NUM_EVENT_THREADS; i++) {
+ pthread_t dont_care;
+ ast_pthread_create_background(&dont_care, NULL, ast_event_dispatcher, NULL);
+ }
+}
diff --git a/res/res_eventtest.c b/res/res_eventtest.c
new file mode 100644
index 000000000..1085eb57e
--- /dev/null
+++ b/res/res_eventtest.c
@@ -0,0 +1,177 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \author Russell Bryant <russell@digium.com>
+ *
+ * \brief Test code for the internal event system
+ *
+ */
+
+/*** MODULEINFO
+ <defaultenabled>no</defaultenabled>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "asterisk/module.h"
+#include "asterisk/event.h"
+#include "asterisk/cli.h"
+
+static void process_event_generic(const struct ast_event *event)
+{
+ ast_log(LOG_DEBUG, "Event type: %u\n", ast_event_get_type(event));
+}
+
+static void process_event_mwi(const struct ast_event *event)
+{
+ const char *mailbox;
+ unsigned int new;
+ unsigned int old;
+
+ mailbox = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
+ new = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
+ old = ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
+
+ ast_log(LOG_DEBUG, "MWI Event. Mailbox: %s New: %u Old: %u\n",
+ mailbox, new, old);
+}
+
+static void ast_event_process(const struct ast_event *event, void *userdata)
+{
+ switch (ast_event_get_type(event)) {
+ case AST_EVENT_MWI:
+ process_event_mwi(event);
+ break;
+ default:
+ process_event_generic(event);
+ }
+}
+
+static char *event_gen(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ struct ast_event *event;
+ const char *mailbox = "1234@fakecontext";
+ unsigned int new = 5;
+ unsigned int old = 12;
+ struct ast_event_sub *event_sub;
+
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "event generate";
+ e->usage =
+ "Usage: event generate\n"
+ " Generate a test event.\n";
+ return NULL;
+
+ case CLI_GENERATE:
+ return NULL; /* no completion */
+ }
+ if (a->argc != e->args)
+ return CLI_SHOWUSAGE;
+
+ if (!(event_sub = ast_event_subscribe(AST_EVENT_ALL, ast_event_process,
+ NULL, AST_EVENT_IE_END))) {
+ return CLI_FAILURE;
+ }
+
+ if (!(event = ast_event_new(AST_EVENT_MWI,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
+ AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, new,
+ AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, old,
+ AST_EVENT_IE_END))) {
+ return CLI_FAILURE;
+ }
+
+ ast_event_queue_and_cache(event,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR,
+ AST_EVENT_IE_END);
+
+ /* XXX This is a hack. I should use a timed thread condition instead. */
+ usleep(1000000);
+
+ ast_event_unsubscribe(event_sub);
+
+ return CLI_SUCCESS;
+}
+
+static char *event_get_cached(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ struct ast_event *event;
+ const char *mailbox = "1234@fakecontext";
+
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "event get cached";
+ e->usage =
+ "Usage: event get cached\n"
+ " Test getting an event from the cache.\n";
+ return NULL;
+
+ case CLI_GENERATE:
+ return NULL; /* no completion */
+ }
+ if (a->argc != e->args)
+ return CLI_SHOWUSAGE;
+
+ event = ast_event_get_cached(AST_EVENT_MWI,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
+ AST_EVENT_IE_END);
+
+ if (!event) {
+ ast_cli(a->fd, "No event retrieved!\n");
+ return CLI_FAILURE;
+ }
+
+ ast_cli(a->fd, "Got the event. New: %u Old: %u\n",
+ ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS),
+ ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS));
+
+ ast_event_destroy(event);
+
+ return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry cli_commands[] = {
+ NEW_CLI(event_gen, "Generate a test event"),
+ NEW_CLI(event_get_cached, "Get an event from the cache"),
+};
+
+static int load_module(void)
+{
+ ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
+
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+ ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
+
+ return 0;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Test code for the internal event system");