Ejemplo n.º 1
0
A2(PUBLIC, OSErr, ROMlib_serialopen, ParmBlkPtr, pbp,		/* INTERNAL */
								 DCtlPtr, dcp)
{
    OSErr err;
    auto DCtlPtr otherp;	/* auto due to old compiler bug */
    hiddenh h;
#if defined (LINUX) || defined (NEXTSTEP)
    const char *devname, *tempname;
    LONGINT fd, ourpid, theirpid, newfd, oumask;
#endif

    err = noErr;
    if (!(dcp->dCtlFlags & CWC(OPENBIT))) {
	h = (hiddenh) NewHandle(sizeof(hidden));
	dcp->dCtlStorage = (Handle) RM(h);
	otherp = otherdctl(pbp);
	if (otherp && (otherp->dCtlFlags & CWC(OPENBIT))) {
	    *STARH(h) = *STARH((hiddenh) (long) MR(otherp->dCtlStorage));
	    dcp->dCtlFlags |= CWC(OPENBIT);
	} else {
#if defined (LINUX) || defined (NEXTSTEP)
	    err = permErr;
	    if ((devname = specialname(pbp, &lockname, &tempname))) {
		oumask = umask(0);
		if (!tempname)
		  err = noErr;
		else if ((fd = Uopen(tempname, O_BINARY|O_CREAT|O_WRONLY, 0666L))
									 >= 0) {
		    ourpid = getpid();
		    if (write(fd, &ourpid, sizeof(ourpid)) == sizeof(ourpid)) {
			if (Ulink(tempname, lockname) == 0)
			    err = noErr;
			else {
			    if ((newfd = Uopen(lockname, O_BINARY|O_RDWR, 0))
									>= 0) {
				if (read(newfd, &theirpid, sizeof(theirpid))
							   == sizeof(theirpid))
				    if ((kill(theirpid, 0) != 0) &&
							      errno == ESRCH) {
					err = noErr;
					Uunlink(lockname);
					Ulink(tempname, lockname);
				    }
				close(newfd);
			    }
			}
			Uunlink(tempname);
		    }
		    close(fd);
		}
		umask(oumask);
	    }
#endif
	    if (err == noErr) {
#if defined (LINUX) || defined (NEXTSTEP)
		HxX(h, fd) = ROMlib_priv_open(devname, O_BINARY|O_RDWR);
		if (HxX(h, fd) < 0) 
		    err = HxX(h, fd);	/* error return piggybacked */
		else {
#if defined(TERMIO)
		    err = ioctl(HxX(h, fd), TCGETA, &HxX(h, state)) < 0 ?
						     ROMlib_maperrno() : noErr;
#else
		    if (ioctl(HxX(h, fd), TIOCGETP, &HxX(h, sgttyb)) < 0 ||
			    ioctl(HxX(h, fd), TIOCGETC, &HxX(h, tchars)) < 0 ||
			     ioctl(HxX(h, fd), TIOCLGET, &HxX(h, lclmode)) < 0)
			err = ROMlib_maperrno();
#endif
#else
		    HxX(h, fd) = (CW(pbp->cntrlParam.ioCRefNum) == AINREFNUM ||
		      CW(pbp->cntrlParam.ioCRefNum) == AOUTREFNUM) ? 0 : 1;
#endif
		    dcp->dCtlFlags |= CWC(OPENBIT);
		    SerReset(CW(pbp->cntrlParam.ioCRefNum),
			    (CW(pbp->cntrlParam.ioCRefNum) == AINREFNUM ||
			     CW(pbp->cntrlParam.ioCRefNum) == AOUTREFNUM) ?
						    CW(SPPortA) : CW(SPPortB));
#if defined (LINUX) || defined (NEXTSTEP)
		}
#endif
	    }
	}
    }
#if defined(SERIALDEBUG)
    warning_trace_info("serial open returning %d", (LONGINT) err);
#endif
    DOCOMPLETION(pbp, err);
}
Ejemplo n.º 2
0
int ndiSerialComm(long serial_port, int baud, const char mode[4], int handshake)
{
  SerShk sconf;
  short config = 0;

  int newbaud;

  switch (baud)
    {
    case 9600:   newbaud = baud9600;   break;
    case 14400:  newbaud = baud14400;  break;
    case 19200:  newbaud = baud19200;  break;
    case 38400:  newbaud = baud38400;  break;
    case 57600:  newbaud = baud57600;  break;
    case 115200: return -1;
    default:     return -1;
    }

  sconf.fXOn = 0;  /* disable software handshaking */
  sconf.fInX = 0;
  sconf.errs = 0;
  sconf.evts = 0;
  sconf.fCTS = (handshake != 0);  /* set hardware handshaking */
  sconf.fDTR = (handshake != 0);

  config = newbaud;             /* set speed */

  if (mode[0] == '8') {                 /* set data bits */
    config |= data8
  }
  else if (mode[0] == '7') {
    config |= data7
  }
  else {
    return -1;
  }

  if (mode[1] == 'N') {                 /* set parity */
    config |= noParity;
  }
  else if (mode[1] == 'O') {
    config |= oddParity;
  }
  else if (mode[1] == 'E') {
    config |= evenParity;
  }
  else {
    return -1;
  }

  if (mode[2] == '1') {                  /* set stop bits */
    config |= stop10;
  }
  else if (mode[2] == '2') {
    config |= stop20;
  }
  else {
    return -1;
  }

  SerHShake(input_file(serial_port), &sconf);
  SerHShake(output_file(serial_port), &sconf);
  SerReset(input_file(serial_port), newbaud);
  SerReset(output_file(serial_port), newbaud);

  return 0;
}