app_milliwatt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  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. /*! \file
  19. *
  20. * \brief Digital Milliwatt Test
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/module.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/indications.h"
  35. #include "asterisk/format_cache.h"
  36. /*** DOCUMENTATION
  37. <application name="Milliwatt" language="en_US">
  38. <synopsis>
  39. Generate a Constant 1004Hz tone at 0dbm (mu-law).
  40. </synopsis>
  41. <syntax>
  42. <parameter name="options">
  43. <optionlist>
  44. <option name="o">
  45. <para>Generate the tone at 1000Hz like previous version.</para>
  46. </option>
  47. </optionlist>
  48. </parameter>
  49. </syntax>
  50. <description>
  51. <para>Previous versions of this application generated the tone at 1000Hz. If for
  52. some reason you would prefer that behavior, supply the <literal>o</literal> option to get the
  53. old behavior.</para>
  54. </description>
  55. </application>
  56. ***/
  57. static const char app[] = "Milliwatt";
  58. static const char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
  59. static void *milliwatt_alloc(struct ast_channel *chan, void *params)
  60. {
  61. return ast_calloc(1, sizeof(int));
  62. }
  63. static void milliwatt_release(struct ast_channel *chan, void *data)
  64. {
  65. ast_free(data);
  66. return;
  67. }
  68. static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
  69. {
  70. unsigned char buf[AST_FRIENDLY_OFFSET + 640];
  71. const int maxsamples = ARRAY_LEN(buf) - (AST_FRIENDLY_OFFSET / sizeof(buf[0]));
  72. int i, *indexp = (int *) data, res;
  73. struct ast_frame wf = {
  74. .frametype = AST_FRAME_VOICE,
  75. .offset = AST_FRIENDLY_OFFSET,
  76. .src = __FUNCTION__,
  77. };
  78. wf.subclass.format = ast_format_ulaw;
  79. wf.data.ptr = buf + AST_FRIENDLY_OFFSET;
  80. /* Instead of len, use samples, because channel.c generator_force
  81. * generate(chan, tmp, 0, 160) ignores len. In any case, len is
  82. * a multiple of samples, given by number of samples times bytes per
  83. * sample. In the case of ulaw, len = samples. for signed linear
  84. * len = 2 * samples */
  85. if (samples > maxsamples) {
  86. ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n", maxsamples, samples);
  87. samples = maxsamples;
  88. }
  89. len = samples * sizeof (buf[0]);
  90. wf.datalen = len;
  91. wf.samples = samples;
  92. /* create a buffer containing the digital milliwatt pattern */
  93. for (i = 0; i < len; i++) {
  94. buf[AST_FRIENDLY_OFFSET + i] = digital_milliwatt[(*indexp)++];
  95. *indexp &= 7;
  96. }
  97. res = ast_write(chan, &wf);
  98. ast_frfree(&wf);
  99. if (res < 0) {
  100. ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",ast_channel_name(chan),strerror(errno));
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. static struct ast_generator milliwattgen = {
  106. .alloc = milliwatt_alloc,
  107. .release = milliwatt_release,
  108. .generate = milliwatt_generate,
  109. };
  110. static int old_milliwatt_exec(struct ast_channel *chan)
  111. {
  112. ast_set_write_format(chan, ast_format_ulaw);
  113. ast_set_read_format(chan, ast_format_ulaw);
  114. if (ast_channel_state(chan) != AST_STATE_UP) {
  115. ast_answer(chan);
  116. }
  117. if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) {
  118. ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",ast_channel_name(chan));
  119. return -1;
  120. }
  121. while (!ast_safe_sleep(chan, 10000))
  122. ;
  123. ast_deactivate_generator(chan);
  124. return -1;
  125. }
  126. static int milliwatt_exec(struct ast_channel *chan, const char *data)
  127. {
  128. const char *options = data;
  129. int res = -1;
  130. if (!ast_strlen_zero(options) && strchr(options, 'o')) {
  131. return old_milliwatt_exec(chan);
  132. }
  133. res = ast_playtones_start(chan, 23255, "1004/1000", 0);
  134. while (!res) {
  135. res = ast_safe_sleep(chan, 10000);
  136. }
  137. return res;
  138. }
  139. static int unload_module(void)
  140. {
  141. return ast_unregister_application(app);
  142. }
  143. static int load_module(void)
  144. {
  145. return ast_register_application_xml(app, milliwatt_exec);
  146. }
  147. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Digital Milliwatt (mu-law) Test Application");