-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGXReplyData.go
More file actions
400 lines (332 loc) · 10.8 KB
/
GXReplyData.go
File metadata and controls
400 lines (332 loc) · 10.8 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package dlms
//
// --------------------------------------------------------------------------
// Gurux Ltd
//
//
//
// Filename: $HeadURL$
//
// Version: $Revision$,
// $Date$
// $Author$
//
// Copyright (c) Gurux Ltd
//
//---------------------------------------------------------------------------
//
// DESCRIPTION
//
// This file is a part of Gurux Device Framework.
//
// Gurux Device Framework is Open Source software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; version 2 of the License.
// Gurux Device Framework is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// More information of Gurux products: https://www.gurux.org
//
// This code is licensed under the GNU General Public License v2.
// Full text may be retrieved at http://www.gnu.org/licenses/gpl-2.0.txt
//---------------------------------------------------------------------------
import (
"fmt"
"time"
"github.com/Gurux/gxdlms-go/enums"
"github.com/Gurux/gxdlms-go/settings"
"github.com/Gurux/gxdlms-go/types"
)
// GXReplyData contains information from received reply data.
type GXReplyData struct {
// Xml settings. This is used only on xml parser.
xml *settings.GXDLMSTranslatorStructure
// Broadcast indicates if data is sent as a broadcast or unicast.
Broadcast bool
// DataType is the type of the received data.
DataType enums.DataType
// Value is the read value.
Value any
// ReadPosition is the last read position. This is used in peek to solve how far data is read.
ReadPosition int
// PacketLength is the length of the packet.
PacketLength int
// RawPdu indicates PDU is not parsed and it's returned as it is.
RawPdu bool
// command is the received command.
command enums.Command
// commandType is the received command type.
commandType byte
// cipheredCommand is the received ciphered command.
cipheredCommand enums.Command
// Data is the received data.
Data *types.GXByteBuffer
// isComplete indicates if the frame is complete.
isComplete bool
// frameId is the HDLC frame ID.
frameId byte
// invokeId is the received invoke ID.
invokeId uint32
// systemTitle is the system title of the received PDU.
// System title is set when ciphered notify packet is received.
systemTitle []byte
// Error is the received error code.
Error int
// EmptyResponses is true if there are empty frames or blocks.
EmptyResponses enums.RequestTypes
// TotalCount is the expected count of elements in the array.
TotalCount int
// CipherIndex is the position where data is decrypted or GBT is read.
CipherIndex int
// Time is the data notification date time.
Time time.Time
// BlockNumber is the GBT block number.
BlockNumber uint16
// BlockNumberAck is the GBT block number ACK.
BlockNumberAck uint16
// streaming indicates if GBT streaming is in use.
streaming bool
// gbtWindowSize is the GBT Window size.
gbtWindowSize byte
// hdlcStreaming indicates if HDLC streaming is in progress.
hdlcStreaming bool
// TargetAddress is the client address of the notification message.
// Notification message sets this. This is also used with XML parser.
TargetAddress int
// SourceAddress is the server address of the notification message.
// Notification message sets this. This is also used with XML parser.
SourceAddress int
// Gateway information.
Gateway *settings.GXDLMSGateway
// PrimeDc is PRIME data concentrator notification information.
PrimeDc *GXDLMSPrimeDataConcentrator
// moreData indicates if more data is available. Returns None if more data is not available or Frame or Block type.
moreData enums.RequestTypes
// Peek indicates if value is try to peek.
Peek bool
}
// NewGXReplyData creates a new GXReplyData with default values.
func NewGXReplyData() *GXReplyData {
r := &GXReplyData{
Data: types.NewGXByteBuffer(),
}
r.Clear()
return r
}
// newGXReplyDataFull creates a new GXReplyData with specified values.
func newGXReplyDataFull(more enums.RequestTypes, cmd enums.Command, buff *types.GXByteBuffer, complete bool, errorCode byte) *GXReplyData {
r := &GXReplyData{
Data: types.NewGXByteBuffer(),
}
r.Clear()
r.moreData = more
r.command = cmd
r.Data = buff
r.isComplete = complete
r.Error = int(errorCode)
return r
}
// Xml returns the XML settings. This is used only on xml parser.
func (r *GXReplyData) Xml() *settings.GXDLMSTranslatorStructure {
return r.xml
}
// SetXml sets the XML settings. This is used only on xml parser.
func (r *GXReplyData) SetXml(value *settings.GXDLMSTranslatorStructure) {
r.xml = value
}
// Command returns the received command.
func (r *GXReplyData) Command() enums.Command {
return r.command
}
// SetCommand sets the received command.
func (r *GXReplyData) SetCommand(value enums.Command) {
r.command = value
}
// CommandType returns the received command type.
func (r *GXReplyData) CommandType() byte {
return r.commandType
}
// SetCommandType sets the received command type.
func (r *GXReplyData) SetCommandType(value byte) {
r.commandType = value
}
// CipheredCommand returns the received ciphered command.
func (r *GXReplyData) CipheredCommand() enums.Command {
return r.cipheredCommand
}
// SetCipheredCommand sets the received ciphered command.
func (r *GXReplyData) SetCipheredCommand(value enums.Command) {
r.cipheredCommand = value
}
// IsComplete returns true if the frame is complete.
func (r *GXReplyData) IsComplete() bool {
return r.isComplete
}
// SetIsComplete sets whether the frame is complete.
func (r *GXReplyData) SetIsComplete(value bool) {
r.isComplete = value
}
// FrameId returns the HDLC frame ID.
func (r *GXReplyData) FrameId() byte {
return r.frameId
}
// SetFrameId sets the HDLC frame ID.
func (r *GXReplyData) SetFrameId(value byte) {
r.frameId = value
}
// GetInvokeId returns the received invoke ID.
func (r *GXReplyData) InvokeId() uint32 {
return r.invokeId
}
// SetInvokeId sets the received invoke ID.
func (r *GXReplyData) SetInvokeId(value uint32) {
r.invokeId = value
}
// SystemTitle returns the system title of the received PDU.
func (r *GXReplyData) SystemTitle() []byte {
return r.systemTitle
}
// SetSystemTitle sets the system title of the received PDU.
func (r *GXReplyData) SetSystemTitle(value []byte) {
r.systemTitle = value
}
// Streaming returns true if GBT streaming is in use.
func (r *GXReplyData) Streaming() bool {
return r.streaming
}
// SetStreaming sets whether GBT streaming is in use.
func (r *GXReplyData) SetStreaming(value bool) {
r.streaming = value
}
// GbtWindowSize returns the GBT Window size.
func (r *GXReplyData) GbtWindowSize() byte {
return r.gbtWindowSize
}
// SetGbtWindowSize sets the GBT Window size.
func (r *GXReplyData) SetGbtWindowSize(value byte) {
r.gbtWindowSize = value
}
// HdlcStreaming returns true if HDLC streaming is in progress.
func (r *GXReplyData) HdlcStreaming() bool {
return r.hdlcStreaming
}
// SetHdlcStreaming sets whether HDLC streaming is in progress.
func (r *GXReplyData) SetHdlcStreaming(value bool) {
r.hdlcStreaming = value
}
// IsStreaming returns true if GBT or HDLC streaming is used.
func (r *GXReplyData) IsStreaming() bool {
return ((r.moreData&enums.RequestTypesFrame) == 0 &&
r.streaming && (r.BlockNumberAck*uint16(r.gbtWindowSize))+1 > r.BlockNumber) ||
((r.moreData&enums.RequestTypesFrame) == enums.RequestTypesFrame && r.hdlcStreaming)
}
// Clear resets data values to default.
func (r *GXReplyData) Clear() {
r.moreData = enums.RequestTypesNone
r.cipheredCommand = enums.CommandNone
r.command = enums.CommandNone
r.commandType = 0
r.Data.SetCapacity(0)
r.isComplete = false
r.Error = 0
r.TotalCount = 0
r.Value = nil
r.ReadPosition = 0
r.PacketLength = 0
r.DataType = enums.DataTypeNone
r.CipherIndex = 0
r.Time = time.Time{}
r.gbtWindowSize = 0
if r.xml != nil {
r.xml.SetXmlLength(0)
}
r.invokeId = 0
}
// IsMoreData returns true if more data is available.
func (r *GXReplyData) IsMoreData() bool {
return r.moreData != enums.RequestTypesNone && r.Error == 0
}
// IsNotify returns true if this is a notify message.
func (r *GXReplyData) IsNotify() bool {
return r.frameId == 0x13 ||
r.command == enums.CommandEventNotification ||
r.command == enums.CommandDataNotification ||
r.command == enums.CommandInformationReport
}
// GetMoreData returns the more data request type.
// Returns None if more data is not available or Frame or Block type.
func (r *GXReplyData) GetMoreData() enums.RequestTypes {
return r.moreData
}
// setMoreData sets the more data request type.
func (r *GXReplyData) setMoreData(value enums.RequestTypes) {
r.moreData = value
}
// GetErrorMessage returns the error message description.
func (r *GXReplyData) GetErrorMessage() string {
return getErrorDescription(enums.ErrorCode(r.Error))
}
// Count returns the count of read elements.
// If this method is used, Peek must be set true.
func (r *GXReplyData) Count() int {
if list, ok := r.Value.([]interface{}); ok {
return len(list)
}
return 0
}
// String returns the content of reply data as a string.
func (r *GXReplyData) String() string {
if r.xml != nil {
return r.xml.String()
}
if r.Data == nil {
return ""
}
return types.ToHexWithRange(r.Data.Array(), true, 0, r.Data.Size())
}
func getErrorDescription(errorCode enums.ErrorCode) string {
switch errorCode {
case enums.ErrorCodeOk:
return ""
case enums.ErrorCodeRejected:
return "Rejected"
case enums.ErrorCodeUnacceptableFrame:
return "Unacceptable Frame"
case enums.ErrorCodeDisconnectMode:
return "Disconnect Mode"
case enums.ErrorCodeHardwareFault:
return "Hardware Fault"
case enums.ErrorCodeTemporaryFailure:
return "Temporary Failure"
case enums.ErrorCodeReadWriteDenied:
return "Read Write Denied"
case enums.ErrorCodeUndefinedObject:
return "Undefined Object"
case enums.ErrorCodeInconsistentClass:
return "Inconsistent Class"
case enums.ErrorCodeUnavailableObject:
return "Unavailable Object"
case enums.ErrorCodeUnmatchedType:
return "Unmatched Type"
case enums.ErrorCodeAccessViolated:
return "Access Violated"
case enums.ErrorCodeDataBlockUnavailable:
return "Data Block Unavailable"
case enums.ErrorCodeLongGetOrReadAborted:
return "Long Get Or Read Aborted"
case enums.ErrorCodeNoLongGetOrReadInProgress:
return "No Long Get Or Read In Progress"
case enums.ErrorCodeLongSetOrWriteAborted:
return "Long Set Or Write Aborted"
case enums.ErrorCodeNoLongSetOrWriteInProgress:
return "No Long Set Or Write In Progress"
case enums.ErrorCodeDataBlockNumberInvalid:
return "Data Block Number Invalid"
case enums.ErrorCodeOtherReason:
return "Other Reason"
default:
return fmt.Sprintf("Unknown Error (%d)", errorCode)
}
}