• 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 <tinyxml2.h>
19 
20 #include "host/commands/modem_simulator/modem_service.h"
21 
22 namespace cuttlefish {
23 
24 using namespace tinyxml2;
25 
26 class NetworkService;
27 
28 class SimService : public ModemService, public std::enable_shared_from_this<SimService> {
29  public:
30   SimService(int32_t service_id, ChannelMonitor* channel_monitor,
31              ThreadLooper* thread_looper);
32   ~SimService() = default;
33 
34   SimService(const SimService &) = delete;
35   SimService &operator=(const SimService &) = delete;
36 
37   void SetupDependency(NetworkService* net);
38 
39   void HandleSIMStatusReq(const Client& client);
40   void HandleChangeOrEnterPIN(const Client& client, const std::string& command);
41   void HandleSIM_IO(const Client& client, const std::string& command);
42   void HandleCSIM_IO(const Client& client, const std::string& command);
43   void HandleGetIMSI(const Client& client);
44   void HandleGetIccId(const Client& client);
45   void HandleFacilityLock(const Client& client, const std::string& command);
46   void HandleOpenLogicalChannel(const Client& client,
47                                 const std::string& command);
48   void HandleCloseLogicalChannel(const Client& client,
49                                  const std::string& command);
50   void HandleTransmitLogicalChannel(const Client& client,
51                                     const std::string& command);
52   void HandleChangePassword(const Client& client, const std::string& command);
53   void HandleQueryRemainTimes(const Client& client, const std::string& command);
54   void HandleCdmaSubscriptionSource(const Client& client,
55                                     const std::string& command);
56   void HandleCdmaRoamingPreference(const Client& client,
57                                    const std::string& command);
58   void HandleSimAuthentication(const Client& client,
59                                    const std::string& command);
60 
61   void SavePinStateToIccProfile();
62   void SaveFacilityLockToIccProfile();
63   bool IsFDNEnabled();
64   bool IsFixedDialNumber(std::string_view number);
65   XMLElement* GetIccProfile();
66   std::string GetPhoneNumber();
67 
68   enum SimStatus {
69     SIM_STATUS_ABSENT = 0,
70     SIM_STATUS_NOT_READY,
71     SIM_STATUS_READY,
72     SIM_STATUS_PIN,
73     SIM_STATUS_PUK,
74   };
75 
76   SimStatus GetSimStatus() const;
77   std::string GetSimOperator();
78 
79  private:
80   void InitializeServiceState();
81   std::vector<CommandHandler> InitializeCommandHandlers();
82   void InitializeSimFileSystemAndSimState();
83   void InitializeFacilityLock();
84   void OnSimStatusChanged();
85 
86   NetworkService* network_service_;
87 
88   /* SimStatus */
89   SimStatus sim_status_;
90 
91   /* SimFileSystem */
92   struct SimFileSystem {
93     enum EFId: int32_t {
94       EF_ADN = 0x6F3A,
95       EF_FDN = 0x6F3B,
96       EF_GID1 = 0x6F3E,
97       EF_GID2 = 0x6F3F,
98       EF_SDN = 0x6F49,
99       EF_EXT1 = 0x6F4A,
100       EF_EXT2 = 0x6F4B,
101       EF_EXT3 = 0x6F4C,
102       EF_EXT5 = 0x6F4E,
103       EF_EXT6 = 0x6FC8,   // Ext record for EF[MBDN]
104       EF_MWIS = 0x6FCA,
105       EF_MBDN = 0x6FC7,
106       EF_PNN = 0x6FC5,
107       EF_OPL = 0x6FC6,
108       EF_SPN = 0x6F46,
109       EF_SMS = 0x6F3C,
110       EF_ICCID = 0x2FE2,
111       EF_AD = 0x6FAD,
112       EF_MBI = 0x6FC9,
113       EF_MSISDN = 0x6F40,
114       EF_SPDI = 0x6FCD,
115       EF_SST = 0x6F38,
116       EF_CFIS = 0x6FCB,
117       EF_IMG = 0x4F20,
118 
119       // USIM SIM file ids from TS 131.102
120       EF_PBR = 0x4F30,
121       EF_LI = 0x6F05,
122 
123       // GSM SIM file ids from CPHS (phase 2, version 4.2) CPHS4_2.WW6
124       EF_MAILBOX_CPHS = 0x6F17,
125       EF_VOICE_MAIL_INDICATOR_CPHS = 0x6F11,
126       EF_CFF_CPHS = 0x6F13,
127       EF_SPN_CPHS = 0x6F14,
128       EF_SPN_SHORT_CPHS = 0x6F18,
129       EF_INFO_CPHS = 0x6F16,
130       EF_CSP_CPHS = 0x6F15,
131 
132       // CDMA RUIM file ids from 3GPP2 C.S0023-0
133       EF_CST = 0x6F32,
134       EF_RUIM_SPN =0x6F41,
135 
136       // ETSI TS.102.221
137       EF_PL = 0x2F05,
138       // 3GPP2 C.S0065
139       EF_CSIM_LI = 0x6F3A,
140       EF_CSIM_SPN =0x6F41,
141       EF_CSIM_MDN = 0x6F44,
142       EF_CSIM_IMSIM = 0x6F22,
143       EF_CSIM_CDMAHOME = 0x6F28,
144       EF_CSIM_EPRL = 0x6F5A,
145       EF_CSIM_MIPUPP = 0x6F4D,
146 
147       //ISIM access
148       EF_IMPU = 0x6F04,
149       EF_IMPI = 0x6F02,
150       EF_DOMAIN = 0x6F03,
151       EF_IST = 0x6F07,
152       EF_PCSCF = 0x6F09,
153       EF_PSI = 0x6FE5,
154 
155       //PLMN Selection Information w/ Access Technology TS 131.102
156       EF_PLMN_W_ACT = 0x6F60,
157       EF_OPLMN_W_ACT = 0x6F61,
158       EF_HPLMN_W_ACT = 0x6F62,
159 
160       //Equivalent Home and Forbidden PLMN Lists TS 131.102
161       EF_EHPLMN = 0x6FD9,
162       EF_FPLMN = 0x6F7B,
163 
164       // Last Roaming Selection Indicator
165       EF_LRPLMNSI = 0x6FDC,
166 
167       //Search interval for higher priority PLMNs
168       EF_HPPLMN = 0x6F31,
169     };
170 
171     XMLElement* GetRootElement();
172 
173     static std::string GetCommonIccEFPath(EFId efid);
174     static std::string GetUsimEFPath(EFId efid);
175 
176     static XMLElement *FindAttribute(XMLElement* parent,
177                                      const std::string& attr_name,
178                                      const std::string& attr_value);
179 
180     XMLElement* AppendNewElement(XMLElement* parent, const char* name);
181     XMLElement* AppendNewElementWithText(XMLElement* parent, const char* name,
182                                          const char* text);
183 
184     XMLDocument doc;
185     std::string file_path;
186   };
187   SimFileSystem sim_file_system_;
188 
189 
190   /* PinStatus */
191   struct PinStatus {
192     enum ChangeMode {WITH_PIN, WITH_PUK};
193 
194     std::string pin_;
195     std::string puk_;
196     int pin_remaining_times_;
197     int puk_remaining_times_;
198 
199     bool CheckPasswordValid(std::string_view password);
200 
201     bool VerifyPIN(const std::string_view pin);
202     bool VerifyPUK(const std::string_view puk);
203     bool ChangePIN(ChangeMode mode, const std::string_view pin_or_puk,
204                    const std::string_view new_pin);
205     bool ChangePUK(const std::string_view puk, const std::string_view new_puk);
206   };
207   PinStatus pin1_status_;
208   PinStatus pin2_status_;
209 
210   bool checkPin1AndAdjustSimStatus(std::string_view password);
211   bool ChangePin1AndAdjustSimStatus(PinStatus::ChangeMode mode,
212                                     std::string_view pin,
213                                     std::string_view new_pin);
214 
215   /*  FacilityLock */
216   struct FacilityLock {
217     enum LockType {
218       AO = 1,  // Barr all outgoing calls
219       OI = 2,  // Barr all outgoing international calls
220       OX = 3,  // Barr all outgoing international calls, except to Home Country
221       AI = 4,  // Barr all incoming calls
222       IR = 5,  // Barr all call, when roaming outside Home Country
223       AB = 6,  // All barring services
224       AG = 7,  // All outgoing barring services
225       AC = 8,  // All incoming barring services
226       SC = 9,  // PIN enable/disable
227       FD = 10,  // SIM fixed FDN dialing lock, PIN2 is required as a password
228     };
229 
230     enum Mode {
231       UNLOCK = 0,
232       LOCK = 1,
233       QUERY = 2,
234     };
235 
236     enum Class : int32_t {
237       DEFAULT = 7,      // all classes
238       VOICE = 1 << 0,   // telephony
239       DATA = 1 << 1,    // to all bear service
240       FAX = 1 << 2,     // facsimile services
241       SMS = 1 << 3,     // short message services
242     };
243 
244     enum LockStatus {
245       DISABLE,
246       ENABLE,
247     };
248 
249     LockStatus lock_status;  // Ignore class
250 
FacilityLockFacilityLock251     FacilityLock(LockStatus status) : lock_status(status) {}
252   };
253   std::map<std::string, FacilityLock> facility_lock_;
254 
255   /* LogicalChannel */
256   struct LogicalChannel {
257     std::string df_name;
258     bool is_open;
259     int session_id;
260 
LogicalChannelLogicalChannel261     LogicalChannel(int session_id) :
262       df_name(""), is_open(false), session_id(session_id) {};
263   };
264   std::vector<LogicalChannel> logical_channels_;
265   std::string last_file_id_;
266 
267   int cdma_subscription_source_;
268   int cdma_roaming_preference_;
269 };
270 
271 }  // namespace cuttlefish
272