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: 119012 $")
00035
00036 #include <stdlib.h>
00037 #include <stdio.h>
00038 #include <string.h>
00039 #include <unistd.h>
00040 #include <errno.h>
00041
00042 #include "asterisk/lock.h"
00043 #include "asterisk/file.h"
00044 #include "asterisk/logger.h"
00045 #include "asterisk/channel.h"
00046 #include "asterisk/pbx.h"
00047 #include "asterisk/module.h"
00048 #include "asterisk/utils.h"
00049
00050 static char *app = "Milliwatt";
00051
00052 static char *synopsis = "Generate a Constant 1004Hz tone at 0dbm (mu-law)";
00053
00054 static char *descrip =
00055 " Milliwatt([options]): Generate a Constant 1004Hz tone at 0dbm.\n"
00056 "Previous versions of this application generated the tone at 1000Hz. If for\n"
00057 "some reason you would prefer that behavior, supply the 'o' option to get the\n"
00058 "old behavior.\n"
00059 "";
00060
00061
00062 static char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
00063
00064 static void *milliwatt_alloc(struct ast_channel *chan, void *params)
00065 {
00066 return ast_calloc(1, sizeof(int));
00067 }
00068
00069 static void milliwatt_release(struct ast_channel *chan, void *data)
00070 {
00071 free(data);
00072 return;
00073 }
00074
00075 static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
00076 {
00077 unsigned char buf[AST_FRIENDLY_OFFSET + 640];
00078 const int maxsamples = sizeof (buf) / sizeof (buf[0]);
00079 int i, *indexp = (int *) data;
00080 struct ast_frame wf = {
00081 .frametype = AST_FRAME_VOICE,
00082 .subclass = AST_FORMAT_ULAW,
00083 .offset = AST_FRIENDLY_OFFSET,
00084 .data = buf + AST_FRIENDLY_OFFSET,
00085 .src = __FUNCTION__,
00086 };
00087
00088
00089
00090
00091
00092
00093
00094 if (samples > maxsamples) {
00095 ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n", maxsamples, samples);
00096 samples = maxsamples;
00097 }
00098
00099 len = samples * sizeof (buf[0]);
00100 wf.datalen = len;
00101 wf.samples = samples;
00102
00103
00104 for (i = 0; i < len; i++) {
00105 buf[AST_FRIENDLY_OFFSET + i] = digital_milliwatt[(*indexp)++];
00106 *indexp &= 7;
00107 }
00108
00109 if (ast_write(chan,&wf) < 0) {
00110 ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",chan->name,strerror(errno));
00111 return -1;
00112 }
00113
00114 return 0;
00115 }
00116
00117 static struct ast_generator milliwattgen = {
00118 alloc: milliwatt_alloc,
00119 release: milliwatt_release,
00120 generate: milliwatt_generate,
00121 };
00122
00123 static int old_milliwatt_exec(struct ast_channel *chan)
00124 {
00125 ast_set_write_format(chan, AST_FORMAT_ULAW);
00126 ast_set_read_format(chan, AST_FORMAT_ULAW);
00127
00128 if (chan->_state != AST_STATE_UP) {
00129 ast_answer(chan);
00130 }
00131
00132 if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) {
00133 ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",chan->name);
00134 return -1;
00135 }
00136
00137 while (!ast_safe_sleep(chan, 10000))
00138 ;
00139
00140 ast_deactivate_generator(chan);
00141
00142 return -1;
00143 }
00144
00145 static int milliwatt_exec(struct ast_channel *chan, void *data)
00146 {
00147 const char *options = data;
00148 struct ast_app *playtones_app;
00149 struct ast_module_user *u;
00150 int res = -1;
00151
00152 u = ast_module_user_add(chan);
00153
00154 if (!ast_strlen_zero(options) && strchr(options, 'o')) {
00155 res = old_milliwatt_exec(chan);
00156 goto exit_app;
00157 }
00158
00159 if (!(playtones_app = pbx_findapp("Playtones"))) {
00160 ast_log(LOG_ERROR, "The Playtones application is required to run Milliwatt()\n");
00161 goto exit_app;
00162 }
00163
00164 res = pbx_exec(chan, playtones_app, "1004/1000");
00165
00166 while (!res) {
00167 res = ast_safe_sleep(chan, 10000);
00168 }
00169
00170 res = 0;
00171
00172 exit_app:
00173 ast_module_user_remove(u);
00174
00175 return res;
00176 }
00177
00178 static int unload_module(void)
00179 {
00180 int res;
00181
00182 res = ast_unregister_application(app);
00183
00184 ast_module_user_hangup_all();
00185
00186 return res;
00187 }
00188
00189 static int load_module(void)
00190 {
00191 return ast_register_application(app, milliwatt_exec, synopsis, descrip);
00192 }
00193
00194 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Digital Milliwatt (mu-law) Test Application");