-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget.go
275 lines (228 loc) · 6.7 KB
/
target.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
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
package m1
import (
"crypto/md5"
"fmt"
"net"
"time"
"github.com/ysmilda/m1-go/internals/m1client"
"github.com/ysmilda/m1-go/modules/mod"
"github.com/ysmilda/m1-go/modules/msys"
"github.com/ysmilda/m1-go/modules/res"
"github.com/ysmilda/m1-go/modules/smi"
"github.com/ysmilda/m1-go/modules/svi"
"github.com/ysmilda/m1-go/modules/sysinfo"
)
const (
toolName = "m1-go"
)
// Target represents a target device.
// It's methods are convenience methods around the various modules on the target.
// For more advanced usage, the modules can be used directly via the struct members.
// Make sure to follow the documentation of the modules when using them directly.
// The target should be closed after usage.
type Target struct {
Res *res.Procedures
SysInfo *sysinfo.Procedures
Mod *mod.Procedures
SVI *svi.Procedures
SMI *smi.Procedures
client *m1client.Client
sessionTimeout int32
sessionLifetime int32
loginChecker bool
loginRequired bool
msysVersion msys.Version
}
// NewTarget creates a new target for the given IP address and timeout.
func NewTarget(ip net.IP, timeout time.Duration) (*Target, error) {
client := m1client.NewClient(ip, timeout)
t := &Target{
client: client,
}
// Setup the connection to the target.
err := t.connect()
if err != nil {
return nil, err
}
infModuleNumber, err := t.Res.GetModuleNumber(res.ModuleNumberCall{
Name: "INFO",
})
if err != nil {
return nil, err
}
t.Res = res.NewProcedures(client)
t.Mod = mod.NewProcedures(client)
t.SVI = svi.NewProcedures(client)
t.SMI = smi.NewProcedures(client)
t.SysInfo = sysinfo.NewProcedures(client, infModuleNumber.ModuleNumber)
return t, nil
}
// Close closes the target connection.
// After closing the target, the target should not be used anymore.
func (t *Target) Close() error {
_, err := t.Res.CloseConnection(res.CloseCall{})
if err != nil {
return err
}
return t.client.Close()
}
// GetClient returns the client used to connect to the target.
func (t *Target) GetClient() *m1client.Client {
return t.client
}
// Login logs in to the target with the given user and password.
//
// This is a helper function that calls the Res.Login or Res.Login2 procedure depending on the target version.
// When using a custom module for authentication use the Res.ExtLogin and Res.ExtLogout functions.
//
// If the login is not required, this function does nothing.
func (t *Target) Login(user, password string) error {
if !t.loginRequired {
return nil
}
// If the version is newer than 3.70.8-release we need to use the login2 procedure.
if t.msysVersion.Compare(msys.Version{Major: 3, Minor: 70, Patch: 8, ReleaseType: msys.Release}) >= 0 {
_, err := t.login2(user, password, t.loginChecker)
return err
}
_, err := t.login(user, password, t.loginChecker)
return err
}
func (t *Target) login(user, password string, loginChecker bool) (*res.Login, error) {
c := res.LoginCall{
SystemInfoCall: res.SystemInfoCall{
Toolname: toolName,
},
Username: user,
Password: password,
}
if !loginChecker {
hash := md5.Sum([]byte(password))
c.Password = string(hash[:])
}
reply, err := t.Res.Login(c)
if err != nil {
return nil, err
}
t.client.SetAuth(reply.Auth, reply.AuthLen)
return &reply.Login, nil
}
func (t *Target) login2(
user, password string, loginChecker bool,
) (*res.Login2, error) {
c := res.Login2Call{
SystemInfoCall: res.SystemInfoCall{
Toolname: toolName,
},
Username: user,
Password: password,
}
if !loginChecker {
hash := md5.Sum([]byte(password))
c.Password = string(hash[:])
}
reply, err := t.Res.Login2(c)
if err != nil {
return nil, err
}
t.client.SetAuth(reply.Auth, reply.AuthLen)
return &reply.Login2, nil
}
// Logout logs out from the target.
func (t *Target) Logout() error {
if !t.loginRequired {
return nil
}
_, err := t.Res.Logout(res.LogoutCall{})
return err
}
// ListVariables returns a list of all variables of the module.
func (t *Target) ListVariables(module string) ([]Variable, error) {
number, err := t.Res.GetModuleNumber(res.ModuleNumberCall{
Name: module,
})
if err != nil {
return nil, err
}
version425 := msys.Version{Major: 4, Minor: 25, Patch: 0, ReleaseType: msys.Release}
if t.msysVersion.Compare(version425) >= 0 {
return t.listVariables2(number.ModuleNumber, module)
} else {
return t.listVariables(number.ModuleNumber)
}
}
// 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 (t *Target) listVariables2(module res.ModuleNumber, name string) ([]Variable, error) {
reply, err := t.SVI.ListExtendedProcessValueInfo(module, &svi.ListExtendedProcessValueInfoCall{
GetSubprocessValues: true,
PathLength: 1,
Path: "", // Start from the root.
}, 1000)
if err != nil {
return nil, err
}
path := name
result := []Variable{}
for _, value := range reply {
if value.Flag == svi.FlagTypeDirectory {
path = fmt.Sprintf("%s/%s", name, value.Name)
continue
}
result = append(result, Variable{
Name: fmt.Sprintf("%s/%s", path, value.Name),
Variable: svi.Variable{
Format: value.Format,
Length: value.Length,
},
})
}
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 (t *Target) listVariables(module res.ModuleNumber) ([]Variable, error) {
reply, err := t.SVI.ListProcessValueInfo(module, &svi.ListProcessValueInfoCall{}, 29)
if err != nil {
return nil, err
}
result := []Variable{}
for _, value := range reply {
result = append(result, Variable{
Name: "RES/" + value.Name,
Variable: svi.Variable{
Format: value.Format,
Length: value.Length,
},
})
}
return result, nil
}
// connect connects to the target and initializes the target.
func (t *Target) connect() error {
info, err := t.Res.GetSystemInfo(res.SystemInfoCall{
Toolname: toolName,
})
if err != nil {
return err
}
t.loginChecker = info.LoginChecker
t.loginRequired = info.LoginRequired
t.msysVersion = info.MSysVersion
// If the version is newer than 3.95.0-release we need to open the RES module.
if info.MSysVersion.Compare(msys.Version{Major: 3, Minor: 95, Patch: 0, ReleaseType: msys.Release}) >= 0 {
reply, err := t.Res.OpenConnection(res.OpenCall{
RequestedSMISize: 0x7FFFFFFF,
})
if err != nil {
return fmt.Errorf("failed to open: %w", err)
}
t.sessionTimeout = reply.SessionTimeout
t.sessionLifetime = reply.SessionLifetime
t.client.SetMaximalCallLength(reply.SMIMessageSize)
t.client.SetAuth(reply.Auth, reply.AuthLen)
}
return nil
}