app_zapbarge.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Zap Barge support
  5. *
  6. * Copyright (C) 2003, Digium
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. *
  13. * Special thanks to comphealth.com for sponsoring this
  14. * GPL application.
  15. */
  16. #include <asterisk/lock.h>
  17. #include <asterisk/file.h>
  18. #include <asterisk/logger.h>
  19. #include <asterisk/channel.h>
  20. #include <asterisk/pbx.h>
  21. #include <asterisk/module.h>
  22. #include <asterisk/config.h>
  23. #include <asterisk/app.h>
  24. #include <asterisk/options.h>
  25. #include <asterisk/cli.h>
  26. #include <asterisk/say.h>
  27. #include <asterisk/utils.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <sys/ioctl.h>
  34. #ifdef __linux__
  35. #include <linux/zaptel.h>
  36. #else
  37. #include <zaptel.h>
  38. #endif /* __linux__ */
  39. static char *tdesc = "Barge in on Zap channel application";
  40. static char *app = "ZapBarge";
  41. static char *synopsis = "Barge in (monitor) Zap channel";
  42. static char *descrip =
  43. " ZapBarge([channel]): Barges in on a specified zap\n"
  44. "channel or prompts if one is not specified. Returns\n"
  45. "-1 when caller user hangs up and is independent of the\n"
  46. "state of the channel being monitored.";
  47. STANDARD_LOCAL_USER;
  48. LOCAL_USER_DECL;
  49. #define CONF_SIZE 160
  50. static int careful_write(int fd, unsigned char *data, int len)
  51. {
  52. int res;
  53. while(len) {
  54. res = write(fd, data, len);
  55. if (res < 1) {
  56. if (errno != EAGAIN) {
  57. ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
  58. return -1;
  59. } else
  60. return 0;
  61. }
  62. len -= res;
  63. data += res;
  64. }
  65. return 0;
  66. }
  67. static int conf_run(struct ast_channel *chan, int confno, int confflags)
  68. {
  69. int fd;
  70. struct zt_confinfo ztc;
  71. struct ast_frame *f;
  72. struct ast_channel *c;
  73. struct ast_frame fr;
  74. int outfd;
  75. int ms;
  76. int nfds;
  77. int res;
  78. int flags;
  79. int retryzap;
  80. int origfd;
  81. int ret = -1;
  82. ZT_BUFFERINFO bi;
  83. char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
  84. char *buf = __buf + AST_FRIENDLY_OFFSET;
  85. /* Set it into U-law mode (write) */
  86. if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
  87. ast_log(LOG_WARNING, "Unable to set '%s' to write ulaw mode\n", chan->name);
  88. goto outrun;
  89. }
  90. /* Set it into U-law mode (read) */
  91. if (ast_set_read_format(chan, AST_FORMAT_ULAW) < 0) {
  92. ast_log(LOG_WARNING, "Unable to set '%s' to read ulaw mode\n", chan->name);
  93. goto outrun;
  94. }
  95. ast_indicate(chan, -1);
  96. retryzap = strcasecmp(chan->type, "Zap");
  97. zapretry:
  98. origfd = chan->fds[0];
  99. if (retryzap) {
  100. fd = open("/dev/zap/pseudo", O_RDWR);
  101. if (fd < 0) {
  102. ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
  103. goto outrun;
  104. }
  105. /* Make non-blocking */
  106. flags = fcntl(fd, F_GETFL);
  107. if (flags < 0) {
  108. ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
  109. close(fd);
  110. goto outrun;
  111. }
  112. if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
  113. ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
  114. close(fd);
  115. goto outrun;
  116. }
  117. /* Setup buffering information */
  118. memset(&bi, 0, sizeof(bi));
  119. bi.bufsize = CONF_SIZE;
  120. bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
  121. bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
  122. bi.numbufs = 4;
  123. if (ioctl(fd, ZT_SET_BUFINFO, &bi)) {
  124. ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
  125. close(fd);
  126. goto outrun;
  127. }
  128. nfds = 1;
  129. } else {
  130. /* XXX Make sure we're not running on a pseudo channel XXX */
  131. fd = chan->fds[0];
  132. nfds = 0;
  133. }
  134. memset(&ztc, 0, sizeof(ztc));
  135. /* Check to see if we're in a conference... */
  136. ztc.chan = 0;
  137. if (ioctl(fd, ZT_GETCONF, &ztc)) {
  138. ast_log(LOG_WARNING, "Error getting conference\n");
  139. close(fd);
  140. goto outrun;
  141. }
  142. if (ztc.confmode) {
  143. /* Whoa, already in a conference... Retry... */
  144. if (!retryzap) {
  145. ast_log(LOG_DEBUG, "Zap channel is in a conference already, retrying with pseudo\n");
  146. retryzap = 1;
  147. goto zapretry;
  148. }
  149. }
  150. memset(&ztc, 0, sizeof(ztc));
  151. /* Add us to the conference */
  152. ztc.chan = 0;
  153. ztc.confno = confno;
  154. ztc.confmode = ZT_CONF_MONITORBOTH;
  155. if (ioctl(fd, ZT_SETCONF, &ztc)) {
  156. ast_log(LOG_WARNING, "Error setting conference\n");
  157. close(fd);
  158. goto outrun;
  159. }
  160. ast_log(LOG_DEBUG, "Placed channel %s in ZAP channel %d monitor\n", chan->name, confno);
  161. for(;;) {
  162. outfd = -1;
  163. ms = -1;
  164. c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
  165. if (c) {
  166. if (c->fds[0] != origfd) {
  167. if (retryzap) {
  168. /* Kill old pseudo */
  169. close(fd);
  170. }
  171. ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
  172. retryzap = 0;
  173. goto zapretry;
  174. }
  175. f = ast_read(c);
  176. if (!f)
  177. break;
  178. if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
  179. ret = 0;
  180. break;
  181. } else if (fd != chan->fds[0]) {
  182. if (f->frametype == AST_FRAME_VOICE) {
  183. if (f->subclass == AST_FORMAT_ULAW) {
  184. /* Carefully write */
  185. careful_write(fd, f->data, f->datalen);
  186. } else
  187. ast_log(LOG_WARNING, "Huh? Got a non-ulaw (%d) frame in the conference\n", f->subclass);
  188. }
  189. }
  190. ast_frfree(f);
  191. } else if (outfd > -1) {
  192. res = read(outfd, buf, CONF_SIZE);
  193. if (res > 0) {
  194. memset(&fr, 0, sizeof(fr));
  195. fr.frametype = AST_FRAME_VOICE;
  196. fr.subclass = AST_FORMAT_ULAW;
  197. fr.datalen = res;
  198. fr.samples = res;
  199. fr.data = buf;
  200. fr.offset = AST_FRIENDLY_OFFSET;
  201. if (ast_write(chan, &fr) < 0) {
  202. ast_log(LOG_WARNING, "Unable to write frame to channel: %s\n", strerror(errno));
  203. /* break; */
  204. }
  205. } else
  206. ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
  207. }
  208. }
  209. if (fd != chan->fds[0])
  210. close(fd);
  211. else {
  212. /* Take out of conference */
  213. /* Add us to the conference */
  214. ztc.chan = 0;
  215. ztc.confno = 0;
  216. ztc.confmode = 0;
  217. if (ioctl(fd, ZT_SETCONF, &ztc)) {
  218. ast_log(LOG_WARNING, "Error setting conference\n");
  219. }
  220. }
  221. outrun:
  222. return ret;
  223. }
  224. static int conf_exec(struct ast_channel *chan, void *data)
  225. {
  226. int res=-1;
  227. struct localuser *u;
  228. int retrycnt = 0;
  229. int confflags = 0;
  230. int confno = 0;
  231. char confstr[80] = "";
  232. if (data && !ast_strlen_zero(data)) {
  233. if ((sscanf(data, "Zap/%d", &confno) != 1) &&
  234. (sscanf(data, "%d", &confno) != 1)) {
  235. ast_log(LOG_WARNING, "ZapBarge Argument (if specified) must be a channel number, not '%s'\n", (char *)data);
  236. return 0;
  237. }
  238. }
  239. LOCAL_USER_ADD(u);
  240. if (chan->_state != AST_STATE_UP)
  241. ast_answer(chan);
  242. while(!confno && (++retrycnt < 4)) {
  243. /* Prompt user for conference number */
  244. confstr[0] = '\0';
  245. res = ast_app_getdata(chan, "conf-getchannel",confstr, sizeof(confstr) - 1, 0);
  246. if (res <0) goto out;
  247. if (sscanf(confstr, "%d", &confno) != 1)
  248. confno = 0;
  249. }
  250. if (confno) {
  251. /* XXX Should prompt user for pin if pin is required XXX */
  252. /* Run the conference */
  253. res = conf_run(chan, confno, confflags);
  254. }
  255. out:
  256. /* Do the conference */
  257. LOCAL_USER_REMOVE(u);
  258. return res;
  259. }
  260. int unload_module(void)
  261. {
  262. STANDARD_HANGUP_LOCALUSERS;
  263. return ast_unregister_application(app);
  264. }
  265. int load_module(void)
  266. {
  267. return ast_register_application(app, conf_exec, synopsis, descrip);
  268. }
  269. char *description(void)
  270. {
  271. return tdesc;
  272. }
  273. int usecount(void)
  274. {
  275. int res;
  276. STANDARD_USECOUNT(res);
  277. return res;
  278. }
  279. char *key()
  280. {
  281. return ASTERISK_GPL_KEY;
  282. }