3cd410daee6a466f3d2ab6ef45ded590ae718360
[asterisk/asterisk.git] / res / res_pjsip_dtmf_info.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Jason Parker <jparker@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_pjsip</depend>
22         <depend>res_pjsip_session</depend>
23         <support_level>core</support_level>
24  ***/
25
26 #include "asterisk.h"
27
28 #include <pjsip.h>
29 #include <pjsip_ua.h>
30
31 #include "asterisk/res_pjsip.h"
32 #include "asterisk/res_pjsip_session.h"
33 #include "asterisk/module.h"
34
35 static int dtmf_info_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
36 {
37         int res = 0;
38         pjsip_msg_body *body = rdata->msg_info.msg->body;
39
40         pjsip_tx_data *tdata;
41
42         char buf[body ? body->len : 0];
43         char *cur = buf;
44         char *line;
45
46         char event = '\0';
47         unsigned int duration = 0;
48
49         if (!body || !ast_sip_is_content_type(&body->content_type, "application", "dtmf-relay")) {
50                 return 0;
51         }
52
53         body->print_body(body, buf, body->len);
54
55         while ((line = strsep(&cur, "\r\n"))) {
56                 char *c;
57
58                 if (!(c = strchr(line, '='))) {
59                         continue;
60                 }
61                 *c++ = '\0';
62
63                 c = ast_skip_blanks(c);
64
65                 if (!strcasecmp(line, "signal")) {
66                         if (c[0] == '!' || c[0] == '*' || c[0] == '#' ||
67                             ('0' <= c[0] && c[0] <= '9') ||
68                             ('A' <= c[0] && c[0] <= 'D') ||
69                             ('a' <= c[0] && c[0] <= 'd')) {
70                                 event = c[0];
71                         } else {
72                                 ast_log(LOG_ERROR, "Invalid DTMF event signal in INFO message.\n");
73                                 res = -1;
74                                 break;
75                         }
76                 } else if (!strcasecmp(line, "duration")) {
77                         sscanf(c, "%30u", &duration);
78                 }
79         }
80
81         if (!duration) {
82                 duration = 100;
83         }
84
85         if (event == '!') {
86                 struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_FLASH, } };
87
88                 ast_queue_frame(session->channel, &f);
89         } else if (event != '\0') {
90                 struct ast_frame f = { AST_FRAME_DTMF, };
91                 f.len = duration;
92                 f.subclass.integer = event;
93
94                 ast_queue_frame(session->channel, &f);
95         } else {
96                 res = -1;
97         }
98
99         if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, !res ? 200 : 500, NULL, &tdata) == PJ_SUCCESS) {
100                 struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
101
102                 pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata);
103         }
104
105         return res;
106 }
107
108 static struct ast_sip_session_supplement dtmf_info_supplement = {
109         .method = "INFO",
110         .incoming_request = dtmf_info_incoming_request,
111 };
112
113 static int load_module(void)
114 {
115         ast_sip_session_register_supplement(&dtmf_info_supplement);
116         return AST_MODULE_LOAD_SUCCESS;
117 }
118
119 static int unload_module(void)
120 {
121         ast_sip_session_unregister_supplement(&dtmf_info_supplement);
122         return 0;
123 }
124
125 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP DTMF INFO Support",
126                 .load = load_module,
127                 .unload = unload_module,
128                 .load_pri = AST_MODPRI_APP_DEPEND,
129                );