Add GetCurrent to Interface 33/170233/7
authorAleksander Mistewicz <a.mistewicz@samsung.com>
Mon, 5 Feb 2018 17:37:15 +0000 (18:37 +0100)
committerAleksander Mistewicz <a.mistewicz@samsung.com>
Fri, 25 May 2018 11:28:34 +0000 (13:28 +0200)
GetCurrent method of STM interface fetches the value of the measured
current drawn by DUT. It sends "current" to STM's UART and parses
the response, which is expected to be an integer.

A new internal helper function, sendAndReceive, is added.

Change-Id: Ifb8b634b5b432685d6b69dc76ebc8f4e9f8fa189

sw/nanopi/cmd/stm/stm.go
sw/nanopi/stm/stm.go

index 366da41..03c0e12 100644 (file)
@@ -18,6 +18,7 @@ package main
 
 import (
        "flag"
+       "fmt"
        "log"
        "time"
 
@@ -27,6 +28,7 @@ import (
 var (
        ts, dut, tick bool
        tickDuration  time.Duration
+       cur           bool
 )
 
 func setFlags() {
@@ -34,6 +36,7 @@ func setFlags() {
        flag.BoolVar(&ts, "ts", false, "connect SD card to test server")
        flag.BoolVar(&dut, "dut", false, "connect SD card to DUT")
        flag.BoolVar(&tick, "tick", false, "power off the DUT, wait 'm' seconds and switch it on again")
+       flag.BoolVar(&cur, "cur", false, "get reading of the current drawn by DUT")
 }
 
 func checkErr(ctx string, err error) {
@@ -63,4 +66,9 @@ func main() {
        if tick {
                checkErr("failed to tick the power supply: ", dev.PowerTick(tickDuration))
        }
+       if cur {
+               i, err := dev.GetCurrent()
+               checkErr("failed to read the power consumption: ", err)
+               fmt.Println(i)
+       }
 }
index 671075a..92cdbe4 100644 (file)
@@ -21,6 +21,7 @@ import (
        "bufio"
        "fmt"
        "io"
+       "strconv"
        "strings"
        "sync"
        "time"
@@ -71,6 +72,7 @@ type UserInterface interface {
        PowerTick(d time.Duration) (err error)
        DUT() (err error)
        TS() (err error)
+       GetCurrent() (value int, err error)
 }
 
 // AdminInterface contains methods of STM that are intended to
@@ -150,6 +152,28 @@ func (stm *STM) readResponse() (string, error) {
        return string(response), nil
 }
 
+// sendAndReceive writes cmd, optionally reads a response if withResponse is set to true,
+// and checks for confirmation of successful execution.
+func (stm *STM) sendAndReceive(cmd string, withResponse bool) (response string, err error) {
+       stm.mux.Lock()
+       defer stm.mux.Unlock()
+       _, err = io.WriteString(stm.port, cmd+"\n")
+       if err != nil {
+               return "", fmt.Errorf("failed to write a command: %s", err)
+       }
+       if withResponse {
+               response, err = stm.readResponse()
+               if err != nil {
+                       return "", err
+               }
+       }
+       err = stm.checkOK()
+       if err != nil {
+               return "", err
+       }
+       return response, nil
+}
+
 // checkOK reads a line from the buffer and rises an error
 // if it is not an expected confirmation response.
 //
@@ -165,15 +189,10 @@ func (stm *STM) checkOK() error {
        return nil
 }
 
-// executeCommand sends a prepared cmd string and checks the response.
+// executeCommand sends a prepared cmd string and checks for OK response.
 func (stm *STM) executeCommand(cmd string) (err error) {
-       stm.mux.Lock()
-       defer stm.mux.Unlock()
-       _, err = io.WriteString(stm.port, cmd+"\n")
-       if err != nil {
-               return fmt.Errorf("failed to write a command: %s", err)
-       }
-       return stm.checkOK()
+       _, err = stm.sendAndReceive(cmd, false)
+       return
 }
 
 // noEcho requires special handling as there are more possible responses.
@@ -230,3 +249,17 @@ func (stm *STM) DUT() error {
 func (stm *STM) TS() error {
        return stm.executeCommand("ts")
 }
+
+// GetCurrent reads value of current drawn by DUT.
+// The value is in milliamperes [mA].
+func (stm *STM) GetCurrent() (int, error) {
+       str, err := stm.sendAndReceive("current", true)
+       if err != nil {
+               return 0, err
+       }
+       i, err := strconv.Atoi(str)
+       if err != nil {
+               return 0, err
+       }
+       return i, nil
+}