Added the new RIL request to set SIM power state

The new RIL request for powering up/down the SIM card.

Test: Telephony sanity tests
bug:32224755
Change-Id: I43245ed8178235e45e592d06c2d1bcb71cd69225
diff --git a/include/telephony/ril.h b/include/telephony/ril.h
index 50f9e17..c6f33e3 100644
--- a/include/telephony/ril.h
+++ b/include/telephony/ril.h
@@ -70,13 +70,14 @@
  * RIL_VERSION = 14 : New data structures are added, namely RIL_CarrierMatchType,
  *                    RIL_Carrier, RIL_CarrierRestrictions and RIL_PCO_Data.
  *                    New commands added: RIL_REQUEST_SET_CARRIER_RESTRICTIONS,
- *                    RIL_REQUEST_SET_CARRIER_RESTRICTIONS and
- *                    RIL_UNSOL_PCO_DATA
+ *                    RIL_REQUEST_SET_CARRIER_RESTRICTIONS and RIL_UNSOL_PCO_DATA.
  *
- * RIL_VERSION = 15 : The new parameters for RIL_REQUEST_SETUP_DATA_CALL,
+ * RIL_VERSION = 15 : New commands added:
+ *                    RIL_REQUEST_SEND_DEVICE_STATE,
+ *                    RIL_REQUEST_SET_UNSOLICITED_RESPONSE_FILTER,
+ *                    RIL_REQUEST_SET_SIM_CARD_POWER
+ *                    The new parameters for RIL_REQUEST_SETUP_DATA_CALL,
  *                    Updated data structures: RIL_DataProfileInfo_v15, RIL_InitialAttachApn_v15
- *                    New commands added: RIL_REQUEST_SEND_DEVICE_STATE,
- *                    RIL_REQUEST_SET_UNSOLICITED_RESPONSE_FILTER
  */
 #define RIL_VERSION 12
 #define LAST_IMPRECISE_RIL_VERSION 12 // Better self-documented name
@@ -5366,6 +5367,32 @@
  */
 #define RIL_REQUEST_SET_UNSOLICITED_RESPONSE_FILTER 139
 
+ /**
+  * RIL_REQUEST_SET_SIM_CARD_POWER
+  *
+  * Set SIM card power up or down
+  *
+  * Request is equivalent to inserting and removing the card, with
+  * an additional effect where the ability to detect card removal/insertion
+  * is disabled when the SIM card is powered down.
+  *
+  * This will generate RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED
+  * as if the SIM had been inserted or removed.
+  *
+  * "data" is int *
+  * ((int *)data)[0] is 1 for "SIM POWER UP"
+  * ((int *)data)[0] is 0 for "SIM POWER DOWN"
+  *
+  * "response" is NULL
+  *
+  * Valid errors:
+  *  SUCCESS
+  *  RADIO_NOT_AVAILABLE
+  *  REQUEST_NOT_SUPPORTED
+  *  SIM_ABSENT
+  *  INVALID_ARGUMENTS
+  */
+#define RIL_REQUEST_SET_SIM_CARD_POWER 140
 /***********************************************************************/
 
 /**
diff --git a/libril/ril_commands.h b/libril/ril_commands.h
index 8404624..e4618da 100644
--- a/libril/ril_commands.h
+++ b/libril/ril_commands.h
@@ -152,3 +152,6 @@
     {RIL_REQUEST_GET_ACTIVITY_INFO, dispatchVoid, radio::getModemActivityInfoResponse},
     {RIL_REQUEST_SET_CARRIER_RESTRICTIONS, dispatchCarrierRestrictions, radio::setAllowedCarriersResponse},
     {RIL_REQUEST_GET_CARRIER_RESTRICTIONS, dispatchVoid, radio::getAllowedCarriersResponse},
+    {RIL_REQUEST_SEND_DEVICE_STATE, dispatchVoid, NULL},
+    {RIL_REQUEST_SET_UNSOLICITED_RESPONSE_FILTER, dispatchVoid, NULL},
+    {RIL_REQUEST_SET_SIM_CARD_POWER, dispatchInts, radio::setSimCardPowerResponse},
diff --git a/libril/ril_service.cpp b/libril/ril_service.cpp
index dd2f5fb..5e5c5bd 100644
--- a/libril/ril_service.cpp
+++ b/libril/ril_service.cpp
@@ -416,6 +416,8 @@
 
     Return<void> setIndicationFilter(int32_t serial, int32_t indicationFilter);
 
+    Return<void> setSimCardPower(int32_t serial, bool powerUp);
+
     Return<void> responseAcknowledgement();
 
     void checkReturnStatus(Return<void>& ret);
@@ -589,7 +591,7 @@
 
     android::Parcel p;   // TODO: should delete this after translation of all commands is complete
 
-    int *pInts = (int *)calloc(countInts, sizeof(int));
+    int *pInts = (int *) calloc(countInts, sizeof(int));
     if (pInts == NULL) {
         RLOGE("Memory allocation failed for request %s", requestToString(request));
         pRI->pCI->responseFunction(p, (int) pRI->socket_id, request,
@@ -2039,6 +2041,12 @@
     return Void();
 }
 
+Return<void> RadioImpl::setSimCardPower(int32_t serial, bool powerUp) {
+    RLOGD("RadioImpl::setSimCardPower: serial %d", serial);
+    dispatchInts(serial, mSlotId, RIL_REQUEST_SET_SIM_CARD_POWER, 1, BOOL_TO_INT(powerUp));
+    return Void();
+}
+
 Return<void> RadioImpl::sendDeviceState(int32_t serial, DeviceStateType deviceStateType, bool state) {return Status::ok();}
 
 Return<void> RadioImpl::setIndicationFilter(int32_t serial, int32_t indicationFilter) {return Status::ok();}
@@ -5095,6 +5103,24 @@
     return 0;
 }
 
+int radio::setSimCardPowerResponse(android::Parcel &p, int slotId, int requestNumber,
+                                   int responseType, int serial, RIL_Errno e,
+                                   void *response, size_t responseLen) {
+    RLOGD("radio::setSimCardPowerResponse: serial %d", serial);
+
+    if (radioService[slotId]->mRadioResponse != NULL) {
+        RadioResponseInfo responseInfo = {};
+        populateResponseInfo(responseInfo, serial, responseType, e);
+        Return<void> retStatus
+                = radioService[slotId]->mRadioResponse->setSimCardPowerResponse(responseInfo);
+        radioService[slotId]->checkReturnStatus(retStatus);
+    } else {
+        RLOGE("radio::setSimCardPowerResponse: radioService[%d]->mRadioResponse == NULL", slotId);
+    }
+
+    return 0;
+}
+
 // Radio Indication functions
 
 RadioIndicationType convertIntToRadioIndicationType(int indicationType) {
diff --git a/libril/ril_service.h b/libril/ril_service.h
index 8294e5f..0beda6c 100644
--- a/libril/ril_service.h
+++ b/libril/ril_service.h
@@ -528,6 +528,10 @@
                               int responseType, int serial, RIL_Errno e,
                               void *response, size_t responselen);
 
+int setSimCardPowerResponse(android::Parcel &p, int slotId, int requestNumber,
+                              int responseType, int serial, RIL_Errno e,
+                              void *response, size_t responselen);
+
 void acknowledgeRequest(int slotId, int serial);
 
 void radioStateChangedInd(int slotId, int indicationType, RIL_RadioState radioState);