chan_modem_aopen.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 <stdlib.h>
  16. #include <errno.h>
  17. #include <asterisk/lock.h>
  18. #include <asterisk/vmodem.h>
  19. #include <asterisk/module.h>
  20. #include <asterisk/frame.h>
  21. #include <asterisk/logger.h>
  22. #include <asterisk/options.h>
  23. #define STATE_COMMAND 0
  24. #define STATE_VOICE 1
  25. #define VRA "40" /* Number of 100ms of non-ring after a ring cadence after which we consider the lien to be answered */
  26. #define VRN "100" /* Number of 100ms of non-ring with no cadence after which we assume an answer */
  27. static char *breakcmd = "\0x10\0x03";
  28. static char *desc = "A/Open (Rockwell Chipset) ITU-2 VoiceModem Driver";
  29. static int usecnt;
  30. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  31. static char *aopen_idents[] = {
  32. /* Identify A/Open Modem */
  33. "V2.210-V90_2M_DLP",
  34. NULL
  35. };
  36. static int aopen_setdev(struct ast_modem_pvt *p, int dev)
  37. {
  38. char cmd[80];
  39. if (ast_modem_send(p, "AT#VLS?", 0)) {
  40. ast_log(LOG_WARNING, "Unable to select current mode %d\n", dev);
  41. return -1;
  42. }
  43. if (ast_modem_read_response(p, 5)) {
  44. ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
  45. return -1;
  46. }
  47. ast_modem_trim(p->response);
  48. strncpy(cmd, p->response, sizeof(cmd)-1);
  49. if (ast_modem_expect(p, "OK", 5)) {
  50. ast_log(LOG_WARNING, "Modem did not respond properly\n");
  51. return -1;
  52. }
  53. if (dev == atoi(cmd)) {
  54. /* We're already in the right mode, don't bother changing for fear of
  55. hanging up */
  56. return 0;
  57. }
  58. snprintf(cmd, sizeof(cmd), "AT#VLS=%d", dev);
  59. if (ast_modem_send(p, cmd, 0)) {
  60. ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
  61. return -1;
  62. }
  63. if (ast_modem_read_response(p, 5)) {
  64. ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
  65. return -1;
  66. }
  67. ast_modem_trim(p->response);
  68. if (strcasecmp(p->response, "VCON") && strcasecmp(p->response, "OK")) {
  69. ast_log(LOG_WARNING, "Unexpected reply: %s\n", p->response);
  70. return -1;
  71. }
  72. return 0;
  73. }
  74. static int aopen_startrec(struct ast_modem_pvt *p)
  75. {
  76. if (ast_modem_send(p, "AT#VRX", 0) ||
  77. ast_modem_expect(p, "CONNECT", 5)) {
  78. ast_log(LOG_WARNING, "Unable to start recording\n");
  79. return -1;
  80. }
  81. p->ministate = STATE_VOICE;
  82. return 0;
  83. }
  84. static int aopen_break(struct ast_modem_pvt *p)
  85. {
  86. if (ast_modem_send(p, "\r\n", 2)) {
  87. ast_log(LOG_WARNING, "Failed to send enter?\n");
  88. return -1;
  89. }
  90. if (ast_modem_send(p, breakcmd, 2)) {
  91. ast_log(LOG_WARNING, "Failed to break\n");
  92. return -1;
  93. }
  94. if (ast_modem_send(p, "\r\n", 2)) {
  95. ast_log(LOG_WARNING, "Failed to send enter?\n");
  96. return -1;
  97. }
  98. /* Read any outstanding junk */
  99. while(!ast_modem_read_response(p, 1));
  100. if (ast_modem_send(p, "AT", 0)) {
  101. /* Modem might be stuck in some weird mode, try to get it out */
  102. ast_modem_send(p, "+++", 3);
  103. if (ast_modem_expect(p, "OK", 10)) {
  104. ast_log(LOG_WARNING, "Modem is not responding\n");
  105. return -1;
  106. }
  107. if (ast_modem_send(p, "AT", 0)) {
  108. ast_log(LOG_WARNING, "Modem is not responding\n");
  109. return -1;
  110. }
  111. }
  112. if (ast_modem_expect(p, "OK", 5)) {
  113. ast_log(LOG_WARNING, "Modem did not respond properly\n");
  114. return -1;
  115. }
  116. return 0;
  117. }
  118. static int aopen_init(struct ast_modem_pvt *p)
  119. {
  120. if (option_debug)
  121. ast_log(LOG_DEBUG, "aopen_init()\n");
  122. if (aopen_break(p))
  123. return -1;
  124. /* Force into command mode */
  125. p->ministate = STATE_COMMAND;
  126. if (ast_modem_send(p, "AT#BDR=0", 0) ||
  127. ast_modem_expect(p, "OK", 5)) {
  128. ast_log(LOG_WARNING, "Unable to set to auto-baud\n");
  129. return -1;
  130. }
  131. if (ast_modem_send(p, "AT#CLS=8", 0) ||
  132. ast_modem_expect(p, "OK", 5)) {
  133. ast_log(LOG_WARNING, "Unable to set to voice mode\n");
  134. return -1;
  135. }
  136. if (ast_modem_send(p, "AT#VBS=8", 0) ||
  137. ast_modem_expect(p, "OK", 5)) {
  138. ast_log(LOG_WARNING, "Unable to set to 8-bit mode\n");
  139. return -1;
  140. }
  141. if (ast_modem_send(p, "AT#VSR=8000", 0) ||
  142. ast_modem_expect(p, "OK", 5)) {
  143. ast_log(LOG_WARNING, "Unable to set to 8000 Hz sampling\n");
  144. return -1;
  145. }
  146. if (ast_modem_send(p, "AT#VLS=0", 0) ||
  147. ast_modem_expect(p, "OK", 5)) {
  148. ast_log(LOG_WARNING, "Unable to set to telco interface\n");
  149. return -1;
  150. }
  151. if (ast_modem_send(p, "AT#VRA=" VRA, 0) ||
  152. ast_modem_expect(p, "OK", 5)) {
  153. ast_log(LOG_WARNING, "Unable to set to 'ringback goes away' timer\n");
  154. return -1;
  155. }
  156. if (ast_modem_send(p, "AT#VRN=" VRN, 0) ||
  157. ast_modem_expect(p, "OK", 5)) {
  158. ast_log(LOG_WARNING, "Unable to set to 'ringback never came timer'\n");
  159. return -1;
  160. }
  161. if (ast_modem_send(p, "AT#VTD=3F,3F,3F", 0) ||
  162. ast_modem_expect(p, "OK", 5)) {
  163. ast_log(LOG_WARNING, "Unable to set to tone detection\n");
  164. return -1;
  165. }
  166. return 0;
  167. }
  168. static struct ast_frame *aopen_handle_escape(struct ast_modem_pvt *p, char esc)
  169. {
  170. /* Handle escaped characters -- but sometimes we call it directly as
  171. a quick way to cause known responses */
  172. p->fr.frametype = AST_FRAME_NULL;
  173. p->fr.subclass = 0;
  174. p->fr.data = NULL;
  175. p->fr.datalen = 0;
  176. p->fr.samples = 0;
  177. p->fr.offset = 0;
  178. p->fr.mallocd = 0;
  179. p->fr.delivery.tv_sec = 0;
  180. p->fr.delivery.tv_usec = 0;
  181. if (esc)
  182. ast_log(LOG_DEBUG, "Escaped character '%c'\n", esc);
  183. switch(esc) {
  184. case 'R': /* Pseudo ring */
  185. p->fr.frametype = AST_FRAME_CONTROL;
  186. p->fr.subclass = AST_CONTROL_RING;
  187. return &p->fr;
  188. case 'X': /* Pseudo connect */
  189. p->fr.frametype = AST_FRAME_CONTROL;
  190. p->fr.subclass = AST_CONTROL_RING;
  191. if (p->owner)
  192. ast_setstate(p->owner, AST_STATE_UP);
  193. if (aopen_startrec(p))
  194. return NULL;
  195. return &p->fr;
  196. case 'b': /* Busy signal */
  197. p->fr.frametype = AST_FRAME_CONTROL;
  198. p->fr.subclass = AST_CONTROL_BUSY;
  199. return &p->fr;
  200. case 'o': /* Overrun */
  201. ast_log(LOG_WARNING, "Overflow on modem, flushing buffers\n");
  202. if (ast_modem_send(p, "\0x10E", 2))
  203. ast_log(LOG_WARNING, "Unable to flush buffers\n");
  204. return &p->fr;
  205. case 'u': /* Underrun */
  206. ast_log(LOG_WARNING, "Data underrun\n");
  207. /* Fall Through */
  208. case CHAR_ETX: /* End Transmission */
  209. case 'd': /* Dialtone */
  210. case 'c': /* Calling Tone */
  211. case 'e': /* European version */
  212. case 'a': /* Answer Tone */
  213. case 'f': /* Bell Answer Tone */
  214. case 'T': /* Timing mark */
  215. case 't': /* Handset off hook */
  216. case 'h': /* Handset hungup */
  217. case 0: /* Pseudo signal */
  218. /* Ignore */
  219. return &p->fr;
  220. default:
  221. ast_log(LOG_DEBUG, "Unknown Escaped character '%c' (%d)\n", esc, esc);
  222. }
  223. return &p->fr;
  224. }
  225. static struct ast_frame *aopen_read(struct ast_modem_pvt *p)
  226. {
  227. char result[256];
  228. short *b;
  229. struct ast_frame *f=NULL;
  230. int res;
  231. int x;
  232. if (p->ministate == STATE_COMMAND) {
  233. /* Read the first two bytes, first, in case it's a control message */
  234. fread(result, 1, 2, p->f);
  235. if (result[0] == CHAR_DLE) {
  236. return aopen_handle_escape(p, result[1]);
  237. } else {
  238. if ((result[0] == '\n') || (result[0] == '\r'))
  239. return aopen_handle_escape(p, 0);
  240. /* Read the rest of the line */
  241. fgets(result + 2, sizeof(result) - 2, p->f);
  242. ast_modem_trim(result);
  243. if (!strcasecmp(result, "VCON")) {
  244. /* If we're in immediate mode, reply now */
  245. if (p->mode == MODEM_MODE_IMMEDIATE)
  246. return aopen_handle_escape(p, 'X');
  247. } else
  248. if (!strcasecmp(result, "BUSY")) {
  249. /* Same as a busy signal */
  250. return aopen_handle_escape(p, 'b');
  251. } else
  252. if (!strcasecmp(result, "RING")) {
  253. return aopen_handle_escape(p, 'R');
  254. } else
  255. if (!strcasecmp(result, "NO DIALTONE")) {
  256. /* There's no dialtone, so the line isn't working */
  257. ast_log(LOG_WARNING, "Device '%s' lacking dialtone\n", p->dev);
  258. return NULL;
  259. }
  260. ast_log(LOG_DEBUG, "Modem said '%s'\n", result);
  261. return aopen_handle_escape(p, 0);
  262. }
  263. } else {
  264. /* We have to be more efficient in voice mode */
  265. b = (short *)(p->obuf + p->obuflen);
  266. while (p->obuflen/2 < 240) {
  267. /* Read ahead the full amount */
  268. res = fread(result, 1, 240 - p->obuflen/2, p->f);
  269. if (res < 1) {
  270. /* If there's nothing there, just continue on */
  271. if (errno == EAGAIN)
  272. return aopen_handle_escape(p, 0);
  273. ast_log(LOG_WARNING, "Read failed: %s\n", strerror(errno));
  274. }
  275. for (x=0;x<res;x++) {
  276. /* Process all the bytes that we've read */
  277. if (result[x] == CHAR_DLE) {
  278. /* We assume there is no more than one signal frame among our
  279. data. */
  280. if (f)
  281. ast_log(LOG_WARNING, "Warning: Dropped a signal frame\n");
  282. f = aopen_handle_escape(p, result[x+1]);
  283. /* If aopen_handle_escape says NULL, say it now, doesn't matter
  284. what else is there, the connection is dead. */
  285. if (!f)
  286. return NULL;
  287. } else {
  288. /* Generate a 16-bit signed linear value from our
  289. unsigned 8-bit value */
  290. *(b++) = (((short)result[x]) - 127) * 0xff;
  291. p->obuflen += 2;
  292. }
  293. }
  294. if (f)
  295. break;
  296. }
  297. /* If we have a control frame, return it now */
  298. if (f)
  299. return f;
  300. /* If we get here, we have a complete voice frame */
  301. p->fr.frametype = AST_FRAME_VOICE;
  302. p->fr.subclass = AST_FORMAT_SLINEAR;
  303. p->fr.samples = 240;
  304. p->fr.data = p->obuf;
  305. p->fr.datalen = p->obuflen;
  306. p->fr.mallocd = 0;
  307. p->fr.delivery.tv_sec = 0;
  308. p->fr.delivery.tv_usec = 0;
  309. p->fr.offset = AST_FRIENDLY_OFFSET;
  310. p->fr.src = __FUNCTION__;
  311. if (option_debug)
  312. ast_log(LOG_DEBUG, "aopen_read(voice frame)\n");
  313. p->obuflen = 0;
  314. return &p->fr;
  315. }
  316. return NULL;
  317. }
  318. static int aopen_write(struct ast_modem_pvt *p, struct ast_frame *f)
  319. {
  320. if (option_debug)
  321. ast_log(LOG_DEBUG, "aopen_write()\n");
  322. return 0;
  323. }
  324. static char *aopen_identify(struct ast_modem_pvt *p)
  325. {
  326. char identity[256];
  327. char mfr[80];
  328. char mdl[80];
  329. char rev[80];
  330. ast_modem_send(p, "AT#MDL?", 0);
  331. ast_modem_read_response(p, 5);
  332. strncpy(mdl, p->response, sizeof(mdl)-1);
  333. ast_modem_trim(mdl);
  334. ast_modem_expect(p, "OK", 5);
  335. ast_modem_send(p, "AT#MFR?", 0);
  336. ast_modem_read_response(p, 5);
  337. strncpy(mfr, p->response, sizeof(mfr)-1);
  338. ast_modem_trim(mfr);
  339. ast_modem_expect(p, "OK", 5);
  340. ast_modem_send(p, "AT#REV?", 0);
  341. ast_modem_read_response(p, 5);
  342. strncpy(rev, p->response, sizeof(rev)-1);
  343. ast_modem_trim(rev);
  344. ast_modem_expect(p, "OK", 5);
  345. snprintf(identity, sizeof(identity), "%s Model %s Revision %s", mfr, mdl, rev);
  346. return strdup(identity);
  347. }
  348. static void aopen_incusecnt(void)
  349. {
  350. ast_mutex_lock(&usecnt_lock);
  351. usecnt++;
  352. ast_mutex_unlock(&usecnt_lock);
  353. ast_update_use_count();
  354. }
  355. static void aopen_decusecnt(void)
  356. {
  357. ast_mutex_lock(&usecnt_lock);
  358. usecnt++;
  359. ast_mutex_unlock(&usecnt_lock);
  360. ast_update_use_count();
  361. }
  362. static int aopen_answer(struct ast_modem_pvt *p)
  363. {
  364. if (ast_modem_send(p, "ATA", 0) ||
  365. ast_modem_expect(p, "VCON", 10)) {
  366. ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
  367. return -1;
  368. }
  369. return 0;
  370. }
  371. static int aopen_dialdigit(struct ast_modem_pvt *p, char digit)
  372. {
  373. char cmd[80];
  374. snprintf(cmd, sizeof(cmd), "AT#VTS=%c", digit);
  375. if (ast_modem_send(p, cmd, 0) ||
  376. ast_modem_expect(p, "VCON", 10)) {
  377. ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
  378. return -1;
  379. }
  380. return 0;
  381. }
  382. static int aopen_dial(struct ast_modem_pvt *p, char *stuff)
  383. {
  384. char cmd[80];
  385. snprintf(cmd, sizeof(cmd), "ATD%c %s", p->dialtype,stuff);
  386. if (ast_modem_send(p, cmd, 0)) {
  387. ast_log(LOG_WARNING, "Unable to dial\n");
  388. return -1;
  389. }
  390. return 0;
  391. }
  392. static int aopen_hangup(struct ast_modem_pvt *p)
  393. {
  394. if (aopen_break(p))
  395. return -1;
  396. /* Hangup by switching to data, then back to voice */
  397. if (ast_modem_send(p, "ATH", 0) ||
  398. ast_modem_expect(p, "OK", 8)) {
  399. ast_log(LOG_WARNING, "Unable to set to data mode\n");
  400. return -1;
  401. }
  402. if (ast_modem_send(p, "AT#CLS=8", 0) ||
  403. ast_modem_expect(p, "OK", 5)) {
  404. ast_log(LOG_WARNING, "Unable to set to voice mode\n");
  405. return -1;
  406. }
  407. return 0;
  408. }
  409. static struct ast_modem_driver aopen_driver =
  410. {
  411. "AOpen",
  412. aopen_idents,
  413. AST_FORMAT_SLINEAR,
  414. 0, /* Not full duplex */
  415. aopen_incusecnt, /* incusecnt */
  416. aopen_decusecnt, /* decusecnt */
  417. aopen_identify, /* identify */
  418. aopen_init, /* init */
  419. aopen_setdev, /* setdev */
  420. aopen_read,
  421. aopen_write,
  422. aopen_dial, /* dial */
  423. aopen_answer, /* answer */
  424. aopen_hangup, /* hangup */
  425. aopen_startrec, /* start record */
  426. NULL, /* stop record */
  427. NULL, /* start playback */
  428. NULL, /* stop playback */
  429. NULL, /* set silence supression */
  430. aopen_dialdigit, /* dialdigit */
  431. };
  432. int usecount(void)
  433. {
  434. int res;
  435. ast_mutex_lock(&usecnt_lock);
  436. res = usecnt;
  437. ast_mutex_unlock(&usecnt_lock);
  438. return res;
  439. }
  440. int load_module(void)
  441. {
  442. return ast_register_modem_driver(&aopen_driver);
  443. }
  444. int unload_module(void)
  445. {
  446. return ast_unregister_modem_driver(&aopen_driver);
  447. }
  448. char *description()
  449. {
  450. return desc;
  451. }
  452. char *key()
  453. {
  454. return ASTERISK_GPL_KEY;
  455. }