Skip to content

Commit dde8406

Browse files
authored
Merge pull request #35 from focusingly/sv-dev
SV 发布和订阅功能, 以及 Goose 代码中存在问题的修复
2 parents ba5edef + f741550 commit dde8406

21 files changed

+1961
-6
lines changed

common.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package iec61850
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
/*
9+
#include "iec61850_common.h"
10+
*/
11+
import "C"
12+
13+
type (
14+
Timestamp struct {
15+
cTimestamp C.Timestamp
16+
}
17+
18+
Quality uint16
19+
Validity uint16
20+
)
21+
22+
const (
23+
QUALITY_VALIDITY_GOOD Quality = 0
24+
QUALITY_VALIDITY_INVALID Quality = 2
25+
QUALITY_VALIDITY_RESERVED Quality = 1
26+
QUALITY_VALIDITY_QUESTIONABLE Quality = 3
27+
QUALITY_DETAIL_OVERFLOW Quality = 4
28+
QUALITY_DETAIL_OUT_OF_RANGE Quality = 8
29+
QUALITY_DETAIL_BAD_REFERENCE Quality = 16
30+
QUALITY_DETAIL_OSCILLATORY Quality = 32
31+
QUALITY_DETAIL_FAILURE Quality = 64
32+
QUALITY_DETAIL_OLD_DATA Quality = 128
33+
QUALITY_DETAIL_INCONSISTENT Quality = 256
34+
QUALITY_DETAIL_INACCURATE Quality = 512
35+
QUALITY_SOURCE_SUBSTITUTED Quality = 1024
36+
QUALITY_TEST Quality = 2048
37+
QUALITY_OPERATOR_BLOCKED Quality = 4096
38+
QUALITY_DERIVED Quality = 8192
39+
)
40+
41+
const (
42+
VALIDITY_GOOD Validity = iota
43+
VALIDITY_INVALID
44+
VALIDITY_RESERVED
45+
VALIDITY_QUESTIONABLE
46+
)
47+
48+
func (receiver Quality) GetValidity() Validity {
49+
return Validity(receiver & 0x3)
50+
}
51+
52+
func NewTimestamp(time ...time.Time) *Timestamp {
53+
v := C.Timestamp_create()
54+
ret := &Timestamp{
55+
cTimestamp: *v,
56+
}
57+
C.Timestamp_destroy(v)
58+
switch len(time) {
59+
case 0:
60+
// skip
61+
case 1:
62+
ret.SetTime(time[0])
63+
default:
64+
panic(fmt.Errorf("expect got 0 or 1 time param, but got: %d", len(time)))
65+
}
66+
67+
return ret
68+
}
69+
70+
func (receiver *Timestamp) GetTimeInSeconds() uint32 {
71+
return uint32(C.Timestamp_getTimeInSeconds(&receiver.cTimestamp))
72+
}
73+
74+
func (receiver *Timestamp) GetTimeInMs() uint64 {
75+
return uint64(C.Timestamp_getTimeInMs(&receiver.cTimestamp))
76+
}
77+
78+
func (receiver *Timestamp) GetTimeInNs() uint64 {
79+
return uint64(C.Timestamp_getTimeInNs(&receiver.cTimestamp))
80+
}
81+
82+
func (receiver *Timestamp) GetTime() time.Time {
83+
return time.Unix(0, int64(receiver.GetTimeInNs()))
84+
}
85+
86+
func (receiver *Timestamp) IsLeapSecondKnown() bool {
87+
return bool(C.Timestamp_isLeapSecondKnown(&receiver.cTimestamp))
88+
}
89+
90+
func (receiver *Timestamp) SetLeapSecondKnown(value bool) *Timestamp {
91+
C.Timestamp_setLeapSecondKnown(&receiver.cTimestamp, C.bool(value))
92+
return receiver
93+
}
94+
95+
func (receiver *Timestamp) HasClockFailure() bool {
96+
return bool(C.Timestamp_hasClockFailure(&receiver.cTimestamp))
97+
}
98+
99+
func (receiver *Timestamp) SetClockFailure(value bool) *Timestamp {
100+
C.Timestamp_setClockFailure(&receiver.cTimestamp, C.bool(value))
101+
102+
return receiver
103+
}
104+
105+
func (receiver *Timestamp) IsClockNotSynchronized() bool {
106+
return bool(C.Timestamp_isClockNotSynchronized(&receiver.cTimestamp))
107+
}
108+
109+
func (receiver *Timestamp) SetClockNotSynchronized(value bool) *Timestamp {
110+
C.Timestamp_setClockNotSynchronized(&receiver.cTimestamp, C.bool(value))
111+
112+
return receiver
113+
}
114+
115+
func (receiver *Timestamp) GetSubSecondPrecision() int {
116+
return int(C.Timestamp_getSubsecondPrecision(&receiver.cTimestamp))
117+
}
118+
119+
func (receiver *Timestamp) SetTime(time time.Time) *Timestamp {
120+
C.Timestamp_setTimeInNanoseconds(&receiver.cTimestamp, C.nsSinceEpoch(time.UnixNano()))
121+
return receiver
122+
}

config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
_ "github.com/wendy512/iec61850/libiec61850/inc/mms/inc"
1414
_ "github.com/wendy512/iec61850/libiec61850/inc/mms/inc_private"
1515
_ "github.com/wendy512/iec61850/libiec61850/inc/mms/iso_mms/asn1c"
16+
_ "github.com/wendy512/iec61850/libiec61850/inc/sampled_values"
1617

1718
_ "github.com/wendy512/iec61850/libiec61850/lib/linux64"
1819
_ "github.com/wendy512/iec61850/libiec61850/lib/linux_armv7l"

config_armv7l.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
package iec61850
44

5-
// #cgo CFLAGS: -I./libiec61850/inc/hal/inc -I./libiec61850/inc/common/inc -I./libiec61850/inc/goose -I./libiec61850/inc/iec61850/inc -I./libiec61850/inc/iec61850/inc_private -I./libiec61850/inc/logging -I./libiec61850/inc/mms/inc -I./libiec61850/inc/mms/inc_private -I./libiec61850/inc/mms/iso_mms/asn1c
5+
// #cgo CFLAGS: -I./libiec61850/inc/hal/inc -I./libiec61850/inc/common/inc -I./libiec61850/inc/goose -I./libiec61850/inc/sampled_values -I./libiec61850/inc/iec61850/inc -I./libiec61850/inc/iec61850/inc_private -I./libiec61850/inc/logging -I./libiec61850/inc/mms/inc -I./libiec61850/inc/mms/inc_private -I./libiec61850/inc/mms/iso_mms/asn1c
66
// #cgo LDFLAGS: -static-libgcc -static-libstdc++ -L./libiec61850/lib/linux_armv7l -liec61850 -lpthread
77
import "C"

config_armv8.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
package iec61850
44

5-
// #cgo CFLAGS: -I./libiec61850/inc/hal/inc -I./libiec61850/inc/common/inc -I./libiec61850/inc/goose -I./libiec61850/inc/iec61850/inc -I./libiec61850/inc/iec61850/inc_private -I./libiec61850/inc/logging -I./libiec61850/inc/mms/inc -I./libiec61850/inc/mms/inc_private -I./libiec61850/inc/mms/iso_mms/asn1c
5+
// #cgo CFLAGS: -I./libiec61850/inc/hal/inc -I./libiec61850/inc/common/inc -I./libiec61850/inc/goose -I./libiec61850/inc/sampled_values -I./libiec61850/inc/iec61850/inc -I./libiec61850/inc/iec61850/inc_private -I./libiec61850/inc/logging -I./libiec61850/inc/mms/inc -I./libiec61850/inc/mms/inc_private -I./libiec61850/inc/mms/iso_mms/asn1c
66
// #cgo LDFLAGS: -static-libgcc -static-libstdc++ -L./libiec61850/lib/linux_armv8 -liec61850 -lpthread
77
import "C"

config_darwinarmv8.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
package iec61850
44

5-
// #cgo CFLAGS: -I./libiec61850/inc/hal/inc -I./libiec61850/inc/common/inc -I./libiec61850/inc/goose -I./libiec61850/inc/iec61850/inc -I./libiec61850/inc/iec61850/inc_private -I./libiec61850/inc/logging -I./libiec61850/inc/mms/inc -I./libiec61850/inc/mms/inc_private -I./libiec61850/inc/mms/iso_mms/asn1c
5+
// #cgo CFLAGS: -I./libiec61850/inc/hal/inc -I./libiec61850/inc/common/inc -I./libiec61850/inc/goose -I./libiec61850/inc/sampled_values -I./libiec61850/inc/iec61850/inc -I./libiec61850/inc/iec61850/inc_private -I./libiec61850/inc/logging -I./libiec61850/inc/mms/inc -I./libiec61850/inc/mms/inc_private -I./libiec61850/inc/mms/iso_mms/asn1c
66
// #cgo LDFLAGS: -static-libstdc++ -L./libiec61850/lib/darwin_armv8 -liec61850 -lpthread
77
import "C"

config_linux64.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
package iec61850
44

5-
// #cgo CFLAGS: -I./libiec61850/inc/hal/inc -I./libiec61850/inc/common/inc -I./libiec61850/inc/goose -I./libiec61850/inc/iec61850/inc -I./libiec61850/inc/iec61850/inc_private -I./libiec61850/inc/logging -I./libiec61850/inc/mms/inc -I./libiec61850/inc/mms/inc_private -I./libiec61850/inc/mms/iso_mms/asn1c
5+
// #cgo CFLAGS: -I./libiec61850/inc/hal/inc -I./libiec61850/inc/common/inc -I./libiec61850/inc/goose -I./libiec61850/inc/sampled_values -I./libiec61850/inc/iec61850/inc -I./libiec61850/inc/iec61850/inc_private -I./libiec61850/inc/logging -I./libiec61850/inc/mms/inc -I./libiec61850/inc/mms/inc_private -I./libiec61850/inc/mms/iso_mms/asn1c
66
// #cgo LDFLAGS: -static-libgcc -static-libstdc++ -L./libiec61850/lib/linux64 -liec61850 -lpthread
77
import "C"

config_win64.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
package iec61850
44

5-
// #cgo CFLAGS: -I./libiec61850/inc/hal/inc -I./libiec61850/inc/common/inc -I./libiec61850/inc/goose -I./libiec61850/inc/iec61850/inc -I./libiec61850/inc/iec61850/inc_private -I./libiec61850/inc/logging -I./libiec61850/inc/mms/inc -I./libiec61850/inc/mms/inc_private -I./libiec61850/inc/mms/iso_mms/asn1c
5+
// #cgo CFLAGS: -I./libiec61850/inc/hal/inc -I./libiec61850/inc/common/inc -I./libiec61850/inc/goose -I./libiec61850/inc/sampled_values -I./libiec61850/inc/iec61850/inc -I./libiec61850/inc/iec61850/inc_private -I./libiec61850/inc/logging -I./libiec61850/inc/mms/inc -I./libiec61850/inc/mms/inc_private -I./libiec61850/inc/mms/iso_mms/asn1c
66
// #cgo LDFLAGS: -static-libgcc -static-libstdc++ -L${SRCDIR}/libiec61850/lib/win64 -liec61850 -lhal -lws2_32
77
import "C"

fc.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,18 @@ const (
3333
EX
3434
// CO Control, deprecated but kept here for backward compatibility
3535
CO
36+
// US Unicast SV
37+
US
38+
// MS Multicast SV
39+
MS
3640
// RP Unbuffered Reporting
3741
RP
3842
// BR Buffered Reporting
3943
BR
44+
// LG Log control blocks
45+
LG
46+
// GO Goose control blocks
47+
GO
4048
// ALL All FCs - wildcard value
4149
ALL FC = 99
4250
NONE FC = -1

goose_publisher.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build linux && amd64
2+
13
package iec61850
24

35
/*

goose_receiver.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build linux && amd64
2+
13
package iec61850
24

35
/*
@@ -26,6 +28,7 @@ type (
2628
GooseReceiver struct {
2729
noCopy struct{}
2830
gooseReceiver *C.struct_sGooseReceiver
31+
refs map[GooseCallbackHandlerID]struct{}
2932
}
3033
)
3134

@@ -69,6 +72,7 @@ func cgoReportCallbackBridgeDispatcher(_ *C.struct_sGooseSubscriber, parameter u
6972
func NewGooseReceiver() *GooseReceiver {
7073
return &GooseReceiver{
7174
gooseReceiver: C.GooseReceiver_create(),
75+
refs: make(map[GooseCallbackHandlerID]struct{}),
7276
}
7377
}
7478

@@ -83,7 +87,7 @@ func (receiver *GooseReceiver) AddSubscriber(subscriber *GooseSubscriber) *Goose
8387
handler: subscriber.Conf.ReportHandler,
8488
subscriber: subscriber,
8589
}
86-
90+
receiver.refs[subscriber.HandlerID] = struct{}{}
8791
C.simple_goose_subscriber_set_listener(
8892
subscriber.subscriber,
8993
C.uintptr_t(subscriber.HandlerID),
@@ -99,6 +103,7 @@ func (receiver *GooseReceiver) RemoveSubscriber(subscriber *GooseSubscriber) *Go
99103

100104
C.GooseReceiver_removeSubscriber(receiver.gooseReceiver, subscriber.subscriber)
101105
delete(gooseCallbackLocker.callbackRefs, subscriber.HandlerID)
106+
delete(receiver.refs, subscriber.HandlerID)
102107

103108
return receiver
104109
}
@@ -136,5 +141,12 @@ func (receiver *GooseReceiver) Stop() *GooseReceiver {
136141
}
137142

138143
func (receiver *GooseReceiver) Destroy() {
144+
gooseCallbackLocker.Lock()
145+
defer gooseCallbackLocker.Unlock()
146+
for id := range receiver.refs {
147+
delete(gooseCallbackLocker.callbackRefs, id)
148+
}
139149
C.GooseReceiver_destroy(receiver.gooseReceiver)
150+
receiver.refs = nil
151+
receiver.gooseReceiver = nil
140152
}

0 commit comments

Comments
 (0)