-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathnode_id.go
436 lines (379 loc) · 9.89 KB
/
node_id.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright 2018-2020 opcua authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package ua
import (
"encoding/base64"
"encoding/json"
"encoding/xml"
"fmt"
"math"
"github.com/gopcua/opcua/errors"
)
// todo(fs): fix mask
// NodeID is an identifier for a node in the address space of an OPC UA Server.
// The NodeID object encodes all different node id types.
type NodeID struct {
mask NodeIDType
ns uint16
nid uint32
bid []byte
gid *GUID
}
// NewTwoByteNodeID returns a new two byte node id.
func NewTwoByteNodeID(id uint8) *NodeID {
return &NodeID{
mask: NodeIDTypeTwoByte,
nid: uint32(id),
}
}
// NewFourByteNodeID returns a new four byte node id.
func NewFourByteNodeID(ns uint8, id uint16) *NodeID {
return &NodeID{
mask: NodeIDTypeFourByte,
ns: uint16(ns),
nid: uint32(id),
}
}
// NewNumericNodeID returns a new numeric node id.
func NewNumericNodeID(ns uint16, id uint32) *NodeID {
return &NodeID{
mask: NodeIDTypeNumeric,
ns: ns,
nid: id,
}
}
// NewStringNodeID returns a new string node id.
func NewStringNodeID(ns uint16, id string) *NodeID {
return &NodeID{
mask: NodeIDTypeString,
ns: ns,
bid: []byte(id),
}
}
// NewGUIDNodeID returns a new GUID node id.
func NewGUIDNodeID(ns uint16, id string) *NodeID {
return &NodeID{
mask: NodeIDTypeGUID,
ns: ns,
gid: NewGUID(id),
}
}
// NewByteStringNodeID returns a new byte string node id.
func NewByteStringNodeID(ns uint16, id []byte) *NodeID {
return &NodeID{
mask: NodeIDTypeByteString,
ns: ns,
bid: id,
}
}
// NewNodeIDFromExpandedNodeID returns a new NodeID derived from ExpandedNodeID
func NewNodeIDFromExpandedNodeID(id *ExpandedNodeID) *NodeID {
bid := make([]byte, len(id.NodeID.bid))
copy(bid, id.NodeID.bid)
if len(bid) == 0 {
bid = nil
}
var gid *GUID
if egid := id.NodeID.gid; egid != nil {
var ngid GUID
ngid.Data1 = egid.Data1
ngid.Data2 = egid.Data2
ngid.Data3 = egid.Data3
ngid.Data4 = make([]byte, len(egid.Data4))
copy(ngid.Data4, egid.Data4)
if len(ngid.Data4) == 0 {
ngid.Data4 = nil
}
gid = &ngid
}
return &NodeID{
mask: id.NodeID.mask & 0b00111111, // expanded flag NamespaceURI and ServerIndex need to be unset
ns: id.NodeID.ns,
nid: id.NodeID.nid,
bid: bid,
gid: gid,
}
}
// MustParseNodeID returns a node id from a string definition
// if it is parseable by ParseNodeID. Otherwise, the function
// panics.
func MustParseNodeID(s string) *NodeID {
id, err := ParseNodeID(s)
if err != nil {
panic(err.Error())
}
return id
}
// ParseNodeID returns a node id from a string definition of the format
// 'ns=<namespace>;{s,i,b,g}=<identifier>'.
//
// The 's=' prefix can be omitted for string node ids in namespace 0.
//
// For numeric ids the smallest possible type which can store the namespace
// and id value is returned.
//
// Namespace URIs are not supported since NodeID cannot store them. If you need
// to support namespace URIs use ParseExpandedNodeID.
func ParseNodeID(s string) (*NodeID, error) {
id, err := ParseExpandedNodeID(s, nil)
if err != nil {
return nil, err
}
if id.HasNamespaceURI() {
return nil, errors.Errorf("namespace uris are not supported. use `ua.ParseExpandedNodeID`")
}
if id.HasServerIndex() {
return nil, errors.Errorf("server index is not supported. use `ua.ParseExpandedNodeID`")
}
return id.NodeID, nil
}
// EncodingMask returns the encoding mask field including the
// type information and additional flags.
func (n *NodeID) EncodingMask() NodeIDType {
return n.mask
}
// Type returns the node id type in EncodingMask.
func (n *NodeID) Type() NodeIDType {
return n.mask & NodeIDType(0xf)
}
// URIFlag returns whether the URI flag is set in EncodingMask.
func (n *NodeID) URIFlag() bool {
return n.mask&0x80 == 0x80
}
// SetURIFlag sets NamespaceURI flag in EncodingMask.
func (n *NodeID) SetURIFlag() {
n.mask |= 0x80
}
// IndexFlag returns whether the Index flag is set in EncodingMask.
func (n *NodeID) IndexFlag() bool {
return n.mask&0x40 == 0x40
}
// SetIndexFlag sets NamespaceURI flag in EncodingMask.
func (n *NodeID) SetIndexFlag() {
n.mask |= 0x40
}
// Namespace returns the namespace id. For two byte node ids
// this will always be zero.
func (n *NodeID) Namespace() uint16 {
return n.ns
}
// SetNamespace sets the namespace id. It returns an error
// if the id is not within the range of the node id type.
func (n *NodeID) SetNamespace(v uint16) error {
switch n.Type() {
case NodeIDTypeTwoByte:
if v != 0 {
return errors.Errorf("out of range [0..0]: %d", v)
}
return nil
case NodeIDTypeFourByte:
if max := uint16(math.MaxUint8); v > max {
return errors.Errorf("out of range [0..%d]: %d", max, v)
}
n.ns = uint16(v)
return nil
default:
if max := uint16(math.MaxUint16); v > max {
return errors.Errorf("out of range [0..%d]: %d", max, v)
}
n.ns = uint16(v)
return nil
}
}
// IntID returns the identifier value if the type is
// TwoByte, FourByte or Numeric. For all other types IntID
// returns 0.
func (n *NodeID) IntID() uint32 {
return n.nid
}
// SetIntID sets the identifier value for two byte, four byte and
// numeric node ids. It returns an error for other types.
func (n *NodeID) SetIntID(v uint32) error {
switch n.Type() {
case NodeIDTypeTwoByte:
if max := uint32(math.MaxUint8); v > max {
return errors.Errorf("out of range [0..%d]: %d", max, v)
}
n.nid = uint32(v)
return nil
case NodeIDTypeFourByte:
if max := uint32(math.MaxUint16); v > max {
return errors.Errorf("out of range [0..%d]: %d", max, v)
}
n.nid = uint32(v)
return nil
case NodeIDTypeNumeric:
if max := uint32(math.MaxUint32); v > max {
return errors.Errorf("out of range [0..%d]: %d", max, v)
}
n.nid = uint32(v)
return nil
default:
return errors.Errorf("incompatible node id type")
}
}
// StringID returns the string value of the identifier
// for String and GUID NodeIDs, and the base64 encoded
// value for Opaque types. For all other types StringID
// returns an empty string.
func (n *NodeID) StringID() string {
switch n.Type() {
case NodeIDTypeGUID:
if n.gid == nil {
return ""
}
return n.gid.String()
case NodeIDTypeString:
return string(n.bid)
case NodeIDTypeByteString:
return base64.StdEncoding.EncodeToString(n.bid)
default:
return ""
}
}
// SetStringID sets the identifier value for string, guid and opaque
// node ids. It returns an error for other types.
func (n *NodeID) SetStringID(v string) error {
switch n.Type() {
case NodeIDTypeGUID:
n.gid = NewGUID(v)
return nil
case NodeIDTypeString:
n.bid = []byte(v)
return nil
case NodeIDTypeByteString:
b, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return err
}
n.bid = b
return nil
default:
return errors.Errorf("incompatible node id type")
}
}
// String returns the string representation of the NodeID
// in the format described by ParseNodeID.
func (n *NodeID) String() string {
if n == nil {
return ""
}
switch n.Type() {
case NodeIDTypeTwoByte:
return fmt.Sprintf("i=%d", n.nid)
case NodeIDTypeFourByte:
if n.ns == 0 {
return fmt.Sprintf("i=%d", n.nid)
}
return fmt.Sprintf("ns=%d;i=%d", n.ns, n.nid)
case NodeIDTypeNumeric:
if n.ns == 0 {
return fmt.Sprintf("i=%d", n.nid)
}
return fmt.Sprintf("ns=%d;i=%d", n.ns, n.nid)
case NodeIDTypeString:
if n.ns == 0 {
return fmt.Sprintf("s=%s", n.StringID())
}
return fmt.Sprintf("ns=%d;s=%s", n.ns, n.StringID())
case NodeIDTypeGUID:
if n.ns == 0 {
return fmt.Sprintf("g=%s", n.StringID())
}
return fmt.Sprintf("ns=%d;g=%s", n.ns, n.StringID())
case NodeIDTypeByteString:
if n.ns == 0 {
return fmt.Sprintf("b=%s", n.StringID())
}
return fmt.Sprintf("ns=%d;b=%s", n.ns, n.StringID())
default:
panic(fmt.Sprintf("invalid node id type: %d", n.Type()))
}
}
func (n *NodeID) Decode(b []byte) (int, error) {
buf := NewBuffer(b)
n.mask = NodeIDType(buf.ReadByte())
typ := n.mask & 0xf
switch typ {
case NodeIDTypeTwoByte:
n.nid = uint32(buf.ReadByte())
return buf.Pos(), buf.Error()
case NodeIDTypeFourByte:
n.ns = uint16(buf.ReadByte())
n.nid = uint32(buf.ReadUint16())
return buf.Pos(), buf.Error()
case NodeIDTypeNumeric:
n.ns = buf.ReadUint16()
n.nid = buf.ReadUint32()
return buf.Pos(), buf.Error()
case NodeIDTypeGUID:
n.ns = buf.ReadUint16()
n.gid = &GUID{}
buf.ReadStruct(n.gid)
return buf.Pos(), buf.Error()
case NodeIDTypeByteString, NodeIDTypeString:
n.ns = buf.ReadUint16()
n.bid = buf.ReadBytes()
return buf.Pos(), buf.Error()
default:
return 0, errors.Errorf("invalid node id type %v", typ)
}
}
func (n *NodeID) Encode() ([]byte, error) {
buf := NewBuffer(nil)
buf.WriteByte(byte(n.mask))
switch n.Type() {
case NodeIDTypeTwoByte:
buf.WriteByte(byte(n.nid))
case NodeIDTypeFourByte:
buf.WriteByte(byte(n.ns))
buf.WriteUint16(uint16(n.nid))
case NodeIDTypeNumeric:
buf.WriteUint16(n.ns)
buf.WriteUint32(n.nid)
case NodeIDTypeGUID:
buf.WriteUint16(n.ns)
buf.WriteStruct(n.gid)
case NodeIDTypeByteString, NodeIDTypeString:
buf.WriteUint16(n.ns)
buf.WriteByteString(n.bid)
default:
return nil, errors.Errorf("invalid node id type %v", n.Type())
}
return buf.Bytes(), buf.Error()
}
func (n *NodeID) Equal(o *NodeID) bool {
return n.String() == o.String()
}
func (n *NodeID) MarshalJSON() ([]byte, error) {
if n == nil {
return []byte(`null`), nil
}
return json.Marshal(n.String())
}
func (n *NodeID) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
nid, err := ParseNodeID(s)
if err != nil {
return err
}
*n = *nid
return nil
}
// todo(fs): not sure if this should exist here. Maybe there are multiple different xml formats?
func (n *NodeID) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var s string
if err := d.DecodeElement(&s, &start); err != nil {
return err
}
id, err := ParseNodeID(s)
if err != nil {
return err
}
*n = *id
return nil
}