home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / UUCPbb_2_1_src.lzh / UUCPBB21 / openport.c < prev    next >
Text File  |  1994-09-25  |  7KB  |  238 lines

  1. /*  openport.c   Open and close the port when making/receiving calls.
  2.     Copyright (C) 1990, 1993  Rick Adams and Bob Billson
  3.  
  4.     This file is part of the OS-9 UUCP package, UUCPbb.
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  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.     The author of UUCPbb, Bob Billson, can be contacted at:
  21.     bob@kc2wz.bubble.org  or  uunet!kc2wz!bob  or  by snail mail:
  22.     21 Bates Way, Westfield, NJ 07090
  23. */
  24.  
  25. #include "uucp.h"
  26. #include "uucico.h"
  27. #include <modes.h>
  28. #include <sgstat.h>
  29. #ifndef _OSK
  30. #include <os9.h>
  31. #else
  32. #include <module.h>
  33. #endif
  34.  
  35. static struct sgbuf orig_opts;                     /* original port options */
  36. static flag portopen = FALSE;
  37.  
  38. void flushport();
  39.  
  40.  
  41. /* Open serial port.  If we are the master, use the port specified in the
  42.    global variable 'device'.  If we are running as slave, we use the standard
  43.    input.  In the latter case, we assume the standard input is the port
  44.    a remote is logged in on. */
  45.  
  46. int openport()
  47. {
  48.      char buf[34];
  49. #ifndef _OSK
  50.      static short baudlist[] =
  51.                 {110,300,600,1200,2400,4800,9600,19200,0};
  52. #else
  53.      /* OSK baud rates */
  54.      static short baudlist[] =
  55.               {50,75,110,134,150,300,600,1200,1800,2000,2400,3600,4800,
  56.                7200,9600,19200,38400};
  57. #endif
  58.      struct sgbuf newopts;
  59.      register int i;
  60.  
  61.      /* open device */
  62.      if (role == MASTER)
  63.        {
  64.           if ((port = open (device, PORTMODE)) <= 0)
  65.                if (errno == NONSHARE)
  66.                     return (PORTBUSY);
  67.                else
  68.                  {
  69.                     portfatal (device);
  70.                     return (ABORT);
  71.                  }
  72.        }
  73.      else
  74.        {
  75.           _gs_devn (0, temp);
  76.           buf[0] = '/';
  77.           strhcpy (&buf[1], temp);
  78.  
  79.           if ((port = open (buf, 3)) <= 0)
  80.                if (errno == NONSHARE)
  81.                     return (PORTBUSY);
  82.                else
  83.                  {
  84.                     portfatal (buf);
  85.                     return (ABORT);
  86.                  }
  87.           strncpy (device, buf, sizeof (device) - 1);
  88.        }
  89.      portopen = offhook = TRUE;
  90.      openlog();
  91.  
  92.      /* set device options.  Changed -- REB */
  93.      _gs_opt (port, &orig_opts);
  94.  
  95.      newopts.sg_class  = orig_opts.sg_class;       /* device class      */
  96.      newopts.sg_d2p    = orig_opts.sg_d2p;
  97.      newopts.sg_err    = orig_opts.sg_err;
  98.      newopts.sg_backsp =                           /* disable backspace */
  99.      newopts.sg_delete =                           /* no delete seq     */
  100.      newopts.sg_echo   =                           /* echo off          */
  101.      newopts.sg_alf    =                           /* no auto LF        */
  102.      newopts.sg_nulls  =                           /* no EOL null count */
  103.      newopts.sg_pause  =                           /* no pause          */
  104.      newopts.sg_page   =                           /* no lines/page     */
  105.      newopts.sg_bspch  =                           /* no backspace      */
  106.      newopts.sg_dlnch  =                           /* no del line       */
  107.      newopts.sg_eorch  =                           /* no eor            */
  108.      newopts.sg_eofch  =                           /* no eof            */
  109.      newopts.sg_rlnch  =                           /* no reprint line   */
  110.      newopts.sg_dulnch =                           /* no dup line       */
  111.      newopts.sg_psch   =                           /* no pause          */
  112.      newopts.sg_kbich  =                           /* no interrupt      */
  113.      newopts.sg_kbach  =                           /* no abort          */
  114.      newopts.sg_bsech  =                           /* no backspace echo */
  115.      newopts.sg_bellch =                           /* no overflow char  */
  116.      newopts.sg_xon    =                           /* no XON            */
  117.      newopts.sg_xoff   = 0;                        /* no XOFF           */
  118.      newopts.sg_baud   = orig_opts.sg_baud;        /* may be reset below */
  119.      
  120.      /* set baudrate */
  121.      if (role == MASTER)
  122.           for (i = 0; baudlist[i] != 0; i++)
  123.                if (baudlist[i] == atoi (baud))
  124.                  {
  125.                     newopts.sg_baud = i;
  126.  
  127.                     if (debuglvl > 1)
  128.                          fprintf (log, "opening port %s at %d bps\n",
  129.                                        device, baudlist[i]);
  130.                     break;
  131.                  }
  132.  
  133. #ifndef RTSFLOW
  134.      /* don't use hardware flow control (default) --REB */
  135.      newsopts.sg_parity = orig_opts.parity;        /* use original setting */
  136. #else
  137.      /* if we are using Bruce Isted's SACIA and a modem using hardware 
  138.         (RTS/CTS) flow control, set the port up for it --REB */
  139.  
  140.      newopts.sg_parity = 0x02;          /* hardware flow control (RTS/CTS) */
  141. #endif
  142.  
  143.      /* update the port's path descriptor */
  144.      _ss_opt (port, &newopts);
  145.      flushport();
  146.      return (role == MASTER ? MAKECALL : SOPEN);
  147. }
  148.  
  149.  
  150.  
  151. /* Hang up and reset the modem, restore the port's original settings and close
  152.    it.  Combined original hangup.c with this.   Added support to hanging up
  153.    the phone by dropping the DTR line.  SACIA/DACIA will support this. --REB */
  154.  
  155. void closeport()
  156. {
  157. #ifndef _OSK
  158.      struct registers regs;
  159. #endif
  160.  
  161.      /* don't mess with the port if it is not opened --REB */
  162.      if (!portopen)
  163.           return;
  164.  
  165.      /* Hangup, restore the port and close */
  166.      strcpy (sender, "uucp");
  167.  
  168.      if (debuglvl >= 1)
  169.           fprintf (log, "%s %s %s DEBUG--closeport(): Closing port\n",
  170.                              sender, sysname, gtime());
  171.  
  172.      /* Drop DTR to hang up? */
  173.      if (dropDTR)
  174.        {
  175. #ifndef _OSK
  176.           regs.rg_a = port;
  177.           regs.rg_b = SS_HNGUP;
  178.           _os9 (I_SETSTT, ®s);
  179. #endif
  180.        }
  181.      else
  182.        {
  183.           /* Hang up using +++ATH */
  184. #ifndef _OSK
  185.           tsleep (MODEMDELAY);
  186. #else
  187.           sleep (1);
  188. #endif
  189.           write (port, "+++", 3);
  190. #ifndef _OSK
  191.           tsleep (MODEMDELAY);
  192. #else
  193.           sleep (1);
  194. #endif
  195.           write (port, "ATH\n", 4);
  196.        }
  197.  
  198.      /* reset the modem */
  199.      sleep (1);
  200.      write (port, modemreset, strlen (modemreset));
  201.  
  202.      /* put the port back the way we found it */
  203.      flushport();
  204.      _ss_opt (port, &orig_opts);
  205.      close (port);
  206.      portopen = FALSE;
  207.      *device = '\0';
  208. }
  209.  
  210.  
  211.  
  212. /* flush the port to make sure SCF has nothing hanging around */
  213.  
  214. void flushport()
  215. {
  216.     register int rdy;
  217.  
  218.     if ((rdy = _gs_rdy (port)) > 0)
  219.          read (port, temp, rdy);
  220. }
  221.  
  222.  
  223.  
  224. int portfatal (device)
  225. char *device;
  226. {
  227.      char buf[80];
  228.  
  229.      sprintf (buf, "openport: can't open path to: %s ...error %d",
  230.                    device, errno);
  231.      openlog();
  232.      logerror (buf);
  233.      closelog();
  234.  
  235.      if (!quiet)
  236.           puts (buf);
  237. }
  238.