home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fax-3.2.1 / lib / libfax / gen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-31  |  4.4 KB  |  190 lines

  1. /*
  2.   This file is part of the NetFax system.
  3.  
  4.   (c) Copyright 1989 by David M. Siegel and Sundar Narasimhan.
  5.       All rights reserved.
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation.
  10.  
  11.     This program is distributed in the hope that it will be useful, 
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <termios.h>
  24.  
  25. #include "log.h"
  26. #include "c2proto.h"
  27. #include "response.h"
  28. #include "read.h"
  29. #include "write.h"
  30. #include "gen.h"
  31.  
  32. int faxmodem_open(f, filename)
  33.      FaxModem *f;
  34.      char *filename;
  35. {
  36.     return (f->fd = tty_open(filename));
  37. }
  38.  
  39. int faxmodem_close(f)
  40.      FaxModem *f;
  41. {
  42.     return (tty_close(f->fd));
  43. }
  44.  
  45. int faxmodem_sync(f, total_tries)
  46.      FaxModem *f;
  47.      int total_tries;
  48. {
  49.     int tries;
  50.  
  51.     log(L_INFO, "syncing with %d tries", total_tries);
  52.  
  53.     for (tries = 0; tries < total_tries; tries++) {
  54.     char buf[128];
  55.     int count;
  56.  
  57.     /* Send XON just in case data stream was left off from last page */
  58.     tcflow(f->fd, TCOON);
  59.     tcflow(f->fd, TCION);
  60.  
  61.     /*
  62.      * Clean out the fd.
  63.      */
  64.     tcflush(f->fd, TCIOFLUSH);
  65.  
  66.     /*
  67.      * Command +FCLASS=2 puts us in class 2 faxmodem mode
  68.      * and supposedly resets other parameters
  69.      *
  70.      *    V0      - result codes are digits
  71.      *    Q0      - result codes are sent
  72.      *    E0      - do not echo
  73.      *      M0        - speaker off
  74.      *    S0=0      - dont automatically answer phone
  75.      *    S2=255      - disable escape character
  76.      *    S12=255      - longest possible escape guard time
  77.      *      +FCLASS=2 - enable faxmodem commands
  78.      *      S7=120    - wait 120 seconds for carrier event
  79.      *    +FCR=1    - enable fax reception
  80.      */
  81.     fdprintf(f->fd, "ATZ\r");
  82.     tcdrain(f->fd); sleep(1);
  83.     fdprintf(f->fd, "ATV0Q0E0M0S0=0S2=255S12=255 +FCLASS=2\r");
  84.     tcdrain(f->fd); sleep(1);
  85.     fdprintf(f->fd, "ATS7=120 +FCR=1\r");
  86.     tcdrain(f->fd); sleep(1);
  87.  
  88.     /* flush any echoes or return codes */
  89.     tcflush(f->fd, TCIFLUSH);
  90.  
  91.     /* now see if the modem is talking to us properly */
  92.     fdprintf(f->fd, "AT\r");
  93.     count = tfdgets(f->fd, buf, sizeof(buf), 2);
  94.     if (count < 0) {
  95.         log(L_ERR, "read failed: %m");
  96.         return (-1);
  97.     } else if (count == 0) {
  98.         log(L_NOTICE, "read timeout, tries=%d", tries);
  99.         continue;
  100.     } else {
  101.         if (strncmp(buf, "0\r", 2) != 0) {
  102.         log(L_NOTICE, "bad modem response: \"%.*s\", tries=%d", 
  103.             count, buf, tries);
  104.         } else {
  105.         log(L_INFO, "modem is now in sync");
  106.         return (0);
  107.         }
  108.     }
  109.  
  110.     /* wait a little while between tries */
  111.     sleep(5);
  112.     }
  113.  
  114.     log(L_NOTICE, "cannot sync with fax modem");
  115.  
  116.     return (-1);
  117. }
  118.  
  119. /*
  120.  * Force the modem to hangup: ATH
  121.  *
  122.  * Usually, this would be called at the very end of a session,
  123.  * to insure that the modem goes back on hook.
  124.  *
  125.  * Return codes:
  126.  *     0    ok, hangup has been issued.
  127.  *    -1    hangup failed.
  128.  */
  129. int faxmodem_hangup(f)
  130.      FaxModem *f;
  131. {
  132.     log(L_NOTICE, "hanging up the modem");
  133.     
  134.     if (fdprintf(f->fd, "ATH\r") < 0)
  135.       return (-1);
  136.  
  137.     return (get_modem_response(f, TIMEOUT_HANGUP));
  138. }
  139.  
  140. /*
  141.  * Bit reversal options: +FBOR
  142.  */
  143. int faxmodem_bit_reverse(f, code)
  144.      FaxModem *f;
  145.      int code;
  146. {
  147.     log(L_NOTICE, "enabling bit reversal");
  148.  
  149.     if (fdprintf(f->fd, "AT+FBOR=%d\r", code) < 0)
  150.       return (-1);
  151.   
  152.     return (get_modem_response(f, TIMEOUT_BIT_REVERSE));
  153. }
  154.  
  155. /* 
  156.  * Bit reversal options: +FREL
  157.  *   0 = data is bit aligned as received
  158.  *   1 = dat is byte aligned at EOLS
  159.  */
  160. int faxmodem_byte_align(f, code)
  161.      FaxModem *f;
  162.      int code;
  163. {
  164.     log(L_NOTICE, "setting byte alignment to mode %d", code);
  165.  
  166.     if (fdprintf(f->fd, "AT+FREL=%d\r", code) < 0)
  167.       return (-1);
  168.   
  169.     return (get_modem_response(f, TIMEOUT_BYTE_ALIGN));
  170. }
  171.  
  172. /* 
  173.  * 8.5.1.1 DCE capabilities parameters, +FDCC
  174.  *   Write Syntax: +FDCC=VR,BR,LN,DF,EC,BF,ST
  175.  *   Default Values: 1,3,2,2,0,0,0,0
  176.  *
  177.  * This just sets the VR and BR, currently.
  178.  */
  179. int faxmodem_set_capabilities(f, vr, br)
  180.      FaxModem *f;
  181.      int vr, br;
  182. {
  183.     log(L_NOTICE, "setting DCE capabilities: %d %d", vr, br);
  184.  
  185.     if (fdprintf(f->fd, "AT+FDCC=%d,%d\r", vr, br) < 0)
  186.       return (-1);
  187.   
  188.     return (get_modem_response(f, TIMEOUT_SET_CAPABILITIES));
  189. }  
  190.