res_pjsip_dtmf_info.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 : 0];
  72. char *cur = buf;
  73. char *line;
  74. char event = '\0';
  75. unsigned int duration = 100;
  76. char is_dtmf = is_media_type(rdata, "dtmf");
  77. if (!is_dtmf && !is_media_type(rdata, "dtmf-relay")) {
  78. return 0;
  79. }
  80. if (!body || !body->len) {
  81. /* need to return 200 OK on empty body */
  82. send_response(session, rdata, 200);
  83. return 0;
  84. }
  85. body->print_body(body, buf, body->len);
  86. if (is_dtmf) {
  87. /* directly use what is in the message body */
  88. event = get_event(cur);
  89. } else { /* content type = application/dtmf-relay */
  90. while ((line = strsep(&cur, "\r\n"))) {
  91. char *c;
  92. if (!(c = strchr(line, '='))) {
  93. continue;
  94. }
  95. *c++ = '\0';
  96. c = ast_skip_blanks(c);
  97. if (!strcasecmp(line, "signal")) {
  98. if (!(event = get_event(c))) {
  99. break;
  100. }
  101. } else if (!strcasecmp(line, "duration")) {
  102. sscanf(c, "%30u", &duration);
  103. }
  104. }
  105. }
  106. if (event == '!') {
  107. struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_FLASH, } };
  108. ast_queue_frame(session->channel, &f);
  109. } else if (event != '\0') {
  110. struct ast_frame f = { AST_FRAME_DTMF, };
  111. f.len = duration;
  112. f.subclass.integer = event;
  113. ast_queue_frame(session->channel, &f);
  114. } else {
  115. ast_log(LOG_ERROR, "Invalid DTMF event signal in INFO message.\n");
  116. }
  117. send_response(session, rdata, event ? 200 : 500);
  118. return event ? 0 : -1;
  119. }
  120. static struct ast_sip_session_supplement dtmf_info_supplement = {
  121. .method = "INFO",
  122. .incoming_request = dtmf_info_incoming_request,
  123. };
  124. static int load_module(void)
  125. {
  126. CHECK_PJSIP_SESSION_MODULE_LOADED();
  127. ast_sip_session_register_supplement(&dtmf_info_supplement);
  128. return AST_MODULE_LOAD_SUCCESS;
  129. }
  130. static int unload_module(void)
  131. {
  132. ast_sip_session_unregister_supplement(&dtmf_info_supplement);
  133. return 0;
  134. }
  135. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP DTMF INFO Support",
  136. .support_level = AST_MODULE_SUPPORT_CORE,
  137. .load = load_module,
  138. .unload = unload_module,
  139. .load_pri = AST_MODPRI_APP_DEPEND,
  140. );