OSDN Git Service

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