OSDN Git Service

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