Skip to content

Commit

Permalink
Refactor mavlink serialport connection instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Dec 18, 2014
1 parent 3ab7aa1 commit 3b82731
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
15 changes: 6 additions & 9 deletions platforms/mavlink/mavlink_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,16 @@ type MavlinkAdaptor struct {
name string
port string
sp io.ReadWriteCloser
connect func(*MavlinkAdaptor) (err error)
connect func(string) (io.ReadWriteCloser, error)
}

// NewMavLinkAdaptor creates a new mavlink adaptor with specified name and port
func NewMavlinkAdaptor(name string, port string) *MavlinkAdaptor {
return &MavlinkAdaptor{
name: name,
port: port,
connect: func(m *MavlinkAdaptor) (err error) {
s, err := serial.OpenPort(&serial.Config{Name: m.Port(), Baud: 57600})
if err != nil {
return err
}
m.sp = s
return
connect: func(port string) (io.ReadWriteCloser, error) {
return serial.OpenPort(&serial.Config{Name: port, Baud: 57600})
},
}
}
Expand All @@ -37,8 +32,10 @@ func (m *MavlinkAdaptor) Port() string { return m.port }

// Connect returns true if connection to device is successful
func (m *MavlinkAdaptor) Connect() (errs []error) {
if err := m.connect(m); err != nil {
if sp, err := m.connect(m.Port()); err != nil {
return []error{err}
} else {
m.sp = sp
}
return
}
Expand Down
5 changes: 3 additions & 2 deletions platforms/mavlink/mavlink_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mavlink

import (
"errors"
"io"
"testing"

"github.com/hybridgroup/gobot"
Expand Down Expand Up @@ -43,7 +44,7 @@ func (nullReadWriteCloser) Close() error {
func initTestMavlinkAdaptor() *MavlinkAdaptor {
m := NewMavlinkAdaptor("myAdaptor", "/dev/null")
m.sp = nullReadWriteCloser{}
m.connect = func(a *MavlinkAdaptor) (err error) { return nil }
m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil }
return m
}

Expand All @@ -56,7 +57,7 @@ func TestMavlinkAdaptorConnect(t *testing.T) {
a := initTestMavlinkAdaptor()
gobot.Assert(t, len(a.Connect()), 0)

a.connect = func(a *MavlinkAdaptor) (err error) { return errors.New("connect error") }
a.connect = func(port string) (io.ReadWriteCloser, error) { return nil, errors.New("connect error") }
gobot.Assert(t, a.Connect()[0], errors.New("connect error"))
}

Expand Down
5 changes: 3 additions & 2 deletions platforms/mavlink/mavlink_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mavlink

import (
"errors"
"io"
"testing"
"time"

Expand All @@ -11,15 +12,15 @@ import (

func initTestMavlinkDriver() *MavlinkDriver {
m := NewMavlinkAdaptor("myAdaptor", "/dev/null")
m.connect = func(a *MavlinkAdaptor) (err error) { return nil }
m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil }
m.sp = nullReadWriteCloser{}
return NewMavlinkDriver(m, "myDriver")
}

func TestMavlinkDriver(t *testing.T) {
m := NewMavlinkAdaptor("myAdaptor", "/dev/null")
m.sp = nullReadWriteCloser{}
m.connect = func(a *MavlinkAdaptor) (err error) { return nil }
m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil }

d := NewMavlinkDriver(m, "myDriver")
gobot.Assert(t, d.Name(), "myDriver")
Expand Down

0 comments on commit 3b82731

Please sign in to comment.