OSDN Git Service

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