Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
| 3 | Copyright (c) 2019 AT&T Intellectual Property. |
| 4 | Copyright (c) 2019 Nokia |
| 5 | |
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | you may not use this file except in compliance with the License. |
| 8 | You may obtain a copy of the License at |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 12 | Unless required by applicable law or agreed to in writing, software |
| 13 | distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | See the License for the specific language governing permissions and |
| 16 | limitations under the License. |
| 17 | ================================================================================== |
| 18 | */ |
| 19 | |
| 20 | package conv |
| 21 | |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 22 | import ( |
| 23 | "io" |
| 24 | ) |
| 25 | |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 26 | //----------------------------------------------------------------------------- |
| 27 | // |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 28 | //----------------------------------------------------------------------------- |
| 29 | type PlmnIdentityIf interface { |
| 30 | String() string |
| 31 | MccString() string |
| 32 | MncString() string |
| 33 | EncodeTo(writer io.Writer) (int, error) |
| 34 | DecodeFrom(reader io.Reader) (int, error) |
| 35 | } |
| 36 | |
| 37 | //----------------------------------------------------------------------------- |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 38 | // |
| 39 | //----------------------------------------------------------------------------- |
| 40 | |
| 41 | type PlmnIdentity struct { |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 42 | Mcc string |
| 43 | Mnc string |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func (plmnid *PlmnIdentity) String() string { |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 47 | return plmnid.MccString() + plmnid.MncString() |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | func (plmnid *PlmnIdentity) MccString() string { |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 51 | return plmnid.Mcc |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | func (plmnid *PlmnIdentity) MncString() string { |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 55 | return plmnid.Mnc |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 58 | func (plmnid *PlmnIdentity) Set(str string) { |
| 59 | plmnid.Mcc = str[0:3] |
| 60 | plmnid.Mnc = str[3:] |
| 61 | } |
| 62 | |
| 63 | //----------------------------------------------------------------------------- |
| 64 | // |
| 65 | // MCC 3 digits MNC 2 digits |
| 66 | // String format : C1C2C3N1N2 |
| 67 | // Pre encode format : C1C2C3FN1N2 |
| 68 | // TBCD Coded format : 0xC2C1 0xfC3 0xN2N1 |
| 69 | // Post decode format : C1C2C3FN1N2 |
| 70 | // String format : C1C2C3N1N2 |
| 71 | // |
| 72 | // MCC 3 digits MNC 3 digits |
| 73 | // String format : C1C2C3N1N2N3 |
| 74 | // Pre encode format : C1C2C3N3N1N2 |
| 75 | // TBCD Coded format : 0xC2C1 0xN3C3 0xN2N1 |
| 76 | // Post decode format : C1C2C3N3N1N2 |
| 77 | // String format : C1C2C3N1N2N3 |
| 78 | // |
| 79 | //----------------------------------------------------------------------------- |
| 80 | |
| 81 | type PlmnIdentityTbcd struct { |
| 82 | PlmnIdentity |
| 83 | } |
| 84 | |
| 85 | func (plmnid *PlmnIdentityTbcd) EncodeTo(writer io.Writer) (int, error) { |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 86 | |
| 87 | var tmpStr string |
| 88 | switch { |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 89 | case len(plmnid.Mnc) == 2: |
| 90 | tmpStr = plmnid.Mcc + string("f") + plmnid.Mnc |
| 91 | case len(plmnid.Mnc) == 3: |
| 92 | tmpStr = plmnid.Mcc + string(plmnid.Mnc[2]) + string(plmnid.Mnc[0:2]) |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 93 | default: |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 94 | return 0, nil |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 97 | buf := TBCD.Encode(tmpStr) |
| 98 | return writer.Write(buf) |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 101 | func (plmnid *PlmnIdentityTbcd) DecodeFrom(reader io.Reader) (int, error) { |
| 102 | tmpBytes := make([]byte, 3) |
| 103 | n, err := reader.Read(tmpBytes) |
| 104 | if err != nil { |
| 105 | return n, err |
| 106 | } |
| 107 | str := TBCD.Decode(tmpBytes) |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 108 | |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 109 | if str[3] == 'f' { |
| 110 | plmnid.Mcc = string(str[0:3]) |
| 111 | plmnid.Mnc = string(str[4:]) |
| 112 | } else { |
| 113 | plmnid.Mcc = string(str[0:3]) |
| 114 | plmnid.Mnc = string(str[4:]) + string(str[3]) |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 115 | } |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 116 | return n, nil |
| 117 | } |
| 118 | |
| 119 | //----------------------------------------------------------------------------- |
| 120 | // |
| 121 | // MCC 3 digits MNC 2 digits |
| 122 | // String format : C1C2C3N1N2 |
| 123 | // Pre encode format : C1C2C3FN1N2 |
| 124 | // BCD Coded format : 0xC2C1 0xC3f 0xN1N2 |
| 125 | // Post decode format : C1C2C3FN1N2 |
| 126 | // String format : C1C2C3N1N2 |
| 127 | // |
| 128 | // MCC 3 digits MNC 3 digits |
| 129 | // String format : C1C2C3N1N2N3 |
| 130 | // Pre encode format : C1C2C3N1N2N3 |
| 131 | // BCD Coded format : 0xC2C1 0xC3N1 0xN2N3 |
| 132 | // Post decode format : C1C2C3N1N2N3 |
| 133 | // String format : C1C2C3N1N2N3 |
| 134 | // |
| 135 | //----------------------------------------------------------------------------- |
| 136 | |
| 137 | type PlmnIdentityBcd struct { |
| 138 | PlmnIdentity |
| 139 | } |
| 140 | |
| 141 | func (plmnid *PlmnIdentityBcd) EncodeTo(writer io.Writer) (int, error) { |
| 142 | |
| 143 | var tmpStr string |
| 144 | switch { |
| 145 | case len(plmnid.Mnc) == 2: |
| 146 | tmpStr = plmnid.Mcc + string("f") + plmnid.Mnc |
| 147 | case len(plmnid.Mnc) == 3: |
| 148 | tmpStr = plmnid.Mcc + plmnid.Mnc |
| 149 | default: |
| 150 | return 0, nil |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 151 | } |
Juha Hyttinen | 14f8273 | 2020-02-27 13:49:06 +0200 | [diff] [blame^] | 152 | |
| 153 | buf := BCD.Encode(tmpStr) |
| 154 | return writer.Write(buf) |
| 155 | } |
| 156 | |
| 157 | func (plmnid *PlmnIdentityBcd) DecodeFrom(reader io.Reader) (int, error) { |
| 158 | tmpBytes := make([]byte, 3) |
| 159 | n, err := reader.Read(tmpBytes) |
| 160 | if err != nil { |
| 161 | return n, err |
| 162 | } |
| 163 | str := BCD.Decode(tmpBytes) |
| 164 | |
| 165 | if str[3] == 'f' { |
| 166 | plmnid.Mcc = string(str[0:3]) |
| 167 | plmnid.Mnc = string(str[4:]) |
| 168 | } else { |
| 169 | plmnid.Mcc = string(str[0:3]) |
| 170 | plmnid.Mnc = string(str[3:]) |
| 171 | } |
| 172 | return n, nil |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 173 | } |