res_pjsip_dtmf_info.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /*** MODULEINFO
  19. <depend>pjproject</depend>
  20. <depend>res_pjsip</depend>
  21. <depend>res_pjsip_session</depend>
  22. <support_level>core</support_level>
  23. ***/
  24. #include "asterisk.h"
  25. #include <pjsip.h>
  26. #include <pjsip_ua.h>
  27. #include "asterisk/res_pjsip.h"
  28. #include "asterisk/res_pjsip_session.h"
  29. #include "asterisk/module.h"
  30. static int is_media_type(pjsip_rx_data *rdata, char *subtype)
  31. {
  32. return rdata->msg_info.ctype
  33. && !pj_strcmp2(&rdata->msg_info.ctype->media.type, "application")
  34. && !pj_strcmp2(&rdata->msg_info.ctype->media.subtype, subtype);
  35. }
  36. static void send_response(struct ast_sip_session *session,
  37. struct pjsip_rx_data *rdata, int code)
  38. {
  39. pjsip_tx_data *tdata;
  40. pjsip_dialog *dlg = session->inv_session->dlg;
  41. if (pjsip_dlg_create_response(dlg, rdata, code,
  42. NULL, &tdata) == PJ_SUCCESS) {
  43. struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
  44. pjsip_dlg_send_response(dlg, tsx, tdata);
  45. }
  46. }
  47. static char get_event(const char *c)
  48. {
  49. unsigned int event;
  50. if (*c == '!' || *c == '*' || *c == '#' ||
  51. ('A' <= *c && *c <= 'D') ||
  52. ('a' <= *c && *c <= 'd')) {
  53. return *c;
  54. }
  55. if ((sscanf(c, "%30u", &event) != 1) || event > 16) {
  56. return '\0';
  57. }
  58. if (event < 10) {
  59. return *c;
  60. }
  61. switch (event) {
  62. case 10: return '*';
  63. case 11: return '#';
  64. case 16: return '!';
  65. }
  66. return 'A' + (event - 12);
  67. }
  68. static int dtmf_info_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  69. {
  70. pjsip_msg_body *body = rdata->msg_info.msg->body;
  71. char buf[body ? body->len + 1 : 1];
  72. char *cur = buf;
  73. char *line;
  74. char event = '\0';
  75. unsigned int duration = 100;
  76. char is_dtmf;
  77. int res;
  78. if (!session->channel) {
  79. return 0;
  80. }
  81. is_dtmf = is_media_type(rdata, "dtmf");
  82. if (!is_dtmf && !is_media_type(rdata, "dtmf-relay")) {
  83. return 0;
  84. }
  85. if (!body || !body->len) {
  86. /* need to return 200 OK on empty body */
  87. send_response(session, rdata, 200);
  88. return 1;
  89. }
  90. res = body->print_body(body, buf, body->len);
  91. if (res < 0) {
  92. send_response(session, rdata, 500);
  93. return 1;
  94. }
  95. buf[res] = '\0';
  96. if (is_dtmf) {
  97. /* directly use what is in the message body */
  98. event = get_event(cur);
  99. } else { /* content type = application/dtmf-relay */
  100. while ((line = strsep(&cur, "\r\n"))) {
  101. char *c;
  102. if (!(c = strchr(line, '='))) {
  103. continue;
  104. }
  105. *c++ = '\0';
  106. c = ast_skip_blanks(c);
  107. if (!strcasecmp(line, "signal")) {
  108. if (!(event = get_event(c))) {
  109. break;
  110. }
  111. } else if (!strcasecmp(line, "duration")) {
  112. sscanf(c, "%30u", &duration);
  113. }
  114. }
  115. }
  116. if (event == '!') {
  117. struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_FLASH, } };
  118. ast_queue_frame(session->channel, &f);
  119. } else if (event != '\0') {
  120. struct ast_frame f = { AST_FRAME_DTMF, };
  121. f.len = duration;
  122. f.subclass.integer = event;
  123. ast_queue_frame(session->channel, &f);
  124. } else {
  125. ast_log(LOG_ERROR, "Invalid DTMF event signal in INFO message.\n");
  126. }
  127. send_response(session, rdata, event ? 200 : 500);
  128. return 1;
  129. }
  130. static struct ast_sip_session_supplement dtmf_info_supplement = {
  131. .method = "INFO",
  132. .priority = AST_SIP_SUPPLEMENT_PRIORITY_FIRST,
  133. .incoming_request = dtmf_info_incoming_request,
  134. };
  135. static int load_module(void)
  136. {
  137. CHECK_PJSIP_SESSION_MODULE_LOADED();
  138. ast_sip_session_register_supplement(&dtmf_info_supplement);
  139. return AST_MODULE_LOAD_SUCCESS;
  140. }
  141. static int unload_module(void)
  142. {
  143. ast_sip_session_unregister_supplement(&dtmf_info_supplement);
  144. return 0;
  145. }
  146. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP DTMF INFO Support",
  147. .support_level = AST_MODULE_SUPPORT_CORE,
  148. .load = load_module,
  149. .unload = unload_module,
  150. .load_pri = AST_MODPRI_APP_DEPEND,
  151. );