Skip to content

Commit

Permalink
Merge pull request #2 from bgentry/fix-escaping
Browse files Browse the repository at this point in the history
Fix API escaping, simplify escaping code
  • Loading branch information
pauleyj authored Feb 21, 2017
2 parents e98ecc2 + 6151059 commit 48ad5f0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
25 changes: 9 additions & 16 deletions api/tx/api_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tx

import (
"bytes"

"github.com/pauleyj/gobee/api"
)

Expand All @@ -19,26 +20,26 @@ func (f *APIFrame) Bytes(frame Frame) ([]byte, error) {

var b bytes.Buffer
b.WriteByte(api.FrameDelimiter)
b.Write(f.encode(uint16ToBytes(uint16(len(p)))...))
b.Write(f.encode(p...))
b.Write(f.encode(uint16ToBytes(uint16(len(p)))))
b.Write(f.encode(p))
c := checksum(p)
b.Write(f.encode(c))
b.Write(f.encode([]byte{c}))

return b.Bytes(), nil
}

func (f *APIFrame) encode(p ...byte) []byte {
func (f *APIFrame) encode(p []byte) []byte {
if f.Mode == api.EscapeModeInactive {
return slicify(p...)
return p
}
return escape(p...)
return escape(p)
}

func escape(p ...byte) []byte {
func escape(p []byte) []byte {
var b bytes.Buffer
for _, c := range p {
if api.ShouldEscape(c) {
b.WriteByte(api.ESCChar)
b.WriteByte(api.ESC)
b.WriteByte(api.Escape(c))
} else {
b.WriteByte(c)
Expand All @@ -47,14 +48,6 @@ func escape(p ...byte) []byte {
return b.Bytes()
}

func slicify(p ...byte) []byte {
var b bytes.Buffer
for _, c := range p {
b.WriteByte(c)
}
return b.Bytes()
}

func checksum(p []byte) byte {
var chksum byte
for _, c := range p {
Expand Down
43 changes: 42 additions & 1 deletion api/tx/tx_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package tx

import (
"github.com/pauleyj/gobee/api"
"testing"

"github.com/pauleyj/gobee/api"
)

func addressOf(b byte) *byte { return &b }
Expand Down Expand Up @@ -47,6 +48,46 @@ func Test_API_Frame_WithEscape(t *testing.T) {
}
}

type dummyFrame struct {
data []byte
}

func (f *dummyFrame) Bytes() ([]byte, error) {
return f.data, nil
}

func Test_API_Frame_ZDO_WithEscape(t *testing.T) {
input := []byte{
zbExplicitAPIID, // frame type
0x01, // frame ID
0x00, 0x13, 0xA2, 0x00, 0x41, 0x53, 0x1D, 0x4F, // dst 64-bit
0x00, 0x00, // dst 16-bit
0x00, 0x00, // src + dst endpoints
0x00, 0x31, // cluster ID
0x00, 0x00, // profile ID
0x00, // broadcastRadius
0x00, // options
0x10, 0x00, // payload
}

fakeFrame := &dummyFrame{input}
expected := []byte{
0x7E, 0x00, 0x18, 0x7D, 0x31, 0x01, 0x00, 0x7D, 0x33, 0xA2, 0x00, 0x41,
0x53, 0x1D, 0x4F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x06,
}

api := &APIFrame{Mode: api.EscapeModeActive}
result, err := api.Bytes(fakeFrame)
if err != nil {
t.Fatalf("Expected no error, but got %v", err)
}
if len(expected) != len(result) {
t.Logf("\nexpected=% #v\n\n result=% #v\n", expected, result)
t.Fatalf("expected len=%d, got len=%d", len(expected), len(result))
}
}

func Test_Valid_AT_No_Param(t *testing.T) {
at := NewATBuilder().
ID(0x01).
Expand Down

0 comments on commit 48ad5f0

Please sign in to comment.