chan_phone.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Generic Linux Telephony Interface driver
  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 <string.h>
  15. #include <asterisk/lock.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/channel_pvt.h>
  18. #include <asterisk/config.h>
  19. #include <asterisk/logger.h>
  20. #include <asterisk/module.h>
  21. #include <asterisk/pbx.h>
  22. #include <asterisk/options.h>
  23. #include <asterisk/utils.h>
  24. #include <asterisk/callerid.h>
  25. #include <sys/socket.h>
  26. #include <sys/time.h>
  27. #include <errno.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <arpa/inet.h>
  31. #include <fcntl.h>
  32. #include <sys/ioctl.h>
  33. #include <linux/telephony.h>
  34. /* Still use some IXJ specific stuff */
  35. #include <linux/version.h>
  36. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
  37. # include <linux/compiler.h>
  38. #endif
  39. #include <linux/ixjuser.h>
  40. #include "DialTone.h"
  41. #ifdef QTI_PHONEJACK_TJ_PCI /* check for the newer quicknet driver v.3.1.0 which has this symbol */
  42. #define QNDRV_VER 310
  43. #else
  44. #define QNDRV_VER 100
  45. #endif
  46. #if QNDRV_VER > 100
  47. #ifdef __linux__
  48. #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
  49. #else /* FreeBSD and others */
  50. #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, x);
  51. #endif /* __linux__ */
  52. #else /* older driver */
  53. #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
  54. #endif
  55. #define DEFAULT_CALLER_ID "Unknown"
  56. #define PHONE_MAX_BUF 480
  57. #define DEFAULT_GAIN 0x100
  58. static char *desc = "Linux Telephony API Support";
  59. static char *type = "Phone";
  60. static char *tdesc = "Standard Linux Telephony API Driver";
  61. static char *config = "phone.conf";
  62. /* Default context for dialtone mode */
  63. static char context[AST_MAX_EXTENSION] = "default";
  64. /* Default language */
  65. static char language[MAX_LANGUAGE] = "";
  66. static int usecnt =0;
  67. static int echocancel = AEC_OFF;
  68. static int silencesupression = 0;
  69. static int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW;
  70. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  71. /* Protect the interface list (of phone_pvt's) */
  72. AST_MUTEX_DEFINE_STATIC(iflock);
  73. /* Protect the monitoring thread, so only one process can kill or start it, and not
  74. when it's doing something critical. */
  75. AST_MUTEX_DEFINE_STATIC(monlock);
  76. /* This is the thread for the monitor which checks for input on the channels
  77. which are not currently in use. */
  78. static pthread_t monitor_thread = AST_PTHREADT_NULL;
  79. static int restart_monitor(void);
  80. /* The private structures of the Phone Jack channels are linked for
  81. selecting outgoing channels */
  82. #define MODE_DIALTONE 1
  83. #define MODE_IMMEDIATE 2
  84. #define MODE_FXO 3
  85. static struct phone_pvt {
  86. int fd; /* Raw file descriptor for this device */
  87. struct ast_channel *owner; /* Channel we belong to, possibly NULL */
  88. int mode; /* Is this in the */
  89. int lastformat; /* Last output format */
  90. int lastinput; /* Last input format */
  91. int ministate; /* Miniature state, for dialtone mode */
  92. char dev[256]; /* Device name */
  93. struct phone_pvt *next; /* Next channel in list */
  94. struct ast_frame fr; /* Frame */
  95. char offset[AST_FRIENDLY_OFFSET];
  96. char buf[PHONE_MAX_BUF]; /* Static buffer for reading frames */
  97. int obuflen;
  98. int dialtone;
  99. int txgain, rxgain; /* gain control for playing, recording */
  100. /* 0x100 - 1.0, 0x200 - 2.0, 0x80 - 0.5 */
  101. int cpt; /* Call Progress Tone playing? */
  102. int silencesupression;
  103. char context[AST_MAX_EXTENSION];
  104. char obuf[PHONE_MAX_BUF * 2];
  105. char ext[AST_MAX_EXTENSION];
  106. char language[MAX_LANGUAGE];
  107. char callerid[AST_MAX_EXTENSION];
  108. } *iflist = NULL;
  109. static char callerid[AST_MAX_EXTENSION];
  110. static int phone_digit(struct ast_channel *ast, char digit)
  111. {
  112. struct phone_pvt *p;
  113. int outdigit;
  114. p = ast->pvt->pvt;
  115. ast_log(LOG_NOTICE, "Dialed %c\n", digit);
  116. switch(digit) {
  117. case '0':
  118. case '1':
  119. case '2':
  120. case '3':
  121. case '4':
  122. case '5':
  123. case '6':
  124. case '7':
  125. case '8':
  126. case '9':
  127. outdigit = digit - '0';
  128. break;
  129. case '*':
  130. outdigit = 11;
  131. break;
  132. case '#':
  133. outdigit = 12;
  134. break;
  135. case 'f': //flash
  136. case 'F':
  137. ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
  138. usleep(320000);
  139. ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
  140. p->lastformat = -1;
  141. return 0;
  142. default:
  143. ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
  144. return -1;
  145. }
  146. ast_log(LOG_NOTICE, "Dialed %i\n", outdigit);
  147. ioctl(p->fd, PHONE_PLAY_TONE, outdigit);
  148. p->lastformat = -1;
  149. return 0;
  150. }
  151. static int phone_call(struct ast_channel *ast, char *dest, int timeout)
  152. {
  153. struct phone_pvt *p;
  154. PHONE_CID cid;
  155. time_t UtcTime;
  156. struct tm tm;
  157. time(&UtcTime);
  158. localtime_r(&UtcTime,&tm);
  159. memset(&cid, 0, sizeof(PHONE_CID));
  160. if(&tm != NULL) {
  161. snprintf(cid.month, sizeof(cid.month), "%02d",(tm.tm_mon + 1));
  162. snprintf(cid.day, sizeof(cid.day), "%02d", tm.tm_mday);
  163. snprintf(cid.hour, sizeof(cid.hour), "%02d", tm.tm_hour);
  164. snprintf(cid.min, sizeof(cid.min), "%02d", tm.tm_min);
  165. }
  166. /* the standard format of ast->callerid is: "name" <number>, but not always complete */
  167. if (!ast->callerid || ast_strlen_zero(ast->callerid)){
  168. strncpy(cid.name, DEFAULT_CALLER_ID, sizeof(cid.name) - 1);
  169. cid.number[0]='\0';
  170. } else {
  171. char *n, *l;
  172. char callerid[256] = "";
  173. strncpy(callerid, ast->callerid, sizeof(callerid) - 1);
  174. ast_callerid_parse(callerid, &n, &l);
  175. if (l) {
  176. ast_shrink_phone_number(l);
  177. if (!ast_isphonenumber(l))
  178. l = NULL;
  179. }
  180. if (l)
  181. strncpy(cid.number, l, sizeof(cid.number) - 1);
  182. if (n)
  183. strncpy(cid.name, n, sizeof(cid.name) - 1);
  184. }
  185. p = ast->pvt->pvt;
  186. if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
  187. ast_log(LOG_WARNING, "phone_call called on %s, neither down nor reserved\n", ast->name);
  188. return -1;
  189. }
  190. if (option_debug)
  191. ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fds[0]);
  192. IXJ_PHONE_RING_START(cid);
  193. ast_setstate(ast, AST_STATE_RINGING);
  194. ast_queue_control(ast, AST_CONTROL_RINGING);
  195. return 0;
  196. }
  197. static int phone_hangup(struct ast_channel *ast)
  198. {
  199. struct phone_pvt *p;
  200. p = ast->pvt->pvt;
  201. if (option_debug)
  202. ast_log(LOG_DEBUG, "phone_hangup(%s)\n", ast->name);
  203. if (!ast->pvt->pvt) {
  204. ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
  205. return 0;
  206. }
  207. /* XXX Is there anything we can do to really hang up except stop recording? */
  208. ast_setstate(ast, AST_STATE_DOWN);
  209. if (ioctl(p->fd, PHONE_REC_STOP))
  210. ast_log(LOG_WARNING, "Failed to stop recording\n");
  211. if (ioctl(p->fd, PHONE_PLAY_STOP))
  212. ast_log(LOG_WARNING, "Failed to stop playing\n");
  213. if (ioctl(p->fd, PHONE_RING_STOP))
  214. ast_log(LOG_WARNING, "Failed to stop ringing\n");
  215. if (ioctl(p->fd, PHONE_CPT_STOP))
  216. ast_log(LOG_WARNING, "Failed to stop sounds\n");
  217. /* If it's an FXO, hang them up */
  218. if (p->mode == MODE_FXO) {
  219. if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK))
  220. ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",ast->name, strerror(errno));
  221. }
  222. /* If they're off hook, give a busy signal */
  223. if (ioctl(p->fd, PHONE_HOOKSTATE)) {
  224. if (option_debug)
  225. ast_log(LOG_DEBUG, "Got hunghup, giving busy signal\n");
  226. ioctl(p->fd, PHONE_BUSY);
  227. p->cpt = 1;
  228. }
  229. p->lastformat = -1;
  230. p->lastinput = -1;
  231. p->ministate = 0;
  232. p->obuflen = 0;
  233. p->dialtone = 0;
  234. memset(p->ext, 0, sizeof(p->ext));
  235. ((struct phone_pvt *)(ast->pvt->pvt))->owner = NULL;
  236. ast_mutex_lock(&usecnt_lock);
  237. usecnt--;
  238. if (usecnt < 0)
  239. ast_log(LOG_WARNING, "Usecnt < 0???\n");
  240. ast_mutex_unlock(&usecnt_lock);
  241. ast_update_use_count();
  242. if (option_verbose > 2)
  243. ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
  244. ast->pvt->pvt = NULL;
  245. ast_setstate(ast, AST_STATE_DOWN);
  246. restart_monitor();
  247. return 0;
  248. }
  249. static int phone_setup(struct ast_channel *ast)
  250. {
  251. struct phone_pvt *p;
  252. p = ast->pvt->pvt;
  253. ioctl(p->fd, PHONE_CPT_STOP);
  254. /* Nothing to answering really, just start recording */
  255. if (ast->pvt->rawreadformat == AST_FORMAT_G723_1) {
  256. /* Prefer g723 */
  257. ioctl(p->fd, PHONE_REC_STOP);
  258. if (p->lastinput != AST_FORMAT_G723_1) {
  259. p->lastinput = AST_FORMAT_G723_1;
  260. if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
  261. ast_log(LOG_WARNING, "Failed to set codec to g723.1\n");
  262. return -1;
  263. }
  264. }
  265. } else if (ast->pvt->rawreadformat == AST_FORMAT_SLINEAR) {
  266. ioctl(p->fd, PHONE_REC_STOP);
  267. if (p->lastinput != AST_FORMAT_SLINEAR) {
  268. p->lastinput = AST_FORMAT_SLINEAR;
  269. if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
  270. ast_log(LOG_WARNING, "Failed to set codec to signed linear 16\n");
  271. return -1;
  272. }
  273. }
  274. } else if (ast->pvt->rawreadformat == AST_FORMAT_ULAW) {
  275. ioctl(p->fd, PHONE_REC_STOP);
  276. if (p->lastinput != AST_FORMAT_ULAW) {
  277. p->lastinput = AST_FORMAT_ULAW;
  278. if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
  279. ast_log(LOG_WARNING, "Failed to set codec to uLaw\n");
  280. return -1;
  281. }
  282. }
  283. } else {
  284. ast_log(LOG_WARNING, "Can't do format %s\n", ast_getformatname(ast->pvt->rawreadformat));
  285. return -1;
  286. }
  287. if (ioctl(p->fd, PHONE_REC_START)) {
  288. ast_log(LOG_WARNING, "Failed to start recording\n");
  289. return -1;
  290. }
  291. //set the DTMF times (the default is too short)
  292. ioctl(p->fd, PHONE_SET_TONE_ON_TIME, 300);
  293. ioctl(p->fd, PHONE_SET_TONE_OFF_TIME, 200);
  294. return 0;
  295. }
  296. static int phone_answer(struct ast_channel *ast)
  297. {
  298. struct phone_pvt *p;
  299. p = ast->pvt->pvt;
  300. /* In case it's a LineJack, take it off hook */
  301. if (p->mode == MODE_FXO) {
  302. if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_OFF_HOOK))
  303. ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n", ast->name, strerror(errno));
  304. else
  305. ast_log(LOG_DEBUG, "Took linejack off hook\n");
  306. }
  307. phone_setup(ast);
  308. if (option_debug)
  309. ast_log(LOG_DEBUG, "phone_answer(%s)\n", ast->name);
  310. ast->rings = 0;
  311. ast_setstate(ast, AST_STATE_UP);
  312. return 0;
  313. }
  314. #if 0
  315. static char phone_2digit(char c)
  316. {
  317. if (c == 12)
  318. return '#';
  319. else if (c == 11)
  320. return '*';
  321. else if ((c < 10) && (c >= 0))
  322. return '0' + c - 1;
  323. else
  324. return '?';
  325. }
  326. #endif
  327. static struct ast_frame *phone_exception(struct ast_channel *ast)
  328. {
  329. int res;
  330. union telephony_exception phonee;
  331. struct phone_pvt *p = ast->pvt->pvt;
  332. char digit;
  333. /* Some nice norms */
  334. p->fr.datalen = 0;
  335. p->fr.samples = 0;
  336. p->fr.data = NULL;
  337. p->fr.src = type;
  338. p->fr.offset = 0;
  339. p->fr.mallocd=0;
  340. p->fr.delivery.tv_sec = 0;
  341. p->fr.delivery.tv_usec = 0;
  342. phonee.bytes = ioctl(p->fd, PHONE_EXCEPTION);
  343. if (phonee.bits.dtmf_ready) {
  344. if (option_debug)
  345. ast_log(LOG_DEBUG, "phone_exception(): DTMF\n");
  346. /* We've got a digit -- Just handle this nicely and easily */
  347. digit = ioctl(p->fd, PHONE_GET_DTMF_ASCII);
  348. p->fr.subclass = digit;
  349. p->fr.frametype = AST_FRAME_DTMF;
  350. return &p->fr;
  351. }
  352. if (phonee.bits.hookstate) {
  353. if (option_debug)
  354. ast_log(LOG_DEBUG, "Hookstate changed\n");
  355. res = ioctl(p->fd, PHONE_HOOKSTATE);
  356. /* See if we've gone on hook, if so, notify by returning NULL */
  357. if (option_debug)
  358. ast_log(LOG_DEBUG, "New hookstate: %d\n", res);
  359. if (!res && (p->mode != MODE_FXO))
  360. return NULL;
  361. else {
  362. if (ast->_state == AST_STATE_RINGING) {
  363. /* They've picked up the phone */
  364. p->fr.frametype = AST_FRAME_CONTROL;
  365. p->fr.subclass = AST_CONTROL_ANSWER;
  366. phone_setup(ast);
  367. ast_setstate(ast, AST_STATE_UP);
  368. return &p->fr;
  369. } else
  370. ast_log(LOG_WARNING, "Got off hook in weird state %d\n", ast->_state);
  371. }
  372. }
  373. #if 1
  374. if (phonee.bits.pstn_ring)
  375. ast_verbose("Unit is ringing\n");
  376. if (phonee.bits.caller_id) {
  377. ast_verbose("We have caller ID\n");
  378. }
  379. if (phonee.bits.pstn_wink)
  380. ast_verbose("Detected Wink\n");
  381. #endif
  382. /* Strange -- nothing there.. */
  383. p->fr.frametype = AST_FRAME_NULL;
  384. p->fr.subclass = 0;
  385. return &p->fr;
  386. }
  387. static struct ast_frame *phone_read(struct ast_channel *ast)
  388. {
  389. int res;
  390. struct phone_pvt *p = ast->pvt->pvt;
  391. /* Some nice norms */
  392. p->fr.datalen = 0;
  393. p->fr.samples = 0;
  394. p->fr.data = NULL;
  395. p->fr.src = type;
  396. p->fr.offset = 0;
  397. p->fr.mallocd=0;
  398. p->fr.delivery.tv_sec = 0;
  399. p->fr.delivery.tv_usec = 0;
  400. /* Try to read some data... */
  401. CHECK_BLOCKING(ast);
  402. res = read(p->fd, p->buf, PHONE_MAX_BUF);
  403. ast->blocking = 0;
  404. if (res < 0) {
  405. #if 0
  406. if (errno == EAGAIN) {
  407. ast_log(LOG_WARNING, "Null frame received\n");
  408. p->fr.frametype = AST_FRAME_NULL;
  409. p->fr.subclass = 0;
  410. return &p->fr;
  411. }
  412. #endif
  413. ast_log(LOG_WARNING, "Error reading: %s\n", strerror(errno));
  414. return NULL;
  415. }
  416. p->fr.data = p->buf;
  417. switch(p->buf[0] & 0x3) {
  418. case '0':
  419. case '1':
  420. /* Normal */
  421. break;
  422. case '2':
  423. case '3':
  424. /* VAD/CNG, only send two words */
  425. res = 4;
  426. break;
  427. }
  428. p->fr.samples = 240;
  429. p->fr.datalen = res;
  430. p->fr.frametype = AST_FRAME_VOICE;
  431. p->fr.subclass = p->lastinput;
  432. p->fr.offset = AST_FRIENDLY_OFFSET;
  433. /* Byteswap from little-endian to native-endian */
  434. if (p->fr.subclass == AST_FORMAT_SLINEAR)
  435. ast_frame_byteswap_le(&p->fr);
  436. return &p->fr;
  437. }
  438. static int phone_write_buf(struct phone_pvt *p, char *buf, int len, int frlen, int swap)
  439. {
  440. int res;
  441. /* Store as much of the buffer as we can, then write fixed frames */
  442. int space = sizeof(p->obuf) - p->obuflen;
  443. /* Make sure we have enough buffer space to store the frame */
  444. if (space < len)
  445. len = space;
  446. if (swap)
  447. ast_memcpy_byteswap(p->obuf+p->obuflen, buf, len/2);
  448. else
  449. memcpy(p->obuf + p->obuflen, buf, len);
  450. p->obuflen += len;
  451. while(p->obuflen > frlen) {
  452. res = write(p->fd, p->obuf, frlen);
  453. if (res != frlen) {
  454. if (res < 1) {
  455. /*
  456. * Card is in non-blocking mode now and it works well now, but there are
  457. * lot of messages like this. So, this message is temporarily disabled.
  458. */
  459. return 0;
  460. } else {
  461. ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
  462. }
  463. }
  464. p->obuflen -= frlen;
  465. /* Move memory if necessary */
  466. if (p->obuflen)
  467. memmove(p->obuf, p->obuf + frlen, p->obuflen);
  468. }
  469. return len;
  470. }
  471. static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
  472. {
  473. struct phone_pvt *p = ast->pvt->pvt;
  474. int res;
  475. int maxfr=0;
  476. char *pos;
  477. int sofar;
  478. int expected;
  479. int codecset = 0;
  480. char tmpbuf[4];
  481. /* Write a frame of (presumably voice) data */
  482. if (frame->frametype != AST_FRAME_VOICE) {
  483. if (frame->frametype != AST_FRAME_IMAGE)
  484. ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
  485. return 0;
  486. }
  487. if (!(frame->subclass &
  488. (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW))) {
  489. ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
  490. return -1;
  491. }
  492. #if 0
  493. /* If we're not in up mode, go into up mode now */
  494. if (ast->_state != AST_STATE_UP) {
  495. ast_setstate(ast, AST_STATE_UP);
  496. phone_setup(ast);
  497. }
  498. #else
  499. if (ast->_state != AST_STATE_UP) {
  500. /* Don't try tos end audio on-hook */
  501. return 0;
  502. }
  503. #endif
  504. if (frame->subclass == AST_FORMAT_G723_1) {
  505. if (p->lastformat != AST_FORMAT_G723_1) {
  506. ioctl(p->fd, PHONE_PLAY_STOP);
  507. ioctl(p->fd, PHONE_REC_STOP);
  508. if (ioctl(p->fd, PHONE_PLAY_CODEC, G723_63)) {
  509. ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
  510. return -1;
  511. }
  512. if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
  513. ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
  514. return -1;
  515. }
  516. p->lastformat = AST_FORMAT_G723_1;
  517. p->lastinput = AST_FORMAT_G723_1;
  518. /* Reset output buffer */
  519. p->obuflen = 0;
  520. codecset = 1;
  521. }
  522. if (frame->datalen > 24) {
  523. ast_log(LOG_WARNING, "Frame size too large for G.723.1 (%d bytes)\n", frame->datalen);
  524. return -1;
  525. }
  526. maxfr = 24;
  527. } else if (frame->subclass == AST_FORMAT_SLINEAR) {
  528. if (p->lastformat != AST_FORMAT_SLINEAR) {
  529. ioctl(p->fd, PHONE_PLAY_STOP);
  530. ioctl(p->fd, PHONE_REC_STOP);
  531. if (ioctl(p->fd, PHONE_PLAY_CODEC, LINEAR16)) {
  532. ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
  533. return -1;
  534. }
  535. if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
  536. ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
  537. return -1;
  538. }
  539. p->lastformat = AST_FORMAT_SLINEAR;
  540. p->lastinput = AST_FORMAT_SLINEAR;
  541. codecset = 1;
  542. /* Reset output buffer */
  543. p->obuflen = 0;
  544. }
  545. maxfr = 480;
  546. } else if (frame->subclass == AST_FORMAT_ULAW) {
  547. if (p->lastformat != AST_FORMAT_ULAW) {
  548. ioctl(p->fd, PHONE_PLAY_STOP);
  549. ioctl(p->fd, PHONE_REC_STOP);
  550. if (ioctl(p->fd, PHONE_PLAY_CODEC, ULAW)) {
  551. ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
  552. return -1;
  553. }
  554. if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
  555. ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
  556. return -1;
  557. }
  558. p->lastformat = AST_FORMAT_ULAW;
  559. p->lastinput = AST_FORMAT_ULAW;
  560. codecset = 1;
  561. /* Reset output buffer */
  562. p->obuflen = 0;
  563. }
  564. maxfr = 240;
  565. }
  566. if (codecset) {
  567. ioctl(p->fd, PHONE_REC_DEPTH, 3);
  568. ioctl(p->fd, PHONE_PLAY_DEPTH, 3);
  569. if (ioctl(p->fd, PHONE_PLAY_START)) {
  570. ast_log(LOG_WARNING, "Failed to start playback\n");
  571. return -1;
  572. }
  573. if (ioctl(p->fd, PHONE_REC_START)) {
  574. ast_log(LOG_WARNING, "Failed to start recording\n");
  575. return -1;
  576. }
  577. }
  578. /* If we get here, we have a voice frame of Appropriate data */
  579. sofar = 0;
  580. pos = frame->data;
  581. while(sofar < frame->datalen) {
  582. /* Write in no more than maxfr sized frames */
  583. expected = frame->datalen - sofar;
  584. if (maxfr < expected)
  585. expected = maxfr;
  586. /* XXX Internet Phone Jack does not handle the 4-byte VAD frame properly! XXX
  587. we have to pad it to 24 bytes still. */
  588. if (frame->datalen == 4) {
  589. if (p->silencesupression) {
  590. memset(tmpbuf + 4, 0, sizeof(tmpbuf) - 4);
  591. memcpy(tmpbuf, frame->data, 4);
  592. expected = 24;
  593. res = phone_write_buf(p, tmpbuf, expected, maxfr, 0);
  594. }
  595. res = 4;
  596. expected=4;
  597. } else {
  598. int swap = 0;
  599. #if __BYTE_ORDER == __BIG_ENDIAN
  600. if (frame->subclass == AST_FORMAT_SLINEAR)
  601. swap = 1; /* Swap big-endian samples to little-endian as we copy */
  602. #endif
  603. res = phone_write_buf(p, pos, expected, maxfr, swap);
  604. }
  605. if (res != expected) {
  606. if ((errno != EAGAIN) && (errno != EINTR)) {
  607. if (res < 0)
  608. ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
  609. /*
  610. * Card is in non-blocking mode now and it works well now, but there are
  611. * lot of messages like this. So, this message is temporarily disabled.
  612. */
  613. #if 0
  614. else
  615. ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
  616. #endif
  617. return -1;
  618. } else /* Pretend it worked */
  619. res = expected;
  620. }
  621. sofar += res;
  622. pos += res;
  623. }
  624. return 0;
  625. }
  626. static int phone_fixup(struct ast_channel *old, struct ast_channel *new)
  627. {
  628. struct phone_pvt *pvt = old->pvt->pvt;
  629. if (pvt && pvt->owner == old)
  630. pvt->owner = new;
  631. return 0;
  632. }
  633. static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *context)
  634. {
  635. struct ast_channel *tmp;
  636. tmp = ast_channel_alloc(1);
  637. if (tmp) {
  638. snprintf(tmp->name, sizeof(tmp->name), "Phone/%s", i->dev + 5);
  639. tmp->type = type;
  640. tmp->fds[0] = i->fd;
  641. /* XXX Switching formats silently causes kernel panics XXX */
  642. tmp->nativeformats = prefformat;
  643. tmp->pvt->rawreadformat = prefformat;
  644. tmp->pvt->rawwriteformat = prefformat;
  645. ast_setstate(tmp, state);
  646. if (state == AST_STATE_RING)
  647. tmp->rings = 1;
  648. tmp->pvt->pvt = i;
  649. tmp->pvt->send_digit = phone_digit;
  650. tmp->pvt->call = phone_call;
  651. tmp->pvt->hangup = phone_hangup;
  652. tmp->pvt->answer = phone_answer;
  653. tmp->pvt->read = phone_read;
  654. tmp->pvt->write = phone_write;
  655. tmp->pvt->exception = phone_exception;
  656. tmp->pvt->fixup = phone_fixup;
  657. strncpy(tmp->context, context, sizeof(tmp->context)-1);
  658. if (strlen(i->ext))
  659. strncpy(tmp->exten, i->ext, sizeof(tmp->exten)-1);
  660. else
  661. strncpy(tmp->exten, "s", sizeof(tmp->exten) - 1);
  662. if (strlen(i->language))
  663. strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
  664. if (strlen(i->callerid))
  665. tmp->callerid = strdup(i->callerid);
  666. i->owner = tmp;
  667. ast_mutex_lock(&usecnt_lock);
  668. usecnt++;
  669. ast_mutex_unlock(&usecnt_lock);
  670. ast_update_use_count();
  671. if (state != AST_STATE_DOWN) {
  672. if (state == AST_STATE_RING) {
  673. ioctl(tmp->fds[0], PHONE_RINGBACK);
  674. i->cpt = 1;
  675. }
  676. if (ast_pbx_start(tmp)) {
  677. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
  678. ast_hangup(tmp);
  679. }
  680. }
  681. } else
  682. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  683. return tmp;
  684. }
  685. static void phone_mini_packet(struct phone_pvt *i)
  686. {
  687. int res;
  688. char buf[1024];
  689. /* Ignore stuff we read... */
  690. res = read(i->fd, buf, sizeof(buf));
  691. if (res < 1) {
  692. ast_log(LOG_WARNING, "Read returned %d: %s\n", res, strerror(errno));
  693. return;
  694. }
  695. }
  696. static void phone_check_exception(struct phone_pvt *i)
  697. {
  698. int offhook=0;
  699. char digit[2] = {0 , 0};
  700. union telephony_exception phonee;
  701. /* XXX Do something XXX */
  702. #if 0
  703. ast_log(LOG_DEBUG, "Exception!\n");
  704. #endif
  705. phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION);
  706. if (phonee.bits.dtmf_ready) {
  707. digit[0] = ioctl(i->fd, PHONE_GET_DTMF_ASCII);
  708. if (i->mode == MODE_DIALTONE) {
  709. ioctl(i->fd, PHONE_PLAY_STOP);
  710. ioctl(i->fd, PHONE_REC_STOP);
  711. ioctl(i->fd, PHONE_CPT_STOP);
  712. i->dialtone = 0;
  713. if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
  714. strncat(i->ext, digit, sizeof(i->ext) - strlen(i->ext) - 1);
  715. if (ast_exists_extension(NULL, i->context, i->ext, 1, i->callerid)) {
  716. /* It's a valid extension in its context, get moving! */
  717. phone_new(i, AST_STATE_RING, i->context);
  718. /* No need to restart monitor, we are the monitor */
  719. if (i->owner) {
  720. ast_mutex_lock(&usecnt_lock);
  721. usecnt--;
  722. ast_mutex_unlock(&usecnt_lock);
  723. ast_update_use_count();
  724. }
  725. } else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->callerid)) {
  726. /* There is nothing in the specified extension that can match anymore.
  727. Try the default */
  728. if (ast_exists_extension(NULL, "default", i->ext, 1, i->callerid)) {
  729. /* Check the default, too... */
  730. phone_new(i, AST_STATE_RING, "default");
  731. if (i->owner) {
  732. ast_mutex_lock(&usecnt_lock);
  733. usecnt--;
  734. ast_mutex_unlock(&usecnt_lock);
  735. ast_update_use_count();
  736. }
  737. /* XXX This should probably be justified better XXX */
  738. } else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->callerid)) {
  739. /* It's not a valid extension, give a busy signal */
  740. if (option_debug)
  741. ast_log(LOG_DEBUG, "%s can't match anything in %s or default\n", i->ext, i->context);
  742. ioctl(i->fd, PHONE_BUSY);
  743. i->cpt = 1;
  744. }
  745. }
  746. #if 0
  747. ast_verbose("Extension is %s\n", i->ext);
  748. #endif
  749. }
  750. }
  751. if (phonee.bits.hookstate) {
  752. offhook = ioctl(i->fd, PHONE_HOOKSTATE);
  753. if (offhook) {
  754. if (i->mode == MODE_IMMEDIATE) {
  755. phone_new(i, AST_STATE_RING, i->context);
  756. } else if (i->mode == MODE_DIALTONE) {
  757. ast_mutex_lock(&usecnt_lock);
  758. usecnt++;
  759. ast_mutex_unlock(&usecnt_lock);
  760. ast_update_use_count();
  761. /* Reset the extension */
  762. i->ext[0] = '\0';
  763. /* Play the dialtone */
  764. i->dialtone++;
  765. ioctl(i->fd, PHONE_PLAY_STOP);
  766. ioctl(i->fd, PHONE_PLAY_CODEC, ULAW);
  767. ioctl(i->fd, PHONE_PLAY_START);
  768. i->lastformat = -1;
  769. }
  770. } else {
  771. if (i->dialtone) {
  772. ast_mutex_lock(&usecnt_lock);
  773. usecnt--;
  774. ast_mutex_unlock(&usecnt_lock);
  775. ast_update_use_count();
  776. }
  777. memset(i->ext, 0, sizeof(i->ext));
  778. if (i->cpt)
  779. {
  780. ioctl(i->fd, PHONE_CPT_STOP);
  781. i->cpt = 0;
  782. }
  783. ioctl(i->fd, PHONE_PLAY_STOP);
  784. ioctl(i->fd, PHONE_REC_STOP);
  785. i->dialtone = 0;
  786. i->lastformat = -1;
  787. }
  788. }
  789. if (phonee.bits.pstn_ring) {
  790. ast_verbose("Unit is ringing\n");
  791. phone_new(i, AST_STATE_RING, i->context);
  792. }
  793. if (phonee.bits.caller_id)
  794. ast_verbose("We have caller ID\n");
  795. }
  796. static void *do_monitor(void *data)
  797. {
  798. fd_set rfds, efds;
  799. int n, res;
  800. struct phone_pvt *i;
  801. int tonepos = 0;
  802. /* The tone we're playing this round */
  803. struct timeval tv = {0,0};
  804. int dotone;
  805. /* This thread monitors all the frame relay interfaces which are not yet in use
  806. (and thus do not have a separate thread) indefinitely */
  807. /* From here on out, we die whenever asked */
  808. if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
  809. ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
  810. return NULL;
  811. }
  812. for(;;) {
  813. /* Don't let anybody kill us right away. Nobody should lock the interface list
  814. and wait for the monitor list, but the other way around is okay. */
  815. if (ast_mutex_lock(&monlock)) {
  816. ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
  817. return NULL;
  818. }
  819. /* Lock the interface list */
  820. if (ast_mutex_lock(&iflock)) {
  821. ast_log(LOG_ERROR, "Unable to grab interface lock\n");
  822. ast_mutex_unlock(&monlock);
  823. return NULL;
  824. }
  825. /* Build the stuff we're going to select on, that is the socket of every
  826. phone_pvt that does not have an associated owner channel */
  827. n = -1;
  828. FD_ZERO(&rfds);
  829. FD_ZERO(&efds);
  830. i = iflist;
  831. dotone = 0;
  832. while(i) {
  833. if (FD_ISSET(i->fd, &rfds))
  834. ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->fd, i->dev);
  835. if (!i->owner) {
  836. /* This needs to be watched, as it lacks an owner */
  837. FD_SET(i->fd, &rfds);
  838. FD_SET(i->fd, &efds);
  839. if (i->fd > n)
  840. n = i->fd;
  841. if (i->dialtone) {
  842. /* Remember we're going to have to come back and play
  843. more dialtones */
  844. if (!tv.tv_usec && !tv.tv_sec) {
  845. /* If we're due for a dialtone, play one */
  846. if (write(i->fd, DialTone + tonepos, 240) != 240)
  847. ast_log(LOG_WARNING, "Dial tone write error\n");
  848. }
  849. dotone++;
  850. }
  851. }
  852. i = i->next;
  853. }
  854. /* Okay, now that we know what to do, release the interface lock */
  855. ast_mutex_unlock(&iflock);
  856. /* And from now on, we're okay to be killed, so release the monitor lock as well */
  857. ast_mutex_unlock(&monlock);
  858. /* Wait indefinitely for something to happen */
  859. if (dotone) {
  860. /* If we're ready to recycle the time, set it to 30 ms */
  861. tonepos += 240;
  862. if (tonepos >= sizeof(DialTone))
  863. tonepos = 0;
  864. if (!tv.tv_usec && !tv.tv_sec) {
  865. tv.tv_usec = 30000;
  866. tv.tv_sec = 0;
  867. }
  868. res = ast_select(n + 1, &rfds, NULL, &efds, &tv);
  869. } else {
  870. res = ast_select(n + 1, &rfds, NULL, &efds, NULL);
  871. tv.tv_usec = 0;
  872. tv.tv_sec = 0;
  873. tonepos = 0;
  874. }
  875. /* Okay, select has finished. Let's see what happened. */
  876. if (res < 0) {
  877. ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
  878. continue;
  879. }
  880. /* If there are no fd's changed, just continue, it's probably time
  881. to play some more dialtones */
  882. if (!res)
  883. continue;
  884. /* Alright, lock the interface list again, and let's look and see what has
  885. happened */
  886. if (ast_mutex_lock(&iflock)) {
  887. ast_log(LOG_WARNING, "Unable to lock the interface list\n");
  888. continue;
  889. }
  890. i = iflist;
  891. while(i) {
  892. if (FD_ISSET(i->fd, &rfds)) {
  893. if (i->owner) {
  894. ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d, %s)...\n", i->fd, i->dev);
  895. continue;
  896. }
  897. phone_mini_packet(i);
  898. }
  899. if (FD_ISSET(i->fd, &efds)) {
  900. if (i->owner) {
  901. ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d, %s)...\n", i->fd, i->dev);
  902. continue;
  903. }
  904. phone_check_exception(i);
  905. }
  906. i=i->next;
  907. }
  908. ast_mutex_unlock(&iflock);
  909. }
  910. /* Never reached */
  911. return NULL;
  912. }
  913. static int restart_monitor()
  914. {
  915. /* If we're supposed to be stopped -- stay stopped */
  916. if (monitor_thread == AST_PTHREADT_STOP)
  917. return 0;
  918. if (ast_mutex_lock(&monlock)) {
  919. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  920. return -1;
  921. }
  922. if (monitor_thread == pthread_self()) {
  923. ast_mutex_unlock(&monlock);
  924. ast_log(LOG_WARNING, "Cannot kill myself\n");
  925. return -1;
  926. }
  927. if (monitor_thread != AST_PTHREADT_NULL) {
  928. pthread_cancel(monitor_thread);
  929. #if 0
  930. pthread_join(monitor_thread, NULL);
  931. #endif
  932. }
  933. /* Start a new monitor */
  934. if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
  935. ast_mutex_unlock(&monlock);
  936. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  937. return -1;
  938. }
  939. ast_mutex_unlock(&monlock);
  940. return 0;
  941. }
  942. static struct phone_pvt *mkif(char *iface, int mode, int txgain, int rxgain)
  943. {
  944. /* Make a phone_pvt structure for this interface */
  945. struct phone_pvt *tmp;
  946. int flags;
  947. tmp = malloc(sizeof(struct phone_pvt));
  948. if (tmp) {
  949. tmp->fd = open(iface, O_RDWR);
  950. if (tmp->fd < 0) {
  951. ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
  952. free(tmp);
  953. return NULL;
  954. }
  955. if (mode == MODE_FXO) {
  956. if (ioctl(tmp->fd, IXJCTL_PORT, PORT_PSTN))
  957. ast_log(LOG_DEBUG, "Unable to set port to PSTN\n");
  958. } else {
  959. if (ioctl(tmp->fd, IXJCTL_PORT, PORT_POTS))
  960. ast_log(LOG_DEBUG, "Unable to set port to POTS\n");
  961. }
  962. ioctl(tmp->fd, PHONE_PLAY_STOP);
  963. ioctl(tmp->fd, PHONE_REC_STOP);
  964. ioctl(tmp->fd, PHONE_RING_STOP);
  965. ioctl(tmp->fd, PHONE_CPT_STOP);
  966. if (ioctl(tmp->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK))
  967. ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",iface, strerror(errno));
  968. if (echocancel != AEC_OFF)
  969. ioctl(tmp->fd, IXJCTL_AEC_START, echocancel);
  970. if (silencesupression)
  971. tmp->silencesupression = 1;
  972. #ifdef PHONE_VAD
  973. ioctl(tmp->fd, PHONE_VAD, tmp->silencesupression);
  974. #endif
  975. tmp->mode = mode;
  976. flags = fcntl(tmp->fd, F_GETFL);
  977. fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
  978. tmp->owner = NULL;
  979. tmp->lastformat = -1;
  980. tmp->lastinput = -1;
  981. tmp->ministate = 0;
  982. memset(tmp->ext, 0, sizeof(tmp->ext));
  983. strncpy(tmp->language, language, sizeof(tmp->language)-1);
  984. strncpy(tmp->dev, iface, sizeof(tmp->dev)-1);
  985. strncpy(tmp->context, context, sizeof(tmp->context)-1);
  986. tmp->next = NULL;
  987. tmp->obuflen = 0;
  988. tmp->dialtone = 0;
  989. tmp->cpt = 0;
  990. strncpy(tmp->callerid, callerid, sizeof(tmp->callerid)-1);
  991. tmp->txgain = txgain;
  992. ioctl(tmp->fd, PHONE_PLAY_VOLUME, tmp->txgain);
  993. tmp->rxgain = rxgain;
  994. ioctl(tmp->fd, PHONE_REC_VOLUME, tmp->rxgain);
  995. }
  996. return tmp;
  997. }
  998. static struct ast_channel *phone_request(char *type, int format, void *data)
  999. {
  1000. int oldformat;
  1001. struct phone_pvt *p;
  1002. struct ast_channel *tmp = NULL;
  1003. char *name = data;
  1004. oldformat = format;
  1005. format &= (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
  1006. if (!format) {
  1007. ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
  1008. return NULL;
  1009. }
  1010. /* Search for an unowned channel */
  1011. if (ast_mutex_lock(&iflock)) {
  1012. ast_log(LOG_ERROR, "Unable to lock interface list???\n");
  1013. return NULL;
  1014. }
  1015. p = iflist;
  1016. while(p) {
  1017. if (!strcmp(name, p->dev + 5)) {
  1018. if (!p->owner) {
  1019. tmp = phone_new(p, AST_STATE_DOWN, p->context);
  1020. break;
  1021. }
  1022. }
  1023. p = p->next;
  1024. }
  1025. ast_mutex_unlock(&iflock);
  1026. restart_monitor();
  1027. return tmp;
  1028. }
  1029. /* parse gain value from config file */
  1030. static int parse_gain_value(char *gain_type, char *value)
  1031. {
  1032. float gain;
  1033. /* try to scan number */
  1034. if (sscanf(value, "%f", &gain) != 1)
  1035. {
  1036. ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n",
  1037. value, gain_type, config);
  1038. return DEFAULT_GAIN;
  1039. }
  1040. /* multiplicate gain by 1.0 gain value */
  1041. gain = gain * (float)DEFAULT_GAIN;
  1042. /* percentage? */
  1043. if (value[strlen(value) - 1] == '%')
  1044. return (int)(gain / (float)100);
  1045. return (int)gain;
  1046. }
  1047. static int __unload_module(void)
  1048. {
  1049. struct phone_pvt *p, *pl;
  1050. /* First, take us out of the channel loop */
  1051. ast_channel_unregister(type);
  1052. if (!ast_mutex_lock(&iflock)) {
  1053. /* Hangup all interfaces if they have an owner */
  1054. p = iflist;
  1055. while(p) {
  1056. if (p->owner)
  1057. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  1058. p = p->next;
  1059. }
  1060. iflist = NULL;
  1061. ast_mutex_unlock(&iflock);
  1062. } else {
  1063. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  1064. return -1;
  1065. }
  1066. if (!ast_mutex_lock(&monlock)) {
  1067. if (monitor_thread > AST_PTHREADT_NULL) {
  1068. pthread_cancel(monitor_thread);
  1069. pthread_join(monitor_thread, NULL);
  1070. }
  1071. monitor_thread = AST_PTHREADT_STOP;
  1072. ast_mutex_unlock(&monlock);
  1073. } else {
  1074. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  1075. return -1;
  1076. }
  1077. if (!ast_mutex_lock(&iflock)) {
  1078. /* Destroy all the interfaces and free their memory */
  1079. p = iflist;
  1080. while(p) {
  1081. /* Close the socket, assuming it's real */
  1082. if (p->fd > -1)
  1083. close(p->fd);
  1084. pl = p;
  1085. p = p->next;
  1086. /* Free associated memory */
  1087. free(pl);
  1088. }
  1089. iflist = NULL;
  1090. ast_mutex_unlock(&iflock);
  1091. } else {
  1092. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  1093. return -1;
  1094. }
  1095. return 0;
  1096. }
  1097. int unload_module(void)
  1098. {
  1099. return __unload_module();
  1100. }
  1101. int load_module()
  1102. {
  1103. struct ast_config *cfg;
  1104. struct ast_variable *v;
  1105. struct phone_pvt *tmp;
  1106. int mode = MODE_IMMEDIATE;
  1107. int txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN; /* default gain 1.0 */
  1108. cfg = ast_load(config);
  1109. /* We *must* have a config file otherwise stop immediately */
  1110. if (!cfg) {
  1111. ast_log(LOG_ERROR, "Unable to load config %s\n", config);
  1112. return -1;
  1113. }
  1114. if (ast_mutex_lock(&iflock)) {
  1115. /* It's a little silly to lock it, but we mind as well just to be sure */
  1116. ast_log(LOG_ERROR, "Unable to lock interface list???\n");
  1117. return -1;
  1118. }
  1119. v = ast_variable_browse(cfg, "interfaces");
  1120. while(v) {
  1121. /* Create the interface list */
  1122. if (!strcasecmp(v->name, "device")) {
  1123. tmp = mkif(v->value, mode, txgain, rxgain);
  1124. if (tmp) {
  1125. tmp->next = iflist;
  1126. iflist = tmp;
  1127. } else {
  1128. ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
  1129. ast_destroy(cfg);
  1130. ast_mutex_unlock(&iflock);
  1131. __unload_module();
  1132. return -1;
  1133. }
  1134. } else if (!strcasecmp(v->name, "silencesupression")) {
  1135. silencesupression = ast_true(v->value);
  1136. } else if (!strcasecmp(v->name, "language")) {
  1137. strncpy(language, v->value, sizeof(language)-1);
  1138. } else if (!strcasecmp(v->name, "callerid")) {
  1139. strncpy(callerid, v->value, sizeof(callerid)-1);
  1140. } else if (!strcasecmp(v->name, "mode")) {
  1141. if (!strncasecmp(v->value, "di", 2))
  1142. mode = MODE_DIALTONE;
  1143. else if (!strncasecmp(v->value, "im", 2))
  1144. mode = MODE_IMMEDIATE;
  1145. else if (!strncasecmp(v->value, "fx", 2))
  1146. mode = MODE_FXO;
  1147. else
  1148. ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
  1149. } else if (!strcasecmp(v->name, "context")) {
  1150. strncpy(context, v->value, sizeof(context)-1);
  1151. } else if (!strcasecmp(v->name, "format")) {
  1152. if (!strcasecmp(v->value, "g723.1")) {
  1153. prefformat = AST_FORMAT_G723_1;
  1154. } else if (!strcasecmp(v->value, "slinear")) {
  1155. prefformat = AST_FORMAT_SLINEAR;
  1156. } else if (!strcasecmp(v->value, "ulaw")) {
  1157. prefformat = AST_FORMAT_ULAW;
  1158. } else
  1159. ast_log(LOG_WARNING, "Unknown format '%s'\n", v->value);
  1160. } else if (!strcasecmp(v->name, "echocancel")) {
  1161. if (!strcasecmp(v->value, "off")) {
  1162. echocancel = AEC_OFF;
  1163. } else if (!strcasecmp(v->value, "low")) {
  1164. echocancel = AEC_LOW;
  1165. } else if (!strcasecmp(v->value, "medium")) {
  1166. echocancel = AEC_MED;
  1167. } else if (!strcasecmp(v->value, "high")) {
  1168. echocancel = AEC_HIGH;
  1169. } else
  1170. ast_log(LOG_WARNING, "Unknown echo cancellation '%s'\n", v->value);
  1171. } else if (!strcasecmp(v->name, "txgain")) {
  1172. txgain = parse_gain_value(v->name, v->value);
  1173. } else if (!strcasecmp(v->name, "rxgain")) {
  1174. rxgain = parse_gain_value(v->name, v->value);
  1175. }
  1176. v = v->next;
  1177. }
  1178. ast_mutex_unlock(&iflock);
  1179. /* Make sure we can register our Adtranphone channel type */
  1180. if (ast_channel_register(type, tdesc,
  1181. AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW, phone_request)) {
  1182. ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
  1183. ast_destroy(cfg);
  1184. __unload_module();
  1185. return -1;
  1186. }
  1187. ast_destroy(cfg);
  1188. /* And start the monitor for the first time */
  1189. restart_monitor();
  1190. return 0;
  1191. }
  1192. int usecount()
  1193. {
  1194. int res;
  1195. ast_mutex_lock(&usecnt_lock);
  1196. res = usecnt;
  1197. ast_mutex_unlock(&usecnt_lock);
  1198. return res;
  1199. }
  1200. char *description()
  1201. {
  1202. return desc;
  1203. }
  1204. char *key()
  1205. {
  1206. return ASTERISK_GPL_KEY;
  1207. }