[asterisk-commits] mmichelson: branch mmichelson/caller_id r383752 - in /team/mmichelson/caller_...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Mar 25 15:01:42 CDT 2013


Author: mmichelson
Date: Mon Mar 25 15:01:38 2013
New Revision: 383752

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=383752
Log:
Initial Caller ID support.

This implements a simple supplement that will set caller ID on the session
on an incoming INVITE and set ID details on an outgoing session based on
channel data.

It's a pretty simple setup and it works quite well if the Dial 'o' option
is used since I have no connected line support in yet.

To make this work accurately, I've introduced a crude priority system for
session supplements so that we can ensure that they register in a proper
order even if modules load in unexpected intervals. This way I can guarantee
that on incoming INVITEs, the caller ID module is visited before chan_gulp.
This will need to be expanded to use some constants so that the priorities
make a bit more sense than they do at the moment.


Added:
    team/mmichelson/caller_id/res/res_sip_caller_id.c   (with props)
Modified:
    team/mmichelson/caller_id/channels/chan_gulp.c
    team/mmichelson/caller_id/include/asterisk/res_sip_session.h
    team/mmichelson/caller_id/res/res_sip_session.c

Modified: team/mmichelson/caller_id/channels/chan_gulp.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/caller_id/channels/chan_gulp.c?view=diff&rev=383752&r1=383751&r2=383752
==============================================================================
--- team/mmichelson/caller_id/channels/chan_gulp.c (original)
+++ team/mmichelson/caller_id/channels/chan_gulp.c Mon Mar 25 15:01:38 2013
@@ -154,6 +154,7 @@
 /*! \brief SIP session supplement structure */
 static struct ast_sip_session_supplement gulp_supplement = {
 	.method = "INVITE",
+	.priority = INT_MAX,
 	.session_begin = gulp_session_begin,
 	.session_end = gulp_session_end,
 	.incoming_request = gulp_incoming_request,
@@ -164,6 +165,7 @@
 
 static struct ast_sip_session_supplement gulp_ack_supplement = {
 	.method = "ACK",
+	.priority = 0,
 	.incoming_request = gulp_incoming_ack,
 };
 
@@ -399,7 +401,7 @@
 		return NULL;
 	}
 
-	if (!(chan = ast_channel_alloc(1, state, "", S_OR(cid_name, ""), "", "", "", linkedid, 0, "Gulp/%s-%.*s", ast_sorcery_object_get_id(session->endpoint),
+	if (!(chan = ast_channel_alloc(1, state, S_OR(session->caller.number.str, ""), S_OR(session->caller.name.str, ""), "", "", "", linkedid, 0, "Gulp/%s-%.*s", ast_sorcery_object_get_id(session->endpoint),
 		(int)session->inv_session->dlg->call_id->id.slen, session->inv_session->dlg->call_id->id.ptr))) {
 		ao2_cleanup(pvt);
 		return NULL;

Modified: team/mmichelson/caller_id/include/asterisk/res_sip_session.h
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/caller_id/include/asterisk/res_sip_session.h?view=diff&rev=383752&r1=383751&r2=383752
==============================================================================
--- team/mmichelson/caller_id/include/asterisk/res_sip_session.h (original)
+++ team/mmichelson/caller_id/include/asterisk/res_sip_session.h Mon Mar 25 15:01:38 2013
@@ -97,6 +97,8 @@
 	pj_timer_entry rescheduled_reinvite;
 	/* Format capabilities pertaining to direct media */
 	struct ast_format_cap *direct_media_cap;
+	/* Identity of calling party */
+	struct ast_party_id caller;
 };
 
 typedef int (*ast_sip_session_request_creation_cb)(struct ast_sip_session *session, pjsip_tx_data *tdata);
@@ -111,6 +113,8 @@
 struct ast_sip_session_supplement {
     /*! Method on which to call the callbacks. If NULL, call on all methods */
     const char *method;
+	/*! Priority for this supplement. Lower numbers are visited before higher numbers */
+	int priority;
     /*!
 	 * \brief Notification that the session has begun
 	 * This method will always be called from a SIP servant thread.

Added: team/mmichelson/caller_id/res/res_sip_caller_id.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/caller_id/res/res_sip_caller_id.c?view=auto&rev=383752
==============================================================================
--- team/mmichelson/caller_id/res/res_sip_caller_id.c (added)
+++ team/mmichelson/caller_id/res/res_sip_caller_id.c Mon Mar 25 15:01:38 2013
@@ -1,0 +1,122 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Mark Michelson <mmichelson at 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.
+ */
+
+/*** MODULEINFO
+	<depend>pjproject</depend>
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include <pjsip.h>
+#include <pjsip_ua.h>
+
+#include "asterisk/res_sip.h"
+#include "asterisk/res_sip_session.h"
+#include "asterisk/channel.h"
+#include "asterisk/module.h"
+
+static int get_id_from_from(struct pjsip_rx_data *rdata, struct ast_party_id *id)
+{
+	pjsip_fromto_hdr *from = pjsip_msg_find_hdr(rdata->msg_info.msg,
+			PJSIP_H_FROM, rdata->msg_info.msg->hdr.next);
+	pjsip_name_addr *id_name_addr;
+	pjsip_sip_uri *from_uri;
+	char cid_name[AST_CHANNEL_NAME];
+	char cid_num[AST_CHANNEL_NAME];
+
+	if (!from) {
+		/* This had better not happen */
+		return -1;
+	}
+
+	id_name_addr = (pjsip_name_addr *)from->uri;
+	from_uri = pjsip_uri_get_uri(id_name_addr);
+	ast_copy_pj_str(cid_name, &id_name_addr->display, sizeof(cid_name));
+	ast_copy_pj_str(cid_num, &from_uri->user, sizeof(cid_num));
+
+	id->name.str = ast_strdup(cid_name);
+	id->name.valid = 1;
+	id->number.str = ast_strdup(cid_num);
+	id->number.valid = 1;
+
+	if (!id->name.str || !id->number.str) {
+		return -1;
+	}
+
+	return 0;
+}
+
+static int caller_id_incoming_request(struct ast_sip_session *session,
+		struct pjsip_rx_data *rdata)
+{
+	get_id_from_from(rdata, &session->caller);
+	return 0;
+}
+
+static void set_from_header(pj_pool_t *pool, pjsip_fromto_hdr *from, struct ast_party_id *caller)
+{
+	pjsip_name_addr *id_name_addr = (pjsip_name_addr *)from->uri;
+	pjsip_sip_uri *from_uri = pjsip_uri_get_uri(id_name_addr);
+
+	pj_strdup2(pool, &id_name_addr->display, caller->name.str);
+	pj_strdup2(pool, &from_uri->user, caller->number.str);
+}
+
+static void caller_id_outgoing_request(struct ast_sip_session *session,
+		struct pjsip_tx_data *tdata)
+{
+	pjsip_fromto_hdr *from;
+	pjsip_dialog *dlg;
+
+	if (!session->channel) {
+		return;
+	}
+
+	from = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_FROM, tdata->msg->hdr.next);
+	dlg = session->inv_session->dlg;
+
+	set_from_header(tdata->pool, from, &ast_channel_caller(session->channel)->id);
+	set_from_header(dlg->pool, dlg->local.info,
+			&ast_channel_caller(session->channel)->id);
+}
+
+static struct ast_sip_session_supplement caller_id_supplement = {
+	.method = "INVITE",
+	.priority = 0,
+	.incoming_request = caller_id_incoming_request,
+	.outgoing_request = caller_id_outgoing_request,
+};
+
+static int load_module(void)
+{
+	ast_sip_session_register_supplement(&caller_id_supplement);
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+	ast_sip_session_unregister_supplement(&caller_id_supplement);
+	return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SIP Caller ID Support",
+		.load = load_module,
+		.unload = unload_module,
+		.load_pri = AST_MODPRI_APP_DEPEND,
+	       );

Propchange: team/mmichelson/caller_id/res/res_sip_caller_id.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/mmichelson/caller_id/res/res_sip_caller_id.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/mmichelson/caller_id/res/res_sip_caller_id.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/mmichelson/caller_id/res/res_sip_session.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/caller_id/res/res_sip_session.c?view=diff&rev=383752&r1=383751&r2=383752
==============================================================================
--- team/mmichelson/caller_id/res/res_sip_session.c (original)
+++ team/mmichelson/caller_id/res/res_sip_session.c Mon Mar 25 15:01:38 2013
@@ -448,8 +448,22 @@
 
 int ast_sip_session_register_supplement(struct ast_sip_session_supplement *supplement)
 {
+	struct ast_sip_session_supplement *iter;
+	int inserted = 0;
 	SCOPED_LOCK(lock, &session_supplements, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
-	AST_RWLIST_INSERT_TAIL(&session_supplements, supplement, next);
+
+	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&session_supplements, iter, next) {
+		if (iter->priority > supplement->priority) {
+			AST_RWLIST_INSERT_BEFORE_CURRENT(supplement, next);
+			inserted = 1;
+			continue;
+		}
+	}
+	AST_RWLIST_TRAVERSE_SAFE_END;
+
+	if (!inserted) {
+		AST_RWLIST_INSERT_TAIL(&session_supplements, supplement, next);
+	}
 	ast_module_ref(ast_module_info->self);
 	return 0;
 }
@@ -870,6 +884,7 @@
 	while ((delay = AST_LIST_REMOVE_HEAD(&session->delayed_requests, next))) {
 		ast_free(delay);
 	}
+	ast_party_id_free(&session->caller);
 	ao2_cleanup(session->endpoint);
 }
 
@@ -943,6 +958,7 @@
 	}
 	session->direct_media_cap = ast_format_cap_alloc_nolock();
 	AST_LIST_HEAD_INIT_NOLOCK(&session->delayed_requests);
+	ast_party_id_init(&session->caller);
 	ao2_ref(session, +1);
 	return session;
 }




More information about the asterisk-commits mailing list