| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/file.h" |
| 35 |
#include "asterisk/channel.h" |
| 36 |
#include "asterisk/pbx.h" |
| 37 |
#include "asterisk/module.h" |
| 38 |
#include "asterisk/lock.h" |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
static char *app = "WaitForRing"; |
| 60 |
|
| 61 |
static int waitforring_exec(struct ast_channel *chan, const char *data) |
| 62 |
{ |
| 63 |
struct ast_frame *f; |
| 64 |
struct ast_silence_generator *silgen = NULL; |
| 65 |
int res = 0; |
| 66 |
double s; |
| 67 |
int timeout_ms; |
| 68 |
int ms; |
| 69 |
struct timeval start = ast_tvnow(); |
| 70 |
|
| 71 |
if (!data || (sscanf(data, "%30lg", &s) != 1)) { |
| 72 |
ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n"); |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
if (s < 0.0) { |
| 77 |
ast_log(LOG_WARNING, "Invalid timeout provided for WaitForRing (%lg)\n", s); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
if (ast_opt_transmit_silence) { |
| 82 |
silgen = ast_channel_start_silence_generator(chan); |
| 83 |
} |
| 84 |
|
| 85 |
timeout_ms = s * 1000.0; |
| 86 |
while ((ms = ast_remaining_ms(start, timeout_ms))) { |
| 87 |
ms = ast_waitfor(chan, ms); |
| 88 |
if (ms < 0) { |
| 89 |
res = -1; |
| 90 |
break; |
| 91 |
} |
| 92 |
if (ms > 0) { |
| 93 |
f = ast_read(chan); |
| 94 |
if (!f) { |
| 95 |
res = -1; |
| 96 |
break; |
| 97 |
} |
| 98 |
if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) { |
| 99 |
ast_verb(3, "Got a ring but still waiting for timeout\n"); |
| 100 |
} |
| 101 |
ast_frfree(f); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if (!res) { |
| 106 |
for (;;) { |
| 107 |
int wait_res = ast_waitfor(chan, -1); |
| 108 |
if (wait_res < 0) { |
| 109 |
res = -1; |
| 110 |
break; |
| 111 |
} else { |
| 112 |
f = ast_read(chan); |
| 113 |
if (!f) { |
| 114 |
res = -1; |
| 115 |
break; |
| 116 |
} |
| 117 |
if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_RING)) { |
| 118 |
ast_verb(3, "Got a ring after the timeout\n"); |
| 119 |
ast_frfree(f); |
| 120 |
break; |
| 121 |
} |
| 122 |
ast_frfree(f); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if (silgen) { |
| 128 |
ast_channel_stop_silence_generator(chan, silgen); |
| 129 |
} |
| 130 |
|
| 131 |
return res; |
| 132 |
} |
| 133 |
|
| 134 |
static int unload_module(void) |
| 135 |
{ |
| 136 |
return ast_unregister_application(app); |
| 137 |
} |
| 138 |
|
| 139 |
static int load_module(void) |
| 140 |
{ |
| 141 |
return ast_register_application_xml(app, waitforring_exec); |
| 142 |
} |
| 143 |
|
| 144 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Waits until first ring after time"); |