autoservice.c 3.1 KB

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