-
Notifications
You must be signed in to change notification settings - Fork 38
/
mms.go
164 lines (152 loc) · 3.44 KB
/
mms.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
package iec61850
// #include <iec61850_client.h>
import "C"
import "unsafe"
import "github.com/spf13/cast"
func toMmsValue(mmsType MmsType, value interface{}) (*C.MmsValue, error) {
var (
mmsValue *C.MmsValue
err error
)
switch mmsType {
case Boolean:
mmsValue, err = toBoolMmsValue(value)
if err != nil {
return nil, err
}
case String:
mmsValue, err = toStringMmsValue(value)
if err != nil {
return nil, err
}
case Float:
mmsValue, err = toFloatMmsValue(value)
if err != nil {
return nil, err
}
case Uint8:
mmsValue, err = toUint8MmsValue(value)
if err != nil {
return nil, err
}
case Uint16:
mmsValue, err = toUint16MmsValue(value)
if err != nil {
return nil, err
}
case Uint32:
mmsValue, err = toUint32MmsValue(value)
if err != nil {
return nil, err
}
case Int8:
mmsValue, err = toInt8MmsValue(value)
if err != nil {
return nil, err
}
case Int16:
mmsValue, err = toInt16MmsValue(value)
if err != nil {
return nil, err
}
case Int32:
mmsValue, err = toInt32MmsValue(value)
if err != nil {
return nil, err
}
case Int64:
mmsValue, err = toInt64MmsValue(value)
if err != nil {
return nil, err
}
default:
return nil, UnSupportedOperation
}
return mmsValue, nil
}
func toInt64MmsValue(value interface{}) (*C.MmsValue, error) {
v, err := cast.ToInt64E(value)
if err != nil {
return nil, err
}
// int64
return C.MmsValue_newIntegerFromInt64(C.int64_t(v)), nil
}
func toInt32MmsValue(value interface{}) (*C.MmsValue, error) {
v, err := cast.ToInt32E(value)
if err != nil {
return nil, err
}
// int32
return C.MmsValue_newIntegerFromInt32(C.int32_t(v)), nil
}
func toInt16MmsValue(value interface{}) (*C.MmsValue, error) {
v, err := cast.ToInt16E(value)
if err != nil {
return nil, err
}
// int16
return C.MmsValue_newIntegerFromInt16(C.int16_t(v)), nil
}
func toInt8MmsValue(value interface{}) (*C.MmsValue, error) {
v, err := cast.ToInt8E(value)
if err != nil {
return nil, err
}
// int8
return C.MmsValue_newIntegerFromInt8(C.int8_t(v)), nil
}
func toUint32MmsValue(value interface{}) (*C.MmsValue, error) {
v, err := cast.ToUint32E(value)
if err != nil {
return nil, err
}
// uint32
mmsValue := C.MmsValue_newUnsigned(C.int(32))
C.MmsValue_setUint32(mmsValue, C.uint32_t(v))
return mmsValue, nil
}
func toUint16MmsValue(value interface{}) (*C.MmsValue, error) {
v, err := cast.ToUint16E(value)
if err != nil {
return nil, err
}
// uint16
mmsValue := C.MmsValue_newUnsigned(C.int(16))
C.MmsValue_setUint16(mmsValue, C.uint16_t(v))
return mmsValue, nil
}
func toUint8MmsValue(value interface{}) (*C.MmsValue, error) {
v, err := cast.ToUint8E(value)
if err != nil {
return nil, err
}
// uint8
mmsValue := C.MmsValue_newUnsigned(C.int(8))
C.MmsValue_setUint8(mmsValue, C.uint8_t(v))
return mmsValue, nil
}
func toFloatMmsValue(value interface{}) (*C.MmsValue, error) {
v, err := cast.ToFloat32E(value)
if err != nil {
return nil, err
}
return C.MmsValue_newFloat(C.float(v)), nil
}
func toBoolMmsValue(value interface{}) (*C.MmsValue, error) {
v, err := cast.ToBoolE(value)
if err != nil {
return nil, err
}
return C.MmsValue_newBoolean(C.bool(v)), nil
}
func toStringMmsValue(value interface{}) (*C.MmsValue, error) {
v, err := cast.ToStringE(value)
if err != nil {
return nil, err
}
stringValue := C.CString(v)
defer C.free(unsafe.Pointer(stringValue))
mmsValue := C.MmsValue_newMmsString(stringValue)
return mmsValue, nil
}