app_milliwatt.c 4.4 KB

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