-
Notifications
You must be signed in to change notification settings - Fork 2
/
type_string.go
108 lines (92 loc) · 3.23 KB
/
type_string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package zcl
import "encoding/binary"
func stringMarshalZcl(ln, b string) ([]byte, error) {
var buf []byte
if ln == "o1" {
bln := len(b)
if bln > 255 {
return nil, ErrTooMuchData
}
buf = append(buf, uint8(bln))
} else if ln == "o2" {
bln := len(b)
if bln > 65535 {
return nil, ErrTooMuchData
}
buf = make([]byte, 2)
binary.LittleEndian.PutUint16(buf, uint16(bln))
} else {
return nil, ErrNotImpl
}
return append(buf, []byte(b)...), nil
}
func stringUnmarshalZcl(ln string, b []byte) ([]byte, []byte, error) {
var bln uint16
if ln == "o1" {
if len(b) < 1 {
return nil, nil, ErrNotEnoughData
}
bln = uint16(b[0])
b = b[1:]
} else if ln == "o2" {
if len(b) < 2 {
return nil, nil, ErrNotEnoughData
}
bln = binary.LittleEndian.Uint16(b[0:2])
b = b[2:]
} else {
return nil, nil, ErrNotImpl
}
if len(b) < int(bln) {
return nil, nil, ErrNotEnoughData
}
return b[0:bln], b[bln:], nil
}
// Zostring is Octet string (0xff = invalid). A/D = D
type Zostring []byte
func (o *Zostring) UnmarshalZcl(buf []byte) ([]byte, error) {
val, buf, err := stringUnmarshalZcl("o1", buf)
*o = Zostring(val)
return buf, err
}
func (o Zostring) MarshalZcl() ([]byte, error) { return stringMarshalZcl("o1", string(o)) }
func (o *Zostring) Values() []Val { return []Val{o} }
func (o Zostring) TypeID() TypeID { return 65 }
func (o Zostring) String() string { return string(o) }
//func (o Zostring) Valid() bool { return o != Zostring(255) }
// Zcstring is Character string (0xff = invalid). A/D = D
type Zcstring string
func (c *Zcstring) UnmarshalZcl(buf []byte) ([]byte, error) {
val, buf, err := stringUnmarshalZcl("o1", buf)
*c = Zcstring(val)
return buf, err
}
func (c Zcstring) MarshalZcl() ([]byte, error) { return stringMarshalZcl("o1", string(c)) }
func (c *Zcstring) Values() []Val { return []Val{c} }
func (c Zcstring) TypeID() TypeID { return 66 }
func (c *Zcstring) String() string { return string(*c) }
//func (c Zcstring) Valid() bool { return c != Zcstring(255) }
// Zlostring is Long octet string (0xffff = invalid). A/D = D
type Zlostring string
func (l *Zlostring) UnmarshalZcl(buf []byte) ([]byte, error) {
val, buf, err := stringUnmarshalZcl("o2", buf)
*l = Zlostring(val)
return buf, err
}
func (l Zlostring) MarshalZcl() ([]byte, error) { return stringMarshalZcl("o2", string(l)) }
func (l *Zlostring) Values() []Val { return []Val{l} }
func (l Zlostring) TypeID() TypeID { return 67 }
func (l Zlostring) String() string { return string(l) }
//func (l Zlostring) Valid() bool { return l != Zlostring(65535) }
// Zlcstring is Long character string (0xffff = invalid). A/D = D
type Zlcstring string
func (l *Zlcstring) UnmarshalZcl(buf []byte) ([]byte, error) {
val, buf, err := stringUnmarshalZcl("o2", buf)
*l = Zlcstring(val)
return buf, err
}
func (l Zlcstring) MarshalZcl() ([]byte, error) { return stringMarshalZcl("o2", string(l)) }
func (l *Zlcstring) Values() []Val { return []Val{l} }
func (l Zlcstring) TypeID() TypeID { return 68 }
func (l Zlcstring) String() string { return string(l) }
//func (l Zlcstring) Valid() bool { return l != Zlcstring(65535) }