summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2016-04-03 14:32:51 (GMT)
committerChristian Pointner <equinox@helsinki.at>2016-04-03 14:32:51 (GMT)
commit8da5bc7ed583e852cbdcae0170210c18551c382b (patch)
tree1a2f01a161da316e06895ec94d00c4e6b2db1882 /src
parent3d8206bf7883b56f5adb4252c7d0c9190d541874 (diff)
improved range checks for switch command parameter
Diffstat (limited to 'src')
-rw-r--r--src/rhctl/audio_switch.go25
-rw-r--r--src/rhctl/audio_switch_command.go66
2 files changed, 74 insertions, 17 deletions
diff --git a/src/rhctl/audio_switch.go b/src/rhctl/audio_switch.go
index 07e0270..f3821d8 100644
--- a/src/rhctl/audio_switch.go
+++ b/src/rhctl/audio_switch.go
@@ -80,7 +80,13 @@ type SwitchUpdate struct {
}
type SwitchState struct {
- // TODO: fill this up with data
+ audio [SwitchOutputNumMax]struct {
+ inputs [SwitchInputNumMax]bool
+ silence bool
+ }
+ gpi [SwitchGPINumMax]bool
+ relay [SwitchRelayNumMax]bool
+ oc [SwitchOCNumMax]bool
}
type AudioSwitch struct {
@@ -152,8 +158,7 @@ func (sw *AudioSwitch) Run() {
case <-stop:
return
case <-sw.timer.C:
- resp := SwitchResponse{SwitchError, "EEE timeout"}
- sw.current.Response <- resp
+ sw.current.Response <- fmt.Errorf("command timed out")
sw.current = nil
sw.timer = nil
case data := <-sw.port.rx:
@@ -164,11 +169,15 @@ func (sw *AudioSwitch) Run() {
case <-stop:
return
case cmd := <-sw.Commands:
- c := cmd.Cmd.Generate(append(cmd.Args, sw.unit)...)
- rhdl.Printf("sending '%s' to switch", c)
- sw.current = cmd
- sw.port.tx <- c
- sw.timer = time.NewTimer(sw.timeout)
+ c, err := cmd.Cmd.Generate(append(cmd.Args, sw.unit)...)
+ if err != nil {
+ cmd.Response <- err
+ } else {
+ rhdl.Printf("sending '%s' to switch", c)
+ sw.current = cmd
+ sw.port.tx <- c
+ sw.timer = time.NewTimer(sw.timeout)
+ }
case data := <-sw.port.rx:
sw.handleData(data)
}
diff --git a/src/rhctl/audio_switch_command.go b/src/rhctl/audio_switch_command.go
index 17be8d3..712eee0 100644
--- a/src/rhctl/audio_switch_command.go
+++ b/src/rhctl/audio_switch_command.go
@@ -27,6 +27,15 @@ import (
"strings"
)
+const (
+ SwitchUnitIDMax SwitchUnitID = 3
+ SwitchInputNumMax SwitchInputNum = 8
+ SwitchOutputNumMax SwitchOutputNum = 2
+ SwitchGPINumMax SwitchGPINum = 16
+ SwitchRelayNumMax SwitchRelayNum = 8
+ SwitchOCNumMax SwitchOCNum = 8
+)
+
type SwitchUnitID uint
func (u SwitchUnitID) String() string {
@@ -38,7 +47,7 @@ func (u *SwitchUnitID) FromString(str string) error {
if err != nil {
return err
}
- if vuint > 3 {
+ if vuint > uint64(SwitchUnitIDMax) {
return fmt.Errorf("switch unit ID is out of range")
}
*u = SwitchUnitID(vuint)
@@ -60,7 +69,7 @@ func (i *SwitchInputNum) FromString(str string) error {
if err != nil {
return err
}
- if vuint < 1 || vuint > 8 {
+ if vuint < 1 || vuint > uint64(SwitchInputNumMax) {
return fmt.Errorf("switch input number is out of range")
}
*i = SwitchInputNum(vuint)
@@ -78,13 +87,31 @@ func (o *SwitchOutputNum) FromString(str string) error {
if err != nil {
return err
}
- if vuint < 1 || vuint > 2 {
+ if vuint < 1 || vuint > uint64(SwitchOutputNumMax) {
return fmt.Errorf("switch output number is out of range")
}
*o = SwitchOutputNum(vuint)
return nil
}
+type SwitchGPINum uint
+
+func (g SwitchGPINum) String() string {
+ return strconv.FormatUint(uint64(g), 10)
+}
+
+func (g *SwitchGPINum) FromString(str string) error {
+ vuint, err := strconv.ParseUint(str, 10, 32)
+ if err != nil {
+ return err
+ }
+ if vuint < 1 || vuint > uint64(SwitchGPINumMax) {
+ return fmt.Errorf("switch GPI number is out of range")
+ }
+ *g = SwitchGPINum(vuint)
+ return nil
+}
+
type SwitchRelayNum uint
func (r SwitchRelayNum) String() string {
@@ -96,7 +123,7 @@ func (r *SwitchRelayNum) FromString(str string) error {
if err != nil {
return err
}
- if vuint < 1 || vuint > 8 {
+ if vuint < 1 || vuint > uint64(SwitchRelayNumMax) {
return fmt.Errorf("switch relay number is out of range")
}
*r = SwitchRelayNum(vuint)
@@ -114,7 +141,7 @@ func (o *SwitchOCNum) FromString(str string) error {
if err != nil {
return err
}
- if vuint < 1 || vuint > 8 {
+ if vuint < 1 || vuint > uint64(SwitchOCNumMax) {
return fmt.Errorf("switch OC number is out of range")
}
*o = SwitchOCNum(vuint)
@@ -125,7 +152,8 @@ type SwitchCmdString string
const (
SwitchCmdStateAudio SwitchCmdString = "*uSL"
- SwitchCmdStateGPI SwitchCmdString = "*uSPA"
+ SwitchCmdStateGPI SwitchCmdString = "*uSPgg"
+ SwitchCmdStateGPIAll SwitchCmdString = "*uSPA"
SwitchCmdStateOC SwitchCmdString = "*uSO"
SwitchCmdStateRelay SwitchCmdString = "*uSR"
SwitchCmdStateSilence SwitchCmdString = "*uSS"
@@ -152,23 +180,43 @@ const (
SwitchCmdRelayPulse SwitchCmdString = "*uORrP"
)
-func (c SwitchCmdString) Generate(args ...interface{}) string {
+func (c SwitchCmdString) Generate(args ...interface{}) (string, error) {
s := string(c)
for _, arg := range args {
switch arg.(type) {
case SwitchUnitID:
+ if arg.(SwitchUnitID) > SwitchUnitIDMax {
+ return "", fmt.Errorf("switch unit ID is out of range")
+ }
s = strings.Replace(s, "u", fmt.Sprintf("%1d", arg.(SwitchUnitID)), -1)
case SwitchInputNum:
+ if arg.(SwitchInputNum) < 1 || arg.(SwitchInputNum) > SwitchInputNumMax {
+ return "", fmt.Errorf("switch input number is out of range")
+ }
s = strings.Replace(s, "ii", fmt.Sprintf("%02d", arg.(SwitchInputNum)), -1)
case SwitchOutputNum:
+ if arg.(SwitchOutputNum) < 1 || arg.(SwitchOutputNum) > SwitchOutputNumMax {
+ return "", fmt.Errorf("switch input number is out of range")
+ }
s = strings.Replace(s, "o", fmt.Sprintf("%1d", arg.(SwitchOutputNum)), -1)
+ case SwitchGPINum:
+ if arg.(SwitchGPINum) < 1 || arg.(SwitchGPINum) > SwitchGPINumMax {
+ return "", fmt.Errorf("switch GPI number is out of range")
+ }
+ s = strings.Replace(s, "gg", fmt.Sprintf("%02d", arg.(SwitchGPINum)), -1)
case SwitchRelayNum:
+ if arg.(SwitchRelayNum) < 1 || arg.(SwitchRelayNum) > SwitchRelayNumMax {
+ return "", fmt.Errorf("switch relay number is out of range")
+ }
s = strings.Replace(s, "r", fmt.Sprintf("%1d", arg.(SwitchRelayNum)), -1)
case SwitchOCNum:
+ if arg.(SwitchOCNum) < 1 || arg.(SwitchOCNum) > SwitchOCNumMax {
+ return "", fmt.Errorf("switch OC number is out of range")
+ }
s = strings.Replace(s, "c", fmt.Sprintf("%1d", arg.(SwitchOCNum)), -1)
}
}
- return s
+ return s, nil
}
type SwitchCommand struct {
@@ -186,7 +234,7 @@ func SwitchCommandParseState(args []string) (cmdstr SwitchCmdString, cmdargs []i
case "audio":
cmdstr = SwitchCmdStateAudio
case "gpi":
- cmdstr = SwitchCmdStateGPI
+ cmdstr = SwitchCmdStateGPIAll
case "oc":
cmdstr = SwitchCmdStateOC
case "relay":