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 #include "host/commands/modem_simulator/stk_service.h"
17
18 #include <android-base/logging.h>
19 namespace cuttlefish {
20
StkService(int32_t service_id,ChannelMonitor * channel_monitor,ThreadLooper * thread_looper)21 StkService::StkService(int32_t service_id, ChannelMonitor* channel_monitor,
22 ThreadLooper* thread_looper)
23 : ModemService(service_id, this->InitializeCommandHandlers(),
24 channel_monitor, thread_looper) {}
25
InitializeCommandHandlers()26 std::vector<CommandHandler> StkService::InitializeCommandHandlers() {
27 std::vector<CommandHandler> command_handlers = {
28 CommandHandler("+CUSATD?",
29 [this](const Client& client) {
30 this->HandleReportStkServiceIsRunning(client);
31 }),
32 CommandHandler("+CUSATE=",
33 [this](const Client& client, std::string& cmd) {
34 this->HandleSendEnvelope(client, cmd);
35 }),
36 CommandHandler("+CUSATT=",
37 [this](const Client& client, std::string& cmd) {
38 this->HandleSendTerminalResponseToSim(client, cmd);
39 }),
40 };
41 return (command_handlers);
42 }
43
SetupDependency(SimService * sim)44 void StkService::SetupDependency(SimService* sim) { sim_service_ = sim; }
45
46 /**
47 * AT+CUSATD
48 * This command determines if, and optionally which profile should be downloaded
49 * to the UICC automatically upon start-up.
50 *
51 * Command Possible response(s)
52 * +CUSATD=[<download>[,<reporting>]] +CME ERROR: <err>
53 * +CUSATD? +CUSATD: <download>,<reporting>
54 *
55 * <download>: integer type.
56 * 0 Download MT default profile automatically during next start-up.
57 * 1 Download the combined TE and MT profile
58 * 2 Halt next UICC start-up when ready for profile download.
59 * <reporting>: integer type.
60 * 0 Disable +CUSATS, i.e. no notification.
61 * 1 Enable +CUSATS, i.e. notify TE.
62 *
63 * see RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING in RIL
64 */
HandleReportStkServiceIsRunning(const Client & client)65 void StkService::HandleReportStkServiceIsRunning(const Client& client) {
66 std::vector<std::string> response = {{"+CUSATD: 0,1"}, {"OK"}};
67 client.SendCommandResponse(response);
68
69 if (!sim_service_) return;
70
71 XMLElement *root = sim_service_->GetIccProfile();
72 if (!root) return;
73
74 XMLElement *setup_menu = root->FirstChildElement("SETUPMENU");
75 auto text = setup_menu->FindAttribute("text");
76
77 std::string unsol_command = "+CUSATP:";
78 unsol_command += text ? text->Value() : "";
79 SendUnsolicitedCommand(unsol_command);
80 }
81
82 /**
83 * AT+CUSATE
84 * Execution command allows the TE to send a USAT envelope command to the MT
85 *
86 * Command Possible response(s)
87 * +CUSATE=<envelope_command> +CUSATE: <envelope_response>[,<busy>]
88 * [<CR><LF>+CUSATE2: <sw1>,<sw2>]
89 * +CME ERROR: <err>
90 *
91 * <envelope_command>: string type in hexadecimal character format.
92 * <envelope_response>: string type in hexadecimal character format.
93 * <busy>: integer type.
94 * 0 UICC indicated normal ending of the command.
95 * 1 UICC responded with USAT is busy, no retry by the MT.
96 * 2 UICC responded with USAT is busy even after one or more retries by the MT.
97 * <sw1>: integer type.
98 * <sw2>: integer type.
99 *
100 * see RIL_REQUEST_STK_SEND_ENVELOPE_COMMAND in RIL
101 */
HandleSendEnvelope(const Client & client,std::string & command)102 void StkService::HandleSendEnvelope(const Client& client , std::string& command) {
103 std::vector<std::string> response = {{"+CUSATE: 0"}, {"OK"}};
104 client.SendCommandResponse(response);
105
106 if (!sim_service_) return;
107
108 CommandParser cmd(command);
109 cmd.SkipPrefix();
110 auto data = cmd.GetNextStr();
111 std::string menu_id(data.substr(data.size() - 2)); // get the last two char
112
113 XMLElement *root = sim_service_->GetIccProfile();
114 if (!root) return;
115
116 XMLElement *setup_menu = root->FirstChildElement("SETUPMENU");
117 if (!setup_menu) return;
118
119 auto select_item = setup_menu->FirstChildElement("SELECTITEM");
120 while (select_item) {
121 auto menu_id_attr = select_item->FindAttribute("menuId");
122 if (menu_id_attr && menu_id_attr->Value() == menu_id) {
123 break;
124 }
125 select_item = select_item->NextSiblingElement("SELECTITEM");
126 }
127 if (!select_item) {
128 LOG(ERROR) << "Not found menu id: " << menu_id;
129 return;
130 }
131
132 auto select_item_cmd = select_item->FindAttribute("cmd");
133 if (select_item_cmd) {
134 std::string cmd_str = select_item_cmd->Value();
135 if (cmd_str == "24") { // SELECT_ITEM
136 current_select_item_menu_ids_.push_back(menu_id);
137 }
138 }
139
140 std::string unsol_command = "+CUSATP:";
141 auto text = select_item->FindAttribute("text");
142 std::string text_value = text ? text->Value() : "";
143 unsol_command.append(text_value);
144 SendUnsolicitedCommand(unsol_command);
145 }
146
147 /**
148 * AT+CUSATT
149 * Execution command sends a USAT terminal response to the MT as an answer to
150 * a preceding USAT proactive command sent from the UICC with unsolicited result
151 * code +CUSATP: <proactive_command>
152 *
153 * Command Possible response(s)
154 * +CUSATT=<terminal_response> +CME ERROR: <err>
155 *
156 * <terminal_response>: string type in hexadecimal character format.
157 *
158 * see RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE in RIL
159 */
HandleSendTerminalResponseToSim(const Client & client,std::string & command)160 void StkService::HandleSendTerminalResponseToSim(const Client& client, std::string& command) {
161 std::vector<std::string> response = {{"+CUSATT: 0"}, {"OK"}};
162 client.SendCommandResponse(response);
163
164 OnUnsolicitedCommandForTR(command);
165 }
166
GetCurrentSelectItem()167 XMLElement* StkService::GetCurrentSelectItem() {
168 if (!sim_service_) return nullptr;
169
170 XMLElement *root = sim_service_->GetIccProfile();
171 if (!root) {
172 current_select_item_menu_ids_.clear();
173 return nullptr;
174 }
175
176 XMLElement *menu = root->FirstChildElement("SETUPMENU");
177 if (!menu) {
178 current_select_item_menu_ids_.clear();
179 return nullptr;
180 }
181
182 /**
183 * e.g. current_select_item_menu_ids_: {"1", "02"}
184 * <SELECTITEM id="1">
185 * <SELECTITEM id="01">
186 * </SELECTITEM>
187 * <SELECTITEM id="02">
188 * </SELECTITEM>
189 * </SELECTITEM>
190 */
191 XMLElement* select_item = nullptr;
192 auto iter = current_select_item_menu_ids_.begin();
193 for (; iter != current_select_item_menu_ids_.end(); ++iter) {
194 select_item = menu->FirstChildElement("SELECTITEM");
195 while (select_item) {
196 auto menu_id_attr = select_item->FindAttribute("menuId");
197 if (menu_id_attr && menu_id_attr->Value() == *iter) {
198 auto menu_id_str = menu_id_attr->Value();
199 if (menu_id_str == *iter) break;
200 }
201 select_item = select_item->NextSiblingElement("SELECTITEM");
202 }
203 if (!select_item) break;
204 menu = select_item;
205 }
206
207 return select_item;
208 }
209
OnUnsolicitedCommandForTR(std::string & command)210 void StkService::OnUnsolicitedCommandForTR(std::string& command) {
211 CommandParser cmd(command);
212 cmd.SkipPrefix();
213 auto data = cmd.GetNextStr();
214 auto menu_id = data.substr(data.size() - 2);
215
216 // '10': UICC_SESSION_TERM_BY_USER
217 // '12': NO_RESPONSE_FROM_USER
218 if (menu_id == "10" || menu_id == "12") {
219 current_select_item_menu_ids_.clear();
220 SendUnsolicitedCommand("+CUSATEND");
221 return;
222 }
223
224 XMLElement *select_item = GetCurrentSelectItem();
225 if (!select_item) {
226 current_select_item_menu_ids_.clear();
227 SendUnsolicitedCommand("+CUSATEND");
228 return;
229 }
230
231 if (menu_id == "11") { // BACKWARD_MOVE_BY_USER
232 current_select_item_menu_ids_.pop_back();
233 if (current_select_item_menu_ids_.size() >= 1) {
234 select_item = GetCurrentSelectItem();
235 auto text = select_item->FindAttribute("text");
236 if (text) {
237 std::string unsol_command = "+CUSATP: ";
238 unsol_command += text->Value();
239 SendUnsolicitedCommand(unsol_command);
240 }
241 } else {
242 SendUnsolicitedCommand("+CUSATEND");
243 }
244 return;
245 } else if (menu_id == "00") { // OK
246 auto text = select_item->FindAttribute("text");
247 if (text) {
248 std::string unsol_command = "+CUSATP: ";
249 unsol_command += text->Value();
250 SendUnsolicitedCommand(unsol_command);
251 }
252 return;
253 }
254
255 auto final = select_item->FirstChildElement();
256 while (final) {
257 auto attr = final->FindAttribute("menuId");
258 if (attr && attr->Value() == menu_id) {
259 std::string attr_value = attr->Value();
260 if (attr_value == menu_id) break;
261 }
262 final = final->NextSiblingElement();
263 }
264 if (!final) {
265 current_select_item_menu_ids_.clear();
266 SendUnsolicitedCommand("+CUSATEND");
267 return;
268 }
269
270 auto cmd_attr = final->FindAttribute("cmd");
271 if (cmd_attr) {
272 std::string cmd_attr_str = cmd_attr->Value();
273 if (cmd_attr_str == "24") {
274 std::string menu_id_str(menu_id);
275 current_select_item_menu_ids_.push_back(menu_id_str);
276 }
277 }
278 auto text = final->FindAttribute("text");
279 std::string unsol_command = "+CUSATP:";
280 unsol_command += text ? text->Value() : "";
281 SendUnsolicitedCommand(unsol_command);
282 }
283
284 } // namespace cuttlefish
285