-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.go
208 lines (174 loc) · 5.23 KB
/
module.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
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
package m1
import (
"fmt"
"github.com/ysmilda/m1-go/pkg/rpc"
)
const (
_SVI_Procedure_GetVariableInfo = 10014
_SVI_Procedure_GetServerInfo = 10016
_SVI_VariableInfoExtendedCall = 0x7575abcd
_SVI_FlagDirectory = 0x0000
)
// Module wraps a generic module of the M1 controller.
type Module struct {
client *client
info ModuleInfo
name string
msysVersion Version
}
// newModule creates a new module with the given module number.
func newModule(client *client, name string, info ModuleInfo, msysVersion Version) (*Module, error) {
m := &Module{
client: client,
name: name,
info: info,
msysVersion: msysVersion,
}
err := client.addConnection(info)
if err != nil {
return nil, fmt.Errorf("failed to add connection: %w", err)
}
return m, nil
}
// GetSVIServerInfo returns the server information of the SVI server.
func (m *Module) GetSVIServerInfo() (*SVIServerInfo, error) {
buf, err := rpc.Call(
m.client.getConnection(m.info),
rpc.Header{
Module: m.info.ModuleNumber,
Version: _RPC_VersionDefault,
Procedure: _SVI_Procedure_GetServerInfo,
Auth: m.client.auth,
},
)
if err != nil {
return nil, fmt.Errorf("failed to get SVI server info: %w", err)
}
reply := &SVIServerInfo{}
returnCode := reply.parse(buf)
if err := parseReturnCode(returnCode); err != nil {
return nil, fmt.Errorf("failed to get SVI server info: %w", err)
}
return reply, nil
}
// GetVariableCount returns the number of variables of the module.
func (m *Module) GetVariableCount() (uint32, error) {
info, err := m.GetSVIServerInfo()
if err != nil {
return 0, fmt.Errorf("failed to get variable count: %w", err)
}
return info.NumberOfVariables, nil
}
// ListVariables returns a list of all variables of the module.
// The returned variables are not initialized. To initialize them, use the VHD module on the target.
func (m *Module) ListVariables() ([]Variable, error) {
version425 := Version{Major: 4, Minor: 25, Patch: 0, ReleaseType: "release"}
if m.msysVersion.Compare(version425) >= 0 {
return m.listVariables2()
} else {
return m.listVariables()
}
}
// listVariables2 returns a list of all variables of the module.
// This is the preferred implementation for newer versions of the M1 controller.
// It supports a maximum of 255 characters for the variable name.
func (m *Module) listVariables2() ([]Variable, error) {
const variablesPerCall = uint32(1000)
index := uint32(0)
path := m.name
result := make([]Variable, 0)
for {
buf, err := rpc.Call(
m.client.getConnection(m.info),
rpc.Header{
Module: m.info.ModuleNumber,
Version: _RPC_VersionDefault,
Procedure: _SVI_Procedure_GetVariableInfo,
Auth: m.client.auth,
},
uint32(_SVI_VariableInfoExtendedCall), variablesPerCall, index,
byte(1), rpc.NewSpare(11), uint32(1), rpc.NewString("", 1),
)
if err != nil {
return nil, fmt.Errorf("failed to get variables: %w", err)
}
returnCode, _ := buf.LittleEndian.ReadUint32()
if err := parseReturnCode(returnCode); err != nil {
return nil, err
}
buf.Skip(4) // Number of PV (old, not used)
index, _ = buf.LittleEndian.ReadUint32() // Next index
buf.Skip(3 * 4) // Spare
count, _ := buf.LittleEndian.ReadUint32() // Number of returned variables
for range count {
buf.Align4()
flags, _ := buf.LittleEndian.ReadUint16()
buf.Skip(2)
format, _ := buf.LittleEndian.ReadUint16()
length, _ := buf.LittleEndian.ReadUint16()
nameLength, _ := buf.LittleEndian.ReadUint32()
name, _ := buf.ReadString(int(nameLength) + 1)
if flags == _SVI_FlagDirectory {
path = fmt.Sprintf("%s/%s", m.name, name)
continue
} else {
name = fmt.Sprintf("%s/%s", path, name)
}
result = append(result, Variable{
Name: name,
Format: format,
Length: length,
})
}
if index == 0 {
break
}
}
return result, nil
}
// listVariables returns a list of all variables of the module.
// This is a fallback implementation for older versions of the M1 controller.
// It supports a maximum of 64 characters for the variable name.
func (m *Module) listVariables() ([]Variable, error) {
const variablesPerCall = uint32(29)
index := uint32(0)
result := make([]Variable, 0)
for {
buf, err := rpc.Call(
m.client.getConnection(m.info),
rpc.Header{
Module: m.info.ModuleNumber,
Version: _RPC_VersionDefault,
Procedure: _SVI_Procedure_GetVariableInfo,
Auth: m.client.auth,
},
index, variablesPerCall,
)
if err != nil {
return nil, fmt.Errorf("failed to get variables: %w", err)
}
returnCode, _ := buf.LittleEndian.ReadUint32()
if returnCode == (_SOURCE_SVI | _ERROR_FAILED) {
// This is how the system announces that there are no more variables.
break
} else if err := parseReturnCode(returnCode); err != nil {
return nil, err
}
count, _ := buf.LittleEndian.ReadUint32()
for range count {
name, _ := buf.ReadString(_SVI_NameLength)
format, _ := buf.LittleEndian.ReadUint16()
length, _ := buf.LittleEndian.ReadUint16()
result = append(result, Variable{
Name: "RES/" + name,
Format: format,
Length: length,
})
}
if count < variablesPerCall {
break
}
index += variablesPerCall
}
return result, nil
}