blob: 711bbc7494924c49078f38ff9a1ec23a08d32d0a [file] [log] [blame]
Shad Ansari2eac6a42018-11-14 22:35:39 -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 core
18
Shad Ansari45806172018-11-19 17:13:08 -080019import (
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090020 "context"
Shad Ansaria3384b02019-01-03 15:11:11 -080021
22 "gerrit.opencord.org/voltha-bbsim/common/logger"
Shad Ansaria3384b02019-01-03 15:11:11 -080023 "gerrit.opencord.org/voltha-bbsim/protos"
Shad Ansarib744bf22019-01-17 11:36:46 -080024 omci "github.com/opencord/omci-sim"
Shad Ansari45806172018-11-19 17:13:08 -080025)
26
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020027// RunOmciResponder starts a go routine to process/respond to OMCI messages from VOLTHA
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090028func RunOmciResponder(ctx context.Context, omciOut chan openolt.OmciMsg, omciIn chan openolt.OmciIndication, errch chan error) {
Shad Ansaria3384b02019-01-03 15:11:11 -080029 go func() {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090030 defer logger.Debug("Omci response process was done")
Shad Ansari65fed642019-01-16 14:04:19 -080031
32 var resp openolt.OmciIndication
33
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090034 for {
Shad Ansaria3384b02019-01-03 15:11:11 -080035 select {
36 case m := <-omciOut:
Shad Ansarib744bf22019-01-17 11:36:46 -080037 resp_pkt, err := omci.OmciSim(m.IntfId, m.OnuId, HexDecode(m.Pkt))
Shad Ansari65fed642019-01-16 14:04:19 -080038 switch err := err.(type) {
39 case nil:
40 // Success
41 resp.IntfId = m.IntfId
42 resp.OnuId = m.OnuId
43 resp.Pkt = resp_pkt
44 omciIn <- resp
Shad Ansarib744bf22019-01-17 11:36:46 -080045 case *omci.OmciError:
Shad Ansari65fed642019-01-16 14:04:19 -080046 // Error in processing omci message. Log and carry on.
47 logger.Debug("%s", err.Msg)
Shad Ansaria3384b02019-01-03 15:11:11 -080048 continue
Shad Ansari65fed642019-01-16 14:04:19 -080049 default:
50 // Fatal error, exit.
Shad Ansaria3384b02019-01-03 15:11:11 -080051 errch <- err
52 return
53 }
Shad Ansaria3384b02019-01-03 15:11:11 -080054 case <-ctx.Done():
55 return
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090056 }
Shad Ansari45806172018-11-19 17:13:08 -080057 }
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090058 }()
Shad Ansari2eac6a42018-11-14 22:35:39 -080059}
Shad Ansari45806172018-11-19 17:13:08 -080060
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020061// HexDecode converts the hex encoding to binary
Shad Ansaria5c79892018-11-29 16:32:59 -080062func HexDecode(pkt []byte) []byte {
Shad Ansaria5c79892018-11-29 16:32:59 -080063 // TODO - Change openolt adapter to send raw binary instead of hex encoded
Shad Ansari45806172018-11-19 17:13:08 -080064 p := make([]byte, len(pkt)/2)
65 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
Shad Ansaria5c79892018-11-29 16:32:59 -080066 // Go figure this ;)
Shad Ansari45806172018-11-19 17:13:08 -080067 u := (pkt[i] & 15) + (pkt[i]>>6)*9
68 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
69 p[j] = u<<4 + l
70 }
71 logger.Debug("Omci decoded: %x.", p)
Shad Ansaria5c79892018-11-29 16:32:59 -080072 return p
Shad Ansari45806172018-11-19 17:13:08 -080073}
74