-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsynctime.go
314 lines (292 loc) · 10.3 KB
/
synctime.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
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
/*
* Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package omci
import (
"encoding/binary"
"errors"
"fmt"
me "github.com/cboling/omci/v2/generated"
"github.com/google/gopacket"
)
type SynchronizeTimeRequest struct {
MeBasePacket
Year uint16
Month uint8
Day uint8
Hour uint8
Minute uint8
Second uint8
}
func (omci *SynchronizeTimeRequest) String() string {
return fmt.Sprintf("%v, Date-Time: %d/%d/%d-%02d:%02d:%02d",
omci.MeBasePacket.String(), omci.Year, omci.Month, omci.Day, omci.Hour, omci.Minute, omci.Second)
}
// LayerType returns LayerTypeSynchronizeTimeRequest
func (omci *SynchronizeTimeRequest) LayerType() gopacket.LayerType {
return LayerTypeSynchronizeTimeRequest
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *SynchronizeTimeRequest) CanDecode() gopacket.LayerClass {
return LayerTypeSynchronizeTimeRequest
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *SynchronizeTimeRequest) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Synchronize Time Request into this layer
func (omci *SynchronizeTimeRequest) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
getDateAndTime := true
var offset, hdrSize int
if omci.Extended {
offset = 6
hdrSize = offset + 7
// Extended format allows for the OLT to support not setting the date and
// time (not present in the message)
// TODO: There is not a way to indicate this to the user at this time, currently
// all date/time fields will be zero
if len(data) < offset {
p.SetTruncated()
return errors.New("frame too small")
}
if len(data) < hdrSize {
getDateAndTime = false
hdrSize = len(data)
}
} else {
offset = 4
hdrSize := offset + 7
if len(data) < hdrSize {
p.SetTruncated()
return errors.New("frame too small")
}
}
err := omci.MeBasePacket.DecodeFromBytes(data, p, hdrSize)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support Synchronize Time
if !me.SupportsMsgType(meDefinition, me.SynchronizeTime) {
return me.NewProcessingError("managed entity does not support Synchronize Time Message-Type")
}
// Synchronize Time Entity Class are always ONU-G (256) and Entity Instance of 0
if omci.EntityClass != me.OnuGClassID {
return me.NewProcessingError("invalid Entity Class for Synchronize Time request")
}
if omci.EntityInstance != 0 {
return me.NewUnknownInstanceError("invalid Entity Instance for Synchronize Time request")
}
if getDateAndTime {
omci.Year = binary.BigEndian.Uint16(data[offset:])
omci.Month = data[offset+2]
omci.Day = data[offset+3]
omci.Hour = data[offset+4]
omci.Minute = data[offset+5]
omci.Second = data[offset+6]
}
return nil
}
func decodeSynchronizeTimeRequest(data []byte, p gopacket.PacketBuilder) error {
omci := &SynchronizeTimeRequest{}
omci.MsgLayerType = LayerTypeSynchronizeTimeRequest
return decodingLayerDecoder(omci, data, p)
}
func decodeSynchronizeTimeRequestExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &SynchronizeTimeRequest{}
omci.MsgLayerType = LayerTypeSynchronizeTimeRequest
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Synchronize Time Request message
func (omci *SynchronizeTimeRequest) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
// Basic (common) OMCI Header is 8 octets, 10
err := omci.MeBasePacket.SerializeTo(b)
if err != nil {
return err
}
entity, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support Synchronize Time
if !me.SupportsMsgType(entity, me.SynchronizeTime) {
return me.NewProcessingError("managed entity does not support the Synchronize Time Message-Type")
}
var offset, length int
if omci.Extended {
// TODO: Extended format allows for the OLT to support not setting the date and
// time (not present in the message). This needs to be supported in a future
// version of the software.
offset = 2
length = 7
} else {
offset = 0
length = 7
}
bytes, err := b.AppendBytes(offset + length)
if err != nil {
return err
}
if omci.Extended {
binary.BigEndian.PutUint16(bytes, uint16(length))
}
binary.BigEndian.PutUint16(bytes[offset:], omci.Year)
if length > 0 {
bytes[offset+2] = omci.Month
bytes[offset+3] = omci.Day
bytes[offset+4] = omci.Hour
bytes[offset+5] = omci.Minute
bytes[offset+6] = omci.Second
}
return nil
}
type SynchronizeTimeResponse struct {
MeBasePacket
Result me.Results
SuccessResults uint8 // Only if 'Result' is 0 -> success
}
func (omci *SynchronizeTimeResponse) String() string {
return fmt.Sprintf("%v, Results: %d (%v), Success: %d",
omci.MeBasePacket.String(), omci.Result, omci.Result, omci.SuccessResults)
}
// LayerType returns LayerTypeSynchronizeTimeResponse
func (omci *SynchronizeTimeResponse) LayerType() gopacket.LayerType {
return LayerTypeSynchronizeTimeResponse
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *SynchronizeTimeResponse) CanDecode() gopacket.LayerClass {
return LayerTypeSynchronizeTimeResponse
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *SynchronizeTimeResponse) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Synchronize Time Response into this layer
func (omci *SynchronizeTimeResponse) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
var hdrSize, offset int
if omci.Extended {
offset = 6
hdrSize = offset + 1
// TODO: Extended message set allows for the optional encoding of the of the
// 12th octet (success results) even if the result code is not 0/success.
// This functionality is not currently supported.
} else {
offset = 4
hdrSize = offset + 2
}
err := omci.MeBasePacket.DecodeFromBytes(data, p, hdrSize)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support Synchronize Time
if !me.SupportsMsgType(meDefinition, me.SynchronizeTime) {
return me.NewProcessingError("managed entity does not support Synchronize Time Message-Type")
}
// Synchronize Time Entity Class are always ONU-G (256) and Entity Instance of 0
if omci.EntityClass != me.OnuGClassID {
return me.NewProcessingError("invalid Entity Class for Synchronize Time response")
}
if omci.EntityInstance != 0 {
return me.NewUnknownInstanceError("invalid Entity Instance for Synchronize Time response")
}
omci.Result = me.Results(data[offset])
if omci.Result > me.DeviceBusy {
msg := fmt.Sprintf("invalid results code: %v, must be 0..6", omci.Result)
return errors.New(msg)
}
if omci.Result == me.Success && len(data) > offset+1 {
omci.SuccessResults = data[offset+1]
} else if omci.Extended && len(data) > offset+1 {
omci.SuccessResults = data[offset+1]
}
return nil
}
func decodeSynchronizeTimeResponse(data []byte, p gopacket.PacketBuilder) error {
omci := &SynchronizeTimeResponse{}
omci.MsgLayerType = LayerTypeSynchronizeTimeResponse
return decodingLayerDecoder(omci, data, p)
}
func decodeSynchronizeTimeResponseExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &SynchronizeTimeResponse{}
omci.MsgLayerType = LayerTypeSynchronizeTimeResponse
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Synchronize Time Response message
func (omci *SynchronizeTimeResponse) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
// Basic (common) OMCI Header is 8 octets, 10
err := omci.MeBasePacket.SerializeTo(b)
if err != nil {
return err
}
entity, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// Synchronize Time Entity Class are always ONU DATA (2) and Entity Instance of 0
if omci.EntityClass != me.OnuGClassID {
return me.NewProcessingError("invalid Entity Class for Synchronize Time response")
}
if omci.EntityInstance != 0 {
return me.NewUnknownInstanceError("invalid Entity Instance for Synchronize Time response")
}
// ME needs to support Synchronize Time
if !me.SupportsMsgType(entity, me.SynchronizeTime) {
return me.NewProcessingError("managed entity does not support the Synchronize Time Message-Type")
}
var offset int
if omci.Extended {
offset = 2
} else {
offset = 0
}
numBytes := 2
if omci.Result != me.Success {
// TODO: Extended message set allows for the optional encoding of the of the
// 12th octet (success results) even if the result code is not 0/success.
// This functionality is not currently supported
numBytes = 1
}
bytes, err := b.AppendBytes(offset + numBytes)
if err != nil {
return err
}
if omci.Extended {
binary.BigEndian.PutUint16(bytes, uint16(numBytes))
}
bytes[offset] = uint8(omci.Result)
if omci.Result == me.Success {
// TODO: Extended message set allows for the optional encoding of the of the
// 12th octet (success results) even if the result code is not 0/success.
// This functionality is not currently supported
bytes[offset+1] = omci.SuccessResults
}
return nil
}