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 / recv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-31  |  5.5 KB  |  231 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. #include <fcntl.h>
  25.  
  26. #include "log.h"
  27. #include "c2proto.h"
  28. #include "response.h"
  29. #include "swap.h"
  30. #include "read.h"
  31. #include "write.h"
  32. #include "tty.h"
  33. #include "recv.h"
  34.  
  35. /*
  36.  * Answer a call: ATA
  37.  *
  38.  * Return codes:
  39.  *     0    ok, call has been answered.
  40.  *    -1    answer failed.
  41.  */
  42. int faxmodem_answer(f)
  43.      FaxModem *f;
  44. {  
  45.     log(L_NOTICE, "answering phone");
  46.  
  47.     init_modem_response(f);
  48.  
  49.     /* flush any echoes or return codes (from the RING) */
  50.     tcflush(f->fd, TCIFLUSH);
  51.  
  52.     if (fdprintf(f->fd, "ATA\r") < 0)
  53.       return (-1);
  54.  
  55.     if (get_modem_response(f, TIMEOUT_ANSWER) < 0)
  56.       return (-1);
  57.  
  58.     if (faxmodem_bit_reverse(f, 0) < 0)
  59.       return (-1);
  60.  
  61.     return (0);
  62. }
  63.  
  64. /* 
  65.  * Receive a page: +FDR
  66.  *
  67.  * The FDR command can return a couple of possible results.  If the
  68.  * connection is valid, it returns at least the messages listed here:
  69.  *   +FCFR
  70.  *   +FDCS: <params>
  71.  *   CONNECT
  72.  * If the other machine has hung up, it returns:
  73.  *   +FHNG: <code>
  74.  * If the connection was not made at all, it returns:
  75.  *    ERROR (numeric code 4)
  76.  *
  77.  * Return codes:
  78.  *     RECV_OK        page data is ready to come
  79.  *     RECV_FAILED    can't receive the page
  80.  *     RECV_DONE    no more pages
  81.  */
  82. recv_code faxmodem_start_recv(f)
  83.      FaxModem *f;
  84. {
  85.     log(L_NOTICE, "asking for remote to send data");
  86.  
  87.     if (fdprintf(f->fd, "AT+FDR\r") < 0)
  88.       return (RECV_FAILED);
  89.  
  90.     /*
  91.      * This flags get reset for every connection.
  92.      */
  93.     f->flags &= ~(FAX_F_CONNECT|FAX_F_FDCS);
  94.  
  95.     /*
  96.      * Make sure that we get the connect message from the modem.
  97.      */
  98.     if (get_modem_response(f, 60) < 0) {
  99.     log(L_NOTICE, "faxmodem_start_recv: didn't receive connect");
  100.     return (RECV_FAILED);
  101.     }
  102.  
  103.     /*
  104.      * Did we connect, hangup, or fail?
  105.      */
  106.     if (FAX_ISSET(f, FAX_F_CONNECT))
  107.       return (RECV_OK);
  108.     else if (FAX_ISSET(f, FAX_F_FHNG))
  109.       return (RECV_DONE);
  110.     else
  111.       return (RECV_FAILED);
  112. }
  113.  
  114. static void add_padding(fd)
  115.      int fd;
  116. {
  117.     /*SUPPRESS 442*/
  118.     char *pad = "\000\020\001\000\020\001\000\020\001";
  119.  
  120.     write(fd, pad, 9);
  121. }
  122.  
  123. /* 
  124.  * Reads a data stream from the faxmodem, unstuffing DLE characters.
  125.  * Returns the +FET value (2 if no more pages).  Write the stream 
  126.  * out to the given fp.  Returns at the end of a page.
  127.  *
  128.  * Return codes:
  129.  *    0    check modem result code for results
  130.  *     -1    failure, check modem status
  131.  */
  132. int faxmodem_recv_page(f, fd)
  133.      FaxModem *f;
  134.      int fd;
  135.     int escaped = FALSE;
  136.  
  137.     log(L_INFO, "receiving a page");
  138.  
  139.     /*
  140.      * Add a delay, for good measures.  May not be needed.
  141.      */
  142.     sleep(1);
  143.  
  144.     /* 
  145.      * Send XON to restart, and disable flow control from modem
  146.      * to the Sun so we can receive all 8 bits.  We can still
  147.      * stop the modem if it is sending us data too fast, though.
  148.      */
  149.     tty_fc(f->fd, FC_INPUT_ON);
  150.     tcflow(f->fd, TCION);
  151.     
  152.     /*
  153.      * Reset the post page response flag, since we are now
  154.      * moving to a new page.
  155.      */
  156.     f->flags &= ~FAX_F_FET;
  157.  
  158.     /*
  159.      * Process the stream, until we get an end of page escape.
  160.      */
  161.     for (;;) {
  162.     unsigned char buf[BUFSIZ];
  163.     unsigned char buf_copy[BUFSIZ];
  164.     int nchars;
  165.     int ochars;
  166.     int i;
  167.  
  168.     /*
  169.      * Get a chunk of data.  Note that it is possible for us
  170.      * to read past the end of the fax data steam.  That is,
  171.      * we might read some of the modem response.  Thus, we must
  172.      * pass the remaining stuff to get_modem_response.
  173.      */
  174.     switch(nchars = tread(f->fd, (char *)buf, sizeof(buf), 120)) {
  175.       case -1:
  176.         log(L_ALERT, "read failed in page recv: %m");
  177.         f->status = MODEM_STATUS_FAILED;
  178.         tty_fc(f->fd, FC_BOTH_ON);
  179.         return (-1);
  180.       case 0:
  181.         log(L_NOTICE, "read timed out in page recv: %m");
  182.         f->status = MODEM_STATUS_TIMEOUT;
  183.         tty_fc(f->fd, FC_BOTH_ON);
  184.         return (-1);
  185.       default:
  186.         break;
  187.     }
  188.  
  189.     /*
  190.      * Reverse and unstuff buffer characters.  Check for the DLE
  191.      * escape.  If a DLE ETX is received, we've got the end  of
  192.      * the page.  A DLE DLE means to stuff a DLE.
  193.      */
  194.     ochars = 0;
  195.     for (i = 0; i < nchars; i++) {
  196.         if (escaped) {
  197.         escaped = FALSE;
  198.         switch (buf[i]) {
  199.           case DLE:
  200.             buf_copy[ochars++] = swap_bits(DLE);
  201.             break;
  202.           case ETX:
  203.             log(L_INFO, "received end of page marker");
  204.             if (ochars > 0)
  205.               write(fd, (char *)buf_copy, ochars);
  206.             add_padding(fd);
  207.             tty_fc(f->fd, FC_BOTH_ON);
  208.             return (get_modem_response_from_buf(f, TIMEOUT_RECV_PAGE, 
  209.                             (char *)&buf[i+1], 
  210.                             nchars-i-1));
  211.           default:
  212.             log(L_INFO, "dropping: %d", buf[i]);
  213.             break;
  214.         }
  215.         } else {
  216.         switch (buf[i]) {
  217.           case DLE:
  218.             escaped = TRUE;
  219.             break;
  220.           default:
  221.             buf_copy[ochars++] = swap_bits(buf[i]);
  222.             break;
  223.         }
  224.         }
  225.     }
  226.     if (ochars > 0)
  227.       write(fd, (char *)buf_copy, ochars);
  228.     }
  229. }
  230.