1 //
2 // Copyright (C) 2020 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 #pragma once
17 
18 #include <ctime>
19 
20 #include "host/commands/modem_simulator/data_service.h"
21 #include "host/commands/modem_simulator/misc_service.h"
22 #include "host/commands/modem_simulator/modem_service.h"
23 #include "host/commands/modem_simulator/sim_service.h"
24 
25 namespace cuttlefish {
26 
27 class NetworkService : public ModemService, public std::enable_shared_from_this<NetworkService> {
28  public:
29   NetworkService(int32_t service_id_, ChannelMonitor* channel_monitor,
30                  ThreadLooper* thread_looper);
31   ~NetworkService() = default;
32 
33   NetworkService(const NetworkService &) = delete;
34   NetworkService &operator=(const NetworkService &) = delete;
35 
36   void SetupDependency(MiscService* misc, SimService* sim, DataService* data);
37 
38   void HandleRadioPowerReq(const Client& client);
39   void HandleRadioPower(const Client& client, std::string& command);
40   void HandleSignalStrength(const Client& client);
41   void HandleQueryNetworkSelectionMode(const Client& client);
42   void HandleRequestOperator(const Client& client);
43   void HandleQueryAvailableNetwork(const Client& client);
44   void HandleSetNetworkSelectionMode(const Client& client, std::string& command);
45   void HandleVoiceNetworkRegistration(const Client& client, std::string& command);
46   void HandleDataNetworkRegistration(const Client& client, std::string& command);
47   void HandleGetPreferredNetworkType(const Client& client);
48   void HandleQuerySupportedTechs(const Client& client);
49   void HandleSetPreferredNetworkType(const Client& client, std::string& command);
50   void HandleNetworkRegistration(cuttlefish::SharedFD client, std::string& command);
51 
52   void HandleReceiveRemoteVoiceDataReg(const Client& client,
53                                        std::string& command);
54   void HandleReceiveRemoteCTEC(const Client& client, std::string& command);
55   void HandleReceiveRemoteSignal(const Client& client, std::string& command);
56 
57   void OnSimStatusChanged(SimService::SimStatus sim_status);
58   void OnVoiceRegisterStateChanged();
59   void OnDataRegisterStateChanged();
60   void OnSignalStrengthChanged();
61 
62   enum RegistrationState {
63     NET_REGISTRATION_UNREGISTERED = 0,
64     NET_REGISTRATION_HOME         = 1,
65     NET_REGISTRATION_SEARCHING    = 2,
66     NET_REGISTRATION_DENIED       = 3,
67     NET_REGISTRATION_UNKNOWN      = 4,
68     NET_REGISTRATION_ROAMING      = 5,
69     NET_REGISTRATION_EMERGENCY    = 8
70   };
71   RegistrationState GetVoiceRegistrationState() const;
72 
73  private:
74   void InitializeServiceState();
75   std::vector<CommandHandler> InitializeCommandHandlers();
76   void InitializeNetworkOperator();
77   void InitializeSimOperator();
78 
79   bool WakeupFromSleep();
80   bool IsHasNetwork();
81   void UpdateRegisterState(RegistrationState state);
82   void AdjustSignalStrengthValue(int& value, const std::pair<int, int>& range);
83   void SetSignalStrengthValue(int& value, const std::pair<int, int>& range,
84                               double percentd);
85   std::string GetSignalStrength();
86 
87   MiscService* misc_service_ = nullptr;
88   SimService* sim_service_ = nullptr;
89   DataService* data_service_ = nullptr;
90 
91   enum RadioState : int32_t {
92     RADIO_STATE_OFF,
93     RADIO_STATE_ON,
94   };
95   RadioState radio_state_;
96 
97   /* Operator */
98   struct NetworkOperator {
99     enum OperatorState {
100       OPER_STATE_UNKNOWN    = 0,
101       OPER_STATE_AVAILABLE  = 1,
102       OPER_STATE_CURRENT    = 2,
103       OPER_STATE_FORBIDDEN  = 3
104     };
105 
106     std::string numeric;
107     std::string long_name;
108     std::string short_name;
109     OperatorState operator_state;
110 
NetworkOperatorNetworkOperator111     NetworkOperator() {}
112 
NetworkOperatorNetworkOperator113     NetworkOperator(const std::string& number,
114                     const std::string& ln,
115                     const std::string& sn,
116                     OperatorState state)
117         : numeric(number),
118           long_name(ln),
119           short_name(sn),
120           operator_state(state) {}
121   };
122 
123   enum OperatorSelectionMode {
124     OPER_SELECTION_AUTOMATIC = 0,
125     OPER_SELECTION_MANUAL,
126     OPER_SELECTION_DEREGISTRATION,
127     OPER_SELECTION_SET_FORMAT,
128     OPER_SELECTION_MANUAL_AUTOMATIC
129   };
130 
131   std::vector<NetworkOperator> operator_list_;
132   std::string current_operator_numeric_ = "";
133   OperatorSelectionMode oper_selection_mode_;
134 
135   /* SignalStrength */
136   struct SignalStrength {
137     int gsm_rssi;  /* Valid values are (0-31, 99) as defined in TS 27.007 8.5 */
138     int gsm_ber;   /* bit error rate (0-7, 99) as defined in TS 27.007 8.5 */
139 
140     int cdma_dbm;   /* Valid values are positive integers.  This value is the actual RSSI value
141                      * multiplied by -1.  Example: If the actual RSSI is -75, then this response
142                      * value will be 75.
143                      */
144     int cdma_ecio;  /* Valid values are positive integers.  This value is the actual Ec/Io multiplied
145                      * by -10.  Example: If the actual Ec/Io is -12.5 dB, then this response value
146                      * will be 125.
147                      */
148 
149     int evdo_dbm;   /* Refer cdma_dbm */
150     int evdo_ecio;  /* Refer cdma_ecio */
151     int evdo_snr;   /* Valid values are 0-8.  8 is the highest signal to noise ratio. */
152 
153     int lte_rssi;   /* Refer gsm_rssi */
154     int lte_rsrp;   /* The current Reference Signal Receive Power in dBm multiplied by -1.
155                      * Range: 44 to 140 dBm
156                      * INT_MAX: 0x7FFFFFFF denotes invalid value.
157                      * Reference: 3GPP TS 36.133 9.1.4 */
158     int lte_rsrq;   /* The current Reference Signal Receive Quality in dB multiplied by -1.
159                      * Range: 20 to 3 dB.
160                      * INT_MAX: 0x7FFFFFFF denotes invalid value.
161                      * Reference: 3GPP TS 36.133 9.1.7 */
162     int lte_rssnr;  /* The current reference signal signal-to-noise ratio in 0.1 dB units.
163                      * Range: -200 to +300 (-200 = -20.0 dB, +300 = 30dB).
164                      * INT_MAX : 0x7FFFFFFF denotes invalid value.
165                      * Reference: 3GPP TS 36.101 8.1.1 */
166     int lte_cqi;    /* The current Channel Quality Indicator.
167                      * Range: 0 to 15.
168                      * INT_MAX : 0x7FFFFFFF denotes invalid value.
169                      * Reference: 3GPP TS 36.101 9.2, 9.3, A.4 */
170     int lte_ta;     /* timing advance in micro seconds for a one way trip from cell to device.
171                      * Approximate distance can be calculated using 300m/us * timingAdvance.
172                      * Range: 0 to 0x7FFFFFFE
173                      * INT_MAX : 0x7FFFFFFF denotes invalid value.
174                      * Reference: 3GPP 36.321 section 6.1.3.5 */
175 
176     int tdscdma_rscp;   /* P-CCPCH RSCP as defined in TS 25.225 5.1.1
177                          * Valid values are (0-96, 255) as defined in TS 27.007 8.69
178                          * INT_MAX denotes that the value is invalid/unreported. */
179 
180     int wcdma_rssi;  /* Refer gsm_rssi */
181     int wcdma_ber;   /* Refer gsm_ber */
182 
183     int32_t nr_ss_rsrp;   /* SS reference signal received power, multiplied by -1.
184                            * Reference: 3GPP TS 38.215.
185                            * Range [44, 140], INT_MAX means invalid/unreported. */
186     int32_t nr_ss_rsrq;   /* SS reference signal received quality, multiplied by -1.
187                            * Reference: 3GPP TS 38.215.
188                            * Range [3, 20], INT_MAX means invalid/unreported. */
189     int32_t nr_ss_sinr;   /* SS signal-to-noise and interference ratio.
190                            * Reference: 3GPP TS 38.215 section 5.1.*, 3GPP TS 38.133 section 10.1.16.1.
191                            * Range [-23, 40], INT_MAX means invalid/unreported. */
192     int32_t nr_csi_rsrp;  /* CSI reference signal received power, multiplied by -1.
193                            * Reference: 3GPP TS 38.215.
194                            * Range [44, 140], INT_MAX means invalid/unreported. */
195     int32_t nr_csi_rsrq;  /* CSI reference signal received quality, multiplied by -1.
196                            * Reference: 3GPP TS 38.215.
197                            * Range [3, 20], INT_MAX means invalid/unreported. */
198     int32_t nr_csi_sinr;  /* CSI signal-to-noise and interference ratio.
199                            * Reference: 3GPP TS 138.215 section 5.1.*, 3GPP TS 38.133 section 10.1.16.1.
200                            * Range [-23, 40], INT_MAX means invalid/unreported. */
201 
202     // Default invalid value
SignalStrengthSignalStrength203     SignalStrength():
204       gsm_rssi(99),     // [0, 31]
205       gsm_ber(0),       // [7, 99]
206       cdma_dbm(125),    // [0, 120]
207       cdma_ecio(165),   // [0, 160]
208       evdo_dbm(125),    // [0, 120]
209       evdo_ecio(165),   // [0, 160]
210       evdo_snr(-1),     // [0, 8]
211       lte_rssi(99),     // [0, 31]
212       lte_rsrp(-1),     // [43,140]
213       lte_rsrq(-5),     // [-3,34]
214       lte_rssnr(-205),  // [-200, 300]
215       lte_cqi(-1),      // [0, 15]
216       lte_ta(-1),       // [0, 1282]
217       tdscdma_rscp(99), // [0, 96]
218       wcdma_rssi(99),   // [0, 31]
219       wcdma_ber(0),     // [7, 99]
220       nr_ss_rsrp(0),    // [44, 140]
221       nr_ss_rsrq(0),    // [3, 10]
222       nr_ss_sinr(45),   // [-23,40]
223       nr_csi_rsrp(0),   // [44, 140]
224       nr_csi_rsrq(0),   // [3, 20]
225       nr_csi_sinr(30)   // [-23, 23]
226       {}
227 
228     // After radio power on, off, or set network mode, reset to invalid value
ResetSignalStrength229     void Reset() {
230       gsm_rssi = 99;
231       gsm_ber = 0;
232       cdma_dbm = 125;
233       cdma_ecio = 165;
234       evdo_dbm = 125;
235       evdo_ecio = 165;
236       evdo_snr = -1;
237       lte_rssi = 99;
238       lte_rsrp = -1;
239       lte_rsrq = -5;
240       lte_rssnr = -205;
241       lte_cqi = -1;
242       lte_ta = -1;
243       tdscdma_rscp = 99;
244       wcdma_rssi = 99;
245       wcdma_ber = 0;
246       nr_ss_rsrp = 0;
247       nr_ss_rsrq = 0;
248       nr_ss_sinr = 45;
249       nr_csi_rsrp = 0;
250       nr_csi_rsrq = 0;
251       nr_csi_sinr = 30;
252     }
253   };
254 
255   SignalStrength signal_strength_;
256 
257   /* Data / voice Registration State */
258   struct NetworkRegistrationStatus {
259     enum RegistrationUnsolMode {
260       REGISTRATION_UNSOL_DISABLED     = 0,
261       REGISTRATION_UNSOL_ENABLED      = 1,
262       REGISTRATION_UNSOL_ENABLED_FULL = 2
263     };
264 
265     enum AccessTechnoloy {
266       ACESS_TECH_GSM          = 0,
267       ACESS_TECH_GSM_COMPACT  = 1,
268       ACESS_TECH_UTRAN        = 2,
269       ACESS_TECH_EGPRS        = 3,
270       ACESS_TECH_HSDPA        = 4,
271       ACESS_TECH_HSUPA        = 5,
272       ACESS_TECH_HSPA         = 6,
273       ACESS_TECH_EUTRAN       = 7,
274       ACESS_TECH_EC_GSM_IoT   = 8,
275       ACESS_TECH_E_UTRAN      = 9,
276       ACESS_TECH_E_UTRA       = 10,
277       ACESS_TECH_NR           = 11,
278       ACESS_TECH_NG_RAN       = 12,
279       ACESS_TECH_E_UTRA_NR    = 13
280     };
281 
NetworkRegistrationStatusNetworkRegistrationStatus282     NetworkRegistrationStatus() :
283       unsol_mode(REGISTRATION_UNSOL_ENABLED_FULL),
284       registration_state(NET_REGISTRATION_UNREGISTERED),
285       network_type(ACESS_TECH_EUTRAN) {}
286 
287     RegistrationUnsolMode unsol_mode;
288     RegistrationState registration_state;
289     AccessTechnoloy network_type;
290   };
291 
292   NetworkRegistrationStatus voice_registration_status_;
293   NetworkRegistrationStatus data_registration_status_;
294 
295   enum ModemTechnology {
296     M_MODEM_TECH_GSM    = 1 << 0,
297     M_MODEM_TECH_WCDMA  = 1 << 1,
298     M_MODEM_TECH_CDMA   = 1 << 2,
299     M_MODEM_TECH_EVDO   = 1 << 3,
300     M_MODEM_TECH_TDSCDMA= 1 << 4,
301     M_MODEM_TECH_LTE    = 1 << 5,
302     M_MODEM_TECH_NR     = 1 << 6,
303   };
304   ModemTechnology current_network_mode_;
305   int preferred_network_mode_;
306   int modem_radio_capability_;
307 
308   NetworkRegistrationStatus::AccessTechnoloy getNetworkTypeFromTech(ModemTechnology modemTech);
309   int getModemTechFromPrefer(int preferred_mask);
310   ModemTechnology getTechFromNetworkType(NetworkRegistrationStatus::AccessTechnoloy act);
311 
312   bool first_signal_strength_request_;  // For time update
313   time_t android_last_signal_time_;
314 };
315 
316 }  // namespace cuttlefish
317