app_milliwatt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * \ingroup applications
  23. */
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/lock.h"
  32. #include "asterisk/file.h"
  33. #include "asterisk/logger.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/module.h"
  37. static char *tdesc = "Digital Milliwatt (mu-law) Test Application";
  38. static char *app = "Milliwatt";
  39. static char *synopsis = "Generate a Constant 1000Hz tone at 0dbm (mu-law)";
  40. static char *descrip =
  41. "Milliwatt(): Generate a Constant 1000Hz tone at 0dbm (mu-law)\n";
  42. STANDARD_LOCAL_USER;
  43. LOCAL_USER_DECL;
  44. static char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
  45. static void *milliwatt_alloc(struct ast_channel *chan, void *params)
  46. {
  47. int *indexp;
  48. indexp = malloc(sizeof(int));
  49. if (indexp == NULL) return(NULL);
  50. *indexp = 0;
  51. return(indexp);
  52. }
  53. static void milliwatt_release(struct ast_channel *chan, void *data)
  54. {
  55. free(data);
  56. return;
  57. }
  58. static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
  59. {
  60. struct ast_frame wf;
  61. unsigned char waste[AST_FRIENDLY_OFFSET];
  62. unsigned char buf[640];
  63. int i,*indexp = (int *) data;
  64. if (len > sizeof(buf))
  65. {
  66. ast_log(LOG_WARNING,"Only doing %d bytes (%d bytes requested)\n",(int)sizeof(buf),len);
  67. len = sizeof(buf);
  68. }
  69. waste[0] = 0; /* make compiler happy */
  70. wf.frametype = AST_FRAME_VOICE;
  71. wf.subclass = AST_FORMAT_ULAW;
  72. wf.offset = AST_FRIENDLY_OFFSET;
  73. wf.mallocd = 0;
  74. wf.data = buf;
  75. wf.datalen = len;
  76. wf.samples = wf.datalen;
  77. wf.src = "app_milliwatt";
  78. wf.delivery.tv_sec = 0;
  79. wf.delivery.tv_usec = 0;
  80. /* create a buffer containing the digital milliwatt pattern */
  81. for(i = 0; i < len; i++)
  82. {
  83. buf[i] = digital_milliwatt[(*indexp)++];
  84. *indexp &= 7;
  85. }
  86. if (ast_write(chan,&wf) < 0)
  87. {
  88. ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",chan->name,strerror(errno));
  89. return -1;
  90. }
  91. return 0;
  92. }
  93. static struct ast_generator milliwattgen =
  94. {
  95. alloc: milliwatt_alloc,
  96. release: milliwatt_release,
  97. generate: milliwatt_generate,
  98. } ;
  99. static int milliwatt_exec(struct ast_channel *chan, void *data)
  100. {
  101. struct localuser *u;
  102. LOCAL_USER_ADD(u);
  103. ast_set_write_format(chan, AST_FORMAT_ULAW);
  104. ast_set_read_format(chan, AST_FORMAT_ULAW);
  105. if (chan->_state != AST_STATE_UP)
  106. {
  107. ast_answer(chan);
  108. }
  109. if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0)
  110. {
  111. ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",chan->name);
  112. LOCAL_USER_REMOVE(u);
  113. return -1;
  114. }
  115. while(!ast_safe_sleep(chan, 10000));
  116. ast_deactivate_generator(chan);
  117. LOCAL_USER_REMOVE(u);
  118. return -1;
  119. }
  120. int unload_module(void)
  121. {
  122. int res;
  123. res = ast_unregister_application(app);
  124. STANDARD_HANGUP_LOCALUSERS;
  125. return res;
  126. }
  127. int load_module(void)
  128. {
  129. return ast_register_application(app, milliwatt_exec, synopsis, descrip);
  130. }
  131. char *description(void)
  132. {
  133. return tdesc;
  134. }
  135. int usecount(void)
  136. {
  137. int res;
  138. STANDARD_USECOUNT(res);
  139. return res;
  140. }
  141. char *key()
  142. {
  143. return ASTERISK_GPL_KEY;
  144. }