blob: 40398ba66b3c68e01525726a9949fffb24c686e1 [file] [log] [blame]
Matteo Scandolof9d43412021-01-12 11:11:34 -08001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package omci
18
19import (
20 "errors"
21 "github.com/google/gopacket"
22 "github.com/opencord/omci-lib-go"
23)
24
25func ParseTestRequest(omciPkt gopacket.Packet) (*omci.TestRequest, error) {
26 msgLayer := omciPkt.Layer(omci.LayerTypeGetRequest)
27 if msgLayer == nil {
28 err := "omci Msg layer could not be detected for LayerTypeTestRequest"
29 omciLogger.Error(err)
30 return nil, errors.New(err)
31 }
32 msgObj, msgOk := msgLayer.(*omci.TestRequest)
33 if !msgOk {
34 err := "omci Msg layer could not be assigned for LayerTypeTestRequest"
35 omciLogger.Error(err)
36 return nil, errors.New(err)
37 }
38 return msgObj, nil
39}
40
41// Return true if msg is an Omci Test Request
42func IsTestRequest(payload []byte) (bool, error) {
43 _, omciMsg, err := ParseOpenOltOmciPacket(payload)
44 if err != nil {
45 return false, err
46 }
47
48 return omciMsg.MessageType == omci.TestRequestType, nil
49}
50
51func BuildTestResult(payload []byte) ([]byte, error) {
52
53 omciPkt, omciMsg, err := ParseOpenOltOmciPacket(payload)
54
55 //transactionId, deviceId, _, class, instance, _, err := omcisim.ParsePkt(payload)
56
57 if err != nil {
58 return []byte{}, err
59 }
60
61 testRequest, err := ParseTestRequest(omciPkt)
62 if err != nil {
63 return []byte{}, err
64 }
65
66 // TODO create a TestResponse using omci-lib-go
67 resp := make([]byte, 48)
68 resp[0] = byte(omciMsg.TransactionID >> 8)
69 resp[1] = byte(omciMsg.TransactionID & 0xFF)
70 resp[2] = 27 // Upper nibble 0x0 is fixed (0000), Lower nibbles defines msg type (TestResult=27)
71 resp[3] = byte(omciMsg.DeviceIdentifier)
72 resp[4] = byte(omciMsg.MessageType)
73 resp[5] = byte(omciMsg.MessageType & 0xFF)
74 resp[6] = byte(testRequest.EntityInstance >> 8)
75 resp[7] = byte(testRequest.EntityInstance & 0xFF)
76 // Each of these is a 1-byte code
77 // follow by a 2-byte (high, low) value
78 resp[8] = 1 // power feed voltage
79 resp[9] = 0
80 resp[10] = 123 // 123 mV, 20 mv res --> 6mv
81 resp[11] = 3 // received optical power
82 resp[12] = 1
83 resp[13] = 200 // 456 decibel-microwatts, 0.002 dB res --> 0.912 db-mw
84 resp[14] = 5 // mean optical launch power
85 resp[15] = 3
86 resp[16] = 21 // 789 uA, 0.002 dB res --> 1.578 db-mw
87 resp[17] = 9 // laser bias current
88 resp[18] = 3
89 resp[19] = 244 // 1012 uA, 2uA res --> 505 ua
90 resp[20] = 12 // temperature
91 resp[21] = 38
92 resp[22] = 148 // 9876 deg C, 1/256 resolution --> 38.57 Deg C
93
94 return resp, nil
95}