• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/network_service_constants.h"
24 #include "host/commands/modem_simulator/sim_service.h"
25 
26 namespace cuttlefish {
27 
28 class NetworkService : public ModemService, public std::enable_shared_from_this<NetworkService> {
29  public:
30   NetworkService(int32_t service_id_, ChannelMonitor* channel_monitor,
31                  ThreadLooper* thread_looper);
32   ~NetworkService() = default;
33 
34   NetworkService(const NetworkService &) = delete;
35   NetworkService &operator=(const NetworkService &) = delete;
36 
37   void SetupDependency(MiscService* misc, SimService* sim, DataService* data);
38 
39   void HandleRadioPowerReq(const Client& client);
40   void HandleRadioPower(const Client& client, std::string& command);
41   void HandleSignalStrength(const Client& client);
42   void HandleQueryNetworkSelectionMode(const Client& client);
43   void HandleRequestOperator(const Client& client);
44   void HandleQueryAvailableNetwork(const Client& client);
45   void HandleSetNetworkSelectionMode(const Client& client, std::string& command);
46   void HandleVoiceNetworkRegistration(const Client& client, std::string& command);
47   void HandleDataNetworkRegistration(const Client& client, std::string& command);
48   void HandleGetPreferredNetworkType(const Client& client);
49   void HandleQuerySupportedTechs(const Client& client);
50   void HandleSetPreferredNetworkType(const Client& client, std::string& command);
51   void HandleNetworkRegistration(cuttlefish::SharedFD client, std::string& command);
52 
53   void HandleReceiveRemoteVoiceDataReg(const Client& client,
54                                        std::string& command);
55   void HandleReceiveRemoteCTEC(const Client& client, std::string& command);
56   void HandleReceiveRemoteSignal(const Client& client, std::string& command);
57 
58   void OnSimStatusChanged(SimService::SimStatus sim_status);
59   void OnVoiceRegisterStateChanged();
60   void OnDataRegisterStateChanged();
61   void OnSignalStrengthChanged();
62 
63   enum RegistrationState {
64     NET_REGISTRATION_UNREGISTERED = 0,
65     NET_REGISTRATION_HOME         = 1,
66     NET_REGISTRATION_SEARCHING    = 2,
67     NET_REGISTRATION_DENIED       = 3,
68     NET_REGISTRATION_UNKNOWN      = 4,
69     NET_REGISTRATION_ROAMING      = 5,
70     NET_REGISTRATION_EMERGENCY    = 8
71   };
72   RegistrationState GetVoiceRegistrationState() const;
73 
isRadioOff()74   bool isRadioOff() const { return radio_state_ == RADIO_STATE_OFF; }
75 
76  private:
77   void InitializeServiceState();
78   std::vector<CommandHandler> InitializeCommandHandlers();
79   void InitializeNetworkOperator();
80   void InitializeSimOperator();
81 
82   bool WakeupFromSleep();
83   bool IsHasNetwork();
84   void UpdateRegisterState(RegistrationState state);
85   void AdjustSignalStrengthValue(int& value, const std::pair<int, int>& range);
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 
SignalStrengthSignalStrength202     SignalStrength()
203         : gsm_rssi(kRssiUnknownValue),
204           gsm_ber(kBerUnknownValue),
205           cdma_dbm(kDbmUnknownValue),
206           cdma_ecio(kEcioUnknownValue),
207           evdo_dbm(kDbmUnknownValue),
208           evdo_ecio(kEcioUnknownValue),
209           evdo_snr(kSnrUnknownValue),
210           lte_rssi(kRssiUnknownValue),
211           lte_rsrp(INT_MAX),
212           lte_rsrq(INT_MAX),
213           lte_rssnr(INT_MAX),
214           lte_cqi(INT_MAX),
215           lte_ta(INT_MAX),
216           tdscdma_rscp(INT_MAX),
217           wcdma_rssi(kRssiUnknownValue),
218           wcdma_ber(kBerUnknownValue),
219           nr_ss_rsrp(INT_MAX),
220           nr_ss_rsrq(INT_MAX),
221           nr_ss_sinr(INT_MAX),
222           nr_csi_rsrp(INT_MAX),
223           nr_csi_rsrq(INT_MAX),
224           nr_csi_sinr(INT_MAX) {}
225   };
226 
227   // There's no such thing as a percentange for signal strength in the real
228   // world, as for example for battery usage, this percent value is used to pick
229   // a value within the corresponding signal strength values range for emulation
230   // purposes only.
231   int signal_strength_percent_{80};
232 
233   static int GetValueInRange(const std::pair<int, int>& range, int percent);
234   static std::string BuildCSQCommandResponse(
235       const SignalStrength& signal_strength);
236   SignalStrength GetCurrentSignalStrength();
237 
238   /* Data / voice Registration State */
239   struct NetworkRegistrationStatus {
240     enum RegistrationUnsolMode {
241       REGISTRATION_UNSOL_DISABLED     = 0,
242       REGISTRATION_UNSOL_ENABLED      = 1,
243       REGISTRATION_UNSOL_ENABLED_FULL = 2
244     };
245 
246     enum AccessTechnoloy {
247       ACESS_TECH_GSM          = 0,
248       ACESS_TECH_GSM_COMPACT  = 1,
249       ACESS_TECH_UTRAN        = 2,
250       ACESS_TECH_EGPRS        = 3,
251       ACESS_TECH_HSDPA        = 4,
252       ACESS_TECH_HSUPA        = 5,
253       ACESS_TECH_HSPA         = 6,
254       ACESS_TECH_EUTRAN       = 7,
255       ACESS_TECH_EC_GSM_IoT   = 8,
256       ACESS_TECH_E_UTRAN      = 9,
257       ACESS_TECH_E_UTRA       = 10,
258       ACESS_TECH_NR           = 11,
259       ACESS_TECH_NG_RAN       = 12,
260       ACESS_TECH_E_UTRA_NR    = 13
261     };
262 
NetworkRegistrationStatusNetworkRegistrationStatus263     NetworkRegistrationStatus() :
264       unsol_mode(REGISTRATION_UNSOL_ENABLED_FULL),
265       registration_state(NET_REGISTRATION_UNREGISTERED),
266       network_type(ACESS_TECH_EUTRAN) {}
267 
268     RegistrationUnsolMode unsol_mode;
269     RegistrationState registration_state;
270     AccessTechnoloy network_type;
271   };
272 
273   NetworkRegistrationStatus voice_registration_status_;
274   NetworkRegistrationStatus data_registration_status_;
275 
276   enum ModemTechnology {
277     M_MODEM_TECH_GSM    = 1 << 0,
278     M_MODEM_TECH_WCDMA  = 1 << 1,
279     M_MODEM_TECH_CDMA   = 1 << 2,
280     M_MODEM_TECH_EVDO   = 1 << 3,
281     M_MODEM_TECH_TDSCDMA= 1 << 4,
282     M_MODEM_TECH_LTE    = 1 << 5,
283     M_MODEM_TECH_NR     = 1 << 6,
284   };
285   ModemTechnology current_network_mode_;
286   int preferred_network_mode_;
287   int modem_radio_capability_;
288 
289   NetworkRegistrationStatus::AccessTechnoloy getNetworkTypeFromTech(ModemTechnology modemTech);
290   int getModemTechFromPrefer(int preferred_mask);
291   ModemTechnology getTechFromNetworkType(NetworkRegistrationStatus::AccessTechnoloy act);
292 
293   bool first_signal_strength_request_;  // For time update
294   time_t android_last_signal_time_;
295 
296   class KeepSignalStrengthChangingLoop {
297    public:
298     KeepSignalStrengthChangingLoop(NetworkService& network_service);
299     void Start();
300 
301    private:
302     void UpdateSignalStrengthCallback();
303 
304     NetworkService& network_service_;
305     std::atomic_flag loop_started_;
306   };
307 
308   KeepSignalStrengthChangingLoop keep_signal_strength_changing_loop_;
309 };
310 
311 }  // namespace cuttlefish
312