chan_phone.c 42 KB

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