RICPLT-2910
[ric-plt/submgr.git] / e2ap / pkg / conv / plmn.go
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
22 //-----------------------------------------------------------------------------
23 //
24 // MCC 3 digits MNC 2 digits
25 // BCD Coded format: 0xC2C1 0xfC3 0xN2N1
26 // String format   : C1C2C3N1N2
27 //
28 // MCC 3 digits MNC 3 digits
29 // BCD Coded format: 0xC2C1 0xN3C3 0xN2N1
30 // String format   : C1C2C3N1N2N3
31 //
32 //-----------------------------------------------------------------------------
33
34 type PlmnIdentity struct {
35         Val [3]uint8
36 }
37
38 func (plmnid *PlmnIdentity) String() string {
39         bcd := NewBcd("0123456789?????f")
40
41         str := bcd.Decode(plmnid.Val[:])
42
43         if str[3] == 'f' {
44                 return string(str[0:3]) + string(str[4:])
45         }
46         return string(str[0:3]) + string(str[4:]) + string(str[3])
47 }
48
49 func (plmnid *PlmnIdentity) MccString() string {
50         fullstr := plmnid.String()
51         return string(fullstr[0:3])
52 }
53
54 func (plmnid *PlmnIdentity) MncString() string {
55         fullstr := plmnid.String()
56         return string(fullstr[3:])
57 }
58
59 func (plmnid *PlmnIdentity) StringPut(str string) bool {
60
61         var tmpStr string
62         switch {
63
64         case len(str) == 5:
65                 //C1 C2 C3 N1 N2 -->
66                 //C2C1 0fC3 N2N1
67                 tmpStr = string(str[0:3]) + string("f") + string(str[3:])
68         case len(str) == 6:
69                 //C1 C2 C3 N1 N2 N3 -->
70                 //C2C1 N3C3 N2N1
71                 tmpStr = string(str[0:3]) + string(str[5]) + string(str[3:5])
72         default:
73                 return false
74         }
75
76         bcd := NewBcd("0123456789?????f")
77         buf := bcd.Encode(tmpStr)
78
79         if buf == nil {
80                 return false
81         }
82
83         return plmnid.BcdPut(buf)
84 }
85
86 func (plmnid *PlmnIdentity) BcdPut(val []uint8) bool {
87
88         if len(val) != 3 {
89                 return false
90         }
91         for i := 0; i < 3; i++ {
92                 plmnid.Val[i] = val[i]
93         }
94         return true
95 }