@@ -8,6 +8,9 @@ import "os"
88import "log"
99import "time"
1010import "encoding/hex"
11+ import "encoding/binary"
12+ import "strconv"
13+ import "bytes"
1114
1215import "github.com/jessevdk/go-flags"
1316import "github.com/proactivity-lab/go-loggers"
@@ -33,22 +36,51 @@ func (self HexString) MarshalFlag() (string, error) {
3336 return hex .EncodeToString (self ), nil
3437}
3538
36- func main () {
39+ func parseValue (opts Options ) ([]byte , error ) {
40+ buf := new (bytes.Buffer )
41+ if opts .Uint8 {
42+ if v , err := strconv .ParseUint (opts .Value , 10 , 8 ); err != nil {
43+ return nil , err
44+ } else if err = binary .Write (buf , binary .BigEndian , uint8 (v )); err != nil {
45+ return nil , err
46+ }
47+ return buf .Bytes (), nil
48+ } else if opts .Uint16 {
49+ if v , err := strconv .ParseUint (opts .Value , 10 , 16 ); err != nil {
50+ return nil , err
51+ } else if err = binary .Write (buf , binary .BigEndian , uint16 (v )); err != nil {
52+ return nil , err
53+ }
54+ } else if opts .Uint32 {
55+ if v , err := strconv .ParseUint (opts .Value , 10 , 32 ); err != nil {
56+ return nil , err
57+ } else if err = binary .Write (buf , binary .BigEndian , uint32 (v )); err != nil {
58+ return nil , err
59+ }
60+ }
61+ return hex .DecodeString (opts .Value )
62+ }
3763
38- var opts struct {
39- Positional struct {
40- ConnectionString string `description:"Connectionstring sf@HOST:PORT"`
41- } `positional-args:"yes"`
64+ type Options struct {
65+ Positional struct {
66+ ConnectionString string `description:"Connectionstring sf@HOST:PORT"`
67+ } `positional-args:"yes"`
4268
43- Group sfconnection.AMGroup `short:"g" long:"group" default:"22" description:"Packet AM Group (hex)"`
69+ Group sfconnection.AMGroup `short:"g" long:"group" default:"22" description:"Packet AM Group (hex)"`
4470
45- Parameter string `short:"p" long:"parameter" description:"Name of the parameter"`
46- Value HexString `short:"v" long:"value" description:"Value to set"`
71+ Parameter string `short:"p" long:"parameter" description:"Name of the parameter"`
72+ Value string `short:"v" long:"value" description:"Value to set"`
73+ Uint8 bool `long:"uint8" description:"Value is uint8"`
74+ Uint16 bool `long:"uint16" description:"Value is uint16"`
75+ Uint32 bool `long:"uint32" description:"Value is uint32"`
4776
48- Debug []bool `short:"D" long:"debug" description:"Debug mode, print raw packets"`
49- ShowVersion func () `short:"V" long:"version" description:"Show application version"`
50- }
77+ Debug []bool `short:"D" long:"debug" description:"Debug mode, print raw packets"`
78+ ShowVersion func () `short:"V" long:"version" description:"Show application version"`
79+ }
80+
81+ func main () {
5182
83+ var opts Options
5284 opts .ShowVersion = func () {
5385 if ApplicationBuildDate == "" {
5486 ApplicationBuildDate = "YYYY-mm-dd_HH:MM:SS"
@@ -91,18 +123,22 @@ func main() {
91123
92124 if len (opts .Parameter ) > 0 {
93125 if len (opts .Value ) > 0 {
94- logger .Info .Printf ("Set %s to %X\n " , opts .Parameter , opts .Value )
95- err := dpm .SetValue (opts .Parameter , opts .Value )
126+ value , err := parseValue (opts )
96127 if err == nil {
97- logger .Info .Printf ("Done\n " )
128+ logger .Info .Printf ("Set %s to 0x%X\n " , opts .Parameter , value )
129+ if val , err := dpm .SetValue (opts .Parameter , value ); err == nil {
130+ logger .Info .Printf ("%s = %s\n " , val .Name , val )
131+ } else {
132+ logger .Info .Printf ("Failed: %s\n " , err )
133+ }
98134 } else {
99- logger .Info .Printf ("Failed: %s \n " , err )
135+ logger .Error .Printf ("%s " , err )
100136 }
101137 } else {
102138 logger .Info .Printf ("Get %s\n " , opts .Parameter )
103139 val , err := dpm .GetValue (opts .Parameter )
104140 if err == nil {
105- logger .Info .Printf ("%s = %X \n " , opts . Parameter , val )
141+ logger .Info .Printf ("%s = %s \n " , val . Name , val )
106142 } else {
107143 logger .Info .Printf ("Failed: %s\n " , err )
108144 }
0 commit comments