Skip to content

Commit 880258d

Browse files
committed
增加iec61850 server相关接口
1 parent 18ee91f commit 880258d

23 files changed

+1632
-48
lines changed

client.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ type Client struct {
1212
connected *atomic.Bool
1313
}
1414

15+
// Settings 连接配置
1516
type Settings struct {
1617
Host string
1718
Port int
18-
ConnectTimeout uint
19-
RequestTimeout uint
19+
ConnectTimeout uint // 连接超时配置,单位:毫秒
20+
RequestTimeout uint // 请求超时配置,单位:毫秒
2021
}
2122

2223
func NewSettings() *Settings {
@@ -28,6 +29,7 @@ func NewSettings() *Settings {
2829
}
2930
}
3031

32+
// NewClient 创建客户端实例
3133
func NewClient(settings *Settings) (*Client, error) {
3234
conn, clientErr := connect(settings)
3335
if err := GetIedClientError(clientErr); err != nil {
@@ -43,6 +45,7 @@ func NewClient(settings *Settings) (*Client, error) {
4345
return connection, nil
4446
}
4547

48+
// Write 写单个属性值,不支持Structure
4649
func (c *Client) Write(objectRef string, fc FC, value interface{}) error {
4750
mmsType, err := c.GetVariableSpecType(objectRef, fc)
4851
if err != nil {
@@ -143,7 +146,7 @@ func (c *Client) ReadString(objectRef string, fc FC) (string, error) {
143146
return C.GoString(value), nil
144147
}
145148

146-
// Read 读取数据
149+
// Read 读取属性数据
147150
func (c *Client) Read(objectRef string, fc FC) (interface{}, error) {
148151
var clientError C.IedClientError
149152
cObjectRef := C.CString(objectRef)
@@ -159,6 +162,7 @@ func (c *Client) Read(objectRef string, fc FC) (interface{}, error) {
159162
return c.toGoValue(mmsValue, mmsType), nil
160163
}
161164

165+
// ReadDataSet 读取DataSet
162166
func (c *Client) ReadDataSet(objectRef string) ([]*MmsValue, error) {
163167
cObjectRef := C.CString(objectRef)
164168
defer C.free(unsafe.Pointer(cObjectRef))
@@ -254,7 +258,7 @@ func (c *Client) toGoValue(mmsValue *C.MmsValue, mmsType MmsType) interface{} {
254258
size := uint16(C.MmsValue_getOctetStringSize(mmsValue))
255259
bytes := make([]byte, size)
256260
for i := 0; i < int(size); i++ {
257-
bytes[i] = uint8(C.getOctetStringOctet(mmsValue, C.int(i)))
261+
bytes[i] = uint8(C.MmsValue_getOctetStringOctet(mmsValue, C.int(i)))
258262
}
259263
value = bytes
260264
case BinaryTime:
@@ -284,6 +288,15 @@ func (c *Client) toGoStructure(mmsValue *C.MmsValue, mmsType MmsType) []*MmsValu
284288
}
285289
}
286290

291+
func (c *Client) getSubElementValue(sgcbVal *C.MmsValue, sgcbVarSpec *C.MmsVariableSpecification, name string) interface{} {
292+
mmsPath := C.CString(name)
293+
defer C.free(unsafe.Pointer(mmsPath))
294+
mmsValue := C.MmsValue_getSubElement(sgcbVal, sgcbVarSpec, mmsPath)
295+
defer C.MmsValue_delete(mmsValue)
296+
return c.toGoValue(mmsValue, MmsType(C.MmsValue_getType(mmsValue)))
297+
}
298+
299+
// connect 建立连接
287300
func connect(settings *Settings) (C.IedConnection, C.IedClientError) {
288301
conn := C.IedConnection_create()
289302
C.IedConnection_setConnectTimeout(conn, C.uint(settings.ConnectTimeout))
@@ -292,7 +305,6 @@ func connect(settings *Settings) (C.IedConnection, C.IedClientError) {
292305
// 释放内存
293306
defer C.free(unsafe.Pointer(host))
294307
var err C.IedClientError
295-
// 建立连接
296308
C.IedConnection_connect(conn, &err, host, C.int(settings.Port))
297309
return conn, err
298310
}

client_control.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,27 @@ package iec61850
44
import "C"
55
import "unsafe"
66

7-
type commandTerminationHandler func(parameter unsafe.Pointer, control C.ControlObjectClient)
8-
9-
// DirectControl 直接控制
10-
func (c *Client) DirectControl(objectRef string, value bool) error {
11-
return c.control(objectRef, value, false, true, false, nil)
7+
// ControlForDirectWithNormalSecurity 控制模式 1[direct-with-normal-security]
8+
func (c *Client) ControlForDirectWithNormalSecurity(objectRef string, value bool) error {
9+
return c.control(objectRef, value, false, true, false)
1210
}
1311

14-
// DirectControlSelect 直接控制之前select
15-
func (c *Client) DirectControlSelect(objectRef string, value bool) error {
16-
return c.control(objectRef, value, true, false, false, nil)
12+
// ControlForSboWithNormalSecurity 控制模式 2[sbo-with-normal-security]
13+
func (c *Client) ControlForSboWithNormalSecurity(objectRef string, value bool) error {
14+
return c.control(objectRef, value, true, false, false)
1715
}
1816

19-
// DirectControlWithEnhancedSecurity 增强安全直接控制
20-
func (c *Client) DirectControlWithEnhancedSecurity(objectRef string, value bool) error {
21-
return c.control(objectRef, value, false, false, true, func(void unsafe.Pointer, control C.ControlObjectClient) {})
17+
// ControlForDirectWithEnhancedSecurity 控制模式 3[direct-with-enhanced-security]
18+
func (c *Client) ControlForDirectWithEnhancedSecurity(objectRef string, value bool) error {
19+
return c.control(objectRef, value, false, false, true)
2220
}
2321

24-
// DirectControlSelectWithEnhancedSecurity 增强安全直接控制之前select
25-
func (c *Client) DirectControlSelectWithEnhancedSecurity(objectRef string, value bool) error {
26-
return c.control(objectRef, value, true, false, true, nil)
22+
// ControlForSboWithEnhancedSecurity 控制模式 4[sbo-with-enhanced-security]
23+
func (c *Client) ControlForSboWithEnhancedSecurity(objectRef string, value bool) error {
24+
return c.control(objectRef, value, true, false, true)
2725
}
2826

29-
// control for FC=CO
30-
func (c *Client) control(objectRef string, value, _select, direct, enhanced bool, handler commandTerminationHandler) error {
27+
func (c *Client) control(objectRef string, value, _select, direct, enhanced bool) error {
3128
cObjectRef := C.CString(objectRef)
3229
defer C.free(unsafe.Pointer(cObjectRef))
3330

@@ -44,7 +41,7 @@ func (c *Client) control(objectRef string, value, _select, direct, enhanced bool
4441

4542
// Direct control with enhanced security
4643
if enhanced {
47-
C.ControlObjectClient_setCommandTerminationHandler(control, C.CommandTerminationHandler(handler), nil)
44+
C.ControlObjectClient_setCommandTerminationHandler(control, nil, nil)
4845
}
4946
ctlVal := C.MmsValue_newBoolean(C.bool(value))
5047
defer C.MmsValue_delete(ctlVal)
@@ -58,9 +55,7 @@ func (c *Client) control(objectRef string, value, _select, direct, enhanced bool
5855
if direct {
5956
C.ControlObjectClient_setOrigin(control, nil, 3)
6057
}
61-
if bool(C.ControlObjectClient_operate(control, ctlVal, 0)) {
62-
return nil
63-
} else {
64-
return ControlObjectFail
65-
}
58+
59+
C.ControlObjectClient_operate(control, ctlVal, 0)
60+
return nil
6661
}

client_sg.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package iec61850
33
// #include <iec61850_client.h>
44
import "C"
55
import (
6+
"fmt"
67
"github.com/spf13/cast"
78
"unsafe"
89
)
910

1011
const (
11-
ActDA = "/LLN0.SGCB.ActSG"
12-
EditDA = "/LLN0.SGCB.EditSG"
13-
CnfDA = "/LLN0.SGCB.CnfEdit"
12+
ActDA = "%s/%s.SGCB.ActSG"
13+
EditDA = "%s/%s.SGCB.EditSG"
14+
CnfDA = "%s/%s.SGCB.CnfEdit"
1415
)
1516

1617
type SettingGroup struct {
@@ -20,14 +21,15 @@ type SettingGroup struct {
2021
CnfEdit bool
2122
}
2223

23-
func (c *Client) WriteSG(ld, objectRef string, fc FC, actSG int, value interface{}) error {
24+
// WriteSG 写入SettingGroup
25+
func (c *Client) WriteSG(ld, ln, objectRef string, fc FC, actSG int, value interface{}) error {
2426
// Set active setting group
25-
if err := c.Write(ld+ActDA, SP, actSG); err != nil {
27+
if err := c.Write(fmt.Sprintf(ActDA, ld, ln), SP, actSG); err != nil {
2628
return err
2729
}
2830

2931
// Set edit setting group
30-
if err := c.Write(ld+EditDA, SP, actSG); err != nil {
32+
if err := c.Write(fmt.Sprintf(EditDA, ld, ln), SP, actSG); err != nil {
3133
return err
3234
}
3335

@@ -37,15 +39,16 @@ func (c *Client) WriteSG(ld, objectRef string, fc FC, actSG int, value interface
3739
}
3840

3941
// Confirm new setting group values
40-
if err := c.Write(ld+CnfDA, SP, true); err != nil {
42+
if err := c.Write(fmt.Sprintf(CnfDA, ld, ln), SP, true); err != nil {
4143
return err
4244
}
4345
return nil
4446
}
4547

46-
func (c *Client) GetSG(ld string) (*SettingGroup, error) {
48+
// GetSG 获取SettingGroup
49+
func (c *Client) GetSG(objectRef string) (*SettingGroup, error) {
4750
var clientError C.IedClientError
48-
cObjectRef := C.CString(ld + "/LLN0.SGCB")
51+
cObjectRef := C.CString(objectRef)
4952
defer C.free(unsafe.Pointer(cObjectRef))
5053

5154
// 获取类型
@@ -62,10 +65,10 @@ func (c *Client) GetSG(ld string) (*SettingGroup, error) {
6265
}
6366
defer C.MmsValue_delete(sgcbVal)
6467

65-
numOfSGValue := c.getSGSubElementValue(sgcbVal, sgcbVarSpec, "NumOfSG")
66-
actSGValue := c.getSGSubElementValue(sgcbVal, sgcbVarSpec, "ActSG")
67-
editSGValue := c.getSGSubElementValue(sgcbVal, sgcbVarSpec, "EditSG")
68-
cnfEditValue := c.getSGSubElementValue(sgcbVal, sgcbVarSpec, "CnfEdit")
68+
numOfSGValue := c.getSubElementValue(sgcbVal, sgcbVarSpec, "NumOfSG")
69+
actSGValue := c.getSubElementValue(sgcbVal, sgcbVarSpec, "ActSG")
70+
editSGValue := c.getSubElementValue(sgcbVal, sgcbVarSpec, "EditSG")
71+
cnfEditValue := c.getSubElementValue(sgcbVal, sgcbVarSpec, "CnfEdit")
6972

7073
sg := &SettingGroup{
7174
NumOfSG: cast.ToInt(numOfSGValue),
@@ -75,11 +78,3 @@ func (c *Client) GetSG(ld string) (*SettingGroup, error) {
7578
}
7679
return sg, nil
7780
}
78-
79-
func (c *Client) getSGSubElementValue(sgcbVal *C.MmsValue, sgcbVarSpec *C.MmsVariableSpecification, name string) interface{} {
80-
mmsPath := C.CString(name)
81-
defer C.free(unsafe.Pointer(mmsPath))
82-
mmsValue := C.MmsValue_getSubElement(sgcbVal, sgcbVarSpec, mmsPath)
83-
defer C.MmsValue_delete(mmsValue)
84-
return c.toGoValue(mmsValue, MmsType(C.MmsValue_getType(mmsValue)))
85-
}

config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ import (
1616

1717
_ "github.com/wendy512/iec61850/libiec61850/lib/linux64"
1818
_ "github.com/wendy512/iec61850/libiec61850/lib/linux_armv7l"
19+
_ "github.com/wendy512/iec61850/libiec61850/lib/linux_armv8"
1920
_ "github.com/wendy512/iec61850/libiec61850/lib/win64"
2021
)

config_armv8.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build linux && arm64
2+
3+
package iec61850
4+
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
6+
// #cgo LDFLAGS: -static-libgcc -static-libstdc++ -L./libiec61850/lib/linux_armv8 -liec61850 -lhal
7+
import "C"

libiec61850/lib/linux64/libiec61850.a

-1.47 MB
Binary file not shown.

libiec61850/lib/linux_armv7l/libhal.a

-27.1 KB
Binary file not shown.
2.42 MB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package linux_armv8
5.72 MB
Binary file not shown.

0 commit comments

Comments
 (0)