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