chan_modem_aopen.c 13 KB

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