blob: 2f56de4834eb4a26f749141cff5e3bdebf4bed90 [file] [log] [blame]
Juha Hyttinenff8dccd2019-12-10 14:34:07 +02001/*
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
20package conv
21
Juha Hyttinen14f82732020-02-27 13:49:06 +020022import (
23 "io"
24)
25
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020026//-----------------------------------------------------------------------------
27//
Juha Hyttinen14f82732020-02-27 13:49:06 +020028//-----------------------------------------------------------------------------
29type 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 Hyttinenff8dccd2019-12-10 14:34:07 +020038//
39//-----------------------------------------------------------------------------
40
41type PlmnIdentity struct {
Juha Hyttinen14f82732020-02-27 13:49:06 +020042 Mcc string
43 Mnc string
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020044}
45
46func (plmnid *PlmnIdentity) String() string {
Juha Hyttinen14f82732020-02-27 13:49:06 +020047 return plmnid.MccString() + plmnid.MncString()
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020048}
49
50func (plmnid *PlmnIdentity) MccString() string {
Juha Hyttinen14f82732020-02-27 13:49:06 +020051 return plmnid.Mcc
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020052}
53
54func (plmnid *PlmnIdentity) MncString() string {
Juha Hyttinen14f82732020-02-27 13:49:06 +020055 return plmnid.Mnc
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020056}
57
Juha Hyttinen14f82732020-02-27 13:49:06 +020058func (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
81type PlmnIdentityTbcd struct {
82 PlmnIdentity
83}
84
85func (plmnid *PlmnIdentityTbcd) EncodeTo(writer io.Writer) (int, error) {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020086
87 var tmpStr string
88 switch {
Juha Hyttinen14f82732020-02-27 13:49:06 +020089 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 Hyttinenff8dccd2019-12-10 14:34:07 +020093 default:
Juha Hyttinen14f82732020-02-27 13:49:06 +020094 return 0, nil
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020095 }
96
Juha Hyttinen14f82732020-02-27 13:49:06 +020097 buf := TBCD.Encode(tmpStr)
98 return writer.Write(buf)
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020099}
100
Juha Hyttinen14f82732020-02-27 13:49:06 +0200101func (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 Hyttinenff8dccd2019-12-10 14:34:07 +0200108
Juha Hyttinen14f82732020-02-27 13:49:06 +0200109 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 Hyttinenff8dccd2019-12-10 14:34:07 +0200115 }
Juha Hyttinen14f82732020-02-27 13:49:06 +0200116 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
137type PlmnIdentityBcd struct {
138 PlmnIdentity
139}
140
141func (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 Hyttinenff8dccd2019-12-10 14:34:07 +0200151 }
Juha Hyttinen14f82732020-02-27 13:49:06 +0200152
153 buf := BCD.Encode(tmpStr)
154 return writer.Write(buf)
155}
156
157func (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 Hyttinenff8dccd2019-12-10 14:34:07 +0200173}