This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ch9120.go
188 lines (177 loc) · 5.4 KB
/
ch9120.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package ch912x
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/json"
"io"
"net"
"strconv"
)
type CH9120 struct {
Kind Kind `json:"-"`
Version string `json:"version,omitempty"`
ModuleName string `json:"module_name,omitempty"`
ModuleMAC net.HardwareAddr `json:"module_mac,omitempty"`
ClientMAC net.HardwareAddr `json:"client_mac,omitempty"`
ModuleOptions *ModuleOptions `json:"module_options,omitempty"`
UART1 *UARTService `json:"uart_1,omitempty"`
}
func (p *CH9120) setClientMAC(addr net.HardwareAddr) {
p.ClientMAC = addr
}
func (p *CH9120) identity() (Kind, net.HardwareAddr) {
return p.Kind, p.ModuleMAC
}
func (p *CH9120) moduleIP() net.IP {
if p.ModuleOptions == nil {
return nil
}
return p.ModuleOptions.IP
}
func (p *CH9120) MarshalJSON() ([]byte, error) {
type Module CH9120
module := new(struct {
Product Product `json:"product"`
*Module
})
module.Product = ProductCH9120
module.Module = (*Module)(p)
return json.Marshal(module)
}
func (p *CH9120) UnmarshalJSON(data []byte) (err error) {
type Module CH9120
module := new(struct {
Product Product `json:"product"`
*Module
})
module.Module = (*Module)(p)
err = json.Unmarshal(data, module)
if err == nil && module.Product != ProductCH9120 {
err = ErrCH9120InvalidJSON
}
return
}
func (p *CH9120) ReadFrom(r io.Reader) (n int64, err error) {
header := new(ch9121Header)
err = binary.Read(r, binary.LittleEndian, header)
if err != nil {
return
}
p.Kind = header.Kind
if p.Kind == KindDiscoveryResponse {
discovery := new(ch9121Discovery)
err = binary.Read(r, binary.LittleEndian, discovery)
p.ModuleMAC = discovery.ModuleMAC[:]
p.ClientMAC = discovery.ClientMAC[:]
p.ModuleOptions = &ModuleOptions{IP: discovery.IP[:]}
buf := bufio.NewReader(r)
moduleName, _ := buf.ReadBytes(0)
p.ModuleName = trimNull(moduleName)
version, _ := buf.ReadByte()
p.Version = strconv.Itoa(int(version))
return
}
c := new(ch9120Configuration)
err = binary.Read(r, binary.LittleEndian, c)
if err != nil {
return
}
p.ModuleName = trimNull(c.ModuleName[:])
p.ModuleMAC = c.ModuleMAC[:]
p.ClientMAC = c.ClientMAC[:]
p.ModuleOptions = &ModuleOptions{
MAC: c.ModuleOptions.ModuleMAC[:],
IP: c.ModuleOptions.IP[:],
Mask: c.ModuleOptions.Mask[:],
Gateway: c.ModuleOptions.Gateway[:],
UseDHCP: fromVariantBool(c.ModuleOptions.DHCP),
SerialNegotiate: fromVariantBool(c.ModuleOptions.SerialNegotiate),
}
p.UART1 = &UARTService{
Mode: UARTMode(c.UART.Mode),
ClientIP: c.UART.ClientIP[:],
ClientPort: c.UART.ClientPort,
LocalPort: c.UART.TargetPort,
PacketSize: c.UART.RXSize,
PacketTimeout: c.UART.RXTimeout,
RandomClientPort: fromVariantBool(c.UART.RandomClientPort),
CloseOnLost: fromVariantBool(c.UART.CloseOnLost),
ClearOnReconnect: fromVariantBool(c.UART.ClearOnTimeout),
Baud: c.UART.Baud,
DataBits: c.UART.DataBits,
StopBit: c.UART.StopBit,
Parity: fromCH9121Parity(c.UART.Parity),
UseDomain: fromVariantBool(c.UART.UseDomain),
ClientDomain: trimNull(c.UART.ClientDomain[:]),
}
return
}
func (p *CH9120) WriteTo(w io.Writer) (n int64, err error) {
var buf bytes.Buffer
h := &ch9121Header{Kind: p.Kind}
copy(h.Header[:], magicCH9120)
_ = binary.Write(&buf, binary.LittleEndian, h)
r := new(ch9120Configuration)
copy(r.ModuleMAC[:], p.ModuleMAC)
copy(r.ClientMAC[:], p.ClientMAC)
copy(r.ModuleName[:], p.ModuleName)
if opt := p.ModuleOptions; opt != nil {
copy(r.ModuleOptions.ModuleMAC[:], opt.MAC)
copy(r.ModuleOptions.IP[:], opt.IP)
copy(r.ModuleOptions.Mask[:], opt.Mask)
copy(r.ModuleOptions.Gateway[:], opt.Gateway)
r.ModuleOptions.DHCP = toVariantBool(opt.UseDHCP)
r.ModuleOptions.SerialNegotiate = toVariantBool(opt.SerialNegotiate)
}
if p.UART1 != nil {
r.UART = ch9120UART{
Mode: byte(p.UART1.Mode),
RandomClientPort: toVariantBool(p.UART1.RandomClientPort),
ClientPort: p.UART1.ClientPort,
TargetPort: p.UART1.LocalPort,
Baud: p.UART1.Baud,
DataBits: p.UART1.DataBits,
StopBit: p.UART1.StopBit,
Parity: toCH9121Parity(p.UART1.Parity),
CloseOnLost: toVariantBool(p.UART1.CloseOnLost),
RXSize: p.UART1.PacketSize,
RXTimeout: p.UART1.PacketTimeout,
ClearOnTimeout: toVariantBool(p.UART1.ClearOnReconnect),
}
copy(r.UART.ClientIP[:], p.UART1.ClientIP)
r.UART.UseDomain = toVariantBool(p.UART1.UseDomain)
copy(r.UART.ClientDomain[:], p.UART1.ClientDomain)
}
_ = binary.Write(&buf, binary.LittleEndian, r)
_, _ = buf.Write(make([]byte, 285-buf.Len()))
return buf.WriteTo(w)
}
type ch9120Configuration struct {
ModuleMAC [6]byte
ClientMAC [6]byte
_ [6]byte
ModuleName [21]byte
ModuleOptions ch9121ModuleOptions
_ [75]byte
UART ch9120UART
}
type ch9120UART struct {
Mode byte
RandomClientPort byte
ClientPort uint16
ClientIP [4]byte
TargetPort uint16
Baud uint32
DataBits byte
StopBit byte
Parity byte // None, Evan, Odd, Mark, Space
CloseOnLost byte
RXSize uint16
_ [2]byte
RXTimeout uint16
_ [3]byte
ClearOnTimeout byte
UseDomain byte
ClientDomain [20]byte
}