Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
| 3 | * Copyright 2020-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package omci |
| 19 | |
| 20 | import ( |
| 21 | "encoding/binary" |
| 22 | "errors" |
| 23 | "fmt" |
| 24 | "github.com/google/gopacket" |
| 25 | me "github.com/opencord/omci-lib-go/generated" |
| 26 | ) |
| 27 | |
| 28 | type SynchronizeTimeRequest struct { |
| 29 | MeBasePacket |
| 30 | Year uint16 |
| 31 | Month uint8 |
| 32 | Day uint8 |
| 33 | Hour uint8 |
| 34 | Minute uint8 |
| 35 | Second uint8 |
| 36 | } |
| 37 | |
| 38 | func (omci *SynchronizeTimeRequest) String() string { |
| 39 | return fmt.Sprintf("%v, Date-Time: %d/%d/%d-%02d:%02d:%02d", |
| 40 | omci.MeBasePacket.String(), omci.Year, omci.Month, omci.Day, omci.Hour, omci.Minute, omci.Second) |
| 41 | } |
| 42 | |
| 43 | // LayerType returns LayerTypeSynchronizeTimeRequest |
| 44 | func (omci *SynchronizeTimeRequest) LayerType() gopacket.LayerType { |
| 45 | return LayerTypeSynchronizeTimeRequest |
| 46 | } |
| 47 | |
| 48 | // CanDecode returns the set of layer types that this DecodingLayer can decode |
| 49 | func (omci *SynchronizeTimeRequest) CanDecode() gopacket.LayerClass { |
| 50 | return LayerTypeSynchronizeTimeRequest |
| 51 | } |
| 52 | |
| 53 | // NextLayerType returns the layer type contained by this DecodingLayer. |
| 54 | func (omci *SynchronizeTimeRequest) NextLayerType() gopacket.LayerType { |
| 55 | return gopacket.LayerTypePayload |
| 56 | } |
| 57 | |
| 58 | // DecodeFromBytes decodes the given bytes of a Synchronize Time Request into this layer |
| 59 | func (omci *SynchronizeTimeRequest) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error { |
| 60 | // Common ClassID/EntityID decode in msgBase |
| 61 | getDateAndTime := true |
| 62 | var offset, hdrSize int |
| 63 | if omci.Extended { |
| 64 | offset = 6 |
| 65 | hdrSize = offset + 7 |
| 66 | // Extended format allows for the OLT to support not setting the date and |
| 67 | // time (not present in the message) |
| 68 | // TODO: There is not a way to indicate this to the user at this time, currently |
| 69 | // all date/time fields will be zero |
| 70 | if len(data) < offset { |
| 71 | p.SetTruncated() |
| 72 | return errors.New("frame too small") |
| 73 | } |
| 74 | if len(data) < hdrSize { |
| 75 | getDateAndTime = false |
| 76 | hdrSize = len(data) |
| 77 | } |
| 78 | } else { |
| 79 | offset = 4 |
| 80 | hdrSize := offset + 7 |
| 81 | if len(data) < hdrSize { |
| 82 | p.SetTruncated() |
| 83 | return errors.New("frame too small") |
| 84 | } |
| 85 | } |
| 86 | err := omci.MeBasePacket.DecodeFromBytes(data, p, hdrSize) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass, |
| 91 | me.ParamData{EntityID: omci.EntityInstance}) |
| 92 | if omciErr.StatusCode() != me.Success { |
| 93 | return omciErr.GetError() |
| 94 | } |
| 95 | // ME needs to support Synchronize Time |
| 96 | if !me.SupportsMsgType(meDefinition, me.SynchronizeTime) { |
| 97 | return me.NewProcessingError("managed entity does not support Synchronize Time Message-Type") |
| 98 | } |
| 99 | // Synchronize Time Entity Class are always ONU-G (256) and Entity Instance of 0 |
| 100 | if omci.EntityClass != me.OnuGClassID { |
| 101 | return me.NewProcessingError("invalid Entity Class for Synchronize Time request") |
| 102 | } |
| 103 | if omci.EntityInstance != 0 { |
| 104 | return me.NewUnknownInstanceError("invalid Entity Instance for Synchronize Time request") |
| 105 | } |
| 106 | |
| 107 | if getDateAndTime { |
| 108 | omci.Year = binary.BigEndian.Uint16(data[offset:]) |
| 109 | omci.Month = data[offset+2] |
| 110 | omci.Day = data[offset+3] |
| 111 | omci.Hour = data[offset+4] |
| 112 | omci.Minute = data[offset+5] |
| 113 | omci.Second = data[offset+6] |
| 114 | } |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | func decodeSynchronizeTimeRequest(data []byte, p gopacket.PacketBuilder) error { |
| 119 | omci := &SynchronizeTimeRequest{} |
| 120 | omci.MsgLayerType = LayerTypeSynchronizeTimeRequest |
| 121 | return decodingLayerDecoder(omci, data, p) |
| 122 | } |
| 123 | |
| 124 | func decodeSynchronizeTimeRequestExtended(data []byte, p gopacket.PacketBuilder) error { |
| 125 | omci := &SynchronizeTimeRequest{} |
| 126 | omci.MsgLayerType = LayerTypeSynchronizeTimeRequest |
| 127 | omci.Extended = true |
| 128 | return decodingLayerDecoder(omci, data, p) |
| 129 | } |
| 130 | |
| 131 | // SerializeTo provides serialization of an Synchronize Time Request message |
| 132 | func (omci *SynchronizeTimeRequest) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error { |
| 133 | // Basic (common) OMCI Header is 8 octets, 10 |
| 134 | err := omci.MeBasePacket.SerializeTo(b) |
| 135 | if err != nil { |
| 136 | return err |
| 137 | } |
| 138 | entity, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass, |
| 139 | me.ParamData{EntityID: omci.EntityInstance}) |
| 140 | if omciErr.StatusCode() != me.Success { |
| 141 | return omciErr.GetError() |
| 142 | } |
| 143 | // ME needs to support Synchronize Time |
| 144 | if !me.SupportsMsgType(entity, me.SynchronizeTime) { |
| 145 | return me.NewProcessingError("managed entity does not support the Synchronize Time Message-Type") |
| 146 | } |
| 147 | var offset, length int |
| 148 | if omci.Extended { |
| 149 | // TODO: Extended format allows for the OLT to support not setting the date and |
| 150 | // time (not present in the message). This needs to be supported in a future |
| 151 | // version of the software. |
| 152 | offset = 2 |
| 153 | length = 7 |
| 154 | } else { |
| 155 | offset = 0 |
| 156 | length = 7 |
| 157 | } |
| 158 | bytes, err := b.AppendBytes(offset + length) |
| 159 | if err != nil { |
| 160 | return err |
| 161 | } |
| 162 | if omci.Extended { |
| 163 | binary.BigEndian.PutUint16(bytes, uint16(length)) |
| 164 | } |
| 165 | binary.BigEndian.PutUint16(bytes[offset:], omci.Year) |
| 166 | if length > 0 { |
| 167 | bytes[offset+2] = omci.Month |
| 168 | bytes[offset+3] = omci.Day |
| 169 | bytes[offset+4] = omci.Hour |
| 170 | bytes[offset+5] = omci.Minute |
| 171 | bytes[offset+6] = omci.Second |
| 172 | } |
| 173 | return nil |
| 174 | } |
| 175 | |
| 176 | type SynchronizeTimeResponse struct { |
| 177 | MeBasePacket |
| 178 | Result me.Results |
| 179 | SuccessResults uint8 // Only if 'Result' is 0 -> success |
| 180 | } |
| 181 | |
| 182 | func (omci *SynchronizeTimeResponse) String() string { |
| 183 | return fmt.Sprintf("%v, Results: %d (%v), Success: %d", |
| 184 | omci.MeBasePacket.String(), omci.Result, omci.Result, omci.SuccessResults) |
| 185 | } |
| 186 | |
| 187 | // LayerType returns LayerTypeSynchronizeTimeResponse |
| 188 | func (omci *SynchronizeTimeResponse) LayerType() gopacket.LayerType { |
| 189 | return LayerTypeSynchronizeTimeResponse |
| 190 | } |
| 191 | |
| 192 | // CanDecode returns the set of layer types that this DecodingLayer can decode |
| 193 | func (omci *SynchronizeTimeResponse) CanDecode() gopacket.LayerClass { |
| 194 | return LayerTypeSynchronizeTimeResponse |
| 195 | } |
| 196 | |
| 197 | // NextLayerType returns the layer type contained by this DecodingLayer. |
| 198 | func (omci *SynchronizeTimeResponse) NextLayerType() gopacket.LayerType { |
| 199 | return gopacket.LayerTypePayload |
| 200 | } |
| 201 | |
| 202 | // DecodeFromBytes decodes the given bytes of a Synchronize Time Response into this layer |
| 203 | func (omci *SynchronizeTimeResponse) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error { |
| 204 | // Common ClassID/EntityID decode in msgBase |
| 205 | var hdrSize, offset int |
| 206 | if omci.Extended { |
| 207 | offset = 6 |
| 208 | hdrSize = offset + 1 |
| 209 | // TODO: Extended message set allows for the optional encoding of the of the |
| 210 | // 12th octet (success results) even if the result code is not 0/success. |
| 211 | // This functionality is not currently supported. |
| 212 | } else { |
| 213 | offset = 4 |
| 214 | hdrSize = offset + 2 |
| 215 | } |
| 216 | err := omci.MeBasePacket.DecodeFromBytes(data, p, hdrSize) |
| 217 | if err != nil { |
| 218 | return err |
| 219 | } |
| 220 | meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass, |
| 221 | me.ParamData{EntityID: omci.EntityInstance}) |
| 222 | if omciErr.StatusCode() != me.Success { |
| 223 | return omciErr.GetError() |
| 224 | } |
| 225 | // ME needs to support Synchronize Time |
| 226 | if !me.SupportsMsgType(meDefinition, me.SynchronizeTime) { |
| 227 | return me.NewProcessingError("managed entity does not support Synchronize Time Message-Type") |
| 228 | } |
| 229 | // Synchronize Time Entity Class are always ONU-G (256) and Entity Instance of 0 |
| 230 | if omci.EntityClass != me.OnuGClassID { |
| 231 | return me.NewProcessingError("invalid Entity Class for Synchronize Time response") |
| 232 | } |
| 233 | if omci.EntityInstance != 0 { |
| 234 | return me.NewUnknownInstanceError("invalid Entity Instance for Synchronize Time response") |
| 235 | } |
| 236 | |
| 237 | omci.Result = me.Results(data[offset]) |
| 238 | if omci.Result > me.DeviceBusy { |
| 239 | msg := fmt.Sprintf("invalid results code: %v, must be 0..6", omci.Result) |
| 240 | return errors.New(msg) |
| 241 | } |
| 242 | if omci.Result == me.Success && len(data) > offset+1 { |
| 243 | omci.SuccessResults = data[offset+1] |
| 244 | } else if omci.Extended && len(data) > offset+1 { |
| 245 | omci.SuccessResults = data[offset+1] |
| 246 | } |
| 247 | return nil |
| 248 | } |
| 249 | |
| 250 | func decodeSynchronizeTimeResponse(data []byte, p gopacket.PacketBuilder) error { |
| 251 | omci := &SynchronizeTimeResponse{} |
| 252 | omci.MsgLayerType = LayerTypeSynchronizeTimeResponse |
| 253 | return decodingLayerDecoder(omci, data, p) |
| 254 | } |
| 255 | |
| 256 | func decodeSynchronizeTimeResponseExtended(data []byte, p gopacket.PacketBuilder) error { |
| 257 | omci := &SynchronizeTimeResponse{} |
| 258 | omci.MsgLayerType = LayerTypeSynchronizeTimeResponse |
| 259 | omci.Extended = true |
| 260 | return decodingLayerDecoder(omci, data, p) |
| 261 | } |
| 262 | |
| 263 | // SerializeTo provides serialization of an Synchronize Time Response message |
| 264 | func (omci *SynchronizeTimeResponse) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error { |
| 265 | // Basic (common) OMCI Header is 8 octets, 10 |
| 266 | err := omci.MeBasePacket.SerializeTo(b) |
| 267 | if err != nil { |
| 268 | return err |
| 269 | } |
| 270 | entity, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass, |
| 271 | me.ParamData{EntityID: omci.EntityInstance}) |
| 272 | if omciErr.StatusCode() != me.Success { |
| 273 | return omciErr.GetError() |
| 274 | } |
| 275 | // Synchronize Time Entity Class are always ONU DATA (2) and Entity Instance of 0 |
| 276 | if omci.EntityClass != me.OnuGClassID { |
| 277 | return me.NewProcessingError("invalid Entity Class for Synchronize Time response") |
| 278 | } |
| 279 | if omci.EntityInstance != 0 { |
| 280 | return me.NewUnknownInstanceError("invalid Entity Instance for Synchronize Time response") |
| 281 | } |
| 282 | // ME needs to support Synchronize Time |
| 283 | if !me.SupportsMsgType(entity, me.SynchronizeTime) { |
| 284 | return me.NewProcessingError("managed entity does not support the Synchronize Time Message-Type") |
| 285 | } |
| 286 | var offset int |
| 287 | if omci.Extended { |
| 288 | offset = 2 |
| 289 | } else { |
| 290 | offset = 0 |
| 291 | } |
| 292 | numBytes := 2 |
| 293 | if omci.Result != me.Success { |
| 294 | // TODO: Extended message set allows for the optional encoding of the of the |
| 295 | // 12th octet (success results) even if the result code is not 0/success. |
| 296 | // This functionality is not currently supported |
| 297 | numBytes = 1 |
| 298 | } |
| 299 | bytes, err := b.AppendBytes(offset + numBytes) |
| 300 | if err != nil { |
| 301 | return err |
| 302 | } |
| 303 | if omci.Extended { |
| 304 | binary.BigEndian.PutUint16(bytes, uint16(numBytes)) |
| 305 | } |
| 306 | bytes[offset] = uint8(omci.Result) |
| 307 | if omci.Result == me.Success { |
| 308 | // TODO: Extended message set allows for the optional encoding of the of the |
| 309 | // 12th octet (success results) even if the result code is not 0/success. |
| 310 | // This functionality is not currently supported |
| 311 | bytes[offset+1] = omci.SuccessResults |
| 312 | } |
| 313 | return nil |
| 314 | } |