Jelajahi Sumber

Trying raw mode tcgetattr. Not working.

Steve Thielemann 3 tahun lalu
induk
melakukan
6fb445fcac
5 mengubah file dengan 81 tambahan dan 3 penghapusan
  1. 1 1
      Makefile
  2. 61 1
      door/door.go
  3. 2 0
      door/go.mod
  4. 15 1
      door32.c
  5. 2 0
      testdoor/go.mod

+ 1 - 1
Makefile

@@ -5,6 +5,6 @@ all: door32 testdoor/testdoor
 door32: door32.c
 	gcc -o door32 door32.c
 
-testdoor/testdoor: testdoor/testdoor.go
+testdoor/testdoor: testdoor/testdoor.go door/door.go
 	cd testdoor; go build testdoor.go
 

+ 61 - 1
door/door.go

@@ -9,6 +9,8 @@ import (
 	"strings"
 	"syscall"
 	"time"
+
+	"golang.org/x/sys/unix"
 )
 
 /*
@@ -185,6 +187,7 @@ func (d *Door) ReadDropfile(filename string) {
 	d.config.time_left, err = strconv.Atoi(lines[8])
 	d.config.emulation, err = strconv.Atoi(lines[9])
 	d.config.node_number, err = strconv.Atoi(lines[10])
+
 	d.READFD = d.config.comm_handle
 	//if d.READFD == 0 {
 	//	d.WRITEFD = 1
@@ -235,6 +238,57 @@ func (d *Door) detect() {
 
 }
 
+func tcgetattr(fd uintptr) (*unix.Termios, error) {
+	return unix.IoctlGetTermios(int(fd), unix.TCGETS)
+}
+
+func cfmakeraw(attr *unix.Termios) {
+	attr.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.ISTRIP | unix.PARMRK |
+		unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON
+	attr.Oflag &^= unix.OPOST
+	attr.Cflag &^= unix.CSIZE | unix.PARENB
+	attr.Cflag |= unix.CS8
+	attr.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.IEXTEN | unix.ISIG
+	attr.Cc[unix.VMIN] = 0
+	attr.Cc[unix.VTIME] = 1
+
+	/*
+		attr.Iflag &^= unix.BRKINT | unix.INLCR | unix.ICRNL | unix.INPCK | unix.ISTRIP | unix.IXON
+		attr.Oflag &^= unix.OPOST
+		attr.Cflag &^= unix.CSIZE | unix.PARENB
+		attr.Cflag |= unix.CS8
+		attr.Lflag &^= unix.ECHO | unix.ICANON | unix.IEXTEN | unix.ISIG
+		attr.Cc[unix.VMIN] = 1
+		attr.Cc[unix.VTIME] = 0
+	*/
+}
+
+const (
+	TCIFLUSH  = 0
+	TCOFLUSH  = 1
+	TCIOFLUSH = 2
+
+	TCSANOW   = 0
+	TCSADRAIN = 1
+	TCSAFLUSH = 2
+)
+
+// Tcsetattr sets the current serial port settings.
+func tcsetattr(fd, action uintptr, argp *unix.Termios) error {
+	var request uintptr
+	switch action {
+	case TCSANOW:
+		request = unix.TCSETS
+	case TCSADRAIN:
+		request = unix.TCSETSW
+	case TCSAFLUSH:
+		request = unix.TCSETSF
+	default:
+		return unix.EINVAL
+	}
+	return unix.IoctlSetTermios(int(fd), uint(request), argp)
+}
+
 func (d *Door) Init() {
 	var dropfile string
 
@@ -248,9 +302,15 @@ func (d *Door) Init() {
 
 	d.ReadDropfile(dropfile)
 
-	fmt.Printf("BBS %s, User %s / Handle %s / File %d\n", d.config.BBSID, d.config.real_name, d.config.handle, d.config.comm_handle)
+	fmt.Printf("BBS %s, User %s / Handle %s / File %d\n", d.config.BBSID, d.config.real_name, d.config.handle, d.READFD)
 	// putting the linux terminal into raw mode ...
 	// requires golang.org/x/sys/unix  unix.Ioctlgetermios, etc.
+
+	// I'm still getting terminal echo, terminal still not in raw mode (needs CR to submit)
+	a, _ := tcgetattr(uintptr(d.READFD))
+	cfmakeraw(a)
+	_ = tcsetattr(uintptr(d.READFD), TCSANOW, a)
+	// _ = tcsetattr(uintptr(d.READFD), TCSAFLUSH, a)
 	d.detect()
 }
 

+ 2 - 0
door/go.mod

@@ -1,3 +1,5 @@
 module red-green/door
 
 go 1.17
+
+require golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e

+ 15 - 1
door32.c

@@ -11,6 +11,13 @@
 void doprocessing (int sock);
 char * cmds[10];
 
+void set_raw(int fd) {
+   struct termios tio_raw;
+   tcgetattr(fd, &tio_raw);
+   cfmakeraw(&tio_raw);
+   tcsetattr(fd, TCSANOW, &tio_raw);
+}
+
 int main( int argc, char *argv[] ) {
    int sockfd, newsockfd, portno, clilen;
    char buffer[256];
@@ -77,7 +84,8 @@ int main( int argc, char *argv[] ) {
       * process will go in sleep mode and will wait
       * for the incoming connection
    */
-   
+   printf("Listening for connections on %d\n", listen_port);
+
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    
@@ -89,6 +97,8 @@ int main( int argc, char *argv[] ) {
          exit(1);
       }
       
+      set_raw(newsockfd);
+
       /* Create child process */
       pid = fork();
 		
@@ -115,10 +125,14 @@ void doprocessing (int sock) {
    int n;
    char buffer[256];
    // raw mode
+   set_raw(sock);
+   /*
    struct termios tio_raw;
    tcgetattr(sock, &tio_raw);
    cfmakeraw(&tio_raw);
    tcsetattr(sock, TCSANOW, &tio_raw);
+   */
+
    // telnet client into character mode
    // sprintf(buffer, "\377\375\042\377\373\001Welcome socket %d\n\r", sock);
    sprintf(buffer, "Welcome socket %d\n\r", sock);

+ 2 - 0
testdoor/go.mod

@@ -5,3 +5,5 @@ go 1.17
 replace red-green/door => ../door
 
 require red-green/door v0.0.0-00010101000000-000000000000
+
+require golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect