chan_modem.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * A/Open ITU-56/2 Voice Modem Driver (Rockwell, IS-101, and others)
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <asterisk/lock.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/channel_pvt.h>
  18. #include <asterisk/config.h>
  19. #include <asterisk/logger.h>
  20. #include <asterisk/module.h>
  21. #include <asterisk/pbx.h>
  22. #include <asterisk/options.h>
  23. #include <asterisk/vmodem.h>
  24. #include <asterisk/utils.h>
  25. #include <sys/socket.h>
  26. #include <sys/time.h>
  27. #include <errno.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <netinet/in.h>
  31. #include <arpa/inet.h>
  32. #include <fcntl.h>
  33. #include <sys/ioctl.h>
  34. #include <sys/termios.h>
  35. #include <sys/signal.h>
  36. #include <ctype.h>
  37. /* Up to 10 seconds for an echo to arrive */
  38. #define ECHO_TIMEOUT 10
  39. static char *desc = "Generic Voice Modem Driver";
  40. static char *tdesc = "Generic Voice Modem Channel Driver";
  41. static char *type = "Modem";
  42. static char *config = "modem.conf";
  43. static char dialtype = 'T';
  44. static int gmode = MODEM_MODE_IMMEDIATE;
  45. /* Default modem type */
  46. static char mtype[80] = "autodetect";
  47. /* Default context for incoming calls */
  48. static char context[AST_MAX_EXTENSION]= "default";
  49. /* Default language */
  50. static char language[MAX_LANGUAGE] = "";
  51. /* Initialization String */
  52. static char initstr[AST_MAX_INIT_STR] = "ATE0Q0";
  53. /* Default MSN */
  54. static char msn[AST_MAX_EXTENSION]="";
  55. /* Default Listen */
  56. static char incomingmsn[AST_MAX_EXTENSION]="";
  57. /* Default DTMF-detection mode (i4l/asterisk) */
  58. static int dtmfmode = MODEM_DTMF_AST;
  59. /* Default DTMF-generation mode (i4l (outband) / asterisk (inband) */
  60. static int dtmfmodegen = MODEM_DTMF_AST;
  61. struct ast_dsp *dsp = NULL;
  62. /* Default valid outgoing MSN */
  63. static char outgoingmsn[AST_MAX_EXTENSION]="";
  64. /* Default group */
  65. static unsigned int cur_group = 0;
  66. static int usecnt =0;
  67. static int baudrate = 115200;
  68. static int stripmsd = 0;
  69. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  70. /* Protect the interface list (of ast_modem_pvt's) */
  71. AST_MUTEX_DEFINE_STATIC(iflock);
  72. /* Protect the monitoring thread, so only one process can kill or start it, and not
  73. when it's doing something critical. */
  74. AST_MUTEX_DEFINE_STATIC(monlock);
  75. /* This is the thread for the monitor which checks for input on the channels
  76. which are not currently in use. */
  77. static pthread_t monitor_thread = AST_PTHREADT_NULL;
  78. static int restart_monitor(void);
  79. /* The private structures of the Phone Jack channels are linked for
  80. selecting outgoing channels */
  81. static struct ast_modem_pvt *iflist = NULL;
  82. static int modem_digit(struct ast_channel *ast, char digit)
  83. {
  84. struct ast_modem_pvt *p;
  85. p = ast->pvt->pvt;
  86. if (p->mc->dialdigit)
  87. return p->mc->dialdigit(p, digit);
  88. ast_log(LOG_DEBUG, "Channel %s lacks digit dialing\n", ast->name);
  89. return -1;
  90. }
  91. static struct ast_modem_driver *drivers = NULL;
  92. static struct ast_frame *modem_read(struct ast_channel *);
  93. static struct ast_modem_driver *find_capability(char *ident)
  94. {
  95. struct ast_modem_driver *mc;
  96. int x;
  97. mc = drivers;
  98. while(mc) {
  99. for (x=0;mc->idents[x];x++) {
  100. if (!strcmp(ident, mc->idents[x]))
  101. break;
  102. }
  103. if (mc->idents[x])
  104. break;
  105. mc = mc->next;
  106. }
  107. if (mc) {
  108. if (mc->incusecnt)
  109. mc->incusecnt();
  110. }
  111. return mc;
  112. }
  113. static struct ast_modem_driver *find_driver(char *drv)
  114. {
  115. struct ast_modem_driver *mc;
  116. mc = drivers;
  117. while(mc) {
  118. if (!strcasecmp(mc->name, drv))
  119. break;
  120. mc = mc->next;
  121. }
  122. if (mc) {
  123. if (mc->incusecnt)
  124. mc->incusecnt();
  125. }
  126. return mc;
  127. }
  128. int ast_register_modem_driver(struct ast_modem_driver *mc)
  129. {
  130. mc->next = drivers;
  131. drivers = mc;
  132. return 0;
  133. }
  134. int ast_unregister_modem_driver(struct ast_modem_driver *mc)
  135. {
  136. struct ast_modem_driver *last = NULL, *cur;
  137. cur = drivers;
  138. while(cur) {
  139. if (cur == mc) {
  140. if (last)
  141. last->next = mc->next;
  142. else
  143. drivers = mc->next;
  144. return 0;
  145. }
  146. cur = cur->next;
  147. }
  148. return -1;
  149. }
  150. static int modem_call(struct ast_channel *ast, char *idest, int timeout)
  151. {
  152. struct ast_modem_pvt *p;
  153. int ms = timeout;
  154. char rdest[80], *where, dstr[100] = "";
  155. char *stringp=NULL;
  156. strncpy(rdest, idest, sizeof(rdest)-1);
  157. stringp=rdest;
  158. strsep(&stringp, ":");
  159. where = strsep(&stringp, ":");
  160. if (!where) {
  161. ast_log(LOG_WARNING, "Destination %s requres a real destination (device:destination)\n", idest);
  162. return -1;
  163. }
  164. p = ast->pvt->pvt;
  165. strncpy(dstr, where + p->stripmsd, sizeof(dstr) - 1);
  166. /* if not a transfer or just sending tones, must be in correct state */
  167. if (strcasecmp(rdest, "transfer") && strcasecmp(rdest,"sendtones")) {
  168. if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
  169. ast_log(LOG_WARNING, "modem_call called on %s, neither down nor reserved\n", ast->name);
  170. return -1;
  171. }
  172. }
  173. if (!strcasecmp(rdest,"transfer")) /* if a transfer, put in transfer stuff */
  174. {
  175. snprintf(dstr, sizeof(dstr), "!,%s", where + p->stripmsd);
  176. }
  177. if (!strcasecmp(where, "handset")) {
  178. if (p->mc->setdev)
  179. if (p->mc->setdev(p, MODEM_DEV_HANDSET))
  180. return -1;
  181. /* Should be immediately up */
  182. ast_setstate(ast, AST_STATE_UP);
  183. } else {
  184. if (p->mc->setdev)
  185. if (p->mc->setdev(p, MODEM_DEV_TELCO_SPK))
  186. return -1;
  187. if (p->mc->dial)
  188. p->mc->dial(p, dstr);
  189. ast_setstate(ast, AST_STATE_DIALING);
  190. while((ast->_state != AST_STATE_UP) && (ms > 0)) {
  191. ms = ast_waitfor(ast, ms);
  192. /* Just read packets and watch what happens */
  193. if (ms > 0) {
  194. if (!modem_read(ast))
  195. return -1;
  196. }
  197. }
  198. if (ms < 0)
  199. return -1;
  200. }
  201. return 0;
  202. }
  203. int ast_modem_send(struct ast_modem_pvt *p, char *cmd, int len)
  204. {
  205. int i;
  206. usleep(5000);
  207. if (!len) {
  208. for(i = 0; cmd[i];)
  209. {
  210. if (fwrite(cmd + i,1,1,p->f) != 1)
  211. {
  212. if (errno == EWOULDBLOCK) continue;
  213. return -1;
  214. }
  215. i++;
  216. }
  217. tcdrain(fileno(p->f));
  218. fprintf(p->f,"\r\n");
  219. return 0;
  220. } else {
  221. if (fwrite(cmd, 1, len, p->f) < len)
  222. return -1;
  223. return 0;
  224. }
  225. }
  226. int ast_modem_read_response(struct ast_modem_pvt *p, int timeout)
  227. {
  228. int res = -1,c,i;
  229. timeout *= 1000;
  230. p->response[0] = 0;
  231. c = i = 0;
  232. do {
  233. res = ast_waitfor_n_fd(&p->fd, 1, &timeout, NULL);
  234. if (res < 0) {
  235. strncpy(p->response, "(No Response)", sizeof(p->response)-1);
  236. return -1;
  237. }
  238. /* get no more then buffer length */
  239. while(i < sizeof(p->response) - 1)
  240. {
  241. c = fgetc(p->f); /* get a char */
  242. if (c < 1) /* if error */
  243. {
  244. /* if nothing in buffer, go back into timeout stuff */
  245. if (errno == EWOULDBLOCK) break;
  246. /* return as error */
  247. strncpy(p->response, "(No Response)", sizeof(p->response)-1);
  248. return -1;
  249. }
  250. /* save char */
  251. p->response[i++] = c;
  252. p->response[i] = 0;
  253. /* if end of input */
  254. if (c == '\n') break;
  255. }
  256. if (c >= 0) /* if input terminated normally */
  257. {
  258. /* ignore just CR/LF */
  259. if (!strcmp(p->response,"\r\n"))
  260. {
  261. /* reset input buffer stuff */
  262. i = 0;
  263. p->response[0] = 0;
  264. }
  265. else /* otherwise return with info in buffer */
  266. {
  267. return 0;
  268. }
  269. }
  270. } while(timeout > 0);
  271. strncpy(p->response, "(No Response)", sizeof(p->response)-1);
  272. return -1;
  273. }
  274. int ast_modem_expect(struct ast_modem_pvt *p, char *result, int timeout)
  275. {
  276. int res = -1;
  277. timeout *= 1000;
  278. strncpy(p->response, "(No Response)", sizeof(p->response)-1);
  279. do {
  280. res = ast_waitfor_n_fd(&p->fd, 1, &timeout, NULL);
  281. if (res < 0) {
  282. return -1;
  283. }
  284. /* Read a response */
  285. fgets(p->response, sizeof(p->response), p->f);
  286. #if 0
  287. fprintf(stderr, "Modem said: %s", p->response);
  288. #endif
  289. if (!strncasecmp(p->response, result, strlen(result)))
  290. return 0;
  291. } while(timeout > 0);
  292. return -1;
  293. }
  294. void ast_modem_trim(char *s)
  295. {
  296. int x;
  297. x = strlen(s) - 1;
  298. while(x >= 0) {
  299. if ((s[x] != '\r') && (s[x] != '\n') && (s[x] != ' '))
  300. break;
  301. s[x] = '\0';
  302. x--;
  303. }
  304. }
  305. static int modem_setup(struct ast_modem_pvt *p, int baudrate)
  306. {
  307. /* Make sure there's a modem there and that it's in a reasonable
  308. mode. Set the baud rate, etc. */
  309. char identity[256];
  310. char *ident = NULL;
  311. char etx[2] = { 0x10, '!' };
  312. if (option_debug)
  313. ast_log(LOG_DEBUG, "Setting up modem %s\n", p->dev);
  314. if (ast_modem_send(p, etx, 2)) {
  315. ast_log(LOG_WARNING, "Failed to send ETX?\n");
  316. return -1;
  317. }
  318. if (ast_modem_send(p, "\r\n", 2)) {
  319. ast_log(LOG_WARNING, "Failed to send enter?\n");
  320. return -1;
  321. }
  322. usleep(10000);
  323. /* Read any outstanding stuff */
  324. while(!ast_modem_read_response(p, 0));
  325. if (ast_modem_send(p, "ATZ", 0)) {
  326. ast_log(LOG_WARNING, "Modem not responding on %s\n", p->dev);
  327. return -1;
  328. }
  329. if (ast_modem_expect(p, "OK", ECHO_TIMEOUT)) {
  330. ast_log(LOG_WARNING, "Modem reset failed: %s\n", p->response);
  331. return -1;
  332. }
  333. if (ast_modem_send(p, p->initstr, 0)) {
  334. ast_log(LOG_WARNING, "Modem not responding on %s\n", p->dev);
  335. return -1;
  336. }
  337. if (ast_modem_expect(p, "OK", ECHO_TIMEOUT)) {
  338. ast_log(LOG_WARNING, "Modem initialization failed: %s\n", p->response);
  339. return -1;
  340. }
  341. if (ast_modem_send(p, "ATI3", 0)) {
  342. ast_log(LOG_WARNING, "Modem not responding on %s\n", p->dev);
  343. return -1;
  344. }
  345. if (ast_modem_read_response(p, ECHO_TIMEOUT)) {
  346. ast_log(LOG_WARNING, "Modem did not provide identification\n");
  347. return -1;
  348. }
  349. strncpy(identity, p->response, sizeof(identity)-1);
  350. ast_modem_trim(identity);
  351. if (ast_modem_expect(p, "OK", ECHO_TIMEOUT)) {
  352. ast_log(LOG_WARNING, "Modem did not provide identification\n");
  353. return -1;
  354. }
  355. if (!strcasecmp(mtype, "autodetect")) {
  356. p->mc = find_capability(identity);
  357. if (!p->mc) {
  358. ast_log(LOG_WARNING, "Unable to autodetect modem. You'll need to specify a driver in modem.conf. Please report modem identification (%s) and which driver works to markster@linux-support.net.\n", identity);
  359. return -1;
  360. }
  361. } else {
  362. p->mc = find_driver(mtype);
  363. if (!p->mc) {
  364. ast_log(LOG_WARNING, "No driver for modem type '%s'\n", mtype);
  365. return -1;
  366. }
  367. }
  368. if (p->mc->init) {
  369. if (p->mc->init(p)) {
  370. ast_log(LOG_WARNING, "Modem Initialization Failed on '%s', driver %s.\n", p->dev, p->mc->name);
  371. p->mc->decusecnt();
  372. return -1;
  373. }
  374. }
  375. if (option_verbose > 2) {
  376. ast_verbose(VERBOSE_PREFIX_3 "Configured modem %s with driver %s (%s)\n", p->dev, p->mc->name, p->mc->identify ? (ident = p->mc->identify(p)) : "No identification");
  377. }
  378. if (ident)
  379. free(ident);
  380. return 0;
  381. }
  382. static int modem_hangup(struct ast_channel *ast)
  383. {
  384. struct ast_modem_pvt *p;
  385. if (option_debug)
  386. ast_log(LOG_DEBUG, "modem_hangup(%s)\n", ast->name);
  387. p = ast->pvt->pvt;
  388. /* Hang up */
  389. if (p->mc->hangup)
  390. p->mc->hangup(p);
  391. /* Re-initialize */
  392. if (p->mc->init)
  393. p->mc->init(p);
  394. ast_setstate(ast, AST_STATE_DOWN);
  395. memset(p->cid, 0, sizeof(p->cid));
  396. memset(p->dnid, 0, sizeof(p->dnid));
  397. ((struct ast_modem_pvt *)(ast->pvt->pvt))->owner = NULL;
  398. ast_mutex_lock(&usecnt_lock);
  399. usecnt--;
  400. if (usecnt < 0)
  401. ast_log(LOG_WARNING, "Usecnt < 0???\n");
  402. ast_mutex_unlock(&usecnt_lock);
  403. ast_update_use_count();
  404. if (option_verbose > 2)
  405. ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
  406. ast->pvt->pvt = NULL;
  407. ast_setstate(ast, AST_STATE_DOWN);
  408. restart_monitor();
  409. return 0;
  410. }
  411. static int modem_answer(struct ast_channel *ast)
  412. {
  413. struct ast_modem_pvt *p;
  414. int res=0;
  415. if (option_debug)
  416. ast_log(LOG_DEBUG, "modem_answer(%s)\n", ast->name);
  417. p = ast->pvt->pvt;
  418. if (p->mc->answer) {
  419. res = p->mc->answer(p);
  420. }
  421. if (!res) {
  422. ast->rings = 0;
  423. ast_setstate(ast, AST_STATE_UP);
  424. }
  425. return res;
  426. }
  427. #if 0
  428. static char modem_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 *modem_read(struct ast_channel *ast)
  441. {
  442. struct ast_modem_pvt *p = ast->pvt->pvt;
  443. struct ast_frame *fr=NULL;
  444. if (p->mc->read)
  445. fr = p->mc->read(p);
  446. return fr;
  447. }
  448. static int modem_write(struct ast_channel *ast, struct ast_frame *frame)
  449. {
  450. int res=0;
  451. long flags;
  452. struct ast_modem_pvt *p = ast->pvt->pvt;
  453. /* Modems tend to get upset when they receive data whilst in
  454. * command mode. This makes esp. dial commands short lived.
  455. * Pauline Middelink - 2002-09-24 */
  456. if (ast->_state != AST_STATE_UP)
  457. return 0;
  458. /* Temporarily make non-blocking */
  459. flags = fcntl(ast->fds[0], F_GETFL);
  460. fcntl(ast->fds[0], F_SETFL, flags | O_NONBLOCK);
  461. if (p->mc->write)
  462. res = p->mc->write(p, frame);
  463. /* Block again */
  464. fcntl(ast->fds[0], F_SETFL, flags);
  465. return res;
  466. }
  467. static int modem_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  468. {
  469. struct ast_modem_pvt *p = newchan->pvt->pvt;
  470. ast_log(LOG_WARNING, "fixup called\n");
  471. if (p->owner!=oldchan) {
  472. ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n",oldchan,p->owner);
  473. return -1;
  474. }
  475. p->owner = newchan;
  476. return 0;
  477. }
  478. struct ast_channel *ast_modem_new(struct ast_modem_pvt *i, int state)
  479. {
  480. struct ast_channel *tmp;
  481. tmp = ast_channel_alloc(1);
  482. if (tmp) {
  483. snprintf(tmp->name, sizeof(tmp->name), "Modem[%s]/%s", i->mc->name, i->dev + 5);
  484. tmp->type = type;
  485. tmp->fds[0] = i->fd;
  486. tmp->nativeformats = i->mc->formats;
  487. ast_setstate(tmp, state);
  488. if (state == AST_STATE_RING)
  489. tmp->rings = 1;
  490. tmp->pvt->pvt = i;
  491. tmp->pvt->send_digit = modem_digit;
  492. tmp->pvt->call = modem_call;
  493. tmp->pvt->hangup = modem_hangup;
  494. tmp->pvt->answer = modem_answer;
  495. tmp->pvt->read = modem_read;
  496. tmp->pvt->write = modem_write;
  497. tmp->pvt->fixup = modem_fixup;
  498. strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
  499. if (strlen(i->cid))
  500. tmp->callerid = strdup(i->cid);
  501. if (strlen(i->language))
  502. strncpy(tmp->language,i->language, sizeof(tmp->language)-1);
  503. if (strlen(i->dnid))
  504. strncpy(tmp->exten, i->dnid, sizeof(tmp->exten) - 1);
  505. i->owner = tmp;
  506. ast_mutex_lock(&usecnt_lock);
  507. usecnt++;
  508. ast_mutex_unlock(&usecnt_lock);
  509. ast_update_use_count();
  510. if (state != AST_STATE_DOWN) {
  511. if (ast_pbx_start(tmp)) {
  512. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
  513. ast_hangup(tmp);
  514. tmp = NULL;
  515. }
  516. }
  517. } else
  518. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  519. return tmp;
  520. }
  521. static void modem_mini_packet(struct ast_modem_pvt *i)
  522. {
  523. struct ast_frame *fr;
  524. fr = i->mc->read(i);
  525. if (!fr) return;
  526. if (fr->frametype == AST_FRAME_CONTROL) {
  527. if (fr->subclass == AST_CONTROL_RING) {
  528. ast_modem_new(i, AST_STATE_RING);
  529. }
  530. }
  531. }
  532. static void *do_monitor(void *data)
  533. {
  534. fd_set rfds, efds;
  535. int n, res;
  536. struct ast_modem_pvt *i;
  537. /* This thread monitors all the frame relay interfaces which are not yet in use
  538. (and thus do not have a separate thread) indefinitely */
  539. /* From here on out, we die whenever asked */
  540. #if 0
  541. if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
  542. ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
  543. return NULL;
  544. }
  545. #endif
  546. for(;;) {
  547. /* Don't let anybody kill us right away. Nobody should lock the interface list
  548. and wait for the monitor list, but the other way around is okay. */
  549. if (ast_mutex_lock(&monlock)) {
  550. ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
  551. return NULL;
  552. }
  553. /* Lock the interface list */
  554. if (ast_mutex_lock(&iflock)) {
  555. ast_log(LOG_ERROR, "Unable to grab interface lock\n");
  556. ast_mutex_unlock(&monlock);
  557. return NULL;
  558. }
  559. /* Build the stuff we're going to select on, that is the socket of every
  560. ast_modem_pvt that does not have an associated owner channel */
  561. n = -1;
  562. FD_ZERO(&rfds);
  563. FD_ZERO(&efds);
  564. i = iflist;
  565. while(i) {
  566. if (FD_ISSET(i->fd, &rfds))
  567. ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->fd, i->dev);
  568. if (!i->owner) {
  569. /* This needs to be watched, as it lacks an owner */
  570. FD_SET(i->fd, &rfds);
  571. FD_SET(i->fd, &efds);
  572. if (i->fd > n)
  573. n = i->fd;
  574. }
  575. i = i->next;
  576. }
  577. /* Okay, now that we know what to do, release the interface lock */
  578. ast_mutex_unlock(&iflock);
  579. /* And from now on, we're okay to be killed, so release the monitor lock as well */
  580. ast_mutex_unlock(&monlock);
  581. #if 0
  582. ast_log(LOG_DEBUG, "In monitor, n=%d, pid=%d\n", n, getpid());
  583. #endif
  584. /* Wait indefinitely for something to happen */
  585. pthread_testcancel();
  586. res = ast_select(n + 1, &rfds, NULL, &efds, NULL);
  587. pthread_testcancel();
  588. /* Okay, select has finished. Let's see what happened. */
  589. if (res < 1) {
  590. if ((errno != EINTR) && (errno != EAGAIN))
  591. ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
  592. continue;
  593. }
  594. /* Alright, lock the interface list again, and let's look and see what has
  595. happened */
  596. if (ast_mutex_lock(&iflock)) {
  597. ast_log(LOG_WARNING, "Unable to lock the interface list\n");
  598. continue;
  599. }
  600. i = iflist;
  601. while(i) {
  602. if (FD_ISSET(i->fd, &rfds) || FD_ISSET(i->fd, &efds)) {
  603. if (i->owner) {
  604. ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d, %s)...\n", i->fd, i->dev);
  605. i = i->next;
  606. continue;
  607. }
  608. modem_mini_packet(i);
  609. }
  610. i=i->next;
  611. }
  612. ast_mutex_unlock(&iflock);
  613. }
  614. /* Never reached */
  615. return NULL;
  616. }
  617. static int restart_monitor()
  618. {
  619. /* If we're supposed to be stopped -- stay stopped */
  620. if (monitor_thread == AST_PTHREADT_STOP)
  621. return 0;
  622. if (ast_mutex_lock(&monlock)) {
  623. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  624. return -1;
  625. }
  626. if (monitor_thread == pthread_self()) {
  627. ast_mutex_unlock(&monlock);
  628. ast_log(LOG_WARNING, "Cannot kill myself\n");
  629. return -1;
  630. }
  631. if (monitor_thread != AST_PTHREADT_NULL) {
  632. pthread_cancel(monitor_thread);
  633. /* Nudge it a little, as it's probably stuck in select */
  634. pthread_kill(monitor_thread, SIGURG);
  635. pthread_join(monitor_thread, NULL);
  636. }
  637. /* Start a new monitor */
  638. if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
  639. ast_mutex_unlock(&monlock);
  640. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  641. return -1;
  642. }
  643. ast_mutex_unlock(&monlock);
  644. return 0;
  645. }
  646. static void stty(struct ast_modem_pvt *p)
  647. {
  648. struct termios mode;
  649. memset(&mode, 0, sizeof(mode));
  650. if (tcgetattr(p->fd, &mode)) {
  651. ast_log(LOG_WARNING, "Unable to get serial parameters on %s: %s\n", p->dev, strerror(errno));
  652. return;
  653. }
  654. cfmakeraw(&mode);
  655. cfsetspeed(&mode, B115200);
  656. if (tcsetattr(p->fd, TCSANOW, &mode))
  657. ast_log(LOG_WARNING, "Unable to set serial parameters on %s: %s\n", p->dev, strerror(errno));
  658. }
  659. static struct ast_modem_pvt *mkif(char *iface)
  660. {
  661. /* Make a ast_modem_pvt structure for this interface */
  662. struct ast_modem_pvt *tmp;
  663. #if 0
  664. int flags;
  665. #endif
  666. tmp = malloc(sizeof(struct ast_modem_pvt));
  667. if (tmp) {
  668. memset(tmp, 0, sizeof(struct ast_modem_pvt));
  669. tmp->fd = open(iface, O_RDWR | O_NONBLOCK);
  670. if (tmp->fd < 0) {
  671. ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
  672. free(tmp);
  673. return NULL;
  674. }
  675. strncpy(tmp->language, language, sizeof(tmp->language)-1);
  676. strncpy(tmp->msn, msn, sizeof(tmp->msn)-1);
  677. strncpy(tmp->incomingmsn, incomingmsn, sizeof(tmp->incomingmsn)-1);
  678. tmp->dtmfmode = dtmfmode;
  679. tmp->dtmfmodegen = dtmfmodegen;
  680. snprintf(tmp->outgoingmsn, sizeof(tmp->outgoingmsn), ",%s,", outgoingmsn);
  681. strncpy(tmp->dev, iface, sizeof(tmp->dev)-1);
  682. /* Maybe in the future we want to allow variable
  683. serial settings */
  684. stty(tmp);
  685. tmp->f = fdopen(tmp->fd, "w+");
  686. /* Disable buffering */
  687. setvbuf(tmp->f, NULL, _IONBF,0);
  688. if (tmp->f < 0) {
  689. ast_log(LOG_WARNING, "Unable to fdopen '%s'\n", iface);
  690. free(tmp);
  691. return NULL;
  692. }
  693. tmp->owner = NULL;
  694. tmp->ministate = 0;
  695. tmp->stripmsd = stripmsd;
  696. tmp->dialtype = dialtype;
  697. tmp->mode = gmode;
  698. tmp->group = cur_group;
  699. memset(tmp->cid, 0, sizeof(tmp->cid));
  700. strncpy(tmp->context, context, sizeof(tmp->context)-1);
  701. strncpy(tmp->initstr, initstr, sizeof(tmp->initstr)-1);
  702. tmp->next = NULL;
  703. tmp->obuflen = 0;
  704. if (modem_setup(tmp, baudrate) < 0) {
  705. ast_log(LOG_WARNING, "Unable to configure modem '%s'\n", iface);
  706. free(tmp);
  707. return NULL;
  708. }
  709. }
  710. return tmp;
  711. }
  712. static struct ast_channel *modem_request(char *type, int format, void *data)
  713. {
  714. int oldformat;
  715. struct ast_modem_pvt *p;
  716. struct ast_channel *tmp = NULL;
  717. char dev[80];
  718. unsigned int group = 0;
  719. char *stringp=NULL;
  720. strncpy(dev, (char *)data, sizeof(dev)-1);
  721. stringp=dev;
  722. strsep(&stringp, ":");
  723. oldformat = format;
  724. if (dev[0]=='g' && isdigit(dev[1])) {
  725. /* Retrieve the group number */
  726. if (sscanf(dev+1, "%u", &group) < 1) {
  727. ast_log(LOG_WARNING, "Unable to determine group from [%s]\n", (char *)data);
  728. return NULL;
  729. }
  730. group = 1 << group;
  731. }
  732. /* Search for an unowned channel */
  733. if (ast_mutex_lock(&iflock)) {
  734. ast_log(LOG_ERROR, "Unable to lock interface list???\n");
  735. return NULL;
  736. }
  737. p = iflist;
  738. while(p) {
  739. if (group) {
  740. /* if it belongs to the proper group, and the format matches
  741. * and it is not in use, we found a candidate! */
  742. if (p->group & group &&
  743. p->mc->formats & format &&
  744. !p->owner) {
  745. /* XXX Not quite sure that not having an owner is
  746. * sufficient evidence of beeing a free device XXX */
  747. tmp = ast_modem_new(p, AST_STATE_DOWN);
  748. restart_monitor();
  749. break;
  750. }
  751. } else {
  752. if (!strcmp(dev, p->dev + 5)) {
  753. if (p->mc->formats & format) {
  754. if (!p->owner) {
  755. tmp = ast_modem_new(p, AST_STATE_DOWN);
  756. restart_monitor();
  757. break;
  758. } else
  759. ast_log(LOG_WARNING, "Device '%s' is busy\n", p->dev);
  760. } else
  761. ast_log(LOG_WARNING, "Asked for a format %s line on %s\n", ast_getformatname(format), p->dev);
  762. break;
  763. }
  764. }
  765. p = p->next;
  766. }
  767. if (!p)
  768. ast_log(LOG_WARNING, "Requested device '%s' does not exist\n", dev);
  769. ast_mutex_unlock(&iflock);
  770. return tmp;
  771. }
  772. static unsigned int get_group(char *s)
  773. {
  774. char *piece;
  775. int start, finish,x;
  776. unsigned int group = 0;
  777. char *copy = ast_strdupa(s);
  778. char *stringp=NULL;
  779. if (!copy) {
  780. ast_log(LOG_ERROR, "Out of memory\n");
  781. return 0;
  782. }
  783. stringp=copy;
  784. piece = strsep(&stringp, ",");
  785. while(piece) {
  786. if (sscanf(piece, "%d-%d", &start, &finish) == 2) {
  787. /* Range */
  788. } else if (sscanf(piece, "%d", &start)) {
  789. /* Just one */
  790. finish = start;
  791. } else {
  792. ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'. Using '0'\n", s,piece);
  793. return 0;
  794. }
  795. piece = strsep(&stringp, ",");
  796. for (x=start;x<=finish;x++) {
  797. if ((x > 31) || (x < 0)) {
  798. ast_log(LOG_WARNING, "Ignoring invalid group %d\n", x);
  799. break;
  800. }
  801. group |= (1 << x);
  802. }
  803. }
  804. return group;
  805. }
  806. static int __unload_module(void)
  807. {
  808. struct ast_modem_pvt *p, *pl;
  809. /* First, take us out of the channel loop */
  810. ast_channel_unregister(type);
  811. if (!ast_mutex_lock(&iflock)) {
  812. /* Hangup all interfaces if they have an owner */
  813. p = iflist;
  814. while(p) {
  815. if (p->owner)
  816. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  817. p = p->next;
  818. }
  819. iflist = NULL;
  820. ast_mutex_unlock(&iflock);
  821. } else {
  822. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  823. return -1;
  824. }
  825. if (!ast_mutex_lock(&monlock)) {
  826. if (monitor_thread != AST_PTHREADT_NULL && monitor_thread != AST_PTHREADT_STOP) {
  827. pthread_cancel(monitor_thread);
  828. pthread_join(monitor_thread, NULL);
  829. }
  830. monitor_thread = AST_PTHREADT_STOP;
  831. ast_mutex_unlock(&monlock);
  832. } else {
  833. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  834. return -1;
  835. }
  836. if (!ast_mutex_lock(&iflock)) {
  837. /* Destroy all the interfaces and free their memory */
  838. p = iflist;
  839. while(p) {
  840. /* Close the socket, assuming it's real */
  841. if (p->fd > -1)
  842. close(p->fd);
  843. pl = p;
  844. p = p->next;
  845. /* Free associated memory */
  846. free(pl);
  847. }
  848. iflist = NULL;
  849. ast_mutex_unlock(&iflock);
  850. } else {
  851. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  852. return -1;
  853. }
  854. return 0;
  855. }
  856. int unload_module()
  857. {
  858. return __unload_module();
  859. }
  860. int load_module()
  861. {
  862. struct ast_config *cfg;
  863. struct ast_variable *v;
  864. struct ast_modem_pvt *tmp;
  865. char driver[80];
  866. cfg = ast_load(config);
  867. /* We *must* have a config file otherwise stop immediately */
  868. if (!cfg) {
  869. ast_log(LOG_ERROR, "Unable to load config %s\n", config);
  870. return -1;
  871. }
  872. if (ast_mutex_lock(&iflock)) {
  873. /* It's a little silly to lock it, but we mind as well just to be sure */
  874. ast_log(LOG_ERROR, "Unable to lock interface list???\n");
  875. return -1;
  876. }
  877. v = ast_variable_browse(cfg, "interfaces");
  878. while(v) {
  879. /* Create the interface list */
  880. if (!strcasecmp(v->name, "device")) {
  881. tmp = mkif(v->value);
  882. if (tmp) {
  883. tmp->next = iflist;
  884. iflist = tmp;
  885. } else {
  886. ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
  887. ast_destroy(cfg);
  888. ast_mutex_unlock(&iflock);
  889. __unload_module();
  890. return -1;
  891. }
  892. } else if (!strcasecmp(v->name, "driver")) {
  893. snprintf(driver, sizeof(driver), "chan_modem_%s.so", v->value);
  894. if (option_verbose > 1)
  895. ast_verbose(VERBOSE_PREFIX_2 "Loading modem driver %s", driver);
  896. if (ast_load_resource(driver)) {
  897. ast_log(LOG_ERROR, "Failed to load driver %s\n", driver);
  898. ast_destroy(cfg);
  899. ast_mutex_unlock(&iflock);
  900. __unload_module();
  901. return -1;
  902. }
  903. } else if (!strcasecmp(v->name, "mode")) {
  904. if (!strncasecmp(v->value, "ri", 2))
  905. gmode = MODEM_MODE_WAIT_RING;
  906. else if (!strncasecmp(v->value, "im", 2))
  907. gmode = MODEM_MODE_IMMEDIATE;
  908. else if (!strncasecmp(v->value, "an", 2))
  909. gmode = MODEM_MODE_WAIT_ANSWER;
  910. else
  911. ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
  912. } else if (!strcasecmp(v->name, "stripmsd")) {
  913. stripmsd = atoi(v->value);
  914. } else if (!strcasecmp(v->name, "type")) {
  915. strncpy(mtype, v->value, sizeof(mtype)-1);
  916. } else if (!strcasecmp(v->name, "initstr")) {
  917. strncpy(initstr, v->value, sizeof(initstr)-1);
  918. } else if (!strcasecmp(v->name, "dialtype")) {
  919. dialtype = toupper(v->value[0]);
  920. } else if (!strcasecmp(v->name, "context")) {
  921. strncpy(context, v->value, sizeof(context)-1);
  922. } else if (!strcasecmp(v->name, "msn")) {
  923. strncpy(msn, v->value, sizeof(msn)-1);
  924. } else if (!strcasecmp(v->name, "incomingmsn")) {
  925. strncpy(incomingmsn, v->value, sizeof(incomingmsn)-1);
  926. } else if (!strcasecmp(v->name, "dtmfmode")) {
  927. char tmp[80];
  928. char *alt;
  929. strncpy(tmp, v->value, sizeof(tmp) - 1);
  930. alt = strchr(tmp, '/');
  931. if (!strcasecmp(tmp, "none"))
  932. dtmfmode=MODEM_DTMF_NONE;
  933. else if (!strcasecmp(tmp, "asterisk"))
  934. dtmfmode = MODEM_DTMF_AST;
  935. else if (!strcasecmp(tmp, "i4l"))
  936. dtmfmode = MODEM_DTMF_I4L;
  937. else {
  938. ast_log(LOG_WARNING, "Unknown dtmf detection mode '%s', using 'asterisk'\n", v->value);
  939. dtmfmode = MODEM_DTMF_AST;
  940. }
  941. if (alt) {
  942. if (!strcasecmp(alt, "none"))
  943. dtmfmodegen=MODEM_DTMF_NONE;
  944. else if (!strcasecmp(alt, "asterisk"))
  945. dtmfmodegen = MODEM_DTMF_AST;
  946. else if (!strcasecmp(alt, "i4l"))
  947. dtmfmodegen = MODEM_DTMF_I4L;
  948. else if (!strcasecmp(alt, "both"))
  949. dtmfmodegen = MODEM_DTMF_I4L | MODEM_DTMF_AST;
  950. else {
  951. ast_log(LOG_WARNING, "Unknown dtmf generation mode '%s', using 'asterisk'\n", v->value);
  952. dtmfmodegen = MODEM_DTMF_AST;
  953. }
  954. } else
  955. dtmfmodegen = dtmfmode;
  956. } else if (!strcasecmp(v->name, "outgoingmsn")) {
  957. strncpy(outgoingmsn, v->value, sizeof(outgoingmsn)-1);
  958. } else if (!strcasecmp(v->name, "language")) {
  959. strncpy(language, v->value, sizeof(language)-1);
  960. } else if (!strcasecmp(v->name, "group")) {
  961. cur_group = get_group(v->value);
  962. }
  963. v = v->next;
  964. }
  965. ast_mutex_unlock(&iflock);
  966. if (ast_channel_register(type, tdesc, /* XXX Don't know our types -- maybe we should register more than one XXX */
  967. AST_FORMAT_SLINEAR, modem_request)) {
  968. ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
  969. ast_destroy(cfg);
  970. __unload_module();
  971. return -1;
  972. }
  973. ast_destroy(cfg);
  974. /* And start the monitor for the first time */
  975. restart_monitor();
  976. return 0;
  977. }
  978. int usecount(void)
  979. {
  980. int res;
  981. ast_mutex_lock(&usecnt_lock);
  982. res = usecnt;
  983. ast_mutex_unlock(&usecnt_lock);
  984. return res;
  985. }
  986. char *description()
  987. {
  988. return desc;
  989. }
  990. char *key()
  991. {
  992. return ASTERISK_GPL_KEY;
  993. }