From fc832be58c6314f83cd67652eba0a97203d18b25 Mon Sep 17 00:00:00 2001 From: Othmar Gsenger Date: Thu, 21 Mar 2013 22:11:49 +0000 Subject: [PATCH] moved go stuff arround --- door_daemon_go/run.sh | 7 - door_daemon_go/src/termios/termios.go | 121 ------------------ door_daemon_go/unix_socket_server.go | 225 --------------------------------- go/door_daemon/run.sh | 4 + go/door_daemon/unix_socket_server.go | 225 +++++++++++++++++++++++++++++++++ go/termios/termios.go | 121 ++++++++++++++++++ 6 files changed, 350 insertions(+), 353 deletions(-) delete mode 100755 door_daemon_go/run.sh delete mode 100644 door_daemon_go/src/termios/termios.go delete mode 100644 door_daemon_go/unix_socket_server.go create mode 100755 go/door_daemon/run.sh create mode 100644 go/door_daemon/unix_socket_server.go create mode 100644 go/termios/termios.go diff --git a/door_daemon_go/run.sh b/door_daemon_go/run.sh deleted file mode 100755 index 049a8ef..0000000 --- a/door_daemon_go/run.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh -clear -rm /tmp/test.sock 2>/dev/null -export GOPATH=`pwd` -#go install termios -#go build unix_socket_server.go && ./unix_socket_server /dev/ttyACM0 || sleep 5 -go build && ./door_daemon_go /dev/ttyACM0 || sleep 5 diff --git a/door_daemon_go/src/termios/termios.go b/door_daemon_go/src/termios/termios.go deleted file mode 100644 index 11325c9..0000000 --- a/door_daemon_go/src/termios/termios.go +++ /dev/null @@ -1,121 +0,0 @@ -package termios - -import ( - "fmt"; - "os"; - "syscall"; - "unsafe" - "errors" -) - -// termios types -type cc_t byte -type speed_t uint -type tcflag_t uint - -// termios constants -const ( - BRKINT = tcflag_t (0000002); - ICRNL = tcflag_t (0000400); - INPCK = tcflag_t (0000020); - ISTRIP = tcflag_t (0000040); - IXON = tcflag_t (0002000); - OPOST = tcflag_t (0000001); - CS8 = tcflag_t (0000060); - ECHO = tcflag_t (0000010); - ICANON = tcflag_t (0000002); - IEXTEN = tcflag_t (0100000); - ISIG = tcflag_t (0000001); - VTIME = tcflag_t (5); - VMIN = tcflag_t (6) -) - -const NCCS = 32 -type termios struct { - c_iflag, c_oflag, c_cflag, c_lflag tcflag_t; - c_line cc_t; - c_cc [NCCS]cc_t; - c_ispeed, c_ospeed speed_t -} - -// ioctl constants -const ( - TCGETS = 0x5401; - TCSETS = 0x5402 -) - -var ( - orig_termios termios; - ttyfd uintptr = 0 // STDIN_FILENO -) - -func Ttyfd(fd uintptr) { - ttyfd=fd -} - -func getTermios (dst *termios) error { - r1, _, errno := syscall.Syscall (syscall.SYS_IOCTL, - uintptr (ttyfd), uintptr (TCGETS), - uintptr (unsafe.Pointer (dst))); - - if err := os.NewSyscallError ("SYS_IOCTL", errno); errno!=0 && err != nil { - return err - } - - if r1 != 0 { - // return errors.New("Error") - } - - return nil -} - -func setTermios (src *termios) error { - r1, _, errno := syscall.Syscall (syscall.SYS_IOCTL, - uintptr (ttyfd), uintptr (TCSETS), - uintptr (unsafe.Pointer (src))); - - if err := os.NewSyscallError ("SYS_IOCTL", errno); errno!=0 &&err != nil { - return err - } - - if r1 != 0 { - return errors.New ("Error") - } - - return nil -} - -func tty_raw () error { - raw := orig_termios; - - raw.c_iflag &= ^(BRKINT | ICRNL | INPCK | ISTRIP | IXON); - raw.c_oflag &= ^(OPOST); - raw.c_cflag |= (CS8); - raw.c_lflag &= ^(ECHO | ICANON | IEXTEN | ISIG); - - raw.c_cc[VMIN] = 1; - raw.c_cc[VTIME] = 0; - - if err := setTermios (&raw); err != nil { return err } - - return nil -} - -func SetRaw () { - var ( - err error - ) - - defer func () { - if err != nil { fmt.Printf ("SetRaw Error: %v\n",err) } - } (); - - if err = getTermios (&orig_termios); err != nil { return } - -// defer func () { -// err = setTermios (&orig_termios) -// } (); - - if err = tty_raw (); err != nil { return } - //if err = screenio (); err != nil { return } -} diff --git a/door_daemon_go/unix_socket_server.go b/door_daemon_go/unix_socket_server.go deleted file mode 100644 index 77a64e6..0000000 --- a/door_daemon_go/unix_socket_server.go +++ /dev/null @@ -1,225 +0,0 @@ -package main -import "fmt" -import "net" -import "bufio" -import "strings" -import "os" -import "io" -import "termios" -import "flag" -import "regexp" -import "sync" -import "time" - -var lock sync.RWMutex - -type DoorConnection struct { - rw * bufio.ReadWriter - c net.Conn - wchan chan string -} - -var DoorConnectionMap = map[uint]DoorConnection { -} - -var cmdHandler = map[string]func([]string,string,*bufio.ReadWriter ) { - "test":handleCmdTest, - "open":handleCmdController, - "close":handleCmdController, - "toggle":handleCmdController, -} - - -func readLineSafe(rw *bufio.Reader) (string, error) { - wasPrefix:=false - var line string - for isPrefix:=true;isPrefix; { - var lineBuf []byte - var err error - lineBuf,isPrefix,err = rw.ReadLine() - if err != nil { - return "",err - } - if isPrefix { - wasPrefix=true - } else { - line=string(lineBuf) - } - } - if wasPrefix { - fmt.Println("line too long") - //fmt.Fprintf(rw,"line too long\n") - //rw.Flush() - return "",nil - } - reg := regexp.MustCompile("\r") - safe := reg.ReplaceAllString(line, "") - return safe,nil -} - -func connToReadWriter(c io.Reader,cw io.Writer) (*bufio.ReadWriter) { - client_r := bufio.NewReaderSize(c,1024) - client_w := bufio.NewWriterSize(cw,1024) - return bufio.NewReadWriter(client_r,client_w) -} - -func handleConnection(c net.Conn, client * bufio.ReadWriter, connID uint) () { - fmt.Println("new connection") - defer func () { - lock.Lock() - delete(DoorConnectionMap,connID) - lock.Unlock() - }() - for { - line,err:=readLineSafe(bufio.NewReader(client)) - if err != nil { - if err.Error() != "EOF" { - fmt.Printf("Error: readLineSafe returned %v\n",err.Error()) - } else { - fmt.Printf("Connection closed by remote host\n"); - } - c.Close() - return - } - if line == "" { - continue - } - fmt.Printf("Received: %v\n", line) - tokens:=strings.Fields(line) - remainStr:=strings.Join(tokens[1:]," ") - handleCmd(tokens,remainStr,client) - } -} - -func handleCmd(tokens []string, remainStr string,client * bufio.ReadWriter) { - cmd:=tokens[0] - func_ptr,present := cmdHandler[cmd] - if present { - func_ptr(tokens, remainStr,client) - } else { - fmt.Printf("Error: unknown Cmd: %v\n", cmd) - } -} - -func handleCmdTest(tokens []string, remainStr string, client * bufio.ReadWriter) { - //cmd:=tokens[0] - fmt.Printf("Test: %v\n", remainStr) -} - -func handleCmdController(tokens []string, remainStr string, client * bufio.ReadWriter) { - cmd:=tokens[0] - s_r:=strings.NewReader(cmd) - char := make([]byte,1) - s_r.Read(char) - fmt.Println(string(char)) -} - - -func openTTY(name string) *os.File { - file, err := os.OpenFile(name,os.O_RDWR ,0600) // For read access. - if err != nil { - fmt.Println(err.Error()) - } - termios.Ttyfd(file.Fd()) - termios.SetRaw() - return file -} -func usage() { - fmt.Fprintf(os.Stderr, "usage: myprog [inputfile]\n") - flag.PrintDefaults() - os.Exit(2) -} - -func SerialWriter(c chan string, serial * os.File ) { - for { - serial.WriteString(<-c) - serial.Sync() - } -} - -func SerialReader(c chan string , serial * bufio.Reader) { - for { - s,err := readLineSafe(serial) - if (s=="") { - continue - } - if (err!=nil) { - fmt.Printf("Error in read from serial: %v\n",err.Error()) - os.Exit(1) - } - //fmt.Printf("Serial: Read %v\n",s); - c<-s - } -} - -func openSerial(filename string) (chan string,chan string) { - serial:=openTTY(filename) - in:=make(chan string) - out:=make(chan string) - go SerialWriter(out,serial) - go SerialReader(in,bufio.NewReaderSize(serial,128)) - return in,out -} - -func SerialHandler(serial_i chan string) { - for { - line:=<-serial_i - fmt.Printf("Serial Read: %s\n",line) - lock.RLock() - for _, v := range DoorConnectionMap{ - select { - case v.wchan<-line: - - case <-time.After(2): - - } - } - lock.RUnlock() - } -} - -func chanWriter(c chan string, w io.Writer) { - for { - line := <-c - w.Write([]byte(line)) - w.Write([]byte("\n")) - } -} -func main() { -// lock = make(sync.RWMutex) - flag.Usage = usage - flag.Parse() - - args := flag.Args() - if len(args) < 1 { - fmt.Println("Input file is missing."); - os.Exit(1); - } - serial_i,serial_o:=openSerial(args[0]) - go SerialHandler(serial_i) - serial_o<-"f" - - ln, err := net.Listen("unix", "/tmp/test.sock") - if err != nil { - fmt.Printf("Error: %s\n",err.Error()) - return - } - fmt.Printf("Listener started\n") - - var connectionID uint =0 - for { - conn, err := ln.Accept() - if err != nil { - // handle error - continue - } - client:=connToReadWriter(conn,conn) - connectionID++ - writeChan := make(chan string) - lock.Lock() - DoorConnectionMap[connectionID]= DoorConnection{ rw:client,c:conn, wchan:writeChan } - lock.Unlock() - go handleConnection(conn,client,connectionID) - go chanWriter(writeChan,conn) - } -} diff --git a/go/door_daemon/run.sh b/go/door_daemon/run.sh new file mode 100755 index 0000000..cc2ebd3 --- /dev/null +++ b/go/door_daemon/run.sh @@ -0,0 +1,4 @@ +#!/bin/sh +clear +rm /tmp/test.sock 2>/dev/null +go build && ./door_daemon_go /dev/ttyACM0 || sleep 5 diff --git a/go/door_daemon/unix_socket_server.go b/go/door_daemon/unix_socket_server.go new file mode 100644 index 0000000..6598e6b --- /dev/null +++ b/go/door_daemon/unix_socket_server.go @@ -0,0 +1,225 @@ +package main +import "fmt" +import "net" +import "bufio" +import "strings" +import "os" +import "io" +import "realraum.at/termios" +import "flag" +import "regexp" +import "sync" +import "time" + +var lock sync.RWMutex + +type DoorConnection struct { + rw * bufio.ReadWriter + c net.Conn + wchan chan string +} + +var DoorConnectionMap = map[uint]DoorConnection { +} + +var cmdHandler = map[string]func([]string,string,*bufio.ReadWriter ) { + "test":handleCmdTest, + "open":handleCmdController, + "close":handleCmdController, + "toggle":handleCmdController, +} + + +func readLineSafe(rw *bufio.Reader) (string, error) { + wasPrefix:=false + var line string + for isPrefix:=true;isPrefix; { + var lineBuf []byte + var err error + lineBuf,isPrefix,err = rw.ReadLine() + if err != nil { + return "",err + } + if isPrefix { + wasPrefix=true + } else { + line=string(lineBuf) + } + } + if wasPrefix { + fmt.Println("line too long") + //fmt.Fprintf(rw,"line too long\n") + //rw.Flush() + return "",nil + } + reg := regexp.MustCompile("\r") + safe := reg.ReplaceAllString(line, "") + return safe,nil +} + +func connToReadWriter(c io.Reader,cw io.Writer) (*bufio.ReadWriter) { + client_r := bufio.NewReaderSize(c,1024) + client_w := bufio.NewWriterSize(cw,1024) + return bufio.NewReadWriter(client_r,client_w) +} + +func handleConnection(c net.Conn, client * bufio.ReadWriter, connID uint) () { + fmt.Println("new connection") + defer func () { + lock.Lock() + delete(DoorConnectionMap,connID) + lock.Unlock() + }() + for { + line,err:=readLineSafe(bufio.NewReader(client)) + if err != nil { + if err.Error() != "EOF" { + fmt.Printf("Error: readLineSafe returned %v\n",err.Error()) + } else { + fmt.Printf("Connection closed by remote host\n"); + } + c.Close() + return + } + if line == "" { + continue + } + fmt.Printf("Received: %v\n", line) + tokens:=strings.Fields(line) + remainStr:=strings.Join(tokens[1:]," ") + handleCmd(tokens,remainStr,client) + } +} + +func handleCmd(tokens []string, remainStr string,client * bufio.ReadWriter) { + cmd:=tokens[0] + func_ptr,present := cmdHandler[cmd] + if present { + func_ptr(tokens, remainStr,client) + } else { + fmt.Printf("Error: unknown Cmd: %v\n", cmd) + } +} + +func handleCmdTest(tokens []string, remainStr string, client * bufio.ReadWriter) { + //cmd:=tokens[0] + fmt.Printf("Test: %v\n", remainStr) +} + +func handleCmdController(tokens []string, remainStr string, client * bufio.ReadWriter) { + cmd:=tokens[0] + s_r:=strings.NewReader(cmd) + char := make([]byte,1) + s_r.Read(char) + fmt.Println(string(char)) +} + + +func openTTY(name string) *os.File { + file, err := os.OpenFile(name,os.O_RDWR ,0600) // For read access. + if err != nil { + fmt.Println(err.Error()) + } + termios.Ttyfd(file.Fd()) + termios.SetRaw() + return file +} +func usage() { + fmt.Fprintf(os.Stderr, "usage: myprog [inputfile]\n") + flag.PrintDefaults() + os.Exit(2) +} + +func SerialWriter(c chan string, serial * os.File ) { + for { + serial.WriteString(<-c) + serial.Sync() + } +} + +func SerialReader(c chan string , serial * bufio.Reader) { + for { + s,err := readLineSafe(serial) + if (s=="") { + continue + } + if (err!=nil) { + fmt.Printf("Error in read from serial: %v\n",err.Error()) + os.Exit(1) + } + //fmt.Printf("Serial: Read %v\n",s); + c<-s + } +} + +func openSerial(filename string) (chan string,chan string) { + serial:=openTTY(filename) + in:=make(chan string) + out:=make(chan string) + go SerialWriter(out,serial) + go SerialReader(in,bufio.NewReaderSize(serial,128)) + return in,out +} + +func SerialHandler(serial_i chan string) { + for { + line:=<-serial_i + fmt.Printf("Serial Read: %s\n",line) + lock.RLock() + for _, v := range DoorConnectionMap{ + select { + case v.wchan<-line: + + case <-time.After(2): + + } + } + lock.RUnlock() + } +} + +func chanWriter(c chan string, w io.Writer) { + for { + line := <-c + w.Write([]byte(line)) + w.Write([]byte("\n")) + } +} +func main() { +// lock = make(sync.RWMutex) + flag.Usage = usage + flag.Parse() + + args := flag.Args() + if len(args) < 1 { + fmt.Println("Input file is missing."); + os.Exit(1); + } + serial_i,serial_o:=openSerial(args[0]) + go SerialHandler(serial_i) + serial_o<-"f" + + ln, err := net.Listen("unix", "/tmp/test.sock") + if err != nil { + fmt.Printf("Error: %s\n",err.Error()) + return + } + fmt.Printf("Listener started\n") + + var connectionID uint =0 + for { + conn, err := ln.Accept() + if err != nil { + // handle error + continue + } + client:=connToReadWriter(conn,conn) + connectionID++ + writeChan := make(chan string) + lock.Lock() + DoorConnectionMap[connectionID]= DoorConnection{ rw:client,c:conn, wchan:writeChan } + lock.Unlock() + go handleConnection(conn,client,connectionID) + go chanWriter(writeChan,conn) + } +} diff --git a/go/termios/termios.go b/go/termios/termios.go new file mode 100644 index 0000000..11325c9 --- /dev/null +++ b/go/termios/termios.go @@ -0,0 +1,121 @@ +package termios + +import ( + "fmt"; + "os"; + "syscall"; + "unsafe" + "errors" +) + +// termios types +type cc_t byte +type speed_t uint +type tcflag_t uint + +// termios constants +const ( + BRKINT = tcflag_t (0000002); + ICRNL = tcflag_t (0000400); + INPCK = tcflag_t (0000020); + ISTRIP = tcflag_t (0000040); + IXON = tcflag_t (0002000); + OPOST = tcflag_t (0000001); + CS8 = tcflag_t (0000060); + ECHO = tcflag_t (0000010); + ICANON = tcflag_t (0000002); + IEXTEN = tcflag_t (0100000); + ISIG = tcflag_t (0000001); + VTIME = tcflag_t (5); + VMIN = tcflag_t (6) +) + +const NCCS = 32 +type termios struct { + c_iflag, c_oflag, c_cflag, c_lflag tcflag_t; + c_line cc_t; + c_cc [NCCS]cc_t; + c_ispeed, c_ospeed speed_t +} + +// ioctl constants +const ( + TCGETS = 0x5401; + TCSETS = 0x5402 +) + +var ( + orig_termios termios; + ttyfd uintptr = 0 // STDIN_FILENO +) + +func Ttyfd(fd uintptr) { + ttyfd=fd +} + +func getTermios (dst *termios) error { + r1, _, errno := syscall.Syscall (syscall.SYS_IOCTL, + uintptr (ttyfd), uintptr (TCGETS), + uintptr (unsafe.Pointer (dst))); + + if err := os.NewSyscallError ("SYS_IOCTL", errno); errno!=0 && err != nil { + return err + } + + if r1 != 0 { + // return errors.New("Error") + } + + return nil +} + +func setTermios (src *termios) error { + r1, _, errno := syscall.Syscall (syscall.SYS_IOCTL, + uintptr (ttyfd), uintptr (TCSETS), + uintptr (unsafe.Pointer (src))); + + if err := os.NewSyscallError ("SYS_IOCTL", errno); errno!=0 &&err != nil { + return err + } + + if r1 != 0 { + return errors.New ("Error") + } + + return nil +} + +func tty_raw () error { + raw := orig_termios; + + raw.c_iflag &= ^(BRKINT | ICRNL | INPCK | ISTRIP | IXON); + raw.c_oflag &= ^(OPOST); + raw.c_cflag |= (CS8); + raw.c_lflag &= ^(ECHO | ICANON | IEXTEN | ISIG); + + raw.c_cc[VMIN] = 1; + raw.c_cc[VTIME] = 0; + + if err := setTermios (&raw); err != nil { return err } + + return nil +} + +func SetRaw () { + var ( + err error + ) + + defer func () { + if err != nil { fmt.Printf ("SetRaw Error: %v\n",err) } + } (); + + if err = getTermios (&orig_termios); err != nil { return } + +// defer func () { +// err = setTermios (&orig_termios) +// } (); + + if err = tty_raw (); err != nil { return } + //if err = screenio (); err != nil { return } +} -- 1.7.10.4