autoservice.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Automatic channel service routines
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/time.h>
  17. #include <signal.h>
  18. #include <errno.h>
  19. #include <unistd.h>
  20. #include <math.h> /* For PI */
  21. #include <asterisk/pbx.h>
  22. #include <asterisk/frame.h>
  23. #include <asterisk/sched.h>
  24. #include <asterisk/options.h>
  25. #include <asterisk/channel.h>
  26. #include <asterisk/channel_pvt.h>
  27. #include <asterisk/logger.h>
  28. #include <asterisk/file.h>
  29. #include <asterisk/translate.h>
  30. #include <asterisk/manager.h>
  31. #include <asterisk/chanvars.h>
  32. #include <asterisk/linkedlists.h>
  33. #include <asterisk/indications.h>
  34. #include <asterisk/lock.h>
  35. #include <asterisk/utils.h>
  36. #define MAX_AUTOMONS 256
  37. AST_MUTEX_DEFINE_STATIC(autolock);
  38. struct asent {
  39. struct ast_channel *chan;
  40. struct asent *next;
  41. };
  42. static struct asent *aslist = NULL;
  43. static pthread_t asthread = AST_PTHREADT_NULL;
  44. static void *autoservice_run(void *ign)
  45. {
  46. struct ast_channel *mons[MAX_AUTOMONS];
  47. int x;
  48. int ms;
  49. struct ast_channel *chan;
  50. struct asent *as;
  51. struct ast_frame *f;
  52. for(;;) {
  53. x = 0;
  54. ast_mutex_lock(&autolock);
  55. as = aslist;
  56. while(as) {
  57. if (!as->chan->_softhangup) {
  58. if (x < MAX_AUTOMONS)
  59. mons[x++] = as->chan;
  60. else
  61. ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
  62. }
  63. as = as->next;
  64. }
  65. ast_mutex_unlock(&autolock);
  66. /* if (!aslist)
  67. break; */
  68. ms = 500;
  69. chan = ast_waitfor_n(mons, x, &ms);
  70. if (chan) {
  71. /* Read and ignore anything that occurs */
  72. f = ast_read(chan);
  73. if (f)
  74. ast_frfree(f);
  75. }
  76. }
  77. asthread = AST_PTHREADT_NULL;
  78. return NULL;
  79. }
  80. int ast_autoservice_start(struct ast_channel *chan)
  81. {
  82. int res = -1;
  83. struct asent *as;
  84. int needstart;
  85. ast_mutex_lock(&autolock);
  86. needstart = (asthread == AST_PTHREADT_NULL) ? 1 : 0 /* aslist ? 0 : 1 */;
  87. as = aslist;
  88. while(as) {
  89. if (as->chan == chan)
  90. break;
  91. as = as->next;
  92. }
  93. if (!as) {
  94. as = malloc(sizeof(struct asent));
  95. if (as) {
  96. memset(as, 0, sizeof(struct asent));
  97. as->chan = chan;
  98. as->next = aslist;
  99. aslist = as;
  100. res = 0;
  101. if (needstart) {
  102. if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
  103. ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
  104. free(aslist);
  105. aslist = NULL;
  106. res = -1;
  107. } else
  108. pthread_kill(asthread, SIGURG);
  109. }
  110. }
  111. }
  112. ast_mutex_unlock(&autolock);
  113. return res;
  114. }
  115. int ast_autoservice_stop(struct ast_channel *chan)
  116. {
  117. int res = -1;
  118. struct asent *as, *prev;
  119. ast_mutex_lock(&autolock);
  120. as = aslist;
  121. prev = NULL;
  122. while(as) {
  123. if (as->chan == chan)
  124. break;
  125. prev = as;
  126. as = as->next;
  127. }
  128. if (as) {
  129. if (prev)
  130. prev->next = as->next;
  131. else
  132. aslist = as->next;
  133. free(as);
  134. if (!chan->_softhangup)
  135. res = 0;
  136. }
  137. if (asthread != AST_PTHREADT_NULL)
  138. pthread_kill(asthread, SIGURG);
  139. ast_mutex_unlock(&autolock);
  140. /* Wait for it to un-block */
  141. while(chan->blocking)
  142. usleep(1000);
  143. return res;
  144. }