chan_phone.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Generic Linux Telephony Interface driver
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup channel_drivers
  25. */
  26. /*** MODULEINFO
  27. <depend>ixjuser</depend>
  28. <support_level>extended</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include <ctype.h>
  33. #include <sys/socket.h>
  34. #include <sys/time.h>
  35. #include <arpa/inet.h>
  36. #include <fcntl.h>
  37. #include <sys/ioctl.h>
  38. #include <signal.h>
  39. #ifdef HAVE_LINUX_COMPILER_H
  40. #include <linux/compiler.h>
  41. #endif
  42. #include <linux/telephony.h>
  43. /* Still use some IXJ specific stuff */
  44. #include <linux/version.h>
  45. #include <linux/ixjuser.h>
  46. #include "asterisk/lock.h"
  47. #include "asterisk/channel.h"
  48. #include "asterisk/config.h"
  49. #include "asterisk/module.h"
  50. #include "asterisk/pbx.h"
  51. #include "asterisk/utils.h"
  52. #include "asterisk/callerid.h"
  53. #include "asterisk/causes.h"
  54. #include "asterisk/stringfields.h"
  55. #include "asterisk/musiconhold.h"
  56. #include "chan_phone.h"
  57. #ifdef QTI_PHONEJACK_TJ_PCI /* check for the newer quicknet driver v.3.1.0 which has this symbol */
  58. #define QNDRV_VER 310
  59. #else
  60. #define QNDRV_VER 100
  61. #endif
  62. #if QNDRV_VER > 100
  63. #ifdef __linux__
  64. #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
  65. #else /* FreeBSD and others */
  66. #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, x);
  67. #endif /* __linux__ */
  68. #else /* older driver */
  69. #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
  70. #endif
  71. #define DEFAULT_CALLER_ID "Unknown"
  72. #define PHONE_MAX_BUF 480
  73. #define DEFAULT_GAIN 0x100
  74. static const char tdesc[] = "Standard Linux Telephony API Driver";
  75. static const char config[] = "phone.conf";
  76. /* Default context for dialtone mode */
  77. static char context[AST_MAX_EXTENSION] = "default";
  78. /* Default language */
  79. static char language[MAX_LANGUAGE] = "";
  80. static int echocancel = AEC_OFF;
  81. static int silencesupression = 0;
  82. static struct ast_format_cap *prefcap;
  83. /* Protect the interface list (of phone_pvt's) */
  84. AST_MUTEX_DEFINE_STATIC(iflock);
  85. /* Protect the monitoring thread, so only one process can kill or start it, and not
  86. when it's doing something critical. */
  87. AST_MUTEX_DEFINE_STATIC(monlock);
  88. /* Boolean value whether the monitoring thread shall continue. */
  89. static unsigned int monitor;
  90. /* This is the thread for the monitor which checks for input on the channels
  91. which are not currently in use. */
  92. static pthread_t monitor_thread = AST_PTHREADT_NULL;
  93. static int restart_monitor(void);
  94. /* The private structures of the Phone Jack channels are linked for
  95. selecting outgoing channels */
  96. #define MODE_DIALTONE 1
  97. #define MODE_IMMEDIATE 2
  98. #define MODE_FXO 3
  99. #define MODE_FXS 4
  100. #define MODE_SIGMA 5
  101. static struct phone_pvt {
  102. int fd; /* Raw file descriptor for this device */
  103. struct ast_channel *owner; /* Channel we belong to, possibly NULL */
  104. int mode; /* Is this in the */
  105. struct ast_format lastformat; /* Last output format */
  106. struct ast_format lastinput; /* Last input format */
  107. int ministate; /* Miniature state, for dialtone mode */
  108. char dev[256]; /* Device name */
  109. struct phone_pvt *next; /* Next channel in list */
  110. struct ast_frame fr; /* Frame */
  111. char offset[AST_FRIENDLY_OFFSET];
  112. char buf[PHONE_MAX_BUF]; /* Static buffer for reading frames */
  113. int obuflen;
  114. int dialtone;
  115. int txgain, rxgain; /* gain control for playing, recording */
  116. /* 0x100 - 1.0, 0x200 - 2.0, 0x80 - 0.5 */
  117. int cpt; /* Call Progress Tone playing? */
  118. int silencesupression;
  119. char context[AST_MAX_EXTENSION];
  120. char obuf[PHONE_MAX_BUF * 2];
  121. char ext[AST_MAX_EXTENSION];
  122. char language[MAX_LANGUAGE];
  123. char cid_num[AST_MAX_EXTENSION];
  124. char cid_name[AST_MAX_EXTENSION];
  125. } *iflist = NULL;
  126. static char cid_num[AST_MAX_EXTENSION];
  127. static char cid_name[AST_MAX_EXTENSION];
  128. static struct ast_channel *phone_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause);
  129. static int phone_digit_begin(struct ast_channel *ast, char digit);
  130. static int phone_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
  131. static int phone_call(struct ast_channel *ast, const char *dest, int timeout);
  132. static int phone_hangup(struct ast_channel *ast);
  133. static int phone_answer(struct ast_channel *ast);
  134. static struct ast_frame *phone_read(struct ast_channel *ast);
  135. static int phone_write(struct ast_channel *ast, struct ast_frame *frame);
  136. static struct ast_frame *phone_exception(struct ast_channel *ast);
  137. static int phone_send_text(struct ast_channel *ast, const char *text);
  138. static int phone_fixup(struct ast_channel *old, struct ast_channel *new);
  139. static int phone_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
  140. static struct ast_channel_tech phone_tech = {
  141. .type = "Phone",
  142. .description = tdesc,
  143. .requester = phone_request,
  144. .send_digit_begin = phone_digit_begin,
  145. .send_digit_end = phone_digit_end,
  146. .call = phone_call,
  147. .hangup = phone_hangup,
  148. .answer = phone_answer,
  149. .read = phone_read,
  150. .write = phone_write,
  151. .exception = phone_exception,
  152. .indicate = phone_indicate,
  153. .fixup = phone_fixup
  154. };
  155. static struct ast_channel_tech phone_tech_fxs = {
  156. .type = "Phone",
  157. .description = tdesc,
  158. .requester = phone_request,
  159. .send_digit_begin = phone_digit_begin,
  160. .send_digit_end = phone_digit_end,
  161. .call = phone_call,
  162. .hangup = phone_hangup,
  163. .answer = phone_answer,
  164. .read = phone_read,
  165. .write = phone_write,
  166. .exception = phone_exception,
  167. .write_video = phone_write,
  168. .send_text = phone_send_text,
  169. .indicate = phone_indicate,
  170. .fixup = phone_fixup
  171. };
  172. static struct ast_channel_tech *cur_tech;
  173. static int phone_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
  174. {
  175. struct phone_pvt *p = ast_channel_tech_pvt(chan);
  176. int res=-1;
  177. ast_debug(1, "Requested indication %d on channel %s\n", condition, ast_channel_name(chan));
  178. switch(condition) {
  179. case AST_CONTROL_FLASH:
  180. ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
  181. usleep(320000);
  182. ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
  183. ast_format_clear(&p->lastformat);
  184. res = 0;
  185. break;
  186. case AST_CONTROL_HOLD:
  187. ast_moh_start(chan, data, NULL);
  188. break;
  189. case AST_CONTROL_UNHOLD:
  190. ast_moh_stop(chan);
  191. break;
  192. case AST_CONTROL_SRCUPDATE:
  193. res = 0;
  194. break;
  195. case AST_CONTROL_PVT_CAUSE_CODE:
  196. break;
  197. default:
  198. ast_log(LOG_WARNING, "Condition %d is not supported on channel %s\n", condition, ast_channel_name(chan));
  199. }
  200. return res;
  201. }
  202. static int phone_fixup(struct ast_channel *old, struct ast_channel *new)
  203. {
  204. struct phone_pvt *pvt = ast_channel_tech_pvt(old);
  205. if (pvt && pvt->owner == old)
  206. pvt->owner = new;
  207. return 0;
  208. }
  209. static int phone_digit_begin(struct ast_channel *chan, char digit)
  210. {
  211. /* XXX Modify this callback to let Asterisk support controlling the length of DTMF */
  212. return 0;
  213. }
  214. static int phone_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  215. {
  216. struct phone_pvt *p;
  217. int outdigit;
  218. p = ast_channel_tech_pvt(ast);
  219. ast_debug(1, "Dialed %c\n", digit);
  220. switch(digit) {
  221. case '0':
  222. case '1':
  223. case '2':
  224. case '3':
  225. case '4':
  226. case '5':
  227. case '6':
  228. case '7':
  229. case '8':
  230. case '9':
  231. outdigit = digit - '0';
  232. break;
  233. case '*':
  234. outdigit = 11;
  235. break;
  236. case '#':
  237. outdigit = 12;
  238. break;
  239. case 'f': /*flash*/
  240. case 'F':
  241. ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
  242. usleep(320000);
  243. ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
  244. ast_format_clear(&p->lastformat);
  245. return 0;
  246. default:
  247. ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
  248. return -1;
  249. }
  250. ast_debug(1, "Dialed %d\n", outdigit);
  251. ioctl(p->fd, PHONE_PLAY_TONE, outdigit);
  252. ast_format_clear(&p->lastformat);
  253. return 0;
  254. }
  255. static int phone_call(struct ast_channel *ast, const char *dest, int timeout)
  256. {
  257. struct phone_pvt *p;
  258. PHONE_CID cid;
  259. struct timeval UtcTime = ast_tvnow();
  260. struct ast_tm tm;
  261. int start;
  262. ast_localtime(&UtcTime, &tm, NULL);
  263. memset(&cid, 0, sizeof(PHONE_CID));
  264. snprintf(cid.month, sizeof(cid.month), "%02d",(tm.tm_mon + 1));
  265. snprintf(cid.day, sizeof(cid.day), "%02d", tm.tm_mday);
  266. snprintf(cid.hour, sizeof(cid.hour), "%02d", tm.tm_hour);
  267. snprintf(cid.min, sizeof(cid.min), "%02d", tm.tm_min);
  268. /* the standard format of ast->callerid is: "name" <number>, but not always complete */
  269. if (!ast_channel_connected(ast)->id.name.valid
  270. || ast_strlen_zero(ast_channel_connected(ast)->id.name.str)) {
  271. strcpy(cid.name, DEFAULT_CALLER_ID);
  272. } else {
  273. ast_copy_string(cid.name, ast_channel_connected(ast)->id.name.str, sizeof(cid.name));
  274. }
  275. if (ast_channel_connected(ast)->id.number.valid && ast_channel_connected(ast)->id.number.str) {
  276. ast_copy_string(cid.number, ast_channel_connected(ast)->id.number.str, sizeof(cid.number));
  277. }
  278. p = ast_channel_tech_pvt(ast);
  279. if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
  280. ast_log(LOG_WARNING, "phone_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
  281. return -1;
  282. }
  283. ast_debug(1, "Ringing %s on %s (%d)\n", dest, ast_channel_name(ast), ast_channel_fd(ast, 0));
  284. start = IXJ_PHONE_RING_START(cid);
  285. if (start == -1)
  286. return -1;
  287. if (p->mode == MODE_FXS) {
  288. const char *digit = strchr(dest, '/');
  289. if (digit)
  290. {
  291. digit++;
  292. while (*digit)
  293. phone_digit_end(ast, *digit++, 0);
  294. }
  295. }
  296. ast_setstate(ast, AST_STATE_RINGING);
  297. ast_queue_control(ast, AST_CONTROL_RINGING);
  298. return 0;
  299. }
  300. static int phone_hangup(struct ast_channel *ast)
  301. {
  302. struct phone_pvt *p;
  303. p = ast_channel_tech_pvt(ast);
  304. ast_debug(1, "phone_hangup(%s)\n", ast_channel_name(ast));
  305. if (!ast_channel_tech_pvt(ast)) {
  306. ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
  307. return 0;
  308. }
  309. /* XXX Is there anything we can do to really hang up except stop recording? */
  310. ast_setstate(ast, AST_STATE_DOWN);
  311. if (ioctl(p->fd, PHONE_REC_STOP))
  312. ast_log(LOG_WARNING, "Failed to stop recording\n");
  313. if (ioctl(p->fd, PHONE_PLAY_STOP))
  314. ast_log(LOG_WARNING, "Failed to stop playing\n");
  315. if (ioctl(p->fd, PHONE_RING_STOP))
  316. ast_log(LOG_WARNING, "Failed to stop ringing\n");
  317. if (ioctl(p->fd, PHONE_CPT_STOP))
  318. ast_log(LOG_WARNING, "Failed to stop sounds\n");
  319. /* If it's an FXO, hang them up */
  320. if (p->mode == MODE_FXO) {
  321. if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK))
  322. ast_debug(1, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",ast_channel_name(ast), strerror(errno));
  323. }
  324. /* If they're off hook, give a busy signal */
  325. if (ioctl(p->fd, PHONE_HOOKSTATE)) {
  326. ast_debug(1, "Got hunghup, giving busy signal\n");
  327. ioctl(p->fd, PHONE_BUSY);
  328. p->cpt = 1;
  329. }
  330. ast_format_clear(&p->lastformat);
  331. ast_format_clear(&p->lastinput);
  332. p->ministate = 0;
  333. p->obuflen = 0;
  334. p->dialtone = 0;
  335. memset(p->ext, 0, sizeof(p->ext));
  336. ((struct phone_pvt *)(ast_channel_tech_pvt(ast)))->owner = NULL;
  337. ast_module_unref(ast_module_info->self);
  338. ast_verb(3, "Hungup '%s'\n", ast_channel_name(ast));
  339. ast_channel_tech_pvt_set(ast, NULL);
  340. ast_setstate(ast, AST_STATE_DOWN);
  341. restart_monitor();
  342. return 0;
  343. }
  344. static int phone_setup(struct ast_channel *ast)
  345. {
  346. struct phone_pvt *p;
  347. p = ast_channel_tech_pvt(ast);
  348. ioctl(p->fd, PHONE_CPT_STOP);
  349. /* Nothing to answering really, just start recording */
  350. if (ast_channel_rawreadformat(ast)->id == AST_FORMAT_G729A) {
  351. /* Prefer g729 */
  352. ioctl(p->fd, PHONE_REC_STOP);
  353. if (p->lastinput.id != AST_FORMAT_G729A) {
  354. ast_format_set(&p->lastinput, AST_FORMAT_G729A, 0);
  355. if (ioctl(p->fd, PHONE_REC_CODEC, G729)) {
  356. ast_log(LOG_WARNING, "Failed to set codec to g729\n");
  357. return -1;
  358. }
  359. }
  360. } else if (ast_channel_rawreadformat(ast)->id == AST_FORMAT_G723_1) {
  361. ioctl(p->fd, PHONE_REC_STOP);
  362. if (p->lastinput.id != AST_FORMAT_G723_1) {
  363. ast_format_set(&p->lastinput, AST_FORMAT_G723_1, 0);
  364. if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
  365. ast_log(LOG_WARNING, "Failed to set codec to g723.1\n");
  366. return -1;
  367. }
  368. }
  369. } else if (ast_channel_rawreadformat(ast)->id == AST_FORMAT_SLINEAR) {
  370. ioctl(p->fd, PHONE_REC_STOP);
  371. if (p->lastinput.id != AST_FORMAT_SLINEAR) {
  372. ast_format_set(&p->lastinput, AST_FORMAT_SLINEAR, 0);
  373. if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
  374. ast_log(LOG_WARNING, "Failed to set codec to signed linear 16\n");
  375. return -1;
  376. }
  377. }
  378. } else if (ast_channel_rawreadformat(ast)->id == AST_FORMAT_ULAW) {
  379. ioctl(p->fd, PHONE_REC_STOP);
  380. if (p->lastinput.id != AST_FORMAT_ULAW) {
  381. ast_format_set(&p->lastinput, AST_FORMAT_ULAW, 0);
  382. if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
  383. ast_log(LOG_WARNING, "Failed to set codec to uLaw\n");
  384. return -1;
  385. }
  386. }
  387. } else if (p->mode == MODE_FXS) {
  388. ioctl(p->fd, PHONE_REC_STOP);
  389. if (ast_format_cmp(&p->lastinput, ast_channel_rawreadformat(ast)) == AST_FORMAT_CMP_NOT_EQUAL) {
  390. ast_format_copy(&p->lastinput, ast_channel_rawreadformat(ast));
  391. if (ioctl(p->fd, PHONE_REC_CODEC, ast_channel_rawreadformat(ast))) {
  392. ast_log(LOG_WARNING, "Failed to set codec to %s\n",
  393. ast_getformatname(ast_channel_rawreadformat(ast)));
  394. return -1;
  395. }
  396. }
  397. } else {
  398. ast_log(LOG_WARNING, "Can't do format %s\n", ast_getformatname(ast_channel_rawreadformat(ast)));
  399. return -1;
  400. }
  401. if (ioctl(p->fd, PHONE_REC_START)) {
  402. ast_log(LOG_WARNING, "Failed to start recording\n");
  403. return -1;
  404. }
  405. /* set the DTMF times (the default is too short) */
  406. ioctl(p->fd, PHONE_SET_TONE_ON_TIME, 300);
  407. ioctl(p->fd, PHONE_SET_TONE_OFF_TIME, 200);
  408. return 0;
  409. }
  410. static int phone_answer(struct ast_channel *ast)
  411. {
  412. struct phone_pvt *p;
  413. p = ast_channel_tech_pvt(ast);
  414. /* In case it's a LineJack, take it off hook */
  415. if (p->mode == MODE_FXO) {
  416. if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_OFF_HOOK))
  417. ast_debug(1, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n", ast_channel_name(ast), strerror(errno));
  418. else
  419. ast_debug(1, "Took linejack off hook\n");
  420. }
  421. phone_setup(ast);
  422. ast_debug(1, "phone_answer(%s)\n", ast_channel_name(ast));
  423. ast_channel_rings_set(ast, 0);
  424. ast_setstate(ast, AST_STATE_UP);
  425. return 0;
  426. }
  427. #if 0
  428. static char phone_2digit(char c)
  429. {
  430. if (c == 12)
  431. return '#';
  432. else if (c == 11)
  433. return '*';
  434. else if ((c < 10) && (c >= 0))
  435. return '0' + c - 1;
  436. else
  437. return '?';
  438. }
  439. #endif
  440. static struct ast_frame *phone_exception(struct ast_channel *ast)
  441. {
  442. int res;
  443. union telephony_exception phonee;
  444. struct phone_pvt *p = ast_channel_tech_pvt(ast);
  445. char digit;
  446. /* Some nice norms */
  447. p->fr.datalen = 0;
  448. p->fr.samples = 0;
  449. p->fr.data.ptr = NULL;
  450. p->fr.src = "Phone";
  451. p->fr.offset = 0;
  452. p->fr.mallocd=0;
  453. p->fr.delivery = ast_tv(0,0);
  454. phonee.bytes = ioctl(p->fd, PHONE_EXCEPTION);
  455. if (phonee.bits.dtmf_ready) {
  456. ast_debug(1, "phone_exception(): DTMF\n");
  457. /* We've got a digit -- Just handle this nicely and easily */
  458. digit = ioctl(p->fd, PHONE_GET_DTMF_ASCII);
  459. p->fr.subclass.integer = digit;
  460. p->fr.frametype = AST_FRAME_DTMF;
  461. return &p->fr;
  462. }
  463. if (phonee.bits.hookstate) {
  464. ast_debug(1, "Hookstate changed\n");
  465. res = ioctl(p->fd, PHONE_HOOKSTATE);
  466. /* See if we've gone on hook, if so, notify by returning NULL */
  467. ast_debug(1, "New hookstate: %d\n", res);
  468. if (!res && (p->mode != MODE_FXO))
  469. return NULL;
  470. else {
  471. if (ast_channel_state(ast) == AST_STATE_RINGING) {
  472. /* They've picked up the phone */
  473. p->fr.frametype = AST_FRAME_CONTROL;
  474. p->fr.subclass.integer = AST_CONTROL_ANSWER;
  475. phone_setup(ast);
  476. ast_setstate(ast, AST_STATE_UP);
  477. return &p->fr;
  478. } else
  479. ast_log(LOG_WARNING, "Got off hook in weird state %u\n", ast_channel_state(ast));
  480. }
  481. }
  482. #if 1
  483. if (phonee.bits.pstn_ring)
  484. ast_verbose("Unit is ringing\n");
  485. if (phonee.bits.caller_id) {
  486. ast_verbose("We have caller ID\n");
  487. }
  488. if (phonee.bits.pstn_wink)
  489. ast_verbose("Detected Wink\n");
  490. #endif
  491. /* Strange -- nothing there.. */
  492. p->fr.frametype = AST_FRAME_NULL;
  493. p->fr.subclass.integer = 0;
  494. return &p->fr;
  495. }
  496. static struct ast_frame *phone_read(struct ast_channel *ast)
  497. {
  498. int res;
  499. struct phone_pvt *p = ast_channel_tech_pvt(ast);
  500. /* Some nice norms */
  501. p->fr.datalen = 0;
  502. p->fr.samples = 0;
  503. p->fr.data.ptr = NULL;
  504. p->fr.src = "Phone";
  505. p->fr.offset = 0;
  506. p->fr.mallocd=0;
  507. p->fr.delivery = ast_tv(0,0);
  508. /* Try to read some data... */
  509. CHECK_BLOCKING(ast);
  510. res = read(p->fd, p->buf, PHONE_MAX_BUF);
  511. ast_clear_flag(ast_channel_flags(ast), AST_FLAG_BLOCKING);
  512. if (res < 0) {
  513. #if 0
  514. if (errno == EAGAIN) {
  515. ast_log(LOG_WARNING, "Null frame received\n");
  516. p->fr.frametype = AST_FRAME_NULL;
  517. p->fr.subclass = 0;
  518. return &p->fr;
  519. }
  520. #endif
  521. ast_log(LOG_WARNING, "Error reading: %s\n", strerror(errno));
  522. return NULL;
  523. }
  524. p->fr.data.ptr = p->buf;
  525. if (p->mode != MODE_FXS)
  526. switch(p->buf[0] & 0x3) {
  527. case '0':
  528. case '1':
  529. /* Normal */
  530. break;
  531. case '2':
  532. case '3':
  533. /* VAD/CNG, only send two words */
  534. res = 4;
  535. break;
  536. }
  537. p->fr.samples = 240;
  538. p->fr.datalen = res;
  539. p->fr.frametype = AST_FORMAT_GET_TYPE(p->lastinput.id) == AST_FORMAT_TYPE_AUDIO ?
  540. AST_FRAME_VOICE : AST_FORMAT_GET_TYPE(p->lastinput.id) == AST_FORMAT_TYPE_IMAGE ?
  541. AST_FRAME_IMAGE : AST_FRAME_VIDEO;
  542. ast_format_copy(&p->fr.subclass.format, &p->lastinput);
  543. p->fr.offset = AST_FRIENDLY_OFFSET;
  544. /* Byteswap from little-endian to native-endian */
  545. if (p->fr.subclass.format.id == AST_FORMAT_SLINEAR)
  546. ast_frame_byteswap_le(&p->fr);
  547. return &p->fr;
  548. }
  549. static int phone_write_buf(struct phone_pvt *p, const char *buf, int len, int frlen, int swap)
  550. {
  551. int res;
  552. /* Store as much of the buffer as we can, then write fixed frames */
  553. int space = sizeof(p->obuf) - p->obuflen;
  554. /* Make sure we have enough buffer space to store the frame */
  555. if (space < len)
  556. len = space;
  557. if (swap)
  558. ast_swapcopy_samples(p->obuf+p->obuflen, buf, len/2);
  559. else
  560. memcpy(p->obuf + p->obuflen, buf, len);
  561. p->obuflen += len;
  562. while(p->obuflen > frlen) {
  563. res = write(p->fd, p->obuf, frlen);
  564. if (res != frlen) {
  565. if (res < 1) {
  566. /*
  567. * Card is in non-blocking mode now and it works well now, but there are
  568. * lot of messages like this. So, this message is temporarily disabled.
  569. */
  570. return 0;
  571. } else {
  572. ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
  573. }
  574. }
  575. p->obuflen -= frlen;
  576. /* Move memory if necessary */
  577. if (p->obuflen)
  578. memmove(p->obuf, p->obuf + frlen, p->obuflen);
  579. }
  580. return len;
  581. }
  582. static int phone_send_text(struct ast_channel *ast, const char *text)
  583. {
  584. int length = strlen(text);
  585. return phone_write_buf(ast_channel_tech_pvt(ast), text, length, length, 0) ==
  586. length ? 0 : -1;
  587. }
  588. static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
  589. {
  590. struct phone_pvt *p = ast_channel_tech_pvt(ast);
  591. int res;
  592. int maxfr=0;
  593. char *pos;
  594. int sofar;
  595. int expected;
  596. int codecset = 0;
  597. char tmpbuf[4];
  598. /* Write a frame of (presumably voice) data */
  599. if (frame->frametype != AST_FRAME_VOICE && p->mode != MODE_FXS) {
  600. if (frame->frametype != AST_FRAME_IMAGE)
  601. ast_log(LOG_WARNING, "Don't know what to do with frame type '%u'\n", frame->frametype);
  602. return 0;
  603. }
  604. if (!(frame->subclass.format.id == AST_FORMAT_G723_1 ||
  605. frame->subclass.format.id == AST_FORMAT_SLINEAR ||
  606. frame->subclass.format.id == AST_FORMAT_ULAW ||
  607. frame->subclass.format.id == AST_FORMAT_G729A) &&
  608. p->mode != MODE_FXS) {
  609. ast_log(LOG_WARNING, "Cannot handle frames in %s format\n", ast_getformatname(&frame->subclass.format));
  610. return -1;
  611. }
  612. #if 0
  613. /* If we're not in up mode, go into up mode now */
  614. if (ast->_state != AST_STATE_UP) {
  615. ast_setstate(ast, AST_STATE_UP);
  616. phone_setup(ast);
  617. }
  618. #else
  619. if (ast_channel_state(ast) != AST_STATE_UP) {
  620. /* Don't try tos end audio on-hook */
  621. return 0;
  622. }
  623. #endif
  624. if (frame->subclass.format.id == AST_FORMAT_G729A) {
  625. if (p->lastformat.id != AST_FORMAT_G729A) {
  626. ioctl(p->fd, PHONE_PLAY_STOP);
  627. ioctl(p->fd, PHONE_REC_STOP);
  628. if (ioctl(p->fd, PHONE_PLAY_CODEC, G729)) {
  629. ast_log(LOG_WARNING, "Unable to set G729 mode\n");
  630. return -1;
  631. }
  632. if (ioctl(p->fd, PHONE_REC_CODEC, G729)) {
  633. ast_log(LOG_WARNING, "Unable to set G729 mode\n");
  634. return -1;
  635. }
  636. ast_format_set(&p->lastformat, AST_FORMAT_G729A, 0);
  637. ast_format_set(&p->lastinput, AST_FORMAT_G729A, 0);
  638. /* Reset output buffer */
  639. p->obuflen = 0;
  640. codecset = 1;
  641. }
  642. if (frame->datalen > 80) {
  643. ast_log(LOG_WARNING, "Frame size too large for G.729 (%d bytes)\n", frame->datalen);
  644. return -1;
  645. }
  646. maxfr = 80;
  647. } else if (frame->subclass.format.id == AST_FORMAT_G723_1) {
  648. if (p->lastformat.id != AST_FORMAT_G723_1) {
  649. ioctl(p->fd, PHONE_PLAY_STOP);
  650. ioctl(p->fd, PHONE_REC_STOP);
  651. if (ioctl(p->fd, PHONE_PLAY_CODEC, G723_63)) {
  652. ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
  653. return -1;
  654. }
  655. if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
  656. ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
  657. return -1;
  658. }
  659. ast_format_set(&p->lastformat, AST_FORMAT_G723_1, 0);
  660. ast_format_set(&p->lastinput, AST_FORMAT_G723_1, 0);
  661. /* Reset output buffer */
  662. p->obuflen = 0;
  663. codecset = 1;
  664. }
  665. if (frame->datalen > 24) {
  666. ast_log(LOG_WARNING, "Frame size too large for G.723.1 (%d bytes)\n", frame->datalen);
  667. return -1;
  668. }
  669. maxfr = 24;
  670. } else if (frame->subclass.format.id == AST_FORMAT_SLINEAR) {
  671. if (p->lastformat.id != AST_FORMAT_SLINEAR) {
  672. ioctl(p->fd, PHONE_PLAY_STOP);
  673. ioctl(p->fd, PHONE_REC_STOP);
  674. if (ioctl(p->fd, PHONE_PLAY_CODEC, LINEAR16)) {
  675. ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
  676. return -1;
  677. }
  678. if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
  679. ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
  680. return -1;
  681. }
  682. ast_format_set(&p->lastformat, AST_FORMAT_SLINEAR, 0);
  683. ast_format_set(&p->lastinput, AST_FORMAT_SLINEAR, 0);
  684. codecset = 1;
  685. /* Reset output buffer */
  686. p->obuflen = 0;
  687. }
  688. maxfr = 480;
  689. } else if (frame->subclass.format.id == AST_FORMAT_ULAW) {
  690. if (p->lastformat.id != AST_FORMAT_ULAW) {
  691. ioctl(p->fd, PHONE_PLAY_STOP);
  692. ioctl(p->fd, PHONE_REC_STOP);
  693. if (ioctl(p->fd, PHONE_PLAY_CODEC, ULAW)) {
  694. ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
  695. return -1;
  696. }
  697. if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
  698. ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
  699. return -1;
  700. }
  701. ast_format_set(&p->lastformat, AST_FORMAT_ULAW, 0);
  702. ast_format_set(&p->lastinput, AST_FORMAT_ULAW, 0);
  703. codecset = 1;
  704. /* Reset output buffer */
  705. p->obuflen = 0;
  706. }
  707. maxfr = 240;
  708. } else {
  709. if (ast_format_cmp(&p->lastformat, &frame->subclass.format) != AST_FORMAT_CMP_EQUAL) {
  710. ioctl(p->fd, PHONE_PLAY_STOP);
  711. ioctl(p->fd, PHONE_REC_STOP);
  712. if (ioctl(p->fd, PHONE_PLAY_CODEC, (int) frame->subclass.format.id)) {
  713. ast_log(LOG_WARNING, "Unable to set %s mode\n",
  714. ast_getformatname(&frame->subclass.format));
  715. return -1;
  716. }
  717. if (ioctl(p->fd, PHONE_REC_CODEC, (int) frame->subclass.format.id)) {
  718. ast_log(LOG_WARNING, "Unable to set %s mode\n",
  719. ast_getformatname(&frame->subclass.format));
  720. return -1;
  721. }
  722. ast_format_copy(&p->lastformat, &frame->subclass.format);
  723. ast_format_copy(&p->lastinput, &frame->subclass.format);
  724. codecset = 1;
  725. /* Reset output buffer */
  726. p->obuflen = 0;
  727. }
  728. maxfr = 480;
  729. }
  730. if (codecset) {
  731. ioctl(p->fd, PHONE_REC_DEPTH, 3);
  732. ioctl(p->fd, PHONE_PLAY_DEPTH, 3);
  733. if (ioctl(p->fd, PHONE_PLAY_START)) {
  734. ast_log(LOG_WARNING, "Failed to start playback\n");
  735. return -1;
  736. }
  737. if (ioctl(p->fd, PHONE_REC_START)) {
  738. ast_log(LOG_WARNING, "Failed to start recording\n");
  739. return -1;
  740. }
  741. }
  742. /* If we get here, we have a frame of Appropriate data */
  743. sofar = 0;
  744. pos = frame->data.ptr;
  745. while(sofar < frame->datalen) {
  746. /* Write in no more than maxfr sized frames */
  747. expected = frame->datalen - sofar;
  748. if (maxfr < expected)
  749. expected = maxfr;
  750. /* XXX Internet Phone Jack does not handle the 4-byte VAD frame properly! XXX
  751. we have to pad it to 24 bytes still. */
  752. if (frame->datalen == 4) {
  753. if (p->silencesupression) {
  754. memcpy(tmpbuf, frame->data.ptr, 4);
  755. expected = 24;
  756. res = phone_write_buf(p, tmpbuf, expected, maxfr, 0);
  757. }
  758. res = 4;
  759. expected=4;
  760. } else {
  761. int swap = 0;
  762. #if __BYTE_ORDER == __BIG_ENDIAN
  763. if (frame->subclass.format.id == AST_FORMAT_SLINEAR)
  764. swap = 1; /* Swap big-endian samples to little-endian as we copy */
  765. #endif
  766. res = phone_write_buf(p, pos, expected, maxfr, swap);
  767. }
  768. if (res != expected) {
  769. if ((errno != EAGAIN) && (errno != EINTR)) {
  770. if (res < 0)
  771. ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
  772. /*
  773. * Card is in non-blocking mode now and it works well now, but there are
  774. * lot of messages like this. So, this message is temporarily disabled.
  775. */
  776. #if 0
  777. else
  778. ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
  779. #endif
  780. return -1;
  781. } else /* Pretend it worked */
  782. res = expected;
  783. }
  784. sofar += res;
  785. pos += res;
  786. }
  787. return 0;
  788. }
  789. static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *cntx, const char *linkedid)
  790. {
  791. struct ast_channel *tmp;
  792. struct phone_codec_data queried_codec;
  793. struct ast_format tmpfmt;
  794. tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, "", i->ext, i->context, linkedid, 0, "Phone/%s", i->dev + 5);
  795. if (tmp) {
  796. ast_channel_tech_set(tmp, cur_tech);
  797. ast_channel_set_fd(tmp, 0, i->fd);
  798. /* XXX Switching formats silently causes kernel panics XXX */
  799. if (i->mode == MODE_FXS &&
  800. ioctl(i->fd, PHONE_QUERY_CODEC, &queried_codec) == 0) {
  801. if (queried_codec.type == LINEAR16) {
  802. ast_format_cap_add(ast_channel_nativeformats(tmp), ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
  803. ast_format_copy(ast_channel_rawreadformat(tmp), &tmpfmt);
  804. ast_format_copy(ast_channel_rawwriteformat(tmp), &tmpfmt);
  805. } else {
  806. ast_format_cap_remove(prefcap, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
  807. }
  808. } else {
  809. ast_format_cap_copy(ast_channel_nativeformats(tmp), prefcap);
  810. ast_best_codec(ast_channel_nativeformats(tmp), &tmpfmt);
  811. ast_format_copy(ast_channel_rawreadformat(tmp), &tmpfmt);
  812. ast_format_copy(ast_channel_rawwriteformat(tmp), &tmpfmt);
  813. }
  814. /* no need to call ast_setstate: the channel_alloc already did its job */
  815. if (state == AST_STATE_RING)
  816. ast_channel_rings_set(tmp, 1);
  817. ast_channel_tech_pvt_set(tmp, i);
  818. ast_channel_context_set(tmp, cntx);
  819. if (!ast_strlen_zero(i->ext))
  820. ast_channel_exten_set(tmp, i->ext);
  821. else
  822. ast_channel_exten_set(tmp, "s");
  823. if (!ast_strlen_zero(i->language))
  824. ast_channel_language_set(tmp, i->language);
  825. /* Don't use ast_set_callerid() here because it will
  826. * generate a NewCallerID event before the NewChannel event */
  827. if (!ast_strlen_zero(i->cid_num)) {
  828. ast_channel_caller(tmp)->ani.number.valid = 1;
  829. ast_channel_caller(tmp)->ani.number.str = ast_strdup(i->cid_num);
  830. }
  831. i->owner = tmp;
  832. ast_module_ref(ast_module_info->self);
  833. if (state != AST_STATE_DOWN) {
  834. if (state == AST_STATE_RING) {
  835. ioctl(ast_channel_fd(tmp, 0), PHONE_RINGBACK);
  836. i->cpt = 1;
  837. }
  838. if (ast_pbx_start(tmp)) {
  839. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
  840. ast_hangup(tmp);
  841. }
  842. }
  843. } else
  844. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  845. return tmp;
  846. }
  847. static void phone_mini_packet(struct phone_pvt *i)
  848. {
  849. int res;
  850. char buf[1024];
  851. /* Ignore stuff we read... */
  852. res = read(i->fd, buf, sizeof(buf));
  853. if (res < 1) {
  854. ast_log(LOG_WARNING, "Read returned %d: %s\n", res, strerror(errno));
  855. return;
  856. }
  857. }
  858. static void phone_check_exception(struct phone_pvt *i)
  859. {
  860. int offhook=0;
  861. char digit[2] = {0 , 0};
  862. union telephony_exception phonee;
  863. /* XXX Do something XXX */
  864. #if 0
  865. ast_debug(1, "Exception!\n");
  866. #endif
  867. phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION);
  868. if (phonee.bits.dtmf_ready) {
  869. digit[0] = ioctl(i->fd, PHONE_GET_DTMF_ASCII);
  870. if (i->mode == MODE_DIALTONE || i->mode == MODE_FXS || i->mode == MODE_SIGMA) {
  871. ioctl(i->fd, PHONE_PLAY_STOP);
  872. ioctl(i->fd, PHONE_REC_STOP);
  873. ioctl(i->fd, PHONE_CPT_STOP);
  874. i->dialtone = 0;
  875. if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
  876. strncat(i->ext, digit, sizeof(i->ext) - strlen(i->ext) - 1);
  877. if ((i->mode != MODE_FXS ||
  878. !(phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION)) ||
  879. !phonee.bits.dtmf_ready) &&
  880. ast_exists_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
  881. /* It's a valid extension in its context, get moving! */
  882. phone_new(i, AST_STATE_RING, i->context, NULL);
  883. /* No need to restart monitor, we are the monitor */
  884. } else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
  885. /* There is nothing in the specified extension that can match anymore.
  886. Try the default */
  887. if (ast_exists_extension(NULL, "default", i->ext, 1, i->cid_num)) {
  888. /* Check the default, too... */
  889. phone_new(i, AST_STATE_RING, "default", NULL);
  890. /* XXX This should probably be justified better XXX */
  891. } else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->cid_num)) {
  892. /* It's not a valid extension, give a busy signal */
  893. ast_debug(1, "%s can't match anything in %s or default\n", i->ext, i->context);
  894. ioctl(i->fd, PHONE_BUSY);
  895. i->cpt = 1;
  896. }
  897. }
  898. #if 0
  899. ast_verbose("Extension is %s\n", i->ext);
  900. #endif
  901. }
  902. }
  903. if (phonee.bits.hookstate) {
  904. offhook = ioctl(i->fd, PHONE_HOOKSTATE);
  905. if (offhook) {
  906. if (i->mode == MODE_IMMEDIATE) {
  907. phone_new(i, AST_STATE_RING, i->context, NULL);
  908. } else if (i->mode == MODE_DIALTONE) {
  909. ast_module_ref(ast_module_info->self);
  910. /* Reset the extension */
  911. i->ext[0] = '\0';
  912. /* Play the dialtone */
  913. i->dialtone++;
  914. ioctl(i->fd, PHONE_PLAY_STOP);
  915. ioctl(i->fd, PHONE_PLAY_CODEC, ULAW);
  916. ioctl(i->fd, PHONE_PLAY_START);
  917. ast_format_clear(&i->lastformat);
  918. } else if (i->mode == MODE_SIGMA) {
  919. ast_module_ref(ast_module_info->self);
  920. /* Reset the extension */
  921. i->ext[0] = '\0';
  922. /* Play the dialtone */
  923. i->dialtone++;
  924. ioctl(i->fd, PHONE_DIALTONE);
  925. }
  926. } else {
  927. if (i->dialtone)
  928. ast_module_unref(ast_module_info->self);
  929. memset(i->ext, 0, sizeof(i->ext));
  930. if (i->cpt)
  931. {
  932. ioctl(i->fd, PHONE_CPT_STOP);
  933. i->cpt = 0;
  934. }
  935. ioctl(i->fd, PHONE_PLAY_STOP);
  936. ioctl(i->fd, PHONE_REC_STOP);
  937. i->dialtone = 0;
  938. ast_format_clear(&i->lastformat);
  939. }
  940. }
  941. if (phonee.bits.pstn_ring) {
  942. ast_verbose("Unit is ringing\n");
  943. phone_new(i, AST_STATE_RING, i->context, NULL);
  944. }
  945. if (phonee.bits.caller_id)
  946. ast_verbose("We have caller ID\n");
  947. }
  948. static void *do_monitor(void *data)
  949. {
  950. struct pollfd *fds = NULL;
  951. int nfds = 0, inuse_fds = 0, res;
  952. struct phone_pvt *i;
  953. int tonepos = 0;
  954. /* The tone we're playing this round */
  955. struct timeval to = { 0, 0 };
  956. int dotone;
  957. /* This thread monitors all the frame relay interfaces which are not yet in use
  958. (and thus do not have a separate thread) indefinitely */
  959. while (monitor) {
  960. /* Don't let anybody kill us right away. Nobody should lock the interface list
  961. and wait for the monitor list, but the other way around is okay. */
  962. /* Lock the interface list */
  963. if (ast_mutex_lock(&iflock)) {
  964. ast_log(LOG_ERROR, "Unable to grab interface lock\n");
  965. return NULL;
  966. }
  967. /* Build the stuff we're going to select on, that is the socket of every
  968. phone_pvt that does not have an associated owner channel */
  969. i = iflist;
  970. dotone = 0;
  971. inuse_fds = 0;
  972. for (i = iflist; i; i = i->next) {
  973. if (!i->owner) {
  974. /* This needs to be watched, as it lacks an owner */
  975. if (inuse_fds == nfds) {
  976. void *tmp = ast_realloc(fds, (nfds + 1) * sizeof(*fds));
  977. if (!tmp) {
  978. /* Avoid leaking */
  979. continue;
  980. }
  981. fds = tmp;
  982. nfds++;
  983. }
  984. fds[inuse_fds].fd = i->fd;
  985. fds[inuse_fds].events = POLLIN | POLLERR;
  986. fds[inuse_fds].revents = 0;
  987. inuse_fds++;
  988. if (i->dialtone && i->mode != MODE_SIGMA) {
  989. /* Remember we're going to have to come back and play
  990. more dialtones */
  991. if (ast_tvzero(to)) {
  992. /* If we're due for a dialtone, play one */
  993. if (write(i->fd, DialTone + tonepos, 240) != 240) {
  994. ast_log(LOG_WARNING, "Dial tone write error\n");
  995. }
  996. }
  997. dotone++;
  998. }
  999. }
  1000. }
  1001. /* Okay, now that we know what to do, release the interface lock */
  1002. ast_mutex_unlock(&iflock);
  1003. /* Wait indefinitely for something to happen */
  1004. if (dotone && i && i->mode != MODE_SIGMA) {
  1005. /* If we're ready to recycle the time, set it to 30 ms */
  1006. tonepos += 240;
  1007. if (tonepos >= sizeof(DialTone)) {
  1008. tonepos = 0;
  1009. }
  1010. if (ast_tvzero(to)) {
  1011. to = ast_tv(0, 30000);
  1012. }
  1013. res = ast_poll2(fds, inuse_fds, &to);
  1014. } else {
  1015. res = ast_poll(fds, inuse_fds, -1);
  1016. to = ast_tv(0, 0);
  1017. tonepos = 0;
  1018. }
  1019. /* Okay, select has finished. Let's see what happened. */
  1020. if (res < 0) {
  1021. ast_debug(1, "poll returned %d: %s\n", res, strerror(errno));
  1022. continue;
  1023. }
  1024. /* If there are no fd's changed, just continue, it's probably time
  1025. to play some more dialtones */
  1026. if (!res) {
  1027. continue;
  1028. }
  1029. /* Alright, lock the interface list again, and let's look and see what has
  1030. happened */
  1031. if (ast_mutex_lock(&iflock)) {
  1032. ast_log(LOG_WARNING, "Unable to lock the interface list\n");
  1033. continue;
  1034. }
  1035. for (i = iflist; i; i = i->next) {
  1036. int j;
  1037. /* Find the record */
  1038. for (j = 0; j < inuse_fds; j++) {
  1039. if (fds[j].fd == i->fd) {
  1040. break;
  1041. }
  1042. }
  1043. /* Not found? */
  1044. if (j == inuse_fds) {
  1045. continue;
  1046. }
  1047. if (fds[j].revents & POLLIN) {
  1048. if (i->owner) {
  1049. continue;
  1050. }
  1051. phone_mini_packet(i);
  1052. }
  1053. if (fds[j].revents & POLLERR) {
  1054. if (i->owner) {
  1055. continue;
  1056. }
  1057. phone_check_exception(i);
  1058. }
  1059. }
  1060. ast_mutex_unlock(&iflock);
  1061. }
  1062. return NULL;
  1063. }
  1064. static int restart_monitor()
  1065. {
  1066. /* If we're supposed to be stopped -- stay stopped */
  1067. if (monitor_thread == AST_PTHREADT_STOP)
  1068. return 0;
  1069. if (ast_mutex_lock(&monlock)) {
  1070. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  1071. return -1;
  1072. }
  1073. if (monitor_thread == pthread_self()) {
  1074. ast_mutex_unlock(&monlock);
  1075. ast_log(LOG_WARNING, "Cannot kill myself\n");
  1076. return -1;
  1077. }
  1078. if (monitor_thread != AST_PTHREADT_NULL) {
  1079. if (ast_mutex_lock(&iflock)) {
  1080. ast_mutex_unlock(&monlock);
  1081. ast_log(LOG_WARNING, "Unable to lock the interface list\n");
  1082. return -1;
  1083. }
  1084. monitor = 0;
  1085. while (pthread_kill(monitor_thread, SIGURG) == 0)
  1086. sched_yield();
  1087. pthread_join(monitor_thread, NULL);
  1088. ast_mutex_unlock(&iflock);
  1089. }
  1090. monitor = 1;
  1091. /* Start a new monitor */
  1092. if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
  1093. ast_mutex_unlock(&monlock);
  1094. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  1095. return -1;
  1096. }
  1097. ast_mutex_unlock(&monlock);
  1098. return 0;
  1099. }
  1100. static struct phone_pvt *mkif(const char *iface, int mode, int txgain, int rxgain)
  1101. {
  1102. /* Make a phone_pvt structure for this interface */
  1103. struct phone_pvt *tmp;
  1104. int flags;
  1105. tmp = ast_calloc(1, sizeof(*tmp));
  1106. if (tmp) {
  1107. tmp->fd = open(iface, O_RDWR);
  1108. if (tmp->fd < 0) {
  1109. ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
  1110. ast_free(tmp);
  1111. return NULL;
  1112. }
  1113. if (mode == MODE_FXO) {
  1114. if (ioctl(tmp->fd, IXJCTL_PORT, PORT_PSTN)) {
  1115. ast_debug(1, "Unable to set port to PSTN\n");
  1116. }
  1117. } else {
  1118. if (ioctl(tmp->fd, IXJCTL_PORT, PORT_POTS))
  1119. if (mode != MODE_FXS)
  1120. ast_debug(1, "Unable to set port to POTS\n");
  1121. }
  1122. ioctl(tmp->fd, PHONE_PLAY_STOP);
  1123. ioctl(tmp->fd, PHONE_REC_STOP);
  1124. ioctl(tmp->fd, PHONE_RING_STOP);
  1125. ioctl(tmp->fd, PHONE_CPT_STOP);
  1126. if (ioctl(tmp->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK))
  1127. ast_debug(1, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",iface, strerror(errno));
  1128. if (echocancel != AEC_OFF)
  1129. ioctl(tmp->fd, IXJCTL_AEC_START, echocancel);
  1130. if (silencesupression)
  1131. tmp->silencesupression = 1;
  1132. #ifdef PHONE_VAD
  1133. ioctl(tmp->fd, PHONE_VAD, tmp->silencesupression);
  1134. #endif
  1135. tmp->mode = mode;
  1136. flags = fcntl(tmp->fd, F_GETFL);
  1137. fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
  1138. tmp->owner = NULL;
  1139. ast_format_clear(&tmp->lastformat);
  1140. ast_format_clear(&tmp->lastinput);
  1141. tmp->ministate = 0;
  1142. memset(tmp->ext, 0, sizeof(tmp->ext));
  1143. ast_copy_string(tmp->language, language, sizeof(tmp->language));
  1144. ast_copy_string(tmp->dev, iface, sizeof(tmp->dev));
  1145. ast_copy_string(tmp->context, context, sizeof(tmp->context));
  1146. tmp->next = NULL;
  1147. tmp->obuflen = 0;
  1148. tmp->dialtone = 0;
  1149. tmp->cpt = 0;
  1150. ast_copy_string(tmp->cid_num, cid_num, sizeof(tmp->cid_num));
  1151. ast_copy_string(tmp->cid_name, cid_name, sizeof(tmp->cid_name));
  1152. tmp->txgain = txgain;
  1153. ioctl(tmp->fd, PHONE_PLAY_VOLUME, tmp->txgain);
  1154. tmp->rxgain = rxgain;
  1155. ioctl(tmp->fd, PHONE_REC_VOLUME, tmp->rxgain);
  1156. }
  1157. return tmp;
  1158. }
  1159. static struct ast_channel *phone_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause)
  1160. {
  1161. struct phone_pvt *p;
  1162. struct ast_channel *tmp = NULL;
  1163. const char *name = data;
  1164. /* Search for an unowned channel */
  1165. if (ast_mutex_lock(&iflock)) {
  1166. ast_log(LOG_ERROR, "Unable to lock interface list???\n");
  1167. return NULL;
  1168. }
  1169. p = iflist;
  1170. while(p) {
  1171. if (p->mode == MODE_FXS || (ast_format_cap_has_joint(cap, phone_tech.capabilities))) {
  1172. size_t length = strlen(p->dev + 5);
  1173. if (strncmp(name, p->dev + 5, length) == 0 &&
  1174. !isalnum(name[length])) {
  1175. if (!p->owner) {
  1176. tmp = phone_new(p, AST_STATE_DOWN, p->context, requestor ? ast_channel_linkedid(requestor) : NULL);
  1177. break;
  1178. } else
  1179. *cause = AST_CAUSE_BUSY;
  1180. }
  1181. }
  1182. p = p->next;
  1183. }
  1184. ast_mutex_unlock(&iflock);
  1185. restart_monitor();
  1186. if (tmp == NULL) {
  1187. if (!(ast_format_cap_has_joint(cap, phone_tech.capabilities))) {
  1188. char buf[256];
  1189. ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
  1190. return NULL;
  1191. }
  1192. }
  1193. return tmp;
  1194. }
  1195. /* parse gain value from config file */
  1196. static int parse_gain_value(const char *gain_type, const char *value)
  1197. {
  1198. float gain;
  1199. /* try to scan number */
  1200. if (sscanf(value, "%30f", &gain) != 1)
  1201. {
  1202. ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n",
  1203. value, gain_type, config);
  1204. return DEFAULT_GAIN;
  1205. }
  1206. /* multiplicate gain by 1.0 gain value */
  1207. gain = gain * (float)DEFAULT_GAIN;
  1208. /* percentage? */
  1209. if (value[strlen(value) - 1] == '%')
  1210. return (int)(gain / (float)100);
  1211. return (int)gain;
  1212. }
  1213. static int __unload_module(void)
  1214. {
  1215. struct phone_pvt *p, *pl;
  1216. /* First, take us out of the channel loop */
  1217. if (cur_tech)
  1218. ast_channel_unregister(cur_tech);
  1219. if (!ast_mutex_lock(&iflock)) {
  1220. /* Hangup all interfaces if they have an owner */
  1221. p = iflist;
  1222. while(p) {
  1223. if (p->owner)
  1224. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  1225. p = p->next;
  1226. }
  1227. iflist = NULL;
  1228. ast_mutex_unlock(&iflock);
  1229. } else {
  1230. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  1231. return -1;
  1232. }
  1233. if (!ast_mutex_lock(&monlock)) {
  1234. if (monitor_thread > AST_PTHREADT_NULL) {
  1235. monitor = 0;
  1236. while (pthread_kill(monitor_thread, SIGURG) == 0)
  1237. sched_yield();
  1238. pthread_join(monitor_thread, NULL);
  1239. }
  1240. monitor_thread = AST_PTHREADT_STOP;
  1241. ast_mutex_unlock(&monlock);
  1242. } else {
  1243. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  1244. return -1;
  1245. }
  1246. if (!ast_mutex_lock(&iflock)) {
  1247. /* Destroy all the interfaces and free their memory */
  1248. p = iflist;
  1249. while(p) {
  1250. /* Close the socket, assuming it's real */
  1251. if (p->fd > -1)
  1252. close(p->fd);
  1253. pl = p;
  1254. p = p->next;
  1255. /* Free associated memory */
  1256. ast_free(pl);
  1257. }
  1258. iflist = NULL;
  1259. ast_mutex_unlock(&iflock);
  1260. } else {
  1261. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  1262. return -1;
  1263. }
  1264. phone_tech.capabilities = ast_format_cap_destroy(phone_tech.capabilities);
  1265. phone_tech_fxs.capabilities = ast_format_cap_destroy(phone_tech_fxs.capabilities);
  1266. prefcap = ast_format_cap_destroy(prefcap);
  1267. return 0;
  1268. }
  1269. static int unload_module(void)
  1270. {
  1271. return __unload_module();
  1272. }
  1273. static int load_module(void)
  1274. {
  1275. struct ast_config *cfg;
  1276. struct ast_variable *v;
  1277. struct phone_pvt *tmp;
  1278. int mode = MODE_IMMEDIATE;
  1279. int txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN; /* default gain 1.0 */
  1280. struct ast_flags config_flags = { 0 };
  1281. struct ast_format tmpfmt;
  1282. if (!(phone_tech.capabilities = ast_format_cap_alloc())) {
  1283. return AST_MODULE_LOAD_DECLINE;
  1284. }
  1285. ast_format_cap_add(phone_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0));
  1286. ast_format_cap_add(phone_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
  1287. ast_format_cap_add(phone_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0));
  1288. ast_format_cap_add(phone_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0));
  1289. if (!(prefcap = ast_format_cap_alloc())) {
  1290. return AST_MODULE_LOAD_DECLINE;
  1291. }
  1292. ast_format_cap_copy(prefcap, phone_tech.capabilities);
  1293. if (!(phone_tech_fxs.capabilities = ast_format_cap_alloc())) {
  1294. return AST_MODULE_LOAD_DECLINE;
  1295. }
  1296. if ((cfg = ast_config_load(config, config_flags)) == CONFIG_STATUS_FILEINVALID) {
  1297. ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config);
  1298. return AST_MODULE_LOAD_DECLINE;
  1299. }
  1300. /* We *must* have a config file otherwise stop immediately */
  1301. if (!cfg) {
  1302. ast_log(LOG_ERROR, "Unable to load config %s\n", config);
  1303. return AST_MODULE_LOAD_DECLINE;
  1304. }
  1305. if (ast_mutex_lock(&iflock)) {
  1306. /* It's a little silly to lock it, but we mind as well just to be sure */
  1307. ast_log(LOG_ERROR, "Unable to lock interface list???\n");
  1308. return AST_MODULE_LOAD_FAILURE;
  1309. }
  1310. v = ast_variable_browse(cfg, "interfaces");
  1311. while(v) {
  1312. /* Create the interface list */
  1313. if (!strcasecmp(v->name, "device")) {
  1314. tmp = mkif(v->value, mode, txgain, rxgain);
  1315. if (tmp) {
  1316. tmp->next = iflist;
  1317. iflist = tmp;
  1318. } else {
  1319. ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
  1320. ast_config_destroy(cfg);
  1321. ast_mutex_unlock(&iflock);
  1322. __unload_module();
  1323. return AST_MODULE_LOAD_FAILURE;
  1324. }
  1325. } else if (!strcasecmp(v->name, "silencesupression")) {
  1326. silencesupression = ast_true(v->value);
  1327. } else if (!strcasecmp(v->name, "language")) {
  1328. ast_copy_string(language, v->value, sizeof(language));
  1329. } else if (!strcasecmp(v->name, "callerid")) {
  1330. ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
  1331. } else if (!strcasecmp(v->name, "mode")) {
  1332. if (!strncasecmp(v->value, "di", 2))
  1333. mode = MODE_DIALTONE;
  1334. else if (!strncasecmp(v->value, "sig", 3))
  1335. mode = MODE_SIGMA;
  1336. else if (!strncasecmp(v->value, "im", 2))
  1337. mode = MODE_IMMEDIATE;
  1338. else if (!strncasecmp(v->value, "fxs", 3)) {
  1339. mode = MODE_FXS;
  1340. ast_format_cap_remove_bytype(prefcap, AST_FORMAT_TYPE_AUDIO); /* All non-voice */
  1341. }
  1342. else if (!strncasecmp(v->value, "fx", 2))
  1343. mode = MODE_FXO;
  1344. else
  1345. ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
  1346. } else if (!strcasecmp(v->name, "context")) {
  1347. ast_copy_string(context, v->value, sizeof(context));
  1348. } else if (!strcasecmp(v->name, "format")) {
  1349. if (!strcasecmp(v->value, "g729")) {
  1350. ast_format_cap_set(prefcap, ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0));
  1351. } else if (!strcasecmp(v->value, "g723.1")) {
  1352. ast_format_cap_set(prefcap, ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0));
  1353. } else if (!strcasecmp(v->value, "slinear")) {
  1354. ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0);
  1355. if (mode == MODE_FXS) {
  1356. ast_format_cap_add(prefcap, &tmpfmt);
  1357. } else {
  1358. ast_format_cap_set(prefcap, &tmpfmt);
  1359. }
  1360. } else if (!strcasecmp(v->value, "ulaw")) {
  1361. ast_format_cap_set(prefcap, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0));
  1362. } else
  1363. ast_log(LOG_WARNING, "Unknown format '%s'\n", v->value);
  1364. } else if (!strcasecmp(v->name, "echocancel")) {
  1365. if (!strcasecmp(v->value, "off")) {
  1366. echocancel = AEC_OFF;
  1367. } else if (!strcasecmp(v->value, "low")) {
  1368. echocancel = AEC_LOW;
  1369. } else if (!strcasecmp(v->value, "medium")) {
  1370. echocancel = AEC_MED;
  1371. } else if (!strcasecmp(v->value, "high")) {
  1372. echocancel = AEC_HIGH;
  1373. } else
  1374. ast_log(LOG_WARNING, "Unknown echo cancellation '%s'\n", v->value);
  1375. } else if (!strcasecmp(v->name, "txgain")) {
  1376. txgain = parse_gain_value(v->name, v->value);
  1377. } else if (!strcasecmp(v->name, "rxgain")) {
  1378. rxgain = parse_gain_value(v->name, v->value);
  1379. }
  1380. v = v->next;
  1381. }
  1382. ast_mutex_unlock(&iflock);
  1383. if (mode == MODE_FXS) {
  1384. ast_format_cap_copy(phone_tech_fxs.capabilities, prefcap);
  1385. cur_tech = &phone_tech_fxs;
  1386. } else
  1387. cur_tech = (struct ast_channel_tech *) &phone_tech;
  1388. /* Make sure we can register our Adtranphone channel type */
  1389. if (ast_channel_register(cur_tech)) {
  1390. ast_log(LOG_ERROR, "Unable to register channel class 'Phone'\n");
  1391. ast_config_destroy(cfg);
  1392. __unload_module();
  1393. return AST_MODULE_LOAD_FAILURE;
  1394. }
  1395. ast_config_destroy(cfg);
  1396. /* And start the monitor for the first time */
  1397. restart_monitor();
  1398. return AST_MODULE_LOAD_SUCCESS;
  1399. }
  1400. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Linux Telephony API Support");