Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align message type usage (BC) #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/e3dc/e3dc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func run() ([]byte, error) {
if conf.splitrequests {
rs = make([]rscp.Message, len(ms))
for i := range ms {
var r *rscp.Message
if r, err = c.Send(ms[i]); err != nil {
r, err := c.Send(ms[i])
if err != nil {
return nil, err
}
rs[i] = *r
rs[i] = r
}
} else if rs, err = c.SendMultiple(ms); err != nil {
return nil, err
Expand Down
18 changes: 9 additions & 9 deletions cmd/example/e3dc.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func run() int {
DataType: rscp.None,
Value: nil,
}
var response *rscp.Message
if response, err = c.Send(request); err != nil {
response, err := c.Send(request)
if err != nil {
fmt.Fprintf(os.Stderr, "Send error: %s\n", err)
return 1
}
Expand All @@ -67,7 +67,7 @@ func run() int {
// DataType: rscp.CString,
// Value: "00:00:00:00:00:00",
// }
fmt.Printf("%#v\n", *response)
fmt.Printf("%#v\n", response)
}
{
//
Expand All @@ -78,8 +78,8 @@ func run() int {
DataType: rscp.None,
Value: nil,
}
var response *rscp.Message
if response, err = c.Send(request); err != nil {
response, err := c.Send(request)
if err != nil {
fmt.Fprintf(os.Stderr, "Send error: %s\n", err)
return 1
}
Expand All @@ -106,7 +106,7 @@ func run() int {
// },
// },
// }
fmt.Printf("%#v\n", *response)
fmt.Printf("%#v\n", response)
}
{
//
Expand All @@ -128,8 +128,8 @@ func run() int {
},
},
}
var response *rscp.Message
if response, err = c.Send(request); err != nil {
response, err := c.Send(request)
if err != nil {
fmt.Fprintf(os.Stderr, "Send error: %s\n", err)
return 1
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func run() int {
// },
// },
// }
fmt.Printf("%#v\n", *response)
fmt.Printf("%#v\n", response)
}
{
//
Expand Down
18 changes: 7 additions & 11 deletions rscp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package rscp

import (
"crypto/cipher"
"errors"
"fmt"
"io"
"math"
"net"
"time"

"errors"

"github.com/azihsoyn/rijndael256"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -132,7 +131,7 @@ func (c *Client) authenticate() error {
Log.SetLevel(orgLogLevel)
}
return err
} else if err := c.send([]Message{*msg}); err != nil {
} else if err := c.send([]Message{msg}); err != nil {
if orgLogLevel < RequiredAuthLogLevel {
Log.SetLevel(orgLogLevel)
}
Expand Down Expand Up @@ -193,15 +192,12 @@ func (c *Client) Disconnect() error {
// Send a message and return the response.
//
// connects and authenticates the first time used.
func (c *Client) Send(request Message) (*Message, error) {
var (
responses []Message
err error
)
if responses, err = (c.SendMultiple([]Message{request})); err != nil {
return nil, err
func (c *Client) Send(request Message) (Message, error) {
responses, err := c.SendMultiple([]Message{request})
if err != nil {
return Message{}, err
}
return &responses[0], nil
return responses[0], nil
}

// Send multiple messages in one round-trip and return the response.
Expand Down
4 changes: 2 additions & 2 deletions rscp/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type Message struct {
}

// NewMessage creates a new message (infer the data type from the tag).
func NewMessage(tag Tag, value interface{}) *Message {
return &Message{Tag: tag, DataType: tag.DataType(), Value: value}
func NewMessage(tag Tag, value interface{}) Message {
return Message{Tag: tag, DataType: tag.DataType(), Value: value}
}

const secretReplaceString = "********"
Expand Down
23 changes: 10 additions & 13 deletions rscp/read_request_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import (
)

// readRequestSlice reads a single request message (recursive for containers) from a slice.
func readRequestSlice(values []interface{}) (*Message, error) {
func readRequestSlice(values []interface{}) (Message, error) {
sr := slicereader.NewSliceReader(values)
return readRequestSliceReader(sr)
}

// readRequestSliceReader reads a single request message (recursive for containers) from the SliceReader.
func readRequestSliceReader(sr *slicereader.SliceReader) (*Message, error) {
func readRequestSliceReader(sr *slicereader.SliceReader) (Message, error) {
var (
t interface{}
tag Tag
isTag bool
err error
)
if t, err = sr.Read(); err != nil {
return nil, err
return Message{}, err
}
if tag, isTag = t.(Tag); !isTag {
return nil, fmt.Errorf("element at index %d: %w", int(sr.Size()-int64(sr.Len())-1), ErrValidTag)
return Message{}, fmt.Errorf("element at index %d: %w", int(sr.Size()-int64(sr.Len())-1), ErrValidTag)
}
msg := NewMessage(tag, nil)
if msg.DataType == None {
Expand All @@ -33,22 +33,19 @@ func readRequestSliceReader(sr *slicereader.SliceReader) (*Message, error) {
if msg.DataType == Container {
msg.Value = make([]Message, 0)
for sr.Len() > 0 {
var (
subMsg *Message
err error
)
if subMsg, err = readRequestSliceReader(sr); err != nil {
return nil, err
subMsg, err := readRequestSliceReader(sr)
if err != nil {
return Message{}, err
}
msg.Value = append(msg.Value.([]Message), *subMsg)
msg.Value = append(msg.Value.([]Message), subMsg)
}
} else {
if msg.Value, err = sr.Read(); err != nil {
return nil, fmt.Errorf("expect value after tag %s with data type %s: %w", msg.Tag, msg.DataType, ErrMissingValue)
return Message{}, fmt.Errorf("expect value after tag %s with data type %s: %w", msg.Tag, msg.DataType, ErrMissingValue)
}
switch msg.Value.(type) {
case Tag, DataType:
return nil, fmt.Errorf("expect element at index %d to be a value for tag %s, got type: %T: %w",
return Message{}, fmt.Errorf("expect element at index %d to be a value for tag %s, got type: %T: %w",
int(sr.Size()-int64(sr.Len())-1), msg.Tag, msg.Value, ErrDataTypeValueMismatch)
}
}
Expand Down
73 changes: 44 additions & 29 deletions rscp/read_request_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,80 +9,95 @@ import (
)

func Test_readRequestSlice(t *testing.T) {
var zero Message
tests := []struct {
name string
args []interface{}
want *Message
want Message
wantErr error
}{
{"nil",
nil,
{
"nil",
nil,
zero,
slicereader.EOS,
},
{"empty slice",
{
"empty slice",
[]interface{}{},
nil,
zero,
slicereader.EOS,
},
{"not a tag",
{
"not a tag",
[]interface{}{"not a tag"},
nil,
zero,
ErrValidTag,
},
{"simple tag",
{
"simple tag",
[]interface{}{INFO_REQ_UTC_TIME},
&Message{INFO_REQ_UTC_TIME, INFO_REQ_UTC_TIME.DataType(), nil},
Message{INFO_REQ_UTC_TIME, INFO_REQ_UTC_TIME.DataType(), nil},
nil,
},
{"simple tag with additional message ignored",
{
"simple tag with additional message ignored",
[]interface{}{INFO_REQ_UTC_TIME, INFO_REQ_UTC_TIME},
&Message{INFO_REQ_UTC_TIME, INFO_REQ_UTC_TIME.DataType(), nil},
Message{INFO_REQ_UTC_TIME, INFO_REQ_UTC_TIME.DataType(), nil},
nil,
},
{"tag with value",
{
"tag with value",
[]interface{}{RSCP_REQ_SET_ENCRYPTION_PASSPHRASE, "test"},
&Message{RSCP_REQ_SET_ENCRYPTION_PASSPHRASE, RSCP_REQ_SET_ENCRYPTION_PASSPHRASE.DataType(), "test"},
Message{RSCP_REQ_SET_ENCRYPTION_PASSPHRASE, RSCP_REQ_SET_ENCRYPTION_PASSPHRASE.DataType(), "test"},
nil,
},
{"tag with tag as value",
{
"tag with tag as value",
[]interface{}{RSCP_REQ_SET_ENCRYPTION_PASSPHRASE, INFO_REQ_UTC_TIME},
nil,
zero,
ErrDataTypeValueMismatch,
},
{"tag with data type as value",
{
"tag with data type as value",
[]interface{}{RSCP_REQ_SET_ENCRYPTION_PASSPHRASE, Bool},
nil,
zero,
ErrDataTypeValueMismatch,
},
{"tag with value and additional container ignored",
{
"tag with value and additional container ignored",
[]interface{}{INFO_REQ_UTC_TIME, RSCP_REQ_AUTHENTICATION, RSCP_AUTHENTICATION_USER, "user", RSCP_AUTHENTICATION_PASSWORD, "password"},
&Message{INFO_REQ_UTC_TIME, INFO_REQ_UTC_TIME.DataType(), nil},
Message{INFO_REQ_UTC_TIME, INFO_REQ_UTC_TIME.DataType(), nil},
nil,
},
{"tag with missing value",
{
"tag with missing value",
[]interface{}{RSCP_REQ_SET_ENCRYPTION_PASSPHRASE},
nil,
zero,
ErrMissingValue,
},
{"empty container tag",
{
"empty container tag",
[]interface{}{RSCP_REQ_AUTHENTICATION},
&Message{RSCP_REQ_AUTHENTICATION, RSCP_REQ_AUTHENTICATION.DataType(), []Message{}},
Message{RSCP_REQ_AUTHENTICATION, RSCP_REQ_AUTHENTICATION.DataType(), []Message{}},
nil,
},
{"container tag with values",
{
"container tag with values",
[]interface{}{RSCP_REQ_AUTHENTICATION, RSCP_AUTHENTICATION_USER, "user", RSCP_AUTHENTICATION_PASSWORD, "password"},
&Message{RSCP_REQ_AUTHENTICATION, RSCP_REQ_AUTHENTICATION.DataType(), []Message{{RSCP_AUTHENTICATION_USER, RSCP_AUTHENTICATION_USER.DataType(), "user"}, {RSCP_AUTHENTICATION_PASSWORD, RSCP_AUTHENTICATION_PASSWORD.DataType(), "password"}}},
Message{RSCP_REQ_AUTHENTICATION, RSCP_REQ_AUTHENTICATION.DataType(), []Message{{RSCP_AUTHENTICATION_USER, RSCP_AUTHENTICATION_USER.DataType(), "user"}, {RSCP_AUTHENTICATION_PASSWORD, RSCP_AUTHENTICATION_PASSWORD.DataType(), "password"}}},
nil,
},
{"unsupported case: container tag with values with additional message ignored",
{
"unsupported case: container tag with values with additional message ignored",
[]interface{}{RSCP_REQ_AUTHENTICATION, RSCP_AUTHENTICATION_USER, "user", RSCP_AUTHENTICATION_PASSWORD, "password", INFO_REQ_UTC_TIME},
&Message{RSCP_REQ_AUTHENTICATION, RSCP_REQ_AUTHENTICATION.DataType(), []Message{{RSCP_AUTHENTICATION_USER, RSCP_AUTHENTICATION_USER.DataType(), "user"}, {RSCP_AUTHENTICATION_PASSWORD, RSCP_AUTHENTICATION_PASSWORD.DataType(), "password"}, {INFO_REQ_UTC_TIME, INFO_REQ_UTC_TIME.DataType(), nil}}},
Message{RSCP_REQ_AUTHENTICATION, RSCP_REQ_AUTHENTICATION.DataType(), []Message{{RSCP_AUTHENTICATION_USER, RSCP_AUTHENTICATION_USER.DataType(), "user"}, {RSCP_AUTHENTICATION_PASSWORD, RSCP_AUTHENTICATION_PASSWORD.DataType(), "password"}, {INFO_REQ_UTC_TIME, INFO_REQ_UTC_TIME.DataType(), nil}}},
nil,
},
{"container tag with missing value",
{
"container tag with missing value",
[]interface{}{RSCP_REQ_AUTHENTICATION, RSCP_AUTHENTICATION_USER, RSCP_AUTHENTICATION_PASSWORD, "password"},
nil,
zero,
ErrDataTypeValueMismatch,
},
}
Expand Down
16 changes: 5 additions & 11 deletions rscp/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ func validateRequests(messages []Message) error {
// CreateRequest(INFO_REQ_UTC_TIME)
// CreateRequest(EMS_REQ_SET_ERROR_BUZZER_ENABLED, true)
// CreateRequest(BAT_REQ_DATA, BAT_INDEX, uint16(0), BAT_REQ_DEVICE_STATE, BAT_REQ_RSOC, BAT_REQ_STATUS_CODE)
func CreateRequest(values ...interface{}) (msg *Message, err error) {
if msg, err = readRequestSlice(values); err != nil {
return nil, err
}
return msg, nil
func CreateRequest(values ...interface{}) (msg Message, err error) {
return readRequestSlice(values)
}

// CreateRequests creates multiple new requests (infer the data type from the tag)
Expand All @@ -52,14 +49,11 @@ func CreateRequests(values ...[]interface{}) ([]Message, error) {
}
msgs := make([]Message, 0)
for _, subValues := range values {
var (
msg *Message
err error
)
if msg, err = CreateRequest(subValues...); err != nil {
msg, err := CreateRequest(subValues...)
if err != nil {
return nil, err
}
msgs = append(msgs, *msg)
msgs = append(msgs, msg)
}
return msgs, nil
}