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