import (
"flag"
+ "fmt"
"log"
"time"
var (
ts, dut, tick bool
tickDuration time.Duration
+ cur bool
)
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) {
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)
+ }
}
"bufio"
"fmt"
"io"
+ "strconv"
"strings"
"sync"
"time"
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
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.
//
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.
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
+}