-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgoplc.go
348 lines (297 loc) · 9.55 KB
/
goplc.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package goplc
import (
"bytes"
"errors"
"fmt"
"github.com/MiguelValentine/goplc/ethernetip"
"github.com/MiguelValentine/goplc/ethernetip/commonIndustrialProtocol"
"github.com/MiguelValentine/goplc/ethernetip/commonIndustrialProtocol/segment"
"github.com/MiguelValentine/goplc/ethernetip/commonIndustrialProtocol/segment/epath"
_type "github.com/MiguelValentine/goplc/ethernetip/type"
"github.com/MiguelValentine/goplc/lib"
"github.com/MiguelValentine/goplc/tag"
"github.com/MiguelValentine/goplc/tagGroup"
"io"
"log"
"math"
"math/rand"
"net"
"time"
)
type controller struct {
VendorID _type.UINT
DeviceType _type.UINT
ProductCode _type.UINT
Major _type.USINT
Minor _type.USINT
Status _type.UINT
SerialNumber _type.UDINT
Version string
Name string
}
type plc struct {
tcpAddr *net.TCPAddr
tcpConn *net.TCPConn
config *Config
session _type.UDINT
Controller *controller
writeRoute bool
sender chan []byte
bufferData []byte
TargetPath []byte
HandleMap map[ethernetip.Command]func(*ethernetip.Encapsulation)
OnConnected func()
ContextPool map[_type.ULINT]func(*commonIndustrialProtocol.MessageRouterResponse)
}
func (p *plc) Connect() error {
p.config.Println("PLC Connecting...")
_conn, err := net.DialTCP("tcp", nil, p.tcpAddr)
if err != nil {
return err
}
err2 := _conn.SetKeepAlive(true)
if err2 != nil {
return err2
}
p.tcpConn = _conn
p.connected()
return nil
}
func (p *plc) connected() {
p.config.Println("PLC Connected!")
p.bufferData = []byte{}
if !p.writeRoute {
go p.write()
}
go p.read()
p.config.Println("Register Session...")
encapsulation := ethernetip.RequestRegisterSession(0)
p.sender <- encapsulation.Buffer()
}
func (p *plc) disconnected(err error) {
if err == io.EOF {
p.config.Println("PLC Disconnected!")
p.config.Println("EOF")
} else {
p.config.Println("PLC Disconnected!")
p.config.Println(err)
}
_ = p.tcpConn.Close()
p.tcpConn = nil
if p.config.ReconnectionInterval != 0 {
time.Sleep(p.config.ReconnectionInterval)
p.config.Println("PLC Reconnecting!")
err := p.Connect()
if err != nil {
panic(err)
}
}
}
func (p *plc) read() {
defer func() {
if err := recover(); err != nil {
go p.disconnected(err.(error))
}
}()
buf := make([]byte, 1024*64)
var err error
for {
var length int
length, err = p.tcpConn.Read(buf)
if err != nil {
break
}
p.config.Printf("Receive => %d bytes\n", length)
p.bufferData = append(p.bufferData, buf[0:length]...)
if len(p.bufferData) > 24 {
read, encapsulations := ethernetip.Slice(p.bufferData)
p.bufferData = p.bufferData[read:]
for _, encapsulation := range encapsulations {
if encapsulation.Status == ethernetip.StatusSuccess {
if exec, ok := p.HandleMap[encapsulation.Command]; ok {
exec(encapsulation)
} else {
p.config.Printf("Received encapsulation Command: %#x ,but no registered handler!\n", encapsulation.Command)
}
}
}
}
}
}
func (p *plc) handleRegisterSession(encapsulation *ethernetip.Encapsulation) {
p.session = encapsulation.SessionHandle
p.config.Printf("Session => %#x\n", p.session)
// get_attribute_all
mr1 := &commonIndustrialProtocol.MessageRouterRequest{}
mr1.Service = 0x01
mr1.RequestPath = segment.Paths(
epath.LogicalBuild(epath.LogicalTypeClassID, 01, true),
epath.LogicalBuild(epath.LogicalTypeInstanceID, 01, true),
)
p.UcmmSend(3, 250, math.MaxUint64, mr1)
p.ContextPool[math.MaxUint64] = p.getAttributeAll
}
func (p *plc) getAttributeAll(mr *commonIndustrialProtocol.MessageRouterResponse) {
p.config.Printf("%+v\n", mr)
dataReader := bytes.NewReader(mr.ResponseData)
lib.ReadByte(dataReader, &p.Controller.VendorID)
lib.ReadByte(dataReader, &p.Controller.DeviceType)
lib.ReadByte(dataReader, &p.Controller.ProductCode)
lib.ReadByte(dataReader, &p.Controller.Major)
lib.ReadByte(dataReader, &p.Controller.Minor)
lib.ReadByte(dataReader, &p.Controller.Status)
lib.ReadByte(dataReader, &p.Controller.SerialNumber)
nameLen := uint8(0)
lib.ReadByte(dataReader, &nameLen)
nameBuf := make([]byte, nameLen)
lib.ReadByte(dataReader, nameBuf)
p.Controller.Name = string(nameBuf)
p.Controller.Version = fmt.Sprintf("%d.%d", p.Controller.Major, p.Controller.Minor)
if p.OnConnected != nil {
p.OnConnected()
}
}
func (p *plc) UcmmSend(timeTicks _type.USINT, timeoutTicks _type.USINT, context _type.ULINT, mr1 *commonIndustrialProtocol.MessageRouterRequest) {
ucmm := &commonIndustrialProtocol.UnconnectedSend{}
ucmm.TimeTick = timeTicks
ucmm.TimeOutTicks = timeoutTicks
ucmm.MessageRequest = mr1
ucmm.RouterPath = p.TargetPath
mr2 := &commonIndustrialProtocol.MessageRouterRequest{}
mr2.Service = 0x52
mr2.RequestPath = segment.Paths(
epath.LogicalBuild(epath.LogicalTypeClassID, 06, true),
epath.LogicalBuild(epath.LogicalTypeInstanceID, 01, true),
)
mr2.RequestData = ucmm.Buffer()
cpf := ðernetip.CommonPacketFormat{}
cpf.UnconnectedData(mr2.Buffer())
pkg := ethernetip.RequestSendRRData(p.session, context, 10, cpf)
p.sender <- pkg.Buffer()
}
func (p *plc) handleSendData(encapsulation *ethernetip.Encapsulation) {
cpf := ethernetip.SendRRDataParser(encapsulation.Data)
mr := commonIndustrialProtocol.MRParser(cpf.DataItem.Data)
if mr.GeneralStatus != 0 {
panic(errors.New(fmt.Sprintf("target error => Service Code: %#x | Status: %#x | Addtional: %s", mr.ReplyService, mr.GeneralStatus, mr.AdditionalStatus)))
} else {
p.ContextPool[encapsulation.SenderContext](mr)
delete(p.ContextPool, encapsulation.SenderContext)
}
}
func (p *plc) write() {
p.writeRoute = true
for {
select {
case data := <-p.sender:
p.config.Printf("Send => %d bytes\n", len(data))
_, _ = p.tcpConn.Write(data)
}
}
}
func (p *plc) ReadTag(tag *tag.Tag) *tag.Tag {
rand.Seed(time.Now().UnixNano())
context := _type.ULINT(rand.Uint64())
p.ContextPool[context] = tag.ReadTagParser
p.UcmmSend(3, 250, context, tag.GenerateReadMessageRequest())
return tag
}
func (p *plc) WriteTag(tag *tag.Tag) *tag.Tag {
rand.Seed(time.Now().UnixNano())
context := _type.ULINT(rand.Uint64())
p.ContextPool[context] = tag.WriteTagParser
p.UcmmSend(3, 250, context, tag.GenerateWriteMessageRequest())
return tag
}
func (p *plc) ReadTagGroup(tg *tagGroup.TagGroup) {
rand.Seed(time.Now().UnixNano())
context := _type.ULINT(rand.Uint64())
p.ContextPool[context] = tg.ReadTagParser
p.UcmmSend(3, 250, context, tg.GenerateReadMessageRequest())
}
func (p *plc) ReadTagGroupInterval(tg *tagGroup.TagGroup, d time.Duration) {
lib.Cron(d, func() {
p.ReadTagGroup(tg)
})
}
const ServiceGetAttributeList = commonIndustrialProtocol.Service(0x03)
func (p *plc) ListTemplate(instanceID uint32) {
mr := &commonIndustrialProtocol.MessageRouterRequest{}
mr.Service = ServiceGetAttributeList
mr.RequestPath = segment.Paths(
epath.LogicalBuild(epath.LogicalTypeClassID, 0x6C, true),
epath.LogicalBuild(epath.LogicalTypeInstanceID, instanceID&0x0fff, true),
)
data := new(bytes.Buffer)
lib.WriteByte(data, uint16(4))
lib.WriteByte(data, uint16(4))
lib.WriteByte(data, uint16(5))
lib.WriteByte(data, uint16(2))
lib.WriteByte(data, uint16(1))
mr.RequestData = data.Bytes()
rand.Seed(time.Now().UnixNano())
context := _type.ULINT(rand.Uint64())
p.ContextPool[context] = func(mr *commonIndustrialProtocol.MessageRouterResponse) {
log.Printf("% x\n", mr.ResponseData)
}
p.UcmmSend(3, 250, context, mr)
}
const ServiceGetInstanceAttributeList = commonIndustrialProtocol.Service(0x55)
func (p *plc) ListAllTags(instanceID uint32) {
mr := &commonIndustrialProtocol.MessageRouterRequest{}
mr.Service = ServiceGetInstanceAttributeList
mr.RequestPath = segment.Paths(
epath.LogicalBuild(epath.LogicalTypeClassID, 0x6B, true),
epath.LogicalBuild(epath.LogicalTypeInstanceID, instanceID, true),
)
data := new(bytes.Buffer)
lib.WriteByte(data, uint16(2))
lib.WriteByte(data, uint16(1))
lib.WriteByte(data, uint16(2))
mr.RequestData = data.Bytes()
rand.Seed(time.Now().UnixNano())
context := _type.ULINT(rand.Uint64())
p.ContextPool[context] = func(mr *commonIndustrialProtocol.MessageRouterResponse) {
reader := bytes.NewReader(mr.ResponseData)
insId := uint32(0)
for reader.Len() > 0 {
namelen := uint16(0)
lib.ReadByte(reader, &insId)
lib.ReadByte(reader, &namelen)
name := make([]byte, namelen)
lib.ReadByte(reader, name)
_tp := uint16(0)
lib.ReadByte(reader, &_tp)
log.Printf("%s : %s(%#x)\n", name, tag.TypeMap[tag.DataType(_tp)], _tp)
}
if mr.GeneralStatus == 0x06 {
p.ListAllTags(insId + 1)
}
}
p.UcmmSend(3, 250, context, mr)
}
func NewOriginator(addr string, slot uint8, cfg *Config) (*plc, error) {
_plc := &plc{}
_plc.config = cfg
_plc.config = cfg
if _plc.config == nil {
_plc.config = defaultConfig
}
_tcp, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", addr, _plc.config.Port))
if err != nil {
return nil, err
}
_plc.tcpAddr = _tcp
_plc.Controller = &controller{}
_plc.sender = make(chan []byte)
_plc.HandleMap = make(map[ethernetip.Command]func(*ethernetip.Encapsulation))
_plc.TargetPath = epath.PortBuild([]byte{slot}, 1, true)
_plc.ContextPool = make(map[_type.ULINT]func(*commonIndustrialProtocol.MessageRouterResponse))
_plc.HandleMap[ethernetip.CommandNOP] = ethernetip.HandleNop
_plc.HandleMap[ethernetip.CommandListIdentity] = ethernetip.HandleListIdentity
_plc.HandleMap[ethernetip.CommandListInterfaces] = ethernetip.HandleListInterfaces
_plc.HandleMap[ethernetip.CommandRegisterSession] = _plc.handleRegisterSession
_plc.HandleMap[ethernetip.CommandSendRRData] = _plc.handleSendData
_plc.HandleMap[ethernetip.CommandSendUnitData] = _plc.handleSendData
return _plc, nil
}