From 438cb57d5ead62803bb5b787907f4911dfa787cc Mon Sep 17 00:00:00 2001 From: Aleksander Mistewicz Date: Tue, 9 Jan 2018 13:48:13 +0100 Subject: [PATCH] Add Interface to stm package 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 --- sw/nanopi/cmd/fota/fota.go | 13 +++++--- sw/nanopi/cmd/stm/stm.go | 13 ++++---- sw/nanopi/fota/fota.go | 14 ++++---- sw/nanopi/fota/sdcard.go | 8 ++--- sw/nanopi/stm/stm.go | 80 +++++++++++++++++++++------------------------- sw/nanopi/stm/stm_test.go | 29 +++++++++-------- 6 files changed, 78 insertions(+), 79 deletions(-) diff --git a/sw/nanopi/cmd/fota/fota.go b/sw/nanopi/cmd/fota/fota.go index 9a6e90f..464b418 100644 --- a/sw/nanopi/cmd/fota/fota.go +++ b/sw/nanopi/cmd/fota/fota.go @@ -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. @@ -23,6 +23,7 @@ import ( "os" "git.tizen.org/tools/muxpi/sw/nanopi/fota" + "git.tizen.org/tools/muxpi/sw/nanopi/stm" ) var ( @@ -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()) } diff --git a/sw/nanopi/cmd/stm/stm.go b/sw/nanopi/cmd/stm/stm.go index df73f21..366da41 100644 --- a/sw/nanopi/cmd/stm/stm.go +++ b/sw/nanopi/cmd/stm/stm.go @@ -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. @@ -46,8 +46,9 @@ 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 { @@ -55,11 +56,11 @@ func main() { 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)) } } diff --git a/sw/nanopi/fota/fota.go b/sw/nanopi/fota/fota.go index 0c28b14..4241aeb 100644 --- a/sw/nanopi/fota/fota.go +++ b/sw/nanopi/fota/fota.go @@ -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. @@ -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. diff --git a/sw/nanopi/fota/sdcard.go b/sw/nanopi/fota/sdcard.go index bed8c41..845283b 100644 --- a/sw/nanopi/fota/sdcard.go +++ b/sw/nanopi/fota/sdcard.go @@ -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. @@ -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) @@ -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) } diff --git a/sw/nanopi/stm/stm.go b/sw/nanopi/stm/stm.go index bf22f74..671075a 100644 --- a/sw/nanopi/stm/stm.go +++ b/sw/nanopi/stm/stm.go @@ -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. @@ -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 @@ -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() -} diff --git a/sw/nanopi/stm/stm_test.go b/sw/nanopi/stm/stm_test.go index 87748be..4b97c23 100644 --- a/sw/nanopi/stm/stm_test.go +++ b/sw/nanopi/stm/stm_test.go @@ -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. @@ -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()) }) }) -- 2.7.4