Repository navigation
Expand file tree
/
Copy pathclient_directory.go
More file actions
526 lines (454 loc) · 15 KB
/
Copy pathclient_directory.go
File metadata and controls
526 lines (454 loc) · 15 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package iec61850
// #include <iec61850_client.h>
import "C"
// DirectoryEntry represents a single directory entry with optional FC annotation
type DirectoryEntry struct {
Name string
FC *FC // Functional Constraint (nil if not applicable)
}
// VariableSpec represents MMS variable specification with type information
type VariableSpec struct {
Name string
Type MmsType
IsArray bool
// For arrays
ArraySize int
// For structures
StructureElements []VariableSpec
}
// ACSIClass represents the ACSI class type for filtering logical node elements
type ACSIClass int
const (
ACSIClassDataObject ACSIClass = 0
ACSIClassDataSet ACSIClass = 1
ACSIClassBRCB ACSIClass = 2 // Buffered Report Control Block
ACSIClassURCB ACSIClass = 3 // Unbuffered Report Control Block
ACSIClassLCB ACSIClass = 4 // Log Control Block
ACSIClassLog ACSIClass = 5
ACSIClassSGCB ACSIClass = 6 // Setting Group Control Block
ACSIClassGoCB ACSIClass = 7 // GOOSE Control Block
ACSIClassGsCB ACSIClass = 8 // GSSE Control Block
ACSIClassMSVCB ACSIClass = 9 // Multicast Sampled Value Control Block
ACSIClassUSVCB ACSIClass = 10 // Unicast Sampled Value Control Block
)
// DataSetInfo represents dataset directory information with deletable flag
type DataSetInfo struct {
Members []string // Dataset member references (format: LDName/LNodeName.item[FC])
IsDeletable bool // Whether the dataset can be deleted by clients
}
// GetServerDirectory returns list of Logical Devices or files from the server.
// This is the modern, recommended API that replaces GetLogicalDeviceList().
//
// When getFileNames is false: Returns list of Logical Device names (clean, no verbose output)
// When getFileNames is true: Returns list of available files on the server
//
// This function uses the GetServerDirectory ACSI service and does not trigger
// verbose "Data set: xxx (not deletable)" output like the deprecated
// IedConnection_getLogicalDeviceList does.
func (c *Client) GetServerDirectory(getFileNames bool) ([]string, error) {
var clientError C.IedClientError
linkedList := C.IedConnection_getServerDirectory(c.conn, &clientError, C.bool(getFileNames))
if err := GetIedClientError(clientError); err != nil {
return nil, err
}
defer C.LinkedList_destroy(linkedList)
var result []string
current := linkedList
for current != nil {
if current.data != nil {
result = append(result, C.GoString((*C.char)(current.data)))
}
current = current.next
}
return result, nil
}
// GetLogicalDeviceNames is a convenience wrapper around GetServerDirectory
// that returns logical device names. This is the recommended replacement
// for the deprecated GetLogicalDeviceList() function.
func (c *Client) GetLogicalDeviceNames() ([]string, error) {
return c.GetServerDirectory(false)
}
// GetLogicalDeviceDirectory returns the directory of logical nodes in a logical device.
// This directly calls IedConnection_getLogicalDeviceDirectory() to get the list of
// logical nodes without triggering verbose "Data set: xxx (not deletable)" output.
//
// Example:
//
// lnNames, err := client.GetLogicalDeviceDirectory("myLD")
// // Returns: ["LLN0", "GGIO1", "MMXU1", ...]
func (c *Client) GetLogicalDeviceDirectory(ldName string) ([]string, error) {
var clientError C.IedClientError
cLdName, freeCLdName := allocCString(ldName)
defer freeCLdName()
linkedList := C.IedConnection_getLogicalDeviceDirectory(c.conn, &clientError, cLdName)
if err := GetIedClientError(clientError); err != nil {
return nil, err
}
defer C.LinkedList_destroy(linkedList)
var result []string
current := linkedList
for current != nil {
if current.data != nil {
result = append(result, C.GoString((*C.char)(current.data)))
}
current = current.next
}
return result, nil
}
// GetLogicalNodeDirectory returns elements of a logical node filtered by ACSI class.
// This allows retrieving specific types of elements (data objects, datasets, control blocks).
//
// ACSI Classes:
// - ACSIClassDataObject: Data objects (DO)
// - ACSIClassDataSet: Data sets
// - ACSIClassBRCB: Buffered report control blocks
// - ACSIClassURCB: Unbuffered report control blocks
// - ACSIClassGoCB: GOOSE control blocks
// - ACSIClassSGCB: Setting group control blocks
// - etc.
//
// Example:
//
// dataObjects, err := client.GetLogicalNodeDirectory("myLD/GGIO1", ACSIClassDataObject)
// // Returns: ["Ind", "SPCSO1", "Mod", "Beh", "Health"]
//
// dataSets, err := client.GetLogicalNodeDirectory("myLD/LLN0", ACSIClassDataSet)
// // Returns: ["Events", "Measurements"]
func (c *Client) GetLogicalNodeDirectory(lnRef string, acsiClass ACSIClass) ([]string, error) {
var clientError C.IedClientError
cLnRef, freeCLnRef := allocCString(lnRef)
defer freeCLnRef()
linkedList := C.IedConnection_getLogicalNodeDirectory(c.conn, &clientError, cLnRef, C.ACSIClass(acsiClass))
if err := GetIedClientError(clientError); err != nil {
return nil, err
}
defer C.LinkedList_destroy(linkedList)
var result []string
current := linkedList
for current != nil {
if current.data != nil {
result = append(result, C.GoString((*C.char)(current.data)))
}
current = current.next
}
return result, nil
}
// GetDataSetDirectory returns the members of a dataset with deletable information.
// Returns the list of data object references (FCDs/FCDAs) that make up the dataset.
//
// The returned member references use the format: LDName/LNodeName.item[FC]
//
// Example:
//
// info, err := client.GetDataSetDirectory("myLD/LLN0.Events")
// // info.Members: ["myLD/GGIO1.Ind.stVal[ST]", "myLD/GGIO1.Ind.q[ST]", "myLD/GGIO1.Ind.t[ST]"]
// // info.IsDeletable: false (pre-configured) or true (dynamic)
func (c *Client) GetDataSetDirectory(dataSetRef string) (*DataSetInfo, error) {
var clientError C.IedClientError
var isDeletable C.bool
cDataSetRef, freeCDataSetRef := allocCString(dataSetRef)
defer freeCDataSetRef()
linkedList := C.IedConnection_getDataSetDirectory(c.conn, &clientError, cDataSetRef, &isDeletable)
if err := GetIedClientError(clientError); err != nil {
return nil, err
}
defer C.LinkedList_destroy(linkedList)
var members []string
current := linkedList
for current != nil {
if current.data != nil {
members = append(members, C.GoString((*C.char)(current.data)))
}
current = current.next
}
return &DataSetInfo{
Members: members,
IsDeletable: bool(isDeletable),
}, nil
}
// GetFileDirectory returns the list of available files on the server
func (c *Client) GetFileDirectory() ([]string, error) {
return c.GetServerDirectory(true)
}
// GetDataDirectory returns the directory of the given data object (DO) or data attribute.
// Returns list of all data attributes or sub data objects as simple name strings.
//
// This function implements the GetDataDirectory ACSI service.
//
// Example:
//
// names, err := client.GetDataDirectory("myLD/myLN.myDO")
// // Returns: ["mag", "q", "t", "subVal"]
func (c *Client) GetDataDirectory(dataRef string) ([]string, error) {
var clientError C.IedClientError
cDataRef, freeCDataRef := allocCString(dataRef)
defer freeCDataRef()
linkedList := C.IedConnection_getDataDirectory(c.conn, &clientError, cDataRef)
if err := GetIedClientError(clientError); err != nil {
return nil, err
}
defer C.LinkedList_destroy(linkedList)
var result []string
current := linkedList
for current != nil {
if current.data != nil {
result = append(result, C.GoString((*C.char)(current.data)))
}
current = current.next
}
return result, nil
}
// GetDataDirectoryWithFC returns the directory of the given data object with FC annotations.
// Returns list of data attributes or sub data objects with functional constraint appended
// in square brackets (e.g., "mag[MX]", "stVal[ST]", "q[MX]").
//
// This solves the enumeration problem for determining which FC to use when reading values!
//
// Example:
//
// entries, err := client.GetDataDirectoryWithFC("myLD/myLN.myDO")
// // Returns: ["mag[MX]", "q[MX]", "t[MX]", "stVal[ST]"]
func (c *Client) GetDataDirectoryWithFC(dataRef string) ([]DirectoryEntry, error) {
var clientError C.IedClientError
cDataRef, freeCDataRef := allocCString(dataRef)
defer freeCDataRef()
linkedList := C.IedConnection_getDataDirectoryFC(c.conn, &clientError, cDataRef)
if err := GetIedClientError(clientError); err != nil {
return nil, err
}
defer C.LinkedList_destroy(linkedList)
var result []DirectoryEntry
current := linkedList
for current != nil {
if current.data != nil {
name := C.GoString((*C.char)(current.data))
entry := parseDirectoryEntryWithFC(name)
result = append(result, entry)
}
current = current.next
}
return result, nil
}
// GetDataDirectoryByFC returns the directory filtered by specific functional constraint.
// More precise than trying multiple FCs - only returns attributes/objects with the given FC.
//
// Example:
//
// names, err := client.GetDataDirectoryByFC("myLD/myLN.myDO", FC_MX)
// // Returns only measurement attributes: ["mag", "q", "t"]
func (c *Client) GetDataDirectoryByFC(dataRef string, fc FC) ([]string, error) {
var clientError C.IedClientError
cDataRef, freeCDataRef := allocCString(dataRef)
defer freeCDataRef()
linkedList := C.IedConnection_getDataDirectoryByFC(c.conn, &clientError, cDataRef, C.FunctionalConstraint(fc))
if err := GetIedClientError(clientError); err != nil {
return nil, err
}
defer C.LinkedList_destroy(linkedList)
var result []string
current := linkedList
for current != nil {
if current.data != nil {
result = append(result, C.GoString((*C.char)(current.data)))
}
current = current.next
}
return result, nil
}
// GetVariableSpecification returns detailed MMS variable specification for a data attribute.
// This allows you to determine the exact type (float32/float64/int32, etc.) BEFORE reading,
// eliminating guesswork in generic client implementations.
//
// Example:
//
// spec, err := client.GetVariableSpecification("myLD/myLN.myDO.mag.f", FC_MX)
// if spec.Type == Float {
// // Now you know it's a float and can read it appropriately
// }
func (c *Client) GetVariableSpecification(dataAttrRef string, fc FC) (*VariableSpec, error) {
var clientError C.IedClientError
cDataAttrRef, freeCDataAttrRef := allocCString(dataAttrRef)
defer freeCDataAttrRef()
mmsSpec := C.IedConnection_getVariableSpecification(c.conn, &clientError, cDataAttrRef, C.FunctionalConstraint(fc))
if err := GetIedClientError(clientError); err != nil {
return nil, err
}
defer C.MmsVariableSpecification_destroy(mmsSpec)
spec := parseVariableSpecification(dataAttrRef, mmsSpec)
return spec, nil
}
// GetLogicalNodeVariables returns all MMS variables that are children of the given logical node.
// Returns raw MMS notation (e.g., "GGIO1$ST$Ind1$stVal") including control block variables.
//
// This is useful for complete enumeration but returns MMS paths, not IEC 61850 references.
func (c *Client) GetLogicalNodeVariables(lnRef string) ([]string, error) {
var clientError C.IedClientError
cLnRef, freeCLnRef := allocCString(lnRef)
defer freeCLnRef()
linkedList := C.IedConnection_getLogicalNodeVariables(c.conn, &clientError, cLnRef)
if err := GetIedClientError(clientError); err != nil {
return nil, err
}
defer C.LinkedList_destroy(linkedList)
var result []string
current := linkedList
for current != nil {
if current.data != nil {
result = append(result, C.GoString((*C.char)(current.data)))
}
current = current.next
}
return result, nil
}
// GetLogicalDeviceVariables returns all MMS variables in a logical device.
// Returns MMS notation (e.g., "GGIO1$ST$Ind1$stVal") and includes control blocks.
//
// Useful for getting a complete picture of all variables in an LD.
func (c *Client) GetLogicalDeviceVariables(ldName string) ([]string, error) {
var clientError C.IedClientError
cLdName, freeCLdName := allocCString(ldName)
defer freeCLdName()
linkedList := C.IedConnection_getLogicalDeviceVariables(c.conn, &clientError, cLdName)
if err := GetIedClientError(clientError); err != nil {
return nil, err
}
defer C.LinkedList_destroy(linkedList)
var result []string
current := linkedList
for current != nil {
if current.data != nil {
result = append(result, C.GoString((*C.char)(current.data)))
}
current = current.next
}
return result, nil
}
// GetLogicalDeviceDataSets returns all data set names in a logical device.
// Returns MMS notation (e.g., "LLN0$dataset1").
func (c *Client) GetLogicalDeviceDataSets(ldName string) ([]string, error) {
var clientError C.IedClientError
cLdName, freeCLdName := allocCString(ldName)
defer freeCLdName()
linkedList := C.IedConnection_getLogicalDeviceDataSets(c.conn, &clientError, cLdName)
if err := GetIedClientError(clientError); err != nil {
return nil, err
}
defer C.LinkedList_destroy(linkedList)
var result []string
current := linkedList
for current != nil {
if current.data != nil {
result = append(result, C.GoString((*C.char)(current.data)))
}
current = current.next
}
return result, nil
}
// Helper function to parse FC from name like "mag[MX]"
func parseDirectoryEntryWithFC(name string) DirectoryEntry {
entry := DirectoryEntry{Name: name}
// Look for FC in square brackets
if len(name) > 3 && name[len(name)-1] == ']' {
for i := len(name) - 2; i >= 0; i-- {
if name[i] == '[' {
fcStr := name[i+1 : len(name)-1]
entry.Name = name[:i]
fc := parseFCString(fcStr)
entry.FC = &fc
break
}
}
}
return entry
}
// Helper function to parse FC string to FC enum
func parseFCString(fcStr string) FC {
switch fcStr {
case "ST":
return ST
case "MX":
return MX
case "SP":
return SP
case "SV":
return SV
case "CF":
return CF
case "DC":
return DC
case "SG":
return SG
case "SE":
return SE
case "SR":
return SR
case "OR":
return OR
case "BL":
return BL
case "EX":
return EX
case "CO":
return CO
case "US":
return US
case "MS":
return MS
case "RP":
return RP
case "BR":
return BR
case "LG":
return LG
default:
return ST // Default fallback
}
}
// Helper function to parse MmsVariableSpecification into VariableSpec
func parseVariableSpecification(name string, mmsSpec *C.MmsVariableSpecification) *VariableSpec {
if mmsSpec == nil {
return nil
}
spec := &VariableSpec{
Name: name,
Type: MmsType(C.MmsVariableSpecification_getType(mmsSpec)),
}
// Handle integer subtypes (Int8, Int16, Int32, Int64)
if spec.Type == Integer {
size := int(mmsSpec.typeSpec[0])
switch size {
case 8:
spec.Type = Int8
case 16:
spec.Type = Int16
case 32:
spec.Type = Int32
default:
spec.Type = Int64
}
}
// Handle unsigned subtypes
if spec.Type == Unsigned {
size := int(mmsSpec.typeSpec[0])
switch size {
case 8:
spec.Type = Uint8
case 16:
spec.Type = Uint16
default:
spec.Type = Uint32
}
}
// Handle arrays
if spec.Type == Array {
spec.IsArray = true
spec.ArraySize = int(C.MmsVariableSpecification_getSize(mmsSpec))
}
// Handle structures (could be expanded to parse child elements)
if spec.Type == Structure {
// For now, just mark as structure
// Full structure parsing would require recursive calls
spec.StructureElements = []VariableSpec{}
}
return spec
}