blob: 962d564db5806041cda3a89c7d4a03d516907864 [file] [log] [blame]
Martin Bouchet0d4bbaf2017-09-23 04:54:37 -03001/*
2 * Copyright (c) 2016 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
17#define LOG_TAG "RIL_SAP"
18
19#include <android/hardware/radio/1.0/ISap.h>
20
21#include <hwbinder/IPCThreadState.h>
22#include <hwbinder/ProcessState.h>
23#include <sap_service.h>
24#include "pb_decode.h"
25#include "pb_encode.h"
26
27using namespace android::hardware::radio::V1_0;
28using ::android::hardware::Return;
29using ::android::hardware::hidl_vec;
30using ::android::hardware::hidl_array;
31using ::android::hardware::Void;
32using android::CommandInfo;
33using android::RequestInfo;
34using android::requestToString;
35using android::sp;
36
37struct SapImpl;
38
39#if (SIM_COUNT >= 2)
40sp<SapImpl> sapService[SIM_COUNT];
41#else
42sp<SapImpl> sapService[1];
43#endif
44
45struct SapImpl : public ISap {
46 int32_t slotId;
47 sp<ISapCallback> sapCallback;
48 RIL_SOCKET_ID rilSocketId;
49
50 Return<void> setCallback(const ::android::sp<ISapCallback>& sapCallbackParam);
51
52 Return<void> connectReq(int32_t token, int32_t maxMsgSize);
53
54 Return<void> disconnectReq(int32_t token);
55
56 Return<void> apduReq(int32_t token, SapApduType type, const hidl_vec<uint8_t>& command);
57
58 Return<void> transferAtrReq(int32_t token);
59
60 Return<void> powerReq(int32_t token, bool state);
61
62 Return<void> resetSimReq(int32_t token);
63
64 Return<void> transferCardReaderStatusReq(int32_t token);
65
66 Return<void> setTransferProtocolReq(int32_t token, SapTransferProtocol transferProtocol);
67
68 MsgHeader* createMsgHeader(MsgId msgId, int32_t token);
69
70 Return<void> addPayloadAndDispatchRequest(MsgHeader *msg, uint16_t reqLen, uint8_t *reqPtr);
71
72 void sendFailedResponse(MsgId msgId, int32_t token, int numPointers, ...);
73
74 void checkReturnStatus(Return<void>& ret);
75};
76
77void SapImpl::checkReturnStatus(Return<void>& ret) {
78 if (ret.isOk() == false) {
79 RLOGE("checkReturnStatus: unable to call response/indication callback: %s",
80 ret.description().c_str());
81 // Remote process (SapRilReceiver.java) hosting the callback must be dead. Reset the
82 // callback object; there's no other recovery to be done here. When the client process is
83 // back up, it will call setCallback()
84 sapCallback = NULL;
85 }
86}
87
88Return<void> SapImpl::setCallback(const ::android::sp<ISapCallback>& sapCallbackParam) {
89 RLOGD("SapImpl::setCallback for slotId %d", slotId);
90 sapCallback = sapCallbackParam;
91 return Void();
92}
93
94MsgHeader* SapImpl::createMsgHeader(MsgId msgId, int32_t token) {
95 // Memory for msg will be freed by RilSapSocket::onRequestComplete()
96 MsgHeader *msg = (MsgHeader *)calloc(1, sizeof(MsgHeader));
97 if (msg == NULL) {
98 return NULL;
99 }
100 msg->token = token;
101 msg->type = MsgType_REQUEST;
102 msg->id = msgId;
103 msg->error = Error_RIL_E_SUCCESS;
104 return msg;
105}
106
107Return<void> SapImpl::addPayloadAndDispatchRequest(MsgHeader *msg, uint16_t reqLen,
108 uint8_t *reqPtr) {
Gohulan Balachandran60a4e9d2017-10-20 09:37:52 -0700109 pb_bytes_array_t *payload = (pb_bytes_array_t *) malloc(sizeof(pb_bytes_array_t) - 1 + reqLen);
110 if (payload == NULL) {
Martin Bouchet0d4bbaf2017-09-23 04:54:37 -0300111 sendFailedResponse(msg->id, msg->token, 2, reqPtr, msg);
112 return Void();
113 }
Gohulan Balachandran60a4e9d2017-10-20 09:37:52 -0700114
115 msg->payload = payload;
Martin Bouchet0d4bbaf2017-09-23 04:54:37 -0300116 msg->payload->size = reqLen;
117 memcpy(msg->payload->bytes, reqPtr, reqLen);
118
119 RilSapSocket *sapSocket = RilSapSocket::getSocketById(rilSocketId);
120 if (sapSocket) {
121 RLOGD("SapImpl::addPayloadAndDispatchRequest: calling dispatchRequest");
122 sapSocket->dispatchRequest(msg);
123 } else {
124 RLOGE("SapImpl::addPayloadAndDispatchRequest: sapSocket is null");
Gohulan Balachandran60a4e9d2017-10-20 09:37:52 -0700125 sendFailedResponse(msg->id, msg->token, 3, payload, reqPtr, msg);
Martin Bouchet0d4bbaf2017-09-23 04:54:37 -0300126 return Void();
127 }
128 free(msg->payload);
129 free(reqPtr);
130 return Void();
131}
132
133void SapImpl::sendFailedResponse(MsgId msgId, int32_t token, int numPointers, ...) {
134 va_list ap;
135 va_start(ap, numPointers);
136 for (int i = 0; i < numPointers; i++) {
137 void *ptr = va_arg(ap, void *);
138 if (ptr) free(ptr);
139 }
140 va_end(ap);
141 Return<void> retStatus;
142 switch(msgId) {
143 case MsgId_RIL_SIM_SAP_CONNECT:
144 retStatus = sapCallback->connectResponse(token, SapConnectRsp::CONNECT_FAILURE, 0);
145 break;
146
147 case MsgId_RIL_SIM_SAP_DISCONNECT:
148 retStatus = sapCallback->disconnectResponse(token);
149 break;
150
151 case MsgId_RIL_SIM_SAP_APDU: {
152 hidl_vec<uint8_t> apduRsp;
153 retStatus = sapCallback->apduResponse(token, SapResultCode::GENERIC_FAILURE, apduRsp);
154 break;
155 }
156
157 case MsgId_RIL_SIM_SAP_TRANSFER_ATR: {
158 hidl_vec<uint8_t> atr;
159 retStatus = sapCallback->transferAtrResponse(token, SapResultCode::GENERIC_FAILURE,
160 atr);
161 break;
162 }
163
164 case MsgId_RIL_SIM_SAP_POWER:
165 retStatus = sapCallback->powerResponse(token, SapResultCode::GENERIC_FAILURE);
166 break;
167
168 case MsgId_RIL_SIM_SAP_RESET_SIM:
169 retStatus = sapCallback->resetSimResponse(token, SapResultCode::GENERIC_FAILURE);
170 break;
171
172 case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS:
173 retStatus = sapCallback->transferCardReaderStatusResponse(token,
174 SapResultCode::GENERIC_FAILURE, 0);
175 break;
176
177 case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL:
178 retStatus = sapCallback->transferProtocolResponse(token, SapResultCode::NOT_SUPPORTED);
179 break;
180
181 default:
182 return;
183 }
184 sapService[slotId]->checkReturnStatus(retStatus);
185}
186
187Return<void> SapImpl::connectReq(int32_t token, int32_t maxMsgSize) {
188 RLOGD("SapImpl::connectReq");
189 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_CONNECT, token);
190 if (msg == NULL) {
191 RLOGE("SapImpl::connectReq: Error allocating memory for msg");
192 sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 0);
193 return Void();
194 }
195
196 /***** Encode RIL_SIM_SAP_CONNECT_REQ *****/
197 RIL_SIM_SAP_CONNECT_REQ req;
198 memset(&req, 0, sizeof(RIL_SIM_SAP_CONNECT_REQ));
199 req.max_message_size = maxMsgSize;
200
201 size_t encodedSize = 0;
202 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_CONNECT_REQ_fields, &req)) {
203 RLOGE("SapImpl::connectReq: Error getting encoded size for RIL_SIM_SAP_CONNECT_REQ");
204 sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 1, msg);
205 return Void();
206 }
207
208 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
209 if (buffer == NULL) {
210 RLOGE("SapImpl::connectReq: Error allocating memory for buffer");
211 sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 1, msg);
212 return Void();
213 }
214 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
215
216 RLOGD("SapImpl::connectReq calling pb_encode");
217 if (!pb_encode(&stream, RIL_SIM_SAP_CONNECT_REQ_fields, &req)) {
218 RLOGE("SapImpl::connectReq: Error encoding RIL_SIM_SAP_CONNECT_REQ");
219 sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 2, buffer, msg);
220 return Void();
221 }
222 /***** Encode RIL_SIM_SAP_CONNECT_REQ done *****/
223
224 /* encoded req is payload */
225 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
226}
227
228Return<void> SapImpl::disconnectReq(int32_t token) {
229 RLOGD("SapImpl::disconnectReq");
230 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_DISCONNECT, token);
231 if (msg == NULL) {
232 RLOGE("SapImpl::disconnectReq: Error allocating memory for msg");
233 sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 0);
234 return Void();
235 }
236
237 /***** Encode RIL_SIM_SAP_DISCONNECT_REQ *****/
238 RIL_SIM_SAP_DISCONNECT_REQ req;
239 memset(&req, 0, sizeof(RIL_SIM_SAP_DISCONNECT_REQ));
240
241 size_t encodedSize = 0;
242 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_DISCONNECT_REQ_fields, &req)) {
243 RLOGE("SapImpl::disconnectReq: Error getting encoded size for RIL_SIM_SAP_DISCONNECT_REQ");
244 sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 1, msg);
245 return Void();
246 }
247
248 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
249 if (buffer == NULL) {
250 RLOGE("SapImpl::disconnectReq: Error allocating memory for buffer");
251 sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 1, msg);
252 return Void();
253 }
254
255 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
256
257 RLOGD("SapImpl::disconnectReq calling pb_encode");
258 if (!pb_encode(&stream, RIL_SIM_SAP_DISCONNECT_REQ_fields, &req)) {
259 RLOGE("SapImpl::disconnectReq: Error encoding RIL_SIM_SAP_DISCONNECT_REQ");
260 sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 2, buffer, msg);
261 return Void();
262 }
263 /***** Encode RIL_SIM_SAP_DISCONNECT_REQ done *****/
264
265 /* encoded req is payload */
266 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
267}
268
269Return<void> SapImpl::apduReq(int32_t token, SapApduType type, const hidl_vec<uint8_t>& command) {
270 RLOGD("SapImpl::apduReq");
271 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_APDU, token);
272 if (msg == NULL) {
273 RLOGE("SapImpl::apduReq: Error allocating memory for msg");
274 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 0);
275 return Void();
276 }
277
278 /***** Encode RIL_SIM_SAP_APDU_REQ *****/
279 RIL_SIM_SAP_APDU_REQ req;
280 memset(&req, 0, sizeof(RIL_SIM_SAP_APDU_REQ));
281 req.type = (RIL_SIM_SAP_APDU_REQ_Type)type;
282
283 if (command.size() > 0) {
284 req.command = (pb_bytes_array_t *)malloc(sizeof(pb_bytes_array_t) - 1 + command.size());
285 if (req.command == NULL) {
286 RLOGE("SapImpl::apduReq: Error allocating memory for req.command");
287 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 1, msg);
288 return Void();
289 }
290 req.command->size = command.size();
291 memcpy(req.command->bytes, command.data(), command.size());
292 }
293
294 size_t encodedSize = 0;
295 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_APDU_REQ_fields, &req)) {
296 RLOGE("SapImpl::apduReq: Error getting encoded size for RIL_SIM_SAP_APDU_REQ");
297 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 2, req.command, msg);
298 return Void();
299 }
300
301 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
302 if (buffer == NULL) {
303 RLOGE("SapImpl::apduReq: Error allocating memory for buffer");
304 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 2, req.command, msg);
305 return Void();
306 }
307
308 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
309
310 RLOGD("SapImpl::apduReq calling pb_encode");
311 if (!pb_encode(&stream, RIL_SIM_SAP_APDU_REQ_fields, &req)) {
312 RLOGE("SapImpl::apduReq: Error encoding RIL_SIM_SAP_APDU_REQ");
313 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 3, req.command, buffer, msg);
314 return Void();
315 }
316 /***** Encode RIL_SIM_SAP_APDU_REQ done *****/
317
318 /* encoded req is payload */
319 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
320}
321
322Return<void> SapImpl::transferAtrReq(int32_t token) {
323 RLOGD("SapImpl::transferAtrReq");
324 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token);
325 if (msg == NULL) {
326 RLOGE("SapImpl::transferAtrReq: Error allocating memory for msg");
327 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 0);
328 return Void();
329 }
330
331 /***** Encode RIL_SIM_SAP_TRANSFER_ATR_REQ *****/
332 RIL_SIM_SAP_TRANSFER_ATR_REQ req;
333 memset(&req, 0, sizeof(RIL_SIM_SAP_TRANSFER_ATR_REQ));
334
335 size_t encodedSize = 0;
336 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_TRANSFER_ATR_REQ_fields, &req)) {
337 RLOGE("SapImpl::transferAtrReq: Error getting encoded size for "
338 "RIL_SIM_SAP_TRANSFER_ATR_REQ");
339 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 1, msg);
340 return Void();
341 }
342
343 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
344 if (buffer == NULL) {
345 RLOGE("SapImpl::transferAtrReq: Error allocating memory for buffer");
346 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 1, msg);
347 return Void();
348 }
349
350 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
351
352 RLOGD("SapImpl::transferAtrReq calling pb_encode");
353 if (!pb_encode(&stream, RIL_SIM_SAP_TRANSFER_ATR_REQ_fields, &req)) {
354 RLOGE("SapImpl::transferAtrReq: Error encoding RIL_SIM_SAP_TRANSFER_ATR_REQ");
355 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 2, buffer, msg);
356 return Void();
357 }
358 /***** Encode RIL_SIM_SAP_TRANSFER_ATR_REQ done *****/
359
360 /* encoded req is payload */
361 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
362}
363
364Return<void> SapImpl::powerReq(int32_t token, bool state) {
365 RLOGD("SapImpl::powerReq");
366 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_POWER, token);
367 if (msg == NULL) {
368 RLOGE("SapImpl::powerReq: Error allocating memory for msg");
369 sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 0);
370 return Void();
371 }
372
373 /***** Encode RIL_SIM_SAP_POWER_REQ *****/
374 RIL_SIM_SAP_POWER_REQ req;
375 memset(&req, 0, sizeof(RIL_SIM_SAP_POWER_REQ));
376 req.state = state;
377
378 size_t encodedSize = 0;
379 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_POWER_REQ_fields, &req)) {
380 RLOGE("SapImpl::powerReq: Error getting encoded size for RIL_SIM_SAP_POWER_REQ");
381 sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 1, msg);
382 return Void();
383 }
384
385 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
386 if (buffer == NULL) {
387 RLOGE("SapImpl::powerReq: Error allocating memory for buffer");
388 sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 1, msg);
389 return Void();
390 }
391
392 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
393
394 RLOGD("SapImpl::powerReq calling pb_encode");
395 if (!pb_encode(&stream, RIL_SIM_SAP_POWER_REQ_fields, &req)) {
396 RLOGE("SapImpl::powerReq: Error encoding RIL_SIM_SAP_POWER_REQ");
397 sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 2, buffer, msg);
398 return Void();
399 }
400 /***** Encode RIL_SIM_SAP_POWER_REQ done *****/
401
402 /* encoded req is payload */
403 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
404}
405
406Return<void> SapImpl::resetSimReq(int32_t token) {
407 RLOGD("SapImpl::resetSimReq");
408 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_RESET_SIM, token);
409 if (msg == NULL) {
410 RLOGE("SapImpl::resetSimReq: Error allocating memory for msg");
411 sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 0);
412 return Void();
413 }
414
415 /***** Encode RIL_SIM_SAP_RESET_SIM_REQ *****/
416 RIL_SIM_SAP_RESET_SIM_REQ req;
417 memset(&req, 0, sizeof(RIL_SIM_SAP_RESET_SIM_REQ));
418
419 size_t encodedSize = 0;
420 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_RESET_SIM_REQ_fields, &req)) {
421 RLOGE("SapImpl::resetSimReq: Error getting encoded size for RIL_SIM_SAP_RESET_SIM_REQ");
422 sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 1, msg);
423 return Void();
424 }
425
426 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
427 if (buffer == NULL) {
428 RLOGE("SapImpl::resetSimReq: Error allocating memory for buffer");
429 sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 1, msg);
430 return Void();
431 }
432
433 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
434
435 RLOGD("SapImpl::resetSimReq calling pb_encode");
436 if (!pb_encode(&stream, RIL_SIM_SAP_RESET_SIM_REQ_fields, &req)) {
437 RLOGE("SapImpl::resetSimReq: Error encoding RIL_SIM_SAP_RESET_SIM_REQ");
438 sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 2, buffer, msg);
439 return Void();
440 }
441 /***** Encode RIL_SIM_SAP_RESET_SIM_REQ done *****/
442
443 /* encoded req is payload */
444 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
445}
446
447Return<void> SapImpl::transferCardReaderStatusReq(int32_t token) {
448 RLOGD("SapImpl::transferCardReaderStatusReq");
449 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token);
450 if (msg == NULL) {
451 RLOGE("SapImpl::transferCardReaderStatusReq: Error allocating memory for msg");
452 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 0);
453 return Void();
454 }
455
456 /***** Encode RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ *****/
457 RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ req;
458 memset(&req, 0, sizeof(RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ));
459
460 size_t encodedSize = 0;
461 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ_fields,
462 &req)) {
463 RLOGE("SapImpl::transferCardReaderStatusReq: Error getting encoded size for "
464 "RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ");
465 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 1, msg);
466 return Void();
467 }
468
469 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
470 if (buffer == NULL) {
471 RLOGE("SapImpl::transferCardReaderStatusReq: Error allocating memory for buffer");
472 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 1, msg);
473 return Void();
474 }
475
476 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
477
478 RLOGD("SapImpl::transferCardReaderStatusReq calling pb_encode");
479 if (!pb_encode(&stream, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ_fields, &req)) {
480 RLOGE("SapImpl::transferCardReaderStatusReq: Error encoding "
481 "RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ");
482 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 2, buffer, msg);
483 return Void();
484 }
485 /***** Encode RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ done *****/
486
487 /* encoded req is payload */
488 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
489}
490
491Return<void> SapImpl::setTransferProtocolReq(int32_t token, SapTransferProtocol transferProtocol) {
492 RLOGD("SapImpl::setTransferProtocolReq");
493 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token);
494 if (msg == NULL) {
495 RLOGE("SapImpl::setTransferProtocolReq: Error allocating memory for msg");
496 sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 0);
497 return Void();
498 }
499
500 /***** Encode RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ *****/
501 RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ req;
502 memset(&req, 0, sizeof(RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ));
503 req.protocol = (RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_Protocol)transferProtocol;
504
505 size_t encodedSize = 0;
506 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_fields, &req)) {
507 RLOGE("SapImpl::setTransferProtocolReq: Error getting encoded size for "
508 "RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ");
509 sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 1, msg);
510 return Void();
511 }
512
513 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
514 if (buffer == NULL) {
515 RLOGE("SapImpl::setTransferProtocolReq: Error allocating memory for buffer");
516 sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 1, msg);
517 return Void();
518 }
519
520 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
521
522 RLOGD("SapImpl::setTransferProtocolReq calling pb_encode");
523 if (!pb_encode(&stream, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_fields, &req)) {
524 RLOGE("SapImpl::setTransferProtocolReq: Error encoding "
525 "RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ");
526 sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 2, buffer, msg);
527 return Void();
528 }
529 /***** Encode RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ done *****/
530
531 /* encoded req is payload */
532 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
533}
534
535void *sapDecodeMessage(MsgId msgId, MsgType msgType, uint8_t *payloadPtr, size_t payloadLen) {
536 void *responsePtr = NULL;
Martin Bouchet0d4bbaf2017-09-23 04:54:37 -0300537 pb_istream_t stream;
538
539 /* Create the stream */
540 stream = pb_istream_from_buffer((uint8_t *)payloadPtr, payloadLen);
541
542 /* Decode based on the message id */
543 switch (msgId)
544 {
545 case MsgId_RIL_SIM_SAP_CONNECT:
546 responsePtr = malloc(sizeof(RIL_SIM_SAP_CONNECT_RSP));
547 if (responsePtr) {
548 if (!pb_decode(&stream, RIL_SIM_SAP_CONNECT_RSP_fields, responsePtr)) {
549 RLOGE("Error decoding RIL_SIM_SAP_CONNECT_RSP");
550 return NULL;
551 }
552 }
553 break;
554
555 case MsgId_RIL_SIM_SAP_DISCONNECT:
556 if (msgType == MsgType_RESPONSE) {
557 responsePtr = malloc(sizeof(RIL_SIM_SAP_DISCONNECT_RSP));
558 if (responsePtr) {
559 if (!pb_decode(&stream, RIL_SIM_SAP_DISCONNECT_RSP_fields, responsePtr)) {
560 RLOGE("Error decoding RIL_SIM_SAP_DISCONNECT_RSP");
561 return NULL;
562 }
563 }
564 } else {
565 responsePtr = malloc(sizeof(RIL_SIM_SAP_DISCONNECT_IND));
566 if (responsePtr) {
567 if (!pb_decode(&stream, RIL_SIM_SAP_DISCONNECT_IND_fields, responsePtr)) {
568 RLOGE("Error decoding RIL_SIM_SAP_DISCONNECT_IND");
569 return NULL;
570 }
571 }
572 }
573 break;
574
575 case MsgId_RIL_SIM_SAP_APDU:
576 responsePtr = malloc(sizeof(RIL_SIM_SAP_APDU_RSP));
577 if (responsePtr) {
578 if (!pb_decode(&stream, RIL_SIM_SAP_APDU_RSP_fields, responsePtr)) {
579 RLOGE("Error decoding RIL_SIM_SAP_APDU_RSP");
580 return NULL;
581 }
582 }
583 break;
584
585 case MsgId_RIL_SIM_SAP_TRANSFER_ATR:
586 responsePtr = malloc(sizeof(RIL_SIM_SAP_TRANSFER_ATR_RSP));
587 if (responsePtr) {
588 if (!pb_decode(&stream, RIL_SIM_SAP_TRANSFER_ATR_RSP_fields, responsePtr)) {
589 RLOGE("Error decoding RIL_SIM_SAP_TRANSFER_ATR_RSP");
590 return NULL;
591 }
592 }
593 break;
594
595 case MsgId_RIL_SIM_SAP_POWER:
596 responsePtr = malloc(sizeof(RIL_SIM_SAP_POWER_RSP));
597 if (responsePtr) {
598 if (!pb_decode(&stream, RIL_SIM_SAP_POWER_RSP_fields, responsePtr)) {
599 RLOGE("Error decoding RIL_SIM_SAP_POWER_RSP");
600 return NULL;
601 }
602 }
603 break;
604
605 case MsgId_RIL_SIM_SAP_RESET_SIM:
606 responsePtr = malloc(sizeof(RIL_SIM_SAP_RESET_SIM_RSP));
607 if (responsePtr) {
608 if (!pb_decode(&stream, RIL_SIM_SAP_RESET_SIM_RSP_fields, responsePtr)) {
609 RLOGE("Error decoding RIL_SIM_SAP_RESET_SIM_RSP");
610 return NULL;
611 }
612 }
613 break;
614
615 case MsgId_RIL_SIM_SAP_STATUS:
616 responsePtr = malloc(sizeof(RIL_SIM_SAP_STATUS_IND));
617 if (responsePtr) {
618 if (!pb_decode(&stream, RIL_SIM_SAP_STATUS_IND_fields, responsePtr)) {
619 RLOGE("Error decoding RIL_SIM_SAP_STATUS_IND");
620 return NULL;
621 }
622 }
623 break;
624
625 case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS:
626 responsePtr = malloc(sizeof(RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP));
627 if (responsePtr) {
628 if (!pb_decode(&stream, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_fields,
629 responsePtr)) {
630 RLOGE("Error decoding RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP");
631 return NULL;
632 }
633 }
634 break;
635
636 case MsgId_RIL_SIM_SAP_ERROR_RESP:
637 responsePtr = malloc(sizeof(RIL_SIM_SAP_ERROR_RSP));
638 if (responsePtr) {
639 if (!pb_decode(&stream, RIL_SIM_SAP_ERROR_RSP_fields, responsePtr)) {
640 RLOGE("Error decoding RIL_SIM_SAP_ERROR_RSP");
641 return NULL;
642 }
643 }
644 break;
645
646 case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL:
647 responsePtr = malloc(sizeof(RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP));
648 if (responsePtr) {
649 if (!pb_decode(&stream, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP_fields,
650 responsePtr)) {
651 RLOGE("Error decoding RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP");
652 return NULL;
653 }
654 }
655 break;
656
657 default:
658 break;
659 }
660 return responsePtr;
661} /* sapDecodeMessage */
662
663sp<SapImpl> getSapImpl(RilSapSocket *sapSocket) {
664 switch (sapSocket->getSocketId()) {
665 case RIL_SOCKET_1:
666 RLOGD("getSapImpl: returning sapService[0]");
667 return sapService[0];
668 #if (SIM_COUNT >= 2)
669 case RIL_SOCKET_2:
670 return sapService[1];
671 #if (SIM_COUNT >= 3)
672 case RIL_SOCKET_3:
673 return sapService[2];
674 #if (SIM_COUNT >= 4)
675 case RIL_SOCKET_4:
676 return sapService[3];
677 #endif
678 #endif
679 #endif
680 default:
681 return NULL;
682 }
683}
684
685SapResultCode convertApduResponseProtoToHal(RIL_SIM_SAP_APDU_RSP_Response responseProto) {
686 switch(responseProto) {
687 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SUCCESS:
688 return SapResultCode::SUCCESS;
689 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_GENERIC_FAILURE:
690 return SapResultCode::GENERIC_FAILURE;
691 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_NOT_READY:
692 return SapResultCode::CARD_NOT_ACCESSSIBLE;
693 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
694 return SapResultCode::CARD_ALREADY_POWERED_OFF;
695 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_ABSENT:
696 return SapResultCode::CARD_REMOVED;
697 default:
698 return SapResultCode::GENERIC_FAILURE;
699 }
700}
701
702SapResultCode convertTransferAtrResponseProtoToHal(
703 RIL_SIM_SAP_TRANSFER_ATR_RSP_Response responseProto) {
704 switch(responseProto) {
705 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SUCCESS:
706 return SapResultCode::SUCCESS;
707 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_GENERIC_FAILURE:
708 return SapResultCode::GENERIC_FAILURE;
709 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
710 return SapResultCode::CARD_ALREADY_POWERED_OFF;
711 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_ABSENT:
712 return SapResultCode::CARD_REMOVED;
713 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_DATA_NOT_AVAILABLE:
714 return SapResultCode::DATA_NOT_AVAILABLE;
715 default:
716 return SapResultCode::GENERIC_FAILURE;
717 }
718}
719
720SapResultCode convertPowerResponseProtoToHal(RIL_SIM_SAP_POWER_RSP_Response responseProto) {
721 switch(responseProto) {
722 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SUCCESS:
723 return SapResultCode::SUCCESS;
724 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_GENERIC_FAILURE:
725 return SapResultCode::GENERIC_FAILURE;
726 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ABSENT:
727 return SapResultCode::CARD_REMOVED;
728 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
729 return SapResultCode::CARD_ALREADY_POWERED_OFF;
730 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ALREADY_POWERED_ON:
731 return SapResultCode::CARD_ALREADY_POWERED_ON;
732 default:
733 return SapResultCode::GENERIC_FAILURE;
734 }
735}
736
737SapResultCode convertResetSimResponseProtoToHal(RIL_SIM_SAP_RESET_SIM_RSP_Response responseProto) {
738 switch(responseProto) {
739 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SUCCESS:
740 return SapResultCode::SUCCESS;
741 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_GENERIC_FAILURE:
742 return SapResultCode::GENERIC_FAILURE;
743 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_ABSENT:
744 return SapResultCode::CARD_REMOVED;
745 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_NOT_READY:
746 return SapResultCode::CARD_NOT_ACCESSSIBLE;
747 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
748 return SapResultCode::CARD_ALREADY_POWERED_OFF;
749 }
750 return SapResultCode::GENERIC_FAILURE;
751}
752
753SapResultCode convertTransferCardReaderStatusResponseProtoToHal(
754 RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response responseProto) {
755 switch(responseProto) {
756 case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_SUCCESS:
757 return SapResultCode::SUCCESS;
758 case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_GENERIC_FAILURE:
759 return SapResultCode::GENERIC_FAILURE;
760 case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_SIM_DATA_NOT_AVAILABLE:
761 return SapResultCode::DATA_NOT_AVAILABLE;
762 }
763 return SapResultCode::GENERIC_FAILURE;
764}
765
766void processResponse(MsgHeader *rsp, RilSapSocket *sapSocket, MsgType msgType) {
767 MsgId msgId = rsp->id;
768 uint8_t *data = rsp->payload->bytes;
769 size_t dataLen = rsp->payload->size;
770
771 void *messagePtr = sapDecodeMessage(msgId, msgType, data, dataLen);
772
773 sp<SapImpl> sapImpl = getSapImpl(sapSocket);
774 if (sapImpl->sapCallback == NULL) {
775 RLOGE("processResponse: sapCallback == NULL; msgId = %d; msgType = %d",
776 msgId, msgType);
777 return;
778 }
779
780 RLOGD("processResponse: sapCallback != NULL; msgId = %d; msgType = %d",
781 msgId, msgType);
782
783 Return<void> retStatus;
784 switch (msgId) {
785 case MsgId_RIL_SIM_SAP_CONNECT: {
786 RIL_SIM_SAP_CONNECT_RSP *connectRsp = (RIL_SIM_SAP_CONNECT_RSP *)messagePtr;
787 RLOGD("processResponse: calling sapCallback->connectResponse %d %d %d",
788 rsp->token,
789 connectRsp->response,
790 connectRsp->max_message_size);
791 retStatus = sapImpl->sapCallback->connectResponse(rsp->token,
792 (SapConnectRsp)connectRsp->response,
793 connectRsp->max_message_size);
794 break;
795 }
796
797 case MsgId_RIL_SIM_SAP_DISCONNECT:
798 if (msgType == MsgType_RESPONSE) {
799 RLOGD("processResponse: calling sapCallback->disconnectResponse %d", rsp->token);
800 retStatus = sapImpl->sapCallback->disconnectResponse(rsp->token);
801 } else {
802 RIL_SIM_SAP_DISCONNECT_IND *disconnectInd =
803 (RIL_SIM_SAP_DISCONNECT_IND *)messagePtr;
804 RLOGD("processResponse: calling sapCallback->disconnectIndication %d %d",
805 rsp->token, disconnectInd->disconnectType);
806 retStatus = sapImpl->sapCallback->disconnectIndication(rsp->token,
807 (SapDisconnectType)disconnectInd->disconnectType);
808 }
809 break;
810
811 case MsgId_RIL_SIM_SAP_APDU: {
812 RIL_SIM_SAP_APDU_RSP *apduRsp = (RIL_SIM_SAP_APDU_RSP *)messagePtr;
813 SapResultCode apduResponse = convertApduResponseProtoToHal(apduRsp->response);
814 RLOGD("processResponse: calling sapCallback->apduResponse %d %d",
815 rsp->token, apduResponse);
816 hidl_vec<uint8_t> apduRspVec;
817 if (apduRsp->apduResponse != NULL && apduRsp->apduResponse->size > 0) {
818 apduRspVec.setToExternal(apduRsp->apduResponse->bytes, apduRsp->apduResponse->size);
819 }
820 retStatus = sapImpl->sapCallback->apduResponse(rsp->token, apduResponse, apduRspVec);
821 break;
822 }
823
824 case MsgId_RIL_SIM_SAP_TRANSFER_ATR: {
825 RIL_SIM_SAP_TRANSFER_ATR_RSP *transferAtrRsp =
826 (RIL_SIM_SAP_TRANSFER_ATR_RSP *)messagePtr;
827 SapResultCode transferAtrResponse =
828 convertTransferAtrResponseProtoToHal(transferAtrRsp->response);
829 RLOGD("processResponse: calling sapCallback->transferAtrResponse %d %d",
830 rsp->token, transferAtrResponse);
831 hidl_vec<uint8_t> transferAtrRspVec;
832 if (transferAtrRsp->atr != NULL && transferAtrRsp->atr->size > 0) {
833 transferAtrRspVec.setToExternal(transferAtrRsp->atr->bytes,
834 transferAtrRsp->atr->size);
835 }
836 retStatus = sapImpl->sapCallback->transferAtrResponse(rsp->token, transferAtrResponse,
837 transferAtrRspVec);
838 break;
839 }
840
841 case MsgId_RIL_SIM_SAP_POWER: {
842 SapResultCode powerResponse = convertPowerResponseProtoToHal(
843 ((RIL_SIM_SAP_POWER_RSP *)messagePtr)->response);
844 RLOGD("processResponse: calling sapCallback->powerResponse %d %d",
845 rsp->token, powerResponse);
846 retStatus = sapImpl->sapCallback->powerResponse(rsp->token, powerResponse);
847 break;
848 }
849
850 case MsgId_RIL_SIM_SAP_RESET_SIM: {
851 SapResultCode resetSimResponse = convertResetSimResponseProtoToHal(
852 ((RIL_SIM_SAP_RESET_SIM_RSP *)messagePtr)->response);
853 RLOGD("processResponse: calling sapCallback->resetSimResponse %d %d",
854 rsp->token, resetSimResponse);
855 retStatus = sapImpl->sapCallback->resetSimResponse(rsp->token, resetSimResponse);
856 break;
857 }
858
859 case MsgId_RIL_SIM_SAP_STATUS: {
860 RIL_SIM_SAP_STATUS_IND *statusInd = (RIL_SIM_SAP_STATUS_IND *)messagePtr;
861 RLOGD("processResponse: calling sapCallback->statusIndication %d %d",
862 rsp->token, statusInd->statusChange);
863 retStatus = sapImpl->sapCallback->statusIndication(rsp->token,
864 (SapStatus)statusInd->statusChange);
865 break;
866 }
867
868 case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS: {
869 RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP *transferStatusRsp =
870 (RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP *)messagePtr;
871 SapResultCode transferCardReaderStatusResponse =
872 convertTransferCardReaderStatusResponseProtoToHal(
873 transferStatusRsp->response);
874 RLOGD("processResponse: calling sapCallback->transferCardReaderStatusResponse %d %d %d",
875 rsp->token,
876 transferCardReaderStatusResponse,
877 transferStatusRsp->CardReaderStatus);
878 retStatus = sapImpl->sapCallback->transferCardReaderStatusResponse(rsp->token,
879 transferCardReaderStatusResponse,
880 transferStatusRsp->CardReaderStatus);
881 break;
882 }
883
884 case MsgId_RIL_SIM_SAP_ERROR_RESP: {
885 RLOGD("processResponse: calling sapCallback->errorResponse %d", rsp->token);
886 retStatus = sapImpl->sapCallback->errorResponse(rsp->token);
887 break;
888 }
889
890 case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL: {
891 SapResultCode setTransferProtocolResponse;
892 if (((RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP *)messagePtr)->response ==
893 RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP_Response_RIL_E_SUCCESS) {
894 setTransferProtocolResponse = SapResultCode::SUCCESS;
895 } else {
896 setTransferProtocolResponse = SapResultCode::NOT_SUPPORTED;
897 }
898 RLOGD("processResponse: calling sapCallback->transferProtocolResponse %d %d",
899 rsp->token, setTransferProtocolResponse);
900 retStatus = sapImpl->sapCallback->transferProtocolResponse(rsp->token,
901 setTransferProtocolResponse);
902 break;
903 }
904
905 default:
906 return;
907 }
908 sapImpl->checkReturnStatus(retStatus);
909}
910
911void sap::processResponse(MsgHeader *rsp, RilSapSocket *sapSocket) {
912 processResponse(rsp, sapSocket, MsgType_RESPONSE);
913}
914
915void sap::processUnsolResponse(MsgHeader *rsp, RilSapSocket *sapSocket) {
916 processResponse(rsp, sapSocket, MsgType_UNSOL_RESPONSE);
917}
918
Nathan Haroldea607872017-07-31 19:44:46 -0700919void sap::registerService(const RIL_RadioFunctions *callbacks) {
Martin Bouchet0d4bbaf2017-09-23 04:54:37 -0300920 using namespace android::hardware;
921 int simCount = 1;
922 const char *serviceNames[] = {
923 android::RIL_getServiceName()
924 #if (SIM_COUNT >= 2)
925 , RIL2_SERVICE_NAME
926 #if (SIM_COUNT >= 3)
927 , RIL3_SERVICE_NAME
928 #if (SIM_COUNT >= 4)
929 , RIL4_SERVICE_NAME
930 #endif
931 #endif
932 #endif
933 };
934
935 RIL_SOCKET_ID socketIds[] = {
936 RIL_SOCKET_1
937 #if (SIM_COUNT >= 2)
938 , RIL_SOCKET_2
939 #if (SIM_COUNT >= 3)
940 , RIL_SOCKET_3
941 #if (SIM_COUNT >= 4)
942 , RIL_SOCKET_4
943 #endif
944 #endif
945 #endif
946 };
947 #if (SIM_COUNT >= 2)
948 simCount = SIM_COUNT;
949 #endif
950
951 for (int i = 0; i < simCount; i++) {
952 sapService[i] = new SapImpl;
953 sapService[i]->slotId = i;
954 sapService[i]->rilSocketId = socketIds[i];
955 RLOGD("registerService: starting ISap %s for slotId %d", serviceNames[i], i);
956 android::status_t status = sapService[i]->registerAsService(serviceNames[i]);
957 RLOGD("registerService: started ISap %s status %d", serviceNames[i], status);
958 }
959}