|
| 1 | +/* |
| 2 | + * Asterisk -- An open source telephony toolkit. |
| 3 | + * |
| 4 | + * Copyright (C) 2021, Naveen Albert |
| 5 | + * |
| 6 | + * Naveen Albert <asterisk@phreaknet.org> |
| 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 | +/*! \file |
| 20 | + * |
| 21 | + * \brief App to send MF digits |
| 22 | + * |
| 23 | + * \author Naveen Albert <asterisk@phreaknet.org> |
| 24 | + * |
| 25 | + * \ingroup applications |
| 26 | + */ |
| 27 | + |
| 28 | +/*** MODULEINFO |
| 29 | + <support_level>extended</support_level> |
| 30 | + ***/ |
| 31 | + |
| 32 | +#include "asterisk.h" |
| 33 | + |
| 34 | +#include "asterisk/pbx.h" |
| 35 | +#include "asterisk/module.h" |
| 36 | +#include "asterisk/app.h" |
| 37 | +#include "asterisk/channel.h" |
| 38 | +#include "asterisk/indications.h" |
| 39 | + |
| 40 | +/*** DOCUMENTATION |
| 41 | + <application name="SendMF" language="en_US"> |
| 42 | + <synopsis> |
| 43 | + Sends arbitrary MF digits |
| 44 | + </synopsis> |
| 45 | + <syntax> |
| 46 | + <parameter name="digits" required="true"> |
| 47 | + <para>List of digits 0-9,*#ABC to send; also f or F for a flash-hook |
| 48 | + if the channel supports flash-hook, and w or W for a wink if the channel |
| 49 | + supports wink.</para> |
| 50 | + <para>Key pulse and start digits are not included automatically. |
| 51 | + * is used for KP, # for ST, A for STP, B for ST2P, and C for ST3P.</para> |
| 52 | + </parameter> |
| 53 | + <parameter name="timeout_ms" required="false"> |
| 54 | + <para>Amount of time to wait in ms between tones. (defaults to 50ms).</para> |
| 55 | + </parameter> |
| 56 | + <parameter name="duration_ms" required="false"> |
| 57 | + <para>Duration of each numeric digit (defaults to 55ms).</para> |
| 58 | + </parameter> |
| 59 | + <parameter name="duration_ms_kp" required="false"> |
| 60 | + <para>Duration of KP digits (defaults to 120ms).</para> |
| 61 | + </parameter> |
| 62 | + <parameter name="duration_ms_st" required="false"> |
| 63 | + <para>Duration of ST, STP, ST2P, and ST3P digits (defaults to 65ms).</para> |
| 64 | + </parameter> |
| 65 | + <parameter name="channel" required="false"> |
| 66 | + <para>Channel where digits will be played</para> |
| 67 | + </parameter> |
| 68 | + </syntax> |
| 69 | + <description> |
| 70 | + <para>It will send all digits or terminate if it encounters an error.</para> |
| 71 | + </description> |
| 72 | + <see-also> |
| 73 | + <ref type="application">SendDTMF</ref> |
| 74 | + </see-also> |
| 75 | + </application> |
| 76 | + <manager name="PlayMF" language="en_US"> |
| 77 | + <synopsis> |
| 78 | + Play MF signal on a specific channel. |
| 79 | + </synopsis> |
| 80 | + <syntax> |
| 81 | + <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" /> |
| 82 | + <parameter name="Channel" required="true"> |
| 83 | + <para>Channel name to send digit to.</para> |
| 84 | + </parameter> |
| 85 | + <parameter name="Digit" required="true"> |
| 86 | + <para>The MF digit to play.</para> |
| 87 | + </parameter> |
| 88 | + <parameter name="Duration" required="false"> |
| 89 | + <para>The duration, in milliseconds, of the digit to be played.</para> |
| 90 | + </parameter> |
| 91 | + </syntax> |
| 92 | + <description> |
| 93 | + <para>Plays an MF digit on the specified channel.</para> |
| 94 | + </description> |
| 95 | + </manager> |
| 96 | + ***/ |
| 97 | + |
| 98 | +static const char sendmf_name[] = "SendMF"; |
| 99 | + |
| 100 | +#define DEFAULT_EMULATE_MF_DURATION 35 |
| 101 | +#define MF_BETWEEN_MS 50 |
| 102 | +#define MF_DURATION 55 |
| 103 | +#define MF_KP_DURATION 120 |
| 104 | +#define MF_ST_DURATION 65 |
| 105 | + |
| 106 | +static int senddigit_mf_begin(struct ast_channel *chan, char digit) |
| 107 | +{ |
| 108 | + static const char * const mf_tones[] = { |
| 109 | + "1300+1500", /* 0 */ |
| 110 | + "700+900", /* 1 */ |
| 111 | + "700+1100", /* 2 */ |
| 112 | + "900+1100", /* 3 */ |
| 113 | + "700+1300", /* 4 */ |
| 114 | + "900+1300", /* 5 */ |
| 115 | + "1100+1300", /* 6 */ |
| 116 | + "700+1500", /* 7 */ |
| 117 | + "900+1500", /* 8 */ |
| 118 | + "1100+1500", /* 9 */ |
| 119 | + "1100+1700", /* * (KP) */ |
| 120 | + "1500+1700", /* # (ST) */ |
| 121 | + "900+1700", /* A (STP) */ |
| 122 | + "1300+1700", /* B (ST2P) */ |
| 123 | + "700+1700" /* C (ST3P) */ |
| 124 | + }; |
| 125 | + |
| 126 | + if (digit >= '0' && digit <='9') { |
| 127 | + ast_playtones_start(chan, 0, mf_tones[digit-'0'], 0); |
| 128 | + } else if (digit == '*') { |
| 129 | + ast_playtones_start(chan, 0, mf_tones[10], 0); |
| 130 | + } else if (digit == '#') { |
| 131 | + ast_playtones_start(chan, 0, mf_tones[11], 0); |
| 132 | + } else if (digit == 'A') { |
| 133 | + ast_playtones_start(chan, 0, mf_tones[12], 0); |
| 134 | + } else if (digit == 'B') { |
| 135 | + ast_playtones_start(chan, 0, mf_tones[13], 0); |
| 136 | + } else if (digit == 'C') { |
| 137 | + ast_playtones_start(chan, 0, mf_tones[14], 0); |
| 138 | + } else { |
| 139 | + /* not handled */ |
| 140 | + ast_log(LOG_WARNING, "Unable to generate MF tone '%c' for '%s'\n", digit, ast_channel_name(chan)); |
| 141 | + } |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | +static int senddigit_mf_end(struct ast_channel *chan) |
| 147 | +{ |
| 148 | + if (ast_channel_generator(chan)) { |
| 149 | + ast_playtones_stop(chan); |
| 150 | + return 0; |
| 151 | + } |
| 152 | + return -1; |
| 153 | +} |
| 154 | + |
| 155 | +static int mysleep(struct ast_channel *chan, int ms, int is_external) |
| 156 | +{ |
| 157 | + return is_external ? usleep(ms * 1000) : ast_safe_sleep(chan, ms); |
| 158 | +} |
| 159 | + |
| 160 | +static int senddigit_mf(struct ast_channel *chan, char digit, unsigned int duration, |
| 161 | + unsigned int durationkp, unsigned int durationst, int is_external) |
| 162 | +{ |
| 163 | + if (duration < DEFAULT_EMULATE_MF_DURATION) { |
| 164 | + duration = DEFAULT_EMULATE_MF_DURATION; |
| 165 | + } |
| 166 | + if (ast_channel_tech(chan)->send_digit_begin) { |
| 167 | + if (digit == '*') { |
| 168 | + duration = durationkp; |
| 169 | + } else if (digit == '#' || digit == 'A' || digit == 'B' || digit == 'C') { |
| 170 | + duration = durationst; |
| 171 | + } |
| 172 | + senddigit_mf_begin(chan, digit); |
| 173 | + mysleep(chan, duration, is_external); |
| 174 | + } |
| 175 | + return senddigit_mf_end(chan); |
| 176 | +} |
| 177 | + |
| 178 | +static int mf_stream(struct ast_channel *chan, const char *digits, int between, unsigned int duration, |
| 179 | + unsigned int durationkp, unsigned int durationst, int is_external) |
| 180 | +{ |
| 181 | + const char *ptr; |
| 182 | + int res; |
| 183 | + struct ast_silence_generator *silgen = NULL; |
| 184 | + |
| 185 | + if (!between) { |
| 186 | + between = 100; |
| 187 | + } |
| 188 | + |
| 189 | + /* Need a quiet time before sending digits. */ |
| 190 | + if (ast_opt_transmit_silence) { |
| 191 | + silgen = ast_channel_start_silence_generator(chan); |
| 192 | + } |
| 193 | + res = mysleep(chan, 100, is_external); |
| 194 | + if (res) { |
| 195 | + goto mf_stream_cleanup; |
| 196 | + } |
| 197 | + |
| 198 | + for (ptr = digits; *ptr; ptr++) { |
| 199 | + if (strchr("0123456789*#ABCwWfF", *ptr)) { |
| 200 | + if (*ptr == 'f' || *ptr == 'F') { |
| 201 | + /* ignore return values if not supported by channel */ |
| 202 | + ast_indicate(chan, AST_CONTROL_FLASH); |
| 203 | + } else if (*ptr == 'w' || *ptr == 'W') { |
| 204 | + /* ignore return values if not supported by channel */ |
| 205 | + ast_indicate(chan, AST_CONTROL_WINK); |
| 206 | + } else { |
| 207 | + /* Character represents valid MF */ |
| 208 | + senddigit_mf(chan, *ptr, duration, durationkp, durationst, is_external); |
| 209 | + } |
| 210 | + /* pause between digits */ |
| 211 | + /* The DSP code in Asterisk does not currently properly receive repeated tones |
| 212 | + if no audio is sent in the middle. Simply sending audio (even 0 Hz) |
| 213 | + works around this limitation and guarantees the correct behavior. |
| 214 | + */ |
| 215 | + ast_playtones_start(chan, 0, "0", 0); |
| 216 | + res = mysleep(chan, between, is_external); |
| 217 | + senddigit_mf_end(chan); |
| 218 | + if (res) { |
| 219 | + break; |
| 220 | + } |
| 221 | + } else { |
| 222 | + ast_log(LOG_WARNING, "Illegal MF character '%c' in string. (0-9*#ABCwWfF allowed)\n", *ptr); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | +mf_stream_cleanup: |
| 227 | + if (silgen) { |
| 228 | + ast_channel_stop_silence_generator(chan, silgen); |
| 229 | + } |
| 230 | + |
| 231 | + return res; |
| 232 | +} |
| 233 | + |
| 234 | +static int sendmf_exec(struct ast_channel *chan, const char *vdata) |
| 235 | +{ |
| 236 | + int res; |
| 237 | + char *data; |
| 238 | + int dinterval = 0, duration = 0, durationkp = 0, durationst = 0; |
| 239 | + struct ast_channel *chan_found = NULL; |
| 240 | + struct ast_channel *chan_dest = chan; |
| 241 | + struct ast_channel *chan_autoservice = NULL; |
| 242 | + AST_DECLARE_APP_ARGS(args, |
| 243 | + AST_APP_ARG(digits); |
| 244 | + AST_APP_ARG(dinterval); |
| 245 | + AST_APP_ARG(duration); |
| 246 | + AST_APP_ARG(durationkp); |
| 247 | + AST_APP_ARG(durationst); |
| 248 | + AST_APP_ARG(channel); |
| 249 | + ); |
| 250 | + |
| 251 | + if (ast_strlen_zero(vdata)) { |
| 252 | + ast_log(LOG_WARNING, "SendMF requires an argument\n"); |
| 253 | + return 0; |
| 254 | + } |
| 255 | + |
| 256 | + data = ast_strdupa(vdata); |
| 257 | + AST_STANDARD_APP_ARGS(args, data); |
| 258 | + |
| 259 | + if (ast_strlen_zero(args.digits)) { |
| 260 | + ast_log(LOG_WARNING, "The digits argument is required (0-9,*#ABC,wf)\n"); |
| 261 | + return 0; |
| 262 | + } |
| 263 | + if (!ast_strlen_zero(args.dinterval)) { |
| 264 | + ast_app_parse_timelen(args.dinterval, &dinterval, TIMELEN_MILLISECONDS); |
| 265 | + } |
| 266 | + if (!ast_strlen_zero(args.duration)) { |
| 267 | + ast_app_parse_timelen(args.duration, &duration, TIMELEN_MILLISECONDS); |
| 268 | + } |
| 269 | + if (!ast_strlen_zero(args.durationkp)) { |
| 270 | + ast_app_parse_timelen(args.durationkp, &durationkp, TIMELEN_MILLISECONDS); |
| 271 | + } |
| 272 | + if (!ast_strlen_zero(args.durationst)) { |
| 273 | + ast_app_parse_timelen(args.durationst, &durationst, TIMELEN_MILLISECONDS); |
| 274 | + } |
| 275 | + if (!ast_strlen_zero(args.channel)) { |
| 276 | + chan_found = ast_channel_get_by_name(args.channel); |
| 277 | + if (!chan_found) { |
| 278 | + ast_log(LOG_WARNING, "No such channel: %s\n", args.channel); |
| 279 | + return 0; |
| 280 | + } |
| 281 | + chan_dest = chan_found; |
| 282 | + if (chan_found != chan) { |
| 283 | + chan_autoservice = chan; |
| 284 | + } |
| 285 | + } |
| 286 | + if (chan_autoservice && ast_autoservice_start(chan_autoservice)) { |
| 287 | + ast_channel_cleanup(chan_found); |
| 288 | + return -1; |
| 289 | + } |
| 290 | + res = mf_stream(chan_dest, args.digits, dinterval <= 0 ? MF_BETWEEN_MS : dinterval, |
| 291 | + duration <= 0 ? MF_DURATION : duration, durationkp <= 0 ? MF_KP_DURATION : durationkp, |
| 292 | + durationst <= 0 ? MF_ST_DURATION : durationst, 0); |
| 293 | + if (chan_autoservice && ast_autoservice_stop(chan_autoservice)) { |
| 294 | + res = -1; |
| 295 | + } |
| 296 | + ast_channel_cleanup(chan_found); |
| 297 | + |
| 298 | + return chan_autoservice ? 0 : res; |
| 299 | +} |
| 300 | + |
| 301 | +static int manager_play_mf(struct mansession *s, const struct message *m) |
| 302 | +{ |
| 303 | + const char *channel = astman_get_header(m, "Channel"); |
| 304 | + const char *digit = astman_get_header(m, "Digit"); |
| 305 | + const char *duration = astman_get_header(m, "Duration"); |
| 306 | + struct ast_channel *chan; |
| 307 | + unsigned int duration_ms = MF_DURATION; |
| 308 | + |
| 309 | + if (!(chan = ast_channel_get_by_name(channel))) { |
| 310 | + astman_send_error(s, m, "Channel not found"); |
| 311 | + return 0; |
| 312 | + } |
| 313 | + |
| 314 | + if (ast_strlen_zero(digit)) { |
| 315 | + astman_send_error(s, m, "No digit specified"); |
| 316 | + chan = ast_channel_unref(chan); |
| 317 | + return 0; |
| 318 | + } |
| 319 | + |
| 320 | + /* Override default duration with KP or ST-specific default durations */ |
| 321 | + if (!strcmp(digit, "*")) |
| 322 | + duration_ms = MF_KP_DURATION; |
| 323 | + |
| 324 | + if (!strcmp(digit, "#") || !strcmp(digit, "A") || !strcmp(digit, "B") || !strcmp(digit, "C")) |
| 325 | + duration_ms = MF_ST_DURATION; |
| 326 | + |
| 327 | + if (!ast_strlen_zero(duration) && (sscanf(duration, "%30u", &duration_ms) != 1)) { |
| 328 | + astman_send_error(s, m, "Could not convert Duration parameter"); |
| 329 | + chan = ast_channel_unref(chan); |
| 330 | + return 0; |
| 331 | + } |
| 332 | + |
| 333 | + senddigit_mf(chan, *digit, duration_ms, duration_ms, duration_ms, 1); |
| 334 | + chan = ast_channel_unref(chan); |
| 335 | + |
| 336 | + astman_send_ack(s, m, "MF successfully queued"); |
| 337 | + |
| 338 | + return 0; |
| 339 | +} |
| 340 | + |
| 341 | +static int unload_module(void) |
| 342 | +{ |
| 343 | + int res; |
| 344 | + |
| 345 | + res = ast_unregister_application(sendmf_name); |
| 346 | + res |= ast_manager_unregister("PlayMF"); |
| 347 | + |
| 348 | + return res; |
| 349 | +} |
| 350 | + |
| 351 | +static int load_module(void) |
| 352 | +{ |
| 353 | + int res; |
| 354 | + |
| 355 | + res = ast_manager_register_xml("PlayMF", EVENT_FLAG_CALL, manager_play_mf); |
| 356 | + res |= ast_register_application_xml(sendmf_name, sendmf_exec); |
| 357 | + |
| 358 | + return res; |
| 359 | +} |
| 360 | + |
| 361 | +AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Send MF digits Application"); |
0 commit comments