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