/*
- * 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.
"os"
"git.tizen.org/tools/muxpi/sw/nanopi/fota"
+ "git.tizen.org/tools/muxpi/sw/nanopi/stm"
)
var (
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())
}
/*
- * 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.
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 {
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))
}
}
/*
- * 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.
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.
/*
- * 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.
// 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)
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)
}
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.
}
}
+// 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
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()
-}
/*
- * 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.
)
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())
})
})