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