ril_service.cpp revision e1bdb3ff16b9dce7045ce8e7566b2a975a094286
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 "RILC"
18
19#include <android/hardware/radio/1.1/IRadio.h>
20#include <android/hardware/radio/1.1/IRadioResponse.h>
21#include <android/hardware/radio/1.1/IRadioIndication.h>
22
23#include <android/hardware/radio/deprecated/1.0/IOemHook.h>
24
25#include <hwbinder/IPCThreadState.h>
26#include <hwbinder/ProcessState.h>
27#include <ril_service.h>
28#include <hidl/HidlTransportSupport.h>
29#include <utils/SystemClock.h>
30#include <inttypes.h>
31
32#define INVALID_HEX_CHAR 16
33
34// Enable verbose logging
35#define VDBG 0
36
37using namespace android::hardware::radio::V1_0;
38using namespace android::hardware::radio::deprecated::V1_0;
39using ::android::hardware::configureRpcThreadpool;
40using ::android::hardware::joinRpcThreadpool;
41using ::android::hardware::Return;
42using ::android::hardware::hidl_string;
43using ::android::hardware::hidl_vec;
44using ::android::hardware::hidl_array;
45using ::android::hardware::Void;
46using android::CommandInfo;
47using android::RequestInfo;
48using android::requestToString;
49using android::sp;
50
51#define BOOL_TO_INT(x) (x ? 1 : 0)
52#define ATOI_NULL_HANDLED(x) (x ? atoi(x) : -1)
53#define ATOI_NULL_HANDLED_DEF(x, defaultVal) (x ? atoi(x) : defaultVal)
54
55RIL_RadioFunctions *s_vendorFunctions = NULL;
56static CommandInfo *s_commands;
57
58struct RadioImpl;
59struct OemHookImpl;
60
61#if (SIM_COUNT >= 2)
62sp<RadioImpl> radioService[SIM_COUNT];
63sp<OemHookImpl> oemHookService[SIM_COUNT];
64// counter used for synchronization. It is incremented every time response callbacks are updated.
65volatile int32_t mCounterRadio[SIM_COUNT];
66volatile int32_t mCounterOemHook[SIM_COUNT];
67#else
68sp<RadioImpl> radioService[1];
69sp<OemHookImpl> oemHookService[1];
70// counter used for synchronization. It is incremented every time response callbacks are updated.
71volatile int32_t mCounterRadio[1];
72volatile int32_t mCounterOemHook[1];
73#endif
74
75static pthread_rwlock_t radioServiceRwlock = PTHREAD_RWLOCK_INITIALIZER;
76
77#if (SIM_COUNT >= 2)
78static pthread_rwlock_t radioServiceRwlock2 = PTHREAD_RWLOCK_INITIALIZER;
79#if (SIM_COUNT >= 3)
80static pthread_rwlock_t radioServiceRwlock3 = PTHREAD_RWLOCK_INITIALIZER;
81#if (SIM_COUNT >= 4)
82static pthread_rwlock_t radioServiceRwlock4 = PTHREAD_RWLOCK_INITIALIZER;
83#endif
84#endif
85#endif
86
87void convertRilHardwareConfigListToHal(void *response, size_t responseLen,
88        hidl_vec<HardwareConfig>& records);
89
90void convertRilRadioCapabilityToHal(void *response, size_t responseLen, RadioCapability& rc);
91
92void convertRilLceDataInfoToHal(void *response, size_t responseLen, LceDataInfo& lce);
93
94void convertRilSignalStrengthToHal(void *response, size_t responseLen,
95        SignalStrength& signalStrength);
96
97void convertRilDataCallToHal(RIL_Data_Call_Response_v11 *dcResponse,
98        SetupDataCallResult& dcResult);
99
100void convertRilDataCallListToHal(void *response, size_t responseLen,
101        hidl_vec<SetupDataCallResult>& dcResultList);
102
103void convertRilCellInfoListToHal(void *response, size_t responseLen, hidl_vec<CellInfo>& records);
104
105struct RadioImpl : public ::android::hardware::radio::V1_1::IRadio {
106    int32_t mSlotId;
107    sp<IRadioResponse> mRadioResponse;
108    sp<IRadioIndication> mRadioIndication;
109
110    Return<void> setResponseFunctions(
111            const ::android::sp<IRadioResponse>& radioResponse,
112            const ::android::sp<IRadioIndication>& radioIndication);
113
114    Return<void> getIccCardStatus(int32_t serial);
115
116    Return<void> supplyIccPinForApp(int32_t serial, const hidl_string& pin,
117            const hidl_string& aid);
118
119    Return<void> supplyIccPukForApp(int32_t serial, const hidl_string& puk,
120            const hidl_string& pin, const hidl_string& aid);
121
122    Return<void> supplyIccPin2ForApp(int32_t serial,
123            const hidl_string& pin2,
124            const hidl_string& aid);
125
126    Return<void> supplyIccPuk2ForApp(int32_t serial, const hidl_string& puk2,
127            const hidl_string& pin2, const hidl_string& aid);
128
129    Return<void> changeIccPinForApp(int32_t serial, const hidl_string& oldPin,
130            const hidl_string& newPin, const hidl_string& aid);
131
132    Return<void> changeIccPin2ForApp(int32_t serial, const hidl_string& oldPin2,
133            const hidl_string& newPin2, const hidl_string& aid);
134
135    Return<void> supplyNetworkDepersonalization(int32_t serial, const hidl_string& netPin);
136
137    Return<void> getCurrentCalls(int32_t serial);
138
139    Return<void> dial(int32_t serial, const Dial& dialInfo);
140
141    Return<void> getImsiForApp(int32_t serial,
142            const ::android::hardware::hidl_string& aid);
143
144    Return<void> hangup(int32_t serial, int32_t gsmIndex);
145
146    Return<void> hangupWaitingOrBackground(int32_t serial);
147
148    Return<void> hangupForegroundResumeBackground(int32_t serial);
149
150    Return<void> switchWaitingOrHoldingAndActive(int32_t serial);
151
152    Return<void> conference(int32_t serial);
153
154    Return<void> rejectCall(int32_t serial);
155
156    Return<void> getLastCallFailCause(int32_t serial);
157
158    Return<void> getSignalStrength(int32_t serial);
159
160    Return<void> getVoiceRegistrationState(int32_t serial);
161
162    Return<void> getDataRegistrationState(int32_t serial);
163
164    Return<void> getOperator(int32_t serial);
165
166    Return<void> setRadioPower(int32_t serial, bool on);
167
168    Return<void> sendDtmf(int32_t serial,
169            const ::android::hardware::hidl_string& s);
170
171    Return<void> sendSms(int32_t serial, const GsmSmsMessage& message);
172
173    Return<void> sendSMSExpectMore(int32_t serial, const GsmSmsMessage& message);
174
175    Return<void> setupDataCall(int32_t serial,
176            RadioTechnology radioTechnology,
177            const DataProfileInfo& profileInfo,
178            bool modemCognitive,
179            bool roamingAllowed,
180            bool isRoaming);
181
182    Return<void> iccIOForApp(int32_t serial,
183            const IccIo& iccIo);
184
185    Return<void> sendUssd(int32_t serial,
186            const ::android::hardware::hidl_string& ussd);
187
188    Return<void> cancelPendingUssd(int32_t serial);
189
190    Return<void> getClir(int32_t serial);
191
192    Return<void> setClir(int32_t serial, int32_t status);
193
194    Return<void> getCallForwardStatus(int32_t serial,
195            const CallForwardInfo& callInfo);
196
197    Return<void> setCallForward(int32_t serial,
198            const CallForwardInfo& callInfo);
199
200    Return<void> getCallWaiting(int32_t serial, int32_t serviceClass);
201
202    Return<void> setCallWaiting(int32_t serial, bool enable, int32_t serviceClass);
203
204    Return<void> acknowledgeLastIncomingGsmSms(int32_t serial,
205            bool success, SmsAcknowledgeFailCause cause);
206
207    Return<void> acceptCall(int32_t serial);
208
209    Return<void> deactivateDataCall(int32_t serial,
210            int32_t cid, bool reasonRadioShutDown);
211
212    Return<void> getFacilityLockForApp(int32_t serial,
213            const ::android::hardware::hidl_string& facility,
214            const ::android::hardware::hidl_string& password,
215            int32_t serviceClass,
216            const ::android::hardware::hidl_string& appId);
217
218    Return<void> setFacilityLockForApp(int32_t serial,
219            const ::android::hardware::hidl_string& facility,
220            bool lockState,
221            const ::android::hardware::hidl_string& password,
222            int32_t serviceClass,
223            const ::android::hardware::hidl_string& appId);
224
225    Return<void> setBarringPassword(int32_t serial,
226            const ::android::hardware::hidl_string& facility,
227            const ::android::hardware::hidl_string& oldPassword,
228            const ::android::hardware::hidl_string& newPassword);
229
230    Return<void> getNetworkSelectionMode(int32_t serial);
231
232    Return<void> setNetworkSelectionModeAutomatic(int32_t serial);
233
234    Return<void> setNetworkSelectionModeManual(int32_t serial,
235            const ::android::hardware::hidl_string& operatorNumeric);
236
237    Return<void> getAvailableNetworks(int32_t serial);
238
239    Return<void> startDtmf(int32_t serial,
240            const ::android::hardware::hidl_string& s);
241
242    Return<void> stopDtmf(int32_t serial);
243
244    Return<void> getBasebandVersion(int32_t serial);
245
246    Return<void> separateConnection(int32_t serial, int32_t gsmIndex);
247
248    Return<void> setMute(int32_t serial, bool enable);
249
250    Return<void> getMute(int32_t serial);
251
252    Return<void> getClip(int32_t serial);
253
254    Return<void> getDataCallList(int32_t serial);
255
256    Return<void> setSuppServiceNotifications(int32_t serial, bool enable);
257
258    Return<void> writeSmsToSim(int32_t serial,
259            const SmsWriteArgs& smsWriteArgs);
260
261    Return<void> deleteSmsOnSim(int32_t serial, int32_t index);
262
263    Return<void> setBandMode(int32_t serial, RadioBandMode mode);
264
265    Return<void> getAvailableBandModes(int32_t serial);
266
267    Return<void> sendEnvelope(int32_t serial,
268            const ::android::hardware::hidl_string& command);
269
270    Return<void> sendTerminalResponseToSim(int32_t serial,
271            const ::android::hardware::hidl_string& commandResponse);
272
273    Return<void> handleStkCallSetupRequestFromSim(int32_t serial, bool accept);
274
275    Return<void> explicitCallTransfer(int32_t serial);
276
277    Return<void> setPreferredNetworkType(int32_t serial, PreferredNetworkType nwType);
278
279    Return<void> getPreferredNetworkType(int32_t serial);
280
281    Return<void> getNeighboringCids(int32_t serial);
282
283    Return<void> setLocationUpdates(int32_t serial, bool enable);
284
285    Return<void> setCdmaSubscriptionSource(int32_t serial,
286            CdmaSubscriptionSource cdmaSub);
287
288    Return<void> setCdmaRoamingPreference(int32_t serial, CdmaRoamingType type);
289
290    Return<void> getCdmaRoamingPreference(int32_t serial);
291
292    Return<void> setTTYMode(int32_t serial, TtyMode mode);
293
294    Return<void> getTTYMode(int32_t serial);
295
296    Return<void> setPreferredVoicePrivacy(int32_t serial, bool enable);
297
298    Return<void> getPreferredVoicePrivacy(int32_t serial);
299
300    Return<void> sendCDMAFeatureCode(int32_t serial,
301            const ::android::hardware::hidl_string& featureCode);
302
303    Return<void> sendBurstDtmf(int32_t serial,
304            const ::android::hardware::hidl_string& dtmf,
305            int32_t on,
306            int32_t off);
307
308    Return<void> sendCdmaSms(int32_t serial, const CdmaSmsMessage& sms);
309
310    Return<void> acknowledgeLastIncomingCdmaSms(int32_t serial,
311            const CdmaSmsAck& smsAck);
312
313    Return<void> getGsmBroadcastConfig(int32_t serial);
314
315    Return<void> setGsmBroadcastConfig(int32_t serial,
316            const hidl_vec<GsmBroadcastSmsConfigInfo>& configInfo);
317
318    Return<void> setGsmBroadcastActivation(int32_t serial, bool activate);
319
320    Return<void> getCdmaBroadcastConfig(int32_t serial);
321
322    Return<void> setCdmaBroadcastConfig(int32_t serial,
323            const hidl_vec<CdmaBroadcastSmsConfigInfo>& configInfo);
324
325    Return<void> setCdmaBroadcastActivation(int32_t serial, bool activate);
326
327    Return<void> getCDMASubscription(int32_t serial);
328
329    Return<void> writeSmsToRuim(int32_t serial, const CdmaSmsWriteArgs& cdmaSms);
330
331    Return<void> deleteSmsOnRuim(int32_t serial, int32_t index);
332
333    Return<void> getDeviceIdentity(int32_t serial);
334
335    Return<void> exitEmergencyCallbackMode(int32_t serial);
336
337    Return<void> getSmscAddress(int32_t serial);
338
339    Return<void> setSmscAddress(int32_t serial,
340            const ::android::hardware::hidl_string& smsc);
341
342    Return<void> reportSmsMemoryStatus(int32_t serial, bool available);
343
344    Return<void> reportStkServiceIsRunning(int32_t serial);
345
346    Return<void> getCdmaSubscriptionSource(int32_t serial);
347
348    Return<void> requestIsimAuthentication(int32_t serial,
349            const ::android::hardware::hidl_string& challenge);
350
351    Return<void> acknowledgeIncomingGsmSmsWithPdu(int32_t serial,
352            bool success,
353            const ::android::hardware::hidl_string& ackPdu);
354
355    Return<void> sendEnvelopeWithStatus(int32_t serial,
356            const ::android::hardware::hidl_string& contents);
357
358    Return<void> getVoiceRadioTechnology(int32_t serial);
359
360    Return<void> getCellInfoList(int32_t serial);
361
362    Return<void> setCellInfoListRate(int32_t serial, int32_t rate);
363
364    Return<void> setInitialAttachApn(int32_t serial, const DataProfileInfo& dataProfileInfo,
365            bool modemCognitive, bool isRoaming);
366
367    Return<void> getImsRegistrationState(int32_t serial);
368
369    Return<void> sendImsSms(int32_t serial, const ImsSmsMessage& message);
370
371    Return<void> iccTransmitApduBasicChannel(int32_t serial, const SimApdu& message);
372
373    Return<void> iccOpenLogicalChannel(int32_t serial,
374            const ::android::hardware::hidl_string& aid, int32_t p2);
375
376    Return<void> iccCloseLogicalChannel(int32_t serial, int32_t channelId);
377
378    Return<void> iccTransmitApduLogicalChannel(int32_t serial, const SimApdu& message);
379
380    Return<void> nvReadItem(int32_t serial, NvItem itemId);
381
382    Return<void> nvWriteItem(int32_t serial, const NvWriteItem& item);
383
384    Return<void> nvWriteCdmaPrl(int32_t serial,
385            const ::android::hardware::hidl_vec<uint8_t>& prl);
386
387    Return<void> nvResetConfig(int32_t serial, ResetNvType resetType);
388
389    Return<void> setUiccSubscription(int32_t serial, const SelectUiccSub& uiccSub);
390
391    Return<void> setDataAllowed(int32_t serial, bool allow);
392
393    Return<void> getHardwareConfig(int32_t serial);
394
395    Return<void> requestIccSimAuthentication(int32_t serial,
396            int32_t authContext,
397            const ::android::hardware::hidl_string& authData,
398            const ::android::hardware::hidl_string& aid);
399
400    Return<void> setDataProfile(int32_t serial,
401            const ::android::hardware::hidl_vec<DataProfileInfo>& profiles, bool isRoaming);
402
403    Return<void> requestShutdown(int32_t serial);
404
405    Return<void> getRadioCapability(int32_t serial);
406
407    Return<void> setRadioCapability(int32_t serial, const RadioCapability& rc);
408
409    Return<void> startLceService(int32_t serial, int32_t reportInterval, bool pullMode);
410
411    Return<void> stopLceService(int32_t serial);
412
413    Return<void> pullLceData(int32_t serial);
414
415    Return<void> getModemActivityInfo(int32_t serial);
416
417    Return<void> setAllowedCarriers(int32_t serial,
418            bool allAllowed,
419            const CarrierRestrictions& carriers);
420
421    Return<void> getAllowedCarriers(int32_t serial);
422
423    Return<void> sendDeviceState(int32_t serial, DeviceStateType deviceStateType, bool state);
424
425    Return<void> setIndicationFilter(int32_t serial, int32_t indicationFilter);
426
427    Return<void> setSimCardPower(int32_t serial, bool powerUp);
428
429    Return<void> responseAcknowledgement();
430
431    Return<void> setCarrierInfoForImsiEncryption(int32_t serial,
432            const ::android::hardware::hidl_vec<uint8_t>& carrierKey,
433            const hidl_string& keyIdentifier);
434
435    void checkReturnStatus(Return<void>& ret);
436};
437
438struct OemHookImpl : public IOemHook {
439    int32_t mSlotId;
440    sp<IOemHookResponse> mOemHookResponse;
441    sp<IOemHookIndication> mOemHookIndication;
442
443    Return<void> setResponseFunctions(
444            const ::android::sp<IOemHookResponse>& oemHookResponse,
445            const ::android::sp<IOemHookIndication>& oemHookIndication);
446
447    Return<void> sendRequestRaw(int32_t serial,
448            const ::android::hardware::hidl_vec<uint8_t>& data);
449
450    Return<void> sendRequestStrings(int32_t serial,
451            const ::android::hardware::hidl_vec<::android::hardware::hidl_string>& data);
452};
453
454void memsetAndFreeStrings(int numPointers, ...) {
455    va_list ap;
456    va_start(ap, numPointers);
457    for (int i = 0; i < numPointers; i++) {
458        char *ptr = va_arg(ap, char *);
459        if (ptr) {
460#ifdef MEMSET_FREED
461#define MAX_STRING_LENGTH 4096
462            memset(ptr, 0, strnlen(ptr, MAX_STRING_LENGTH));
463#endif
464            free(ptr);
465        }
466    }
467    va_end(ap);
468}
469
470void sendErrorResponse(RequestInfo *pRI, RIL_Errno err) {
471    pRI->pCI->responseFunction((int) pRI->socket_id,
472            (int) RadioResponseType::SOLICITED, pRI->token, err, NULL, 0);
473}
474
475/**
476 * Copies over src to dest. If memory allocation fails, responseFunction() is called for the
477 * request with error RIL_E_NO_MEMORY.
478 * Returns true on success, and false on failure.
479 */
480bool copyHidlStringToRil(char **dest, const hidl_string &src, RequestInfo *pRI) {
481    size_t len = src.size();
482    if (len == 0) {
483        *dest = NULL;
484        return true;
485    }
486    *dest = (char *) calloc(len + 1, sizeof(char));
487    if (*dest == NULL) {
488        RLOGE("Memory allocation failed for request %s", requestToString(pRI->pCI->requestNumber));
489        sendErrorResponse(pRI, RIL_E_NO_MEMORY);
490        return false;
491    }
492    strncpy(*dest, src.c_str(), len + 1);
493    return true;
494}
495
496hidl_string convertCharPtrToHidlString(const char *ptr) {
497    hidl_string ret;
498    if (ptr != NULL) {
499        // TODO: replace this with strnlen
500        ret.setToExternal(ptr, strlen(ptr));
501    }
502    return ret;
503}
504
505bool dispatchVoid(int serial, int slotId, int request) {
506    RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
507    if (pRI == NULL) {
508        return false;
509    }
510    s_vendorFunctions->onRequest(request, NULL, 0, pRI);
511    return true;
512}
513
514bool dispatchString(int serial, int slotId, int request, const char * str) {
515    RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
516    if (pRI == NULL) {
517        return false;
518    }
519
520    char *pString;
521    if (!copyHidlStringToRil(&pString, str, pRI)) {
522        return false;
523    }
524
525    s_vendorFunctions->onRequest(request, pString, sizeof(char *), pRI);
526
527    memsetAndFreeStrings(1, pString);
528    return true;
529}
530
531bool dispatchStrings(int serial, int slotId, int request, int countStrings, ...) {
532    RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
533    if (pRI == NULL) {
534        return false;
535    }
536
537    char **pStrings;
538    pStrings = (char **)calloc(countStrings, sizeof(char *));
539    if (pStrings == NULL) {
540        RLOGE("Memory allocation failed for request %s", requestToString(request));
541        sendErrorResponse(pRI, RIL_E_NO_MEMORY);
542        return false;
543    }
544    va_list ap;
545    va_start(ap, countStrings);
546    for (int i = 0; i < countStrings; i++) {
547        const char* str = va_arg(ap, const char *);
548        if (!copyHidlStringToRil(&pStrings[i], hidl_string(str), pRI)) {
549            va_end(ap);
550            for (int j = 0; j < i; j++) {
551                memsetAndFreeStrings(1, pStrings[j]);
552            }
553            free(pStrings);
554            return false;
555        }
556    }
557    va_end(ap);
558
559    s_vendorFunctions->onRequest(request, pStrings, countStrings * sizeof(char *), pRI);
560
561    if (pStrings != NULL) {
562        for (int i = 0 ; i < countStrings ; i++) {
563            memsetAndFreeStrings(1, pStrings[i]);
564        }
565
566#ifdef MEMSET_FREED
567        memset(pStrings, 0, countStrings * sizeof(char *));
568#endif
569        free(pStrings);
570    }
571    return true;
572}
573
574bool dispatchStrings(int serial, int slotId, int request, const hidl_vec<hidl_string>& data) {
575    RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
576    if (pRI == NULL) {
577        return false;
578    }
579
580    int countStrings = data.size();
581    char **pStrings;
582    pStrings = (char **)calloc(countStrings, sizeof(char *));
583    if (pStrings == NULL) {
584        RLOGE("Memory allocation failed for request %s", requestToString(request));
585        sendErrorResponse(pRI, RIL_E_NO_MEMORY);
586        return false;
587    }
588
589    for (int i = 0; i < countStrings; i++) {
590        if (!copyHidlStringToRil(&pStrings[i], data[i], pRI)) {
591            for (int j = 0; j < i; j++) {
592                memsetAndFreeStrings(1, pStrings[j]);
593            }
594            free(pStrings);
595            return false;
596        }
597    }
598
599    s_vendorFunctions->onRequest(request, pStrings, countStrings * sizeof(char *), pRI);
600
601    if (pStrings != NULL) {
602        for (int i = 0 ; i < countStrings ; i++) {
603            memsetAndFreeStrings(1, pStrings[i]);
604        }
605
606#ifdef MEMSET_FREED
607        memset(pStrings, 0, countStrings * sizeof(char *));
608#endif
609        free(pStrings);
610    }
611    return true;
612}
613
614bool dispatchInts(int serial, int slotId, int request, int countInts, ...) {
615    RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
616    if (pRI == NULL) {
617        return false;
618    }
619
620    int *pInts = (int *)calloc(countInts, sizeof(int));
621
622    if (pInts == NULL) {
623        RLOGE("Memory allocation failed for request %s", requestToString(request));
624        sendErrorResponse(pRI, RIL_E_NO_MEMORY);
625        return false;
626    }
627    va_list ap;
628    va_start(ap, countInts);
629    for (int i = 0; i < countInts; i++) {
630        pInts[i] = va_arg(ap, int);
631    }
632    va_end(ap);
633
634    s_vendorFunctions->onRequest(request, pInts, countInts * sizeof(int), pRI);
635
636    if (pInts != NULL) {
637#ifdef MEMSET_FREED
638        memset(pInts, 0, countInts * sizeof(int));
639#endif
640        free(pInts);
641    }
642    return true;
643}
644
645bool dispatchCallForwardStatus(int serial, int slotId, int request,
646                              const CallForwardInfo& callInfo) {
647    RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
648    if (pRI == NULL) {
649        return false;
650    }
651
652    RIL_CallForwardInfo cf;
653    cf.status = (int) callInfo.status;
654    cf.reason = callInfo.reason;
655    cf.serviceClass = callInfo.serviceClass;
656    cf.toa = callInfo.toa;
657    cf.timeSeconds = callInfo.timeSeconds;
658
659    if (!copyHidlStringToRil(&cf.number, callInfo.number, pRI)) {
660        return false;
661    }
662
663    s_vendorFunctions->onRequest(request, &cf, sizeof(cf), pRI);
664
665    memsetAndFreeStrings(1, cf.number);
666
667    return true;
668}
669
670bool dispatchRaw(int serial, int slotId, int request, const hidl_vec<uint8_t>& rawBytes) {
671    RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
672    if (pRI == NULL) {
673        return false;
674    }
675
676    const uint8_t *uData = rawBytes.data();
677
678    s_vendorFunctions->onRequest(request, (void *) uData, rawBytes.size(), pRI);
679
680    return true;
681}
682
683bool dispatchIccApdu(int serial, int slotId, int request, const SimApdu& message) {
684    RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
685    if (pRI == NULL) {
686        return false;
687    }
688
689    RIL_SIM_APDU apdu = {};
690
691    apdu.sessionid = message.sessionId;
692    apdu.cla = message.cla;
693    apdu.instruction = message.instruction;
694    apdu.p1 = message.p1;
695    apdu.p2 = message.p2;
696    apdu.p3 = message.p3;
697
698    if (!copyHidlStringToRil(&apdu.data, message.data, pRI)) {
699        return false;
700    }
701
702    s_vendorFunctions->onRequest(request, &apdu, sizeof(apdu), pRI);
703
704    memsetAndFreeStrings(1, apdu.data);
705
706    return true;
707}
708
709void checkReturnStatus(int32_t slotId, Return<void>& ret, bool isRadioService) {
710    if (ret.isOk() == false) {
711        RLOGE("checkReturnStatus: unable to call response/indication callback");
712        // Remote process hosting the callbacks must be dead. Reset the callback objects;
713        // there's no other recovery to be done here. When the client process is back up, it will
714        // call setResponseFunctions()
715
716        // Caller should already hold rdlock, release that first
717        // note the current counter to avoid overwriting updates made by another thread before
718        // write lock is acquired.
719        int counter = isRadioService ? mCounterRadio[slotId] : mCounterOemHook[slotId];
720        pthread_rwlock_t *radioServiceRwlockPtr = radio::getRadioServiceRwlock(slotId);
721        int ret = pthread_rwlock_unlock(radioServiceRwlockPtr);
722        assert(ret == 0);
723
724        // acquire wrlock
725        ret = pthread_rwlock_wrlock(radioServiceRwlockPtr);
726        assert(ret == 0);
727
728        // make sure the counter value has not changed
729        if (counter == (isRadioService ? mCounterRadio[slotId] : mCounterOemHook[slotId])) {
730            if (isRadioService) {
731                radioService[slotId]->mRadioResponse = NULL;
732                radioService[slotId]->mRadioIndication = NULL;
733            } else {
734                oemHookService[slotId]->mOemHookResponse = NULL;
735                oemHookService[slotId]->mOemHookIndication = NULL;
736            }
737            isRadioService ? mCounterRadio[slotId]++ : mCounterOemHook[slotId]++;
738        } else {
739            RLOGE("checkReturnStatus: not resetting responseFunctions as they likely "
740                    "got updated on another thread");
741        }
742
743        // release wrlock
744        ret = pthread_rwlock_unlock(radioServiceRwlockPtr);
745        assert(ret == 0);
746
747        // Reacquire rdlock
748        ret = pthread_rwlock_rdlock(radioServiceRwlockPtr);
749        assert(ret == 0);
750    }
751}
752
753void RadioImpl::checkReturnStatus(Return<void>& ret) {
754    ::checkReturnStatus(mSlotId, ret, true);
755}
756
757Return<void> RadioImpl::setResponseFunctions(
758        const ::android::sp<IRadioResponse>& radioResponseParam,
759        const ::android::sp<IRadioIndication>& radioIndicationParam) {
760    RLOGD("setResponseFunctions");
761
762    pthread_rwlock_t *radioServiceRwlockPtr = radio::getRadioServiceRwlock(mSlotId);
763    int ret = pthread_rwlock_wrlock(radioServiceRwlockPtr);
764    assert(ret == 0);
765
766    mRadioResponse = radioResponseParam;
767    mRadioIndication = radioIndicationParam;
768    mCounterRadio[mSlotId]++;
769
770    ret = pthread_rwlock_unlock(radioServiceRwlockPtr);
771    assert(ret == 0);
772
773    // client is connected. Send initial indications.
774    android::onNewCommandConnect((RIL_SOCKET_ID) mSlotId);
775
776    return Void();
777}
778
779Return<void> RadioImpl::getIccCardStatus(int32_t serial) {
780#if VDBG
781    RLOGD("getIccCardStatus: serial %d", serial);
782#endif
783    dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_SIM_STATUS);
784    return Void();
785}
786
787Return<void> RadioImpl::supplyIccPinForApp(int32_t serial, const hidl_string& pin,
788        const hidl_string& aid) {
789#if VDBG
790    RLOGD("supplyIccPinForApp: serial %d", serial);
791#endif
792    dispatchStrings(serial, mSlotId, RIL_REQUEST_ENTER_SIM_PIN,
793            2, pin.c_str(), aid.c_str());
794    return Void();
795}
796
797Return<void> RadioImpl::supplyIccPukForApp(int32_t serial, const hidl_string& puk,
798                                           const hidl_string& pin, const hidl_string& aid) {
799#if VDBG
800    RLOGD("supplyIccPukForApp: serial %d", serial);
801#endif
802    dispatchStrings(serial, mSlotId, RIL_REQUEST_ENTER_SIM_PUK,
803            3, puk.c_str(), pin.c_str(), aid.c_str());
804    return Void();
805}
806
807Return<void> RadioImpl::supplyIccPin2ForApp(int32_t serial, const hidl_string& pin2,
808                                            const hidl_string& aid) {
809#if VDBG
810    RLOGD("supplyIccPin2ForApp: serial %d", serial);
811#endif
812    dispatchStrings(serial, mSlotId, RIL_REQUEST_ENTER_SIM_PIN2,
813            2, pin2.c_str(), aid.c_str());
814    return Void();
815}
816
817Return<void> RadioImpl::supplyIccPuk2ForApp(int32_t serial, const hidl_string& puk2,
818                                            const hidl_string& pin2, const hidl_string& aid) {
819#if VDBG
820    RLOGD("supplyIccPuk2ForApp: serial %d", serial);
821#endif
822    dispatchStrings(serial, mSlotId, RIL_REQUEST_ENTER_SIM_PUK2,
823            3, puk2.c_str(), pin2.c_str(), aid.c_str());
824    return Void();
825}
826
827Return<void> RadioImpl::changeIccPinForApp(int32_t serial, const hidl_string& oldPin,
828                                           const hidl_string& newPin, const hidl_string& aid) {
829#if VDBG
830    RLOGD("changeIccPinForApp: serial %d", serial);
831#endif
832    dispatchStrings(serial, mSlotId, RIL_REQUEST_CHANGE_SIM_PIN,
833            3, oldPin.c_str(), newPin.c_str(), aid.c_str());
834    return Void();
835}
836
837Return<void> RadioImpl::changeIccPin2ForApp(int32_t serial, const hidl_string& oldPin2,
838                                            const hidl_string& newPin2, const hidl_string& aid) {
839#if VDBG
840    RLOGD("changeIccPin2ForApp: serial %d", serial);
841#endif
842    dispatchStrings(serial, mSlotId, RIL_REQUEST_CHANGE_SIM_PIN2,
843            3, oldPin2.c_str(), newPin2.c_str(), aid.c_str());
844    return Void();
845}
846
847Return<void> RadioImpl::supplyNetworkDepersonalization(int32_t serial,
848                                                       const hidl_string& netPin) {
849#if VDBG
850    RLOGD("supplyNetworkDepersonalization: serial %d", serial);
851#endif
852    dispatchStrings(serial, mSlotId, RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION,
853            1, netPin.c_str());
854    return Void();
855}
856
857Return<void> RadioImpl::getCurrentCalls(int32_t serial) {
858#if VDBG
859    RLOGD("getCurrentCalls: serial %d", serial);
860#endif
861    dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_CURRENT_CALLS);
862    return Void();
863}
864
865Return<void> RadioImpl::dial(int32_t serial, const Dial& dialInfo) {
866#if VDBG
867    RLOGD("dial: serial %d", serial);
868#endif
869    RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_DIAL);
870    if (pRI == NULL) {
871        return Void();
872    }
873    RIL_Dial dial = {};
874    RIL_UUS_Info uusInfo = {};
875    int32_t sizeOfDial = sizeof(dial);
876
877    if (!copyHidlStringToRil(&dial.address, dialInfo.address, pRI)) {
878        return Void();
879    }
880    dial.clir = (int) dialInfo.clir;
881
882    if (dialInfo.uusInfo.size() != 0) {
883        uusInfo.uusType = (RIL_UUS_Type) dialInfo.uusInfo[0].uusType;
884        uusInfo.uusDcs = (RIL_UUS_DCS) dialInfo.uusInfo[0].uusDcs;
885
886        if (dialInfo.uusInfo[0].uusData.size() == 0) {
887            uusInfo.uusData = NULL;
888            uusInfo.uusLength = 0;
889        } else {
890            if (!copyHidlStringToRil(&uusInfo.uusData, dialInfo.uusInfo[0].uusData, pRI)) {
891                memsetAndFreeStrings(1, dial.address);
892                return Void();
893            }
894            uusInfo.uusLength = dialInfo.uusInfo[0].uusData.size();
895        }
896
897        dial.uusInfo = &uusInfo;
898    }
899
900    s_vendorFunctions->onRequest(RIL_REQUEST_DIAL, &dial, sizeOfDial, pRI);
901
902    memsetAndFreeStrings(2, dial.address, uusInfo.uusData);
903
904    return Void();
905}
906
907Return<void> RadioImpl::getImsiForApp(int32_t serial, const hidl_string& aid) {
908#if VDBG
909    RLOGD("getImsiForApp: serial %d", serial);
910#endif
911    dispatchStrings(serial, mSlotId, RIL_REQUEST_GET_IMSI,
912            1, aid.c_str());
913    return Void();
914}
915
916Return<void> RadioImpl::hangup(int32_t serial, int32_t gsmIndex) {
917#if VDBG
918    RLOGD("hangup: serial %d", serial);
919#endif
920    dispatchInts(serial, mSlotId, RIL_REQUEST_HANGUP, 1, gsmIndex);
921    return Void();
922}
923
924Return<void> RadioImpl::hangupWaitingOrBackground(int32_t serial) {
925#if VDBG
926    RLOGD("hangupWaitingOrBackground: serial %d", serial);
927#endif
928    dispatchVoid(serial, mSlotId, RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND);
929    return Void();
930}
931
932Return<void> RadioImpl::hangupForegroundResumeBackground(int32_t serial) {
933#if VDBG
934    RLOGD("hangupForegroundResumeBackground: serial %d", serial);
935#endif
936    dispatchVoid(serial, mSlotId, RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND);
937    return Void();
938}
939
940Return<void> RadioImpl::switchWaitingOrHoldingAndActive(int32_t serial) {
941#if VDBG
942    RLOGD("switchWaitingOrHoldingAndActive: serial %d", serial);
943#endif
944    dispatchVoid(serial, mSlotId, RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE);
945    return Void();
946}
947
948Return<void> RadioImpl::conference(int32_t serial) {
949#if VDBG
950    RLOGD("conference: serial %d", serial);
951#endif
952    dispatchVoid(serial, mSlotId, RIL_REQUEST_CONFERENCE);
953    return Void();
954}
955
956Return<void> RadioImpl::rejectCall(int32_t serial) {
957#if VDBG
958    RLOGD("rejectCall: serial %d", serial);
959#endif
960    dispatchVoid(serial, mSlotId, RIL_REQUEST_UDUB);
961    return Void();
962}
963
964Return<void> RadioImpl::getLastCallFailCause(int32_t serial) {
965#if VDBG
966    RLOGD("getLastCallFailCause: serial %d", serial);
967#endif
968    dispatchVoid(serial, mSlotId, RIL_REQUEST_LAST_CALL_FAIL_CAUSE);
969    return Void();
970}
971
972Return<void> RadioImpl::getSignalStrength(int32_t serial) {
973#if VDBG
974    RLOGD("getSignalStrength: serial %d", serial);
975#endif
976    dispatchVoid(serial, mSlotId, RIL_REQUEST_SIGNAL_STRENGTH);
977    return Void();
978}
979
980Return<void> RadioImpl::getVoiceRegistrationState(int32_t serial) {
981#if VDBG
982    RLOGD("getVoiceRegistrationState: serial %d", serial);
983#endif
984    dispatchVoid(serial, mSlotId, RIL_REQUEST_VOICE_REGISTRATION_STATE);
985    return Void();
986}
987
988Return<void> RadioImpl::getDataRegistrationState(int32_t serial) {
989#if VDBG
990    RLOGD("getDataRegistrationState: serial %d", serial);
991#endif
992    dispatchVoid(serial, mSlotId, RIL_REQUEST_DATA_REGISTRATION_STATE);
993    return Void();
994}
995
996Return<void> RadioImpl::getOperator(int32_t serial) {
997#if VDBG
998    RLOGD("getOperator: serial %d", serial);
999#endif
1000    dispatchVoid(serial, mSlotId, RIL_REQUEST_OPERATOR);
1001    return Void();
1002}
1003
1004Return<void> RadioImpl::setRadioPower(int32_t serial, bool on) {
1005    RLOGD("setRadioPower: serial %d on %d", serial, on);
1006    dispatchInts(serial, mSlotId, RIL_REQUEST_RADIO_POWER, 1, BOOL_TO_INT(on));
1007    return Void();
1008}
1009
1010Return<void> RadioImpl::sendDtmf(int32_t serial, const hidl_string& s) {
1011#if VDBG
1012    RLOGD("sendDtmf: serial %d", serial);
1013#endif
1014    dispatchString(serial, mSlotId, RIL_REQUEST_DTMF, s.c_str());
1015    return Void();
1016}
1017
1018Return<void> RadioImpl::sendSms(int32_t serial, const GsmSmsMessage& message) {
1019#if VDBG
1020    RLOGD("sendSms: serial %d", serial);
1021#endif
1022    dispatchStrings(serial, mSlotId, RIL_REQUEST_SEND_SMS,
1023            2, message.smscPdu.c_str(), message.pdu.c_str());
1024    return Void();
1025}
1026
1027Return<void> RadioImpl::sendSMSExpectMore(int32_t serial, const GsmSmsMessage& message) {
1028#if VDBG
1029    RLOGD("sendSMSExpectMore: serial %d", serial);
1030#endif
1031    dispatchStrings(serial, mSlotId, RIL_REQUEST_SEND_SMS_EXPECT_MORE,
1032            2, message.smscPdu.c_str(), message.pdu.c_str());
1033    return Void();
1034}
1035
1036static bool convertMvnoTypeToString(MvnoType type, char *&str) {
1037    switch (type) {
1038        case MvnoType::IMSI:
1039            str = (char *)"imsi";
1040            return true;
1041        case MvnoType::GID:
1042            str = (char *)"gid";
1043            return true;
1044        case MvnoType::SPN:
1045            str = (char *)"spn";
1046            return true;
1047        case MvnoType::NONE:
1048            str = (char *)"";
1049            return true;
1050    }
1051    return false;
1052}
1053
1054Return<void> RadioImpl::setupDataCall(int32_t serial, RadioTechnology radioTechnology,
1055                                      const DataProfileInfo& dataProfileInfo, bool modemCognitive,
1056                                      bool roamingAllowed, bool isRoaming) {
1057
1058#if VDBG
1059    RLOGD("setupDataCall: serial %d", serial);
1060#endif
1061
1062    if (s_vendorFunctions->version >= 4 && s_vendorFunctions->version <= 14) {
1063        const hidl_string &protocol =
1064                (isRoaming ? dataProfileInfo.roamingProtocol : dataProfileInfo.protocol);
1065        dispatchStrings(serial, mSlotId, RIL_REQUEST_SETUP_DATA_CALL, 7,
1066            std::to_string((int) radioTechnology + 2).c_str(),
1067            std::to_string((int) dataProfileInfo.profileId).c_str(),
1068            dataProfileInfo.apn.c_str(),
1069            dataProfileInfo.user.c_str(),
1070            dataProfileInfo.password.c_str(),
1071            std::to_string((int) dataProfileInfo.authType).c_str(),
1072            protocol.c_str());
1073    } else if (s_vendorFunctions->version >= 15) {
1074        char *mvnoTypeStr = NULL;
1075        if (!convertMvnoTypeToString(dataProfileInfo.mvnoType, mvnoTypeStr)) {
1076            RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
1077                    RIL_REQUEST_SETUP_DATA_CALL);
1078            if (pRI != NULL) {
1079                sendErrorResponse(pRI, RIL_E_INVALID_ARGUMENTS);
1080            }
1081            return Void();
1082        }
1083        dispatchStrings(serial, mSlotId, RIL_REQUEST_SETUP_DATA_CALL, 15,
1084            std::to_string((int) radioTechnology + 2).c_str(),
1085            std::to_string((int) dataProfileInfo.profileId).c_str(),
1086            dataProfileInfo.apn.c_str(),
1087            dataProfileInfo.user.c_str(),
1088            dataProfileInfo.password.c_str(),
1089            std::to_string((int) dataProfileInfo.authType).c_str(),
1090            dataProfileInfo.protocol.c_str(),
1091            dataProfileInfo.roamingProtocol.c_str(),
1092            std::to_string(dataProfileInfo.supportedApnTypesBitmap).c_str(),
1093            std::to_string(dataProfileInfo.bearerBitmap).c_str(),
1094            modemCognitive ? "1" : "0",
1095            std::to_string(dataProfileInfo.mtu).c_str(),
1096            mvnoTypeStr,
1097            dataProfileInfo.mvnoMatchData.c_str(),
1098            roamingAllowed ? "1" : "0");
1099    } else {
1100        RLOGE("Unsupported RIL version %d, min version expected 4", s_vendorFunctions->version);
1101        RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
1102                RIL_REQUEST_SETUP_DATA_CALL);
1103        if (pRI != NULL) {
1104            sendErrorResponse(pRI, RIL_E_REQUEST_NOT_SUPPORTED);
1105        }
1106    }
1107    return Void();
1108}
1109
1110Return<void> RadioImpl::iccIOForApp(int32_t serial, const IccIo& iccIo) {
1111#if VDBG
1112    RLOGD("iccIOForApp: serial %d", serial);
1113#endif
1114    RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_SIM_IO);
1115    if (pRI == NULL) {
1116        return Void();
1117    }
1118
1119    RIL_SIM_IO_v6 rilIccIo = {};
1120    rilIccIo.command = iccIo.command;
1121    rilIccIo.fileid = iccIo.fileId;
1122    if (!copyHidlStringToRil(&rilIccIo.path, iccIo.path, pRI)) {
1123        return Void();
1124    }
1125
1126    rilIccIo.p1 = iccIo.p1;
1127    rilIccIo.p2 = iccIo.p2;
1128    rilIccIo.p3 = iccIo.p3;
1129
1130    if (!copyHidlStringToRil(&rilIccIo.data, iccIo.data, pRI)) {
1131        memsetAndFreeStrings(1, rilIccIo.path);
1132        return Void();
1133    }
1134
1135    if (!copyHidlStringToRil(&rilIccIo.pin2, iccIo.pin2, pRI)) {
1136        memsetAndFreeStrings(2, rilIccIo.path, rilIccIo.data);
1137        return Void();
1138    }
1139
1140    if (!copyHidlStringToRil(&rilIccIo.aidPtr, iccIo.aid, pRI)) {
1141        memsetAndFreeStrings(3, rilIccIo.path, rilIccIo.data, rilIccIo.pin2);
1142        return Void();
1143    }
1144
1145    s_vendorFunctions->onRequest(RIL_REQUEST_SIM_IO, &rilIccIo, sizeof(rilIccIo), pRI);
1146
1147    memsetAndFreeStrings(4, rilIccIo.path, rilIccIo.data, rilIccIo.pin2, rilIccIo.aidPtr);
1148
1149    return Void();
1150}
1151
1152Return<void> RadioImpl::sendUssd(int32_t serial, const hidl_string& ussd) {
1153#if VDBG
1154    RLOGD("sendUssd: serial %d", serial);
1155#endif
1156    dispatchString(serial, mSlotId, RIL_REQUEST_SEND_USSD, ussd.c_str());
1157    return Void();
1158}
1159
1160Return<void> RadioImpl::cancelPendingUssd(int32_t serial) {
1161#if VDBG
1162    RLOGD("cancelPendingUssd: serial %d", serial);
1163#endif
1164    dispatchVoid(serial, mSlotId, RIL_REQUEST_CANCEL_USSD);
1165    return Void();
1166}
1167
1168Return<void> RadioImpl::getClir(int32_t serial) {
1169#if VDBG
1170    RLOGD("getClir: serial %d", serial);
1171#endif
1172    dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_CLIR);
1173    return Void();
1174}
1175
1176Return<void> RadioImpl::setClir(int32_t serial, int32_t status) {
1177#if VDBG
1178    RLOGD("setClir: serial %d", serial);
1179#endif
1180    dispatchInts(serial, mSlotId, RIL_REQUEST_SET_CLIR, 1, status);
1181    return Void();
1182}
1183
1184Return<void> RadioImpl::getCallForwardStatus(int32_t serial, const CallForwardInfo& callInfo) {
1185#if VDBG
1186    RLOGD("getCallForwardStatus: serial %d", serial);
1187#endif
1188    dispatchCallForwardStatus(serial, mSlotId, RIL_REQUEST_QUERY_CALL_FORWARD_STATUS,
1189            callInfo);
1190    return Void();
1191}
1192
1193Return<void> RadioImpl::setCallForward(int32_t serial, const CallForwardInfo& callInfo) {
1194#if VDBG
1195    RLOGD("setCallForward: serial %d", serial);
1196#endif
1197    dispatchCallForwardStatus(serial, mSlotId, RIL_REQUEST_SET_CALL_FORWARD,
1198            callInfo);
1199    return Void();
1200}
1201
1202Return<void> RadioImpl::getCallWaiting(int32_t serial, int32_t serviceClass) {
1203#if VDBG
1204    RLOGD("getCallWaiting: serial %d", serial);
1205#endif
1206    dispatchInts(serial, mSlotId, RIL_REQUEST_QUERY_CALL_WAITING, 1, serviceClass);
1207    return Void();
1208}
1209
1210Return<void> RadioImpl::setCallWaiting(int32_t serial, bool enable, int32_t serviceClass) {
1211#if VDBG
1212    RLOGD("setCallWaiting: serial %d", serial);
1213#endif
1214    dispatchInts(serial, mSlotId, RIL_REQUEST_SET_CALL_WAITING, 2, BOOL_TO_INT(enable),
1215            serviceClass);
1216    return Void();
1217}
1218
1219Return<void> RadioImpl::acknowledgeLastIncomingGsmSms(int32_t serial,
1220                                                      bool success, SmsAcknowledgeFailCause cause) {
1221#if VDBG
1222    RLOGD("acknowledgeLastIncomingGsmSms: serial %d", serial);
1223#endif
1224    dispatchInts(serial, mSlotId, RIL_REQUEST_SMS_ACKNOWLEDGE, 2, BOOL_TO_INT(success),
1225            cause);
1226    return Void();
1227}
1228
1229Return<void> RadioImpl::acceptCall(int32_t serial) {
1230#if VDBG
1231    RLOGD("acceptCall: serial %d", serial);
1232#endif
1233    dispatchVoid(serial, mSlotId, RIL_REQUEST_ANSWER);
1234    return Void();
1235}
1236
1237Return<void> RadioImpl::deactivateDataCall(int32_t serial,
1238                                           int32_t cid, bool reasonRadioShutDown) {
1239#if VDBG
1240    RLOGD("deactivateDataCall: serial %d", serial);
1241#endif
1242    dispatchStrings(serial, mSlotId, RIL_REQUEST_DEACTIVATE_DATA_CALL,
1243            2, (std::to_string(cid)).c_str(), reasonRadioShutDown ? "1" : "0");
1244    return Void();
1245}
1246
1247Return<void> RadioImpl::getFacilityLockForApp(int32_t serial, const hidl_string& facility,
1248                                              const hidl_string& password, int32_t serviceClass,
1249                                              const hidl_string& appId) {
1250#if VDBG
1251    RLOGD("getFacilityLockForApp: serial %d", serial);
1252#endif
1253    dispatchStrings(serial, mSlotId, RIL_REQUEST_QUERY_FACILITY_LOCK,
1254            4, facility.c_str(), password.c_str(),
1255            (std::to_string(serviceClass)).c_str(), appId.c_str());
1256    return Void();
1257}
1258
1259Return<void> RadioImpl::setFacilityLockForApp(int32_t serial, const hidl_string& facility,
1260                                              bool lockState, const hidl_string& password,
1261                                              int32_t serviceClass, const hidl_string& appId) {
1262#if VDBG
1263    RLOGD("setFacilityLockForApp: serial %d", serial);
1264#endif
1265    dispatchStrings(serial, mSlotId, RIL_REQUEST_SET_FACILITY_LOCK,
1266            5, facility.c_str(), lockState ? "1" : "0", password.c_str(),
1267            (std::to_string(serviceClass)).c_str(), appId.c_str() );
1268    return Void();
1269}
1270
1271Return<void> RadioImpl::setBarringPassword(int32_t serial, const hidl_string& facility,
1272                                           const hidl_string& oldPassword,
1273                                           const hidl_string& newPassword) {
1274#if VDBG
1275    RLOGD("setBarringPassword: serial %d", serial);
1276#endif
1277    dispatchStrings(serial, mSlotId, RIL_REQUEST_CHANGE_BARRING_PASSWORD,
1278            2, oldPassword.c_str(), newPassword.c_str());
1279    return Void();
1280}
1281
1282Return<void> RadioImpl::getNetworkSelectionMode(int32_t serial) {
1283#if VDBG
1284    RLOGD("getNetworkSelectionMode: serial %d", serial);
1285#endif
1286    dispatchVoid(serial, mSlotId, RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE);
1287    return Void();
1288}
1289
1290Return<void> RadioImpl::setNetworkSelectionModeAutomatic(int32_t serial) {
1291#if VDBG
1292    RLOGD("setNetworkSelectionModeAutomatic: serial %d", serial);
1293#endif
1294    dispatchVoid(serial, mSlotId, RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC);
1295    return Void();
1296}
1297
1298Return<void> RadioImpl::setNetworkSelectionModeManual(int32_t serial,
1299                                                      const hidl_string& operatorNumeric) {
1300#if VDBG
1301    RLOGD("setNetworkSelectionModeManual: serial %d", serial);
1302#endif
1303    dispatchString(serial, mSlotId, RIL_REQUEST_SET_NETWORK_SELECTION_MANUAL,
1304            operatorNumeric.c_str());
1305    return Void();
1306}
1307
1308Return<void> RadioImpl::getAvailableNetworks(int32_t serial) {
1309#if VDBG
1310    RLOGD("getAvailableNetworks: serial %d", serial);
1311#endif
1312    dispatchVoid(serial, mSlotId, RIL_REQUEST_QUERY_AVAILABLE_NETWORKS);
1313    return Void();
1314}
1315
1316Return<void> RadioImpl::startDtmf(int32_t serial, const hidl_string& s) {
1317#if VDBG
1318    RLOGD("startDtmf: serial %d", serial);
1319#endif
1320    dispatchString(serial, mSlotId, RIL_REQUEST_DTMF_START,
1321            s.c_str());
1322    return Void();
1323}
1324
1325Return<void> RadioImpl::stopDtmf(int32_t serial) {
1326#if VDBG
1327    RLOGD("stopDtmf: serial %d", serial);
1328#endif
1329    dispatchVoid(serial, mSlotId, RIL_REQUEST_DTMF_STOP);
1330    return Void();
1331}
1332
1333Return<void> RadioImpl::getBasebandVersion(int32_t serial) {
1334#if VDBG
1335    RLOGD("getBasebandVersion: serial %d", serial);
1336#endif
1337    dispatchVoid(serial, mSlotId, RIL_REQUEST_BASEBAND_VERSION);
1338    return Void();
1339}
1340
1341Return<void> RadioImpl::separateConnection(int32_t serial, int32_t gsmIndex) {
1342#if VDBG
1343    RLOGD("separateConnection: serial %d", serial);
1344#endif
1345    dispatchInts(serial, mSlotId, RIL_REQUEST_SEPARATE_CONNECTION, 1, gsmIndex);
1346    return Void();
1347}
1348
1349Return<void> RadioImpl::setMute(int32_t serial, bool enable) {
1350#if VDBG
1351    RLOGD("setMute: serial %d", serial);
1352#endif
1353    dispatchInts(serial, mSlotId, RIL_REQUEST_SET_MUTE, 1, BOOL_TO_INT(enable));
1354    return Void();
1355}
1356
1357Return<void> RadioImpl::getMute(int32_t serial) {
1358#if VDBG
1359    RLOGD("getMute: serial %d", serial);
1360#endif
1361    dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_MUTE);
1362    return Void();
1363}
1364
1365Return<void> RadioImpl::getClip(int32_t serial) {
1366#if VDBG
1367    RLOGD("getClip: serial %d", serial);
1368#endif
1369    dispatchVoid(serial, mSlotId, RIL_REQUEST_QUERY_CLIP);
1370    return Void();
1371}
1372
1373Return<void> RadioImpl::getDataCallList(int32_t serial) {
1374#if VDBG
1375    RLOGD("getDataCallList: serial %d", serial);
1376#endif
1377    dispatchVoid(serial, mSlotId, RIL_REQUEST_DATA_CALL_LIST);
1378    return Void();
1379}
1380
1381Return<void> RadioImpl::setSuppServiceNotifications(int32_t serial, bool enable) {
1382#if VDBG
1383    RLOGD("setSuppServiceNotifications: serial %d", serial);
1384#endif
1385    dispatchInts(serial, mSlotId, RIL_REQUEST_SET_SUPP_SVC_NOTIFICATION, 1,
1386            BOOL_TO_INT(enable));
1387    return Void();
1388}
1389
1390Return<void> RadioImpl::writeSmsToSim(int32_t serial, const SmsWriteArgs& smsWriteArgs) {
1391#if VDBG
1392    RLOGD("writeSmsToSim: serial %d", serial);
1393#endif
1394    RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_WRITE_SMS_TO_SIM);
1395    if (pRI == NULL) {
1396        return Void();
1397    }
1398
1399    RIL_SMS_WriteArgs args;
1400    args.status = (int) smsWriteArgs.status;
1401
1402    int len;
1403    if (!copyHidlStringToRil(&args.pdu, smsWriteArgs.pdu, pRI)) {
1404        return Void();
1405    }
1406
1407    if (!copyHidlStringToRil(&args.smsc, smsWriteArgs.smsc, pRI)) {
1408        memsetAndFreeStrings(1, args.pdu);
1409        return Void();
1410    }
1411
1412    s_vendorFunctions->onRequest(RIL_REQUEST_WRITE_SMS_TO_SIM, &args, sizeof(args), pRI);
1413
1414    memsetAndFreeStrings(2, args.smsc, args.pdu);
1415
1416    return Void();
1417}
1418
1419Return<void> RadioImpl::deleteSmsOnSim(int32_t serial, int32_t index) {
1420#if VDBG
1421    RLOGD("deleteSmsOnSim: serial %d", serial);
1422#endif
1423    dispatchInts(serial, mSlotId, RIL_REQUEST_DELETE_SMS_ON_SIM, 1, index);
1424    return Void();
1425}
1426
1427Return<void> RadioImpl::setBandMode(int32_t serial, RadioBandMode mode) {
1428#if VDBG
1429    RLOGD("setBandMode: serial %d", serial);
1430#endif
1431    dispatchInts(serial, mSlotId, RIL_REQUEST_SET_BAND_MODE, 1, mode);
1432    return Void();
1433}
1434
1435Return<void> RadioImpl::getAvailableBandModes(int32_t serial) {
1436#if VDBG
1437    RLOGD("getAvailableBandModes: serial %d", serial);
1438#endif
1439    dispatchVoid(serial, mSlotId, RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE);
1440    return Void();
1441}
1442
1443Return<void> RadioImpl::sendEnvelope(int32_t serial, const hidl_string& command) {
1444#if VDBG
1445    RLOGD("sendEnvelope: serial %d", serial);
1446#endif
1447    dispatchString(serial, mSlotId, RIL_REQUEST_STK_SEND_ENVELOPE_COMMAND,
1448            command.c_str());
1449    return Void();
1450}
1451
1452Return<void> RadioImpl::sendTerminalResponseToSim(int32_t serial,
1453                                                  const hidl_string& commandResponse) {
1454#if VDBG
1455    RLOGD("sendTerminalResponseToSim: serial %d", serial);
1456#endif
1457    dispatchString(serial, mSlotId, RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE,
1458            commandResponse.c_str());
1459    return Void();
1460}
1461
1462Return<void> RadioImpl::handleStkCallSetupRequestFromSim(int32_t serial, bool accept) {
1463#if VDBG
1464    RLOGD("handleStkCallSetupRequestFromSim: serial %d", serial);
1465#endif
1466    dispatchInts(serial, mSlotId, RIL_REQUEST_STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM,
1467            1, BOOL_TO_INT(accept));
1468    return Void();
1469}
1470
1471Return<void> RadioImpl::explicitCallTransfer(int32_t serial) {
1472#if VDBG
1473    RLOGD("explicitCallTransfer: serial %d", serial);
1474#endif
1475    dispatchVoid(serial, mSlotId, RIL_REQUEST_EXPLICIT_CALL_TRANSFER);
1476    return Void();
1477}
1478
1479Return<void> RadioImpl::setPreferredNetworkType(int32_t serial, PreferredNetworkType nwType) {
1480#if VDBG
1481    RLOGD("setPreferredNetworkType: serial %d", serial);
1482#endif
1483    dispatchInts(serial, mSlotId, RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE, 1, nwType);
1484    return Void();
1485}
1486
1487Return<void> RadioImpl::getPreferredNetworkType(int32_t serial) {
1488#if VDBG
1489    RLOGD("getPreferredNetworkType: serial %d", serial);
1490#endif
1491    dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE);
1492    return Void();
1493}
1494
1495Return<void> RadioImpl::getNeighboringCids(int32_t serial) {
1496#if VDBG
1497    RLOGD("getNeighboringCids: serial %d", serial);
1498#endif
1499    dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_NEIGHBORING_CELL_IDS);
1500    return Void();
1501}
1502
1503Return<void> RadioImpl::setLocationUpdates(int32_t serial, bool enable) {
1504#if VDBG
1505    RLOGD("setLocationUpdates: serial %d", serial);
1506#endif
1507    dispatchInts(serial, mSlotId, RIL_REQUEST_SET_LOCATION_UPDATES, 1, BOOL_TO_INT(enable));
1508    return Void();
1509}
1510
1511Return<void> RadioImpl::setCdmaSubscriptionSource(int32_t serial, CdmaSubscriptionSource cdmaSub) {
1512#if VDBG
1513    RLOGD("setCdmaSubscriptionSource: serial %d", serial);
1514#endif
1515    dispatchInts(serial, mSlotId, RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE, 1, cdmaSub);
1516    return Void();
1517}
1518
1519Return<void> RadioImpl::setCdmaRoamingPreference(int32_t serial, CdmaRoamingType type) {
1520#if VDBG
1521    RLOGD("setCdmaRoamingPreference: serial %d", serial);
1522#endif
1523    dispatchInts(serial, mSlotId, RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE, 1, type);
1524    return Void();
1525}
1526
1527Return<void> RadioImpl::getCdmaRoamingPreference(int32_t serial) {
1528#if VDBG
1529    RLOGD("getCdmaRoamingPreference: serial %d", serial);
1530#endif
1531    dispatchVoid(serial, mSlotId, RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE);
1532    return Void();
1533}
1534
1535Return<void> RadioImpl::setTTYMode(int32_t serial, TtyMode mode) {
1536#if VDBG
1537    RLOGD("setTTYMode: serial %d", serial);
1538#endif
1539    dispatchInts(serial, mSlotId, RIL_REQUEST_SET_TTY_MODE, 1, mode);
1540    return Void();
1541}
1542
1543Return<void> RadioImpl::getTTYMode(int32_t serial) {
1544#if VDBG
1545    RLOGD("getTTYMode: serial %d", serial);
1546#endif
1547    dispatchVoid(serial, mSlotId, RIL_REQUEST_QUERY_TTY_MODE);
1548    return Void();
1549}
1550
1551Return<void> RadioImpl::setPreferredVoicePrivacy(int32_t serial, bool enable) {
1552#if VDBG
1553    RLOGD("setPreferredVoicePrivacy: serial %d", serial);
1554#endif
1555    dispatchInts(serial, mSlotId, RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE,
1556            1, BOOL_TO_INT(enable));
1557    return Void();
1558}
1559
1560Return<void> RadioImpl::getPreferredVoicePrivacy(int32_t serial) {
1561#if VDBG
1562    RLOGD("getPreferredVoicePrivacy: serial %d", serial);
1563#endif
1564    dispatchVoid(serial, mSlotId, RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE);
1565    return Void();
1566}
1567
1568Return<void> RadioImpl::sendCDMAFeatureCode(int32_t serial, const hidl_string& featureCode) {
1569#if VDBG
1570    RLOGD("sendCDMAFeatureCode: serial %d", serial);
1571#endif
1572    dispatchString(serial, mSlotId, RIL_REQUEST_CDMA_FLASH,
1573            featureCode.c_str());
1574    return Void();
1575}
1576
1577Return<void> RadioImpl::sendBurstDtmf(int32_t serial, const hidl_string& dtmf, int32_t on,
1578                                      int32_t off) {
1579#if VDBG
1580    RLOGD("sendBurstDtmf: serial %d", serial);
1581#endif
1582    dispatchStrings(serial, mSlotId, RIL_REQUEST_CDMA_BURST_DTMF,
1583            3, dtmf.c_str(), (std::to_string(on)).c_str(),
1584            (std::to_string(off)).c_str());
1585    return Void();
1586}
1587
1588void constructCdmaSms(RIL_CDMA_SMS_Message &rcsm, const CdmaSmsMessage& sms) {
1589    rcsm.uTeleserviceID = sms.teleserviceId;
1590    rcsm.bIsServicePresent = BOOL_TO_INT(sms.isServicePresent);
1591    rcsm.uServicecategory = sms.serviceCategory;
1592    rcsm.sAddress.digit_mode = (RIL_CDMA_SMS_DigitMode) sms.address.digitMode;
1593    rcsm.sAddress.number_mode = (RIL_CDMA_SMS_NumberMode) sms.address.numberMode;
1594    rcsm.sAddress.number_type = (RIL_CDMA_SMS_NumberType) sms.address.numberType;
1595    rcsm.sAddress.number_plan = (RIL_CDMA_SMS_NumberPlan) sms.address.numberPlan;
1596
1597    rcsm.sAddress.number_of_digits = sms.address.digits.size();
1598    int digitLimit= MIN((rcsm.sAddress.number_of_digits), RIL_CDMA_SMS_ADDRESS_MAX);
1599    for (int i = 0; i < digitLimit; i++) {
1600        rcsm.sAddress.digits[i] = sms.address.digits[i];
1601    }
1602
1603    rcsm.sSubAddress.subaddressType = (RIL_CDMA_SMS_SubaddressType) sms.subAddress.subaddressType;
1604    rcsm.sSubAddress.odd = BOOL_TO_INT(sms.subAddress.odd);
1605
1606    rcsm.sSubAddress.number_of_digits = sms.subAddress.digits.size();
1607    digitLimit= MIN((rcsm.sSubAddress.number_of_digits), RIL_CDMA_SMS_SUBADDRESS_MAX);
1608    for (int i = 0; i < digitLimit; i++) {
1609        rcsm.sSubAddress.digits[i] = sms.subAddress.digits[i];
1610    }
1611
1612    rcsm.uBearerDataLen = sms.bearerData.size();
1613    digitLimit= MIN((rcsm.uBearerDataLen), RIL_CDMA_SMS_BEARER_DATA_MAX);
1614    for (int i = 0; i < digitLimit; i++) {
1615        rcsm.aBearerData[i] = sms.bearerData[i];
1616    }
1617}
1618
1619Return<void> RadioImpl::sendCdmaSms(int32_t serial, const CdmaSmsMessage& sms) {
1620#if VDBG
1621    RLOGD("sendCdmaSms: serial %d", serial);
1622#endif
1623    RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_CDMA_SEND_SMS);
1624    if (pRI == NULL) {
1625        return Void();
1626    }
1627
1628    RIL_CDMA_SMS_Message rcsm = {};
1629    constructCdmaSms(rcsm, sms);
1630
1631    s_vendorFunctions->onRequest(pRI->pCI->requestNumber, &rcsm, sizeof(rcsm), pRI);
1632    return Void();
1633}
1634
1635Return<void> RadioImpl::acknowledgeLastIncomingCdmaSms(int32_t serial, const CdmaSmsAck& smsAck) {
1636#if VDBG
1637    RLOGD("acknowledgeLastIncomingCdmaSms: serial %d", serial);
1638#endif
1639    RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_CDMA_SMS_ACKNOWLEDGE);
1640    if (pRI == NULL) {
1641        return Void();
1642    }
1643
1644    RIL_CDMA_SMS_Ack rcsa = {};
1645
1646    rcsa.uErrorClass = (RIL_CDMA_SMS_ErrorClass) smsAck.errorClass;
1647    rcsa.uSMSCauseCode = smsAck.smsCauseCode;
1648
1649    s_vendorFunctions->onRequest(pRI->pCI->requestNumber, &rcsa, sizeof(rcsa), pRI);
1650    return Void();
1651}
1652
1653Return<void> RadioImpl::getGsmBroadcastConfig(int32_t serial) {
1654#if VDBG
1655    RLOGD("getGsmBroadcastConfig: serial %d", serial);
1656#endif
1657    dispatchVoid(serial, mSlotId, RIL_REQUEST_GSM_GET_BROADCAST_SMS_CONFIG);
1658    return Void();
1659}
1660
1661Return<void> RadioImpl::setGsmBroadcastConfig(int32_t serial,
1662                                              const hidl_vec<GsmBroadcastSmsConfigInfo>&
1663                                              configInfo) {
1664#if VDBG
1665    RLOGD("setGsmBroadcastConfig: serial %d", serial);
1666#endif
1667    RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
1668            RIL_REQUEST_GSM_SET_BROADCAST_SMS_CONFIG);
1669    if (pRI == NULL) {
1670        return Void();
1671    }
1672
1673    int num = configInfo.size();
1674    RIL_GSM_BroadcastSmsConfigInfo gsmBci[num];
1675    RIL_GSM_BroadcastSmsConfigInfo *gsmBciPtrs[num];
1676
1677    for (int i = 0 ; i < num ; i++ ) {
1678        gsmBciPtrs[i] = &gsmBci[i];
1679        gsmBci[i].fromServiceId = configInfo[i].fromServiceId;
1680        gsmBci[i].toServiceId = configInfo[i].toServiceId;
1681        gsmBci[i].fromCodeScheme = configInfo[i].fromCodeScheme;
1682        gsmBci[i].toCodeScheme = configInfo[i].toCodeScheme;
1683        gsmBci[i].selected = BOOL_TO_INT(configInfo[i].selected);
1684    }
1685
1686    s_vendorFunctions->onRequest(pRI->pCI->requestNumber, gsmBciPtrs,
1687            num * sizeof(RIL_GSM_BroadcastSmsConfigInfo *), pRI);
1688    return Void();
1689}
1690
1691Return<void> RadioImpl::setGsmBroadcastActivation(int32_t serial, bool activate) {
1692#if VDBG
1693    RLOGD("setGsmBroadcastActivation: serial %d", serial);
1694#endif
1695    dispatchInts(serial, mSlotId, RIL_REQUEST_GSM_SMS_BROADCAST_ACTIVATION,
1696            1, BOOL_TO_INT(!activate));
1697    return Void();
1698}
1699
1700Return<void> RadioImpl::getCdmaBroadcastConfig(int32_t serial) {
1701#if VDBG
1702    RLOGD("getCdmaBroadcastConfig: serial %d", serial);
1703#endif
1704    dispatchVoid(serial, mSlotId, RIL_REQUEST_CDMA_GET_BROADCAST_SMS_CONFIG);
1705    return Void();
1706}
1707
1708Return<void> RadioImpl::setCdmaBroadcastConfig(int32_t serial,
1709                                               const hidl_vec<CdmaBroadcastSmsConfigInfo>&
1710                                               configInfo) {
1711#if VDBG
1712    RLOGD("setCdmaBroadcastConfig: serial %d", serial);
1713#endif
1714    RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
1715            RIL_REQUEST_CDMA_SET_BROADCAST_SMS_CONFIG);
1716    if (pRI == NULL) {
1717        return Void();
1718    }
1719
1720    int num = configInfo.size();
1721    RIL_CDMA_BroadcastSmsConfigInfo cdmaBci[num];
1722    RIL_CDMA_BroadcastSmsConfigInfo *cdmaBciPtrs[num];
1723
1724    for (int i = 0 ; i < num ; i++ ) {
1725        cdmaBciPtrs[i] = &cdmaBci[i];
1726        cdmaBci[i].service_category = configInfo[i].serviceCategory;
1727        cdmaBci[i].language = configInfo[i].language;
1728        cdmaBci[i].selected = BOOL_TO_INT(configInfo[i].selected);
1729    }
1730
1731    s_vendorFunctions->onRequest(pRI->pCI->requestNumber, cdmaBciPtrs,
1732            num * sizeof(RIL_CDMA_BroadcastSmsConfigInfo *), pRI);
1733    return Void();
1734}
1735
1736Return<void> RadioImpl::setCdmaBroadcastActivation(int32_t serial, bool activate) {
1737#if VDBG
1738    RLOGD("setCdmaBroadcastActivation: serial %d", serial);
1739#endif
1740    dispatchInts(serial, mSlotId, RIL_REQUEST_CDMA_SMS_BROADCAST_ACTIVATION,
1741            1, BOOL_TO_INT(!activate));
1742    return Void();
1743}
1744
1745Return<void> RadioImpl::getCDMASubscription(int32_t serial) {
1746#if VDBG
1747    RLOGD("getCDMASubscription: serial %d", serial);
1748#endif
1749    dispatchVoid(serial, mSlotId, RIL_REQUEST_CDMA_SUBSCRIPTION);
1750    return Void();
1751}
1752
1753Return<void> RadioImpl::writeSmsToRuim(int32_t serial, const CdmaSmsWriteArgs& cdmaSms) {
1754#if VDBG
1755    RLOGD("writeSmsToRuim: serial %d", serial);
1756#endif
1757    RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
1758            RIL_REQUEST_CDMA_WRITE_SMS_TO_RUIM);
1759    if (pRI == NULL) {
1760        return Void();
1761    }
1762
1763    RIL_CDMA_SMS_WriteArgs rcsw = {};
1764    rcsw.status = (int) cdmaSms.status;
1765    constructCdmaSms(rcsw.message, cdmaSms.message);
1766
1767    s_vendorFunctions->onRequest(pRI->pCI->requestNumber, &rcsw, sizeof(rcsw), pRI);
1768    return Void();
1769}
1770
1771Return<void> RadioImpl::deleteSmsOnRuim(int32_t serial, int32_t index) {
1772#if VDBG
1773    RLOGD("deleteSmsOnRuim: serial %d", serial);
1774#endif
1775    dispatchInts(serial, mSlotId, RIL_REQUEST_CDMA_DELETE_SMS_ON_RUIM, 1, index);
1776    return Void();
1777}
1778
1779Return<void> RadioImpl::getDeviceIdentity(int32_t serial) {
1780#if VDBG
1781    RLOGD("getDeviceIdentity: serial %d", serial);
1782#endif
1783    dispatchVoid(serial, mSlotId, RIL_REQUEST_DEVICE_IDENTITY);
1784    return Void();
1785}
1786
1787Return<void> RadioImpl::exitEmergencyCallbackMode(int32_t serial) {
1788#if VDBG
1789    RLOGD("exitEmergencyCallbackMode: serial %d", serial);
1790#endif
1791    dispatchVoid(serial, mSlotId, RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE);
1792    return Void();
1793}
1794
1795Return<void> RadioImpl::getSmscAddress(int32_t serial) {
1796#if VDBG
1797    RLOGD("getSmscAddress: serial %d", serial);
1798#endif
1799    dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_SMSC_ADDRESS);
1800    return Void();
1801}
1802
1803Return<void> RadioImpl::setSmscAddress(int32_t serial, const hidl_string& smsc) {
1804#if VDBG
1805    RLOGD("setSmscAddress: serial %d", serial);
1806#endif
1807    dispatchString(serial, mSlotId, RIL_REQUEST_SET_SMSC_ADDRESS,
1808            smsc.c_str());
1809    return Void();
1810}
1811
1812Return<void> RadioImpl::reportSmsMemoryStatus(int32_t serial, bool available) {
1813#if VDBG
1814    RLOGD("reportSmsMemoryStatus: serial %d", serial);
1815#endif
1816    dispatchInts(serial, mSlotId, RIL_REQUEST_REPORT_SMS_MEMORY_STATUS, 1,
1817            BOOL_TO_INT(available));
1818    return Void();
1819}
1820
1821Return<void> RadioImpl::reportStkServiceIsRunning(int32_t serial) {
1822#if VDBG
1823    RLOGD("reportStkServiceIsRunning: serial %d", serial);
1824#endif
1825    dispatchVoid(serial, mSlotId, RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING);
1826    return Void();
1827}
1828
1829Return<void> RadioImpl::getCdmaSubscriptionSource(int32_t serial) {
1830#if VDBG
1831    RLOGD("getCdmaSubscriptionSource: serial %d", serial);
1832#endif
1833    dispatchVoid(serial, mSlotId, RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE);
1834    return Void();
1835}
1836
1837Return<void> RadioImpl::requestIsimAuthentication(int32_t serial, const hidl_string& challenge) {
1838#if VDBG
1839    RLOGD("requestIsimAuthentication: serial %d", serial);
1840#endif
1841    dispatchString(serial, mSlotId, RIL_REQUEST_ISIM_AUTHENTICATION,
1842            challenge.c_str());
1843    return Void();
1844}
1845
1846Return<void> RadioImpl::acknowledgeIncomingGsmSmsWithPdu(int32_t serial, bool success,
1847                                                         const hidl_string& ackPdu) {
1848#if VDBG
1849    RLOGD("acknowledgeIncomingGsmSmsWithPdu: serial %d", serial);
1850#endif
1851    dispatchStrings(serial, mSlotId, RIL_REQUEST_ACKNOWLEDGE_INCOMING_GSM_SMS_WITH_PDU,
1852            2, success ? "1" : "0", ackPdu.c_str());
1853    return Void();
1854}
1855
1856Return<void> RadioImpl::sendEnvelopeWithStatus(int32_t serial, const hidl_string& contents) {
1857#if VDBG
1858    RLOGD("sendEnvelopeWithStatus: serial %d", serial);
1859#endif
1860    dispatchString(serial, mSlotId, RIL_REQUEST_STK_SEND_ENVELOPE_WITH_STATUS,
1861            contents.c_str());
1862    return Void();
1863}
1864
1865Return<void> RadioImpl::getVoiceRadioTechnology(int32_t serial) {
1866#if VDBG
1867    RLOGD("getVoiceRadioTechnology: serial %d", serial);
1868#endif
1869    dispatchVoid(serial, mSlotId, RIL_REQUEST_VOICE_RADIO_TECH);
1870    return Void();
1871}
1872
1873Return<void> RadioImpl::getCellInfoList(int32_t serial) {
1874#if VDBG
1875    RLOGD("getCellInfoList: serial %d", serial);
1876#endif
1877    dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_CELL_INFO_LIST);
1878    return Void();
1879}
1880
1881Return<void> RadioImpl::setCellInfoListRate(int32_t serial, int32_t rate) {
1882#if VDBG
1883    RLOGD("setCellInfoListRate: serial %d", serial);
1884#endif
1885    dispatchInts(serial, mSlotId, RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE, 1, rate);
1886    return Void();
1887}
1888
1889Return<void> RadioImpl::setInitialAttachApn(int32_t serial, const DataProfileInfo& dataProfileInfo,
1890                                            bool modemCognitive, bool isRoaming) {
1891#if VDBG
1892    RLOGD("setInitialAttachApn: serial %d", serial);
1893#endif
1894    RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
1895            RIL_REQUEST_SET_INITIAL_ATTACH_APN);
1896    if (pRI == NULL) {
1897        return Void();
1898    }
1899
1900    if (s_vendorFunctions->version <= 14) {
1901        RIL_InitialAttachApn iaa = {};
1902
1903        if (!copyHidlStringToRil(&iaa.apn, dataProfileInfo.apn, pRI)) {
1904            return Void();
1905        }
1906
1907        const hidl_string &protocol =
1908                (isRoaming ? dataProfileInfo.roamingProtocol : dataProfileInfo.protocol);
1909
1910        if (!copyHidlStringToRil(&iaa.protocol, protocol, pRI)) {
1911            memsetAndFreeStrings(1, iaa.apn);
1912            return Void();
1913        }
1914        iaa.authtype = (int) dataProfileInfo.authType;
1915        if (!copyHidlStringToRil(&iaa.username, dataProfileInfo.user, pRI)) {
1916            memsetAndFreeStrings(2, iaa.apn, iaa.protocol);
1917            return Void();
1918        }
1919        if (!copyHidlStringToRil(&iaa.password, dataProfileInfo.password, pRI)) {
1920            memsetAndFreeStrings(3, iaa.apn, iaa.protocol, iaa.username);
1921            return Void();
1922        }
1923
1924        s_vendorFunctions->onRequest(RIL_REQUEST_SET_INITIAL_ATTACH_APN, &iaa, sizeof(iaa), pRI);
1925
1926        memsetAndFreeStrings(4, iaa.apn, iaa.protocol, iaa.username, iaa.password);
1927    } else {
1928        RIL_InitialAttachApn_v15 iaa = {};
1929
1930        if (!copyHidlStringToRil(&iaa.apn, dataProfileInfo.apn, pRI)) {
1931            return Void();
1932        }
1933        if (!copyHidlStringToRil(&iaa.protocol, dataProfileInfo.protocol, pRI)) {
1934            memsetAndFreeStrings(1, iaa.apn);
1935            return Void();
1936        }
1937        if (!copyHidlStringToRil(&iaa.roamingProtocol, dataProfileInfo.roamingProtocol, pRI)) {
1938            memsetAndFreeStrings(2, iaa.apn, iaa.protocol);
1939            return Void();
1940        }
1941        iaa.authtype = (int) dataProfileInfo.authType;
1942        if (!copyHidlStringToRil(&iaa.username, dataProfileInfo.user, pRI)) {
1943            memsetAndFreeStrings(3, iaa.apn, iaa.protocol, iaa.roamingProtocol);
1944            return Void();
1945        }
1946        if (!copyHidlStringToRil(&iaa.password, dataProfileInfo.password, pRI)) {
1947            memsetAndFreeStrings(4, iaa.apn, iaa.protocol, iaa.roamingProtocol, iaa.username);
1948            return Void();
1949        }
1950        iaa.supportedTypesBitmask = dataProfileInfo.supportedApnTypesBitmap;
1951        iaa.bearerBitmask = dataProfileInfo.bearerBitmap;
1952        iaa.modemCognitive = BOOL_TO_INT(modemCognitive);
1953        iaa.mtu = dataProfileInfo.mtu;
1954
1955        if (!convertMvnoTypeToString(dataProfileInfo.mvnoType, iaa.mvnoType)) {
1956            sendErrorResponse(pRI, RIL_E_INVALID_ARGUMENTS);
1957            memsetAndFreeStrings(5, iaa.apn, iaa.protocol, iaa.roamingProtocol, iaa.username,
1958                    iaa.password);
1959            return Void();
1960        }
1961
1962        if (!copyHidlStringToRil(&iaa.mvnoMatchData, dataProfileInfo.mvnoMatchData, pRI)) {
1963            memsetAndFreeStrings(5, iaa.apn, iaa.protocol, iaa.roamingProtocol, iaa.username,
1964                    iaa.password);
1965            return Void();
1966        }
1967
1968        s_vendorFunctions->onRequest(RIL_REQUEST_SET_INITIAL_ATTACH_APN, &iaa, sizeof(iaa), pRI);
1969
1970        memsetAndFreeStrings(6, iaa.apn, iaa.protocol, iaa.roamingProtocol, iaa.username,
1971                iaa.password, iaa.mvnoMatchData);
1972    }
1973
1974    return Void();
1975}
1976
1977Return<void> RadioImpl::getImsRegistrationState(int32_t serial) {
1978#if VDBG
1979    RLOGD("getImsRegistrationState: serial %d", serial);
1980#endif
1981    dispatchVoid(serial, mSlotId, RIL_REQUEST_IMS_REGISTRATION_STATE);
1982    return Void();
1983}
1984
1985bool dispatchImsGsmSms(const ImsSmsMessage& message, RequestInfo *pRI) {
1986    RIL_IMS_SMS_Message rism = {};
1987    char **pStrings;
1988    int countStrings = 2;
1989    int dataLen = sizeof(char *) * countStrings;
1990
1991    rism.tech = RADIO_TECH_3GPP;
1992    rism.retry = BOOL_TO_INT(message.retry);
1993    rism.messageRef = message.messageRef;
1994
1995    if (message.gsmMessage.size() != 1) {
1996        RLOGE("dispatchImsGsmSms: Invalid len %s", requestToString(pRI->pCI->requestNumber));
1997        sendErrorResponse(pRI, RIL_E_INVALID_ARGUMENTS);
1998        return false;
1999    }
2000
2001    pStrings = (char **)calloc(countStrings, sizeof(char *));
2002    if (pStrings == NULL) {
2003        RLOGE("dispatchImsGsmSms: Memory allocation failed for request %s",
2004                requestToString(pRI->pCI->requestNumber));
2005        sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2006        return false;
2007    }
2008
2009    if (!copyHidlStringToRil(&pStrings[0], message.gsmMessage[0].smscPdu, pRI)) {
2010#ifdef MEMSET_FREED
2011        memset(pStrings, 0, dataLen);
2012#endif
2013        free(pStrings);
2014        return false;
2015    }
2016
2017    if (!copyHidlStringToRil(&pStrings[1], message.gsmMessage[0].pdu, pRI)) {
2018        memsetAndFreeStrings(1, pStrings[0]);
2019#ifdef MEMSET_FREED
2020        memset(pStrings, 0, dataLen);
2021#endif
2022        free(pStrings);
2023        return false;
2024    }
2025
2026    rism.message.gsmMessage = pStrings;
2027    s_vendorFunctions->onRequest(pRI->pCI->requestNumber, &rism, sizeof(RIL_RadioTechnologyFamily) +
2028            sizeof(uint8_t) + sizeof(int32_t) + dataLen, pRI);
2029
2030    for (int i = 0 ; i < countStrings ; i++) {
2031        memsetAndFreeStrings(1, pStrings[i]);
2032    }
2033
2034#ifdef MEMSET_FREED
2035    memset(pStrings, 0, dataLen);
2036#endif
2037    free(pStrings);
2038
2039    return true;
2040}
2041
2042bool dispatchImsCdmaSms(const ImsSmsMessage& message, RequestInfo *pRI) {
2043    RIL_IMS_SMS_Message rism = {};
2044    RIL_CDMA_SMS_Message rcsm = {};
2045
2046    if (message.cdmaMessage.size() != 1) {
2047        RLOGE("dispatchImsCdmaSms: Invalid len %s", requestToString(pRI->pCI->requestNumber));
2048        sendErrorResponse(pRI, RIL_E_INVALID_ARGUMENTS);
2049        return false;
2050    }
2051
2052    rism.tech = RADIO_TECH_3GPP2;
2053    rism.retry = BOOL_TO_INT(message.retry);
2054    rism.messageRef = message.messageRef;
2055    rism.message.cdmaMessage = &rcsm;
2056
2057    constructCdmaSms(rcsm, message.cdmaMessage[0]);
2058
2059    s_vendorFunctions->onRequest(pRI->pCI->requestNumber, &rism, sizeof(RIL_RadioTechnologyFamily) +
2060            sizeof(uint8_t) + sizeof(int32_t) + sizeof(rcsm), pRI);
2061
2062    return true;
2063}
2064
2065Return<void> RadioImpl::sendImsSms(int32_t serial, const ImsSmsMessage& message) {
2066#if VDBG
2067    RLOGD("sendImsSms: serial %d", serial);
2068#endif
2069    RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_IMS_SEND_SMS);
2070    if (pRI == NULL) {
2071        return Void();
2072    }
2073
2074    RIL_RadioTechnologyFamily format = (RIL_RadioTechnologyFamily) message.tech;
2075
2076    if (RADIO_TECH_3GPP == format) {
2077        dispatchImsGsmSms(message, pRI);
2078    } else if (RADIO_TECH_3GPP2 == format) {
2079        dispatchImsCdmaSms(message, pRI);
2080    } else {
2081        RLOGE("sendImsSms: Invalid radio tech %s",
2082                requestToString(pRI->pCI->requestNumber));
2083        sendErrorResponse(pRI, RIL_E_INVALID_ARGUMENTS);
2084    }
2085    return Void();
2086}
2087
2088Return<void> RadioImpl::iccTransmitApduBasicChannel(int32_t serial, const SimApdu& message) {
2089#if VDBG
2090    RLOGD("iccTransmitApduBasicChannel: serial %d", serial);
2091#endif
2092    dispatchIccApdu(serial, mSlotId, RIL_REQUEST_SIM_TRANSMIT_APDU_BASIC, message);
2093    return Void();
2094}
2095
2096Return<void> RadioImpl::iccOpenLogicalChannel(int32_t serial, const hidl_string& aid, int32_t p2) {
2097#if VDBG
2098    RLOGD("iccOpenLogicalChannel: serial %d", serial);
2099#endif
2100    if (s_vendorFunctions->version < 15) {
2101        dispatchString(serial, mSlotId, RIL_REQUEST_SIM_OPEN_CHANNEL, aid.c_str());
2102    } else {
2103        RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_SIM_OPEN_CHANNEL);
2104        if (pRI == NULL) {
2105            return Void();
2106        }
2107
2108        RIL_OpenChannelParams params = {};
2109
2110        params.p2 = p2;
2111
2112        if (!copyHidlStringToRil(&params.aidPtr, aid, pRI)) {
2113            return Void();
2114        }
2115
2116        s_vendorFunctions->onRequest(pRI->pCI->requestNumber, &params, sizeof(params), pRI);
2117
2118        memsetAndFreeStrings(1, params.aidPtr);
2119    }
2120    return Void();
2121}
2122
2123Return<void> RadioImpl::iccCloseLogicalChannel(int32_t serial, int32_t channelId) {
2124#if VDBG
2125    RLOGD("iccCloseLogicalChannel: serial %d", serial);
2126#endif
2127    dispatchInts(serial, mSlotId, RIL_REQUEST_SIM_CLOSE_CHANNEL, 1, channelId);
2128    return Void();
2129}
2130
2131Return<void> RadioImpl::iccTransmitApduLogicalChannel(int32_t serial, const SimApdu& message) {
2132#if VDBG
2133    RLOGD("iccTransmitApduLogicalChannel: serial %d", serial);
2134#endif
2135    dispatchIccApdu(serial, mSlotId, RIL_REQUEST_SIM_TRANSMIT_APDU_CHANNEL, message);
2136    return Void();
2137}
2138
2139Return<void> RadioImpl::nvReadItem(int32_t serial, NvItem itemId) {
2140#if VDBG
2141    RLOGD("nvReadItem: serial %d", serial);
2142#endif
2143    RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_NV_READ_ITEM);
2144    if (pRI == NULL) {
2145        return Void();
2146    }
2147
2148    RIL_NV_ReadItem nvri = {};
2149    nvri.itemID = (RIL_NV_Item) itemId;
2150
2151    s_vendorFunctions->onRequest(pRI->pCI->requestNumber, &nvri, sizeof(nvri), pRI);
2152    return Void();
2153}
2154
2155Return<void> RadioImpl::nvWriteItem(int32_t serial, const NvWriteItem& item) {
2156#if VDBG
2157    RLOGD("nvWriteItem: serial %d", serial);
2158#endif
2159    RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_NV_WRITE_ITEM);
2160    if (pRI == NULL) {
2161        return Void();
2162    }
2163
2164    RIL_NV_WriteItem nvwi = {};
2165
2166    nvwi.itemID = (RIL_NV_Item) item.itemId;
2167
2168    if (!copyHidlStringToRil(&nvwi.value, item.value, pRI)) {
2169        return Void();
2170    }
2171
2172    s_vendorFunctions->onRequest(pRI->pCI->requestNumber, &nvwi, sizeof(nvwi), pRI);
2173
2174    memsetAndFreeStrings(1, nvwi.value);
2175    return Void();
2176}
2177
2178Return<void> RadioImpl::nvWriteCdmaPrl(int32_t serial, const hidl_vec<uint8_t>& prl) {
2179#if VDBG
2180    RLOGD("nvWriteCdmaPrl: serial %d", serial);
2181#endif
2182    dispatchRaw(serial, mSlotId, RIL_REQUEST_NV_WRITE_CDMA_PRL, prl);
2183    return Void();
2184}
2185
2186Return<void> RadioImpl::nvResetConfig(int32_t serial, ResetNvType resetType) {
2187#if VDBG
2188    RLOGD("nvResetConfig: serial %d", serial);
2189#endif
2190    dispatchInts(serial, mSlotId, RIL_REQUEST_NV_RESET_CONFIG, 1, (int) resetType);
2191    return Void();
2192}
2193
2194Return<void> RadioImpl::setUiccSubscription(int32_t serial, const SelectUiccSub& uiccSub) {
2195#if VDBG
2196    RLOGD("setUiccSubscription: serial %d", serial);
2197#endif
2198    RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
2199            RIL_REQUEST_SET_UICC_SUBSCRIPTION);
2200    if (pRI == NULL) {
2201        return Void();
2202    }
2203
2204    RIL_SelectUiccSub rilUiccSub = {};
2205
2206    rilUiccSub.slot = uiccSub.slot;
2207    rilUiccSub.app_index = uiccSub.appIndex;
2208    rilUiccSub.sub_type = (RIL_SubscriptionType) uiccSub.subType;
2209    rilUiccSub.act_status = (RIL_UiccSubActStatus) uiccSub.actStatus;
2210
2211    s_vendorFunctions->onRequest(pRI->pCI->requestNumber, &rilUiccSub, sizeof(rilUiccSub), pRI);
2212    return Void();
2213}
2214
2215Return<void> RadioImpl::setDataAllowed(int32_t serial, bool allow) {
2216#if VDBG
2217    RLOGD("setDataAllowed: serial %d", serial);
2218#endif
2219    dispatchInts(serial, mSlotId, RIL_REQUEST_ALLOW_DATA, 1, BOOL_TO_INT(allow));
2220    return Void();
2221}
2222
2223Return<void> RadioImpl::getHardwareConfig(int32_t serial) {
2224#if VDBG
2225    RLOGD("getHardwareConfig: serial %d", serial);
2226#endif
2227    dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_HARDWARE_CONFIG);
2228    return Void();
2229}
2230
2231Return<void> RadioImpl::requestIccSimAuthentication(int32_t serial, int32_t authContext,
2232        const hidl_string& authData, const hidl_string& aid) {
2233#if VDBG
2234    RLOGD("requestIccSimAuthentication: serial %d", serial);
2235#endif
2236    RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_SIM_AUTHENTICATION);
2237    if (pRI == NULL) {
2238        return Void();
2239    }
2240
2241    RIL_SimAuthentication pf = {};
2242
2243    pf.authContext = authContext;
2244
2245    int len;
2246    if (!copyHidlStringToRil(&pf.authData, authData, pRI)) {
2247        return Void();
2248    }
2249
2250    if (!copyHidlStringToRil(&pf.aid, aid, pRI)) {
2251        memsetAndFreeStrings(1, pf.authData);
2252        return Void();
2253    }
2254
2255    s_vendorFunctions->onRequest(pRI->pCI->requestNumber, &pf, sizeof(pf), pRI);
2256
2257    memsetAndFreeStrings(2, pf.authData, pf.aid);
2258    return Void();
2259}
2260
2261/**
2262 * @param numProfiles number of data profile
2263 * @param dataProfiles the pointer to the actual data profiles. The acceptable type is
2264          RIL_DataProfileInfo or RIL_DataProfileInfo_v15.
2265 * @param dataProfilePtrs the pointer to the pointers that point to each data profile structure
2266 * @param numfields number of string-type member in the data profile structure
2267 * @param ... the variadic parameters are pointers to each string-type member
2268 **/
2269template <typename T>
2270void freeSetDataProfileData(int numProfiles, T *dataProfiles, T **dataProfilePtrs,
2271                            int numfields, ...) {
2272    va_list args;
2273    va_start(args, numfields);
2274
2275    // Iterate through each string-type field that need to be free.
2276    for (int i = 0; i < numfields; i++) {
2277        // Iterate through each data profile and free that specific string-type field.
2278        // The type 'char *T::*' is a type of pointer to a 'char *' member inside T structure.
2279        char *T::*ptr = va_arg(args, char *T::*);
2280        for (int j = 0; j < numProfiles; j++) {
2281            memsetAndFreeStrings(1, dataProfiles[j].*ptr);
2282        }
2283    }
2284
2285    va_end(args);
2286
2287#ifdef MEMSET_FREED
2288    memset(dataProfiles, 0, numProfiles * sizeof(T));
2289    memset(dataProfilePtrs, 0, numProfiles * sizeof(T *));
2290#endif
2291    free(dataProfiles);
2292    free(dataProfilePtrs);
2293}
2294
2295Return<void> RadioImpl::setDataProfile(int32_t serial, const hidl_vec<DataProfileInfo>& profiles,
2296                                       bool isRoaming) {
2297#if VDBG
2298    RLOGD("setDataProfile: serial %d", serial);
2299#endif
2300    RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_SET_DATA_PROFILE);
2301    if (pRI == NULL) {
2302        return Void();
2303    }
2304
2305    size_t num = profiles.size();
2306    bool success = false;
2307
2308    if (s_vendorFunctions->version <= 14) {
2309
2310        RIL_DataProfileInfo *dataProfiles =
2311            (RIL_DataProfileInfo *) calloc(num, sizeof(RIL_DataProfileInfo));
2312
2313        if (dataProfiles == NULL) {
2314            RLOGE("Memory allocation failed for request %s",
2315                    requestToString(pRI->pCI->requestNumber));
2316            sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2317            return Void();
2318        }
2319
2320        RIL_DataProfileInfo **dataProfilePtrs =
2321            (RIL_DataProfileInfo **) calloc(num, sizeof(RIL_DataProfileInfo *));
2322        if (dataProfilePtrs == NULL) {
2323            RLOGE("Memory allocation failed for request %s",
2324                    requestToString(pRI->pCI->requestNumber));
2325            free(dataProfiles);
2326            sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2327            return Void();
2328        }
2329
2330        for (size_t i = 0; i < num; i++) {
2331            dataProfilePtrs[i] = &dataProfiles[i];
2332
2333            success = copyHidlStringToRil(&dataProfiles[i].apn, profiles[i].apn, pRI);
2334
2335            const hidl_string &protocol =
2336                    (isRoaming ? profiles[i].roamingProtocol : profiles[i].protocol);
2337
2338            if (success && !copyHidlStringToRil(&dataProfiles[i].protocol, protocol, pRI)) {
2339                success = false;
2340            }
2341
2342            if (success && !copyHidlStringToRil(&dataProfiles[i].user, profiles[i].user, pRI)) {
2343                success = false;
2344            }
2345            if (success && !copyHidlStringToRil(&dataProfiles[i].password, profiles[i].password,
2346                    pRI)) {
2347                success = false;
2348            }
2349
2350            if (!success) {
2351                freeSetDataProfileData(num, dataProfiles, dataProfilePtrs, 4,
2352                    &RIL_DataProfileInfo::apn, &RIL_DataProfileInfo::protocol,
2353                    &RIL_DataProfileInfo::user, &RIL_DataProfileInfo::password);
2354                return Void();
2355            }
2356
2357            dataProfiles[i].profileId = (RIL_DataProfile) profiles[i].profileId;
2358            dataProfiles[i].authType = (int) profiles[i].authType;
2359            dataProfiles[i].type = (int) profiles[i].type;
2360            dataProfiles[i].maxConnsTime = profiles[i].maxConnsTime;
2361            dataProfiles[i].maxConns = profiles[i].maxConns;
2362            dataProfiles[i].waitTime = profiles[i].waitTime;
2363            dataProfiles[i].enabled = BOOL_TO_INT(profiles[i].enabled);
2364        }
2365
2366        s_vendorFunctions->onRequest(RIL_REQUEST_SET_DATA_PROFILE, dataProfilePtrs,
2367                num * sizeof(RIL_DataProfileInfo *), pRI);
2368
2369        freeSetDataProfileData(num, dataProfiles, dataProfilePtrs, 4,
2370                &RIL_DataProfileInfo::apn, &RIL_DataProfileInfo::protocol,
2371                &RIL_DataProfileInfo::user, &RIL_DataProfileInfo::password);
2372    } else {
2373        RIL_DataProfileInfo_v15 *dataProfiles =
2374            (RIL_DataProfileInfo_v15 *) calloc(num, sizeof(RIL_DataProfileInfo_v15));
2375
2376        if (dataProfiles == NULL) {
2377            RLOGE("Memory allocation failed for request %s",
2378                    requestToString(pRI->pCI->requestNumber));
2379            sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2380            return Void();
2381        }
2382
2383        RIL_DataProfileInfo_v15 **dataProfilePtrs =
2384            (RIL_DataProfileInfo_v15 **) calloc(num, sizeof(RIL_DataProfileInfo_v15 *));
2385        if (dataProfilePtrs == NULL) {
2386            RLOGE("Memory allocation failed for request %s",
2387                    requestToString(pRI->pCI->requestNumber));
2388            free(dataProfiles);
2389            sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2390            return Void();
2391        }
2392
2393        for (size_t i = 0; i < num; i++) {
2394            dataProfilePtrs[i] = &dataProfiles[i];
2395
2396            success = copyHidlStringToRil(&dataProfiles[i].apn, profiles[i].apn, pRI);
2397            if (success && !copyHidlStringToRil(&dataProfiles[i].protocol, profiles[i].protocol,
2398                    pRI)) {
2399                success = false;
2400            }
2401            if (success && !copyHidlStringToRil(&dataProfiles[i].roamingProtocol,
2402                    profiles[i].roamingProtocol, pRI)) {
2403                success = false;
2404            }
2405            if (success && !copyHidlStringToRil(&dataProfiles[i].user, profiles[i].user, pRI)) {
2406                success = false;
2407            }
2408            if (success && !copyHidlStringToRil(&dataProfiles[i].password, profiles[i].password,
2409                    pRI)) {
2410                success = false;
2411            }
2412
2413            if (success && !copyHidlStringToRil(&dataProfiles[i].mvnoMatchData,
2414                    profiles[i].mvnoMatchData, pRI)) {
2415                success = false;
2416            }
2417
2418            if (success && !convertMvnoTypeToString(profiles[i].mvnoType,
2419                    dataProfiles[i].mvnoType)) {
2420                sendErrorResponse(pRI, RIL_E_INVALID_ARGUMENTS);
2421                success = false;
2422            }
2423
2424            if (!success) {
2425                freeSetDataProfileData(num, dataProfiles, dataProfilePtrs, 6,
2426                    &RIL_DataProfileInfo_v15::apn, &RIL_DataProfileInfo_v15::protocol,
2427                    &RIL_DataProfileInfo_v15::roamingProtocol, &RIL_DataProfileInfo_v15::user,
2428                    &RIL_DataProfileInfo_v15::password, &RIL_DataProfileInfo_v15::mvnoMatchData);
2429                return Void();
2430            }
2431
2432            dataProfiles[i].profileId = (RIL_DataProfile) profiles[i].profileId;
2433            dataProfiles[i].authType = (int) profiles[i].authType;
2434            dataProfiles[i].type = (int) profiles[i].type;
2435            dataProfiles[i].maxConnsTime = profiles[i].maxConnsTime;
2436            dataProfiles[i].maxConns = profiles[i].maxConns;
2437            dataProfiles[i].waitTime = profiles[i].waitTime;
2438            dataProfiles[i].enabled = BOOL_TO_INT(profiles[i].enabled);
2439            dataProfiles[i].supportedTypesBitmask = profiles[i].supportedApnTypesBitmap;
2440            dataProfiles[i].bearerBitmask = profiles[i].bearerBitmap;
2441            dataProfiles[i].mtu = profiles[i].mtu;
2442        }
2443
2444        s_vendorFunctions->onRequest(RIL_REQUEST_SET_DATA_PROFILE, dataProfilePtrs,
2445                num * sizeof(RIL_DataProfileInfo_v15 *), pRI);
2446
2447        freeSetDataProfileData(num, dataProfiles, dataProfilePtrs, 6,
2448                &RIL_DataProfileInfo_v15::apn, &RIL_DataProfileInfo_v15::protocol,
2449                &RIL_DataProfileInfo_v15::roamingProtocol, &RIL_DataProfileInfo_v15::user,
2450                &RIL_DataProfileInfo_v15::password, &RIL_DataProfileInfo_v15::mvnoMatchData);
2451    }
2452
2453    return Void();
2454}
2455
2456Return<void> RadioImpl::requestShutdown(int32_t serial) {
2457#if VDBG
2458    RLOGD("requestShutdown: serial %d", serial);
2459#endif
2460    dispatchVoid(serial, mSlotId, RIL_REQUEST_SHUTDOWN);
2461    return Void();
2462}
2463
2464Return<void> RadioImpl::getRadioCapability(int32_t serial) {
2465#if VDBG
2466    RLOGD("getRadioCapability: serial %d", serial);
2467#endif
2468    dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_RADIO_CAPABILITY);
2469    return Void();
2470}
2471
2472Return<void> RadioImpl::setRadioCapability(int32_t serial, const RadioCapability& rc) {
2473#if VDBG
2474    RLOGD("setRadioCapability: serial %d", serial);
2475#endif
2476    RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_SET_RADIO_CAPABILITY);
2477    if (pRI == NULL) {
2478        return Void();
2479    }
2480
2481    RIL_RadioCapability rilRc = {};
2482
2483    // TODO : set rilRc.version using HIDL version ?
2484    rilRc.session = rc.session;
2485    rilRc.phase = (int) rc.phase;
2486    rilRc.rat = (int) rc.raf;
2487    rilRc.status = (int) rc.status;
2488    strncpy(rilRc.logicalModemUuid, rc.logicalModemUuid.c_str(), MAX_UUID_LENGTH);
2489
2490    s_vendorFunctions->onRequest(pRI->pCI->requestNumber, &rilRc, sizeof(rilRc), pRI);
2491
2492    return Void();
2493}
2494
2495Return<void> RadioImpl::startLceService(int32_t serial, int32_t reportInterval, bool pullMode) {
2496#if VDBG
2497    RLOGD("startLceService: serial %d", serial);
2498#endif
2499    dispatchInts(serial, mSlotId, RIL_REQUEST_START_LCE, 2, reportInterval,
2500            BOOL_TO_INT(pullMode));
2501    return Void();
2502}
2503
2504Return<void> RadioImpl::stopLceService(int32_t serial) {
2505#if VDBG
2506    RLOGD("stopLceService: serial %d", serial);
2507#endif
2508    dispatchVoid(serial, mSlotId, RIL_REQUEST_STOP_LCE);
2509    return Void();
2510}
2511
2512Return<void> RadioImpl::pullLceData(int32_t serial) {
2513#if VDBG
2514    RLOGD("pullLceData: serial %d", serial);
2515#endif
2516    dispatchVoid(serial, mSlotId, RIL_REQUEST_PULL_LCEDATA);
2517    return Void();
2518}
2519
2520Return<void> RadioImpl::getModemActivityInfo(int32_t serial) {
2521#if VDBG
2522    RLOGD("getModemActivityInfo: serial %d", serial);
2523#endif
2524    dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_ACTIVITY_INFO);
2525    return Void();
2526}
2527
2528Return<void> RadioImpl::setAllowedCarriers(int32_t serial, bool allAllowed,
2529                                           const CarrierRestrictions& carriers) {
2530#if VDBG
2531    RLOGD("setAllowedCarriers: serial %d", serial);
2532#endif
2533    RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
2534            RIL_REQUEST_SET_CARRIER_RESTRICTIONS);
2535    if (pRI == NULL) {
2536        return Void();
2537    }
2538
2539    RIL_CarrierRestrictions cr = {};
2540    RIL_Carrier *allowedCarriers = NULL;
2541    RIL_Carrier *excludedCarriers = NULL;
2542
2543    cr.len_allowed_carriers = carriers.allowedCarriers.size();
2544    allowedCarriers = (RIL_Carrier *)calloc(cr.len_allowed_carriers, sizeof(RIL_Carrier));
2545    if (allowedCarriers == NULL) {
2546        RLOGE("setAllowedCarriers: Memory allocation failed for request %s",
2547                requestToString(pRI->pCI->requestNumber));
2548        sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2549        return Void();
2550    }
2551    cr.allowed_carriers = allowedCarriers;
2552
2553    cr.len_excluded_carriers = carriers.excludedCarriers.size();
2554    excludedCarriers = (RIL_Carrier *)calloc(cr.len_excluded_carriers, sizeof(RIL_Carrier));
2555    if (excludedCarriers == NULL) {
2556        RLOGE("setAllowedCarriers: Memory allocation failed for request %s",
2557                requestToString(pRI->pCI->requestNumber));
2558        sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2559#ifdef MEMSET_FREED
2560        memset(allowedCarriers, 0, cr.len_allowed_carriers * sizeof(RIL_Carrier));
2561#endif
2562        free(allowedCarriers);
2563        return Void();
2564    }
2565    cr.excluded_carriers = excludedCarriers;
2566
2567    for (int i = 0; i < cr.len_allowed_carriers; i++) {
2568        allowedCarriers[i].mcc = carriers.allowedCarriers[i].mcc.c_str();
2569        allowedCarriers[i].mnc = carriers.allowedCarriers[i].mnc.c_str();
2570        allowedCarriers[i].match_type = (RIL_CarrierMatchType) carriers.allowedCarriers[i].matchType;
2571        allowedCarriers[i].match_data = carriers.allowedCarriers[i].matchData.c_str();
2572    }
2573
2574    for (int i = 0; i < cr.len_excluded_carriers; i++) {
2575        excludedCarriers[i].mcc = carriers.excludedCarriers[i].mcc.c_str();
2576        excludedCarriers[i].mnc = carriers.excludedCarriers[i].mnc.c_str();
2577        excludedCarriers[i].match_type =
2578                (RIL_CarrierMatchType) carriers.excludedCarriers[i].matchType;
2579        excludedCarriers[i].match_data = carriers.excludedCarriers[i].matchData.c_str();
2580    }
2581
2582    s_vendorFunctions->onRequest(pRI->pCI->requestNumber, &cr, sizeof(RIL_CarrierRestrictions), pRI);
2583
2584#ifdef MEMSET_FREED
2585    memset(allowedCarriers, 0, cr.len_allowed_carriers * sizeof(RIL_Carrier));
2586    memset(excludedCarriers, 0, cr.len_excluded_carriers * sizeof(RIL_Carrier));
2587#endif
2588    free(allowedCarriers);
2589    free(excludedCarriers);
2590    return Void();
2591}
2592
2593Return<void> RadioImpl::getAllowedCarriers(int32_t serial) {
2594#if VDBG
2595    RLOGD("getAllowedCarriers: serial %d", serial);
2596#endif
2597    dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_CARRIER_RESTRICTIONS);
2598    return Void();
2599}
2600
2601Return<void> RadioImpl::sendDeviceState(int32_t serial, DeviceStateType deviceStateType,
2602                                        bool state) {
2603#if VDBG
2604    RLOGD("sendDeviceState: serial %d", serial);
2605#endif
2606    if (s_vendorFunctions->version < 15) {
2607        if (deviceStateType ==  DeviceStateType::LOW_DATA_EXPECTED) {
2608            RLOGD("sendDeviceState: calling screen state %d", BOOL_TO_INT(!state));
2609            dispatchInts(serial, mSlotId, RIL_REQUEST_SCREEN_STATE, 1, BOOL_TO_INT(!state));
2610        } else {
2611            RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
2612                    RIL_REQUEST_SEND_DEVICE_STATE);
2613            sendErrorResponse(pRI, RIL_E_REQUEST_NOT_SUPPORTED);
2614        }
2615        return Void();
2616    }
2617    dispatchInts(serial, mSlotId, RIL_REQUEST_SEND_DEVICE_STATE, 2, (int) deviceStateType,
2618            BOOL_TO_INT(state));
2619    return Void();
2620}
2621
2622Return<void> RadioImpl::setIndicationFilter(int32_t serial, int32_t indicationFilter) {
2623#if VDBG
2624    RLOGD("setIndicationFilter: serial %d", serial);
2625#endif
2626    if (s_vendorFunctions->version < 15) {
2627        RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
2628                RIL_REQUEST_SET_UNSOLICITED_RESPONSE_FILTER);
2629        sendErrorResponse(pRI, RIL_E_REQUEST_NOT_SUPPORTED);
2630        return Void();
2631    }
2632    dispatchInts(serial, mSlotId, RIL_REQUEST_SET_UNSOLICITED_RESPONSE_FILTER, 1, indicationFilter);
2633    return Void();
2634}
2635
2636Return<void> RadioImpl::setSimCardPower(int32_t serial, bool powerUp) {
2637#if VDBG
2638    RLOGD("setSimCardPower: serial %d", serial);
2639#endif
2640    dispatchInts(serial, mSlotId, RIL_REQUEST_SET_SIM_CARD_POWER, 1, BOOL_TO_INT(powerUp));
2641    return Void();
2642}
2643
2644Return<void> RadioImpl::responseAcknowledgement() {
2645    android::releaseWakeLock();
2646    return Void();
2647}
2648
2649Return<void> OemHookImpl::setResponseFunctions(
2650        const ::android::sp<IOemHookResponse>& oemHookResponseParam,
2651        const ::android::sp<IOemHookIndication>& oemHookIndicationParam) {
2652#if VDBG
2653    RLOGD("OemHookImpl::setResponseFunctions");
2654#endif
2655
2656    pthread_rwlock_t *radioServiceRwlockPtr = radio::getRadioServiceRwlock(mSlotId);
2657    int ret = pthread_rwlock_wrlock(radioServiceRwlockPtr);
2658    assert(ret == 0);
2659
2660    mOemHookResponse = oemHookResponseParam;
2661    mOemHookIndication = oemHookIndicationParam;
2662    mCounterOemHook[mSlotId]++;
2663
2664    ret = pthread_rwlock_unlock(radioServiceRwlockPtr);
2665    assert(ret == 0);
2666
2667    return Void();
2668}
2669
2670Return<void> OemHookImpl::sendRequestRaw(int32_t serial, const hidl_vec<uint8_t>& data) {
2671#if VDBG
2672    RLOGD("OemHookImpl::sendRequestRaw: serial %d", serial);
2673#endif
2674    dispatchRaw(serial, mSlotId, RIL_REQUEST_OEM_HOOK_RAW, data);
2675    return Void();
2676}
2677
2678Return<void> OemHookImpl::sendRequestStrings(int32_t serial,
2679        const hidl_vec<hidl_string>& data) {
2680#if VDBG
2681    RLOGD("OemHookImpl::sendRequestStrings: serial %d", serial);
2682#endif
2683    dispatchStrings(serial, mSlotId, RIL_REQUEST_OEM_HOOK_STRINGS, data);
2684    return Void();
2685}
2686
2687Return<void> RadioImpl::setCarrierInfoForImsiEncryption(int32_t serial,
2688        const ::android::hardware::hidl_vec<uint8_t>& carrierKey,
2689        const hidl_string& keyIdentifier) {
2690    RLOGD("setCarrierInfoForImsiEncryption: serial %d", serial);
2691    dispatchRaw(serial, mSlotId, RIL_REQUEST_SET_CARRIER_INFO_IMSI_ENCRYPTION, carrierKey);
2692    return Void();
2693}
2694
2695/***************************************************************************************************
2696 * RESPONSE FUNCTIONS
2697 * Functions above are used for requests going from framework to vendor code. The ones below are
2698 * responses for those requests coming back from the vendor code.
2699 **************************************************************************************************/
2700
2701void radio::acknowledgeRequest(int slotId, int serial) {
2702    if (radioService[slotId]->mRadioResponse != NULL) {
2703        Return<void> retStatus = radioService[slotId]->mRadioResponse->acknowledgeRequest(serial);
2704        radioService[slotId]->checkReturnStatus(retStatus);
2705    } else {
2706        RLOGE("acknowledgeRequest: radioService[%d]->mRadioResponse == NULL", slotId);
2707    }
2708}
2709
2710void populateResponseInfo(RadioResponseInfo& responseInfo, int serial, int responseType,
2711                         RIL_Errno e) {
2712    responseInfo.serial = serial;
2713    switch (responseType) {
2714        case RESPONSE_SOLICITED:
2715            responseInfo.type = RadioResponseType::SOLICITED;
2716            break;
2717        case RESPONSE_SOLICITED_ACK_EXP:
2718            responseInfo.type = RadioResponseType::SOLICITED_ACK_EXP;
2719            break;
2720    }
2721    responseInfo.error = (RadioError) e;
2722}
2723
2724int responseIntOrEmpty(RadioResponseInfo& responseInfo, int serial, int responseType, RIL_Errno e,
2725               void *response, size_t responseLen) {
2726    populateResponseInfo(responseInfo, serial, responseType, e);
2727    int ret = -1;
2728
2729    if (response == NULL && responseLen == 0) {
2730        // Earlier RILs did not send a response for some cases although the interface
2731        // expected an integer as response. Do not return error if response is empty. Instead
2732        // Return -1 in those cases to maintain backward compatibility.
2733    } else if (response == NULL || responseLen != sizeof(int)) {
2734        RLOGE("responseIntOrEmpty: Invalid response");
2735        if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
2736    } else {
2737        int *p_int = (int *) response;
2738        ret = p_int[0];
2739    }
2740    return ret;
2741}
2742
2743int responseInt(RadioResponseInfo& responseInfo, int serial, int responseType, RIL_Errno e,
2744               void *response, size_t responseLen) {
2745    populateResponseInfo(responseInfo, serial, responseType, e);
2746    int ret = -1;
2747
2748    if (response == NULL || responseLen != sizeof(int)) {
2749        RLOGE("responseInt: Invalid response");
2750        if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
2751    } else {
2752        int *p_int = (int *) response;
2753        ret = p_int[0];
2754    }
2755    return ret;
2756}
2757
2758int radio::getIccCardStatusResponse(int slotId,
2759                                   int responseType, int serial, RIL_Errno e,
2760                                   void *response, size_t responseLen) {
2761    if (radioService[slotId]->mRadioResponse != NULL) {
2762        RadioResponseInfo responseInfo = {};
2763        populateResponseInfo(responseInfo, serial, responseType, e);
2764        CardStatus cardStatus = {};
2765        if (response == NULL || responseLen != sizeof(RIL_CardStatus_v6)) {
2766            RLOGE("getIccCardStatusResponse: Invalid response");
2767            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
2768        } else {
2769            RIL_CardStatus_v6 *p_cur = ((RIL_CardStatus_v6 *) response);
2770            cardStatus.cardState = (CardState) p_cur->card_state;
2771            cardStatus.universalPinState = (PinState) p_cur->universal_pin_state;
2772            cardStatus.gsmUmtsSubscriptionAppIndex = p_cur->gsm_umts_subscription_app_index;
2773            cardStatus.cdmaSubscriptionAppIndex = p_cur->cdma_subscription_app_index;
2774            cardStatus.imsSubscriptionAppIndex = p_cur->ims_subscription_app_index;
2775
2776            RIL_AppStatus *rilAppStatus = p_cur->applications;
2777            cardStatus.applications.resize(p_cur->num_applications);
2778            AppStatus *appStatus = cardStatus.applications.data();
2779#if VDBG
2780            RLOGD("getIccCardStatusResponse: num_applications %d", p_cur->num_applications);
2781#endif
2782            for (int i = 0; i < p_cur->num_applications; i++) {
2783                appStatus[i].appType = (AppType) rilAppStatus[i].app_type;
2784                appStatus[i].appState = (AppState) rilAppStatus[i].app_state;
2785                appStatus[i].persoSubstate = (PersoSubstate) rilAppStatus[i].perso_substate;
2786                appStatus[i].aidPtr = convertCharPtrToHidlString(rilAppStatus[i].aid_ptr);
2787                appStatus[i].appLabelPtr = convertCharPtrToHidlString(
2788                        rilAppStatus[i].app_label_ptr);
2789                appStatus[i].pin1Replaced = rilAppStatus[i].pin1_replaced;
2790                appStatus[i].pin1 = (PinState) rilAppStatus[i].pin1;
2791                appStatus[i].pin2 = (PinState) rilAppStatus[i].pin2;
2792            }
2793        }
2794
2795        Return<void> retStatus = radioService[slotId]->mRadioResponse->
2796                getIccCardStatusResponse(responseInfo, cardStatus);
2797        radioService[slotId]->checkReturnStatus(retStatus);
2798    } else {
2799        RLOGE("getIccCardStatusResponse: radioService[%d]->mRadioResponse == NULL", slotId);
2800    }
2801
2802    return 0;
2803}
2804
2805int radio::supplyIccPinForAppResponse(int slotId,
2806                                     int responseType, int serial, RIL_Errno e,
2807                                     void *response, size_t responseLen) {
2808#if VDBG
2809    RLOGD("supplyIccPinForAppResponse: serial %d", serial);
2810#endif
2811
2812    if (radioService[slotId]->mRadioResponse != NULL) {
2813        RadioResponseInfo responseInfo = {};
2814        int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2815        Return<void> retStatus = radioService[slotId]->mRadioResponse->
2816                supplyIccPinForAppResponse(responseInfo, ret);
2817        radioService[slotId]->checkReturnStatus(retStatus);
2818    } else {
2819        RLOGE("supplyIccPinForAppResponse: radioService[%d]->mRadioResponse == NULL",
2820                slotId);
2821    }
2822
2823    return 0;
2824}
2825
2826int radio::supplyIccPukForAppResponse(int slotId,
2827                                     int responseType, int serial, RIL_Errno e,
2828                                     void *response, size_t responseLen) {
2829#if VDBG
2830    RLOGD("supplyIccPukForAppResponse: serial %d", serial);
2831#endif
2832
2833    if (radioService[slotId]->mRadioResponse != NULL) {
2834        RadioResponseInfo responseInfo = {};
2835        int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2836        Return<void> retStatus = radioService[slotId]->mRadioResponse->supplyIccPukForAppResponse(
2837                responseInfo, ret);
2838        radioService[slotId]->checkReturnStatus(retStatus);
2839    } else {
2840        RLOGE("supplyIccPukForAppResponse: radioService[%d]->mRadioResponse == NULL",
2841                slotId);
2842    }
2843
2844    return 0;
2845}
2846
2847int radio::supplyIccPin2ForAppResponse(int slotId,
2848                                      int responseType, int serial, RIL_Errno e,
2849                                      void *response, size_t responseLen) {
2850#if VDBG
2851    RLOGD("supplyIccPin2ForAppResponse: serial %d", serial);
2852#endif
2853
2854    if (radioService[slotId]->mRadioResponse != NULL) {
2855        RadioResponseInfo responseInfo = {};
2856        int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2857        Return<void> retStatus = radioService[slotId]->mRadioResponse->
2858                supplyIccPin2ForAppResponse(responseInfo, ret);
2859        radioService[slotId]->checkReturnStatus(retStatus);
2860    } else {
2861        RLOGE("supplyIccPin2ForAppResponse: radioService[%d]->mRadioResponse == NULL",
2862                slotId);
2863    }
2864
2865    return 0;
2866}
2867
2868int radio::supplyIccPuk2ForAppResponse(int slotId,
2869                                      int responseType, int serial, RIL_Errno e,
2870                                      void *response, size_t responseLen) {
2871#if VDBG
2872    RLOGD("supplyIccPuk2ForAppResponse: serial %d", serial);
2873#endif
2874
2875    if (radioService[slotId]->mRadioResponse != NULL) {
2876        RadioResponseInfo responseInfo = {};
2877        int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2878        Return<void> retStatus = radioService[slotId]->mRadioResponse->
2879                supplyIccPuk2ForAppResponse(responseInfo, ret);
2880        radioService[slotId]->checkReturnStatus(retStatus);
2881    } else {
2882        RLOGE("supplyIccPuk2ForAppResponse: radioService[%d]->mRadioResponse == NULL",
2883                slotId);
2884    }
2885
2886    return 0;
2887}
2888
2889int radio::changeIccPinForAppResponse(int slotId,
2890                                     int responseType, int serial, RIL_Errno e,
2891                                     void *response, size_t responseLen) {
2892#if VDBG
2893    RLOGD("changeIccPinForAppResponse: serial %d", serial);
2894#endif
2895
2896    if (radioService[slotId]->mRadioResponse != NULL) {
2897        RadioResponseInfo responseInfo = {};
2898        int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2899        Return<void> retStatus = radioService[slotId]->mRadioResponse->
2900                changeIccPinForAppResponse(responseInfo, ret);
2901        radioService[slotId]->checkReturnStatus(retStatus);
2902    } else {
2903        RLOGE("changeIccPinForAppResponse: radioService[%d]->mRadioResponse == NULL",
2904                slotId);
2905    }
2906
2907    return 0;
2908}
2909
2910int radio::changeIccPin2ForAppResponse(int slotId,
2911                                      int responseType, int serial, RIL_Errno e,
2912                                      void *response, size_t responseLen) {
2913#if VDBG
2914    RLOGD("changeIccPin2ForAppResponse: serial %d", serial);
2915#endif
2916
2917    if (radioService[slotId]->mRadioResponse != NULL) {
2918        RadioResponseInfo responseInfo = {};
2919        int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2920        Return<void> retStatus = radioService[slotId]->mRadioResponse->
2921                changeIccPin2ForAppResponse(responseInfo, ret);
2922        radioService[slotId]->checkReturnStatus(retStatus);
2923    } else {
2924        RLOGE("changeIccPin2ForAppResponse: radioService[%d]->mRadioResponse == NULL",
2925                slotId);
2926    }
2927
2928    return 0;
2929}
2930
2931int radio::supplyNetworkDepersonalizationResponse(int slotId,
2932                                                 int responseType, int serial, RIL_Errno e,
2933                                                 void *response, size_t responseLen) {
2934#if VDBG
2935    RLOGD("supplyNetworkDepersonalizationResponse: serial %d", serial);
2936#endif
2937
2938    if (radioService[slotId]->mRadioResponse != NULL) {
2939        RadioResponseInfo responseInfo = {};
2940        int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2941        Return<void> retStatus = radioService[slotId]->mRadioResponse->
2942                supplyNetworkDepersonalizationResponse(responseInfo, ret);
2943        radioService[slotId]->checkReturnStatus(retStatus);
2944    } else {
2945        RLOGE("supplyNetworkDepersonalizationResponse: radioService[%d]->mRadioResponse == "
2946                "NULL", slotId);
2947    }
2948
2949    return 0;
2950}
2951
2952int radio::getCurrentCallsResponse(int slotId,
2953                                  int responseType, int serial, RIL_Errno e,
2954                                  void *response, size_t responseLen) {
2955#if VDBG
2956    RLOGD("getCurrentCallsResponse: serial %d", serial);
2957#endif
2958
2959    if (radioService[slotId]->mRadioResponse != NULL) {
2960        RadioResponseInfo responseInfo = {};
2961        populateResponseInfo(responseInfo, serial, responseType, e);
2962
2963        hidl_vec<Call> calls;
2964        if (response == NULL || (responseLen % sizeof(RIL_Call *)) != 0) {
2965            RLOGE("getCurrentCallsResponse: Invalid response");
2966            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
2967        } else {
2968            int num = responseLen / sizeof(RIL_Call *);
2969            calls.resize(num);
2970
2971            for (int i = 0 ; i < num ; i++) {
2972                RIL_Call *p_cur = ((RIL_Call **) response)[i];
2973                /* each call info */
2974                calls[i].state = (CallState) p_cur->state;
2975                calls[i].index = p_cur->index;
2976                calls[i].toa = p_cur->toa;
2977                calls[i].isMpty = p_cur->isMpty;
2978                calls[i].isMT = p_cur->isMT;
2979                calls[i].als = p_cur->als;
2980                calls[i].isVoice = p_cur->isVoice;
2981                calls[i].isVoicePrivacy = p_cur->isVoicePrivacy;
2982                calls[i].number = convertCharPtrToHidlString(p_cur->number);
2983                calls[i].numberPresentation = (CallPresentation) p_cur->numberPresentation;
2984                calls[i].name = convertCharPtrToHidlString(p_cur->name);
2985                calls[i].namePresentation = (CallPresentation) p_cur->namePresentation;
2986                if (p_cur->uusInfo != NULL && p_cur->uusInfo->uusData != NULL) {
2987                    RIL_UUS_Info *uusInfo = p_cur->uusInfo;
2988                    calls[i].uusInfo[0].uusType = (UusType) uusInfo->uusType;
2989                    calls[i].uusInfo[0].uusDcs = (UusDcs) uusInfo->uusDcs;
2990                    // convert uusInfo->uusData to a null-terminated string
2991                    char *nullTermStr = strndup(uusInfo->uusData, uusInfo->uusLength);
2992                    calls[i].uusInfo[0].uusData = nullTermStr;
2993                    free(nullTermStr);
2994                }
2995            }
2996        }
2997
2998        Return<void> retStatus = radioService[slotId]->mRadioResponse->
2999                getCurrentCallsResponse(responseInfo, calls);
3000        radioService[slotId]->checkReturnStatus(retStatus);
3001    } else {
3002        RLOGE("getCurrentCallsResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3003    }
3004
3005    return 0;
3006}
3007
3008int radio::dialResponse(int slotId,
3009                       int responseType, int serial, RIL_Errno e, void *response,
3010                       size_t responseLen) {
3011#if VDBG
3012    RLOGD("dialResponse: serial %d", serial);
3013#endif
3014
3015    if (radioService[slotId]->mRadioResponse != NULL) {
3016        RadioResponseInfo responseInfo = {};
3017        populateResponseInfo(responseInfo, serial, responseType, e);
3018        Return<void> retStatus = radioService[slotId]->mRadioResponse->dialResponse(responseInfo);
3019        radioService[slotId]->checkReturnStatus(retStatus);
3020    } else {
3021        RLOGE("dialResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3022    }
3023
3024    return 0;
3025}
3026
3027int radio::getIMSIForAppResponse(int slotId,
3028                                int responseType, int serial, RIL_Errno e, void *response,
3029                                size_t responseLen) {
3030#if VDBG
3031    RLOGD("getIMSIForAppResponse: serial %d", serial);
3032#endif
3033
3034    if (radioService[slotId]->mRadioResponse != NULL) {
3035        RadioResponseInfo responseInfo = {};
3036        populateResponseInfo(responseInfo, serial, responseType, e);
3037        Return<void> retStatus = radioService[slotId]->mRadioResponse->getIMSIForAppResponse(
3038                responseInfo, convertCharPtrToHidlString((char *) response));
3039        radioService[slotId]->checkReturnStatus(retStatus);
3040    } else {
3041        RLOGE("getIMSIForAppResponse: radioService[%d]->mRadioResponse == NULL",
3042                slotId);
3043    }
3044
3045    return 0;
3046}
3047
3048int radio::hangupConnectionResponse(int slotId,
3049                                   int responseType, int serial, RIL_Errno e,
3050                                   void *response, size_t responseLen) {
3051#if VDBG
3052    RLOGD("hangupConnectionResponse: serial %d", serial);
3053#endif
3054
3055    if (radioService[slotId]->mRadioResponse != NULL) {
3056        RadioResponseInfo responseInfo = {};
3057        populateResponseInfo(responseInfo, serial, responseType, e);
3058        Return<void> retStatus = radioService[slotId]->mRadioResponse->hangupConnectionResponse(
3059                responseInfo);
3060        radioService[slotId]->checkReturnStatus(retStatus);
3061    } else {
3062        RLOGE("hangupConnectionResponse: radioService[%d]->mRadioResponse == NULL",
3063                slotId);
3064    }
3065
3066    return 0;
3067}
3068
3069int radio::hangupWaitingOrBackgroundResponse(int slotId,
3070                                            int responseType, int serial, RIL_Errno e,
3071                                            void *response, size_t responseLen) {
3072#if VDBG
3073    RLOGD("hangupWaitingOrBackgroundResponse: serial %d", serial);
3074#endif
3075
3076    if (radioService[slotId]->mRadioResponse != NULL) {
3077        RadioResponseInfo responseInfo = {};
3078        populateResponseInfo(responseInfo, serial, responseType, e);
3079        Return<void> retStatus =
3080                radioService[slotId]->mRadioResponse->hangupWaitingOrBackgroundResponse(
3081                responseInfo);
3082        radioService[slotId]->checkReturnStatus(retStatus);
3083    } else {
3084        RLOGE("hangupWaitingOrBackgroundResponse: radioService[%d]->mRadioResponse == NULL",
3085                slotId);
3086    }
3087
3088    return 0;
3089}
3090
3091int radio::hangupForegroundResumeBackgroundResponse(int slotId, int responseType, int serial,
3092                                                    RIL_Errno e, void *response,
3093                                                    size_t responseLen) {
3094#if VDBG
3095    RLOGD("hangupWaitingOrBackgroundResponse: serial %d", serial);
3096#endif
3097
3098    if (radioService[slotId]->mRadioResponse != NULL) {
3099        RadioResponseInfo responseInfo = {};
3100        populateResponseInfo(responseInfo, serial, responseType, e);
3101        Return<void> retStatus =
3102                radioService[slotId]->mRadioResponse->hangupWaitingOrBackgroundResponse(
3103                responseInfo);
3104        radioService[slotId]->checkReturnStatus(retStatus);
3105    } else {
3106        RLOGE("hangupWaitingOrBackgroundResponse: radioService[%d]->mRadioResponse == NULL",
3107                slotId);
3108    }
3109
3110    return 0;
3111}
3112
3113int radio::switchWaitingOrHoldingAndActiveResponse(int slotId, int responseType, int serial,
3114                                                   RIL_Errno e, void *response,
3115                                                   size_t responseLen) {
3116#if VDBG
3117    RLOGD("switchWaitingOrHoldingAndActiveResponse: serial %d", serial);
3118#endif
3119
3120    if (radioService[slotId]->mRadioResponse != NULL) {
3121        RadioResponseInfo responseInfo = {};
3122        populateResponseInfo(responseInfo, serial, responseType, e);
3123        Return<void> retStatus =
3124                radioService[slotId]->mRadioResponse->switchWaitingOrHoldingAndActiveResponse(
3125                responseInfo);
3126        radioService[slotId]->checkReturnStatus(retStatus);
3127    } else {
3128        RLOGE("switchWaitingOrHoldingAndActiveResponse: radioService[%d]->mRadioResponse "
3129                "== NULL", slotId);
3130    }
3131
3132    return 0;
3133}
3134
3135int radio::conferenceResponse(int slotId, int responseType,
3136                             int serial, RIL_Errno e, void *response, size_t responseLen) {
3137#if VDBG
3138    RLOGD("conferenceResponse: serial %d", serial);
3139#endif
3140
3141    if (radioService[slotId]->mRadioResponse != NULL) {
3142        RadioResponseInfo responseInfo = {};
3143        populateResponseInfo(responseInfo, serial, responseType, e);
3144        Return<void> retStatus = radioService[slotId]->mRadioResponse->conferenceResponse(
3145                responseInfo);
3146        radioService[slotId]->checkReturnStatus(retStatus);
3147    } else {
3148        RLOGE("conferenceResponse: radioService[%d]->mRadioResponse == NULL",
3149                slotId);
3150    }
3151
3152    return 0;
3153}
3154
3155int radio::rejectCallResponse(int slotId, int responseType,
3156                             int serial, RIL_Errno e, void *response, size_t responseLen) {
3157#if VDBG
3158    RLOGD("rejectCallResponse: serial %d", serial);
3159#endif
3160
3161    if (radioService[slotId]->mRadioResponse != NULL) {
3162        RadioResponseInfo responseInfo = {};
3163        populateResponseInfo(responseInfo, serial, responseType, e);
3164        Return<void> retStatus = radioService[slotId]->mRadioResponse->rejectCallResponse(
3165                responseInfo);
3166        radioService[slotId]->checkReturnStatus(retStatus);
3167    } else {
3168        RLOGE("rejectCallResponse: radioService[%d]->mRadioResponse == NULL",
3169                slotId);
3170    }
3171
3172    return 0;
3173}
3174
3175int radio::getLastCallFailCauseResponse(int slotId,
3176                                       int responseType, int serial, RIL_Errno e, void *response,
3177                                       size_t responseLen) {
3178#if VDBG
3179    RLOGD("getLastCallFailCauseResponse: serial %d", serial);
3180#endif
3181
3182    if (radioService[slotId]->mRadioResponse != NULL) {
3183        RadioResponseInfo responseInfo = {};
3184        populateResponseInfo(responseInfo, serial, responseType, e);
3185
3186        LastCallFailCauseInfo info = {};
3187        info.vendorCause = hidl_string();
3188        if (response == NULL) {
3189            RLOGE("getCurrentCallsResponse Invalid response: NULL");
3190            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3191        } else if (responseLen == sizeof(int)) {
3192            int *pInt = (int *) response;
3193            info.causeCode = (LastCallFailCause) pInt[0];
3194        } else if (responseLen == sizeof(RIL_LastCallFailCauseInfo))  {
3195            RIL_LastCallFailCauseInfo *pFailCauseInfo = (RIL_LastCallFailCauseInfo *) response;
3196            info.causeCode = (LastCallFailCause) pFailCauseInfo->cause_code;
3197            info.vendorCause = convertCharPtrToHidlString(pFailCauseInfo->vendor_cause);
3198        } else {
3199            RLOGE("getCurrentCallsResponse Invalid response: NULL");
3200            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3201        }
3202
3203        Return<void> retStatus = radioService[slotId]->mRadioResponse->getLastCallFailCauseResponse(
3204                responseInfo, info);
3205        radioService[slotId]->checkReturnStatus(retStatus);
3206    } else {
3207        RLOGE("getLastCallFailCauseResponse: radioService[%d]->mRadioResponse == NULL",
3208                slotId);
3209    }
3210
3211    return 0;
3212}
3213
3214int radio::getSignalStrengthResponse(int slotId,
3215                                     int responseType, int serial, RIL_Errno e,
3216                                     void *response, size_t responseLen) {
3217#if VDBG
3218    RLOGD("getSignalStrengthResponse: serial %d", serial);
3219#endif
3220
3221    if (radioService[slotId]->mRadioResponse != NULL) {
3222        RadioResponseInfo responseInfo = {};
3223        populateResponseInfo(responseInfo, serial, responseType, e);
3224        SignalStrength signalStrength = {};
3225        if (response == NULL || responseLen != sizeof(RIL_SignalStrength_v10)) {
3226            RLOGE("getSignalStrengthResponse: Invalid response");
3227            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3228        } else {
3229            convertRilSignalStrengthToHal(response, responseLen, signalStrength);
3230        }
3231
3232        Return<void> retStatus = radioService[slotId]->mRadioResponse->getSignalStrengthResponse(
3233                responseInfo, signalStrength);
3234        radioService[slotId]->checkReturnStatus(retStatus);
3235    } else {
3236        RLOGE("getSignalStrengthResponse: radioService[%d]->mRadioResponse == NULL",
3237                slotId);
3238    }
3239
3240    return 0;
3241}
3242
3243RIL_CellInfoType getCellInfoTypeRadioTechnology(char *rat) {
3244    if (rat == NULL) {
3245        return RIL_CELL_INFO_TYPE_NONE;
3246    }
3247
3248    int radioTech = atoi(rat);
3249
3250    switch(radioTech) {
3251
3252        case RADIO_TECH_GPRS:
3253        case RADIO_TECH_EDGE:
3254        case RADIO_TECH_GSM: {
3255            return RIL_CELL_INFO_TYPE_GSM;
3256        }
3257
3258        case RADIO_TECH_UMTS:
3259        case RADIO_TECH_HSDPA:
3260        case RADIO_TECH_HSUPA:
3261        case RADIO_TECH_HSPA:
3262        case RADIO_TECH_HSPAP: {
3263            return RIL_CELL_INFO_TYPE_WCDMA;
3264        }
3265
3266        case RADIO_TECH_IS95A:
3267        case RADIO_TECH_IS95B:
3268        case RADIO_TECH_1xRTT:
3269        case RADIO_TECH_EVDO_0:
3270        case RADIO_TECH_EVDO_A:
3271        case RADIO_TECH_EVDO_B:
3272        case RADIO_TECH_EHRPD: {
3273            return RIL_CELL_INFO_TYPE_CDMA;
3274        }
3275
3276        case RADIO_TECH_LTE:
3277        case RADIO_TECH_LTE_CA: {
3278            return RIL_CELL_INFO_TYPE_LTE;
3279        }
3280
3281        case RADIO_TECH_TD_SCDMA: {
3282            return RIL_CELL_INFO_TYPE_TD_SCDMA;
3283        }
3284
3285        default: {
3286            break;
3287        }
3288    }
3289
3290    return RIL_CELL_INFO_TYPE_NONE;
3291
3292}
3293
3294void fillCellIdentityResponse(CellIdentity &cellIdentity, RIL_CellIdentity_v16 &rilCellIdentity) {
3295
3296    cellIdentity.cellIdentityGsm.resize(0);
3297    cellIdentity.cellIdentityWcdma.resize(0);
3298    cellIdentity.cellIdentityCdma.resize(0);
3299    cellIdentity.cellIdentityTdscdma.resize(0);
3300    cellIdentity.cellIdentityLte.resize(0);
3301    cellIdentity.cellInfoType = (CellInfoType)rilCellIdentity.cellInfoType;
3302    switch(rilCellIdentity.cellInfoType) {
3303
3304        case RIL_CELL_INFO_TYPE_GSM: {
3305            cellIdentity.cellIdentityGsm.resize(1);
3306            cellIdentity.cellIdentityGsm[0].mcc =
3307                    std::to_string(rilCellIdentity.cellIdentityGsm.mcc);
3308            cellIdentity.cellIdentityGsm[0].mnc =
3309                    std::to_string(rilCellIdentity.cellIdentityGsm.mnc);
3310            cellIdentity.cellIdentityGsm[0].lac = rilCellIdentity.cellIdentityGsm.lac;
3311            cellIdentity.cellIdentityGsm[0].cid = rilCellIdentity.cellIdentityGsm.cid;
3312            cellIdentity.cellIdentityGsm[0].arfcn = rilCellIdentity.cellIdentityGsm.arfcn;
3313            cellIdentity.cellIdentityGsm[0].bsic = rilCellIdentity.cellIdentityGsm.bsic;
3314            break;
3315        }
3316
3317        case RIL_CELL_INFO_TYPE_WCDMA: {
3318            cellIdentity.cellIdentityWcdma.resize(1);
3319            cellIdentity.cellIdentityWcdma[0].mcc =
3320                    std::to_string(rilCellIdentity.cellIdentityWcdma.mcc);
3321            cellIdentity.cellIdentityWcdma[0].mnc =
3322                    std::to_string(rilCellIdentity.cellIdentityWcdma.mnc);
3323            cellIdentity.cellIdentityWcdma[0].lac = rilCellIdentity.cellIdentityWcdma.lac;
3324            cellIdentity.cellIdentityWcdma[0].cid = rilCellIdentity.cellIdentityWcdma.cid;
3325            cellIdentity.cellIdentityWcdma[0].psc = rilCellIdentity.cellIdentityWcdma.psc;
3326            cellIdentity.cellIdentityWcdma[0].uarfcn = rilCellIdentity.cellIdentityWcdma.uarfcn;
3327            break;
3328        }
3329
3330        case RIL_CELL_INFO_TYPE_CDMA: {
3331            cellIdentity.cellIdentityCdma.resize(1);
3332            cellIdentity.cellIdentityCdma[0].networkId = rilCellIdentity.cellIdentityCdma.networkId;
3333            cellIdentity.cellIdentityCdma[0].systemId = rilCellIdentity.cellIdentityCdma.systemId;
3334            cellIdentity.cellIdentityCdma[0].baseStationId =
3335                    rilCellIdentity.cellIdentityCdma.basestationId;
3336            cellIdentity.cellIdentityCdma[0].longitude = rilCellIdentity.cellIdentityCdma.longitude;
3337            cellIdentity.cellIdentityCdma[0].latitude = rilCellIdentity.cellIdentityCdma.latitude;
3338            break;
3339        }
3340
3341        case RIL_CELL_INFO_TYPE_LTE: {
3342            cellIdentity.cellIdentityLte.resize(1);
3343            cellIdentity.cellIdentityLte[0].mcc =
3344                    std::to_string(rilCellIdentity.cellIdentityLte.mcc);
3345            cellIdentity.cellIdentityLte[0].mnc =
3346                    std::to_string(rilCellIdentity.cellIdentityLte.mnc);
3347            cellIdentity.cellIdentityLte[0].ci = rilCellIdentity.cellIdentityLte.ci;
3348            cellIdentity.cellIdentityLte[0].pci = rilCellIdentity.cellIdentityLte.pci;
3349            cellIdentity.cellIdentityLte[0].tac = rilCellIdentity.cellIdentityLte.tac;
3350            cellIdentity.cellIdentityLte[0].earfcn = rilCellIdentity.cellIdentityLte.earfcn;
3351            break;
3352        }
3353
3354        case RIL_CELL_INFO_TYPE_TD_SCDMA: {
3355            cellIdentity.cellIdentityTdscdma.resize(1);
3356            cellIdentity.cellIdentityTdscdma[0].mcc =
3357                    std::to_string(rilCellIdentity.cellIdentityTdscdma.mcc);
3358            cellIdentity.cellIdentityTdscdma[0].mnc =
3359                    std::to_string(rilCellIdentity.cellIdentityTdscdma.mnc);
3360            cellIdentity.cellIdentityTdscdma[0].lac = rilCellIdentity.cellIdentityTdscdma.lac;
3361            cellIdentity.cellIdentityTdscdma[0].cid = rilCellIdentity.cellIdentityTdscdma.cid;
3362            cellIdentity.cellIdentityTdscdma[0].cpid = rilCellIdentity.cellIdentityTdscdma.cpid;
3363            break;
3364        }
3365
3366        default: {
3367            break;
3368        }
3369    }
3370}
3371
3372int convertResponseStringEntryToInt(char **response, int index, int numStrings) {
3373    if ((response != NULL) &&  (numStrings > index) && (response[index] != NULL)) {
3374        return atoi(response[index]);
3375    }
3376
3377    return -1;
3378}
3379
3380void fillCellIdentityFromVoiceRegStateResponseString(CellIdentity &cellIdentity,
3381        int numStrings, char** response) {
3382
3383    RIL_CellIdentity_v16 rilCellIdentity;
3384    memset(&rilCellIdentity, -1, sizeof(RIL_CellIdentity_v16));
3385
3386    rilCellIdentity.cellInfoType = getCellInfoTypeRadioTechnology(response[3]);
3387    switch(rilCellIdentity.cellInfoType) {
3388
3389        case RIL_CELL_INFO_TYPE_GSM: {
3390            rilCellIdentity.cellIdentityGsm.lac =
3391                    convertResponseStringEntryToInt(response, 1, numStrings);
3392            rilCellIdentity.cellIdentityGsm.cid =
3393                    convertResponseStringEntryToInt(response, 2, numStrings);
3394            break;
3395        }
3396
3397        case RIL_CELL_INFO_TYPE_WCDMA: {
3398            rilCellIdentity.cellIdentityWcdma.lac =
3399                    convertResponseStringEntryToInt(response, 1, numStrings);
3400            rilCellIdentity.cellIdentityWcdma.cid =
3401                    convertResponseStringEntryToInt(response, 2, numStrings);
3402            rilCellIdentity.cellIdentityWcdma.psc =
3403                    convertResponseStringEntryToInt(response, 14, numStrings);
3404            break;
3405        }
3406
3407        case RIL_CELL_INFO_TYPE_TD_SCDMA:{
3408            rilCellIdentity.cellIdentityTdscdma.lac =
3409                    convertResponseStringEntryToInt(response, 1, numStrings);
3410            rilCellIdentity.cellIdentityTdscdma.cid =
3411                    convertResponseStringEntryToInt(response, 2, numStrings);
3412            break;
3413        }
3414
3415        case RIL_CELL_INFO_TYPE_CDMA:{
3416            rilCellIdentity.cellIdentityCdma.basestationId =
3417                    convertResponseStringEntryToInt(response, 4, numStrings);
3418            rilCellIdentity.cellIdentityCdma.longitude =
3419                    convertResponseStringEntryToInt(response, 5, numStrings);
3420            rilCellIdentity.cellIdentityCdma.latitude =
3421                    convertResponseStringEntryToInt(response, 6, numStrings);
3422            rilCellIdentity.cellIdentityCdma.systemId =
3423                    convertResponseStringEntryToInt(response, 8, numStrings);
3424            rilCellIdentity.cellIdentityCdma.networkId =
3425                    convertResponseStringEntryToInt(response, 9, numStrings);
3426            break;
3427        }
3428
3429        case RIL_CELL_INFO_TYPE_LTE:{
3430            rilCellIdentity.cellIdentityLte.tac =
3431                    convertResponseStringEntryToInt(response, 1, numStrings);
3432            rilCellIdentity.cellIdentityLte.ci =
3433                    convertResponseStringEntryToInt(response, 2, numStrings);
3434            break;
3435        }
3436
3437        default: {
3438            break;
3439        }
3440    }
3441
3442    fillCellIdentityResponse(cellIdentity, rilCellIdentity);
3443}
3444
3445void fillCellIdentityFromDataRegStateResponseString(CellIdentity &cellIdentity,
3446        int numStrings, char** response) {
3447
3448    RIL_CellIdentity_v16 rilCellIdentity;
3449    memset(&rilCellIdentity, -1, sizeof(RIL_CellIdentity_v16));
3450
3451    rilCellIdentity.cellInfoType = getCellInfoTypeRadioTechnology(response[3]);
3452    switch(rilCellIdentity.cellInfoType) {
3453        case RIL_CELL_INFO_TYPE_GSM: {
3454            rilCellIdentity.cellIdentityGsm.lac =
3455                    convertResponseStringEntryToInt(response, 1, numStrings);
3456            rilCellIdentity.cellIdentityGsm.cid =
3457                    convertResponseStringEntryToInt(response, 2, numStrings);
3458            break;
3459        }
3460        case RIL_CELL_INFO_TYPE_WCDMA: {
3461            rilCellIdentity.cellIdentityWcdma.lac =
3462                    convertResponseStringEntryToInt(response, 1, numStrings);
3463            rilCellIdentity.cellIdentityWcdma.cid =
3464                    convertResponseStringEntryToInt(response, 2, numStrings);
3465            break;
3466        }
3467        case RIL_CELL_INFO_TYPE_TD_SCDMA:{
3468            rilCellIdentity.cellIdentityTdscdma.lac =
3469                    convertResponseStringEntryToInt(response, 1, numStrings);
3470            rilCellIdentity.cellIdentityTdscdma.cid =
3471                    convertResponseStringEntryToInt(response, 2, numStrings);
3472            break;
3473        }
3474        case RIL_CELL_INFO_TYPE_LTE: {
3475            rilCellIdentity.cellIdentityLte.tac =
3476                    convertResponseStringEntryToInt(response, 6, numStrings);
3477            rilCellIdentity.cellIdentityLte.pci =
3478                    convertResponseStringEntryToInt(response, 7, numStrings);
3479            rilCellIdentity.cellIdentityLte.ci =
3480                    convertResponseStringEntryToInt(response, 8, numStrings);
3481            break;
3482        }
3483        default: {
3484            break;
3485        }
3486    }
3487
3488    fillCellIdentityResponse(cellIdentity, rilCellIdentity);
3489}
3490
3491int radio::getVoiceRegistrationStateResponse(int slotId,
3492                                            int responseType, int serial, RIL_Errno e,
3493                                            void *response, size_t responseLen) {
3494#if VDBG
3495    RLOGD("getVoiceRegistrationStateResponse: serial %d", serial);
3496#endif
3497
3498    if (radioService[slotId]->mRadioResponse != NULL) {
3499        RadioResponseInfo responseInfo = {};
3500        populateResponseInfo(responseInfo, serial, responseType, e);
3501
3502        VoiceRegStateResult voiceRegResponse = {};
3503        int numStrings = responseLen / sizeof(char *);
3504        if (response == NULL) {
3505               RLOGE("getVoiceRegistrationStateResponse Invalid response: NULL");
3506               if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3507        } else if (s_vendorFunctions->version <= 14) {
3508            if (numStrings != 15) {
3509                RLOGE("getVoiceRegistrationStateResponse Invalid response: NULL");
3510                if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3511            } else {
3512                char **resp = (char **) response;
3513                voiceRegResponse.regState = (RegState) ATOI_NULL_HANDLED_DEF(resp[0], 4);
3514                voiceRegResponse.rat = ATOI_NULL_HANDLED(resp[3]);
3515                voiceRegResponse.cssSupported = ATOI_NULL_HANDLED_DEF(resp[7], 0);
3516                voiceRegResponse.roamingIndicator = ATOI_NULL_HANDLED(resp[10]);
3517                voiceRegResponse.systemIsInPrl = ATOI_NULL_HANDLED_DEF(resp[11], 0);
3518                voiceRegResponse.defaultRoamingIndicator = ATOI_NULL_HANDLED_DEF(resp[12], 0);
3519                voiceRegResponse.reasonForDenial = ATOI_NULL_HANDLED_DEF(resp[13], 0);
3520                fillCellIdentityFromVoiceRegStateResponseString(voiceRegResponse.cellIdentity,
3521                        numStrings, resp);
3522            }
3523        } else {
3524            RIL_VoiceRegistrationStateResponse *voiceRegState =
3525                    (RIL_VoiceRegistrationStateResponse *)response;
3526
3527            if (responseLen != sizeof(RIL_VoiceRegistrationStateResponse)) {
3528                RLOGE("getVoiceRegistrationStateResponse Invalid response: NULL");
3529                if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3530            } else {
3531                voiceRegResponse.regState = (RegState) voiceRegState->regState;
3532                voiceRegResponse.rat = voiceRegState->rat;;
3533                voiceRegResponse.cssSupported = voiceRegState->cssSupported;
3534                voiceRegResponse.roamingIndicator = voiceRegState->roamingIndicator;
3535                voiceRegResponse.systemIsInPrl = voiceRegState->systemIsInPrl;
3536                voiceRegResponse.defaultRoamingIndicator = voiceRegState->defaultRoamingIndicator;
3537                voiceRegResponse.reasonForDenial = voiceRegState->reasonForDenial;
3538                fillCellIdentityResponse(voiceRegResponse.cellIdentity,
3539                        voiceRegState->cellIdentity);
3540            }
3541        }
3542
3543        Return<void> retStatus =
3544                radioService[slotId]->mRadioResponse->getVoiceRegistrationStateResponse(
3545                responseInfo, voiceRegResponse);
3546        radioService[slotId]->checkReturnStatus(retStatus);
3547    } else {
3548        RLOGE("getVoiceRegistrationStateResponse: radioService[%d]->mRadioResponse == NULL",
3549                slotId);
3550    }
3551
3552    return 0;
3553}
3554
3555int radio::getDataRegistrationStateResponse(int slotId,
3556                                           int responseType, int serial, RIL_Errno e,
3557                                           void *response, size_t responseLen) {
3558#if VDBG
3559    RLOGD("getDataRegistrationStateResponse: serial %d", serial);
3560#endif
3561
3562    if (radioService[slotId]->mRadioResponse != NULL) {
3563        RadioResponseInfo responseInfo = {};
3564        populateResponseInfo(responseInfo, serial, responseType, e);
3565        DataRegStateResult dataRegResponse = {};
3566        if (response == NULL) {
3567            RLOGE("getDataRegistrationStateResponse Invalid response: NULL");
3568            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3569        } else if (s_vendorFunctions->version <= 14) {
3570            int numStrings = responseLen / sizeof(char *);
3571            if ((numStrings != 6) && (numStrings != 11)) {
3572                RLOGE("getDataRegistrationStateResponse Invalid response: NULL");
3573                if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3574            } else {
3575                char **resp = (char **) response;
3576                dataRegResponse.regState = (RegState) ATOI_NULL_HANDLED_DEF(resp[0], 4);
3577                dataRegResponse.rat =  ATOI_NULL_HANDLED_DEF(resp[3], 0);
3578                dataRegResponse.reasonDataDenied =  ATOI_NULL_HANDLED(resp[4]);
3579                dataRegResponse.maxDataCalls =  ATOI_NULL_HANDLED_DEF(resp[5], 1);
3580                fillCellIdentityFromDataRegStateResponseString(dataRegResponse.cellIdentity,
3581                        numStrings, resp);
3582            }
3583        } else {
3584            RIL_DataRegistrationStateResponse *dataRegState =
3585                    (RIL_DataRegistrationStateResponse *)response;
3586
3587            if (responseLen != sizeof(RIL_DataRegistrationStateResponse)) {
3588                RLOGE("getDataRegistrationStateResponse Invalid response: NULL");
3589                if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3590            } else {
3591                dataRegResponse.regState = (RegState) dataRegState->regState;
3592                dataRegResponse.rat = dataRegState->rat;;
3593                dataRegResponse.reasonDataDenied = dataRegState->reasonDataDenied;
3594                dataRegResponse.maxDataCalls = dataRegState->maxDataCalls;
3595                fillCellIdentityResponse(dataRegResponse.cellIdentity, dataRegState->cellIdentity);
3596            }
3597        }
3598
3599        Return<void> retStatus =
3600                radioService[slotId]->mRadioResponse->getDataRegistrationStateResponse(responseInfo,
3601                dataRegResponse);
3602        radioService[slotId]->checkReturnStatus(retStatus);
3603    } else {
3604        RLOGE("getDataRegistrationStateResponse: radioService[%d]->mRadioResponse == NULL",
3605                slotId);
3606    }
3607
3608    return 0;
3609}
3610
3611int radio::getOperatorResponse(int slotId,
3612                              int responseType, int serial, RIL_Errno e, void *response,
3613                              size_t responseLen) {
3614#if VDBG
3615    RLOGD("getOperatorResponse: serial %d", serial);
3616#endif
3617
3618    if (radioService[slotId]->mRadioResponse != NULL) {
3619        RadioResponseInfo responseInfo = {};
3620        populateResponseInfo(responseInfo, serial, responseType, e);
3621        hidl_string longName;
3622        hidl_string shortName;
3623        hidl_string numeric;
3624        int numStrings = responseLen / sizeof(char *);
3625        if (response == NULL || numStrings != 3) {
3626            RLOGE("getOperatorResponse Invalid response: NULL");
3627            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3628
3629        } else {
3630            char **resp = (char **) response;
3631            longName = convertCharPtrToHidlString(resp[0]);
3632            shortName = convertCharPtrToHidlString(resp[1]);
3633            numeric = convertCharPtrToHidlString(resp[2]);
3634        }
3635        Return<void> retStatus = radioService[slotId]->mRadioResponse->getOperatorResponse(
3636                responseInfo, longName, shortName, numeric);
3637        radioService[slotId]->checkReturnStatus(retStatus);
3638    } else {
3639        RLOGE("getOperatorResponse: radioService[%d]->mRadioResponse == NULL",
3640                slotId);
3641    }
3642
3643    return 0;
3644}
3645
3646int radio::setRadioPowerResponse(int slotId,
3647                                int responseType, int serial, RIL_Errno e, void *response,
3648                                size_t responseLen) {
3649    RLOGD("setRadioPowerResponse: serial %d", serial);
3650
3651    if (radioService[slotId]->mRadioResponse != NULL) {
3652        RadioResponseInfo responseInfo = {};
3653        populateResponseInfo(responseInfo, serial, responseType, e);
3654        Return<void> retStatus = radioService[slotId]->mRadioResponse->setRadioPowerResponse(
3655                responseInfo);
3656        radioService[slotId]->checkReturnStatus(retStatus);
3657    } else {
3658        RLOGE("setRadioPowerResponse: radioService[%d]->mRadioResponse == NULL",
3659                slotId);
3660    }
3661
3662    return 0;
3663}
3664
3665int radio::sendDtmfResponse(int slotId,
3666                           int responseType, int serial, RIL_Errno e, void *response,
3667                           size_t responseLen) {
3668#if VDBG
3669    RLOGD("sendDtmfResponse: serial %d", serial);
3670#endif
3671
3672    if (radioService[slotId]->mRadioResponse != NULL) {
3673        RadioResponseInfo responseInfo = {};
3674        populateResponseInfo(responseInfo, serial, responseType, e);
3675        Return<void> retStatus = radioService[slotId]->mRadioResponse->sendDtmfResponse(
3676                responseInfo);
3677        radioService[slotId]->checkReturnStatus(retStatus);
3678    } else {
3679        RLOGE("sendDtmfResponse: radioService[%d]->mRadioResponse == NULL",
3680                slotId);
3681    }
3682
3683    return 0;
3684}
3685
3686SendSmsResult makeSendSmsResult(RadioResponseInfo& responseInfo, int serial, int responseType,
3687                                RIL_Errno e, void *response, size_t responseLen) {
3688    populateResponseInfo(responseInfo, serial, responseType, e);
3689    SendSmsResult result = {};
3690
3691    if (response == NULL || responseLen != sizeof(RIL_SMS_Response)) {
3692        RLOGE("Invalid response: NULL");
3693        if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3694        result.ackPDU = hidl_string();
3695    } else {
3696        RIL_SMS_Response *resp = (RIL_SMS_Response *) response;
3697        result.messageRef = resp->messageRef;
3698        result.ackPDU = convertCharPtrToHidlString(resp->ackPDU);
3699        result.errorCode = resp->errorCode;
3700    }
3701    return result;
3702}
3703
3704int radio::sendSmsResponse(int slotId,
3705                          int responseType, int serial, RIL_Errno e, void *response,
3706                          size_t responseLen) {
3707#if VDBG
3708    RLOGD("sendSmsResponse: serial %d", serial);
3709#endif
3710
3711    if (radioService[slotId]->mRadioResponse != NULL) {
3712        RadioResponseInfo responseInfo = {};
3713        SendSmsResult result = makeSendSmsResult(responseInfo, serial, responseType, e, response,
3714                responseLen);
3715
3716        Return<void> retStatus = radioService[slotId]->mRadioResponse->sendSmsResponse(responseInfo,
3717                result);
3718        radioService[slotId]->checkReturnStatus(retStatus);
3719    } else {
3720        RLOGE("sendSmsResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3721    }
3722
3723    return 0;
3724}
3725
3726int radio::sendSMSExpectMoreResponse(int slotId,
3727                                    int responseType, int serial, RIL_Errno e, void *response,
3728                                    size_t responseLen) {
3729#if VDBG
3730    RLOGD("sendSMSExpectMoreResponse: serial %d", serial);
3731#endif
3732
3733    if (radioService[slotId]->mRadioResponse != NULL) {
3734        RadioResponseInfo responseInfo = {};
3735        SendSmsResult result = makeSendSmsResult(responseInfo, serial, responseType, e, response,
3736                responseLen);
3737
3738        Return<void> retStatus = radioService[slotId]->mRadioResponse->sendSMSExpectMoreResponse(
3739                responseInfo, result);
3740        radioService[slotId]->checkReturnStatus(retStatus);
3741    } else {
3742        RLOGE("sendSMSExpectMoreResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3743    }
3744
3745    return 0;
3746}
3747
3748int radio::setupDataCallResponse(int slotId,
3749                                 int responseType, int serial, RIL_Errno e, void *response,
3750                                 size_t responseLen) {
3751#if VDBG
3752    RLOGD("setupDataCallResponse: serial %d", serial);
3753#endif
3754
3755    if (radioService[slotId]->mRadioResponse != NULL) {
3756        RadioResponseInfo responseInfo = {};
3757        populateResponseInfo(responseInfo, serial, responseType, e);
3758
3759        SetupDataCallResult result = {};
3760        if (response == NULL || responseLen != sizeof(RIL_Data_Call_Response_v11)) {
3761            RLOGE("setupDataCallResponse: Invalid response");
3762            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3763            result.status = DataCallFailCause::ERROR_UNSPECIFIED;
3764            result.type = hidl_string();
3765            result.ifname = hidl_string();
3766            result.addresses = hidl_string();
3767            result.dnses = hidl_string();
3768            result.gateways = hidl_string();
3769            result.pcscf = hidl_string();
3770        } else {
3771            convertRilDataCallToHal((RIL_Data_Call_Response_v11 *) response, result);
3772        }
3773
3774        Return<void> retStatus = radioService[slotId]->mRadioResponse->setupDataCallResponse(
3775                responseInfo, result);
3776        radioService[slotId]->checkReturnStatus(retStatus);
3777    } else {
3778        RLOGE("setupDataCallResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3779    }
3780
3781    return 0;
3782}
3783
3784IccIoResult responseIccIo(RadioResponseInfo& responseInfo, int serial, int responseType,
3785                           RIL_Errno e, void *response, size_t responseLen) {
3786    populateResponseInfo(responseInfo, serial, responseType, e);
3787    IccIoResult result = {};
3788
3789    if (response == NULL || responseLen != sizeof(RIL_SIM_IO_Response)) {
3790        RLOGE("Invalid response: NULL");
3791        if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3792        result.simResponse = hidl_string();
3793    } else {
3794        RIL_SIM_IO_Response *resp = (RIL_SIM_IO_Response *) response;
3795        result.sw1 = resp->sw1;
3796        result.sw2 = resp->sw2;
3797        result.simResponse = convertCharPtrToHidlString(resp->simResponse);
3798    }
3799    return result;
3800}
3801
3802int radio::iccIOForAppResponse(int slotId,
3803                      int responseType, int serial, RIL_Errno e, void *response,
3804                      size_t responseLen) {
3805#if VDBG
3806    RLOGD("iccIOForAppResponse: serial %d", serial);
3807#endif
3808
3809    if (radioService[slotId]->mRadioResponse != NULL) {
3810        RadioResponseInfo responseInfo = {};
3811        IccIoResult result = responseIccIo(responseInfo, serial, responseType, e, response,
3812                responseLen);
3813
3814        Return<void> retStatus = radioService[slotId]->mRadioResponse->iccIOForAppResponse(
3815                responseInfo, result);
3816        radioService[slotId]->checkReturnStatus(retStatus);
3817    } else {
3818        RLOGE("iccIOForAppResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3819    }
3820
3821    return 0;
3822}
3823
3824int radio::sendUssdResponse(int slotId,
3825                           int responseType, int serial, RIL_Errno e, void *response,
3826                           size_t responseLen) {
3827#if VDBG
3828    RLOGD("sendUssdResponse: serial %d", serial);
3829#endif
3830
3831    if (radioService[slotId]->mRadioResponse != NULL) {
3832        RadioResponseInfo responseInfo = {};
3833        populateResponseInfo(responseInfo, serial, responseType, e);
3834        Return<void> retStatus = radioService[slotId]->mRadioResponse->sendUssdResponse(
3835                responseInfo);
3836        radioService[slotId]->checkReturnStatus(retStatus);
3837    } else {
3838        RLOGE("sendUssdResponse: radioService[%d]->mRadioResponse == NULL",
3839                slotId);
3840    }
3841
3842    return 0;
3843}
3844
3845int radio::cancelPendingUssdResponse(int slotId,
3846                                    int responseType, int serial, RIL_Errno e, void *response,
3847                                    size_t responseLen) {
3848#if VDBG
3849    RLOGD("cancelPendingUssdResponse: serial %d", serial);
3850#endif
3851
3852    if (radioService[slotId]->mRadioResponse != NULL) {
3853        RadioResponseInfo responseInfo = {};
3854        populateResponseInfo(responseInfo, serial, responseType, e);
3855        Return<void> retStatus = radioService[slotId]->mRadioResponse->cancelPendingUssdResponse(
3856                responseInfo);
3857        radioService[slotId]->checkReturnStatus(retStatus);
3858    } else {
3859        RLOGE("cancelPendingUssdResponse: radioService[%d]->mRadioResponse == NULL",
3860                slotId);
3861    }
3862
3863    return 0;
3864}
3865
3866int radio::getClirResponse(int slotId,
3867                              int responseType, int serial, RIL_Errno e, void *response,
3868                              size_t responseLen) {
3869#if VDBG
3870    RLOGD("getClirResponse: serial %d", serial);
3871#endif
3872
3873    if (radioService[slotId]->mRadioResponse != NULL) {
3874        RadioResponseInfo responseInfo = {};
3875        populateResponseInfo(responseInfo, serial, responseType, e);
3876        int n = -1, m = -1;
3877        int numInts = responseLen / sizeof(int);
3878        if (response == NULL || numInts != 2) {
3879            RLOGE("getClirResponse Invalid response: NULL");
3880            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3881        } else {
3882            int *pInt = (int *) response;
3883            n = pInt[0];
3884            m = pInt[1];
3885        }
3886        Return<void> retStatus = radioService[slotId]->mRadioResponse->getClirResponse(responseInfo,
3887                n, m);
3888        radioService[slotId]->checkReturnStatus(retStatus);
3889    } else {
3890        RLOGE("getClirResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3891    }
3892
3893    return 0;
3894}
3895
3896int radio::setClirResponse(int slotId,
3897                          int responseType, int serial, RIL_Errno e, void *response,
3898                          size_t responseLen) {
3899#if VDBG
3900    RLOGD("setClirResponse: serial %d", serial);
3901#endif
3902
3903    if (radioService[slotId]->mRadioResponse != NULL) {
3904        RadioResponseInfo responseInfo = {};
3905        populateResponseInfo(responseInfo, serial, responseType, e);
3906        Return<void> retStatus = radioService[slotId]->mRadioResponse->setClirResponse(
3907                responseInfo);
3908        radioService[slotId]->checkReturnStatus(retStatus);
3909    } else {
3910        RLOGE("setClirResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3911    }
3912
3913    return 0;
3914}
3915
3916int radio::getCallForwardStatusResponse(int slotId,
3917                                       int responseType, int serial, RIL_Errno e,
3918                                       void *response, size_t responseLen) {
3919#if VDBG
3920    RLOGD("getCallForwardStatusResponse: serial %d", serial);
3921#endif
3922
3923    if (radioService[slotId]->mRadioResponse != NULL) {
3924        RadioResponseInfo responseInfo = {};
3925        populateResponseInfo(responseInfo, serial, responseType, e);
3926        hidl_vec<CallForwardInfo> callForwardInfos;
3927
3928        if (response == NULL || responseLen % sizeof(RIL_CallForwardInfo *) != 0) {
3929            RLOGE("getCallForwardStatusResponse Invalid response: NULL");
3930            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3931        } else {
3932            int num = responseLen / sizeof(RIL_CallForwardInfo *);
3933            callForwardInfos.resize(num);
3934            for (int i = 0 ; i < num; i++) {
3935                RIL_CallForwardInfo *resp = ((RIL_CallForwardInfo **) response)[i];
3936                callForwardInfos[i].status = (CallForwardInfoStatus) resp->status;
3937                callForwardInfos[i].reason = resp->reason;
3938                callForwardInfos[i].serviceClass = resp->serviceClass;
3939                callForwardInfos[i].toa = resp->toa;
3940                callForwardInfos[i].number = convertCharPtrToHidlString(resp->number);
3941                callForwardInfos[i].timeSeconds = resp->timeSeconds;
3942            }
3943        }
3944
3945        Return<void> retStatus = radioService[slotId]->mRadioResponse->getCallForwardStatusResponse(
3946                responseInfo, callForwardInfos);
3947        radioService[slotId]->checkReturnStatus(retStatus);
3948    } else {
3949        RLOGE("getCallForwardStatusResponse: radioService[%d]->mRadioResponse == NULL",
3950                slotId);
3951    }
3952
3953    return 0;
3954}
3955
3956int radio::setCallForwardResponse(int slotId,
3957                                 int responseType, int serial, RIL_Errno e, void *response,
3958                                 size_t responseLen) {
3959#if VDBG
3960    RLOGD("setCallForwardResponse: serial %d", serial);
3961#endif
3962
3963    if (radioService[slotId]->mRadioResponse != NULL) {
3964        RadioResponseInfo responseInfo = {};
3965        populateResponseInfo(responseInfo, serial, responseType, e);
3966        Return<void> retStatus = radioService[slotId]->mRadioResponse->setCallForwardResponse(
3967                responseInfo);
3968        radioService[slotId]->checkReturnStatus(retStatus);
3969    } else {
3970        RLOGE("setCallForwardResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3971    }
3972
3973    return 0;
3974}
3975
3976int radio::getCallWaitingResponse(int slotId,
3977                                 int responseType, int serial, RIL_Errno e, void *response,
3978                                 size_t responseLen) {
3979#if VDBG
3980    RLOGD("getCallWaitingResponse: serial %d", serial);
3981#endif
3982
3983    if (radioService[slotId]->mRadioResponse != NULL) {
3984        RadioResponseInfo responseInfo = {};
3985        populateResponseInfo(responseInfo, serial, responseType, e);
3986        bool enable = false;
3987        int serviceClass = -1;
3988        int numInts = responseLen / sizeof(int);
3989        if (response == NULL || numInts != 2) {
3990            RLOGE("getCallWaitingResponse Invalid response: NULL");
3991            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3992        } else {
3993            int *pInt = (int *) response;
3994            enable = pInt[0] == 1 ? true : false;
3995            serviceClass = pInt[1];
3996        }
3997        Return<void> retStatus = radioService[slotId]->mRadioResponse->getCallWaitingResponse(
3998                responseInfo, enable, serviceClass);
3999        radioService[slotId]->checkReturnStatus(retStatus);
4000    } else {
4001        RLOGE("getCallWaitingResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4002    }
4003
4004    return 0;
4005}
4006
4007int radio::setCallWaitingResponse(int slotId,
4008                                 int responseType, int serial, RIL_Errno e, void *response,
4009                                 size_t responseLen) {
4010#if VDBG
4011    RLOGD("setCallWaitingResponse: serial %d", serial);
4012#endif
4013
4014    if (radioService[slotId]->mRadioResponse != NULL) {
4015        RadioResponseInfo responseInfo = {};
4016        populateResponseInfo(responseInfo, serial, responseType, e);
4017        Return<void> retStatus = radioService[slotId]->mRadioResponse->setCallWaitingResponse(
4018                responseInfo);
4019        radioService[slotId]->checkReturnStatus(retStatus);
4020    } else {
4021        RLOGE("setCallWaitingResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4022    }
4023
4024    return 0;
4025}
4026
4027int radio::acknowledgeLastIncomingGsmSmsResponse(int slotId,
4028                                                int responseType, int serial, RIL_Errno e,
4029                                                void *response, size_t responseLen) {
4030#if VDBG
4031    RLOGD("acknowledgeLastIncomingGsmSmsResponse: serial %d", serial);
4032#endif
4033
4034    if (radioService[slotId]->mRadioResponse != NULL) {
4035        RadioResponseInfo responseInfo = {};
4036        populateResponseInfo(responseInfo, serial, responseType, e);
4037        Return<void> retStatus =
4038                radioService[slotId]->mRadioResponse->acknowledgeLastIncomingGsmSmsResponse(
4039                responseInfo);
4040        radioService[slotId]->checkReturnStatus(retStatus);
4041    } else {
4042        RLOGE("acknowledgeLastIncomingGsmSmsResponse: radioService[%d]->mRadioResponse "
4043                "== NULL", slotId);
4044    }
4045
4046    return 0;
4047}
4048
4049int radio::acceptCallResponse(int slotId,
4050                             int responseType, int serial, RIL_Errno e,
4051                             void *response, size_t responseLen) {
4052#if VDBG
4053    RLOGD("acceptCallResponse: serial %d", serial);
4054#endif
4055
4056    if (radioService[slotId]->mRadioResponse != NULL) {
4057        RadioResponseInfo responseInfo = {};
4058        populateResponseInfo(responseInfo, serial, responseType, e);
4059        Return<void> retStatus = radioService[slotId]->mRadioResponse->acceptCallResponse(
4060                responseInfo);
4061        radioService[slotId]->checkReturnStatus(retStatus);
4062    } else {
4063        RLOGE("acceptCallResponse: radioService[%d]->mRadioResponse == NULL",
4064                slotId);
4065    }
4066
4067    return 0;
4068}
4069
4070int radio::deactivateDataCallResponse(int slotId,
4071                                                int responseType, int serial, RIL_Errno e,
4072                                                void *response, size_t responseLen) {
4073#if VDBG
4074    RLOGD("deactivateDataCallResponse: serial %d", serial);
4075#endif
4076
4077    if (radioService[slotId]->mRadioResponse != NULL) {
4078        RadioResponseInfo responseInfo = {};
4079        populateResponseInfo(responseInfo, serial, responseType, e);
4080        Return<void> retStatus = radioService[slotId]->mRadioResponse->deactivateDataCallResponse(
4081                responseInfo);
4082        radioService[slotId]->checkReturnStatus(retStatus);
4083    } else {
4084        RLOGE("deactivateDataCallResponse: radioService[%d]->mRadioResponse == NULL",
4085                slotId);
4086    }
4087
4088    return 0;
4089}
4090
4091int radio::getFacilityLockForAppResponse(int slotId,
4092                                        int responseType, int serial, RIL_Errno e,
4093                                        void *response, size_t responseLen) {
4094#if VDBG
4095    RLOGD("getFacilityLockForAppResponse: serial %d", serial);
4096#endif
4097
4098    if (radioService[slotId]->mRadioResponse != NULL) {
4099        RadioResponseInfo responseInfo = {};
4100        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4101        Return<void> retStatus = radioService[slotId]->mRadioResponse->
4102                getFacilityLockForAppResponse(responseInfo, ret);
4103        radioService[slotId]->checkReturnStatus(retStatus);
4104    } else {
4105        RLOGE("getFacilityLockForAppResponse: radioService[%d]->mRadioResponse == NULL",
4106                slotId);
4107    }
4108
4109    return 0;
4110}
4111
4112int radio::setFacilityLockForAppResponse(int slotId,
4113                                      int responseType, int serial, RIL_Errno e,
4114                                      void *response, size_t responseLen) {
4115#if VDBG
4116    RLOGD("setFacilityLockForAppResponse: serial %d", serial);
4117#endif
4118
4119    if (radioService[slotId]->mRadioResponse != NULL) {
4120        RadioResponseInfo responseInfo = {};
4121        int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
4122        Return<void> retStatus
4123                = radioService[slotId]->mRadioResponse->setFacilityLockForAppResponse(responseInfo,
4124                ret);
4125        radioService[slotId]->checkReturnStatus(retStatus);
4126    } else {
4127        RLOGE("setFacilityLockForAppResponse: radioService[%d]->mRadioResponse == NULL",
4128                slotId);
4129    }
4130
4131    return 0;
4132}
4133
4134int radio::setBarringPasswordResponse(int slotId,
4135                             int responseType, int serial, RIL_Errno e,
4136                             void *response, size_t responseLen) {
4137#if VDBG
4138    RLOGD("acceptCallResponse: serial %d", serial);
4139#endif
4140
4141    if (radioService[slotId]->mRadioResponse != NULL) {
4142        RadioResponseInfo responseInfo = {};
4143        populateResponseInfo(responseInfo, serial, responseType, e);
4144        Return<void> retStatus
4145                = radioService[slotId]->mRadioResponse->setBarringPasswordResponse(responseInfo);
4146        radioService[slotId]->checkReturnStatus(retStatus);
4147    } else {
4148        RLOGE("setBarringPasswordResponse: radioService[%d]->mRadioResponse == NULL",
4149                slotId);
4150    }
4151
4152    return 0;
4153}
4154
4155int radio::getNetworkSelectionModeResponse(int slotId,
4156                                          int responseType, int serial, RIL_Errno e, void *response,
4157                                          size_t responseLen) {
4158#if VDBG
4159    RLOGD("getNetworkSelectionModeResponse: serial %d", serial);
4160#endif
4161
4162    if (radioService[slotId]->mRadioResponse != NULL) {
4163        RadioResponseInfo responseInfo = {};
4164        populateResponseInfo(responseInfo, serial, responseType, e);
4165        bool manual = false;
4166        int serviceClass;
4167        if (response == NULL || responseLen != sizeof(int)) {
4168            RLOGE("getNetworkSelectionModeResponse Invalid response: NULL");
4169            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4170        } else {
4171            int *pInt = (int *) response;
4172            manual = pInt[0] == 1 ? true : false;
4173        }
4174        Return<void> retStatus
4175                = radioService[slotId]->mRadioResponse->getNetworkSelectionModeResponse(
4176                responseInfo,
4177                manual);
4178        radioService[slotId]->checkReturnStatus(retStatus);
4179    } else {
4180        RLOGE("getNetworkSelectionModeResponse: radioService[%d]->mRadioResponse == NULL",
4181                slotId);
4182    }
4183
4184    return 0;
4185}
4186
4187int radio::setNetworkSelectionModeAutomaticResponse(int slotId, int responseType, int serial,
4188                                                    RIL_Errno e, void *response,
4189                                                    size_t responseLen) {
4190#if VDBG
4191    RLOGD("setNetworkSelectionModeAutomaticResponse: serial %d", serial);
4192#endif
4193
4194    if (radioService[slotId]->mRadioResponse != NULL) {
4195        RadioResponseInfo responseInfo = {};
4196        populateResponseInfo(responseInfo, serial, responseType, e);
4197        Return<void> retStatus
4198                = radioService[slotId]->mRadioResponse->setNetworkSelectionModeAutomaticResponse(
4199                responseInfo);
4200        radioService[slotId]->checkReturnStatus(retStatus);
4201    } else {
4202        RLOGE("setNetworkSelectionModeAutomaticResponse: radioService[%d]->mRadioResponse "
4203                "== NULL", slotId);
4204    }
4205
4206    return 0;
4207}
4208
4209int radio::setNetworkSelectionModeManualResponse(int slotId,
4210                             int responseType, int serial, RIL_Errno e,
4211                             void *response, size_t responseLen) {
4212#if VDBG
4213    RLOGD("setNetworkSelectionModeManualResponse: serial %d", serial);
4214#endif
4215
4216    if (radioService[slotId]->mRadioResponse != NULL) {
4217        RadioResponseInfo responseInfo = {};
4218        populateResponseInfo(responseInfo, serial, responseType, e);
4219        Return<void> retStatus
4220                = radioService[slotId]->mRadioResponse->setNetworkSelectionModeManualResponse(
4221                responseInfo);
4222        radioService[slotId]->checkReturnStatus(retStatus);
4223    } else {
4224        RLOGE("acceptCallResponse: radioService[%d]->setNetworkSelectionModeManualResponse "
4225                "== NULL", slotId);
4226    }
4227
4228    return 0;
4229}
4230
4231int convertOperatorStatusToInt(const char *str) {
4232    if (strncmp("unknown", str, 9) == 0) {
4233        return (int) OperatorStatus::UNKNOWN;
4234    } else if (strncmp("available", str, 9) == 0) {
4235        return (int) OperatorStatus::AVAILABLE;
4236    } else if (strncmp("current", str, 9) == 0) {
4237        return (int) OperatorStatus::CURRENT;
4238    } else if (strncmp("forbidden", str, 9) == 0) {
4239        return (int) OperatorStatus::FORBIDDEN;
4240    } else {
4241        return -1;
4242    }
4243}
4244
4245int radio::getAvailableNetworksResponse(int slotId,
4246                              int responseType, int serial, RIL_Errno e, void *response,
4247                              size_t responseLen) {
4248#if VDBG
4249    RLOGD("getAvailableNetworksResponse: serial %d", serial);
4250#endif
4251
4252    if (radioService[slotId]->mRadioResponse != NULL) {
4253        RadioResponseInfo responseInfo = {};
4254        populateResponseInfo(responseInfo, serial, responseType, e);
4255        hidl_vec<OperatorInfo> networks;
4256        if (response == NULL || responseLen % (4 * sizeof(char *))!= 0) {
4257            RLOGE("getAvailableNetworksResponse Invalid response: NULL");
4258            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4259        } else {
4260            char **resp = (char **) response;
4261            int numStrings = responseLen / sizeof(char *);
4262            networks.resize(numStrings/4);
4263            for (int i = 0, j = 0; i < numStrings; i = i + 4, j++) {
4264                networks[j].alphaLong = convertCharPtrToHidlString(resp[i]);
4265                networks[j].alphaShort = convertCharPtrToHidlString(resp[i + 1]);
4266                networks[j].operatorNumeric = convertCharPtrToHidlString(resp[i + 2]);
4267                int status = convertOperatorStatusToInt(resp[i + 3]);
4268                if (status == -1) {
4269                    if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4270                } else {
4271                    networks[j].status = (OperatorStatus) status;
4272                }
4273            }
4274        }
4275        Return<void> retStatus
4276                = radioService[slotId]->mRadioResponse->getAvailableNetworksResponse(responseInfo,
4277                networks);
4278        radioService[slotId]->checkReturnStatus(retStatus);
4279    } else {
4280        RLOGE("getAvailableNetworksResponse: radioService[%d]->mRadioResponse == NULL",
4281                slotId);
4282    }
4283
4284    return 0;
4285}
4286
4287int radio::startDtmfResponse(int slotId,
4288                            int responseType, int serial, RIL_Errno e,
4289                            void *response, size_t responseLen) {
4290#if VDBG
4291    RLOGD("startDtmfResponse: serial %d", serial);
4292#endif
4293
4294    if (radioService[slotId]->mRadioResponse != NULL) {
4295        RadioResponseInfo responseInfo = {};
4296        populateResponseInfo(responseInfo, serial, responseType, e);
4297        Return<void> retStatus
4298                = radioService[slotId]->mRadioResponse->startDtmfResponse(responseInfo);
4299        radioService[slotId]->checkReturnStatus(retStatus);
4300    } else {
4301        RLOGE("startDtmfResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4302    }
4303
4304    return 0;
4305}
4306
4307int radio::stopDtmfResponse(int slotId,
4308                           int responseType, int serial, RIL_Errno e,
4309                           void *response, size_t responseLen) {
4310#if VDBG
4311    RLOGD("stopDtmfResponse: serial %d", serial);
4312#endif
4313
4314    if (radioService[slotId]->mRadioResponse != NULL) {
4315        RadioResponseInfo responseInfo = {};
4316        populateResponseInfo(responseInfo, serial, responseType, e);
4317        Return<void> retStatus
4318                = radioService[slotId]->mRadioResponse->stopDtmfResponse(responseInfo);
4319        radioService[slotId]->checkReturnStatus(retStatus);
4320    } else {
4321        RLOGE("stopDtmfResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4322    }
4323
4324    return 0;
4325}
4326
4327int radio::getBasebandVersionResponse(int slotId,
4328                                     int responseType, int serial, RIL_Errno e,
4329                                     void *response, size_t responseLen) {
4330#if VDBG
4331    RLOGD("getBasebandVersionResponse: serial %d", serial);
4332#endif
4333
4334    if (radioService[slotId]->mRadioResponse != NULL) {
4335        RadioResponseInfo responseInfo = {};
4336        populateResponseInfo(responseInfo, serial, responseType, e);
4337        Return<void> retStatus
4338                = radioService[slotId]->mRadioResponse->getBasebandVersionResponse(responseInfo,
4339                convertCharPtrToHidlString((char *) response));
4340        radioService[slotId]->checkReturnStatus(retStatus);
4341    } else {
4342        RLOGE("getBasebandVersionResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4343    }
4344
4345    return 0;
4346}
4347
4348int radio::separateConnectionResponse(int slotId,
4349                                     int responseType, int serial, RIL_Errno e,
4350                                     void *response, size_t responseLen) {
4351#if VDBG
4352    RLOGD("separateConnectionResponse: serial %d", serial);
4353#endif
4354
4355    if (radioService[slotId]->mRadioResponse != NULL) {
4356        RadioResponseInfo responseInfo = {};
4357        populateResponseInfo(responseInfo, serial, responseType, e);
4358        Return<void> retStatus
4359                = radioService[slotId]->mRadioResponse->separateConnectionResponse(responseInfo);
4360        radioService[slotId]->checkReturnStatus(retStatus);
4361    } else {
4362        RLOGE("separateConnectionResponse: radioService[%d]->mRadioResponse == NULL",
4363                slotId);
4364    }
4365
4366    return 0;
4367}
4368
4369int radio::setMuteResponse(int slotId,
4370                          int responseType, int serial, RIL_Errno e,
4371                          void *response, size_t responseLen) {
4372#if VDBG
4373    RLOGD("setMuteResponse: serial %d", serial);
4374#endif
4375
4376    if (radioService[slotId]->mRadioResponse != NULL) {
4377        RadioResponseInfo responseInfo = {};
4378        populateResponseInfo(responseInfo, serial, responseType, e);
4379        Return<void> retStatus
4380                = radioService[slotId]->mRadioResponse->setMuteResponse(responseInfo);
4381        radioService[slotId]->checkReturnStatus(retStatus);
4382    } else {
4383        RLOGE("setMuteResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4384    }
4385
4386    return 0;
4387}
4388
4389int radio::getMuteResponse(int slotId,
4390                          int responseType, int serial, RIL_Errno e, void *response,
4391                          size_t responseLen) {
4392#if VDBG
4393    RLOGD("getMuteResponse: serial %d", serial);
4394#endif
4395
4396    if (radioService[slotId]->mRadioResponse != NULL) {
4397        RadioResponseInfo responseInfo = {};
4398        populateResponseInfo(responseInfo, serial, responseType, e);
4399        bool enable = false;
4400        int serviceClass;
4401        if (response == NULL || responseLen != sizeof(int)) {
4402            RLOGE("getMuteResponse Invalid response: NULL");
4403            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4404        } else {
4405            int *pInt = (int *) response;
4406            enable = pInt[0] == 1 ? true : false;
4407        }
4408        Return<void> retStatus = radioService[slotId]->mRadioResponse->getMuteResponse(responseInfo,
4409                enable);
4410        radioService[slotId]->checkReturnStatus(retStatus);
4411    } else {
4412        RLOGE("getMuteResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4413    }
4414
4415    return 0;
4416}
4417
4418int radio::getClipResponse(int slotId,
4419                          int responseType, int serial, RIL_Errno e,
4420                          void *response, size_t responseLen) {
4421#if VDBG
4422    RLOGD("getClipResponse: serial %d", serial);
4423#endif
4424
4425    if (radioService[slotId]->mRadioResponse != NULL) {
4426        RadioResponseInfo responseInfo = {};
4427        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4428        Return<void> retStatus = radioService[slotId]->mRadioResponse->getClipResponse(responseInfo,
4429                (ClipStatus) ret);
4430        radioService[slotId]->checkReturnStatus(retStatus);
4431    } else {
4432        RLOGE("getClipResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4433    }
4434
4435    return 0;
4436}
4437
4438int radio::getDataCallListResponse(int slotId,
4439                                   int responseType, int serial, RIL_Errno e,
4440                                   void *response, size_t responseLen) {
4441#if VDBG
4442    RLOGD("getDataCallListResponse: serial %d", serial);
4443#endif
4444
4445    if (radioService[slotId]->mRadioResponse != NULL) {
4446        RadioResponseInfo responseInfo = {};
4447        populateResponseInfo(responseInfo, serial, responseType, e);
4448
4449        hidl_vec<SetupDataCallResult> ret;
4450        if (response == NULL || responseLen % sizeof(RIL_Data_Call_Response_v11) != 0) {
4451            RLOGE("getDataCallListResponse: invalid response");
4452            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4453        } else {
4454            convertRilDataCallListToHal(response, responseLen, ret);
4455        }
4456
4457        Return<void> retStatus = radioService[slotId]->mRadioResponse->getDataCallListResponse(
4458                responseInfo, ret);
4459        radioService[slotId]->checkReturnStatus(retStatus);
4460    } else {
4461        RLOGE("getDataCallListResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4462    }
4463
4464    return 0;
4465}
4466
4467int radio::setSuppServiceNotificationsResponse(int slotId,
4468                                              int responseType, int serial, RIL_Errno e,
4469                                              void *response, size_t responseLen) {
4470#if VDBG
4471    RLOGD("setSuppServiceNotificationsResponse: serial %d", serial);
4472#endif
4473
4474    if (radioService[slotId]->mRadioResponse != NULL) {
4475        RadioResponseInfo responseInfo = {};
4476        populateResponseInfo(responseInfo, serial, responseType, e);
4477        Return<void> retStatus
4478                = radioService[slotId]->mRadioResponse->setSuppServiceNotificationsResponse(
4479                responseInfo);
4480        radioService[slotId]->checkReturnStatus(retStatus);
4481    } else {
4482        RLOGE("setSuppServiceNotificationsResponse: radioService[%d]->mRadioResponse "
4483                "== NULL", slotId);
4484    }
4485
4486    return 0;
4487}
4488
4489int radio::deleteSmsOnSimResponse(int slotId,
4490                                 int responseType, int serial, RIL_Errno e,
4491                                 void *response, size_t responseLen) {
4492#if VDBG
4493    RLOGD("deleteSmsOnSimResponse: serial %d", serial);
4494#endif
4495
4496    if (radioService[slotId]->mRadioResponse != NULL) {
4497        RadioResponseInfo responseInfo = {};
4498        populateResponseInfo(responseInfo, serial, responseType, e);
4499        Return<void> retStatus
4500                = radioService[slotId]->mRadioResponse->deleteSmsOnSimResponse(responseInfo);
4501        radioService[slotId]->checkReturnStatus(retStatus);
4502    } else {
4503        RLOGE("deleteSmsOnSimResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4504    }
4505
4506    return 0;
4507}
4508
4509int radio::setBandModeResponse(int slotId,
4510                              int responseType, int serial, RIL_Errno e,
4511                              void *response, size_t responseLen) {
4512#if VDBG
4513    RLOGD("setBandModeResponse: serial %d", serial);
4514#endif
4515
4516    if (radioService[slotId]->mRadioResponse != NULL) {
4517        RadioResponseInfo responseInfo = {};
4518        populateResponseInfo(responseInfo, serial, responseType, e);
4519        Return<void> retStatus
4520                = radioService[slotId]->mRadioResponse->setBandModeResponse(responseInfo);
4521        radioService[slotId]->checkReturnStatus(retStatus);
4522    } else {
4523        RLOGE("setBandModeResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4524    }
4525
4526    return 0;
4527}
4528
4529int radio::writeSmsToSimResponse(int slotId,
4530                                int responseType, int serial, RIL_Errno e,
4531                                void *response, size_t responseLen) {
4532#if VDBG
4533    RLOGD("writeSmsToSimResponse: serial %d", serial);
4534#endif
4535
4536    if (radioService[slotId]->mRadioResponse != NULL) {
4537        RadioResponseInfo responseInfo = {};
4538        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4539        Return<void> retStatus
4540                = radioService[slotId]->mRadioResponse->writeSmsToSimResponse(responseInfo, ret);
4541        radioService[slotId]->checkReturnStatus(retStatus);
4542    } else {
4543        RLOGE("writeSmsToSimResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4544    }
4545
4546    return 0;
4547}
4548
4549int radio::getAvailableBandModesResponse(int slotId,
4550                                        int responseType, int serial, RIL_Errno e, void *response,
4551                                        size_t responseLen) {
4552#if VDBG
4553    RLOGD("getAvailableBandModesResponse: serial %d", serial);
4554#endif
4555
4556    if (radioService[slotId]->mRadioResponse != NULL) {
4557        RadioResponseInfo responseInfo = {};
4558        populateResponseInfo(responseInfo, serial, responseType, e);
4559        hidl_vec<RadioBandMode> modes;
4560        if (response == NULL || responseLen % sizeof(int) != 0) {
4561            RLOGE("getAvailableBandModesResponse Invalid response: NULL");
4562            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4563        } else {
4564            int *pInt = (int *) response;
4565            int numInts = responseLen / sizeof(int);
4566            modes.resize(numInts);
4567            for (int i = 0; i < numInts; i++) {
4568                modes[i] = (RadioBandMode) pInt[i];
4569            }
4570        }
4571        Return<void> retStatus
4572                = radioService[slotId]->mRadioResponse->getAvailableBandModesResponse(responseInfo,
4573                modes);
4574        radioService[slotId]->checkReturnStatus(retStatus);
4575    } else {
4576        RLOGE("getAvailableBandModesResponse: radioService[%d]->mRadioResponse == NULL",
4577                slotId);
4578    }
4579
4580    return 0;
4581}
4582
4583int radio::sendEnvelopeResponse(int slotId,
4584                               int responseType, int serial, RIL_Errno e,
4585                               void *response, size_t responseLen) {
4586#if VDBG
4587    RLOGD("sendEnvelopeResponse: serial %d", serial);
4588#endif
4589
4590    if (radioService[slotId]->mRadioResponse != NULL) {
4591        RadioResponseInfo responseInfo = {};
4592        populateResponseInfo(responseInfo, serial, responseType, e);
4593        Return<void> retStatus
4594                = radioService[slotId]->mRadioResponse->sendEnvelopeResponse(responseInfo,
4595                convertCharPtrToHidlString((char *) response));
4596        radioService[slotId]->checkReturnStatus(retStatus);
4597    } else {
4598        RLOGE("sendEnvelopeResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4599    }
4600
4601    return 0;
4602}
4603
4604int radio::sendTerminalResponseToSimResponse(int slotId,
4605                                            int responseType, int serial, RIL_Errno e,
4606                                            void *response, size_t responseLen) {
4607#if VDBG
4608    RLOGD("sendTerminalResponseToSimResponse: serial %d", serial);
4609#endif
4610
4611    if (radioService[slotId]->mRadioResponse != NULL) {
4612        RadioResponseInfo responseInfo = {};
4613        populateResponseInfo(responseInfo, serial, responseType, e);
4614        Return<void> retStatus
4615                = radioService[slotId]->mRadioResponse->sendTerminalResponseToSimResponse(
4616                responseInfo);
4617        radioService[slotId]->checkReturnStatus(retStatus);
4618    } else {
4619        RLOGE("sendTerminalResponseToSimResponse: radioService[%d]->mRadioResponse == NULL",
4620                slotId);
4621    }
4622
4623    return 0;
4624}
4625
4626int radio::handleStkCallSetupRequestFromSimResponse(int slotId,
4627                                                   int responseType, int serial,
4628                                                   RIL_Errno e, void *response,
4629                                                   size_t responseLen) {
4630#if VDBG
4631    RLOGD("handleStkCallSetupRequestFromSimResponse: serial %d", serial);
4632#endif
4633
4634    if (radioService[slotId]->mRadioResponse != NULL) {
4635        RadioResponseInfo responseInfo = {};
4636        populateResponseInfo(responseInfo, serial, responseType, e);
4637        Return<void> retStatus
4638                = radioService[slotId]->mRadioResponse->handleStkCallSetupRequestFromSimResponse(
4639                responseInfo);
4640        radioService[slotId]->checkReturnStatus(retStatus);
4641    } else {
4642        RLOGE("handleStkCallSetupRequestFromSimResponse: radioService[%d]->mRadioResponse "
4643                "== NULL", slotId);
4644    }
4645
4646    return 0;
4647}
4648
4649int radio::explicitCallTransferResponse(int slotId,
4650                                       int responseType, int serial, RIL_Errno e,
4651                                       void *response, size_t responseLen) {
4652#if VDBG
4653    RLOGD("explicitCallTransferResponse: serial %d", serial);
4654#endif
4655
4656    if (radioService[slotId]->mRadioResponse != NULL) {
4657        RadioResponseInfo responseInfo = {};
4658        populateResponseInfo(responseInfo, serial, responseType, e);
4659        Return<void> retStatus
4660                = radioService[slotId]->mRadioResponse->explicitCallTransferResponse(responseInfo);
4661        radioService[slotId]->checkReturnStatus(retStatus);
4662    } else {
4663        RLOGE("explicitCallTransferResponse: radioService[%d]->mRadioResponse == NULL",
4664                slotId);
4665    }
4666
4667    return 0;
4668}
4669
4670int radio::setPreferredNetworkTypeResponse(int slotId,
4671                                 int responseType, int serial, RIL_Errno e,
4672                                 void *response, size_t responseLen) {
4673#if VDBG
4674    RLOGD("setPreferredNetworkTypeResponse: serial %d", serial);
4675#endif
4676
4677    if (radioService[slotId]->mRadioResponse != NULL) {
4678        RadioResponseInfo responseInfo = {};
4679        populateResponseInfo(responseInfo, serial, responseType, e);
4680        Return<void> retStatus
4681                = radioService[slotId]->mRadioResponse->setPreferredNetworkTypeResponse(
4682                responseInfo);
4683        radioService[slotId]->checkReturnStatus(retStatus);
4684    } else {
4685        RLOGE("setPreferredNetworkTypeResponse: radioService[%d]->mRadioResponse == NULL",
4686                slotId);
4687    }
4688
4689    return 0;
4690}
4691
4692
4693int radio::getPreferredNetworkTypeResponse(int slotId,
4694                                          int responseType, int serial, RIL_Errno e,
4695                                          void *response, size_t responseLen) {
4696#if VDBG
4697    RLOGD("getPreferredNetworkTypeResponse: serial %d", serial);
4698#endif
4699
4700    if (radioService[slotId]->mRadioResponse != NULL) {
4701        RadioResponseInfo responseInfo = {};
4702        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4703        Return<void> retStatus
4704                = radioService[slotId]->mRadioResponse->getPreferredNetworkTypeResponse(
4705                responseInfo, (PreferredNetworkType) ret);
4706        radioService[slotId]->checkReturnStatus(retStatus);
4707    } else {
4708        RLOGE("getPreferredNetworkTypeResponse: radioService[%d]->mRadioResponse == NULL",
4709                slotId);
4710    }
4711
4712    return 0;
4713}
4714
4715int radio::getNeighboringCidsResponse(int slotId,
4716                                     int responseType, int serial, RIL_Errno e,
4717                                     void *response, size_t responseLen) {
4718#if VDBG
4719    RLOGD("getNeighboringCidsResponse: serial %d", serial);
4720#endif
4721
4722    if (radioService[slotId]->mRadioResponse != NULL) {
4723        RadioResponseInfo responseInfo = {};
4724        populateResponseInfo(responseInfo, serial, responseType, e);
4725        hidl_vec<NeighboringCell> cells;
4726
4727        if (response == NULL || responseLen % sizeof(RIL_NeighboringCell *) != 0) {
4728            RLOGE("getNeighboringCidsResponse Invalid response: NULL");
4729            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4730        } else {
4731            int num = responseLen / sizeof(RIL_NeighboringCell *);
4732            cells.resize(num);
4733            for (int i = 0 ; i < num; i++) {
4734                RIL_NeighboringCell *resp = ((RIL_NeighboringCell **) response)[i];
4735                cells[i].cid = convertCharPtrToHidlString(resp->cid);
4736                cells[i].rssi = resp->rssi;
4737            }
4738        }
4739
4740        Return<void> retStatus
4741                = radioService[slotId]->mRadioResponse->getNeighboringCidsResponse(responseInfo,
4742                cells);
4743        radioService[slotId]->checkReturnStatus(retStatus);
4744    } else {
4745        RLOGE("getNeighboringCidsResponse: radioService[%d]->mRadioResponse == NULL",
4746                slotId);
4747    }
4748
4749    return 0;
4750}
4751
4752int radio::setLocationUpdatesResponse(int slotId,
4753                                     int responseType, int serial, RIL_Errno e,
4754                                     void *response, size_t responseLen) {
4755#if VDBG
4756    RLOGD("setLocationUpdatesResponse: serial %d", serial);
4757#endif
4758
4759    if (radioService[slotId]->mRadioResponse != NULL) {
4760        RadioResponseInfo responseInfo = {};
4761        populateResponseInfo(responseInfo, serial, responseType, e);
4762        Return<void> retStatus
4763                = radioService[slotId]->mRadioResponse->setLocationUpdatesResponse(responseInfo);
4764        radioService[slotId]->checkReturnStatus(retStatus);
4765    } else {
4766        RLOGE("setLocationUpdatesResponse: radioService[%d]->mRadioResponse == NULL",
4767                slotId);
4768    }
4769
4770    return 0;
4771}
4772
4773int radio::setCdmaSubscriptionSourceResponse(int slotId,
4774                                 int responseType, int serial, RIL_Errno e,
4775                                 void *response, size_t responseLen) {
4776#if VDBG
4777    RLOGD("setCdmaSubscriptionSourceResponse: serial %d", serial);
4778#endif
4779
4780    if (radioService[slotId]->mRadioResponse != NULL) {
4781        RadioResponseInfo responseInfo = {};
4782        populateResponseInfo(responseInfo, serial, responseType, e);
4783        Return<void> retStatus
4784                = radioService[slotId]->mRadioResponse->setCdmaSubscriptionSourceResponse(
4785                responseInfo);
4786        radioService[slotId]->checkReturnStatus(retStatus);
4787    } else {
4788        RLOGE("setCdmaSubscriptionSourceResponse: radioService[%d]->mRadioResponse == NULL",
4789                slotId);
4790    }
4791
4792    return 0;
4793}
4794
4795int radio::setCdmaRoamingPreferenceResponse(int slotId,
4796                                 int responseType, int serial, RIL_Errno e,
4797                                 void *response, size_t responseLen) {
4798#if VDBG
4799    RLOGD("setCdmaRoamingPreferenceResponse: serial %d", serial);
4800#endif
4801
4802    if (radioService[slotId]->mRadioResponse != NULL) {
4803        RadioResponseInfo responseInfo = {};
4804        populateResponseInfo(responseInfo, serial, responseType, e);
4805        Return<void> retStatus
4806                = radioService[slotId]->mRadioResponse->setCdmaRoamingPreferenceResponse(
4807                responseInfo);
4808        radioService[slotId]->checkReturnStatus(retStatus);
4809    } else {
4810        RLOGE("setCdmaRoamingPreferenceResponse: radioService[%d]->mRadioResponse == NULL",
4811                slotId);
4812    }
4813
4814    return 0;
4815}
4816
4817int radio::getCdmaRoamingPreferenceResponse(int slotId,
4818                                           int responseType, int serial, RIL_Errno e,
4819                                           void *response, size_t responseLen) {
4820#if VDBG
4821    RLOGD("getCdmaRoamingPreferenceResponse: serial %d", serial);
4822#endif
4823
4824    if (radioService[slotId]->mRadioResponse != NULL) {
4825        RadioResponseInfo responseInfo = {};
4826        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4827        Return<void> retStatus
4828                = radioService[slotId]->mRadioResponse->getCdmaRoamingPreferenceResponse(
4829                responseInfo, (CdmaRoamingType) ret);
4830        radioService[slotId]->checkReturnStatus(retStatus);
4831    } else {
4832        RLOGE("getCdmaRoamingPreferenceResponse: radioService[%d]->mRadioResponse == NULL",
4833                slotId);
4834    }
4835
4836    return 0;
4837}
4838
4839int radio::setTTYModeResponse(int slotId,
4840                             int responseType, int serial, RIL_Errno e,
4841                             void *response, size_t responseLen) {
4842#if VDBG
4843    RLOGD("setTTYModeResponse: serial %d", serial);
4844#endif
4845
4846    if (radioService[slotId]->mRadioResponse != NULL) {
4847        RadioResponseInfo responseInfo = {};
4848        populateResponseInfo(responseInfo, serial, responseType, e);
4849        Return<void> retStatus
4850                = radioService[slotId]->mRadioResponse->setTTYModeResponse(responseInfo);
4851        radioService[slotId]->checkReturnStatus(retStatus);
4852    } else {
4853        RLOGE("setTTYModeResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4854    }
4855
4856    return 0;
4857}
4858
4859int radio::getTTYModeResponse(int slotId,
4860                             int responseType, int serial, RIL_Errno e,
4861                             void *response, size_t responseLen) {
4862#if VDBG
4863    RLOGD("getTTYModeResponse: serial %d", serial);
4864#endif
4865
4866    if (radioService[slotId]->mRadioResponse != NULL) {
4867        RadioResponseInfo responseInfo = {};
4868        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4869        Return<void> retStatus
4870                = radioService[slotId]->mRadioResponse->getTTYModeResponse(responseInfo,
4871                (TtyMode) ret);
4872        radioService[slotId]->checkReturnStatus(retStatus);
4873    } else {
4874        RLOGE("getTTYModeResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4875    }
4876
4877    return 0;
4878}
4879
4880int radio::setPreferredVoicePrivacyResponse(int slotId,
4881                                 int responseType, int serial, RIL_Errno e,
4882                                 void *response, size_t responseLen) {
4883#if VDBG
4884    RLOGD("setPreferredVoicePrivacyResponse: serial %d", serial);
4885#endif
4886
4887    if (radioService[slotId]->mRadioResponse != NULL) {
4888        RadioResponseInfo responseInfo = {};
4889        populateResponseInfo(responseInfo, serial, responseType, e);
4890        Return<void> retStatus
4891                = radioService[slotId]->mRadioResponse->setPreferredVoicePrivacyResponse(
4892                responseInfo);
4893        radioService[slotId]->checkReturnStatus(retStatus);
4894    } else {
4895        RLOGE("setPreferredVoicePrivacyResponse: radioService[%d]->mRadioResponse == NULL",
4896                slotId);
4897    }
4898
4899    return 0;
4900}
4901
4902int radio::getPreferredVoicePrivacyResponse(int slotId,
4903                                           int responseType, int serial, RIL_Errno e,
4904                                           void *response, size_t responseLen) {
4905#if VDBG
4906    RLOGD("getPreferredVoicePrivacyResponse: serial %d", serial);
4907#endif
4908
4909    if (radioService[slotId]->mRadioResponse != NULL) {
4910        RadioResponseInfo responseInfo = {};
4911        populateResponseInfo(responseInfo, serial, responseType, e);
4912        bool enable = false;
4913        int numInts = responseLen / sizeof(int);
4914        if (response == NULL || numInts != 1) {
4915            RLOGE("getPreferredVoicePrivacyResponse Invalid response: NULL");
4916            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4917        } else {
4918            int *pInt = (int *) response;
4919            enable = pInt[0] == 1 ? true : false;
4920        }
4921        Return<void> retStatus
4922                = radioService[slotId]->mRadioResponse->getPreferredVoicePrivacyResponse(
4923                responseInfo, enable);
4924        radioService[slotId]->checkReturnStatus(retStatus);
4925    } else {
4926        RLOGE("getPreferredVoicePrivacyResponse: radioService[%d]->mRadioResponse == NULL",
4927                slotId);
4928    }
4929
4930    return 0;
4931}
4932
4933int radio::sendCDMAFeatureCodeResponse(int slotId,
4934                                 int responseType, int serial, RIL_Errno e,
4935                                 void *response, size_t responseLen) {
4936#if VDBG
4937    RLOGD("sendCDMAFeatureCodeResponse: serial %d", serial);
4938#endif
4939
4940    if (radioService[slotId]->mRadioResponse != NULL) {
4941        RadioResponseInfo responseInfo = {};
4942        populateResponseInfo(responseInfo, serial, responseType, e);
4943        Return<void> retStatus
4944                = radioService[slotId]->mRadioResponse->sendCDMAFeatureCodeResponse(responseInfo);
4945        radioService[slotId]->checkReturnStatus(retStatus);
4946    } else {
4947        RLOGE("sendCDMAFeatureCodeResponse: radioService[%d]->mRadioResponse == NULL",
4948                slotId);
4949    }
4950
4951    return 0;
4952}
4953
4954int radio::sendBurstDtmfResponse(int slotId,
4955                                 int responseType, int serial, RIL_Errno e,
4956                                 void *response, size_t responseLen) {
4957#if VDBG
4958    RLOGD("sendBurstDtmfResponse: serial %d", serial);
4959#endif
4960
4961    if (radioService[slotId]->mRadioResponse != NULL) {
4962        RadioResponseInfo responseInfo = {};
4963        populateResponseInfo(responseInfo, serial, responseType, e);
4964        Return<void> retStatus
4965                = radioService[slotId]->mRadioResponse->sendBurstDtmfResponse(responseInfo);
4966        radioService[slotId]->checkReturnStatus(retStatus);
4967    } else {
4968        RLOGE("sendBurstDtmfResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4969    }
4970
4971    return 0;
4972}
4973
4974int radio::sendCdmaSmsResponse(int slotId,
4975                              int responseType, int serial, RIL_Errno e, void *response,
4976                              size_t responseLen) {
4977#if VDBG
4978    RLOGD("sendCdmaSmsResponse: serial %d", serial);
4979#endif
4980
4981    if (radioService[slotId]->mRadioResponse != NULL) {
4982        RadioResponseInfo responseInfo = {};
4983        SendSmsResult result = makeSendSmsResult(responseInfo, serial, responseType, e, response,
4984                responseLen);
4985
4986        Return<void> retStatus
4987                = radioService[slotId]->mRadioResponse->sendCdmaSmsResponse(responseInfo, result);
4988        radioService[slotId]->checkReturnStatus(retStatus);
4989    } else {
4990        RLOGE("sendCdmaSmsResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4991    }
4992
4993    return 0;
4994}
4995
4996int radio::acknowledgeLastIncomingCdmaSmsResponse(int slotId,
4997                                                 int responseType, int serial, RIL_Errno e,
4998                                                 void *response, size_t responseLen) {
4999#if VDBG
5000    RLOGD("acknowledgeLastIncomingCdmaSmsResponse: serial %d", serial);
5001#endif
5002
5003    if (radioService[slotId]->mRadioResponse != NULL) {
5004        RadioResponseInfo responseInfo = {};
5005        populateResponseInfo(responseInfo, serial, responseType, e);
5006        Return<void> retStatus
5007                = radioService[slotId]->mRadioResponse->acknowledgeLastIncomingCdmaSmsResponse(
5008                responseInfo);
5009        radioService[slotId]->checkReturnStatus(retStatus);
5010    } else {
5011        RLOGE("acknowledgeLastIncomingCdmaSmsResponse: radioService[%d]->mRadioResponse "
5012                "== NULL", slotId);
5013    }
5014
5015    return 0;
5016}
5017
5018int radio::getGsmBroadcastConfigResponse(int slotId,
5019                                        int responseType, int serial, RIL_Errno e,
5020                                        void *response, size_t responseLen) {
5021#if VDBG
5022    RLOGD("getGsmBroadcastConfigResponse: serial %d", serial);
5023#endif
5024
5025    if (radioService[slotId]->mRadioResponse != NULL) {
5026        RadioResponseInfo responseInfo = {};
5027        populateResponseInfo(responseInfo, serial, responseType, e);
5028        hidl_vec<GsmBroadcastSmsConfigInfo> configs;
5029
5030        if (response == NULL || responseLen % sizeof(RIL_GSM_BroadcastSmsConfigInfo *) != 0) {
5031            RLOGE("getGsmBroadcastConfigResponse Invalid response: NULL");
5032            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5033        } else {
5034            int num = responseLen / sizeof(RIL_GSM_BroadcastSmsConfigInfo *);
5035            configs.resize(num);
5036            for (int i = 0 ; i < num; i++) {
5037                RIL_GSM_BroadcastSmsConfigInfo *resp =
5038                        ((RIL_GSM_BroadcastSmsConfigInfo **) response)[i];
5039                configs[i].fromServiceId = resp->fromServiceId;
5040                configs[i].toServiceId = resp->toServiceId;
5041                configs[i].fromCodeScheme = resp->fromCodeScheme;
5042                configs[i].toCodeScheme = resp->toCodeScheme;
5043                configs[i].selected = resp->selected == 1 ? true : false;
5044            }
5045        }
5046
5047        Return<void> retStatus
5048                = radioService[slotId]->mRadioResponse->getGsmBroadcastConfigResponse(responseInfo,
5049                configs);
5050        radioService[slotId]->checkReturnStatus(retStatus);
5051    } else {
5052        RLOGE("getGsmBroadcastConfigResponse: radioService[%d]->mRadioResponse == NULL",
5053                slotId);
5054    }
5055
5056    return 0;
5057}
5058
5059int radio::setGsmBroadcastConfigResponse(int slotId,
5060                                        int responseType, int serial, RIL_Errno e,
5061                                        void *response, size_t responseLen) {
5062#if VDBG
5063    RLOGD("setGsmBroadcastConfigResponse: serial %d", serial);
5064#endif
5065
5066    if (radioService[slotId]->mRadioResponse != NULL) {
5067        RadioResponseInfo responseInfo = {};
5068        populateResponseInfo(responseInfo, serial, responseType, e);
5069        Return<void> retStatus
5070                = radioService[slotId]->mRadioResponse->setGsmBroadcastConfigResponse(responseInfo);
5071        radioService[slotId]->checkReturnStatus(retStatus);
5072    } else {
5073        RLOGE("setGsmBroadcastConfigResponse: radioService[%d]->mRadioResponse == NULL",
5074                slotId);
5075    }
5076
5077    return 0;
5078}
5079
5080int radio::setGsmBroadcastActivationResponse(int slotId,
5081                                            int responseType, int serial, RIL_Errno e,
5082                                            void *response, size_t responseLen) {
5083#if VDBG
5084    RLOGD("setGsmBroadcastActivationResponse: serial %d", serial);
5085#endif
5086
5087    if (radioService[slotId]->mRadioResponse != NULL) {
5088        RadioResponseInfo responseInfo = {};
5089        populateResponseInfo(responseInfo, serial, responseType, e);
5090        Return<void> retStatus
5091                = radioService[slotId]->mRadioResponse->setGsmBroadcastActivationResponse(
5092                responseInfo);
5093        radioService[slotId]->checkReturnStatus(retStatus);
5094    } else {
5095        RLOGE("setGsmBroadcastActivationResponse: radioService[%d]->mRadioResponse == NULL",
5096                slotId);
5097    }
5098
5099    return 0;
5100}
5101
5102int radio::getCdmaBroadcastConfigResponse(int slotId,
5103                                         int responseType, int serial, RIL_Errno e,
5104                                         void *response, size_t responseLen) {
5105#if VDBG
5106    RLOGD("getCdmaBroadcastConfigResponse: serial %d", serial);
5107#endif
5108
5109    if (radioService[slotId]->mRadioResponse != NULL) {
5110        RadioResponseInfo responseInfo = {};
5111        populateResponseInfo(responseInfo, serial, responseType, e);
5112        hidl_vec<CdmaBroadcastSmsConfigInfo> configs;
5113
5114        if (response == NULL || responseLen % sizeof(RIL_CDMA_BroadcastSmsConfigInfo *) != 0) {
5115            RLOGE("getCdmaBroadcastConfigResponse Invalid response: NULL");
5116            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5117        } else {
5118            int num = responseLen / sizeof(RIL_CDMA_BroadcastSmsConfigInfo *);
5119            configs.resize(num);
5120            for (int i = 0 ; i < num; i++) {
5121                RIL_CDMA_BroadcastSmsConfigInfo *resp =
5122                        ((RIL_CDMA_BroadcastSmsConfigInfo **) response)[i];
5123                configs[i].serviceCategory = resp->service_category;
5124                configs[i].language = resp->language;
5125                configs[i].selected = resp->selected == 1 ? true : false;
5126            }
5127        }
5128
5129        Return<void> retStatus
5130                = radioService[slotId]->mRadioResponse->getCdmaBroadcastConfigResponse(responseInfo,
5131                configs);
5132        radioService[slotId]->checkReturnStatus(retStatus);
5133    } else {
5134        RLOGE("getCdmaBroadcastConfigResponse: radioService[%d]->mRadioResponse == NULL",
5135                slotId);
5136    }
5137
5138    return 0;
5139}
5140
5141int radio::setCdmaBroadcastConfigResponse(int slotId,
5142                                         int responseType, int serial, RIL_Errno e,
5143                                         void *response, size_t responseLen) {
5144#if VDBG
5145    RLOGD("setCdmaBroadcastConfigResponse: serial %d", serial);
5146#endif
5147
5148    if (radioService[slotId]->mRadioResponse != NULL) {
5149        RadioResponseInfo responseInfo = {};
5150        populateResponseInfo(responseInfo, serial, responseType, e);
5151        Return<void> retStatus
5152                = radioService[slotId]->mRadioResponse->setCdmaBroadcastConfigResponse(
5153                responseInfo);
5154        radioService[slotId]->checkReturnStatus(retStatus);
5155    } else {
5156        RLOGE("setCdmaBroadcastConfigResponse: radioService[%d]->mRadioResponse == NULL",
5157                slotId);
5158    }
5159
5160    return 0;
5161}
5162
5163int radio::setCdmaBroadcastActivationResponse(int slotId,
5164                                             int responseType, int serial, RIL_Errno e,
5165                                             void *response, size_t responseLen) {
5166#if VDBG
5167    RLOGD("setCdmaBroadcastActivationResponse: serial %d", serial);
5168#endif
5169
5170    if (radioService[slotId]->mRadioResponse != NULL) {
5171        RadioResponseInfo responseInfo = {};
5172        populateResponseInfo(responseInfo, serial, responseType, e);
5173        Return<void> retStatus
5174                = radioService[slotId]->mRadioResponse->setCdmaBroadcastActivationResponse(
5175                responseInfo);
5176        radioService[slotId]->checkReturnStatus(retStatus);
5177    } else {
5178        RLOGE("setCdmaBroadcastActivationResponse: radioService[%d]->mRadioResponse == NULL",
5179                slotId);
5180    }
5181
5182    return 0;
5183}
5184
5185int radio::getCDMASubscriptionResponse(int slotId,
5186                                      int responseType, int serial, RIL_Errno e, void *response,
5187                                      size_t responseLen) {
5188#if VDBG
5189    RLOGD("getCDMASubscriptionResponse: serial %d", serial);
5190#endif
5191
5192    if (radioService[slotId]->mRadioResponse != NULL) {
5193        RadioResponseInfo responseInfo = {};
5194        populateResponseInfo(responseInfo, serial, responseType, e);
5195
5196        int numStrings = responseLen / sizeof(char *);
5197        hidl_string emptyString;
5198        if (response == NULL || numStrings != 5) {
5199            RLOGE("getOperatorResponse Invalid response: NULL");
5200            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5201            Return<void> retStatus
5202                    = radioService[slotId]->mRadioResponse->getCDMASubscriptionResponse(
5203                    responseInfo, emptyString, emptyString, emptyString, emptyString, emptyString);
5204            radioService[slotId]->checkReturnStatus(retStatus);
5205        } else {
5206            char **resp = (char **) response;
5207            Return<void> retStatus
5208                    = radioService[slotId]->mRadioResponse->getCDMASubscriptionResponse(
5209                    responseInfo,
5210                    convertCharPtrToHidlString(resp[0]),
5211                    convertCharPtrToHidlString(resp[1]),
5212                    convertCharPtrToHidlString(resp[2]),
5213                    convertCharPtrToHidlString(resp[3]),
5214                    convertCharPtrToHidlString(resp[4]));
5215            radioService[slotId]->checkReturnStatus(retStatus);
5216        }
5217    } else {
5218        RLOGE("getCDMASubscriptionResponse: radioService[%d]->mRadioResponse == NULL",
5219                slotId);
5220    }
5221
5222    return 0;
5223}
5224
5225int radio::writeSmsToRuimResponse(int slotId,
5226                                 int responseType, int serial, RIL_Errno e,
5227                                 void *response, size_t responseLen) {
5228#if VDBG
5229    RLOGD("writeSmsToRuimResponse: serial %d", serial);
5230#endif
5231
5232    if (radioService[slotId]->mRadioResponse != NULL) {
5233        RadioResponseInfo responseInfo = {};
5234        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
5235        Return<void> retStatus
5236                = radioService[slotId]->mRadioResponse->writeSmsToRuimResponse(responseInfo, ret);
5237        radioService[slotId]->checkReturnStatus(retStatus);
5238    } else {
5239        RLOGE("writeSmsToRuimResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5240    }
5241
5242    return 0;
5243}
5244
5245int radio::deleteSmsOnRuimResponse(int slotId,
5246                                  int responseType, int serial, RIL_Errno e,
5247                                  void *response, size_t responseLen) {
5248#if VDBG
5249    RLOGD("deleteSmsOnRuimResponse: serial %d", serial);
5250#endif
5251
5252    if (radioService[slotId]->mRadioResponse != NULL) {
5253        RadioResponseInfo responseInfo = {};
5254        populateResponseInfo(responseInfo, serial, responseType, e);
5255        Return<void> retStatus
5256                = radioService[slotId]->mRadioResponse->deleteSmsOnRuimResponse(responseInfo);
5257        radioService[slotId]->checkReturnStatus(retStatus);
5258    } else {
5259        RLOGE("deleteSmsOnRuimResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5260    }
5261
5262    return 0;
5263}
5264
5265int radio::getDeviceIdentityResponse(int slotId,
5266                                    int responseType, int serial, RIL_Errno e, void *response,
5267                                    size_t responseLen) {
5268#if VDBG
5269    RLOGD("getDeviceIdentityResponse: serial %d", serial);
5270#endif
5271
5272    if (radioService[slotId]->mRadioResponse != NULL) {
5273        RadioResponseInfo responseInfo = {};
5274        populateResponseInfo(responseInfo, serial, responseType, e);
5275
5276        int numStrings = responseLen / sizeof(char *);
5277        hidl_string emptyString;
5278        if (response == NULL || numStrings != 4) {
5279            RLOGE("getDeviceIdentityResponse Invalid response: NULL");
5280            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5281            Return<void> retStatus
5282                    = radioService[slotId]->mRadioResponse->getDeviceIdentityResponse(responseInfo,
5283                    emptyString, emptyString, emptyString, emptyString);
5284            radioService[slotId]->checkReturnStatus(retStatus);
5285        } else {
5286            char **resp = (char **) response;
5287            Return<void> retStatus
5288                    = radioService[slotId]->mRadioResponse->getDeviceIdentityResponse(responseInfo,
5289                    convertCharPtrToHidlString(resp[0]),
5290                    convertCharPtrToHidlString(resp[1]),
5291                    convertCharPtrToHidlString(resp[2]),
5292                    convertCharPtrToHidlString(resp[3]));
5293            radioService[slotId]->checkReturnStatus(retStatus);
5294        }
5295    } else {
5296        RLOGE("getDeviceIdentityResponse: radioService[%d]->mRadioResponse == NULL",
5297                slotId);
5298    }
5299
5300    return 0;
5301}
5302
5303int radio::exitEmergencyCallbackModeResponse(int slotId,
5304                                            int responseType, int serial, RIL_Errno e,
5305                                            void *response, size_t responseLen) {
5306#if VDBG
5307    RLOGD("exitEmergencyCallbackModeResponse: serial %d", serial);
5308#endif
5309
5310    if (radioService[slotId]->mRadioResponse != NULL) {
5311        RadioResponseInfo responseInfo = {};
5312        populateResponseInfo(responseInfo, serial, responseType, e);
5313        Return<void> retStatus
5314                = radioService[slotId]->mRadioResponse->exitEmergencyCallbackModeResponse(
5315                responseInfo);
5316        radioService[slotId]->checkReturnStatus(retStatus);
5317    } else {
5318        RLOGE("exitEmergencyCallbackModeResponse: radioService[%d]->mRadioResponse == NULL",
5319                slotId);
5320    }
5321
5322    return 0;
5323}
5324
5325int radio::getSmscAddressResponse(int slotId,
5326                                  int responseType, int serial, RIL_Errno e,
5327                                  void *response, size_t responseLen) {
5328#if VDBG
5329    RLOGD("getSmscAddressResponse: serial %d", serial);
5330#endif
5331
5332    if (radioService[slotId]->mRadioResponse != NULL) {
5333        RadioResponseInfo responseInfo = {};
5334        populateResponseInfo(responseInfo, serial, responseType, e);
5335        Return<void> retStatus
5336                = radioService[slotId]->mRadioResponse->getSmscAddressResponse(responseInfo,
5337                convertCharPtrToHidlString((char *) response));
5338        radioService[slotId]->checkReturnStatus(retStatus);
5339    } else {
5340        RLOGE("getSmscAddressResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5341    }
5342
5343    return 0;
5344}
5345
5346int radio::setSmscAddressResponse(int slotId,
5347                                             int responseType, int serial, RIL_Errno e,
5348                                             void *response, size_t responseLen) {
5349#if VDBG
5350    RLOGD("setSmscAddressResponse: serial %d", serial);
5351#endif
5352
5353    if (radioService[slotId]->mRadioResponse != NULL) {
5354        RadioResponseInfo responseInfo = {};
5355        populateResponseInfo(responseInfo, serial, responseType, e);
5356        Return<void> retStatus
5357                = radioService[slotId]->mRadioResponse->setSmscAddressResponse(responseInfo);
5358        radioService[slotId]->checkReturnStatus(retStatus);
5359    } else {
5360        RLOGE("setSmscAddressResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5361    }
5362
5363    return 0;
5364}
5365
5366int radio::reportSmsMemoryStatusResponse(int slotId,
5367                                        int responseType, int serial, RIL_Errno e,
5368                                        void *response, size_t responseLen) {
5369#if VDBG
5370    RLOGD("reportSmsMemoryStatusResponse: serial %d", serial);
5371#endif
5372
5373    if (radioService[slotId]->mRadioResponse != NULL) {
5374        RadioResponseInfo responseInfo = {};
5375        populateResponseInfo(responseInfo, serial, responseType, e);
5376        Return<void> retStatus
5377                = radioService[slotId]->mRadioResponse->reportSmsMemoryStatusResponse(responseInfo);
5378        radioService[slotId]->checkReturnStatus(retStatus);
5379    } else {
5380        RLOGE("reportSmsMemoryStatusResponse: radioService[%d]->mRadioResponse == NULL",
5381                slotId);
5382    }
5383
5384    return 0;
5385}
5386
5387int radio::reportStkServiceIsRunningResponse(int slotId,
5388                                             int responseType, int serial, RIL_Errno e,
5389                                             void *response, size_t responseLen) {
5390#if VDBG
5391    RLOGD("reportStkServiceIsRunningResponse: serial %d", serial);
5392#endif
5393
5394    if (radioService[slotId]->mRadioResponse != NULL) {
5395        RadioResponseInfo responseInfo = {};
5396        populateResponseInfo(responseInfo, serial, responseType, e);
5397        Return<void> retStatus = radioService[slotId]->mRadioResponse->
5398                reportStkServiceIsRunningResponse(responseInfo);
5399        radioService[slotId]->checkReturnStatus(retStatus);
5400    } else {
5401        RLOGE("reportStkServiceIsRunningResponse: radioService[%d]->mRadioResponse == NULL",
5402                slotId);
5403    }
5404
5405    return 0;
5406}
5407
5408int radio::getCdmaSubscriptionSourceResponse(int slotId,
5409                                            int responseType, int serial, RIL_Errno e,
5410                                            void *response, size_t responseLen) {
5411#if VDBG
5412    RLOGD("getCdmaSubscriptionSourceResponse: serial %d", serial);
5413#endif
5414
5415    if (radioService[slotId]->mRadioResponse != NULL) {
5416        RadioResponseInfo responseInfo = {};
5417        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
5418        Return<void> retStatus
5419                = radioService[slotId]->mRadioResponse->getCdmaSubscriptionSourceResponse(
5420                responseInfo, (CdmaSubscriptionSource) ret);
5421        radioService[slotId]->checkReturnStatus(retStatus);
5422    } else {
5423        RLOGE("getCdmaSubscriptionSourceResponse: radioService[%d]->mRadioResponse == NULL",
5424                slotId);
5425    }
5426
5427    return 0;
5428}
5429
5430int radio::requestIsimAuthenticationResponse(int slotId,
5431                                            int responseType, int serial, RIL_Errno e,
5432                                            void *response, size_t responseLen) {
5433#if VDBG
5434    RLOGD("requestIsimAuthenticationResponse: serial %d", serial);
5435#endif
5436
5437    if (radioService[slotId]->mRadioResponse != NULL) {
5438        RadioResponseInfo responseInfo = {};
5439        populateResponseInfo(responseInfo, serial, responseType, e);
5440        Return<void> retStatus
5441                = radioService[slotId]->mRadioResponse->requestIsimAuthenticationResponse(
5442                responseInfo,
5443                convertCharPtrToHidlString((char *) response));
5444        radioService[slotId]->checkReturnStatus(retStatus);
5445    } else {
5446        RLOGE("requestIsimAuthenticationResponse: radioService[%d]->mRadioResponse == NULL",
5447                slotId);
5448    }
5449
5450    return 0;
5451}
5452
5453int radio::acknowledgeIncomingGsmSmsWithPduResponse(int slotId,
5454                                                   int responseType,
5455                                                   int serial, RIL_Errno e, void *response,
5456                                                   size_t responseLen) {
5457#if VDBG
5458    RLOGD("acknowledgeIncomingGsmSmsWithPduResponse: serial %d", serial);
5459#endif
5460
5461    if (radioService[slotId]->mRadioResponse != NULL) {
5462        RadioResponseInfo responseInfo = {};
5463        populateResponseInfo(responseInfo, serial, responseType, e);
5464        Return<void> retStatus
5465                = radioService[slotId]->mRadioResponse->acknowledgeIncomingGsmSmsWithPduResponse(
5466                responseInfo);
5467        radioService[slotId]->checkReturnStatus(retStatus);
5468    } else {
5469        RLOGE("acknowledgeIncomingGsmSmsWithPduResponse: radioService[%d]->mRadioResponse "
5470                "== NULL", slotId);
5471    }
5472
5473    return 0;
5474}
5475
5476int radio::sendEnvelopeWithStatusResponse(int slotId,
5477                                         int responseType, int serial, RIL_Errno e, void *response,
5478                                         size_t responseLen) {
5479#if VDBG
5480    RLOGD("sendEnvelopeWithStatusResponse: serial %d", serial);
5481#endif
5482
5483    if (radioService[slotId]->mRadioResponse != NULL) {
5484        RadioResponseInfo responseInfo = {};
5485        IccIoResult result = responseIccIo(responseInfo, serial, responseType, e,
5486                response, responseLen);
5487
5488        Return<void> retStatus
5489                = radioService[slotId]->mRadioResponse->sendEnvelopeWithStatusResponse(responseInfo,
5490                result);
5491        radioService[slotId]->checkReturnStatus(retStatus);
5492    } else {
5493        RLOGE("sendEnvelopeWithStatusResponse: radioService[%d]->mRadioResponse == NULL",
5494                slotId);
5495    }
5496
5497    return 0;
5498}
5499
5500int radio::getVoiceRadioTechnologyResponse(int slotId,
5501                                          int responseType, int serial, RIL_Errno e,
5502                                          void *response, size_t responseLen) {
5503#if VDBG
5504    RLOGD("getVoiceRadioTechnologyResponse: serial %d", serial);
5505#endif
5506
5507    if (radioService[slotId]->mRadioResponse != NULL) {
5508        RadioResponseInfo responseInfo = {};
5509        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
5510        Return<void> retStatus
5511                = radioService[slotId]->mRadioResponse->getVoiceRadioTechnologyResponse(
5512                responseInfo, (RadioTechnology) ret);
5513        radioService[slotId]->checkReturnStatus(retStatus);
5514    } else {
5515        RLOGE("getVoiceRadioTechnologyResponse: radioService[%d]->mRadioResponse == NULL",
5516                slotId);
5517    }
5518
5519    return 0;
5520}
5521
5522int radio::getCellInfoListResponse(int slotId,
5523                                   int responseType,
5524                                   int serial, RIL_Errno e, void *response,
5525                                   size_t responseLen) {
5526#if VDBG
5527    RLOGD("getCellInfoListResponse: serial %d", serial);
5528#endif
5529
5530    if (radioService[slotId]->mRadioResponse != NULL) {
5531        RadioResponseInfo responseInfo = {};
5532        populateResponseInfo(responseInfo, serial, responseType, e);
5533
5534        hidl_vec<CellInfo> ret;
5535        if (response == NULL || responseLen % sizeof(RIL_CellInfo_v12) != 0) {
5536            RLOGE("getCellInfoListResponse: Invalid response");
5537            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5538        } else {
5539            convertRilCellInfoListToHal(response, responseLen, ret);
5540        }
5541
5542        Return<void> retStatus = radioService[slotId]->mRadioResponse->getCellInfoListResponse(
5543                responseInfo, ret);
5544        radioService[slotId]->checkReturnStatus(retStatus);
5545    } else {
5546        RLOGE("getCellInfoListResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5547    }
5548
5549    return 0;
5550}
5551
5552int radio::setCellInfoListRateResponse(int slotId,
5553                                       int responseType,
5554                                       int serial, RIL_Errno e, void *response,
5555                                       size_t responseLen) {
5556#if VDBG
5557    RLOGD("setCellInfoListRateResponse: serial %d", serial);
5558#endif
5559
5560    if (radioService[slotId]->mRadioResponse != NULL) {
5561        RadioResponseInfo responseInfo = {};
5562        populateResponseInfo(responseInfo, serial, responseType, e);
5563        Return<void> retStatus
5564                = radioService[slotId]->mRadioResponse->setCellInfoListRateResponse(responseInfo);
5565        radioService[slotId]->checkReturnStatus(retStatus);
5566    } else {
5567        RLOGE("setCellInfoListRateResponse: radioService[%d]->mRadioResponse == NULL",
5568                slotId);
5569    }
5570
5571    return 0;
5572}
5573
5574int radio::setInitialAttachApnResponse(int slotId,
5575                                       int responseType, int serial, RIL_Errno e,
5576                                       void *response, size_t responseLen) {
5577#if VDBG
5578    RLOGD("setInitialAttachApnResponse: serial %d", serial);
5579#endif
5580
5581    if (radioService[slotId]->mRadioResponse != NULL) {
5582        RadioResponseInfo responseInfo = {};
5583        populateResponseInfo(responseInfo, serial, responseType, e);
5584        Return<void> retStatus
5585                = radioService[slotId]->mRadioResponse->setInitialAttachApnResponse(responseInfo);
5586        radioService[slotId]->checkReturnStatus(retStatus);
5587    } else {
5588        RLOGE("setInitialAttachApnResponse: radioService[%d]->mRadioResponse == NULL",
5589                slotId);
5590    }
5591
5592    return 0;
5593}
5594
5595int radio::getImsRegistrationStateResponse(int slotId,
5596                                           int responseType, int serial, RIL_Errno e,
5597                                           void *response, size_t responseLen) {
5598#if VDBG
5599    RLOGD("getImsRegistrationStateResponse: serial %d", serial);
5600#endif
5601
5602    if (radioService[slotId]->mRadioResponse != NULL) {
5603        RadioResponseInfo responseInfo = {};
5604        populateResponseInfo(responseInfo, serial, responseType, e);
5605        bool isRegistered = false;
5606        int ratFamily = 0;
5607        int numInts = responseLen / sizeof(int);
5608        if (response == NULL || numInts != 2) {
5609            RLOGE("getImsRegistrationStateResponse Invalid response: NULL");
5610            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5611        } else {
5612            int *pInt = (int *) response;
5613            isRegistered = pInt[0] == 1 ? true : false;
5614            ratFamily = pInt[1];
5615        }
5616        Return<void> retStatus
5617                = radioService[slotId]->mRadioResponse->getImsRegistrationStateResponse(
5618                responseInfo, isRegistered, (RadioTechnologyFamily) ratFamily);
5619        radioService[slotId]->checkReturnStatus(retStatus);
5620    } else {
5621        RLOGE("getImsRegistrationStateResponse: radioService[%d]->mRadioResponse == NULL",
5622                slotId);
5623    }
5624
5625    return 0;
5626}
5627
5628int radio::sendImsSmsResponse(int slotId,
5629                              int responseType, int serial, RIL_Errno e, void *response,
5630                              size_t responseLen) {
5631#if VDBG
5632    RLOGD("sendImsSmsResponse: serial %d", serial);
5633#endif
5634
5635    if (radioService[slotId]->mRadioResponse != NULL) {
5636        RadioResponseInfo responseInfo = {};
5637        SendSmsResult result = makeSendSmsResult(responseInfo, serial, responseType, e, response,
5638                responseLen);
5639
5640        Return<void> retStatus
5641                = radioService[slotId]->mRadioResponse->sendImsSmsResponse(responseInfo, result);
5642        radioService[slotId]->checkReturnStatus(retStatus);
5643    } else {
5644        RLOGE("sendSmsResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5645    }
5646
5647    return 0;
5648}
5649
5650int radio::iccTransmitApduBasicChannelResponse(int slotId,
5651                                               int responseType, int serial, RIL_Errno e,
5652                                               void *response, size_t responseLen) {
5653#if VDBG
5654    RLOGD("iccTransmitApduBasicChannelResponse: serial %d", serial);
5655#endif
5656
5657    if (radioService[slotId]->mRadioResponse != NULL) {
5658        RadioResponseInfo responseInfo = {};
5659        IccIoResult result = responseIccIo(responseInfo, serial, responseType, e, response,
5660                responseLen);
5661
5662        Return<void> retStatus
5663                = radioService[slotId]->mRadioResponse->iccTransmitApduBasicChannelResponse(
5664                responseInfo, result);
5665        radioService[slotId]->checkReturnStatus(retStatus);
5666    } else {
5667        RLOGE("iccTransmitApduBasicChannelResponse: radioService[%d]->mRadioResponse "
5668                "== NULL", slotId);
5669    }
5670
5671    return 0;
5672}
5673
5674int radio::iccOpenLogicalChannelResponse(int slotId,
5675                                         int responseType, int serial, RIL_Errno e, void *response,
5676                                         size_t responseLen) {
5677#if VDBG
5678    RLOGD("iccOpenLogicalChannelResponse: serial %d", serial);
5679#endif
5680
5681    if (radioService[slotId]->mRadioResponse != NULL) {
5682        RadioResponseInfo responseInfo = {};
5683        populateResponseInfo(responseInfo, serial, responseType, e);
5684        int channelId = -1;
5685        hidl_vec<int8_t> selectResponse;
5686        int numInts = responseLen / sizeof(int);
5687        if (response == NULL || responseLen % sizeof(int) != 0) {
5688            RLOGE("iccOpenLogicalChannelResponse Invalid response: NULL");
5689            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5690        } else {
5691            int *pInt = (int *) response;
5692            channelId = pInt[0];
5693            selectResponse.resize(numInts - 1);
5694            for (int i = 1; i < numInts; i++) {
5695                selectResponse[i - 1] = (int8_t) pInt[i];
5696            }
5697        }
5698        Return<void> retStatus
5699                = radioService[slotId]->mRadioResponse->iccOpenLogicalChannelResponse(responseInfo,
5700                channelId, selectResponse);
5701        radioService[slotId]->checkReturnStatus(retStatus);
5702    } else {
5703        RLOGE("iccOpenLogicalChannelResponse: radioService[%d]->mRadioResponse == NULL",
5704                slotId);
5705    }
5706
5707    return 0;
5708}
5709
5710int radio::iccCloseLogicalChannelResponse(int slotId,
5711                                          int responseType, int serial, RIL_Errno e,
5712                                          void *response, size_t responseLen) {
5713#if VDBG
5714    RLOGD("iccCloseLogicalChannelResponse: serial %d", serial);
5715#endif
5716
5717    if (radioService[slotId]->mRadioResponse != NULL) {
5718        RadioResponseInfo responseInfo = {};
5719        populateResponseInfo(responseInfo, serial, responseType, e);
5720        Return<void> retStatus
5721                = radioService[slotId]->mRadioResponse->iccCloseLogicalChannelResponse(
5722                responseInfo);
5723        radioService[slotId]->checkReturnStatus(retStatus);
5724    } else {
5725        RLOGE("iccCloseLogicalChannelResponse: radioService[%d]->mRadioResponse == NULL",
5726                slotId);
5727    }
5728
5729    return 0;
5730}
5731
5732int radio::iccTransmitApduLogicalChannelResponse(int slotId,
5733                                                 int responseType, int serial, RIL_Errno e,
5734                                                 void *response, size_t responseLen) {
5735#if VDBG
5736    RLOGD("iccTransmitApduLogicalChannelResponse: serial %d", serial);
5737#endif
5738
5739    if (radioService[slotId]->mRadioResponse != NULL) {
5740        RadioResponseInfo responseInfo = {};
5741        IccIoResult result = responseIccIo(responseInfo, serial, responseType, e, response,
5742                responseLen);
5743
5744        Return<void> retStatus
5745                = radioService[slotId]->mRadioResponse->iccTransmitApduLogicalChannelResponse(
5746                responseInfo, result);
5747        radioService[slotId]->checkReturnStatus(retStatus);
5748    } else {
5749        RLOGE("iccTransmitApduLogicalChannelResponse: radioService[%d]->mRadioResponse "
5750                "== NULL", slotId);
5751    }
5752
5753    return 0;
5754}
5755
5756int radio::nvReadItemResponse(int slotId,
5757                              int responseType, int serial, RIL_Errno e,
5758                              void *response, size_t responseLen) {
5759#if VDBG
5760    RLOGD("nvReadItemResponse: serial %d", serial);
5761#endif
5762
5763    if (radioService[slotId]->mRadioResponse != NULL) {
5764        RadioResponseInfo responseInfo = {};
5765        populateResponseInfo(responseInfo, serial, responseType, e);
5766        Return<void> retStatus = radioService[slotId]->mRadioResponse->nvReadItemResponse(
5767                responseInfo,
5768                convertCharPtrToHidlString((char *) response));
5769        radioService[slotId]->checkReturnStatus(retStatus);
5770    } else {
5771        RLOGE("nvReadItemResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5772    }
5773
5774    return 0;
5775}
5776
5777int radio::nvWriteItemResponse(int slotId,
5778                               int responseType, int serial, RIL_Errno e,
5779                               void *response, size_t responseLen) {
5780#if VDBG
5781    RLOGD("nvWriteItemResponse: serial %d", serial);
5782#endif
5783
5784    if (radioService[slotId]->mRadioResponse != NULL) {
5785        RadioResponseInfo responseInfo = {};
5786        populateResponseInfo(responseInfo, serial, responseType, e);
5787        Return<void> retStatus
5788                = radioService[slotId]->mRadioResponse->nvWriteItemResponse(responseInfo);
5789        radioService[slotId]->checkReturnStatus(retStatus);
5790    } else {
5791        RLOGE("nvWriteItemResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5792    }
5793
5794    return 0;
5795}
5796
5797int radio::nvWriteCdmaPrlResponse(int slotId,
5798                                  int responseType, int serial, RIL_Errno e,
5799                                  void *response, size_t responseLen) {
5800#if VDBG
5801    RLOGD("nvWriteCdmaPrlResponse: serial %d", serial);
5802#endif
5803
5804    if (radioService[slotId]->mRadioResponse != NULL) {
5805        RadioResponseInfo responseInfo = {};
5806        populateResponseInfo(responseInfo, serial, responseType, e);
5807        Return<void> retStatus
5808                = radioService[slotId]->mRadioResponse->nvWriteCdmaPrlResponse(responseInfo);
5809        radioService[slotId]->checkReturnStatus(retStatus);
5810    } else {
5811        RLOGE("nvWriteCdmaPrlResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5812    }
5813
5814    return 0;
5815}
5816
5817int radio::nvResetConfigResponse(int slotId,
5818                                 int responseType, int serial, RIL_Errno e,
5819                                 void *response, size_t responseLen) {
5820#if VDBG
5821    RLOGD("nvResetConfigResponse: serial %d", serial);
5822#endif
5823
5824    if (radioService[slotId]->mRadioResponse != NULL) {
5825        RadioResponseInfo responseInfo = {};
5826        populateResponseInfo(responseInfo, serial, responseType, e);
5827        Return<void> retStatus
5828                = radioService[slotId]->mRadioResponse->nvResetConfigResponse(responseInfo);
5829        radioService[slotId]->checkReturnStatus(retStatus);
5830    } else {
5831        RLOGE("nvResetConfigResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5832    }
5833
5834    return 0;
5835}
5836
5837int radio::setUiccSubscriptionResponse(int slotId,
5838                                       int responseType, int serial, RIL_Errno e,
5839                                       void *response, size_t responseLen) {
5840#if VDBG
5841    RLOGD("setUiccSubscriptionResponse: serial %d", serial);
5842#endif
5843
5844    if (radioService[slotId]->mRadioResponse != NULL) {
5845        RadioResponseInfo responseInfo = {};
5846        populateResponseInfo(responseInfo, serial, responseType, e);
5847        Return<void> retStatus
5848                = radioService[slotId]->mRadioResponse->setUiccSubscriptionResponse(responseInfo);
5849        radioService[slotId]->checkReturnStatus(retStatus);
5850    } else {
5851        RLOGE("setUiccSubscriptionResponse: radioService[%d]->mRadioResponse == NULL",
5852                slotId);
5853    }
5854
5855    return 0;
5856}
5857
5858int radio::setDataAllowedResponse(int slotId,
5859                                  int responseType, int serial, RIL_Errno e,
5860                                  void *response, size_t responseLen) {
5861#if VDBG
5862    RLOGD("setDataAllowedResponse: serial %d", serial);
5863#endif
5864
5865    if (radioService[slotId]->mRadioResponse != NULL) {
5866        RadioResponseInfo responseInfo = {};
5867        populateResponseInfo(responseInfo, serial, responseType, e);
5868        Return<void> retStatus
5869                = radioService[slotId]->mRadioResponse->setDataAllowedResponse(responseInfo);
5870        radioService[slotId]->checkReturnStatus(retStatus);
5871    } else {
5872        RLOGE("setDataAllowedResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5873    }
5874
5875    return 0;
5876}
5877
5878int radio::getHardwareConfigResponse(int slotId,
5879                                     int responseType, int serial, RIL_Errno e,
5880                                     void *response, size_t responseLen) {
5881#if VDBG
5882    RLOGD("getHardwareConfigResponse: serial %d", serial);
5883#endif
5884
5885    if (radioService[slotId]->mRadioResponse != NULL) {
5886        RadioResponseInfo responseInfo = {};
5887        populateResponseInfo(responseInfo, serial, responseType, e);
5888
5889        hidl_vec<HardwareConfig> result;
5890        if (response == NULL || responseLen % sizeof(RIL_HardwareConfig) != 0) {
5891            RLOGE("hardwareConfigChangedInd: invalid response");
5892            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5893        } else {
5894            convertRilHardwareConfigListToHal(response, responseLen, result);
5895        }
5896
5897        Return<void> retStatus = radioService[slotId]->mRadioResponse->getHardwareConfigResponse(
5898                responseInfo, result);
5899        radioService[slotId]->checkReturnStatus(retStatus);
5900    } else {
5901        RLOGE("getHardwareConfigResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5902    }
5903
5904    return 0;
5905}
5906
5907int radio::requestIccSimAuthenticationResponse(int slotId,
5908                                               int responseType, int serial, RIL_Errno e,
5909                                               void *response, size_t responseLen) {
5910#if VDBG
5911    RLOGD("requestIccSimAuthenticationResponse: serial %d", serial);
5912#endif
5913
5914    if (radioService[slotId]->mRadioResponse != NULL) {
5915        RadioResponseInfo responseInfo = {};
5916        IccIoResult result = responseIccIo(responseInfo, serial, responseType, e, response,
5917                responseLen);
5918
5919        Return<void> retStatus
5920                = radioService[slotId]->mRadioResponse->requestIccSimAuthenticationResponse(
5921                responseInfo, result);
5922        radioService[slotId]->checkReturnStatus(retStatus);
5923    } else {
5924        RLOGE("requestIccSimAuthenticationResponse: radioService[%d]->mRadioResponse "
5925                "== NULL", slotId);
5926    }
5927
5928    return 0;
5929}
5930
5931int radio::setDataProfileResponse(int slotId,
5932                                  int responseType, int serial, RIL_Errno e,
5933                                  void *response, size_t responseLen) {
5934#if VDBG
5935    RLOGD("setDataProfileResponse: serial %d", serial);
5936#endif
5937
5938    if (radioService[slotId]->mRadioResponse != NULL) {
5939        RadioResponseInfo responseInfo = {};
5940        populateResponseInfo(responseInfo, serial, responseType, e);
5941        Return<void> retStatus
5942                = radioService[slotId]->mRadioResponse->setDataProfileResponse(responseInfo);
5943        radioService[slotId]->checkReturnStatus(retStatus);
5944    } else {
5945        RLOGE("setDataProfileResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5946    }
5947
5948    return 0;
5949}
5950
5951int radio::requestShutdownResponse(int slotId,
5952                                  int responseType, int serial, RIL_Errno e,
5953                                  void *response, size_t responseLen) {
5954#if VDBG
5955    RLOGD("requestShutdownResponse: serial %d", serial);
5956#endif
5957
5958    if (radioService[slotId]->mRadioResponse != NULL) {
5959        RadioResponseInfo responseInfo = {};
5960        populateResponseInfo(responseInfo, serial, responseType, e);
5961        Return<void> retStatus
5962                = radioService[slotId]->mRadioResponse->requestShutdownResponse(responseInfo);
5963        radioService[slotId]->checkReturnStatus(retStatus);
5964    } else {
5965        RLOGE("requestShutdownResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5966    }
5967
5968    return 0;
5969}
5970
5971void responseRadioCapability(RadioResponseInfo& responseInfo, int serial,
5972        int responseType, RIL_Errno e, void *response, size_t responseLen, RadioCapability& rc) {
5973    populateResponseInfo(responseInfo, serial, responseType, e);
5974
5975    if (response == NULL || responseLen != sizeof(RIL_RadioCapability)) {
5976        RLOGE("responseRadioCapability: Invalid response");
5977        if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5978        rc.logicalModemUuid = hidl_string();
5979    } else {
5980        convertRilRadioCapabilityToHal(response, responseLen, rc);
5981    }
5982}
5983
5984int radio::getRadioCapabilityResponse(int slotId,
5985                                     int responseType, int serial, RIL_Errno e,
5986                                     void *response, size_t responseLen) {
5987#if VDBG
5988    RLOGD("getRadioCapabilityResponse: serial %d", serial);
5989#endif
5990
5991    if (radioService[slotId]->mRadioResponse != NULL) {
5992        RadioResponseInfo responseInfo = {};
5993        RadioCapability result = {};
5994        responseRadioCapability(responseInfo, serial, responseType, e, response, responseLen,
5995                result);
5996        Return<void> retStatus = radioService[slotId]->mRadioResponse->getRadioCapabilityResponse(
5997                responseInfo, result);
5998        radioService[slotId]->checkReturnStatus(retStatus);
5999    } else {
6000        RLOGE("getRadioCapabilityResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6001    }
6002
6003    return 0;
6004}
6005
6006int radio::setRadioCapabilityResponse(int slotId,
6007                                     int responseType, int serial, RIL_Errno e,
6008                                     void *response, size_t responseLen) {
6009#if VDBG
6010    RLOGD("setRadioCapabilityResponse: serial %d", serial);
6011#endif
6012
6013    if (radioService[slotId]->mRadioResponse != NULL) {
6014        RadioResponseInfo responseInfo = {};
6015        RadioCapability result = {};
6016        responseRadioCapability(responseInfo, serial, responseType, e, response, responseLen,
6017                result);
6018        Return<void> retStatus = radioService[slotId]->mRadioResponse->setRadioCapabilityResponse(
6019                responseInfo, result);
6020        radioService[slotId]->checkReturnStatus(retStatus);
6021    } else {
6022        RLOGE("setRadioCapabilityResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6023    }
6024
6025    return 0;
6026}
6027
6028LceStatusInfo responseLceStatusInfo(RadioResponseInfo& responseInfo, int serial, int responseType,
6029                                    RIL_Errno e, void *response, size_t responseLen) {
6030    populateResponseInfo(responseInfo, serial, responseType, e);
6031    LceStatusInfo result = {};
6032
6033    if (response == NULL || responseLen != sizeof(RIL_LceStatusInfo)) {
6034        RLOGE("Invalid response: NULL");
6035        if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6036    } else {
6037        RIL_LceStatusInfo *resp = (RIL_LceStatusInfo *) response;
6038        result.lceStatus = (LceStatus) resp->lce_status;
6039        result.actualIntervalMs = (uint8_t) resp->actual_interval_ms;
6040    }
6041    return result;
6042}
6043
6044int radio::startLceServiceResponse(int slotId,
6045                                   int responseType, int serial, RIL_Errno e,
6046                                   void *response, size_t responseLen) {
6047#if VDBG
6048    RLOGD("startLceServiceResponse: serial %d", serial);
6049#endif
6050
6051    if (radioService[slotId]->mRadioResponse != NULL) {
6052        RadioResponseInfo responseInfo = {};
6053        LceStatusInfo result = responseLceStatusInfo(responseInfo, serial, responseType, e,
6054                response, responseLen);
6055
6056        Return<void> retStatus
6057                = radioService[slotId]->mRadioResponse->startLceServiceResponse(responseInfo,
6058                result);
6059        radioService[slotId]->checkReturnStatus(retStatus);
6060    } else {
6061        RLOGE("startLceServiceResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6062    }
6063
6064    return 0;
6065}
6066
6067int radio::stopLceServiceResponse(int slotId,
6068                                  int responseType, int serial, RIL_Errno e,
6069                                  void *response, size_t responseLen) {
6070#if VDBG
6071    RLOGD("stopLceServiceResponse: serial %d", serial);
6072#endif
6073
6074    if (radioService[slotId]->mRadioResponse != NULL) {
6075        RadioResponseInfo responseInfo = {};
6076        LceStatusInfo result = responseLceStatusInfo(responseInfo, serial, responseType, e,
6077                response, responseLen);
6078
6079        Return<void> retStatus
6080                = radioService[slotId]->mRadioResponse->stopLceServiceResponse(responseInfo,
6081                result);
6082        radioService[slotId]->checkReturnStatus(retStatus);
6083    } else {
6084        RLOGE("stopLceServiceResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6085    }
6086
6087    return 0;
6088}
6089
6090int radio::pullLceDataResponse(int slotId,
6091                               int responseType, int serial, RIL_Errno e,
6092                               void *response, size_t responseLen) {
6093#if VDBG
6094    RLOGD("pullLceDataResponse: serial %d", serial);
6095#endif
6096
6097    if (radioService[slotId]->mRadioResponse != NULL) {
6098        RadioResponseInfo responseInfo = {};
6099        populateResponseInfo(responseInfo, serial, responseType, e);
6100
6101        LceDataInfo result = {};
6102        if (response == NULL || responseLen != sizeof(RIL_LceDataInfo)) {
6103            RLOGE("pullLceDataResponse: Invalid response");
6104            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6105        } else {
6106            convertRilLceDataInfoToHal(response, responseLen, result);
6107        }
6108
6109        Return<void> retStatus = radioService[slotId]->mRadioResponse->pullLceDataResponse(
6110                responseInfo, result);
6111        radioService[slotId]->checkReturnStatus(retStatus);
6112    } else {
6113        RLOGE("pullLceDataResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6114    }
6115
6116    return 0;
6117}
6118
6119int radio::getModemActivityInfoResponse(int slotId,
6120                                        int responseType, int serial, RIL_Errno e,
6121                                        void *response, size_t responseLen) {
6122#if VDBG
6123    RLOGD("getModemActivityInfoResponse: serial %d", serial);
6124#endif
6125
6126    if (radioService[slotId]->mRadioResponse != NULL) {
6127        RadioResponseInfo responseInfo = {};
6128        populateResponseInfo(responseInfo, serial, responseType, e);
6129        ActivityStatsInfo info;
6130        if (response == NULL || responseLen != sizeof(RIL_ActivityStatsInfo)) {
6131            RLOGE("getModemActivityInfoResponse Invalid response: NULL");
6132            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6133        } else {
6134            RIL_ActivityStatsInfo *resp = (RIL_ActivityStatsInfo *)response;
6135            info.sleepModeTimeMs = resp->sleep_mode_time_ms;
6136            info.idleModeTimeMs = resp->idle_mode_time_ms;
6137            for(int i = 0; i < RIL_NUM_TX_POWER_LEVELS; i++) {
6138                info.txmModetimeMs[i] = resp->tx_mode_time_ms[i];
6139            }
6140            info.rxModeTimeMs = resp->rx_mode_time_ms;
6141        }
6142
6143        Return<void> retStatus
6144                = radioService[slotId]->mRadioResponse->getModemActivityInfoResponse(responseInfo,
6145                info);
6146        radioService[slotId]->checkReturnStatus(retStatus);
6147    } else {
6148        RLOGE("getModemActivityInfoResponse: radioService[%d]->mRadioResponse == NULL",
6149                slotId);
6150    }
6151
6152    return 0;
6153}
6154
6155int radio::setAllowedCarriersResponse(int slotId,
6156                                      int responseType, int serial, RIL_Errno e,
6157                                      void *response, size_t responseLen) {
6158#if VDBG
6159    RLOGD("setAllowedCarriersResponse: serial %d", serial);
6160#endif
6161
6162    if (radioService[slotId]->mRadioResponse != NULL) {
6163        RadioResponseInfo responseInfo = {};
6164        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
6165        Return<void> retStatus
6166                = radioService[slotId]->mRadioResponse->setAllowedCarriersResponse(responseInfo,
6167                ret);
6168        radioService[slotId]->checkReturnStatus(retStatus);
6169    } else {
6170        RLOGE("setAllowedCarriersResponse: radioService[%d]->mRadioResponse == NULL",
6171                slotId);
6172    }
6173
6174    return 0;
6175}
6176
6177int radio::getAllowedCarriersResponse(int slotId,
6178                                      int responseType, int serial, RIL_Errno e,
6179                                      void *response, size_t responseLen) {
6180#if VDBG
6181    RLOGD("getAllowedCarriersResponse: serial %d", serial);
6182#endif
6183
6184    if (radioService[slotId]->mRadioResponse != NULL) {
6185        RadioResponseInfo responseInfo = {};
6186        populateResponseInfo(responseInfo, serial, responseType, e);
6187        CarrierRestrictions carrierInfo = {};
6188        bool allAllowed = true;
6189        if (response == NULL) {
6190#if VDBG
6191            RLOGD("getAllowedCarriersResponse response is NULL: all allowed");
6192#endif
6193            carrierInfo.allowedCarriers.resize(0);
6194            carrierInfo.excludedCarriers.resize(0);
6195        } else if (responseLen != sizeof(RIL_CarrierRestrictions)) {
6196            RLOGE("getAllowedCarriersResponse Invalid response");
6197            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6198        } else {
6199            RIL_CarrierRestrictions *pCr = (RIL_CarrierRestrictions *)response;
6200            if (pCr->len_allowed_carriers > 0 || pCr->len_excluded_carriers > 0) {
6201                allAllowed = false;
6202            }
6203
6204            carrierInfo.allowedCarriers.resize(pCr->len_allowed_carriers);
6205            for(int i = 0; i < pCr->len_allowed_carriers; i++) {
6206                RIL_Carrier *carrier = pCr->allowed_carriers + i;
6207                carrierInfo.allowedCarriers[i].mcc = convertCharPtrToHidlString(carrier->mcc);
6208                carrierInfo.allowedCarriers[i].mnc = convertCharPtrToHidlString(carrier->mnc);
6209                carrierInfo.allowedCarriers[i].matchType = (CarrierMatchType) carrier->match_type;
6210                carrierInfo.allowedCarriers[i].matchData =
6211                        convertCharPtrToHidlString(carrier->match_data);
6212            }
6213
6214            carrierInfo.excludedCarriers.resize(pCr->len_excluded_carriers);
6215            for(int i = 0; i < pCr->len_excluded_carriers; i++) {
6216                RIL_Carrier *carrier = pCr->excluded_carriers + i;
6217                carrierInfo.excludedCarriers[i].mcc = convertCharPtrToHidlString(carrier->mcc);
6218                carrierInfo.excludedCarriers[i].mnc = convertCharPtrToHidlString(carrier->mnc);
6219                carrierInfo.excludedCarriers[i].matchType = (CarrierMatchType) carrier->match_type;
6220                carrierInfo.excludedCarriers[i].matchData =
6221                        convertCharPtrToHidlString(carrier->match_data);
6222            }
6223        }
6224
6225        Return<void> retStatus
6226                = radioService[slotId]->mRadioResponse->getAllowedCarriersResponse(responseInfo,
6227                allAllowed, carrierInfo);
6228        radioService[slotId]->checkReturnStatus(retStatus);
6229    } else {
6230        RLOGE("getAllowedCarriersResponse: radioService[%d]->mRadioResponse == NULL",
6231                slotId);
6232    }
6233
6234    return 0;
6235}
6236
6237int radio::sendDeviceStateResponse(int slotId,
6238                              int responseType, int serial, RIL_Errno e,
6239                              void *response, size_t responselen) {
6240#if VDBG
6241    RLOGD("sendDeviceStateResponse: serial %d", serial);
6242#endif
6243
6244    if (radioService[slotId]->mRadioResponse != NULL) {
6245        RadioResponseInfo responseInfo = {};
6246        populateResponseInfo(responseInfo, serial, responseType, e);
6247        Return<void> retStatus
6248                = radioService[slotId]->mRadioResponse->sendDeviceStateResponse(responseInfo);
6249        radioService[slotId]->checkReturnStatus(retStatus);
6250    } else {
6251        RLOGE("sendDeviceStateResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6252    }
6253
6254    return 0;
6255}
6256
6257int radio::setCarrierInfoForImsiEncryptionResponse(int slotId,
6258                               int responseType, int serial, RIL_Errno e,
6259                               void *response, size_t responseLen) {
6260    RLOGD("setCarrierInfoForImsiEncryptionResponse: serial %d", serial);
6261    if (radioService[slotId]->mRadioResponse != NULL) {
6262        RadioResponseInfo responseInfo = {};
6263        populateResponseInfo(responseInfo, serial, responseType, e);
6264        Return<sp<::android::hardware::radio::V1_1::IRadioResponse>> ret =
6265            ::android::hardware::radio::V1_1::IRadioResponse::castFrom(
6266            radioService[slotId]->mRadioResponse);
6267        if (ret.isOk()) {
6268            sp<::android::hardware::radio::V1_1::IRadioResponse> radioResponseV1_1 = ret;
6269            Return<void> retStatus
6270                   = radioResponseV1_1->setCarrierInfoForImsiEncryptionResponse(responseInfo);
6271            radioService[slotId]->checkReturnStatus(retStatus);
6272        } else {
6273            RLOGE("setCarrierInfoForImsiEncryptionResponse: ret.isOk() == false for "
6274                    "radioService[%d]" , slotId);
6275        }
6276    } else {
6277        RLOGE("setCarrierInfoForImsiEncryptionResponse: radioService[%d]->mRadioResponse == NULL",
6278                slotId);
6279    }
6280    return 0;
6281}
6282
6283int radio::setIndicationFilterResponse(int slotId,
6284                              int responseType, int serial, RIL_Errno e,
6285                              void *response, size_t responselen) {
6286#if VDBG
6287    RLOGD("setIndicationFilterResponse: serial %d", serial);
6288#endif
6289
6290    if (radioService[slotId]->mRadioResponse != NULL) {
6291        RadioResponseInfo responseInfo = {};
6292        populateResponseInfo(responseInfo, serial, responseType, e);
6293        Return<void> retStatus
6294                = radioService[slotId]->mRadioResponse->setIndicationFilterResponse(responseInfo);
6295        radioService[slotId]->checkReturnStatus(retStatus);
6296    } else {
6297        RLOGE("setIndicationFilterResponse: radioService[%d]->mRadioResponse == NULL",
6298                slotId);
6299    }
6300
6301    return 0;
6302}
6303
6304
6305int radio::setSimCardPowerResponse(int slotId,
6306                                   int responseType, int serial, RIL_Errno e,
6307                                   void *response, size_t responseLen) {
6308#if VDBG
6309    RLOGD("setSimCardPowerResponse: serial %d", serial);
6310#endif
6311
6312    if (radioService[slotId]->mRadioResponse != NULL) {
6313        RadioResponseInfo responseInfo = {};
6314        populateResponseInfo(responseInfo, serial, responseType, e);
6315        Return<void> retStatus
6316                = radioService[slotId]->mRadioResponse->setSimCardPowerResponse(responseInfo);
6317        radioService[slotId]->checkReturnStatus(retStatus);
6318    } else {
6319        RLOGE("setSimCardPowerResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6320    }
6321
6322    return 0;
6323}
6324
6325int radio::sendRequestRawResponse(int slotId,
6326                                  int responseType, int serial, RIL_Errno e,
6327                                  void *response, size_t responseLen) {
6328#if VDBG
6329   RLOGD("sendRequestRawResponse: serial %d", serial);
6330#endif
6331
6332    if (oemHookService[slotId]->mOemHookResponse != NULL) {
6333        RadioResponseInfo responseInfo = {};
6334        populateResponseInfo(responseInfo, serial, responseType, e);
6335        hidl_vec<uint8_t> data;
6336
6337        if (response == NULL) {
6338            RLOGE("sendRequestRawResponse: Invalid response");
6339            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6340        } else {
6341            data.setToExternal((uint8_t *) response, responseLen);
6342        }
6343        Return<void> retStatus = oemHookService[slotId]->mOemHookResponse->
6344                sendRequestRawResponse(responseInfo, data);
6345        checkReturnStatus(slotId, retStatus, false);
6346    } else {
6347        RLOGE("sendRequestRawResponse: oemHookService[%d]->mOemHookResponse == NULL",
6348                slotId);
6349    }
6350
6351    return 0;
6352}
6353
6354int radio::sendRequestStringsResponse(int slotId,
6355                                      int responseType, int serial, RIL_Errno e,
6356                                      void *response, size_t responseLen) {
6357#if VDBG
6358    RLOGD("sendRequestStringsResponse: serial %d", serial);
6359#endif
6360
6361    if (oemHookService[slotId]->mOemHookResponse != NULL) {
6362        RadioResponseInfo responseInfo = {};
6363        populateResponseInfo(responseInfo, serial, responseType, e);
6364        hidl_vec<hidl_string> data;
6365
6366        if (response == NULL || responseLen % sizeof(char *) != 0) {
6367            RLOGE("sendRequestStringsResponse Invalid response: NULL");
6368            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6369        } else {
6370            char **resp = (char **) response;
6371            int numStrings = responseLen / sizeof(char *);
6372            data.resize(numStrings);
6373            for (int i = 0; i < numStrings; i++) {
6374                data[i] = convertCharPtrToHidlString(resp[i]);
6375            }
6376        }
6377        Return<void> retStatus
6378                = oemHookService[slotId]->mOemHookResponse->sendRequestStringsResponse(
6379                responseInfo, data);
6380        checkReturnStatus(slotId, retStatus, false);
6381    } else {
6382        RLOGE("sendRequestStringsResponse: oemHookService[%d]->mOemHookResponse == "
6383                "NULL", slotId);
6384    }
6385
6386    return 0;
6387}
6388
6389// Radio Indication functions
6390
6391RadioIndicationType convertIntToRadioIndicationType(int indicationType) {
6392    return indicationType == RESPONSE_UNSOLICITED ? (RadioIndicationType::UNSOLICITED) :
6393            (RadioIndicationType::UNSOLICITED_ACK_EXP);
6394}
6395
6396int radio::radioStateChangedInd(int slotId,
6397                                 int indicationType, int token, RIL_Errno e, void *response,
6398                                 size_t responseLen) {
6399    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6400        RadioState radioState = (RadioState) s_vendorFunctions->onStateRequest();
6401        RLOGD("radioStateChangedInd: radioState %d", radioState);
6402        Return<void> retStatus = radioService[slotId]->mRadioIndication->radioStateChanged(
6403                convertIntToRadioIndicationType(indicationType), radioState);
6404        radioService[slotId]->checkReturnStatus(retStatus);
6405    } else {
6406        RLOGE("radioStateChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6407    }
6408
6409    return 0;
6410}
6411
6412int radio::callStateChangedInd(int slotId,
6413                               int indicationType, int token, RIL_Errno e, void *response,
6414                               size_t responseLen) {
6415    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6416#if VDBG
6417        RLOGD("callStateChangedInd");
6418#endif
6419        Return<void> retStatus = radioService[slotId]->mRadioIndication->callStateChanged(
6420                convertIntToRadioIndicationType(indicationType));
6421        radioService[slotId]->checkReturnStatus(retStatus);
6422    } else {
6423        RLOGE("callStateChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6424    }
6425
6426    return 0;
6427}
6428
6429int radio::networkStateChangedInd(int slotId,
6430                                  int indicationType, int token, RIL_Errno e, void *response,
6431                                  size_t responseLen) {
6432    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6433#if VDBG
6434        RLOGD("networkStateChangedInd");
6435#endif
6436        Return<void> retStatus = radioService[slotId]->mRadioIndication->networkStateChanged(
6437                convertIntToRadioIndicationType(indicationType));
6438        radioService[slotId]->checkReturnStatus(retStatus);
6439    } else {
6440        RLOGE("networkStateChangedInd: radioService[%d]->mRadioIndication == NULL",
6441                slotId);
6442    }
6443
6444    return 0;
6445}
6446
6447uint8_t hexCharToInt(uint8_t c) {
6448    if (c >= '0' && c <= '9') return (c - '0');
6449    if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
6450    if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
6451
6452    return INVALID_HEX_CHAR;
6453}
6454
6455uint8_t * convertHexStringToBytes(void *response, size_t responseLen) {
6456    if (responseLen % 2 != 0) {
6457        return NULL;
6458    }
6459
6460    uint8_t *bytes = (uint8_t *)calloc(responseLen/2, sizeof(uint8_t));
6461    if (bytes == NULL) {
6462        RLOGE("convertHexStringToBytes: cannot allocate memory for bytes string");
6463        return NULL;
6464    }
6465    uint8_t *hexString = (uint8_t *)response;
6466
6467    for (size_t i = 0; i < responseLen; i += 2) {
6468        uint8_t hexChar1 = hexCharToInt(hexString[i]);
6469        uint8_t hexChar2 = hexCharToInt(hexString[i + 1]);
6470
6471        if (hexChar1 == INVALID_HEX_CHAR || hexChar2 == INVALID_HEX_CHAR) {
6472            RLOGE("convertHexStringToBytes: invalid hex char %d %d",
6473                    hexString[i], hexString[i + 1]);
6474            free(bytes);
6475            return NULL;
6476        }
6477        bytes[i/2] = ((hexChar1 << 4) | hexChar2);
6478    }
6479
6480    return bytes;
6481}
6482
6483int radio::newSmsInd(int slotId, int indicationType,
6484                     int token, RIL_Errno e, void *response, size_t responseLen) {
6485    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6486        if (response == NULL || responseLen == 0) {
6487            RLOGE("newSmsInd: invalid response");
6488            return 0;
6489        }
6490
6491        uint8_t *bytes = convertHexStringToBytes(response, responseLen);
6492        if (bytes == NULL) {
6493            RLOGE("newSmsInd: convertHexStringToBytes failed");
6494            return 0;
6495        }
6496
6497        hidl_vec<uint8_t> pdu;
6498        pdu.setToExternal(bytes, responseLen/2);
6499#if VDBG
6500        RLOGD("newSmsInd");
6501#endif
6502        Return<void> retStatus = radioService[slotId]->mRadioIndication->newSms(
6503                convertIntToRadioIndicationType(indicationType), pdu);
6504        radioService[slotId]->checkReturnStatus(retStatus);
6505        free(bytes);
6506    } else {
6507        RLOGE("newSmsInd: radioService[%d]->mRadioIndication == NULL", slotId);
6508    }
6509
6510    return 0;
6511}
6512
6513int radio::newSmsStatusReportInd(int slotId,
6514                                 int indicationType, int token, RIL_Errno e, void *response,
6515                                 size_t responseLen) {
6516    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6517        if (response == NULL || responseLen == 0) {
6518            RLOGE("newSmsStatusReportInd: invalid response");
6519            return 0;
6520        }
6521
6522        uint8_t *bytes = convertHexStringToBytes(response, responseLen);
6523        if (bytes == NULL) {
6524            RLOGE("newSmsStatusReportInd: convertHexStringToBytes failed");
6525            return 0;
6526        }
6527
6528        hidl_vec<uint8_t> pdu;
6529        pdu.setToExternal(bytes, responseLen/2);
6530#if VDBG
6531        RLOGD("newSmsStatusReportInd");
6532#endif
6533        Return<void> retStatus = radioService[slotId]->mRadioIndication->newSmsStatusReport(
6534                convertIntToRadioIndicationType(indicationType), pdu);
6535        radioService[slotId]->checkReturnStatus(retStatus);
6536        free(bytes);
6537    } else {
6538        RLOGE("newSmsStatusReportInd: radioService[%d]->mRadioIndication == NULL", slotId);
6539    }
6540
6541    return 0;
6542}
6543
6544int radio::newSmsOnSimInd(int slotId, int indicationType,
6545                          int token, RIL_Errno e, void *response, size_t responseLen) {
6546    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6547        if (response == NULL || responseLen != sizeof(int)) {
6548            RLOGE("newSmsOnSimInd: invalid response");
6549            return 0;
6550        }
6551        int32_t recordNumber = ((int32_t *) response)[0];
6552#if VDBG
6553        RLOGD("newSmsOnSimInd: slotIndex %d", recordNumber);
6554#endif
6555        Return<void> retStatus = radioService[slotId]->mRadioIndication->newSmsOnSim(
6556                convertIntToRadioIndicationType(indicationType), recordNumber);
6557        radioService[slotId]->checkReturnStatus(retStatus);
6558    } else {
6559        RLOGE("newSmsOnSimInd: radioService[%d]->mRadioIndication == NULL", slotId);
6560    }
6561
6562    return 0;
6563}
6564
6565int radio::onUssdInd(int slotId, int indicationType,
6566                     int token, RIL_Errno e, void *response, size_t responseLen) {
6567    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6568        if (response == NULL || responseLen != 2 * sizeof(char *)) {
6569            RLOGE("onUssdInd: invalid response");
6570            return 0;
6571        }
6572        char **strings = (char **) response;
6573        char *mode = strings[0];
6574        hidl_string msg = convertCharPtrToHidlString(strings[1]);
6575        UssdModeType modeType = (UssdModeType) atoi(mode);
6576#if VDBG
6577        RLOGD("onUssdInd: mode %s", mode);
6578#endif
6579        Return<void> retStatus = radioService[slotId]->mRadioIndication->onUssd(
6580                convertIntToRadioIndicationType(indicationType), modeType, msg);
6581        radioService[slotId]->checkReturnStatus(retStatus);
6582    } else {
6583        RLOGE("onUssdInd: radioService[%d]->mRadioIndication == NULL", slotId);
6584    }
6585
6586    return 0;
6587}
6588
6589int radio::nitzTimeReceivedInd(int slotId,
6590                               int indicationType, int token, RIL_Errno e, void *response,
6591                               size_t responseLen) {
6592    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6593        if (response == NULL || responseLen == 0) {
6594            RLOGE("nitzTimeReceivedInd: invalid response");
6595            return 0;
6596        }
6597        hidl_string nitzTime = convertCharPtrToHidlString((char *) response);
6598        int64_t timeReceived = android::elapsedRealtime();
6599#if VDBG
6600        RLOGD("nitzTimeReceivedInd: nitzTime %s receivedTime %" PRId64, nitzTime.c_str(),
6601                timeReceived);
6602#endif
6603        Return<void> retStatus = radioService[slotId]->mRadioIndication->nitzTimeReceived(
6604                convertIntToRadioIndicationType(indicationType), nitzTime, timeReceived);
6605        radioService[slotId]->checkReturnStatus(retStatus);
6606    } else {
6607        RLOGE("nitzTimeReceivedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6608        return -1;
6609    }
6610
6611    return 0;
6612}
6613
6614void convertRilSignalStrengthToHal(void *response, size_t responseLen,
6615        SignalStrength& signalStrength) {
6616    RIL_SignalStrength_v10 *rilSignalStrength = (RIL_SignalStrength_v10 *) response;
6617
6618    // Fixup LTE for backwards compatibility
6619    // signalStrength: -1 -> 99
6620    if (rilSignalStrength->LTE_SignalStrength.signalStrength == -1) {
6621        rilSignalStrength->LTE_SignalStrength.signalStrength = 99;
6622    }
6623    // rsrp: -1 -> INT_MAX all other negative value to positive.
6624    // So remap here
6625    if (rilSignalStrength->LTE_SignalStrength.rsrp == -1) {
6626        rilSignalStrength->LTE_SignalStrength.rsrp = INT_MAX;
6627    } else if (rilSignalStrength->LTE_SignalStrength.rsrp < -1) {
6628        rilSignalStrength->LTE_SignalStrength.rsrp = -rilSignalStrength->LTE_SignalStrength.rsrp;
6629    }
6630    // rsrq: -1 -> INT_MAX
6631    if (rilSignalStrength->LTE_SignalStrength.rsrq == -1) {
6632        rilSignalStrength->LTE_SignalStrength.rsrq = INT_MAX;
6633    }
6634    // Not remapping rssnr is already using INT_MAX
6635    // cqi: -1 -> INT_MAX
6636    if (rilSignalStrength->LTE_SignalStrength.cqi == -1) {
6637        rilSignalStrength->LTE_SignalStrength.cqi = INT_MAX;
6638    }
6639
6640    signalStrength.gw.signalStrength = rilSignalStrength->GW_SignalStrength.signalStrength;
6641    signalStrength.gw.bitErrorRate = rilSignalStrength->GW_SignalStrength.bitErrorRate;
6642    signalStrength.cdma.dbm = rilSignalStrength->CDMA_SignalStrength.dbm;
6643    signalStrength.cdma.ecio = rilSignalStrength->CDMA_SignalStrength.ecio;
6644    signalStrength.evdo.dbm = rilSignalStrength->EVDO_SignalStrength.dbm;
6645    signalStrength.evdo.ecio = rilSignalStrength->EVDO_SignalStrength.ecio;
6646    signalStrength.evdo.signalNoiseRatio =
6647            rilSignalStrength->EVDO_SignalStrength.signalNoiseRatio;
6648    signalStrength.lte.signalStrength = rilSignalStrength->LTE_SignalStrength.signalStrength;
6649    signalStrength.lte.rsrp = rilSignalStrength->LTE_SignalStrength.rsrp;
6650    signalStrength.lte.rsrq = rilSignalStrength->LTE_SignalStrength.rsrq;
6651    signalStrength.lte.rssnr = rilSignalStrength->LTE_SignalStrength.rssnr;
6652    signalStrength.lte.cqi = rilSignalStrength->LTE_SignalStrength.cqi;
6653    signalStrength.lte.timingAdvance = rilSignalStrength->LTE_SignalStrength.timingAdvance;
6654    signalStrength.tdScdma.rscp = rilSignalStrength->TD_SCDMA_SignalStrength.rscp;
6655}
6656
6657int radio::currentSignalStrengthInd(int slotId,
6658                                    int indicationType, int token, RIL_Errno e,
6659                                    void *response, size_t responseLen) {
6660    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6661        if (response == NULL || responseLen != sizeof(RIL_SignalStrength_v10)) {
6662            RLOGE("currentSignalStrengthInd: invalid response");
6663            return 0;
6664        }
6665
6666        SignalStrength signalStrength = {};
6667        convertRilSignalStrengthToHal(response, responseLen, signalStrength);
6668
6669#if VDBG
6670        RLOGD("currentSignalStrengthInd");
6671#endif
6672        Return<void> retStatus = radioService[slotId]->mRadioIndication->currentSignalStrength(
6673                convertIntToRadioIndicationType(indicationType), signalStrength);
6674        radioService[slotId]->checkReturnStatus(retStatus);
6675    } else {
6676        RLOGE("currentSignalStrengthInd: radioService[%d]->mRadioIndication == NULL",
6677                slotId);
6678    }
6679
6680    return 0;
6681}
6682
6683void convertRilDataCallToHal(RIL_Data_Call_Response_v11 *dcResponse,
6684        SetupDataCallResult& dcResult) {
6685    dcResult.status = (DataCallFailCause) dcResponse->status;
6686    dcResult.suggestedRetryTime = dcResponse->suggestedRetryTime;
6687    dcResult.cid = dcResponse->cid;
6688    dcResult.active = dcResponse->active;
6689    dcResult.type = convertCharPtrToHidlString(dcResponse->type);
6690    dcResult.ifname = convertCharPtrToHidlString(dcResponse->ifname);
6691    dcResult.addresses = convertCharPtrToHidlString(dcResponse->addresses);
6692    dcResult.dnses = convertCharPtrToHidlString(dcResponse->dnses);
6693    dcResult.gateways = convertCharPtrToHidlString(dcResponse->gateways);
6694    dcResult.pcscf = convertCharPtrToHidlString(dcResponse->pcscf);
6695    dcResult.mtu = dcResponse->mtu;
6696}
6697
6698void convertRilDataCallListToHal(void *response, size_t responseLen,
6699        hidl_vec<SetupDataCallResult>& dcResultList) {
6700    int num = responseLen / sizeof(RIL_Data_Call_Response_v11);
6701
6702    RIL_Data_Call_Response_v11 *dcResponse = (RIL_Data_Call_Response_v11 *) response;
6703    dcResultList.resize(num);
6704    for (int i = 0; i < num; i++) {
6705        convertRilDataCallToHal(&dcResponse[i], dcResultList[i]);
6706    }
6707}
6708
6709int radio::dataCallListChangedInd(int slotId,
6710                                  int indicationType, int token, RIL_Errno e, void *response,
6711                                  size_t responseLen) {
6712    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6713        if (response == NULL || responseLen % sizeof(RIL_Data_Call_Response_v11) != 0) {
6714            RLOGE("dataCallListChangedInd: invalid response");
6715            return 0;
6716        }
6717        hidl_vec<SetupDataCallResult> dcList;
6718        convertRilDataCallListToHal(response, responseLen, dcList);
6719#if VDBG
6720        RLOGD("dataCallListChangedInd");
6721#endif
6722        Return<void> retStatus = radioService[slotId]->mRadioIndication->dataCallListChanged(
6723                convertIntToRadioIndicationType(indicationType), dcList);
6724        radioService[slotId]->checkReturnStatus(retStatus);
6725    } else {
6726        RLOGE("dataCallListChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6727    }
6728
6729    return 0;
6730}
6731
6732int radio::suppSvcNotifyInd(int slotId, int indicationType,
6733                            int token, RIL_Errno e, void *response, size_t responseLen) {
6734    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6735        if (response == NULL || responseLen != sizeof(RIL_SuppSvcNotification)) {
6736            RLOGE("suppSvcNotifyInd: invalid response");
6737            return 0;
6738        }
6739
6740        SuppSvcNotification suppSvc = {};
6741        RIL_SuppSvcNotification *ssn = (RIL_SuppSvcNotification *) response;
6742        suppSvc.isMT = ssn->notificationType;
6743        suppSvc.code = ssn->code;
6744        suppSvc.index = ssn->index;
6745        suppSvc.type = ssn->type;
6746        suppSvc.number = convertCharPtrToHidlString(ssn->number);
6747
6748#if VDBG
6749        RLOGD("suppSvcNotifyInd: isMT %d code %d index %d type %d",
6750                suppSvc.isMT, suppSvc.code, suppSvc.index, suppSvc.type);
6751#endif
6752        Return<void> retStatus = radioService[slotId]->mRadioIndication->suppSvcNotify(
6753                convertIntToRadioIndicationType(indicationType), suppSvc);
6754        radioService[slotId]->checkReturnStatus(retStatus);
6755    } else {
6756        RLOGE("suppSvcNotifyInd: radioService[%d]->mRadioIndication == NULL", slotId);
6757    }
6758
6759    return 0;
6760}
6761
6762int radio::stkSessionEndInd(int slotId, int indicationType,
6763                            int token, RIL_Errno e, void *response, size_t responseLen) {
6764    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6765#if VDBG
6766        RLOGD("stkSessionEndInd");
6767#endif
6768        Return<void> retStatus = radioService[slotId]->mRadioIndication->stkSessionEnd(
6769                convertIntToRadioIndicationType(indicationType));
6770        radioService[slotId]->checkReturnStatus(retStatus);
6771    } else {
6772        RLOGE("stkSessionEndInd: radioService[%d]->mRadioIndication == NULL", slotId);
6773    }
6774
6775    return 0;
6776}
6777
6778int radio::stkProactiveCommandInd(int slotId,
6779                                  int indicationType, int token, RIL_Errno e, void *response,
6780                                  size_t responseLen) {
6781    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6782        if (response == NULL || responseLen == 0) {
6783            RLOGE("stkProactiveCommandInd: invalid response");
6784            return 0;
6785        }
6786#if VDBG
6787        RLOGD("stkProactiveCommandInd");
6788#endif
6789        Return<void> retStatus = radioService[slotId]->mRadioIndication->stkProactiveCommand(
6790                convertIntToRadioIndicationType(indicationType),
6791                convertCharPtrToHidlString((char *) response));
6792        radioService[slotId]->checkReturnStatus(retStatus);
6793    } else {
6794        RLOGE("stkProactiveCommandInd: radioService[%d]->mRadioIndication == NULL", slotId);
6795    }
6796
6797    return 0;
6798}
6799
6800int radio::stkEventNotifyInd(int slotId, int indicationType,
6801                             int token, RIL_Errno e, void *response, size_t responseLen) {
6802    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6803        if (response == NULL || responseLen == 0) {
6804            RLOGE("stkEventNotifyInd: invalid response");
6805            return 0;
6806        }
6807#if VDBG
6808        RLOGD("stkEventNotifyInd");
6809#endif
6810        Return<void> retStatus = radioService[slotId]->mRadioIndication->stkEventNotify(
6811                convertIntToRadioIndicationType(indicationType),
6812                convertCharPtrToHidlString((char *) response));
6813        radioService[slotId]->checkReturnStatus(retStatus);
6814    } else {
6815        RLOGE("stkEventNotifyInd: radioService[%d]->mRadioIndication == NULL", slotId);
6816    }
6817
6818    return 0;
6819}
6820
6821int radio::stkCallSetupInd(int slotId, int indicationType,
6822                           int token, RIL_Errno e, void *response, size_t responseLen) {
6823    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6824        if (response == NULL || responseLen != sizeof(int)) {
6825            RLOGE("stkCallSetupInd: invalid response");
6826            return 0;
6827        }
6828        int32_t timeout = ((int32_t *) response)[0];
6829#if VDBG
6830        RLOGD("stkCallSetupInd: timeout %d", timeout);
6831#endif
6832        Return<void> retStatus = radioService[slotId]->mRadioIndication->stkCallSetup(
6833                convertIntToRadioIndicationType(indicationType), timeout);
6834        radioService[slotId]->checkReturnStatus(retStatus);
6835    } else {
6836        RLOGE("stkCallSetupInd: radioService[%d]->mRadioIndication == NULL", slotId);
6837    }
6838
6839    return 0;
6840}
6841
6842int radio::simSmsStorageFullInd(int slotId,
6843                                int indicationType, int token, RIL_Errno e, void *response,
6844                                size_t responseLen) {
6845    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6846#if VDBG
6847        RLOGD("simSmsStorageFullInd");
6848#endif
6849        Return<void> retStatus = radioService[slotId]->mRadioIndication->simSmsStorageFull(
6850                convertIntToRadioIndicationType(indicationType));
6851        radioService[slotId]->checkReturnStatus(retStatus);
6852    } else {
6853        RLOGE("simSmsStorageFullInd: radioService[%d]->mRadioIndication == NULL", slotId);
6854    }
6855
6856    return 0;
6857}
6858
6859int radio::simRefreshInd(int slotId, int indicationType,
6860                         int token, RIL_Errno e, void *response, size_t responseLen) {
6861    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6862        if (response == NULL || responseLen != sizeof(RIL_SimRefreshResponse_v7)) {
6863            RLOGE("simRefreshInd: invalid response");
6864            return 0;
6865        }
6866
6867        SimRefreshResult refreshResult = {};
6868        RIL_SimRefreshResponse_v7 *simRefreshResponse = ((RIL_SimRefreshResponse_v7 *) response);
6869        refreshResult.type =
6870                (android::hardware::radio::V1_0::SimRefreshType) simRefreshResponse->result;
6871        refreshResult.efId = simRefreshResponse->ef_id;
6872        refreshResult.aid = convertCharPtrToHidlString(simRefreshResponse->aid);
6873
6874#if VDBG
6875        RLOGD("simRefreshInd: type %d efId %d", refreshResult.type, refreshResult.efId);
6876#endif
6877        Return<void> retStatus = radioService[slotId]->mRadioIndication->simRefresh(
6878                convertIntToRadioIndicationType(indicationType), refreshResult);
6879        radioService[slotId]->checkReturnStatus(retStatus);
6880    } else {
6881        RLOGE("simRefreshInd: radioService[%d]->mRadioIndication == NULL", slotId);
6882    }
6883
6884    return 0;
6885}
6886
6887void convertRilCdmaSignalInfoRecordToHal(RIL_CDMA_SignalInfoRecord *signalInfoRecord,
6888        CdmaSignalInfoRecord& record) {
6889    record.isPresent = signalInfoRecord->isPresent;
6890    record.signalType = signalInfoRecord->signalType;
6891    record.alertPitch = signalInfoRecord->alertPitch;
6892    record.signal = signalInfoRecord->signal;
6893}
6894
6895int radio::callRingInd(int slotId, int indicationType,
6896                       int token, RIL_Errno e, void *response, size_t responseLen) {
6897    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6898        bool isGsm;
6899        CdmaSignalInfoRecord record = {};
6900        if (response == NULL || responseLen == 0) {
6901            isGsm = true;
6902        } else {
6903            isGsm = false;
6904            if (responseLen != sizeof (RIL_CDMA_SignalInfoRecord)) {
6905                RLOGE("callRingInd: invalid response");
6906                return 0;
6907            }
6908            convertRilCdmaSignalInfoRecordToHal((RIL_CDMA_SignalInfoRecord *) response, record);
6909        }
6910
6911#if VDBG
6912        RLOGD("callRingInd: isGsm %d", isGsm);
6913#endif
6914        Return<void> retStatus = radioService[slotId]->mRadioIndication->callRing(
6915                convertIntToRadioIndicationType(indicationType), isGsm, record);
6916        radioService[slotId]->checkReturnStatus(retStatus);
6917    } else {
6918        RLOGE("callRingInd: radioService[%d]->mRadioIndication == NULL", slotId);
6919    }
6920
6921    return 0;
6922}
6923
6924int radio::simStatusChangedInd(int slotId,
6925                               int indicationType, int token, RIL_Errno e, void *response,
6926                               size_t responseLen) {
6927    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6928#if VDBG
6929        RLOGD("simStatusChangedInd");
6930#endif
6931        Return<void> retStatus = radioService[slotId]->mRadioIndication->simStatusChanged(
6932                convertIntToRadioIndicationType(indicationType));
6933        radioService[slotId]->checkReturnStatus(retStatus);
6934    } else {
6935        RLOGE("simStatusChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6936    }
6937
6938    return 0;
6939}
6940
6941int radio::cdmaNewSmsInd(int slotId, int indicationType,
6942                         int token, RIL_Errno e, void *response, size_t responseLen) {
6943    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6944        if (response == NULL || responseLen != sizeof(RIL_CDMA_SMS_Message)) {
6945            RLOGE("cdmaNewSmsInd: invalid response");
6946            return 0;
6947        }
6948
6949        CdmaSmsMessage msg = {};
6950        RIL_CDMA_SMS_Message *rilMsg = (RIL_CDMA_SMS_Message *) response;
6951        msg.teleserviceId = rilMsg->uTeleserviceID;
6952        msg.isServicePresent = rilMsg->bIsServicePresent;
6953        msg.serviceCategory = rilMsg->uServicecategory;
6954        msg.address.digitMode =
6955                (android::hardware::radio::V1_0::CdmaSmsDigitMode) rilMsg->sAddress.digit_mode;
6956        msg.address.numberMode =
6957                (android::hardware::radio::V1_0::CdmaSmsNumberMode) rilMsg->sAddress.number_mode;
6958        msg.address.numberType =
6959                (android::hardware::radio::V1_0::CdmaSmsNumberType) rilMsg->sAddress.number_type;
6960        msg.address.numberPlan =
6961                (android::hardware::radio::V1_0::CdmaSmsNumberPlan) rilMsg->sAddress.number_plan;
6962
6963        int digitLimit = MIN((rilMsg->sAddress.number_of_digits), RIL_CDMA_SMS_ADDRESS_MAX);
6964        msg.address.digits.setToExternal(rilMsg->sAddress.digits, digitLimit);
6965
6966        msg.subAddress.subaddressType = (android::hardware::radio::V1_0::CdmaSmsSubaddressType)
6967                rilMsg->sSubAddress.subaddressType;
6968        msg.subAddress.odd = rilMsg->sSubAddress.odd;
6969
6970        digitLimit= MIN((rilMsg->sSubAddress.number_of_digits), RIL_CDMA_SMS_SUBADDRESS_MAX);
6971        msg.subAddress.digits.setToExternal(rilMsg->sSubAddress.digits, digitLimit);
6972
6973        digitLimit = MIN((rilMsg->uBearerDataLen), RIL_CDMA_SMS_BEARER_DATA_MAX);
6974        msg.bearerData.setToExternal(rilMsg->aBearerData, digitLimit);
6975
6976#if VDBG
6977        RLOGD("cdmaNewSmsInd");
6978#endif
6979        Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaNewSms(
6980                convertIntToRadioIndicationType(indicationType), msg);
6981        radioService[slotId]->checkReturnStatus(retStatus);
6982    } else {
6983        RLOGE("cdmaNewSmsInd: radioService[%d]->mRadioIndication == NULL", slotId);
6984    }
6985
6986    return 0;
6987}
6988
6989int radio::newBroadcastSmsInd(int slotId,
6990                              int indicationType, int token, RIL_Errno e, void *response,
6991                              size_t responseLen) {
6992    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6993        if (response == NULL || responseLen == 0) {
6994            RLOGE("newBroadcastSmsInd: invalid response");
6995            return 0;
6996        }
6997
6998        hidl_vec<uint8_t> data;
6999        data.setToExternal((uint8_t *) response, responseLen);
7000#if VDBG
7001        RLOGD("newBroadcastSmsInd");
7002#endif
7003        Return<void> retStatus = radioService[slotId]->mRadioIndication->newBroadcastSms(
7004                convertIntToRadioIndicationType(indicationType), data);
7005        radioService[slotId]->checkReturnStatus(retStatus);
7006    } else {
7007        RLOGE("newBroadcastSmsInd: radioService[%d]->mRadioIndication == NULL", slotId);
7008    }
7009
7010    return 0;
7011}
7012
7013int radio::cdmaRuimSmsStorageFullInd(int slotId,
7014                                     int indicationType, int token, RIL_Errno e, void *response,
7015                                     size_t responseLen) {
7016    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7017#if VDBG
7018        RLOGD("cdmaRuimSmsStorageFullInd");
7019#endif
7020        Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaRuimSmsStorageFull(
7021                convertIntToRadioIndicationType(indicationType));
7022        radioService[slotId]->checkReturnStatus(retStatus);
7023    } else {
7024        RLOGE("cdmaRuimSmsStorageFullInd: radioService[%d]->mRadioIndication == NULL",
7025                slotId);
7026    }
7027
7028    return 0;
7029}
7030
7031int radio::restrictedStateChangedInd(int slotId,
7032                                     int indicationType, int token, RIL_Errno e, void *response,
7033                                     size_t responseLen) {
7034    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7035        if (response == NULL || responseLen != sizeof(int)) {
7036            RLOGE("restrictedStateChangedInd: invalid response");
7037            return 0;
7038        }
7039        int32_t state = ((int32_t *) response)[0];
7040#if VDBG
7041        RLOGD("restrictedStateChangedInd: state %d", state);
7042#endif
7043        Return<void> retStatus = radioService[slotId]->mRadioIndication->restrictedStateChanged(
7044                convertIntToRadioIndicationType(indicationType), (PhoneRestrictedState) state);
7045        radioService[slotId]->checkReturnStatus(retStatus);
7046    } else {
7047        RLOGE("restrictedStateChangedInd: radioService[%d]->mRadioIndication == NULL",
7048                slotId);
7049    }
7050
7051    return 0;
7052}
7053
7054int radio::enterEmergencyCallbackModeInd(int slotId,
7055                                         int indicationType, int token, RIL_Errno e, void *response,
7056                                         size_t responseLen) {
7057    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7058#if VDBG
7059        RLOGD("enterEmergencyCallbackModeInd");
7060#endif
7061        Return<void> retStatus = radioService[slotId]->mRadioIndication->enterEmergencyCallbackMode(
7062                convertIntToRadioIndicationType(indicationType));
7063        radioService[slotId]->checkReturnStatus(retStatus);
7064    } else {
7065        RLOGE("enterEmergencyCallbackModeInd: radioService[%d]->mRadioIndication == NULL",
7066                slotId);
7067    }
7068
7069    return 0;
7070}
7071
7072int radio::cdmaCallWaitingInd(int slotId,
7073                              int indicationType, int token, RIL_Errno e, void *response,
7074                              size_t responseLen) {
7075    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7076        if (response == NULL || responseLen != sizeof(RIL_CDMA_CallWaiting_v6)) {
7077            RLOGE("cdmaCallWaitingInd: invalid response");
7078            return 0;
7079        }
7080
7081        CdmaCallWaiting callWaitingRecord = {};
7082        RIL_CDMA_CallWaiting_v6 *callWaitingRil = ((RIL_CDMA_CallWaiting_v6 *) response);
7083        callWaitingRecord.number = convertCharPtrToHidlString(callWaitingRil->number);
7084        callWaitingRecord.numberPresentation =
7085                (CdmaCallWaitingNumberPresentation) callWaitingRil->numberPresentation;
7086        callWaitingRecord.name = convertCharPtrToHidlString(callWaitingRil->name);
7087        convertRilCdmaSignalInfoRecordToHal(&callWaitingRil->signalInfoRecord,
7088                callWaitingRecord.signalInfoRecord);
7089        callWaitingRecord.numberType = (CdmaCallWaitingNumberType) callWaitingRil->number_type;
7090        callWaitingRecord.numberPlan = (CdmaCallWaitingNumberPlan) callWaitingRil->number_plan;
7091
7092#if VDBG
7093        RLOGD("cdmaCallWaitingInd");
7094#endif
7095        Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaCallWaiting(
7096                convertIntToRadioIndicationType(indicationType), callWaitingRecord);
7097        radioService[slotId]->checkReturnStatus(retStatus);
7098    } else {
7099        RLOGE("cdmaCallWaitingInd: radioService[%d]->mRadioIndication == NULL", slotId);
7100    }
7101
7102    return 0;
7103}
7104
7105int radio::cdmaOtaProvisionStatusInd(int slotId,
7106                                     int indicationType, int token, RIL_Errno e, void *response,
7107                                     size_t responseLen) {
7108    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7109        if (response == NULL || responseLen != sizeof(int)) {
7110            RLOGE("cdmaOtaProvisionStatusInd: invalid response");
7111            return 0;
7112        }
7113        int32_t status = ((int32_t *) response)[0];
7114#if VDBG
7115        RLOGD("cdmaOtaProvisionStatusInd: status %d", status);
7116#endif
7117        Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaOtaProvisionStatus(
7118                convertIntToRadioIndicationType(indicationType), (CdmaOtaProvisionStatus) status);
7119        radioService[slotId]->checkReturnStatus(retStatus);
7120    } else {
7121        RLOGE("cdmaOtaProvisionStatusInd: radioService[%d]->mRadioIndication == NULL",
7122                slotId);
7123    }
7124
7125    return 0;
7126}
7127
7128int radio::cdmaInfoRecInd(int slotId,
7129                          int indicationType, int token, RIL_Errno e, void *response,
7130                          size_t responseLen) {
7131    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7132        if (response == NULL || responseLen != sizeof(RIL_CDMA_InformationRecords)) {
7133            RLOGE("cdmaInfoRecInd: invalid response");
7134            return 0;
7135        }
7136
7137        CdmaInformationRecords records = {};
7138        RIL_CDMA_InformationRecords *recordsRil = (RIL_CDMA_InformationRecords *) response;
7139
7140        char* string8 = NULL;
7141        int num = MIN(recordsRil->numberOfInfoRecs, RIL_CDMA_MAX_NUMBER_OF_INFO_RECS);
7142        if (recordsRil->numberOfInfoRecs > RIL_CDMA_MAX_NUMBER_OF_INFO_RECS) {
7143            RLOGE("cdmaInfoRecInd: received %d recs which is more than %d, dropping "
7144                    "additional ones", recordsRil->numberOfInfoRecs,
7145                    RIL_CDMA_MAX_NUMBER_OF_INFO_RECS);
7146        }
7147        records.infoRec.resize(num);
7148        for (int i = 0 ; i < num ; i++) {
7149            CdmaInformationRecord *record = &records.infoRec[i];
7150            RIL_CDMA_InformationRecord *infoRec = &recordsRil->infoRec[i];
7151            record->name = (CdmaInfoRecName) infoRec->name;
7152            // All vectors should be size 0 except one which will be size 1. Set everything to
7153            // size 0 initially.
7154            record->display.resize(0);
7155            record->number.resize(0);
7156            record->signal.resize(0);
7157            record->redir.resize(0);
7158            record->lineCtrl.resize(0);
7159            record->clir.resize(0);
7160            record->audioCtrl.resize(0);
7161            switch (infoRec->name) {
7162                case RIL_CDMA_DISPLAY_INFO_REC:
7163                case RIL_CDMA_EXTENDED_DISPLAY_INFO_REC: {
7164                    if (infoRec->rec.display.alpha_len > CDMA_ALPHA_INFO_BUFFER_LENGTH) {
7165                        RLOGE("cdmaInfoRecInd: invalid display info response length %d "
7166                                "expected not more than %d", (int) infoRec->rec.display.alpha_len,
7167                                CDMA_ALPHA_INFO_BUFFER_LENGTH);
7168                        return 0;
7169                    }
7170                    string8 = (char*) malloc((infoRec->rec.display.alpha_len + 1) * sizeof(char));
7171                    if (string8 == NULL) {
7172                        RLOGE("cdmaInfoRecInd: Memory allocation failed for "
7173                                "responseCdmaInformationRecords");
7174                        return 0;
7175                    }
7176                    memcpy(string8, infoRec->rec.display.alpha_buf, infoRec->rec.display.alpha_len);
7177                    string8[(int)infoRec->rec.display.alpha_len] = '\0';
7178
7179                    record->display.resize(1);
7180                    record->display[0].alphaBuf = string8;
7181                    free(string8);
7182                    string8 = NULL;
7183                    break;
7184                }
7185
7186                case RIL_CDMA_CALLED_PARTY_NUMBER_INFO_REC:
7187                case RIL_CDMA_CALLING_PARTY_NUMBER_INFO_REC:
7188                case RIL_CDMA_CONNECTED_NUMBER_INFO_REC: {
7189                    if (infoRec->rec.number.len > CDMA_NUMBER_INFO_BUFFER_LENGTH) {
7190                        RLOGE("cdmaInfoRecInd: invalid display info response length %d "
7191                                "expected not more than %d", (int) infoRec->rec.number.len,
7192                                CDMA_NUMBER_INFO_BUFFER_LENGTH);
7193                        return 0;
7194                    }
7195                    string8 = (char*) malloc((infoRec->rec.number.len + 1) * sizeof(char));
7196                    if (string8 == NULL) {
7197                        RLOGE("cdmaInfoRecInd: Memory allocation failed for "
7198                                "responseCdmaInformationRecords");
7199                        return 0;
7200                    }
7201                    memcpy(string8, infoRec->rec.number.buf, infoRec->rec.number.len);
7202                    string8[(int)infoRec->rec.number.len] = '\0';
7203
7204                    record->number.resize(1);
7205                    record->number[0].number = string8;
7206                    free(string8);
7207                    string8 = NULL;
7208                    record->number[0].numberType = infoRec->rec.number.number_type;
7209                    record->number[0].numberPlan = infoRec->rec.number.number_plan;
7210                    record->number[0].pi = infoRec->rec.number.pi;
7211                    record->number[0].si = infoRec->rec.number.si;
7212                    break;
7213                }
7214
7215                case RIL_CDMA_SIGNAL_INFO_REC: {
7216                    record->signal.resize(1);
7217                    record->signal[0].isPresent = infoRec->rec.signal.isPresent;
7218                    record->signal[0].signalType = infoRec->rec.signal.signalType;
7219                    record->signal[0].alertPitch = infoRec->rec.signal.alertPitch;
7220                    record->signal[0].signal = infoRec->rec.signal.signal;
7221                    break;
7222                }
7223
7224                case RIL_CDMA_REDIRECTING_NUMBER_INFO_REC: {
7225                    if (infoRec->rec.redir.redirectingNumber.len >
7226                                                  CDMA_NUMBER_INFO_BUFFER_LENGTH) {
7227                        RLOGE("cdmaInfoRecInd: invalid display info response length %d "
7228                                "expected not more than %d\n",
7229                                (int)infoRec->rec.redir.redirectingNumber.len,
7230                                CDMA_NUMBER_INFO_BUFFER_LENGTH);
7231                        return 0;
7232                    }
7233                    string8 = (char*) malloc((infoRec->rec.redir.redirectingNumber.len + 1) *
7234                            sizeof(char));
7235                    if (string8 == NULL) {
7236                        RLOGE("cdmaInfoRecInd: Memory allocation failed for "
7237                                "responseCdmaInformationRecords");
7238                        return 0;
7239                    }
7240                    memcpy(string8, infoRec->rec.redir.redirectingNumber.buf,
7241                            infoRec->rec.redir.redirectingNumber.len);
7242                    string8[(int)infoRec->rec.redir.redirectingNumber.len] = '\0';
7243
7244                    record->redir.resize(1);
7245                    record->redir[0].redirectingNumber.number = string8;
7246                    free(string8);
7247                    string8 = NULL;
7248                    record->redir[0].redirectingNumber.numberType =
7249                            infoRec->rec.redir.redirectingNumber.number_type;
7250                    record->redir[0].redirectingNumber.numberPlan =
7251                            infoRec->rec.redir.redirectingNumber.number_plan;
7252                    record->redir[0].redirectingNumber.pi = infoRec->rec.redir.redirectingNumber.pi;
7253                    record->redir[0].redirectingNumber.si = infoRec->rec.redir.redirectingNumber.si;
7254                    record->redir[0].redirectingReason =
7255                            (CdmaRedirectingReason) infoRec->rec.redir.redirectingReason;
7256                    break;
7257                }
7258
7259                case RIL_CDMA_LINE_CONTROL_INFO_REC: {
7260                    record->lineCtrl.resize(1);
7261                    record->lineCtrl[0].lineCtrlPolarityIncluded =
7262                            infoRec->rec.lineCtrl.lineCtrlPolarityIncluded;
7263                    record->lineCtrl[0].lineCtrlToggle = infoRec->rec.lineCtrl.lineCtrlToggle;
7264                    record->lineCtrl[0].lineCtrlReverse = infoRec->rec.lineCtrl.lineCtrlReverse;
7265                    record->lineCtrl[0].lineCtrlPowerDenial =
7266                            infoRec->rec.lineCtrl.lineCtrlPowerDenial;
7267                    break;
7268                }
7269
7270                case RIL_CDMA_T53_CLIR_INFO_REC: {
7271                    record->clir.resize(1);
7272                    record->clir[0].cause = infoRec->rec.clir.cause;
7273                    break;
7274                }
7275
7276                case RIL_CDMA_T53_AUDIO_CONTROL_INFO_REC: {
7277                    record->audioCtrl.resize(1);
7278                    record->audioCtrl[0].upLink = infoRec->rec.audioCtrl.upLink;
7279                    record->audioCtrl[0].downLink = infoRec->rec.audioCtrl.downLink;
7280                    break;
7281                }
7282
7283                case RIL_CDMA_T53_RELEASE_INFO_REC:
7284                    RLOGE("cdmaInfoRecInd: RIL_CDMA_T53_RELEASE_INFO_REC: INVALID");
7285                    return 0;
7286
7287                default:
7288                    RLOGE("cdmaInfoRecInd: Incorrect name value");
7289                    return 0;
7290            }
7291        }
7292
7293#if VDBG
7294        RLOGD("cdmaInfoRecInd");
7295#endif
7296        Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaInfoRec(
7297                convertIntToRadioIndicationType(indicationType), records);
7298        radioService[slotId]->checkReturnStatus(retStatus);
7299    } else {
7300        RLOGE("cdmaInfoRecInd: radioService[%d]->mRadioIndication == NULL", slotId);
7301    }
7302
7303    return 0;
7304}
7305
7306int radio::indicateRingbackToneInd(int slotId,
7307                                   int indicationType, int token, RIL_Errno e, void *response,
7308                                   size_t responseLen) {
7309    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7310        if (response == NULL || responseLen != sizeof(int)) {
7311            RLOGE("indicateRingbackToneInd: invalid response");
7312            return 0;
7313        }
7314        bool start = ((int32_t *) response)[0];
7315#if VDBG
7316        RLOGD("indicateRingbackToneInd: start %d", start);
7317#endif
7318        Return<void> retStatus = radioService[slotId]->mRadioIndication->indicateRingbackTone(
7319                convertIntToRadioIndicationType(indicationType), start);
7320        radioService[slotId]->checkReturnStatus(retStatus);
7321    } else {
7322        RLOGE("indicateRingbackToneInd: radioService[%d]->mRadioIndication == NULL", slotId);
7323    }
7324
7325    return 0;
7326}
7327
7328int radio::resendIncallMuteInd(int slotId,
7329                               int indicationType, int token, RIL_Errno e, void *response,
7330                               size_t responseLen) {
7331    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7332#if VDBG
7333        RLOGD("resendIncallMuteInd");
7334#endif
7335        Return<void> retStatus = radioService[slotId]->mRadioIndication->resendIncallMute(
7336                convertIntToRadioIndicationType(indicationType));
7337        radioService[slotId]->checkReturnStatus(retStatus);
7338    } else {
7339        RLOGE("resendIncallMuteInd: radioService[%d]->mRadioIndication == NULL", slotId);
7340    }
7341
7342    return 0;
7343}
7344
7345int radio::cdmaSubscriptionSourceChangedInd(int slotId,
7346                                            int indicationType, int token, RIL_Errno e,
7347                                            void *response, size_t responseLen) {
7348    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7349        if (response == NULL || responseLen != sizeof(int)) {
7350            RLOGE("cdmaSubscriptionSourceChangedInd: invalid response");
7351            return 0;
7352        }
7353        int32_t cdmaSource = ((int32_t *) response)[0];
7354#if VDBG
7355        RLOGD("cdmaSubscriptionSourceChangedInd: cdmaSource %d", cdmaSource);
7356#endif
7357        Return<void> retStatus = radioService[slotId]->mRadioIndication->
7358                cdmaSubscriptionSourceChanged(convertIntToRadioIndicationType(indicationType),
7359                (CdmaSubscriptionSource) cdmaSource);
7360        radioService[slotId]->checkReturnStatus(retStatus);
7361    } else {
7362        RLOGE("cdmaSubscriptionSourceChangedInd: radioService[%d]->mRadioIndication == NULL",
7363                slotId);
7364    }
7365
7366    return 0;
7367}
7368
7369int radio::cdmaPrlChangedInd(int slotId,
7370                             int indicationType, int token, RIL_Errno e, void *response,
7371                             size_t responseLen) {
7372    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7373        if (response == NULL || responseLen != sizeof(int)) {
7374            RLOGE("cdmaPrlChangedInd: invalid response");
7375            return 0;
7376        }
7377        int32_t version = ((int32_t *) response)[0];
7378#if VDBG
7379        RLOGD("cdmaPrlChangedInd: version %d", version);
7380#endif
7381        Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaPrlChanged(
7382                convertIntToRadioIndicationType(indicationType), version);
7383        radioService[slotId]->checkReturnStatus(retStatus);
7384    } else {
7385        RLOGE("cdmaPrlChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
7386    }
7387
7388    return 0;
7389}
7390
7391int radio::exitEmergencyCallbackModeInd(int slotId,
7392                                        int indicationType, int token, RIL_Errno e, void *response,
7393                                        size_t responseLen) {
7394    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7395#if VDBG
7396        RLOGD("exitEmergencyCallbackModeInd");
7397#endif
7398        Return<void> retStatus = radioService[slotId]->mRadioIndication->exitEmergencyCallbackMode(
7399                convertIntToRadioIndicationType(indicationType));
7400        radioService[slotId]->checkReturnStatus(retStatus);
7401    } else {
7402        RLOGE("exitEmergencyCallbackModeInd: radioService[%d]->mRadioIndication == NULL",
7403                slotId);
7404    }
7405
7406    return 0;
7407}
7408
7409int radio::rilConnectedInd(int slotId,
7410                           int indicationType, int token, RIL_Errno e, void *response,
7411                           size_t responseLen) {
7412    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7413        RLOGD("rilConnectedInd");
7414        Return<void> retStatus = radioService[slotId]->mRadioIndication->rilConnected(
7415                convertIntToRadioIndicationType(indicationType));
7416        radioService[slotId]->checkReturnStatus(retStatus);
7417    } else {
7418        RLOGE("rilConnectedInd: radioService[%d]->mRadioIndication == NULL", slotId);
7419    }
7420
7421    return 0;
7422}
7423
7424int radio::voiceRadioTechChangedInd(int slotId,
7425                                    int indicationType, int token, RIL_Errno e, void *response,
7426                                    size_t responseLen) {
7427    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7428        if (response == NULL || responseLen != sizeof(int)) {
7429            RLOGE("voiceRadioTechChangedInd: invalid response");
7430            return 0;
7431        }
7432        int32_t rat = ((int32_t *) response)[0];
7433#if VDBG
7434        RLOGD("voiceRadioTechChangedInd: rat %d", rat);
7435#endif
7436        Return<void> retStatus = radioService[slotId]->mRadioIndication->voiceRadioTechChanged(
7437                convertIntToRadioIndicationType(indicationType), (RadioTechnology) rat);
7438        radioService[slotId]->checkReturnStatus(retStatus);
7439    } else {
7440        RLOGE("voiceRadioTechChangedInd: radioService[%d]->mRadioIndication == NULL",
7441                slotId);
7442    }
7443
7444    return 0;
7445}
7446
7447void convertRilCellInfoListToHal(void *response, size_t responseLen, hidl_vec<CellInfo>& records) {
7448    int num = responseLen / sizeof(RIL_CellInfo_v12);
7449    records.resize(num);
7450
7451    RIL_CellInfo_v12 *rillCellInfo = (RIL_CellInfo_v12 *) response;
7452    for (int i = 0; i < num; i++) {
7453        records[i].cellInfoType = (CellInfoType) rillCellInfo->cellInfoType;
7454        records[i].registered = rillCellInfo->registered;
7455        records[i].timeStampType = (TimeStampType) rillCellInfo->timeStampType;
7456        records[i].timeStamp = rillCellInfo->timeStamp;
7457        // All vectors should be size 0 except one which will be size 1. Set everything to
7458        // size 0 initially.
7459        records[i].gsm.resize(0);
7460        records[i].wcdma.resize(0);
7461        records[i].cdma.resize(0);
7462        records[i].lte.resize(0);
7463        records[i].tdscdma.resize(0);
7464        switch(rillCellInfo->cellInfoType) {
7465            case RIL_CELL_INFO_TYPE_GSM: {
7466                records[i].gsm.resize(1);
7467                CellInfoGsm *cellInfoGsm = &records[i].gsm[0];
7468                cellInfoGsm->cellIdentityGsm.mcc =
7469                        std::to_string(rillCellInfo->CellInfo.gsm.cellIdentityGsm.mcc);
7470                cellInfoGsm->cellIdentityGsm.mnc =
7471                        std::to_string(rillCellInfo->CellInfo.gsm.cellIdentityGsm.mnc);
7472                cellInfoGsm->cellIdentityGsm.lac =
7473                        rillCellInfo->CellInfo.gsm.cellIdentityGsm.lac;
7474                cellInfoGsm->cellIdentityGsm.cid =
7475                        rillCellInfo->CellInfo.gsm.cellIdentityGsm.cid;
7476                cellInfoGsm->cellIdentityGsm.arfcn =
7477                        rillCellInfo->CellInfo.gsm.cellIdentityGsm.arfcn;
7478                cellInfoGsm->cellIdentityGsm.bsic =
7479                        rillCellInfo->CellInfo.gsm.cellIdentityGsm.bsic;
7480                cellInfoGsm->signalStrengthGsm.signalStrength =
7481                        rillCellInfo->CellInfo.gsm.signalStrengthGsm.signalStrength;
7482                cellInfoGsm->signalStrengthGsm.bitErrorRate =
7483                        rillCellInfo->CellInfo.gsm.signalStrengthGsm.bitErrorRate;
7484                cellInfoGsm->signalStrengthGsm.timingAdvance =
7485                        rillCellInfo->CellInfo.gsm.signalStrengthGsm.timingAdvance;
7486                break;
7487            }
7488
7489            case RIL_CELL_INFO_TYPE_WCDMA: {
7490                records[i].wcdma.resize(1);
7491                CellInfoWcdma *cellInfoWcdma = &records[i].wcdma[0];
7492                cellInfoWcdma->cellIdentityWcdma.mcc =
7493                        std::to_string(rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.mcc);
7494                cellInfoWcdma->cellIdentityWcdma.mnc =
7495                        std::to_string(rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.mnc);
7496                cellInfoWcdma->cellIdentityWcdma.lac =
7497                        rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.lac;
7498                cellInfoWcdma->cellIdentityWcdma.cid =
7499                        rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.cid;
7500                cellInfoWcdma->cellIdentityWcdma.psc =
7501                        rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.psc;
7502                cellInfoWcdma->cellIdentityWcdma.uarfcn =
7503                        rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.uarfcn;
7504                cellInfoWcdma->signalStrengthWcdma.signalStrength =
7505                        rillCellInfo->CellInfo.wcdma.signalStrengthWcdma.signalStrength;
7506                cellInfoWcdma->signalStrengthWcdma.bitErrorRate =
7507                        rillCellInfo->CellInfo.wcdma.signalStrengthWcdma.bitErrorRate;
7508                break;
7509            }
7510
7511            case RIL_CELL_INFO_TYPE_CDMA: {
7512                records[i].cdma.resize(1);
7513                CellInfoCdma *cellInfoCdma = &records[i].cdma[0];
7514                cellInfoCdma->cellIdentityCdma.networkId =
7515                        rillCellInfo->CellInfo.cdma.cellIdentityCdma.networkId;
7516                cellInfoCdma->cellIdentityCdma.systemId =
7517                        rillCellInfo->CellInfo.cdma.cellIdentityCdma.systemId;
7518                cellInfoCdma->cellIdentityCdma.baseStationId =
7519                        rillCellInfo->CellInfo.cdma.cellIdentityCdma.basestationId;
7520                cellInfoCdma->cellIdentityCdma.longitude =
7521                        rillCellInfo->CellInfo.cdma.cellIdentityCdma.longitude;
7522                cellInfoCdma->cellIdentityCdma.latitude =
7523                        rillCellInfo->CellInfo.cdma.cellIdentityCdma.latitude;
7524                cellInfoCdma->signalStrengthCdma.dbm =
7525                        rillCellInfo->CellInfo.cdma.signalStrengthCdma.dbm;
7526                cellInfoCdma->signalStrengthCdma.ecio =
7527                        rillCellInfo->CellInfo.cdma.signalStrengthCdma.ecio;
7528                cellInfoCdma->signalStrengthEvdo.dbm =
7529                        rillCellInfo->CellInfo.cdma.signalStrengthEvdo.dbm;
7530                cellInfoCdma->signalStrengthEvdo.ecio =
7531                        rillCellInfo->CellInfo.cdma.signalStrengthEvdo.ecio;
7532                cellInfoCdma->signalStrengthEvdo.signalNoiseRatio =
7533                        rillCellInfo->CellInfo.cdma.signalStrengthEvdo.signalNoiseRatio;
7534                break;
7535            }
7536
7537            case RIL_CELL_INFO_TYPE_LTE: {
7538                records[i].lte.resize(1);
7539                CellInfoLte *cellInfoLte = &records[i].lte[0];
7540                cellInfoLte->cellIdentityLte.mcc =
7541                        std::to_string(rillCellInfo->CellInfo.lte.cellIdentityLte.mcc);
7542                cellInfoLte->cellIdentityLte.mnc =
7543                        std::to_string(rillCellInfo->CellInfo.lte.cellIdentityLte.mnc);
7544                cellInfoLte->cellIdentityLte.ci =
7545                        rillCellInfo->CellInfo.lte.cellIdentityLte.ci;
7546                cellInfoLte->cellIdentityLte.pci =
7547                        rillCellInfo->CellInfo.lte.cellIdentityLte.pci;
7548                cellInfoLte->cellIdentityLte.tac =
7549                        rillCellInfo->CellInfo.lte.cellIdentityLte.tac;
7550                cellInfoLte->cellIdentityLte.earfcn =
7551                        rillCellInfo->CellInfo.lte.cellIdentityLte.earfcn;
7552                cellInfoLte->signalStrengthLte.signalStrength =
7553                        rillCellInfo->CellInfo.lte.signalStrengthLte.signalStrength;
7554                cellInfoLte->signalStrengthLte.rsrp =
7555                        rillCellInfo->CellInfo.lte.signalStrengthLte.rsrp;
7556                cellInfoLte->signalStrengthLte.rsrq =
7557                        rillCellInfo->CellInfo.lte.signalStrengthLte.rsrq;
7558                cellInfoLte->signalStrengthLte.rssnr =
7559                        rillCellInfo->CellInfo.lte.signalStrengthLte.rssnr;
7560                cellInfoLte->signalStrengthLte.cqi =
7561                        rillCellInfo->CellInfo.lte.signalStrengthLte.cqi;
7562                cellInfoLte->signalStrengthLte.timingAdvance =
7563                        rillCellInfo->CellInfo.lte.signalStrengthLte.timingAdvance;
7564                break;
7565            }
7566
7567            case RIL_CELL_INFO_TYPE_TD_SCDMA: {
7568                records[i].tdscdma.resize(1);
7569                CellInfoTdscdma *cellInfoTdscdma = &records[i].tdscdma[0];
7570                cellInfoTdscdma->cellIdentityTdscdma.mcc =
7571                        std::to_string(rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.mcc);
7572                cellInfoTdscdma->cellIdentityTdscdma.mnc =
7573                        std::to_string(rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.mnc);
7574                cellInfoTdscdma->cellIdentityTdscdma.lac =
7575                        rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.lac;
7576                cellInfoTdscdma->cellIdentityTdscdma.cid =
7577                        rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.cid;
7578                cellInfoTdscdma->cellIdentityTdscdma.cpid =
7579                        rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.cpid;
7580                cellInfoTdscdma->signalStrengthTdscdma.rscp =
7581                        rillCellInfo->CellInfo.tdscdma.signalStrengthTdscdma.rscp;
7582                break;
7583            }
7584            default: {
7585                break;
7586            }
7587        }
7588        rillCellInfo += 1;
7589    }
7590}
7591
7592int radio::cellInfoListInd(int slotId,
7593                           int indicationType, int token, RIL_Errno e, void *response,
7594                           size_t responseLen) {
7595    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7596        if (response == NULL || responseLen % sizeof(RIL_CellInfo_v12) != 0) {
7597            RLOGE("cellInfoListInd: invalid response");
7598            return 0;
7599        }
7600
7601        hidl_vec<CellInfo> records;
7602        convertRilCellInfoListToHal(response, responseLen, records);
7603
7604#if VDBG
7605        RLOGD("cellInfoListInd");
7606#endif
7607        Return<void> retStatus = radioService[slotId]->mRadioIndication->cellInfoList(
7608                convertIntToRadioIndicationType(indicationType), records);
7609        radioService[slotId]->checkReturnStatus(retStatus);
7610    } else {
7611        RLOGE("cellInfoListInd: radioService[%d]->mRadioIndication == NULL", slotId);
7612    }
7613
7614    return 0;
7615}
7616
7617int radio::imsNetworkStateChangedInd(int slotId,
7618                                     int indicationType, int token, RIL_Errno e, void *response,
7619                                     size_t responseLen) {
7620    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7621#if VDBG
7622        RLOGD("imsNetworkStateChangedInd");
7623#endif
7624        Return<void> retStatus = radioService[slotId]->mRadioIndication->imsNetworkStateChanged(
7625                convertIntToRadioIndicationType(indicationType));
7626        radioService[slotId]->checkReturnStatus(retStatus);
7627    } else {
7628        RLOGE("imsNetworkStateChangedInd: radioService[%d]->mRadioIndication == NULL",
7629                slotId);
7630    }
7631
7632    return 0;
7633}
7634
7635int radio::subscriptionStatusChangedInd(int slotId,
7636                                        int indicationType, int token, RIL_Errno e, void *response,
7637                                        size_t responseLen) {
7638    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7639        if (response == NULL || responseLen != sizeof(int)) {
7640            RLOGE("subscriptionStatusChangedInd: invalid response");
7641            return 0;
7642        }
7643        bool activate = ((int32_t *) response)[0];
7644#if VDBG
7645        RLOGD("subscriptionStatusChangedInd: activate %d", activate);
7646#endif
7647        Return<void> retStatus = radioService[slotId]->mRadioIndication->subscriptionStatusChanged(
7648                convertIntToRadioIndicationType(indicationType), activate);
7649        radioService[slotId]->checkReturnStatus(retStatus);
7650    } else {
7651        RLOGE("subscriptionStatusChangedInd: radioService[%d]->mRadioIndication == NULL",
7652                slotId);
7653    }
7654
7655    return 0;
7656}
7657
7658int radio::srvccStateNotifyInd(int slotId,
7659                               int indicationType, int token, RIL_Errno e, void *response,
7660                               size_t responseLen) {
7661    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7662        if (response == NULL || responseLen != sizeof(int)) {
7663            RLOGE("srvccStateNotifyInd: invalid response");
7664            return 0;
7665        }
7666        int32_t state = ((int32_t *) response)[0];
7667#if VDBG
7668        RLOGD("srvccStateNotifyInd: rat %d", state);
7669#endif
7670        Return<void> retStatus = radioService[slotId]->mRadioIndication->srvccStateNotify(
7671                convertIntToRadioIndicationType(indicationType), (SrvccState) state);
7672        radioService[slotId]->checkReturnStatus(retStatus);
7673    } else {
7674        RLOGE("srvccStateNotifyInd: radioService[%d]->mRadioIndication == NULL", slotId);
7675    }
7676
7677    return 0;
7678}
7679
7680void convertRilHardwareConfigListToHal(void *response, size_t responseLen,
7681        hidl_vec<HardwareConfig>& records) {
7682    int num = responseLen / sizeof(RIL_HardwareConfig);
7683    records.resize(num);
7684
7685    RIL_HardwareConfig *rilHardwareConfig = (RIL_HardwareConfig *) response;
7686    for (int i = 0; i < num; i++) {
7687        records[i].type = (HardwareConfigType) rilHardwareConfig[i].type;
7688        records[i].uuid = convertCharPtrToHidlString(rilHardwareConfig[i].uuid);
7689        records[i].state = (HardwareConfigState) rilHardwareConfig[i].state;
7690        switch (rilHardwareConfig[i].type) {
7691            case RIL_HARDWARE_CONFIG_MODEM: {
7692                records[i].modem.resize(1);
7693                records[i].sim.resize(0);
7694                HardwareConfigModem *hwConfigModem = &records[i].modem[0];
7695                hwConfigModem->rat = rilHardwareConfig[i].cfg.modem.rat;
7696                hwConfigModem->maxVoice = rilHardwareConfig[i].cfg.modem.maxVoice;
7697                hwConfigModem->maxData = rilHardwareConfig[i].cfg.modem.maxData;
7698                hwConfigModem->maxStandby = rilHardwareConfig[i].cfg.modem.maxStandby;
7699                break;
7700            }
7701
7702            case RIL_HARDWARE_CONFIG_SIM: {
7703                records[i].sim.resize(1);
7704                records[i].modem.resize(0);
7705                records[i].sim[0].modemUuid =
7706                        convertCharPtrToHidlString(rilHardwareConfig[i].cfg.sim.modemUuid);
7707                break;
7708            }
7709        }
7710    }
7711}
7712
7713int radio::hardwareConfigChangedInd(int slotId,
7714                                    int indicationType, int token, RIL_Errno e, void *response,
7715                                    size_t responseLen) {
7716    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7717        if (response == NULL || responseLen % sizeof(RIL_HardwareConfig) != 0) {
7718            RLOGE("hardwareConfigChangedInd: invalid response");
7719            return 0;
7720        }
7721
7722        hidl_vec<HardwareConfig> configs;
7723        convertRilHardwareConfigListToHal(response, responseLen, configs);
7724
7725#if VDBG
7726        RLOGD("hardwareConfigChangedInd");
7727#endif
7728        Return<void> retStatus = radioService[slotId]->mRadioIndication->hardwareConfigChanged(
7729                convertIntToRadioIndicationType(indicationType), configs);
7730        radioService[slotId]->checkReturnStatus(retStatus);
7731    } else {
7732        RLOGE("hardwareConfigChangedInd: radioService[%d]->mRadioIndication == NULL",
7733                slotId);
7734    }
7735
7736    return 0;
7737}
7738
7739void convertRilRadioCapabilityToHal(void *response, size_t responseLen, RadioCapability& rc) {
7740    RIL_RadioCapability *rilRadioCapability = (RIL_RadioCapability *) response;
7741    rc.session = rilRadioCapability->session;
7742    rc.phase = (android::hardware::radio::V1_0::RadioCapabilityPhase) rilRadioCapability->phase;
7743    rc.raf = rilRadioCapability->rat;
7744    rc.logicalModemUuid = convertCharPtrToHidlString(rilRadioCapability->logicalModemUuid);
7745    rc.status = (android::hardware::radio::V1_0::RadioCapabilityStatus) rilRadioCapability->status;
7746}
7747
7748int radio::radioCapabilityIndicationInd(int slotId,
7749                                        int indicationType, int token, RIL_Errno e, void *response,
7750                                        size_t responseLen) {
7751    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7752        if (response == NULL || responseLen != sizeof(RIL_RadioCapability)) {
7753            RLOGE("radioCapabilityIndicationInd: invalid response");
7754            return 0;
7755        }
7756
7757        RadioCapability rc = {};
7758        convertRilRadioCapabilityToHal(response, responseLen, rc);
7759
7760#if VDBG
7761        RLOGD("radioCapabilityIndicationInd");
7762#endif
7763        Return<void> retStatus = radioService[slotId]->mRadioIndication->radioCapabilityIndication(
7764                convertIntToRadioIndicationType(indicationType), rc);
7765        radioService[slotId]->checkReturnStatus(retStatus);
7766    } else {
7767        RLOGE("radioCapabilityIndicationInd: radioService[%d]->mRadioIndication == NULL",
7768                slotId);
7769    }
7770
7771    return 0;
7772}
7773
7774bool isServiceTypeCfQuery(RIL_SsServiceType serType, RIL_SsRequestType reqType) {
7775    if ((reqType == SS_INTERROGATION) &&
7776        (serType == SS_CFU ||
7777         serType == SS_CF_BUSY ||
7778         serType == SS_CF_NO_REPLY ||
7779         serType == SS_CF_NOT_REACHABLE ||
7780         serType == SS_CF_ALL ||
7781         serType == SS_CF_ALL_CONDITIONAL)) {
7782        return true;
7783    }
7784    return false;
7785}
7786
7787int radio::onSupplementaryServiceIndicationInd(int slotId,
7788                                               int indicationType, int token, RIL_Errno e,
7789                                               void *response, size_t responseLen) {
7790    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7791        if (response == NULL || responseLen != sizeof(RIL_StkCcUnsolSsResponse)) {
7792            RLOGE("onSupplementaryServiceIndicationInd: invalid response");
7793            return 0;
7794        }
7795
7796        RIL_StkCcUnsolSsResponse *rilSsResponse = (RIL_StkCcUnsolSsResponse *) response;
7797        StkCcUnsolSsResult ss = {};
7798        ss.serviceType = (SsServiceType) rilSsResponse->serviceType;
7799        ss.requestType = (SsRequestType) rilSsResponse->requestType;
7800        ss.teleserviceType = (SsTeleserviceType) rilSsResponse->teleserviceType;
7801        ss.serviceClass = rilSsResponse->serviceClass;
7802        ss.result = (RadioError) rilSsResponse->result;
7803
7804        if (isServiceTypeCfQuery(rilSsResponse->serviceType, rilSsResponse->requestType)) {
7805#if VDBG
7806            RLOGD("onSupplementaryServiceIndicationInd CF type, num of Cf elements %d",
7807                    rilSsResponse->cfData.numValidIndexes);
7808#endif
7809            if (rilSsResponse->cfData.numValidIndexes > NUM_SERVICE_CLASSES) {
7810                RLOGE("onSupplementaryServiceIndicationInd numValidIndexes is greater than "
7811                        "max value %d, truncating it to max value", NUM_SERVICE_CLASSES);
7812                rilSsResponse->cfData.numValidIndexes = NUM_SERVICE_CLASSES;
7813            }
7814
7815            ss.cfData.resize(1);
7816            ss.ssInfo.resize(0);
7817
7818            /* number of call info's */
7819            ss.cfData[0].cfInfo.resize(rilSsResponse->cfData.numValidIndexes);
7820
7821            for (int i = 0; i < rilSsResponse->cfData.numValidIndexes; i++) {
7822                 RIL_CallForwardInfo cf = rilSsResponse->cfData.cfInfo[i];
7823                 CallForwardInfo *cfInfo = &ss.cfData[0].cfInfo[i];
7824
7825                 cfInfo->status = (CallForwardInfoStatus) cf.status;
7826                 cfInfo->reason = cf.reason;
7827                 cfInfo->serviceClass = cf.serviceClass;
7828                 cfInfo->toa = cf.toa;
7829                 cfInfo->number = convertCharPtrToHidlString(cf.number);
7830                 cfInfo->timeSeconds = cf.timeSeconds;
7831#if VDBG
7832                 RLOGD("onSupplementaryServiceIndicationInd: "
7833                        "Data: %d,reason=%d,cls=%d,toa=%d,num=%s,tout=%d],", cf.status,
7834                        cf.reason, cf.serviceClass, cf.toa, (char*)cf.number, cf.timeSeconds);
7835#endif
7836            }
7837        } else {
7838            ss.ssInfo.resize(1);
7839            ss.cfData.resize(0);
7840
7841            /* each int */
7842            ss.ssInfo[0].ssInfo.resize(SS_INFO_MAX);
7843            for (int i = 0; i < SS_INFO_MAX; i++) {
7844#if VDBG
7845                 RLOGD("onSupplementaryServiceIndicationInd: Data: %d",
7846                        rilSsResponse->ssInfo[i]);
7847#endif
7848                 ss.ssInfo[0].ssInfo[i] = rilSsResponse->ssInfo[i];
7849            }
7850        }
7851
7852#if VDBG
7853        RLOGD("onSupplementaryServiceIndicationInd");
7854#endif
7855        Return<void> retStatus = radioService[slotId]->mRadioIndication->
7856                onSupplementaryServiceIndication(convertIntToRadioIndicationType(indicationType),
7857                ss);
7858        radioService[slotId]->checkReturnStatus(retStatus);
7859    } else {
7860        RLOGE("onSupplementaryServiceIndicationInd: "
7861                "radioService[%d]->mRadioIndication == NULL", slotId);
7862    }
7863
7864    return 0;
7865}
7866
7867int radio::stkCallControlAlphaNotifyInd(int slotId,
7868                                        int indicationType, int token, RIL_Errno e, void *response,
7869                                        size_t responseLen) {
7870    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7871        if (response == NULL || responseLen == 0) {
7872            RLOGE("stkCallControlAlphaNotifyInd: invalid response");
7873            return 0;
7874        }
7875#if VDBG
7876        RLOGD("stkCallControlAlphaNotifyInd");
7877#endif
7878        Return<void> retStatus = radioService[slotId]->mRadioIndication->stkCallControlAlphaNotify(
7879                convertIntToRadioIndicationType(indicationType),
7880                convertCharPtrToHidlString((char *) response));
7881        radioService[slotId]->checkReturnStatus(retStatus);
7882    } else {
7883        RLOGE("stkCallControlAlphaNotifyInd: radioService[%d]->mRadioIndication == NULL",
7884                slotId);
7885    }
7886
7887    return 0;
7888}
7889
7890void convertRilLceDataInfoToHal(void *response, size_t responseLen, LceDataInfo& lce) {
7891    RIL_LceDataInfo *rilLceDataInfo = (RIL_LceDataInfo *)response;
7892    lce.lastHopCapacityKbps = rilLceDataInfo->last_hop_capacity_kbps;
7893    lce.confidenceLevel = rilLceDataInfo->confidence_level;
7894    lce.lceSuspended = rilLceDataInfo->lce_suspended;
7895}
7896
7897int radio::lceDataInd(int slotId,
7898                      int indicationType, int token, RIL_Errno e, void *response,
7899                      size_t responseLen) {
7900    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7901        if (response == NULL || responseLen != sizeof(RIL_LceDataInfo)) {
7902            RLOGE("lceDataInd: invalid response");
7903            return 0;
7904        }
7905
7906        LceDataInfo lce = {};
7907        convertRilLceDataInfoToHal(response, responseLen, lce);
7908#if VDBG
7909        RLOGD("lceDataInd");
7910#endif
7911        Return<void> retStatus = radioService[slotId]->mRadioIndication->lceData(
7912                convertIntToRadioIndicationType(indicationType), lce);
7913        radioService[slotId]->checkReturnStatus(retStatus);
7914    } else {
7915        RLOGE("lceDataInd: radioService[%d]->mRadioIndication == NULL", slotId);
7916    }
7917
7918    return 0;
7919}
7920
7921int radio::pcoDataInd(int slotId,
7922                      int indicationType, int token, RIL_Errno e, void *response,
7923                      size_t responseLen) {
7924    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7925        if (response == NULL || responseLen != sizeof(RIL_PCO_Data)) {
7926            RLOGE("pcoDataInd: invalid response");
7927            return 0;
7928        }
7929
7930        PcoDataInfo pco = {};
7931        RIL_PCO_Data *rilPcoData = (RIL_PCO_Data *)response;
7932        pco.cid = rilPcoData->cid;
7933        pco.bearerProto = convertCharPtrToHidlString(rilPcoData->bearer_proto);
7934        pco.pcoId = rilPcoData->pco_id;
7935        pco.contents.setToExternal((uint8_t *) rilPcoData->contents, rilPcoData->contents_length);
7936
7937#if VDBG
7938        RLOGD("pcoDataInd");
7939#endif
7940        Return<void> retStatus = radioService[slotId]->mRadioIndication->pcoData(
7941                convertIntToRadioIndicationType(indicationType), pco);
7942        radioService[slotId]->checkReturnStatus(retStatus);
7943    } else {
7944        RLOGE("pcoDataInd: radioService[%d]->mRadioIndication == NULL", slotId);
7945    }
7946
7947    return 0;
7948}
7949
7950int radio::modemResetInd(int slotId,
7951                         int indicationType, int token, RIL_Errno e, void *response,
7952                         size_t responseLen) {
7953    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7954        if (response == NULL || responseLen == 0) {
7955            RLOGE("modemResetInd: invalid response");
7956            return 0;
7957        }
7958#if VDBG
7959        RLOGD("modemResetInd");
7960#endif
7961        Return<void> retStatus = radioService[slotId]->mRadioIndication->modemReset(
7962                convertIntToRadioIndicationType(indicationType),
7963                convertCharPtrToHidlString((char *) response));
7964        radioService[slotId]->checkReturnStatus(retStatus);
7965    } else {
7966        RLOGE("modemResetInd: radioService[%d]->mRadioIndication == NULL", slotId);
7967    }
7968
7969    return 0;
7970}
7971
7972int radio::carrierInfoForImsiEncryption(int slotId,
7973                                  int indicationType, int token, RIL_Errno e, void *response,
7974                                  size_t responseLen) {
7975    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7976        if (response == NULL || responseLen == 0) {
7977            RLOGE("carrierInfoForImsiEncryption: invalid response");
7978            return 0;
7979        }
7980        RLOGD("carrierInfoForImsiEncryption");
7981        Return<sp<::android::hardware::radio::V1_1::IRadioIndication>> ret =
7982            ::android::hardware::radio::V1_1::IRadioIndication::castFrom(
7983            radioService[slotId]->mRadioIndication);
7984        if (ret.isOk()) {
7985            sp<::android::hardware::radio::V1_1::IRadioIndication> radioIndicationV1_1 = ret;
7986            Return<void> retStatus = radioIndicationV1_1->carrierInfoForImsiEncryption(
7987                    convertIntToRadioIndicationType(indicationType));
7988            radioService[slotId]->checkReturnStatus(retStatus);
7989        } else {
7990            RLOGE("carrierInfoForImsiEncryptionResponse: ret.isOk() == false for radioService[%d]",
7991                    slotId);
7992        }
7993    } else {
7994        RLOGE("carrierInfoForImsiEncryption: radioService[%d]->mRadioIndication == NULL", slotId);
7995    }
7996
7997    return 0;
7998}
7999
8000int radio::oemHookRawInd(int slotId,
8001                         int indicationType, int token, RIL_Errno e, void *response,
8002                         size_t responseLen) {
8003    if (oemHookService[slotId] != NULL && oemHookService[slotId]->mOemHookIndication != NULL) {
8004        if (response == NULL || responseLen == 0) {
8005            RLOGE("oemHookRawInd: invalid response");
8006            return 0;
8007        }
8008
8009        hidl_vec<uint8_t> data;
8010        data.setToExternal((uint8_t *) response, responseLen);
8011#if VDBG
8012        RLOGD("oemHookRawInd");
8013#endif
8014        Return<void> retStatus = oemHookService[slotId]->mOemHookIndication->oemHookRaw(
8015                convertIntToRadioIndicationType(indicationType), data);
8016        checkReturnStatus(slotId, retStatus, false);
8017    } else {
8018        RLOGE("oemHookRawInd: oemHookService[%d]->mOemHookIndication == NULL", slotId);
8019    }
8020
8021    return 0;
8022}
8023
8024void radio::registerService(RIL_RadioFunctions *callbacks, CommandInfo *commands) {
8025    using namespace android::hardware;
8026    int simCount = 1;
8027    const char *serviceNames[] = {
8028            android::RIL_getServiceName()
8029            #if (SIM_COUNT >= 2)
8030            , RIL2_SERVICE_NAME
8031            #if (SIM_COUNT >= 3)
8032            , RIL3_SERVICE_NAME
8033            #if (SIM_COUNT >= 4)
8034            , RIL4_SERVICE_NAME
8035            #endif
8036            #endif
8037            #endif
8038            };
8039
8040    #if (SIM_COUNT >= 2)
8041    simCount = SIM_COUNT;
8042    #endif
8043
8044    configureRpcThreadpool(1, true /* callerWillJoin */);
8045    for (int i = 0; i < simCount; i++) {
8046        pthread_rwlock_t *radioServiceRwlockPtr = getRadioServiceRwlock(i);
8047        int ret = pthread_rwlock_wrlock(radioServiceRwlockPtr);
8048        assert(ret == 0);
8049
8050        radioService[i] = new RadioImpl;
8051        radioService[i]->mSlotId = i;
8052        oemHookService[i] = new OemHookImpl;
8053        oemHookService[i]->mSlotId = i;
8054        RLOGD("registerService: starting android::hardware::radio::V1_1::IRadio %s",
8055                serviceNames[i]);
8056        android::status_t status = radioService[i]->registerAsService(serviceNames[i]);
8057        status = oemHookService[i]->registerAsService(serviceNames[i]);
8058
8059        ret = pthread_rwlock_unlock(radioServiceRwlockPtr);
8060        assert(ret == 0);
8061    }
8062
8063    s_vendorFunctions = callbacks;
8064    s_commands = commands;
8065}
8066
8067void rilc_thread_pool() {
8068    joinRpcThreadpool();
8069}
8070
8071pthread_rwlock_t * radio::getRadioServiceRwlock(int slotId) {
8072    pthread_rwlock_t *radioServiceRwlockPtr = &radioServiceRwlock;
8073
8074    #if (SIM_COUNT >= 2)
8075    if (slotId == 2) radioServiceRwlockPtr = &radioServiceRwlock2;
8076    #if (SIM_COUNT >= 3)
8077    if (slotId == 3) radioServiceRwlockPtr = &radioServiceRwlock3;
8078    #if (SIM_COUNT >= 4)
8079    if (slotId == 4) radioServiceRwlockPtr = &radioServiceRwlock4;
8080    #endif
8081    #endif
8082    #endif
8083
8084    return radioServiceRwlockPtr;
8085}
8086