OSDN Git Service

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