chan_phone.c 44 KB

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