| 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/module.h" |
| 35 |
#include "asterisk/channel.h" |
| 36 |
#include "asterisk/pbx.h" |
| 37 |
#include "asterisk/indications.h" |
| 38 |
#include "asterisk/format_cache.h" |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
static const char app[] = "Milliwatt"; |
| 76 |
|
| 77 |
static const char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ; |
| 78 |
|
| 79 |
static void *milliwatt_alloc(struct ast_channel *chan, void *params) |
| 80 |
{ |
| 81 |
return ast_calloc(1, sizeof(int)); |
| 82 |
} |
| 83 |
|
| 84 |
static void milliwatt_release(struct ast_channel *chan, void *data) |
| 85 |
{ |
| 86 |
ast_free(data); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples) |
| 91 |
{ |
| 92 |
unsigned char buf[AST_FRIENDLY_OFFSET + 640]; |
| 93 |
const int maxsamples = ARRAY_LEN(buf) - (AST_FRIENDLY_OFFSET / sizeof(buf[0])); |
| 94 |
int i, *indexp = (int *) data, res; |
| 95 |
struct ast_frame wf = { |
| 96 |
.frametype = AST_FRAME_VOICE, |
| 97 |
.offset = AST_FRIENDLY_OFFSET, |
| 98 |
.src = __FUNCTION__, |
| 99 |
}; |
| 100 |
|
| 101 |
wf.subclass.format = ast_format_ulaw; |
| 102 |
wf.data.ptr = buf + AST_FRIENDLY_OFFSET; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if (samples > maxsamples) { |
| 110 |
ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n", maxsamples, samples); |
| 111 |
samples = maxsamples; |
| 112 |
} |
| 113 |
|
| 114 |
len = samples * sizeof (buf[0]); |
| 115 |
wf.datalen = len; |
| 116 |
wf.samples = samples; |
| 117 |
|
| 118 |
|
| 119 |
for (i = 0; i < len; i++) { |
| 120 |
buf[AST_FRIENDLY_OFFSET + i] = digital_milliwatt[(*indexp)++]; |
| 121 |
*indexp &= 7; |
| 122 |
} |
| 123 |
|
| 124 |
res = ast_write(chan, &wf); |
| 125 |
ast_frfree(&wf); |
| 126 |
|
| 127 |
if (res < 0) { |
| 128 |
ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",ast_channel_name(chan),strerror(errno)); |
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
static struct ast_generator milliwattgen = { |
| 136 |
.alloc = milliwatt_alloc, |
| 137 |
.release = milliwatt_release, |
| 138 |
.generate = milliwatt_generate, |
| 139 |
}; |
| 140 |
|
| 141 |
static int old_milliwatt_exec(struct ast_channel *chan) |
| 142 |
{ |
| 143 |
ast_set_write_format(chan, ast_format_ulaw); |
| 144 |
ast_set_read_format(chan, ast_format_ulaw); |
| 145 |
|
| 146 |
if (ast_channel_state(chan) != AST_STATE_UP) { |
| 147 |
ast_answer(chan); |
| 148 |
} |
| 149 |
|
| 150 |
if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) { |
| 151 |
ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",ast_channel_name(chan)); |
| 152 |
return -1; |
| 153 |
} |
| 154 |
|
| 155 |
while (!ast_safe_sleep(chan, 10000)) |
| 156 |
; |
| 157 |
|
| 158 |
ast_deactivate_generator(chan); |
| 159 |
|
| 160 |
return -1; |
| 161 |
} |
| 162 |
|
| 163 |
static int milliwatt_exec(struct ast_channel *chan, const char *data) |
| 164 |
{ |
| 165 |
const char *options = data; |
| 166 |
int res = -1; |
| 167 |
|
| 168 |
if (!ast_strlen_zero(options) && strchr(options, 'o')) { |
| 169 |
return old_milliwatt_exec(chan); |
| 170 |
} |
| 171 |
if (!ast_strlen_zero(options) && strchr(options, 'm')) { |
| 172 |
res = ast_playtones_start(chan, 23255, "1004/9000,0/1000", 0); |
| 173 |
} else { |
| 174 |
res = ast_playtones_start(chan, 23255, "1004/1000", 0); |
| 175 |
} |
| 176 |
|
| 177 |
while (!res) { |
| 178 |
res = ast_safe_sleep(chan, 10000); |
| 179 |
} |
| 180 |
|
| 181 |
return res; |
| 182 |
} |
| 183 |
|
| 184 |
static int unload_module(void) |
| 185 |
{ |
| 186 |
return ast_unregister_application(app); |
| 187 |
} |
| 188 |
|
| 189 |
static int load_module(void) |
| 190 |
{ |
| 191 |
return ast_register_application_xml(app, milliwatt_exec); |
| 192 |
} |
| 193 |
|
| 194 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Digital Milliwatt (mu-law) Test Application"); |