Skip to content

Commit

Permalink
Add Interface to stm package
Browse files Browse the repository at this point in the history
It makes possible to replace underlying implementation. Package default
interface has been removed. GetDefaultSTM() should be used instead.

FOTA and STM were modified to work after this change.

Change-Id: I291a9d22bda7253556a4a13229b9db3400fe6ef5
  • Loading branch information
amistewicz committed May 25, 2018
1 parent adbe853 commit 438cb57
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 79 deletions.
13 changes: 8 additions & 5 deletions sw/nanopi/cmd/fota/fota.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
* Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@ import (
"os"

"git.tizen.org/tools/muxpi/sw/nanopi/fota"
"git.tizen.org/tools/muxpi/sw/nanopi/stm"
)

var (
Expand Down Expand Up @@ -71,15 +72,17 @@ func main() {
decoder := json.NewDecoder(f)
checkErr("failed to decode the mapping: ", decoder.Decode(&partMapping))

flasher, err := fota.NewFOTA(flag.Args(), md5sums, sdcard, partMapping)
checkErr("failed to initialize FOTA: ", err)
defer flasher.Close()
dev, err := stm.GetDefaultSTM()
checkErr("failed to connect to STM: ", err)
defer dev.Close()

flasher := fota.NewFOTA(dev, flag.Args(), md5sums, sdcard, partMapping)
if !quiet {
flasher.SetVerbose()
}
verbose("FOTA initialized")

checkErr("SDcard not found: ", fota.WaitForSDcard(sdcard, 10))
checkErr("SDcard not found: ", fota.WaitForSDcard(dev, sdcard, 10))
verbose("SDcard detected")
checkErr("failed to flash images: ", flasher.DownloadAndFlash())
}
13 changes: 7 additions & 6 deletions sw/nanopi/cmd/stm/stm.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
* Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,20 +46,21 @@ func main() {
setFlags()
flag.Parse()

checkErr("failed to open connection to STM:", stm.Open())
defer stm.Close()
dev, err := stm.GetDefaultSTM()
checkErr("failed to open connection to dev:", err)
defer dev.Close()

// SD card multiplexer related actions.
switch {
// Only one is allowed at a time.
case ts && dut:
log.Fatal("conflicting flags: DUT and TS")
case ts:
checkErr("failed to switch to the test server: ", stm.TS())
checkErr("failed to switch to the test server: ", dev.TS())
case dut:
checkErr("failed to switch to the DUT: ", stm.DUT())
checkErr("failed to switch to the DUT: ", dev.DUT())
}
if tick {
checkErr("failed to tick the power supply: ", stm.PowerTick(tickDuration))
checkErr("failed to tick the power supply: ", dev.PowerTick(tickDuration))
}
}
14 changes: 6 additions & 8 deletions sw/nanopi/fota/fota.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
* Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,22 +50,20 @@ type FOTA struct {
PartMapping map[string]string
// verbose allows logging to default log.Logger instance or prevents it if false.
verbose bool
// dev is interface to STM.
dev stm.Interface
}

// NewFOTA returns new instance of FOTA. It also opens connection to STM.
func NewFOTA(URLs []string, md5sums string, sdcard string, partMapping map[string]string) (*FOTA, error) {
func NewFOTA(dev stm.Interface, URLs []string, md5sums string, sdcard string, partMapping map[string]string) *FOTA {
return &FOTA{
URLs: URLs,
md5sums: md5sums,
checksums: make(map[string]string),
SDcard: sdcard,
PartMapping: partMapping,
}, stm.Open()
}

// Close releases FOTA resources.
func (fota *FOTA) Close() error {
return stm.Close()
dev: dev,
}
}

// SetVerbose increases logging of FOTA actions.
Expand Down
8 changes: 4 additions & 4 deletions sw/nanopi/fota/sdcard.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
* Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,7 +51,7 @@ func checkFile(filename string) (bool, error) {
// WaitForSDcard checks if an SDcard is present in the system and returns.
// If card is not found it restarts it by DUT -> TS switch and tries again until
// number of retryCount is exceeded (then it returns an error).
func WaitForSDcard(sdcard string, retryCount int) error {
func WaitForSDcard(dev stm.Interface, sdcard string, retryCount int) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("starting watcher failed: %s", err)
Expand All @@ -76,13 +76,13 @@ func WaitForSDcard(sdcard string, retryCount int) error {
return nil
}

err = stm.DUT()
err = dev.DUT()
if err != nil {
return fmt.Errorf("failed to DUT: %s", err)
}
// It is good to make sure that it is completely disconnected from the reader.
time.Sleep(2 * time.Second)
err = stm.TS()
err = dev.TS()
if err != nil {
return fmt.Errorf("failed to TS: %s", err)
}
Expand Down
80 changes: 37 additions & 43 deletions sw/nanopi/stm/stm.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,32 @@ type STM struct {
mux *sync.Mutex
}

var muxPi *STM
// UserInterface contains methods of STM that are intended to
// be used by a regular user and admin.
type UserInterface interface {
PowerTick(d time.Duration) (err error)
DUT() (err error)
TS() (err error)
}

// AdminInterface contains methods of STM that are intended to
// be used by administrators only.
type AdminInterface interface {
SetLED(led LED, r, g, b uint8) (err error)
ClearDisplay() (err error)
PrintText(x, y uint, color Color, text string) (err error)
}

func init() {
muxPi = NewSTM("/dev/ttyS2", 115200)
// Interface contains all methods of STM.
type Interface interface {
UserInterface
AdminInterface
}

// InterfaceCloser is Interface expanded by Close method.
type InterfaceCloser interface {
Interface
Close() error
}

// NewSTM prepares STM structure with serial configuration.
Expand All @@ -85,6 +107,18 @@ func NewSTM(ttyPath string, baudrate int) *STM {
}
}

// GetDefaultSTM provides InterfaceCloser to STM with default values. The caller should call Close()
// to free the underlying serial connection. The returned instance is different for each call. Care
// should be taken to not use two such objects concurrently as they use the same device.
func GetDefaultSTM() (InterfaceCloser, error) {
stm := NewSTM("/dev/ttyS2", 115200)
err := stm.Open()
if err != nil {
return nil, err
}
return stm, err
}

// Open starts a serial connection.
//
// It should be called only once after structure creation
Expand Down Expand Up @@ -196,43 +230,3 @@ func (stm *STM) DUT() error {
func (stm *STM) TS() error {
return stm.executeCommand("ts")
}

// Open is a convenience function for default MuxPi settings.
func Open() error {
return muxPi.Open()
}

// Close is a convenience function for default MuxPi settings.
func Close() error {
return muxPi.Close()
}

// PowerTick is a convenience function for default MuxPi settings.
func PowerTick(d time.Duration) error {
return muxPi.PowerTick(d)
}

// SetLED is a convenience function for default MuxPi settings.
func SetLED(led LED, r, g, b uint8) error {
return muxPi.SetLED(led, r, g, b)
}

// ClearDisplay is a convenience function for default MuxPi settings.
func ClearDisplay() error {
return muxPi.ClearDisplay()
}

// PrintText is a convenience function for default MuxPi settings.
func PrintText(x, y uint, color Color, text string) error {
return muxPi.PrintText(x, y, color, text)
}

// DUT is a convenience function for default MuxPi settings.
func DUT() error {
return muxPi.DUT()
}

// TS is a convenience function for default MuxPi settings.
func TS() error {
return muxPi.TS()
}
29 changes: 16 additions & 13 deletions sw/nanopi/stm/stm_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
* Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,57 +26,60 @@ import (
)

var _ = Describe("Stm", func() {
var dev InterfaceCloser

BeforeEach(func() {
err := Open()
var err error
dev, err = GetDefaultSTM()
Expect(err).ToNot(HaveOccurred())
})

AfterEach(func() {
err := Close()
err := dev.Close()
Expect(err).ToNot(HaveOccurred())
})

It("should switch to DUT", func() {
err := DUT()
err := dev.DUT()
Expect(err).ToNot(HaveOccurred())
})

It("should do power tick", func() {
err := PowerTick(time.Second)
err := dev.PowerTick(time.Second)
Expect(err).ToNot(HaveOccurred())
})

It("should switch to TS", func() {
err := TS()
err := dev.TS()
Expect(err).ToNot(HaveOccurred())
})

It("should clear the display", func() {
err := ClearDisplay()
err := dev.ClearDisplay()
Expect(err).ToNot(HaveOccurred())
})

It("should print text on the display", func() {
err := PrintText(0, 0, Foreground, "test text")
err := dev.PrintText(0, 0, Foreground, "test text")
Expect(err).ToNot(HaveOccurred())
time.Sleep(2 * time.Second)
err = PrintText(0, 0, Background, "test text")
err = dev.PrintText(0, 0, Background, "test text")
Expect(err).ToNot(HaveOccurred())
})

It("should blink the left LED", func() {
err := SetLED(LED1, 255, 255, 255)
err := dev.SetLED(LED1, 255, 255, 255)
Expect(err).ToNot(HaveOccurred())
time.Sleep(2 * time.Second)
err = SetLED(LED1, 0, 0, 0)
err = dev.SetLED(LED1, 0, 0, 0)
Expect(err).ToNot(HaveOccurred())
})

It("should blink the right LED", func() {
err := SetLED(LED2, 255, 255, 255)
err := dev.SetLED(LED2, 255, 255, 255)
Expect(err).ToNot(HaveOccurred())
time.Sleep(2 * time.Second)
err = SetLED(LED2, 0, 0, 0)
err = dev.SetLED(LED2, 0, 0, 0)
Expect(err).ToNot(HaveOccurred())
})
})

0 comments on commit 438cb57

Please sign in to comment.