OSDN Git Service

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