1 /*
2  * Copyright (C) 2021 The Android Open Source Project
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 #include "utils.h"
18 
19 namespace android::hardware::radio::compat {
20 
21 namespace RAF {
22 using E = V1_4::RadioAccessFamily;
23 constexpr auto GSM = E::GSM | E::GPRS;
24 constexpr auto CDMA = E::IS95A | E::IS95B | E::ONE_X_RTT;
25 constexpr auto EVDO = E::EVDO_0 | E::EVDO_A | E::EVDO_B | E::EHRPD;
26 constexpr auto HS = E::HSUPA | E::HSDPA | E::HSPA | E::HSPAP;
27 constexpr auto WCDMA = HS | E::UMTS;
28 constexpr auto LTE = E::LTE | E::LTE_CA;
29 constexpr auto NR = E::NR;
30 }  // namespace RAF
31 
32 static hidl_bitfield<V1_4::RadioAccessFamily>  //
getAdjustedRaf(hidl_bitfield<V1_4::RadioAccessFamily> raf)33 getAdjustedRaf(hidl_bitfield<V1_4::RadioAccessFamily> raf) {
34     if (raf & RAF::GSM) raf |= RAF::GSM;
35     if (raf & RAF::WCDMA) raf |= RAF::WCDMA;
36     if (raf & RAF::CDMA) raf |= RAF::CDMA;
37     if (raf & RAF::EVDO) raf |= RAF::EVDO;
38     if (raf & RAF::LTE) raf |= RAF::LTE;
39     if (raf & RAF::NR) raf |= RAF::NR;
40 
41     return raf;
42 }
43 
getNetworkTypeFromRaf(hidl_bitfield<V1_4::RadioAccessFamily> raf)44 V1_0::PreferredNetworkType getNetworkTypeFromRaf(hidl_bitfield<V1_4::RadioAccessFamily> raf) {
45     raf = getAdjustedRaf(raf);
46     switch (raf) {
47         case RAF::GSM | RAF::WCDMA:
48             return V1_0::PreferredNetworkType::GSM_WCDMA_AUTO;
49         case RAF::GSM:
50             return V1_0::PreferredNetworkType::GSM_ONLY;
51         case RAF::WCDMA:
52             return V1_0::PreferredNetworkType::WCDMA;
53         case (RAF::CDMA | RAF::EVDO):
54             return V1_0::PreferredNetworkType::CDMA_EVDO_AUTO;
55         case (RAF::LTE | RAF::CDMA | RAF::EVDO):
56             return V1_0::PreferredNetworkType::LTE_CDMA_EVDO;
57         case (RAF::LTE | RAF::GSM | RAF::WCDMA):
58             return V1_0::PreferredNetworkType::LTE_GSM_WCDMA;
59         case (RAF::LTE | RAF::CDMA | RAF::EVDO | RAF::GSM | RAF::WCDMA):
60             return V1_0::PreferredNetworkType::LTE_CMDA_EVDO_GSM_WCDMA;  // CDMA typo
61         case RAF::LTE:
62             return V1_0::PreferredNetworkType::LTE_ONLY;
63         case (RAF::LTE | RAF::WCDMA):
64             return V1_0::PreferredNetworkType::LTE_WCDMA;
65         case RAF::CDMA:
66             return V1_0::PreferredNetworkType::CDMA_ONLY;
67         case RAF::EVDO:
68             return V1_0::PreferredNetworkType::EVDO_ONLY;
69         case (RAF::GSM | RAF::WCDMA | RAF::CDMA | RAF::EVDO):
70             return V1_0::PreferredNetworkType::GSM_WCDMA_CDMA_EVDO_AUTO;
71         case static_cast<int>(RAF::E::TD_SCDMA):
72             return V1_0::PreferredNetworkType::TD_SCDMA_ONLY;
73         case (RAF::E::TD_SCDMA | RAF::WCDMA):
74             return V1_0::PreferredNetworkType::TD_SCDMA_WCDMA;
75         case (RAF::LTE | RAF::E::TD_SCDMA):
76             return V1_0::PreferredNetworkType::TD_SCDMA_LTE;
77         case (RAF::E::TD_SCDMA | RAF::GSM):
78             return V1_0::PreferredNetworkType::TD_SCDMA_GSM;
79         case (RAF::LTE | RAF::E::TD_SCDMA | RAF::GSM):
80             return V1_0::PreferredNetworkType::TD_SCDMA_GSM_LTE;
81         case (RAF::E::TD_SCDMA | RAF::GSM | RAF::WCDMA):
82             return V1_0::PreferredNetworkType::TD_SCDMA_GSM_WCDMA;
83         case (RAF::LTE | RAF::E::TD_SCDMA | RAF::WCDMA):
84             return V1_0::PreferredNetworkType::TD_SCDMA_WCDMA_LTE;
85         case (RAF::LTE | RAF::E::TD_SCDMA | RAF::GSM | RAF::WCDMA):
86             return V1_0::PreferredNetworkType::TD_SCDMA_GSM_WCDMA_LTE;
87         case (RAF::E::TD_SCDMA | RAF::CDMA | RAF::EVDO | RAF::GSM | RAF::WCDMA):
88             return V1_0::PreferredNetworkType::TD_SCDMA_GSM_WCDMA_CDMA_EVDO_AUTO;
89         case (RAF::LTE | RAF::E::TD_SCDMA | RAF::CDMA | RAF::EVDO | RAF::GSM | RAF::WCDMA):
90             return V1_0::PreferredNetworkType::TD_SCDMA_LTE_CDMA_EVDO_GSM_WCDMA;
91         case static_cast<int>(RAF::NR):
92             return V1_0::PreferredNetworkType(23);  //  NR_ONLY
93         case (RAF::NR | RAF::LTE):
94             return V1_0::PreferredNetworkType(24);  //  NR_LTE
95         case (RAF::NR | RAF::LTE | RAF::CDMA | RAF::EVDO):
96             return V1_0::PreferredNetworkType(25);  //  NR_LTE_CDMA_EVDO
97         case (RAF::NR | RAF::LTE | RAF::GSM | RAF::WCDMA):
98             return V1_0::PreferredNetworkType(26);  //  NR_LTE_GSM_WCDMA
99         case (RAF::NR | RAF::LTE | RAF::CDMA | RAF::EVDO | RAF::GSM | RAF::WCDMA):
100             return V1_0::PreferredNetworkType(27);  //  NR_LTE_CDMA_EVDO_GSM_WCDMA
101         case (RAF::NR | RAF::LTE | RAF::WCDMA):
102             return V1_0::PreferredNetworkType(28);  //  NR_LTE_WCDMA
103         case (RAF::NR | RAF::LTE | RAF::E::TD_SCDMA):
104             return V1_0::PreferredNetworkType(29);  //  NR_LTE_TDSCDMA
105         case (RAF::NR | RAF::LTE | RAF::E::TD_SCDMA | RAF::GSM):
106             return V1_0::PreferredNetworkType(30);  //  NR_LTE_TDSCDMA_GSM
107         case (RAF::NR | RAF::LTE | RAF::E::TD_SCDMA | RAF::WCDMA):
108             return V1_0::PreferredNetworkType(31);  //  NR_LTE_TDSCDMA_WCDMA
109         case (RAF::NR | RAF::LTE | RAF::E::TD_SCDMA | RAF::GSM | RAF::WCDMA):
110             return V1_0::PreferredNetworkType(32);  //  NR_LTE_TDSCDMA_GSM_WCDMA
111         case (RAF::NR | RAF::LTE | RAF::E::TD_SCDMA | RAF::CDMA | RAF::EVDO | RAF::GSM |
112               RAF::WCDMA):
113             return V1_0::PreferredNetworkType(33);  //  NR_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA
114         default:
115             return V1_0::PreferredNetworkType::WCDMA;
116     }
117 }
118 
getRafFromNetworkType(V1_0::PreferredNetworkType type)119 hidl_bitfield<V1_4::RadioAccessFamily> getRafFromNetworkType(V1_0::PreferredNetworkType type) {
120     switch (type) {
121         case V1_0::PreferredNetworkType::GSM_WCDMA_AUTO:
122             return RAF::GSM | RAF::WCDMA;
123         case V1_0::PreferredNetworkType::GSM_ONLY:
124             return RAF::GSM;
125         case V1_0::PreferredNetworkType::WCDMA:
126             return RAF::WCDMA;
127         case V1_0::PreferredNetworkType::CDMA_EVDO_AUTO:
128             return (RAF::CDMA | RAF::EVDO);
129         case V1_0::PreferredNetworkType::LTE_CDMA_EVDO:
130             return (RAF::LTE | RAF::CDMA | RAF::EVDO);
131         case V1_0::PreferredNetworkType::LTE_GSM_WCDMA:
132             return (RAF::LTE | RAF::GSM | RAF::WCDMA);
133         case V1_0::PreferredNetworkType::LTE_CMDA_EVDO_GSM_WCDMA:
134             return (RAF::LTE | RAF::CDMA | RAF::EVDO | RAF::GSM | RAF::WCDMA);
135         case V1_0::PreferredNetworkType::LTE_ONLY:
136             return RAF::LTE;
137         case V1_0::PreferredNetworkType::LTE_WCDMA:
138             return (RAF::LTE | RAF::WCDMA);
139         case V1_0::PreferredNetworkType::CDMA_ONLY:
140             return RAF::CDMA;
141         case V1_0::PreferredNetworkType::EVDO_ONLY:
142             return RAF::EVDO;
143         case V1_0::PreferredNetworkType::GSM_WCDMA_CDMA_EVDO_AUTO:
144             return (RAF::GSM | RAF::WCDMA | RAF::CDMA | RAF::EVDO);
145         case V1_0::PreferredNetworkType::TD_SCDMA_ONLY:
146             return static_cast<int>(RAF::E::TD_SCDMA);
147         case V1_0::PreferredNetworkType::TD_SCDMA_WCDMA:
148             return (RAF::E::TD_SCDMA | RAF::WCDMA);
149         case V1_0::PreferredNetworkType::TD_SCDMA_LTE:
150             return (RAF::LTE | RAF::E::TD_SCDMA);
151         case V1_0::PreferredNetworkType::TD_SCDMA_GSM:
152             return (RAF::E::TD_SCDMA | RAF::GSM);
153         case V1_0::PreferredNetworkType::TD_SCDMA_GSM_LTE:
154             return (RAF::LTE | RAF::E::TD_SCDMA | RAF::GSM);
155         case V1_0::PreferredNetworkType::TD_SCDMA_GSM_WCDMA:
156             return (RAF::E::TD_SCDMA | RAF::GSM | RAF::WCDMA);
157         case V1_0::PreferredNetworkType::TD_SCDMA_WCDMA_LTE:
158             return (RAF::LTE | RAF::E::TD_SCDMA | RAF::WCDMA);
159         case V1_0::PreferredNetworkType::TD_SCDMA_GSM_WCDMA_LTE:
160             return (RAF::LTE | RAF::E::TD_SCDMA | RAF::GSM | RAF::WCDMA);
161         case V1_0::PreferredNetworkType::TD_SCDMA_GSM_WCDMA_CDMA_EVDO_AUTO:
162             return (RAF::E::TD_SCDMA | RAF::CDMA | RAF::EVDO | RAF::GSM | RAF::WCDMA);
163         case V1_0::PreferredNetworkType::TD_SCDMA_LTE_CDMA_EVDO_GSM_WCDMA:
164             return (RAF::LTE | RAF::E::TD_SCDMA | RAF::CDMA | RAF::EVDO | RAF::GSM | RAF::WCDMA);
165 #pragma GCC diagnostic push
166 #pragma GCC diagnostic ignored "-Wswitch"
167         case V1_0::PreferredNetworkType(23):  //  NR_ONLY
168             return static_cast<int>(RAF::NR);
169         case V1_0::PreferredNetworkType(24):  //  NR_LTE
170             return (RAF::NR | RAF::LTE);
171         case V1_0::PreferredNetworkType(25):  //  NR_LTE_CDMA_EVDO
172             return (RAF::NR | RAF::LTE | RAF::CDMA | RAF::EVDO);
173         case V1_0::PreferredNetworkType(26):  //  NR_LTE_GSM_WCDMA
174             return (RAF::NR | RAF::LTE | RAF::GSM | RAF::WCDMA);
175         case V1_0::PreferredNetworkType(27):  //  NR_LTE_CDMA_EVDO_GSM_WCDMA
176             return (RAF::NR | RAF::LTE | RAF::CDMA | RAF::EVDO | RAF::GSM | RAF::WCDMA);
177         case V1_0::PreferredNetworkType(28):  //  NR_LTE_WCDMA
178             return (RAF::NR | RAF::LTE | RAF::WCDMA);
179         case V1_0::PreferredNetworkType(29):  //  NR_LTE_TDSCDMA
180             return (RAF::NR | RAF::LTE | RAF::E::TD_SCDMA);
181         case V1_0::PreferredNetworkType(30):  //  NR_LTE_TDSCDMA_GSM
182             return (RAF::NR | RAF::LTE | RAF::E::TD_SCDMA | RAF::GSM);
183         case V1_0::PreferredNetworkType(31):  //  NR_LTE_TDSCDMA_WCDMA
184             return (RAF::NR | RAF::LTE | RAF::E::TD_SCDMA | RAF::WCDMA);
185         case V1_0::PreferredNetworkType(32):  //  NR_LTE_TDSCDMA_GSM_WCDMA
186             return (RAF::NR | RAF::LTE | RAF::E::TD_SCDMA | RAF::GSM | RAF::WCDMA);
187         case V1_0::PreferredNetworkType(33):  //  NR_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA
188             return (RAF::NR | RAF::LTE | RAF::E::TD_SCDMA | RAF::CDMA | RAF::EVDO | RAF::GSM |
189                     RAF::WCDMA);
190 #pragma GCC diagnostic pop
191         default:
192             return {};  // unknown
193     }
194 }
195 
196 }  // namespace android::hardware::radio::compat
197