OSDN Git Service

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