Repository navigation
Expand file tree
/
Copy pathclient_mms.go
More file actions
708 lines (668 loc) · 25.5 KB
/
Copy pathclient_mms.go
File metadata and controls
708 lines (668 loc) · 25.5 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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
package iec61850
/*
#include <iec61850_client.h>
#include <mms_client_connection.h>
#include <mms_value.h>
#include <stdlib.h>
static void destroyMmsValueLinkedList(LinkedList L) {
if (L) LinkedList_destroyDeep(L, (LinkedListValueDeleteFunction)MmsValue_delete);
}
static void destroyJournalEntryLinkedList(LinkedList L) {
if (L) LinkedList_destroyDeep(L, (LinkedListValueDeleteFunction)MmsJournalEntry_destroy);
}
*/
import "C"
import (
"unsafe"
)
// VariableAccessSpec describes a single variable for defining a named variable list.
// Use ArrayIndex -1 and ComponentName "" for simple variables; set them for array/component access.
type VariableAccessSpec struct {
DomainID string
ItemID string
ArrayIndex int32 // -1 for no array index
ComponentName string // optional component of array element
}
// VariableListEntry is one entry from ReadNamedVariableListDirectory.
type VariableListEntry struct {
DomainID string
ItemID string
ArrayIndex int32
ComponentName string
}
// getMmsConnection returns the underlying MmsConnection for the client. Must be connected.
func (c *Client) getMmsConnection() C.MmsConnection {
return C.IedConnection_getMmsConnection(c.conn)
}
// GetConnectionParameters returns the MMS connection parameters (max outstanding calls, PDU size, etc.).
// The client must have an established connection (conn may be set; parameters are updated after connect).
func (c *Client) GetConnectionParameters() (*MmsConnectionParameters, error) {
if c.conn == nil {
return nil, NotConnected
}
p := C.MmsConnection_getMmsConnectionParameters(c.getMmsConnection())
var sv [11]uint8
for i := 0; i < 11; i++ {
sv[i] = uint8(p.servicesSupported[i])
}
return &MmsConnectionParameters{
MaxServOutstandingCalling: int32(p.maxServOutstandingCalling),
MaxServOutstandingCalled: int32(p.maxServOutstandingCalled),
DataStructureNestingLevel: int32(p.dataStructureNestingLevel),
MaxPduSize: int32(p.maxPduSize),
ServicesSupported: sv,
}, nil
}
// GetServerStatus returns the MMS server status (VMD logical and physical status).
// extendedDerivation instructs the server to run self-diagnosis to determine status.
func (c *Client) GetServerStatus(extendedDerivation bool) (*MmsServerStatus, error) {
var cError C.MmsError
var vmdLogical, vmdPhysical C.int
C.MmsConnection_getServerStatus(c.getMmsConnection(), &cError, &vmdLogical, &vmdPhysical, C.bool(extendedDerivation))
if err := GetMmsError(cError); err != nil {
return nil, err
}
return &MmsServerStatus{
VmdLogicalStatus: int32(vmdLogical),
VmdPhysicalStatus: int32(vmdPhysical),
LocalDetail: 0,
}, nil
}
// ObtainFile sends an obtainFile request: the server will read the file from the client.
// sourceFile is the local (client) path, destinationFile is the remote (server) path.
// This is the same as uploading a file to the server (MMS obtainFile service).
func (c *Client) ObtainFile(sourceFile, destinationFile string) error {
cSrc, freecSrc := allocCString(sourceFile)
defer freecSrc()
cDst, freecDst := allocCString(destinationFile)
defer freecDst()
var cError C.MmsError
C.MmsConnection_obtainFile(c.getMmsConnection(), &cError, cSrc, cDst)
return GetMmsError(cError)
}
// RenameFile renames a file on the server (currentName -> newName).
func (c *Client) RenameFile(currentName, newName string) error {
cCur, freecCur := allocCString(currentName)
defer freecCur()
cNew, freecNew := allocCString(newName)
defer freecNew()
var cError C.MmsError
C.MmsConnection_fileRename(c.getMmsConnection(), &cError, cCur, cNew)
return GetMmsError(cError)
}
// GetVariableAccessAttributes returns the MMS variable access attributes (type specification) for a named variable.
// The caller must call Free() on the returned ref when done.
func (c *Client) GetVariableAccessAttributes(domainID, itemID string) (*MmsVariableSpecificationRef, error) {
cDomain, freecDomain := allocCString(domainID)
defer freecDomain()
cItem, freecItem := allocCString(itemID)
defer freecItem()
var cError C.MmsError
spec := C.MmsConnection_getVariableAccessAttributes(c.getMmsConnection(), &cError, cDomain, cItem)
if err := GetMmsError(cError); err != nil {
return nil, err
}
if spec == nil {
return nil, NullPointer
}
return &MmsVariableSpecificationRef{c: spec, owned: true, libraryOwned: true}, nil
}
// GetDomainNames returns the list of MMS domain names on the server.
func (c *Client) GetDomainNames() ([]string, error) {
var cError C.MmsError
list := C.MmsConnection_getDomainNames(c.getMmsConnection(), &cError)
if err := GetMmsError(cError); err != nil {
return nil, err
}
if list == nil {
return nil, nil
}
defer C.LinkedList_destroy(list)
return linkedListToStrings(list), nil
}
// GetDomainVariableNames returns the names of variables in an MMS domain.
func (c *Client) GetDomainVariableNames(domainID string) ([]string, error) {
cDomain, freecDomain := allocCString(domainID)
defer freecDomain()
var cError C.MmsError
list := C.MmsConnection_getDomainVariableNames(c.getMmsConnection(), &cError, cDomain)
if err := GetMmsError(cError); err != nil {
return nil, err
}
if list == nil {
return nil, nil
}
defer C.LinkedList_destroy(list)
return linkedListToStrings(list), nil
}
// GetDomainVariableListNames returns the names of named variable lists in a domain, or VMD scope if domainID is "".
func (c *Client) GetDomainVariableListNames(domainID string) ([]string, error) {
var cDomain *C.char
var freeCDomain func()
if domainID != "" {
cDomain, freeCDomain = allocCString(domainID)
defer freeCDomain()
}
var cError C.MmsError
list := C.MmsConnection_getDomainVariableListNames(c.getMmsConnection(), &cError, cDomain)
if err := GetMmsError(cError); err != nil {
return nil, err
}
if list == nil {
return nil, nil
}
defer C.LinkedList_destroy(list)
return linkedListToStrings(list), nil
}
// GetVariableListNamesAssociationSpecific returns the names of association-specific named variable lists.
func (c *Client) GetVariableListNamesAssociationSpecific() ([]string, error) {
var cError C.MmsError
list := C.MmsConnection_getVariableListNamesAssociationSpecific(c.getMmsConnection(), &cError)
if err := GetMmsError(cError); err != nil {
return nil, err
}
if list == nil {
return nil, nil
}
defer C.LinkedList_destroy(list)
return linkedListToStrings(list), nil
}
func linkedListToStrings(list C.LinkedList) []string {
var out []string
for node := list; node != nil; node = C.LinkedList_getNext(node) {
data := C.LinkedList_getData(node)
if data != nil {
out = append(out, C.GoString((*C.char)(data)))
}
}
return out
}
// WriteMultipleVariablesFromSpecs writes multiple variables in one request using variable access specs.
// All items must be in the same domain (domainID). values must have the same length as items; each value is written to the variable specified by the corresponding VariableAccessSpec. Returns one MmsDataAccessError per variable.
func (c *Client) WriteMultipleVariablesFromSpecs(domainID string, items []VariableAccessSpec, values []*MmsValueRef) ([]MmsDataAccessError, error) {
if len(items) != len(values) {
return nil, UserProvidedInvalidArgument
}
if len(items) == 0 {
return nil, nil
}
itemIDs := make([]string, len(items))
for i := range items {
itemIDs[i] = items[i].ItemID
}
return c.WriteMultipleVariables(domainID, itemIDs, values)
}
// WriteMultipleVariables writes multiple variables in one request. itemIDs and values must have the same length; each value is written to the variable named by the corresponding itemID in the given domain. Returns one MmsDataAccessError per variable.
func (c *Client) WriteMultipleVariables(domainID string, itemIDs []string, values []*MmsValueRef) ([]MmsDataAccessError, error) {
if len(itemIDs) != len(values) {
return nil, UserProvidedInvalidArgument
}
if len(itemIDs) == 0 {
return nil, nil
}
cDomain, freecDomain := allocCString(domainID)
defer freecDomain()
itemsList := C.LinkedList_create()
defer C.LinkedList_destroyDeep(itemsList, (C.LinkedListValueDeleteFunction)(C.free))
valuesList := C.LinkedList_create()
for _, id := range itemIDs {
cItem := C.CString(id)
C.LinkedList_add(itemsList, unsafe.Pointer(cItem))
}
for _, v := range values {
if v != nil && v.c != nil {
C.LinkedList_add(valuesList, unsafe.Pointer(v.c))
}
}
defer C.LinkedList_destroyStatic(valuesList)
var cError C.MmsError
var cResults C.LinkedList
C.MmsConnection_writeMultipleVariables(c.getMmsConnection(), &cError, cDomain, itemsList, valuesList, &cResults)
if err := GetMmsError(cError); err != nil {
return nil, err
}
if cResults == nil {
return nil, nil
}
defer C.destroyMmsValueLinkedList(cResults)
var results []MmsDataAccessError
for node := cResults; node != nil; node = C.LinkedList_getNext(node) {
data := C.LinkedList_getData(node)
if data != nil {
val := (*C.MmsValue)(data)
results = append(results, MmsDataAccessError(C.MmsValue_getDataAccessError(val)))
}
}
return results, nil
}
// ReadNamedVariableListValues reads all values from a domain or VMD scoped named variable list.
// Pass domainID as "" for VMD scope. specWithResult should be true for IEC 61850 compliant requests.
// The returned slice contains one MmsValue per list entry; the caller does not own the underlying C memory (it is freed by the library after the call returns), so values are converted to Go types.
func (c *Client) ReadNamedVariableListValues(domainID, listName string, specWithResult bool) ([]*MmsValue, error) {
var cDomain *C.char
var freeCDomain func()
if domainID != "" {
cDomain, freeCDomain = allocCString(domainID)
defer freeCDomain()
}
cList, freecList := allocCString(listName)
defer freecList()
var cError C.MmsError
result := C.MmsConnection_readNamedVariableListValues(c.getMmsConnection(), &cError, cDomain, cList, C.bool(specWithResult))
if err := GetMmsError(cError); err != nil {
return nil, err
}
if result == nil {
return nil, nil
}
defer C.MmsValue_delete(result)
return toGoStructure(result, Array)
}
// ReadNamedVariableListValuesAssociationSpecific reads values from an association-specific named variable list.
func (c *Client) ReadNamedVariableListValuesAssociationSpecific(listName string, specWithResult bool) ([]*MmsValue, error) {
cList, freecList := allocCString(listName)
defer freecList()
var cError C.MmsError
result := C.MmsConnection_readNamedVariableListValuesAssociationSpecific(c.getMmsConnection(), &cError, cList, C.bool(specWithResult))
if err := GetMmsError(cError); err != nil {
return nil, err
}
if result == nil {
return nil, nil
}
defer C.MmsValue_delete(result)
return toGoStructure(result, Array)
}
// ClientDataSet holds data set values read from the server. Create with ReadDataSetValues; call Destroy when done.
// The underlying MmsValue (MMS_ARRAY) can be passed to NewGooseSubscriberWithDataSet for GOOSE; keep the
// ClientDataSet alive for the lifetime of that subscriber.
type ClientDataSet struct {
c C.ClientDataSet
}
// ReadDataSetValues reads the data set values from the server. dataSetReference is the object reference
// (e.g. "LD/LN.dsName" or "@asName"). Pass nil for existing to create a new container; pass an existing
// *ClientDataSet to update it in place.
func (c *Client) ReadDataSetValues(dataSetReference string) (*ClientDataSet, error) {
if c.conn == nil {
return nil, NotConnected
}
ref, freeRef := allocCString(dataSetReference)
defer freeRef()
var cErr C.IedClientError
ds := C.IedConnection_readDataSetValues(c.conn, &cErr, ref, nil)
if err := GetIedClientError(cErr); err != nil {
return nil, err
}
if ds == nil {
return nil, nil
}
return &ClientDataSet{c: ds}, nil
}
// Destroy frees the ClientDataSet. Do not use it or any GooseDataSetValues derived from it after Destroy.
func (d *ClientDataSet) Destroy() {
if d != nil && d.c != nil {
C.ClientDataSet_destroy(d.c)
d.c = nil
}
}
// GooseDataSetValues returns a handle to the underlying MmsValue (MMS_ARRAY) for use with
// NewGooseSubscriberWithDataSet. The ClientDataSet must remain alive while the subscriber uses it.
func (d *ClientDataSet) GooseDataSetValues() GooseDataSetValues {
if d == nil || d.c == nil {
return GooseDataSetValues{}
}
return GooseDataSetValues{p: unsafe.Pointer(C.ClientDataSet_getValues(d.c))}
}
// GooseDataSetValues is an opaque handle to the MmsValue array from a ClientDataSet, for use with
// NewGooseSubscriberWithDataSet. Obtain it from ClientDataSet.GooseDataSetValues(); keep the
// ClientDataSet alive for the lifetime of the subscriber.
type GooseDataSetValues struct {
p unsafe.Pointer
}
// ReadNamedVariableListDirectory returns the directory (list of variable references) and whether the list is deletable.
func (c *Client) ReadNamedVariableListDirectory(domainID, listName string) (entries []VariableListEntry, deletable bool, err error) {
var cDomain *C.char
var freeCDomain func()
if domainID != "" {
cDomain, freeCDomain = allocCString(domainID)
defer freeCDomain()
}
cList, freecList := allocCString(listName)
defer freecList()
var cError C.MmsError
var cDeletable C.bool
list := C.MmsConnection_readNamedVariableListDirectory(c.getMmsConnection(), &cError, cDomain, cList, &cDeletable)
if err = GetMmsError(cError); err != nil {
return nil, false, err
}
deletable = bool(cDeletable)
if list == nil {
return nil, deletable, nil
}
defer C.LinkedList_destroyDeep(list, (C.LinkedListValueDeleteFunction)(C.MmsVariableAccessSpecification_destroy))
for node := list; node != nil; node = C.LinkedList_getNext(node) {
data := C.LinkedList_getData(node)
if data == nil {
continue
}
spec := (*C.MmsVariableAccessSpecification)(data)
entries = append(entries, VariableListEntry{
DomainID: C.GoString(spec.domainId),
ItemID: C.GoString(spec.itemId),
ArrayIndex: int32(spec.arrayIndex),
ComponentName: C.GoString(spec.componentName),
})
}
return entries, deletable, nil
}
// ReadNamedVariableListDirectoryAssociationSpecific returns the directory for an association-specific list.
func (c *Client) ReadNamedVariableListDirectoryAssociationSpecific(listName string) (entries []VariableListEntry, deletable bool, err error) {
cList, freecList := allocCString(listName)
defer freecList()
var cError C.MmsError
var cDeletable C.bool
list := C.MmsConnection_readNamedVariableListDirectoryAssociationSpecific(c.getMmsConnection(), &cError, cList, &cDeletable)
if err = GetMmsError(cError); err != nil {
return nil, false, err
}
deletable = bool(cDeletable)
if list == nil {
return nil, deletable, nil
}
defer C.LinkedList_destroyDeep(list, (C.LinkedListValueDeleteFunction)(C.MmsVariableAccessSpecification_destroy))
for node := list; node != nil; node = C.LinkedList_getNext(node) {
data := C.LinkedList_getData(node)
if data == nil {
continue
}
spec := (*C.MmsVariableAccessSpecification)(data)
entries = append(entries, VariableListEntry{
DomainID: C.GoString(spec.domainId),
ItemID: C.GoString(spec.itemId),
ArrayIndex: int32(spec.arrayIndex),
ComponentName: C.GoString(spec.componentName),
})
}
return entries, deletable, nil
}
// DefineNamedVariableList defines a new domain or VMD scoped named variable list. Pass domainID as "" for VMD scope.
func (c *Client) DefineNamedVariableList(domainID, listName string, variableSpecs []VariableAccessSpec) error {
if len(variableSpecs) == 0 {
return UserProvidedInvalidArgument
}
var cDomain *C.char
var freeCDomain func()
if domainID != "" {
cDomain, freeCDomain = allocCString(domainID)
defer freeCDomain()
}
cList, freecList := allocCString(listName)
defer freecList()
clist := C.LinkedList_create()
defer C.LinkedList_destroyDeep(clist, (C.LinkedListValueDeleteFunction)(C.MmsVariableAccessSpecification_destroy))
for _, vs := range variableSpecs {
cDom := C.CString(vs.DomainID)
cItem := C.CString(vs.ItemID)
var spec *C.MmsVariableAccessSpecification
if vs.ArrayIndex >= 0 || vs.ComponentName != "" {
var cComp *C.char
if vs.ComponentName != "" {
cComp = C.CString(vs.ComponentName)
}
spec = C.MmsVariableAccessSpecification_createAlternateAccess(cDom, cItem, C.int32_t(vs.ArrayIndex), cComp)
} else {
spec = C.MmsVariableAccessSpecification_create(cDom, cItem)
}
C.LinkedList_add(clist, unsafe.Pointer(spec))
}
var cError C.MmsError
C.MmsConnection_defineNamedVariableList(c.getMmsConnection(), &cError, cDomain, cList, clist)
return GetMmsError(cError)
}
// DefineNamedVariableListAssociationSpecific defines a new association-specific named variable list.
func (c *Client) DefineNamedVariableListAssociationSpecific(listName string, variableSpecs []VariableAccessSpec) error {
if len(variableSpecs) == 0 {
return UserProvidedInvalidArgument
}
cList, freecList := allocCString(listName)
defer freecList()
clist := C.LinkedList_create()
defer C.LinkedList_destroyDeep(clist, (C.LinkedListValueDeleteFunction)(C.MmsVariableAccessSpecification_destroy))
for _, vs := range variableSpecs {
cDom := C.CString(vs.DomainID)
cItem := C.CString(vs.ItemID)
var spec *C.MmsVariableAccessSpecification
if vs.ArrayIndex >= 0 || vs.ComponentName != "" {
var cComp *C.char
if vs.ComponentName != "" {
cComp = C.CString(vs.ComponentName)
}
spec = C.MmsVariableAccessSpecification_createAlternateAccess(cDom, cItem, C.int32_t(vs.ArrayIndex), cComp)
} else {
spec = C.MmsVariableAccessSpecification_create(cDom, cItem)
}
C.LinkedList_add(clist, unsafe.Pointer(spec))
}
var cError C.MmsError
C.MmsConnection_defineNamedVariableListAssociationSpecific(c.getMmsConnection(), &cError, cList, clist)
return GetMmsError(cError)
}
// DeleteNamedVariableList deletes a domain or VMD scoped named variable list. Pass domainID as "" for VMD scope.
func (c *Client) DeleteNamedVariableList(domainID, listName string) (bool, error) {
var cDomain *C.char
var freeCDomain func()
if domainID != "" {
cDomain, freeCDomain = allocCString(domainID)
defer freeCDomain()
}
cList, freecList := allocCString(listName)
defer freecList()
var cError C.MmsError
ok := C.MmsConnection_deleteNamedVariableList(c.getMmsConnection(), &cError, cDomain, cList)
if err := GetMmsError(cError); err != nil {
return false, err
}
return bool(ok), nil
}
// DeleteAssociationSpecificNamedVariableList deletes an association-specific named variable list.
func (c *Client) DeleteAssociationSpecificNamedVariableList(listName string) (bool, error) {
cList, freecList := allocCString(listName)
defer freecList()
var cError C.MmsError
ok := C.MmsConnection_deleteAssociationSpecificNamedVariableList(c.getMmsConnection(), &cError, cList)
if err := GetMmsError(cError); err != nil {
return false, err
}
return bool(ok), nil
}
// SetNamedVariableListValues is an alias for WriteNamedVariableList: writes values to a domain or VMD scoped named variable list.
func (c *Client) SetNamedVariableListValues(domainID, listName string, values []*MmsValueRef) ([]MmsDataAccessError, error) {
return c.WriteNamedVariableList(domainID, listName, values)
}
// WriteNamedVariableList writes values to a domain or VMD scoped named variable list.
// Pass domainID as "" for VMD scope. values must contain one MmsValueRef per list entry; refs are not consumed.
// Returns the data access error result for each variable write.
func (c *Client) WriteNamedVariableList(domainID, listName string, values []*MmsValueRef) ([]MmsDataAccessError, error) {
if len(values) == 0 {
return nil, UserProvidedInvalidArgument
}
var cDomain *C.char
var freeCDomain func()
if domainID != "" {
cDomain, freeCDomain = allocCString(domainID)
defer freeCDomain()
}
cList, freecList := allocCString(listName)
defer freecList()
clist := C.LinkedList_create()
for _, v := range values {
if v != nil && v.c != nil {
C.LinkedList_add(clist, unsafe.Pointer(v.c))
}
}
defer C.LinkedList_destroyStatic(clist)
var cError C.MmsError
var cResults C.LinkedList
C.MmsConnection_writeNamedVariableList(c.getMmsConnection(), &cError, C.bool(false), cDomain, cList, clist, &cResults)
if err := GetMmsError(cError); err != nil {
return nil, err
}
if cResults == nil {
return nil, nil
}
defer C.destroyMmsValueLinkedList(cResults)
var results []MmsDataAccessError
for node := cResults; node != nil; node = C.LinkedList_getNext(node) {
data := C.LinkedList_getData(node)
if data != nil {
val := (*C.MmsValue)(data)
results = append(results, MmsDataAccessError(C.MmsValue_getDataAccessError(val)))
}
}
return results, nil
}
// MmsServerIdentity holds the result of MMS Identify (VMD identity).
type MmsServerIdentity struct {
VendorName string
ModelName string
Revision string
}
// Identify returns the server identity (vendor name, model name, revision) via the MMS identify service.
func (c *Client) Identify() (*MmsServerIdentity, error) {
var cError C.MmsError
ident := C.MmsConnection_identify(c.getMmsConnection(), &cError)
if err := GetMmsError(cError); err != nil {
return nil, err
}
if ident == nil {
return nil, NullPointer
}
defer C.MmsServerIdentity_destroy(ident)
return &MmsServerIdentity{
VendorName: C.GoString(ident.vendorName),
ModelName: C.GoString(ident.modelName),
Revision: C.GoString(ident.revision),
}, nil
}
// JournalVariable is one variable in a journal entry.
type JournalVariable struct {
Tag string
Value *MmsValue
}
// JournalEntry is one entry from a journal read.
type JournalEntry struct {
EntryID *MmsValue // Octet string
OccurrenceTime *MmsValue // Binary time
Variables []JournalVariable
}
func convertJournalEntry(entry C.MmsJournalEntry) JournalEntry {
je := JournalEntry{}
je.EntryID = nil
je.OccurrenceTime = nil
if eid := C.MmsJournalEntry_getEntryID(entry); eid != nil {
t := MmsType(C.MmsValue_getType(eid))
if v, err := toGoValue(eid, t); err == nil {
je.EntryID = &MmsValue{Type: t, Value: v}
}
}
if ot := C.MmsJournalEntry_getOccurenceTime(entry); ot != nil {
t := MmsType(C.MmsValue_getType(ot))
if v, err := toGoValue(ot, t); err == nil {
je.OccurrenceTime = &MmsValue{Type: t, Value: v}
}
}
varsList := C.MmsJournalEntry_getJournalVariables(entry)
for node := varsList; node != nil; node = C.LinkedList_getNext(node) {
data := C.LinkedList_getData(node)
if data == nil {
continue
}
jv := (C.MmsJournalVariable)(data)
tag := ""
if t := C.MmsJournalVariable_getTag(jv); t != nil {
tag = C.GoString(t)
}
val := C.MmsJournalVariable_getValue(jv)
var goVal *MmsValue
if val != nil {
mt := MmsType(C.MmsValue_getType(val))
if v, err := toGoValue(val, mt); err == nil {
goVal = &MmsValue{Type: mt, Value: v}
}
}
je.Variables = append(je.Variables, JournalVariable{Tag: tag, Value: goVal})
}
return je
}
// ReadJournalTimeRange reads journal entries in the given time range. startTimeMs and endTimeMs are milliseconds since Unix epoch (binary time). The caller does not own the returned entries.
func (c *Client) ReadJournalTimeRange(domainID, itemID string, startTimeMs, endTimeMs uint64) (entries []JournalEntry, moreFollows bool, err error) {
cDomain, freecDomain := allocCString(domainID)
defer freecDomain()
cItem, freecItem := allocCString(itemID)
defer freecItem()
startV := C.MmsValue_newBinaryTime(C.bool(false))
defer C.MmsValue_delete(startV)
C.MmsValue_setBinaryTime(startV, C.uint64_t(startTimeMs))
endV := C.MmsValue_newBinaryTime(C.bool(false))
defer C.MmsValue_delete(endV)
C.MmsValue_setBinaryTime(endV, C.uint64_t(endTimeMs))
var cMore C.bool
var cError C.MmsError
list := C.MmsConnection_readJournalTimeRange(c.getMmsConnection(), &cError, cDomain, cItem, startV, endV, &cMore)
if err = GetMmsError(cError); err != nil {
return nil, false, err
}
moreFollows = bool(cMore)
if list == nil {
return nil, moreFollows, nil
}
defer C.destroyJournalEntryLinkedList(list)
for node := list; node != nil; node = C.LinkedList_getNext(node) {
data := C.LinkedList_getData(node)
if data != nil {
entries = append(entries, convertJournalEntry(C.MmsJournalEntry(data)))
}
}
return entries, moreFollows, nil
}
// ReadJournalStartAfter reads journal entries starting after the given time and entry specification.
// timeSpecification and entrySpecification are MMS binary time and octet string respectively; pass nil for entrySpecification to start from the time.
func (c *Client) ReadJournalStartAfter(domainID, itemID string, timeSpecificationMs uint64, entrySpecification []byte) (entries []JournalEntry, moreFollows bool, err error) {
cDomain, freecDomain := allocCString(domainID)
defer freecDomain()
cItem, freecItem := allocCString(itemID)
defer freecItem()
timeV := C.MmsValue_newBinaryTime(C.bool(false))
defer C.MmsValue_delete(timeV)
C.MmsValue_setBinaryTime(timeV, C.uint64_t(timeSpecificationMs))
// C library requires non-NULL entrySpecification (it calls MmsValue_getType on it).
var entryV *C.MmsValue
if len(entrySpecification) > 0 {
entryV = C.MmsValue_newOctetString(C.int(len(entrySpecification)), C.int(len(entrySpecification)))
defer C.MmsValue_delete(entryV)
for i, b := range entrySpecification {
C.MmsValue_setOctetStringOctet(entryV, C.int(i), C.uint8_t(b))
}
} else {
entryV = C.MmsValue_newOctetString(0, 0)
defer C.MmsValue_delete(entryV)
}
var cMore C.bool
var cError C.MmsError
list := C.MmsConnection_readJournalStartAfter(c.getMmsConnection(), &cError, cDomain, cItem, timeV, entryV, &cMore)
if err = GetMmsError(cError); err != nil {
return nil, false, err
}
moreFollows = bool(cMore)
if list == nil {
return nil, moreFollows, nil
}
defer C.destroyJournalEntryLinkedList(list)
for node := list; node != nil; node = C.LinkedList_getNext(node) {
data := C.LinkedList_getData(node)
if data != nil {
entries = append(entries, convertJournalEntry(C.MmsJournalEntry(data)))
}
}
return entries, moreFollows, nil
}