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 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 27 | 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] | 28 | go func() { |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 29 | defer logger.Debug("Omci response process was done") |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 30 | |
| 31 | var resp openolt.OmciIndication |
| 32 | |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 33 | for { |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 34 | select { |
| 35 | case m := <-omciOut: |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 36 | resp_pkt, err := omci.OmciSim(m.IntfId, m.OnuId, HexDecode(m.Pkt)) |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 37 | 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 Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 44 | case *omci.OmciError: |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 45 | // Error in processing omci message. Log and carry on. |
| 46 | logger.Debug("%s", err.Msg) |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 47 | continue |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 48 | default: |
| 49 | // Fatal error, exit. |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 50 | errch <- err |
| 51 | return |
| 52 | } |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 53 | case <-ctx.Done(): |
| 54 | return |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 55 | } |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 56 | } |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 57 | }() |
Shad Ansari | 2eac6a4 | 2018-11-14 22:35:39 -0800 | [diff] [blame] | 58 | } |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 59 | |
Shad Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 60 | func 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 Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 63 | p := make([]byte, len(pkt)/2) |
| 64 | 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] | 65 | // Go figure this ;) |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 66 | 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 Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 71 | return p |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 72 | } |
| 73 | |