Shad Ansari | 2eac6a4 | 2018-11-14 22:35:39 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package core |
| 18 | |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 19 | import ( |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 20 | "context" |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 21 | |
| 22 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 23 | "gerrit.opencord.org/voltha-bbsim/protos" |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 24 | omci "github.com/opencord/omci-sim" |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 27 | // RunOmciResponder starts a go routine to process/respond to OMCI messages from VOLTHA |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 28 | func RunOmciResponder(ctx context.Context, omciOut chan openolt.OmciMsg, omciIn chan openolt.OmciIndication, errch chan error) { |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 29 | go func() { |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 30 | defer logger.Debug("Omci response process was done") |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 31 | |
| 32 | var resp openolt.OmciIndication |
| 33 | |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 34 | for { |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 35 | select { |
| 36 | case m := <-omciOut: |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 37 | resp_pkt, err := omci.OmciSim(m.IntfId, m.OnuId, HexDecode(m.Pkt)) |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 38 | 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 Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 45 | case *omci.OmciError: |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 46 | // Error in processing omci message. Log and carry on. |
| 47 | logger.Debug("%s", err.Msg) |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 48 | continue |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 49 | default: |
| 50 | // Fatal error, exit. |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 51 | errch <- err |
| 52 | return |
| 53 | } |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 54 | case <-ctx.Done(): |
| 55 | return |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 56 | } |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 57 | } |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 58 | }() |
Shad Ansari | 2eac6a4 | 2018-11-14 22:35:39 -0800 | [diff] [blame] | 59 | } |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 60 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame] | 61 | // HexDecode converts the hex encoding to binary |
Shad Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 62 | func HexDecode(pkt []byte) []byte { |
Shad Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 63 | // TODO - Change openolt adapter to send raw binary instead of hex encoded |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 64 | p := make([]byte, len(pkt)/2) |
| 65 | for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 { |
Shad Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 66 | // Go figure this ;) |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 67 | 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 Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 72 | return p |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 73 | } |
| 74 | |