Asterisk - The Open Source Telephony Project  GIT-master-0190e70
res_pjsip_dtmf_info.c
Go to the documentation of this file.
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"
33 #include "asterisk/module.h"
34 
35 static int is_media_type(pjsip_rx_data *rdata, char *subtype)
36 {
37  return rdata->msg_info.ctype
38  && !pj_strcmp2(&rdata->msg_info.ctype->media.type, "application")
39  && !pj_strcmp2(&rdata->msg_info.ctype->media.subtype, subtype);
40 }
41 
43  struct pjsip_rx_data *rdata, int code)
44 {
45  pjsip_tx_data *tdata;
46  pjsip_dialog *dlg = session->inv_session->dlg;
47 
48  if (pjsip_dlg_create_response(dlg, rdata, code,
49  NULL, &tdata) == PJ_SUCCESS) {
50  struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
51  pjsip_dlg_send_response(dlg, tsx, tdata);
52  }
53 }
54 
55 static char get_event(const char *c)
56 {
57  unsigned int event;
58 
59  if (*c == '!' || *c == '*' || *c == '#' ||
60  ('A' <= *c && *c <= 'D') ||
61  ('a' <= *c && *c <= 'd')) {
62  return *c;
63  }
64 
65  if ((sscanf(c, "%30u", &event) != 1) || event > 16) {
66  return '\0';
67  }
68 
69  if (event < 10) {
70  return *c;
71  }
72 
73  switch (event) {
74  case 10: return '*';
75  case 11: return '#';
76  case 16: return '!';
77  }
78 
79  return 'A' + (event - 12);
80 }
81 
82 static int dtmf_info_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
83 {
84  pjsip_msg_body *body = rdata->msg_info.msg->body;
85  char buf[body ? body->len + 1 : 1];
86  char *cur = buf;
87  char *line;
88  char event = '\0';
89  unsigned int duration = 100;
90  char is_dtmf;
91  int res;
92 
93  if (!session->channel) {
94  return 0;
95  }
96 
97  is_dtmf = is_media_type(rdata, "dtmf");
98 
99  if (!is_dtmf && !is_media_type(rdata, "dtmf-relay")) {
100  return 0;
101  }
102 
103  if (!body || !body->len) {
104  /* need to return 200 OK on empty body */
105  send_response(session, rdata, 200);
106  return 1;
107  }
108 
109  res = body->print_body(body, buf, body->len);
110  if (res < 0) {
111  send_response(session, rdata, 500);
112  return 1;
113  }
114  buf[res] = '\0';
115 
116  if (is_dtmf) {
117  /* directly use what is in the message body */
118  event = get_event(cur);
119  } else { /* content type = application/dtmf-relay */
120  while ((line = strsep(&cur, "\r\n"))) {
121  char *c;
122 
123  if (!(c = strchr(line, '='))) {
124  continue;
125  }
126 
127  *c++ = '\0';
128  c = ast_skip_blanks(c);
129 
130  if (!strcasecmp(line, "signal")) {
131  if (!(event = get_event(c))) {
132  break;
133  }
134  } else if (!strcasecmp(line, "duration")) {
135  sscanf(c, "%30u", &duration);
136  }
137  }
138  }
139 
140  if (event == '!') {
141  struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_FLASH, } };
142  ast_queue_frame(session->channel, &f);
143  } else if (event != '\0') {
144  struct ast_frame f = { AST_FRAME_DTMF, };
145  f.len = duration;
146  f.subclass.integer = event;
147  ast_queue_frame(session->channel, &f);
148  } else {
149  ast_log(LOG_ERROR, "Invalid DTMF event signal in INFO message.\n");
150  }
151 
152  send_response(session, rdata, event ? 200 : 500);
153  return 1;
154 }
155 
157  .method = "INFO",
159  .incoming_request = dtmf_info_incoming_request,
160 };
161 
162 static int load_module(void)
163 {
164  ast_sip_session_register_supplement(&dtmf_info_supplement);
166 }
167 
168 static int unload_module(void)
169 {
170  ast_sip_session_unregister_supplement(&dtmf_info_supplement);
171  return 0;
172 }
173 
174 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP DTMF INFO Support",
175  .support_level = AST_MODULE_SUPPORT_CORE,
176  .load = load_module,
177  .unload = unload_module,
178  .load_pri = AST_MODPRI_APP_DEPEND,
179  .requires = "res_pjsip,res_pjsip_session",
180 );
Asterisk main include file. File version handling, generic pbx functions.
static int load_module(void)
static char get_event(const char *c)
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static struct ast_sip_session_supplement dtmf_info_supplement
Definition: astman.c:222
static struct test_val c
void ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement)
Unregister a an supplement to SIP session processing.
Definition: pjsip_session.c:63
#define NULL
Definition: resample.c:96
#define AST_FRAME_DTMF
struct pjsip_inv_session * inv_session
A structure describing a SIP session.
struct ast_frame_subclass subclass
#define ast_log
Definition: astobj2.c:42
static struct ast_mansession session
struct ast_channel * channel
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *f)
Queue one or more frames to a channel&#39;s frame queue.
Definition: channel.c:1135
static int is_media_type(pjsip_rx_data *rdata, char *subtype)
#define LOG_ERROR
Definition: logger.h:285
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition: strings.h:157
static int dtmf_info_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
A supplement to SIP message processing.
char * strsep(char **str, const char *delims)
static int unload_module(void)
Data structure associated with a single frame of data.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
static void send_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata, int code)
#define ast_sip_session_register_supplement(supplement)