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