res_pjsip_dtmf_info.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2013, Digium, Inc.
00005  *
00006  * Jason Parker <jparker@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*** MODULEINFO
00020    <depend>pjproject</depend>
00021    <depend>res_pjsip</depend>
00022    <depend>res_pjsip_session</depend>
00023    <support_level>core</support_level>
00024  ***/
00025 
00026 #include "asterisk.h"
00027 
00028 #include <pjsip.h>
00029 #include <pjsip_ua.h>
00030 
00031 #include "asterisk/res_pjsip.h"
00032 #include "asterisk/res_pjsip_session.h"
00033 #include "asterisk/module.h"
00034 
00035 static int is_media_type(pjsip_rx_data *rdata, char *subtype)
00036 {
00037    return rdata->msg_info.ctype
00038       && !pj_strcmp2(&rdata->msg_info.ctype->media.type, "application")
00039       && !pj_strcmp2(&rdata->msg_info.ctype->media.subtype, subtype);
00040 }
00041 
00042 static void send_response(struct ast_sip_session *session,
00043            struct pjsip_rx_data *rdata, int code)
00044 {
00045    pjsip_tx_data *tdata;
00046    pjsip_dialog *dlg = session->inv_session->dlg;
00047 
00048    if (pjsip_dlg_create_response(dlg, rdata, code,
00049                   NULL, &tdata) == PJ_SUCCESS) {
00050       struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
00051       pjsip_dlg_send_response(dlg, tsx, tdata);
00052    }
00053 }
00054 
00055 static char get_event(const char *c)
00056 {
00057    unsigned int event;
00058 
00059    if (*c == '!' || *c == '*' || *c == '#' ||
00060        ('A' <= *c && *c <= 'D') ||
00061        ('a' <= *c && *c <= 'd')) {
00062       return *c;
00063    }
00064 
00065    if ((sscanf(c, "%30u", &event) != 1) || event > 16) {
00066       return '\0';
00067    }
00068 
00069    if (event < 10) {
00070       return *c;
00071    }
00072 
00073    switch (event) {
00074    case 10: return '*';
00075    case 11: return '#';
00076    case 16: return '!';
00077    }
00078 
00079    return 'A' + (event - 12);
00080 }
00081 
00082 static int dtmf_info_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
00083 {
00084    pjsip_msg_body *body = rdata->msg_info.msg->body;
00085    char buf[body ? body->len : 0];
00086    char *cur = buf;
00087    char *line;
00088 
00089    char event = '\0';
00090    unsigned int duration = 100;
00091 
00092    char is_dtmf = is_media_type(rdata, "dtmf");
00093 
00094    if (!is_dtmf && !is_media_type(rdata, "dtmf-relay")) {
00095       return 0;
00096    }
00097 
00098    if (!body || !body->len) {
00099       /* need to return 200 OK on empty body */
00100       send_response(session, rdata, 200);
00101       return 0;
00102    }
00103 
00104    body->print_body(body, buf, body->len);
00105 
00106    if (is_dtmf) {
00107       /* directly use what is in the message body */
00108       event = get_event(cur);
00109    } else { /* content type = application/dtmf-relay */
00110       while ((line = strsep(&cur, "\r\n"))) {
00111          char *c;
00112 
00113          if (!(c = strchr(line, '='))) {
00114             continue;
00115          }
00116 
00117          *c++ = '\0';
00118          c = ast_skip_blanks(c);
00119 
00120          if (!strcasecmp(line, "signal")) {
00121             if (!(event = get_event(c))) {
00122                break;
00123             }
00124          } else if (!strcasecmp(line, "duration")) {
00125             sscanf(c, "%30u", &duration);
00126          }
00127       }
00128    }
00129 
00130    if (event == '!') {
00131       struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_FLASH, } };
00132       ast_queue_frame(session->channel, &f);
00133    } else if (event != '\0') {
00134       struct ast_frame f = { AST_FRAME_DTMF, };
00135       f.len = duration;
00136       f.subclass.integer = event;
00137       ast_queue_frame(session->channel, &f);
00138    } else {
00139       ast_log(LOG_ERROR, "Invalid DTMF event signal in INFO message.\n");
00140    }
00141 
00142    send_response(session, rdata, event ? 200 : 500);
00143    return event ? 0 : -1;
00144 }
00145 
00146 static struct ast_sip_session_supplement dtmf_info_supplement = {
00147    .method = "INFO",
00148    .incoming_request = dtmf_info_incoming_request,
00149 };
00150 
00151 static int load_module(void)
00152 {
00153    CHECK_PJSIP_SESSION_MODULE_LOADED();
00154 
00155    ast_sip_session_register_supplement(&dtmf_info_supplement);
00156    return AST_MODULE_LOAD_SUCCESS;
00157 }
00158 
00159 static int unload_module(void)
00160 {
00161    ast_sip_session_unregister_supplement(&dtmf_info_supplement);
00162    return 0;
00163 }
00164 
00165 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP DTMF INFO Support",
00166       .support_level = AST_MODULE_SUPPORT_CORE,
00167       .load = load_module,
00168       .unload = unload_module,
00169       .load_pri = AST_MODPRI_APP_DEPEND,
00170           );

Generated on Thu Apr 16 06:27:48 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6