app_milliwatt.c 4.0 KB

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