app_zapscan.c 11 KB

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