chan_phone.c 46 KB

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