summaryrefslogtreecommitdiff
path: root/src/rhctl/serial_port.go
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2016-03-26 17:24:42 (GMT)
committerChristian Pointner <equinox@spreadspace.org>2016-03-26 17:24:42 (GMT)
commit7cfc7644c284b72997eb3b31b98fab447fe06bed (patch)
tree217b65c1418a1262610f683fdbe638e2a5d44920 /src/rhctl/serial_port.go
parente9d6068037e7ff2b8c7a006fb01b482b0b7de409 (diff)
added config file parser
improved baudrate handling
Diffstat (limited to 'src/rhctl/serial_port.go')
-rw-r--r--src/rhctl/serial_port.go111
1 files changed, 98 insertions, 13 deletions
diff --git a/src/rhctl/serial_port.go b/src/rhctl/serial_port.go
index 9f3f3f6..c7804a6 100644
--- a/src/rhctl/serial_port.go
+++ b/src/rhctl/serial_port.go
@@ -23,23 +23,108 @@ package main
import (
"bufio"
+ "errors"
+ "strconv"
"syscall"
"github.com/schleibinger/sio"
)
-type Baudrate uint32
+type Baudrate struct {
+ value uint32
+ code uint32
+}
-const (
- B1200 Baudrate = syscall.B1200
- B2400 = syscall.B2400
- B4800 = syscall.B4800
- B9600 = syscall.B9600
- B19200 = syscall.B19200
- B38400 = syscall.B38400
- B57600 = syscall.B57600
- B115200 = syscall.B115200
-)
+func NewBaudrate(value uint32) (b *Baudrate, err error) {
+ b = &Baudrate{}
+ err = b.FromUint(value)
+ return
+}
+
+func (b Baudrate) String() string {
+ return strconv.FormatUint(uint64(b.value), 10)
+}
+
+func (b *Baudrate) FromUint(value uint32) error {
+ b.value = value
+ switch b.value {
+ case 50:
+ b.code = syscall.B50
+ case 75:
+ b.code = syscall.B75
+ case 110:
+ b.code = syscall.B110
+ case 134:
+ b.code = syscall.B134
+ case 150:
+ b.code = syscall.B150
+ case 200:
+ b.code = syscall.B200
+ case 300:
+ b.code = syscall.B300
+ case 600:
+ b.code = syscall.B600
+ case 1200:
+ b.code = syscall.B1200
+ case 1800:
+ b.code = syscall.B1800
+ case 2400:
+ b.code = syscall.B2400
+ case 4800:
+ b.code = syscall.B4800
+ case 9600:
+ b.code = syscall.B9600
+ case 19200:
+ b.code = syscall.B19200
+ case 38400:
+ b.code = syscall.B38400
+ case 57600:
+ b.code = syscall.B57600
+ case 115200:
+ b.code = syscall.B115200
+ case 230400:
+ b.code = syscall.B230400
+ case 460800:
+ b.code = syscall.B460800
+ case 500000:
+ b.code = syscall.B500000
+ case 576000:
+ b.code = syscall.B576000
+ case 921600:
+ b.code = syscall.B921600
+ case 1000000:
+ b.code = syscall.B1000000
+ case 1152000:
+ b.code = syscall.B1152000
+ case 1500000:
+ b.code = syscall.B1500000
+ case 2000000:
+ b.code = syscall.B2000000
+ case 2500000:
+ b.code = syscall.B2500000
+ case 3000000:
+ b.code = syscall.B3000000
+ case 3500000:
+ b.code = syscall.B3500000
+ case 4000000:
+ b.code = syscall.B4000000
+ default:
+ return errors.New("invalid baudrate")
+ }
+ return nil
+}
+
+func (b *Baudrate) FromString(str string) error {
+ vuint, err := strconv.ParseUint(str, 10, 32)
+ if err != nil {
+ return err
+ }
+ return b.FromUint(uint32(vuint))
+}
+
+func (b *Baudrate) UnmarshalTOML(data []byte) error {
+ return b.FromString(string(data))
+}
type SerialPort struct {
port *sio.Port
@@ -69,9 +154,9 @@ func serialWriter(c <-chan string, port *sio.Port, newline string) {
port.Close()
}
-func SerialOpenAndHandle(device string, speed Baudrate, newline string) (port *SerialPort, err error) {
+func SerialOpenAndHandle(device string, rate Baudrate, newline string) (port *SerialPort, err error) {
port = &SerialPort{}
- if port.port, err = sio.Open(device, uint32(speed)); err != nil {
+ if port.port, err = sio.Open(device, rate.code); err != nil {
return
}
tx := make(chan string, 10)