app_milliwatt.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "asterisk.h"
00033
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 361142 $")
00035
00036 #include "asterisk/module.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/indications.h"
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 static const char app[] = "Milliwatt";
00064
00065 static const char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
00066
00067 static void *milliwatt_alloc(struct ast_channel *chan, void *params)
00068 {
00069 return ast_calloc(1, sizeof(int));
00070 }
00071
00072 static void milliwatt_release(struct ast_channel *chan, void *data)
00073 {
00074 ast_free(data);
00075 return;
00076 }
00077
00078 static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
00079 {
00080 unsigned char buf[AST_FRIENDLY_OFFSET + 640];
00081 const int maxsamples = ARRAY_LEN(buf) - (AST_FRIENDLY_OFFSET / sizeof(buf[0]));
00082 int i, *indexp = (int *) data;
00083 struct ast_frame wf = {
00084 .frametype = AST_FRAME_VOICE,
00085 .subclass.codec = AST_FORMAT_ULAW,
00086 .offset = AST_FRIENDLY_OFFSET,
00087 .src = __FUNCTION__,
00088 };
00089 wf.data.ptr = buf + AST_FRIENDLY_OFFSET;
00090
00091
00092
00093
00094
00095
00096 if (samples > maxsamples) {
00097 ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n", maxsamples, samples);
00098 samples = maxsamples;
00099 }
00100
00101 len = samples * sizeof (buf[0]);
00102 wf.datalen = len;
00103 wf.samples = samples;
00104
00105
00106 for (i = 0; i < len; i++) {
00107 buf[AST_FRIENDLY_OFFSET + i] = digital_milliwatt[(*indexp)++];
00108 *indexp &= 7;
00109 }
00110
00111 if (ast_write(chan,&wf) < 0) {
00112 ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",chan->name,strerror(errno));
00113 return -1;
00114 }
00115
00116 return 0;
00117 }
00118
00119 static struct ast_generator milliwattgen = {
00120 .alloc = milliwatt_alloc,
00121 .release = milliwatt_release,
00122 .generate = milliwatt_generate,
00123 };
00124
00125 static int old_milliwatt_exec(struct ast_channel *chan)
00126 {
00127 ast_set_write_format(chan, AST_FORMAT_ULAW);
00128 ast_set_read_format(chan, AST_FORMAT_ULAW);
00129
00130 if (chan->_state != AST_STATE_UP) {
00131 ast_answer(chan);
00132 }
00133
00134 if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) {
00135 ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",chan->name);
00136 return -1;
00137 }
00138
00139 while (!ast_safe_sleep(chan, 10000))
00140 ;
00141
00142 ast_deactivate_generator(chan);
00143
00144 return -1;
00145 }
00146
00147 static int milliwatt_exec(struct ast_channel *chan, const char *data)
00148 {
00149 const char *options = data;
00150 int res = -1;
00151
00152 if (!ast_strlen_zero(options) && strchr(options, 'o')) {
00153 return old_milliwatt_exec(chan);
00154 }
00155
00156 res = ast_playtones_start(chan, 23255, "1004/1000", 0);
00157
00158 while (!res) {
00159 res = ast_safe_sleep(chan, 10000);
00160 }
00161
00162 return res;
00163 }
00164
00165 static int unload_module(void)
00166 {
00167 return ast_unregister_application(app);
00168 }
00169
00170 static int load_module(void)
00171 {
00172 return ast_register_application_xml(app, milliwatt_exec);
00173 }
00174
00175 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Digital Milliwatt (mu-law) Test Application");