blob: 433cb57f6394ee0102e4c286614bddbbb6c5e495 [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
Keita NISHIMOTO621a43d2019-01-30 20:53:03 +090027func RunOmciResponder(ctx context.Context, omciOut chan openolt.OmciMsg, omciIn chan openolt.OmciIndication, errch chan error) {
Shad Ansaria3384b02019-01-03 15:11:11 -080028 go func() {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090029 defer logger.Debug("Omci response process was done")
Shad Ansari65fed642019-01-16 14:04:19 -080030
31 var resp openolt.OmciIndication
32
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090033 for {
Shad Ansaria3384b02019-01-03 15:11:11 -080034 select {
35 case m := <-omciOut:
Shad Ansarib744bf22019-01-17 11:36:46 -080036 resp_pkt, err := omci.OmciSim(m.IntfId, m.OnuId, HexDecode(m.Pkt))
Shad Ansari65fed642019-01-16 14:04:19 -080037 switch err := err.(type) {
38 case nil:
39 // Success
40 resp.IntfId = m.IntfId
41 resp.OnuId = m.OnuId
42 resp.Pkt = resp_pkt
43 omciIn <- resp
Shad Ansarib744bf22019-01-17 11:36:46 -080044 case *omci.OmciError:
Shad Ansari65fed642019-01-16 14:04:19 -080045 // Error in processing omci message. Log and carry on.
46 logger.Debug("%s", err.Msg)
Shad Ansaria3384b02019-01-03 15:11:11 -080047 continue
Shad Ansari65fed642019-01-16 14:04:19 -080048 default:
49 // Fatal error, exit.
Shad Ansaria3384b02019-01-03 15:11:11 -080050 errch <- err
51 return
52 }
Shad Ansaria3384b02019-01-03 15:11:11 -080053 case <-ctx.Done():
54 return
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090055 }
Shad Ansari45806172018-11-19 17:13:08 -080056 }
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090057 }()
Shad Ansari2eac6a42018-11-14 22:35:39 -080058}
Shad Ansari45806172018-11-19 17:13:08 -080059
Shad Ansaria5c79892018-11-29 16:32:59 -080060func HexDecode(pkt []byte) []byte {
61 // Convert the hex encoding to binary
62 // TODO - Change openolt adapter to send raw binary instead of hex encoded
Shad Ansari45806172018-11-19 17:13:08 -080063 p := make([]byte, len(pkt)/2)
64 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
Shad Ansaria5c79892018-11-29 16:32:59 -080065 // Go figure this ;)
Shad Ansari45806172018-11-19 17:13:08 -080066 u := (pkt[i] & 15) + (pkt[i]>>6)*9
67 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
68 p[j] = u<<4 + l
69 }
70 logger.Debug("Omci decoded: %x.", p)
Shad Ansaria5c79892018-11-29 16:32:59 -080071 return p
Shad Ansari45806172018-11-19 17:13:08 -080072}
73