Skip to content

Commit

Permalink
Merge branch 'stdevAlDen_t167_emulator_ip_as_argument' into stdevAlDe…
Browse files Browse the repository at this point in the history
…n_t165_inprove_input_reader ref fibercrypto#165
  • Loading branch information
Alvaro Denis committed Feb 5, 2020
2 parents 1d8f120 + 113a1be commit 0ef4186
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed
- Change `protobuf` file definitions from http://github.com/skycoin/hardware-wallet-protob.git to http://github.com/fibercrypto/skywallet-go.git
- Be able to set the emulator ip as argument.

### Removed

Expand Down
12 changes: 10 additions & 2 deletions src/skywallet/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,23 @@ func initUsb() []usb.Bus {
}

// NewDriver create a new device driver
func NewDriver(deviceType DeviceType) (*Driver, error) {
func NewDriver(deviceType DeviceType, emulatorAddress ...string) (*Driver, error) {
switch deviceType {
case DeviceTypeUSB:
if len(emulatorAddress) > 0 {
log.WithField("emulatorAddress", emulatorAddress).Warningln("ip addresses ignored as this does not make sense for physical device")
}
return &Driver{
deviceType: deviceType,
bus: usb.Init(initUsb()...),
}, nil
case DeviceTypeEmulator:
udpBus, err := usb.InitUDP([]int{EmulatorPort})
if len(emulatorAddress) == 0 {
emulatorAddress = []string{"127.0.0.1"}
} else if len(emulatorAddress) > 1 {
return nil, WrrInvalidArgCountForEmulatorIpAddress
}
udpBus, err := usb.InitUDP([]int{EmulatorPort}, emulatorAddress[0])
if err != nil {
return nil, err
}
Expand Down
10 changes: 6 additions & 4 deletions src/skywallet/skywallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ var (
ErrDeviceTypeEmulator = errors.New("device type cannot be emulator")
// ErrInvalidWordCount is returned if word count is not valid mnemonic word length
ErrInvalidWordCount = errors.New("word count must be 12 or 24")
// WrrInvalidArgCountForEmulatorIpAddress emulator ip address is the only one expected argument for emulatorAddress
WrrInvalidArgCountForEmulatorIpAddress = errors.New("emulator ip address is the only one expected argument for emulatorAddress")
// ErrNoDeviceConnected is returned if no device is connected to the system
ErrNoDeviceConnected = errors.New("no device connected")
// ErrInvalidWalletType a valid wallet type should be specified
Expand Down Expand Up @@ -141,8 +143,8 @@ func DeviceTypeFromString(deviceType string) DeviceType {
var devSingleCreator sync.Once
var devSingleInstance *Device

func newDevice(deviceType DeviceType) *Device {
driver, err := NewDriver(deviceType)
func newDevice(deviceType DeviceType, emulatorAddress ...string) *Device {
driver, err := NewDriver(deviceType, emulatorAddress...)
if err != nil {
log.Fatalf("failed to create driver: %s", err)
}
Expand All @@ -158,11 +160,11 @@ func newDevice(deviceType DeviceType) *Device {
}

// NewDevice returns a new device instance
func NewDevice(deviceType DeviceType) *Device {
func NewDevice(deviceType DeviceType, emulatorAddress ...string) *Device {
// TODO rename NewDevice to DeviceInstance as this is a singleton
// implementation.
devSingleCreator.Do(func() {
devSingleInstance = newDevice(deviceType)
devSingleInstance = newDevice(deviceType, emulatorAddress...)
})
return devSingleInstance
}
Expand Down
8 changes: 5 additions & 3 deletions src/skywallet/usb/udp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package usb

import (
"fmt"
"io"
"net"
"strconv"
Expand All @@ -10,16 +11,17 @@ import (

const (
emulatorPrefix = "emulator"
emulatorAddress = "127.0.0.1"
)

type UDP struct {
ports []int
emulatorAddress string
}

func InitUDP(ports []int) (*UDP, error) {
func InitUDP(ports []int, emulatorAddress string) (*UDP, error) {
udp := UDP{
ports: ports,
emulatorAddress: emulatorAddress,
}

return &udp, nil
Expand Down Expand Up @@ -51,7 +53,7 @@ func (udp *UDP) Connect(path string) (Device, error) {
return nil, err
}

address := emulatorAddress + ":" + strconv.Itoa(port)
address := fmt.Sprintf("%s:%d", udp.emulatorAddress, port)
dev, err := net.Dial("udp", address)
if err != nil {
return nil, err
Expand Down

0 comments on commit 0ef4186

Please sign in to comment.