• 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/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 
isRadioOff()73   bool isRadioOff() const { return radio_state_ == RADIO_STATE_OFF; }
74 
75  private:
76   void InitializeServiceState();
77   std::vector<CommandHandler> InitializeCommandHandlers();
78   void InitializeNetworkOperator();
79   void InitializeSimOperator();
80 
81   bool WakeupFromSleep();
82   bool IsHasNetwork();
83   void UpdateRegisterState(RegistrationState state);
84   void AdjustSignalStrengthValue(int& value, const std::pair<int, int>& range);
85   void SetSignalStrengthValue(int& value, const std::pair<int, int>& range,
86                               double percentd);
87   std::string GetSignalStrength();
88   void applySignalPercentage(double percentd);
89 
90   MiscService* misc_service_ = nullptr;
91   SimService* sim_service_ = nullptr;
92   DataService* data_service_ = nullptr;
93 
94   enum RadioState : int32_t {
95     RADIO_STATE_OFF,
96     RADIO_STATE_ON,
97   };
98   RadioState radio_state_;
99 
100   /* Operator */
101   struct NetworkOperator {
102     enum OperatorState {
103       OPER_STATE_UNKNOWN    = 0,
104       OPER_STATE_AVAILABLE  = 1,
105       OPER_STATE_CURRENT    = 2,
106       OPER_STATE_FORBIDDEN  = 3
107     };
108 
109     std::string numeric;
110     std::string long_name;
111     std::string short_name;
112     OperatorState operator_state;
113 
NetworkOperatorNetworkOperator114     NetworkOperator() {}
115 
NetworkOperatorNetworkOperator116     NetworkOperator(const std::string& number,
117                     const std::string& ln,
118                     const std::string& sn,
119                     OperatorState state)
120         : numeric(number),
121           long_name(ln),
122           short_name(sn),
123           operator_state(state) {}
124   };
125 
126   enum OperatorSelectionMode {
127     OPER_SELECTION_AUTOMATIC = 0,
128     OPER_SELECTION_MANUAL,
129     OPER_SELECTION_DEREGISTRATION,
130     OPER_SELECTION_SET_FORMAT,
131     OPER_SELECTION_MANUAL_AUTOMATIC
132   };
133 
134   std::vector<NetworkOperator> operator_list_;
135   std::string current_operator_numeric_ = "";
136   OperatorSelectionMode oper_selection_mode_;
137 
138   /* SignalStrength */
139   struct SignalStrength {
140     int gsm_rssi;  /* Valid values are (0-31, 99) as defined in TS 27.007 8.5 */
141     int gsm_ber;   /* bit error rate (0-7, 99) as defined in TS 27.007 8.5 */
142 
143     int cdma_dbm;   /* Valid values are positive integers.  This value is the actual RSSI value
144                      * multiplied by -1.  Example: If the actual RSSI is -75, then this response
145                      * value will be 75.
146                      */
147     int cdma_ecio;  /* Valid values are positive integers.  This value is the actual Ec/Io multiplied
148                      * by -10.  Example: If the actual Ec/Io is -12.5 dB, then this response value
149                      * will be 125.
150                      */
151 
152     int evdo_dbm;   /* Refer cdma_dbm */
153     int evdo_ecio;  /* Refer cdma_ecio */
154     int evdo_snr;   /* Valid values are 0-8.  8 is the highest signal to noise ratio. */
155 
156     int lte_rssi;   /* Refer gsm_rssi */
157     int lte_rsrp;   /* The current Reference Signal Receive Power in dBm multiplied by -1.
158                      * Range: 44 to 140 dBm
159                      * INT_MAX: 0x7FFFFFFF denotes invalid value.
160                      * Reference: 3GPP TS 36.133 9.1.4 */
161     int lte_rsrq;   /* The current Reference Signal Receive Quality in dB multiplied by -1.
162                      * Range: 20 to 3 dB.
163                      * INT_MAX: 0x7FFFFFFF denotes invalid value.
164                      * Reference: 3GPP TS 36.133 9.1.7 */
165     int lte_rssnr;  /* The current reference signal signal-to-noise ratio in 0.1 dB units.
166                      * Range: -200 to +300 (-200 = -20.0 dB, +300 = 30dB).
167                      * INT_MAX : 0x7FFFFFFF denotes invalid value.
168                      * Reference: 3GPP TS 36.101 8.1.1 */
169     int lte_cqi;    /* The current Channel Quality Indicator.
170                      * Range: 0 to 15.
171                      * INT_MAX : 0x7FFFFFFF denotes invalid value.
172                      * Reference: 3GPP TS 36.101 9.2, 9.3, A.4 */
173     int lte_ta;     /* timing advance in micro seconds for a one way trip from cell to device.
174                      * Approximate distance can be calculated using 300m/us * timingAdvance.
175                      * Range: 0 to 0x7FFFFFFE
176                      * INT_MAX : 0x7FFFFFFF denotes invalid value.
177                      * Reference: 3GPP 36.321 section 6.1.3.5 */
178 
179     int tdscdma_rscp;   /* P-CCPCH RSCP as defined in TS 25.225 5.1.1
180                          * Valid values are (0-96, 255) as defined in TS 27.007 8.69
181                          * INT_MAX denotes that the value is invalid/unreported. */
182 
183     int wcdma_rssi;  /* Refer gsm_rssi */
184     int wcdma_ber;   /* Refer gsm_ber */
185 
186     int32_t nr_ss_rsrp;   /* SS reference signal received power, multiplied by -1.
187                            * Reference: 3GPP TS 38.215.
188                            * Range [44, 140], INT_MAX means invalid/unreported. */
189     int32_t nr_ss_rsrq;   /* SS reference signal received quality, multiplied by -1.
190                            * Reference: 3GPP TS 38.215.
191                            * Range [3, 20], INT_MAX means invalid/unreported. */
192     int32_t nr_ss_sinr;   /* SS signal-to-noise and interference ratio.
193                            * Reference: 3GPP TS 38.215 section 5.1.*, 3GPP TS 38.133 section 10.1.16.1.
194                            * Range [-23, 40], INT_MAX means invalid/unreported. */
195     int32_t nr_csi_rsrp;  /* CSI reference signal received power, multiplied by -1.
196                            * Reference: 3GPP TS 38.215.
197                            * Range [44, 140], INT_MAX means invalid/unreported. */
198     int32_t nr_csi_rsrq;  /* CSI reference signal received quality, multiplied by -1.
199                            * Reference: 3GPP TS 38.215.
200                            * Range [3, 20], INT_MAX means invalid/unreported. */
201     int32_t nr_csi_sinr;  /* CSI signal-to-noise and interference ratio.
202                            * Reference: 3GPP TS 138.215 section 5.1.*, 3GPP TS 38.133 section 10.1.16.1.
203                            * Range [-23, 40], INT_MAX means invalid/unreported. */
204 
205     // Default invalid value
SignalStrengthSignalStrength206     SignalStrength():
207       gsm_rssi(99),     // [0, 31]
208       gsm_ber(0),       // [7, 99]
209       cdma_dbm(125),    // [0, 120]
210       cdma_ecio(165),   // [0, 160]
211       evdo_dbm(125),    // [0, 120]
212       evdo_ecio(165),   // [0, 160]
213       evdo_snr(-1),     // [0, 8]
214       lte_rssi(99),     // [0, 31]
215       lte_rsrp(-1),     // [43,140]
216       lte_rsrq(-5),     // [-3,34]
217       lte_rssnr(-205),  // [-200, 300]
218       lte_cqi(-1),      // [0, 15]
219       lte_ta(-1),       // [0, 1282]
220       tdscdma_rscp(99), // [0, 96]
221       wcdma_rssi(99),   // [0, 31]
222       wcdma_ber(0),     // [7, 99]
223       nr_ss_rsrp(0),    // [44, 140]
224       nr_ss_rsrq(0),    // [3, 10]
225       nr_ss_sinr(45),   // [-23,40]
226       nr_csi_rsrp(0),   // [44, 140]
227       nr_csi_rsrq(0),   // [3, 20]
228       nr_csi_sinr(30)   // [-23, 23]
229       {}
230 
231     // After radio power on, off, or set network mode, reset to invalid value
ResetSignalStrength232     void Reset() {
233       gsm_rssi = INT_MAX;
234       gsm_ber = INT_MAX;
235       cdma_dbm = INT_MAX;
236       cdma_ecio = INT_MAX;
237       evdo_dbm = INT_MAX;
238       evdo_ecio = INT_MAX;
239       evdo_snr = INT_MAX;
240       lte_rssi = INT_MAX;
241       lte_rsrp = INT_MAX;
242       lte_rsrq = INT_MAX;
243       lte_rssnr = INT_MAX;
244       lte_cqi = INT_MAX;
245       lte_ta = INT_MAX;
246       tdscdma_rscp = INT_MAX;
247       wcdma_rssi = INT_MAX;
248       wcdma_ber = INT_MAX;
249       nr_ss_rsrp = INT_MAX;
250       nr_ss_rsrq = INT_MAX;
251       nr_ss_sinr = INT_MAX;
252       nr_csi_rsrp = INT_MAX;
253       nr_csi_rsrq = INT_MAX;
254       nr_csi_sinr = INT_MAX;
255     }
256   };
257 
258   double percentd_{0.8};
259   SignalStrength signal_strength_;
260 
261   /* Data / voice Registration State */
262   struct NetworkRegistrationStatus {
263     enum RegistrationUnsolMode {
264       REGISTRATION_UNSOL_DISABLED     = 0,
265       REGISTRATION_UNSOL_ENABLED      = 1,
266       REGISTRATION_UNSOL_ENABLED_FULL = 2
267     };
268 
269     enum AccessTechnoloy {
270       ACESS_TECH_GSM          = 0,
271       ACESS_TECH_GSM_COMPACT  = 1,
272       ACESS_TECH_UTRAN        = 2,
273       ACESS_TECH_EGPRS        = 3,
274       ACESS_TECH_HSDPA        = 4,
275       ACESS_TECH_HSUPA        = 5,
276       ACESS_TECH_HSPA         = 6,
277       ACESS_TECH_EUTRAN       = 7,
278       ACESS_TECH_EC_GSM_IoT   = 8,
279       ACESS_TECH_E_UTRAN      = 9,
280       ACESS_TECH_E_UTRA       = 10,
281       ACESS_TECH_NR           = 11,
282       ACESS_TECH_NG_RAN       = 12,
283       ACESS_TECH_E_UTRA_NR    = 13
284     };
285 
NetworkRegistrationStatusNetworkRegistrationStatus286     NetworkRegistrationStatus() :
287       unsol_mode(REGISTRATION_UNSOL_ENABLED_FULL),
288       registration_state(NET_REGISTRATION_UNREGISTERED),
289       network_type(ACESS_TECH_EUTRAN) {}
290 
291     RegistrationUnsolMode unsol_mode;
292     RegistrationState registration_state;
293     AccessTechnoloy network_type;
294   };
295 
296   NetworkRegistrationStatus voice_registration_status_;
297   NetworkRegistrationStatus data_registration_status_;
298 
299   enum ModemTechnology {
300     M_MODEM_TECH_GSM    = 1 << 0,
301     M_MODEM_TECH_WCDMA  = 1 << 1,
302     M_MODEM_TECH_CDMA   = 1 << 2,
303     M_MODEM_TECH_EVDO   = 1 << 3,
304     M_MODEM_TECH_TDSCDMA= 1 << 4,
305     M_MODEM_TECH_LTE    = 1 << 5,
306     M_MODEM_TECH_NR     = 1 << 6,
307   };
308   ModemTechnology current_network_mode_;
309   int preferred_network_mode_;
310   int modem_radio_capability_;
311 
312   NetworkRegistrationStatus::AccessTechnoloy getNetworkTypeFromTech(ModemTechnology modemTech);
313   int getModemTechFromPrefer(int preferred_mask);
314   ModemTechnology getTechFromNetworkType(NetworkRegistrationStatus::AccessTechnoloy act);
315 
316   bool first_signal_strength_request_;  // For time update
317   time_t android_last_signal_time_;
318 };
319 
320 }  // namespace cuttlefish
321