Skip to content

Commit

Permalink
MDB baud rate is fixed 9600, no need for parameter
Browse files Browse the repository at this point in the history
plus hardware section in config
  • Loading branch information
temoto committed Dec 3, 2018
1 parent ab73bdb commit 910c01a
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 44 deletions.
2 changes: 1 addition & 1 deletion cmd/mdb-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {
}
defer uarter.Close()

m, err := mdb.NewMDB(uarter, *devicePath, 0)
m, err := mdb.NewMDB(uarter, *devicePath)
if err != nil {
log.Fatalf("mdb open: %v", errors.ErrorStack(err))
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/vender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ func main() {
log.Fatal(errors.ErrorStack(err))
}

mdber, err := mdb.NewMDB(config.Mdb.Uarter, config.Mdb.UartDevice, config.Mdb.UartBaudrate)
mdber, err := mdb.NewMDB(config.Hardware.Mdb.Uarter, config.Hardware.Mdb.UartDevice)
if err != nil {
log.Fatal(errors.ErrorStack(err))
}
if config.Mdb.Log {
if config.Hardware.Mdb.Log {
mdber.SetLog(log.Printf)
}
mdber.BreakCustom(200*time.Millisecond, 500*time.Millisecond)
Expand Down
5 changes: 1 addition & 4 deletions hardware/mdb/io_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ func (self *fileUart) Close() error {
return self.f.Close()
}

func (self *fileUart) Open(path string, baud int) (err error) {
if baud != 9600 {
return errors.NotSupportedf("fileUart: baud rate other than 9600 is not supported")
}
func (self *fileUart) Open(path string) (err error) {
if self.f != nil {
self.Close() // skip error
}
Expand Down
5 changes: 1 addition & 4 deletions hardware/mdb/io_iodin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ func (self *iodinUart) Break(d time.Duration) error {
return errors.Trace(err)
}

func (self *iodinUart) Open(path string, baud int) error {
if baud != 9600 {
return errors.New("Not implemented support for baud rate other than 9600")
}
func (self *iodinUart) Open(path string) error {
var r iodin.Response
err := self.c.Do(&iodin.Request{Command: iodin.Request_MDB_OPEN, ArgBytes: []byte(path)}, &r)
return errors.Trace(err)
Expand Down
9 changes: 3 additions & 6 deletions hardware/mdb/mdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type Uarter interface {
Break(d time.Duration) error
Close() error
Open(path string, baud int) error
Open(path string) error
Tx(request, response []byte) (int, error)
}

Expand Down Expand Up @@ -65,16 +65,13 @@ func checksum(bs []byte) byte {
return chk
}

func NewMDB(u Uarter, path string, baud int) (*mdb, error) {
func NewMDB(u Uarter, path string) (*mdb, error) {
self := &mdb{
io: u,
log: helpers.Discardf,
recvBuf: make([]byte, 0, PacketMaxLength),
}
if baud == 0 {
baud = 9600
}
err := self.io.Open(path, baud)
err := self.io.Open(path)
return self, errors.Annotate(err, "NewMDB")
}

Expand Down
2 changes: 1 addition & 1 deletion hardware/mdb/mdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func testMdberStrings(t testing.TB, r io.Reader, w io.Writer) Mdber {
m, err := NewMDB(NewNullUart(r, w), "", 0)
m, err := NewMDB(NewNullUart(r, w), "")
if err != nil {
t.Fatal(errors.ErrorStack(err))
}
Expand Down
6 changes: 3 additions & 3 deletions hardware/mdb/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (self *nullUart) Close() error {
return nil
}

func (self *nullUart) Open(path string, baud int) (err error) { return nil }
func (self *nullUart) Open(path string) error { return nil }

func (self *nullUart) Tx(request, response []byte) (n int, err error) {
if _, err = self.write9(request, true); err != nil {
Expand Down Expand Up @@ -115,7 +115,7 @@ func NewTestMDBRaw(t testing.TB) (Mdber, func([]byte), *bytes.Buffer) {
r := bytes.NewBuffer(nil)
w := bytes.NewBuffer(nil)
uarter := NewNullUart(r, w)
m, err := NewMDB(uarter, "", 0)
m, err := NewMDB(uarter, "")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -169,7 +169,7 @@ func NewChanIO(timeout time.Duration) *ChanIO {
func NewTestMDBChan(t testing.TB) (Mdber, <-chan *Packet, chan<- *Packet) {
cio := NewChanIO(5 * time.Second)
uarter := NewNullUart(cio, cio)
m, err := NewMDB(uarter, "", 0)
m, err := NewMDB(uarter, "")
if err != nil {
t.Fatal(err)
}
Expand Down
27 changes: 14 additions & 13 deletions head/state/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import (
)

type Config struct {
IodinPath string `hcl:"iodin_path"`
Mdb struct {
Log bool `hcl:"log_enable"`
Uarter mdb.Uarter
UartDevice string `hcl:"uart_device"`
UartDriver string `hcl:"uart_driver"`
UartBaudrate int `hcl:"uart_baudrate"`
Hardware struct {
IodinPath string `hcl:"iodin_path"`
Mdb struct {
Log bool `hcl:"log_enable"`
Uarter mdb.Uarter
UartDevice string `hcl:"uart_device"`
UartDriver string `hcl:"uart_driver"`
}
}
Papa struct {
Address string
Expand Down Expand Up @@ -52,17 +53,17 @@ func ReadConfig(r io.Reader) (*Config, error) {
c := new(Config)
err = hcl.Unmarshal(b, c)

switch c.Mdb.UartDriver {
switch c.Hardware.Mdb.UartDriver {
case "", "file":
c.Mdb.Uarter = mdb.NewFileUart()
c.Hardware.Mdb.Uarter = mdb.NewFileUart()
case "iodin":
iodin, err := iodin.NewClient(c.IodinPath)
iodin, err := iodin.NewClient(c.Hardware.IodinPath)
if err != nil {
return nil, errors.Annotatef(err, "config: mdb.uart_driver=%s iodin_path=%s", c.Mdb.UartDriver, c.IodinPath)
return nil, errors.Annotatef(err, "config: mdb.uart_driver=%s iodin_path=%s", c.Hardware.Mdb.UartDriver, c.Hardware.IodinPath)
}
c.Mdb.Uarter = mdb.NewIodinUart(iodin)
c.Hardware.Mdb.Uarter = mdb.NewIodinUart(iodin)
default:
return nil, fmt.Errorf("config: unknown mdb.uart_driver=\"%s\" valid: file, fast", c.Mdb.UartDriver)
return nil, fmt.Errorf("config: unknown mdb.uart_driver=\"%s\" valid: file, fast", c.Hardware.Mdb.UartDriver)
}

return c, err
Expand Down
6 changes: 3 additions & 3 deletions head/state/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ func TestReadConfig(t *testing.T) {
}
cases := []Case{
Case{"empty", "",
func(c *Config) bool { return !c.Mdb.Log }},
func(c *Config) bool { return !c.Hardware.Mdb.Log }},
Case{"mdb",
"mdb { uart_device = \"/dev/shmoo\" }",
func(c *Config) bool { return c.Mdb.UartDevice == "/dev/shmoo" },
"hardware { mdb { uart_device = \"/dev/shmoo\" } }",
func(c *Config) bool { return c.Hardware.Mdb.UartDevice == "/dev/shmoo" },
},
}
mkCheck := func(c Case) func(*testing.T) {
Expand Down
16 changes: 9 additions & 7 deletions vender.hcl
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
iodin_path = "target/release/iodin"
hardware {
iodin_path = "target/release/iodin"

mdb {
log_enable = false
mdb {
log_enable = false

uart_driver = "file"
uart_device = "/dev/ttyAMA0"
uart_driver = "file"
uart_device = "/dev/ttyAMA0"

#uart_driver = "iodin"
#uart_device = "\x0f\x0e"
#uart_driver = "iodin"
#uart_device = "\x0f\x0e"
}
}

papa {
Expand Down

0 comments on commit 910c01a

Please sign in to comment.