Add non-dds check for startNetworkScan

Providing non-dds exemption for HAL 1.2 network scan due to some devices
only perform network scan on preferred data sim. Since logical modem
id(0/1) and physical sim slot id(0/1) are intuitively aligned. Set
first sim as dds sim, and skip the network scan on the second one
(non-dds).

Test: Vts
Bug: 135243177
Change-Id: I58b89473714dc6d3ce6567ba1809baad6cd6d799
(cherry picked from commit fc85e8fa50ccae2cd0e538260ccb9e62c109b181)
diff --git a/radio/1.2/vts/functional/Android.bp b/radio/1.2/vts/functional/Android.bp
index 6782f14..c5838a8 100644
--- a/radio/1.2/vts/functional/Android.bp
+++ b/radio/1.2/vts/functional/Android.bp
@@ -20,6 +20,7 @@
     srcs: [
         "radio_hidl_hal_api.cpp",
         "radio_hidl_hal_test.cpp",
+        "radio_config_response.cpp",
         "radio_response.cpp",
         "radio_indication.cpp",
         "VtsHalRadioV1_2TargetTest.cpp",
@@ -29,6 +30,8 @@
         "android.hardware.radio@1.2",
         "android.hardware.radio@1.1",
         "android.hardware.radio@1.0",
+        "android.hardware.radio.config@1.0",
+        "android.hardware.radio.config@1.1",
     ],
     header_libs: ["radio.util.header@1.0"],
     test_suites: ["general-tests"],
diff --git a/radio/1.2/vts/functional/radio_config_response.cpp b/radio/1.2/vts/functional/radio_config_response.cpp
new file mode 100644
index 0000000..71b7497
--- /dev/null
+++ b/radio/1.2/vts/functional/radio_config_response.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <radio_hidl_hal_utils_v1_2.h>
+
+RadioConfigResponse::RadioConfigResponse(RadioHidlTest_v1_2& parent) : parent_v1_2(parent) {}
+
+Return<void> RadioConfigResponse::getSimSlotsStatusResponse(
+        const RadioResponseInfo& info,
+        const ::android::hardware::hidl_vec<SimSlotStatus>& slotStatus) {
+    rspInfo = info;
+    simSlotStatus = slotStatus;
+    parent_v1_2.notify(info.serial);
+    return Void();
+}
+
+Return<void> RadioConfigResponse::setSimSlotsMappingResponse(const RadioResponseInfo& /* info */) {
+    return Void();
+}
+
+Return<void> RadioConfigResponse::getPhoneCapabilityResponse(
+        const RadioResponseInfo& info, const PhoneCapability& phoneCapability) {
+    rspInfo = info;
+    phoneCap = phoneCapability;
+    parent_v1_2.notify(info.serial);
+    return Void();
+}
+
+Return<void> RadioConfigResponse::setPreferredDataModemResponse(const RadioResponseInfo& info) {
+    rspInfo = info;
+    parent_v1_2.notify(info.serial);
+    return Void();
+}
+
+Return<void> RadioConfigResponse::getModemsConfigResponse(const RadioResponseInfo& info,
+                                                          const ModemsConfig& /* mConfig */) {
+    rspInfo = info;
+    parent_v1_2.notify(info.serial);
+    return Void();
+}
+
+Return<void> RadioConfigResponse::setModemsConfigResponse(const RadioResponseInfo& info) {
+    rspInfo = info;
+    parent_v1_2.notify(info.serial);
+    return Void();
+}
diff --git a/radio/1.2/vts/functional/radio_hidl_hal_api.cpp b/radio/1.2/vts/functional/radio_hidl_hal_api.cpp
index f11f0d8..5184ef9 100644
--- a/radio/1.2/vts/functional/radio_hidl_hal_api.cpp
+++ b/radio/1.2/vts/functional/radio_hidl_hal_api.cpp
@@ -34,6 +34,15 @@
 TEST_F(RadioHidlTest_v1_2, startNetworkScan) {
     serial = GetRandomSerialNumber();
 
+    if (radioConfig != NULL && DDS_LOGICAL_SLOT_INDEX != logicalSlotId) {
+        // Some DSDS devices have a limitation that network scans can only be performed on the
+        // logical modem that currently used for packet data. For now, skip the test on the
+        // non-data SIM. This exemption is removed in HAL version 1.4. See b/135243177 for
+        // additional information.
+        ALOGI("Skip network scan on non-dds SIM, slot id = %d", logicalSlotId);
+        return;
+    }
+
     ::android::hardware::radio::V1_2::NetworkScanRequest request = {
             .type = ScanType::ONE_SHOT,
             .interval = 60,
diff --git a/radio/1.2/vts/functional/radio_hidl_hal_test.cpp b/radio/1.2/vts/functional/radio_hidl_hal_test.cpp
index bff7481..835d9b8 100644
--- a/radio/1.2/vts/functional/radio_hidl_hal_test.cpp
+++ b/radio/1.2/vts/functional/radio_hidl_hal_test.cpp
@@ -37,6 +37,7 @@
     ASSERT_NE(nullptr, radioRsp_v1_2.get());
 
     count_ = 0;
+    logicalSlotId = -1;
 
     radioInd_v1_2 = new (std::nothrow) RadioIndication_v1_2(*this);
     ASSERT_NE(nullptr, radioInd_v1_2.get());
@@ -50,6 +51,71 @@
 
     /* Enforce Vts Testing with Sim Status Present only. */
     EXPECT_EQ(CardState::PRESENT, cardStatus.base.cardState);
+
+    radioConfig = ::testing::VtsHalHidlTargetTestBase::getService<
+            ::android::hardware::radio::config::V1_1::IRadioConfig>();
+
+    /* Enforce Vts tesing with RadioConfig for network scan excemption. */
+    // Some devices can only perform network scan on logical modem that currently used for packet
+    // data. This exemption is removed in HAL version 1.4. See b/135243177 for additional info.
+    if (radioConfig != NULL) {
+        // RadioConfig 1.1 available, some devices fall in excepmtion category.
+        ASSERT_NE(nullptr, radioConfig.get());
+
+        radioConfigRsp = new (std::nothrow) RadioConfigResponse(*this);
+        ASSERT_NE(nullptr, radioConfigRsp.get());
+
+        /* Set radio config response functions */
+        radioConfig->setResponseFunctions(radioConfigRsp, nullptr);
+
+        /* set preferred data modem */
+        setPreferredDataModem();
+
+        /* get current logical sim id */
+        getLogicalSimId();
+    }
+}
+
+void RadioHidlTest_v1_2::getLogicalSimId() {
+    serial = GetRandomSerialNumber();
+    radioConfig->getSimSlotsStatus(serial);
+    EXPECT_EQ(std::cv_status::no_timeout, wait());
+    EXPECT_EQ(RadioResponseType::SOLICITED, radioConfigRsp->rspInfo.type);
+    EXPECT_EQ(serial, radioConfigRsp->rspInfo.serial);
+
+    ASSERT_TRUE(CheckAnyOfErrors(radioConfigRsp->rspInfo.error,
+                                 {RadioError::NONE, RadioError::REQUEST_NOT_SUPPORTED}));
+
+    if (radioConfigRsp->rspInfo.error != RadioError ::NONE) {
+        ALOGI("Failed to get sim slot status, rspInfo.error = %s\n",
+              toString(radioConfigRsp->rspInfo.error).c_str());
+        return;
+    }
+
+    if (cardStatus.physicalSlotId < 0 ||
+        cardStatus.physicalSlotId >= radioConfigRsp->simSlotStatus.size()) {
+        ALOGI("Physical slot id: %d is out of range", cardStatus.physicalSlotId);
+        return;
+    }
+
+    logicalSlotId = radioConfigRsp->simSlotStatus[cardStatus.physicalSlotId].logicalSlotId;
+}
+
+/*
+ * Set preferred data modem
+ */
+void RadioHidlTest_v1_2::setPreferredDataModem() {
+    serial = GetRandomSerialNumber();
+    // Even for single sim device, the setPreferredDataModem should still success. Enforce dds on
+    // first logical modem.
+    radioConfig->setPreferredDataModem(serial, DDS_LOGICAL_SLOT_INDEX);
+    EXPECT_EQ(std::cv_status::no_timeout, wait());
+    EXPECT_EQ(RadioResponseType::SOLICITED, radioConfigRsp->rspInfo.type);
+    EXPECT_EQ(serial, radioConfigRsp->rspInfo.serial);
+
+    ASSERT_TRUE(CheckAnyOfErrors(
+            radioConfigRsp->rspInfo.error,
+            {RadioError::NONE, RadioError::RADIO_NOT_AVAILABLE, RadioError::INTERNAL_ERR}));
 }
 
 /*
diff --git a/radio/1.2/vts/functional/radio_hidl_hal_utils_v1_2.h b/radio/1.2/vts/functional/radio_hidl_hal_utils_v1_2.h
index 3f780e5..2db1cac 100644
--- a/radio/1.2/vts/functional/radio_hidl_hal_utils_v1_2.h
+++ b/radio/1.2/vts/functional/radio_hidl_hal_utils_v1_2.h
@@ -22,6 +22,10 @@
 #include <condition_variable>
 #include <mutex>
 
+#include <android/hardware/radio/config/1.1/IRadioConfig.h>
+#include <android/hardware/radio/config/1.1/IRadioConfigResponse.h>
+#include <android/hardware/radio/config/1.1/types.h>
+
 #include <android/hardware/radio/1.2/IRadio.h>
 #include <android/hardware/radio/1.2/IRadioIndication.h>
 #include <android/hardware/radio/1.2/IRadioResponse.h>
@@ -32,13 +36,17 @@
 using namespace ::android::hardware::radio::V1_2;
 using namespace ::android::hardware::radio::V1_1;
 using namespace ::android::hardware::radio::V1_0;
+using namespace ::android::hardware::radio::config::V1_1;
 
+using ::android::sp;
 using ::android::hardware::hidl_bitfield;
 using ::android::hardware::hidl_string;
 using ::android::hardware::hidl_vec;
 using ::android::hardware::Return;
 using ::android::hardware::Void;
-using ::android::sp;
+using ::android::hardware::radio::config::V1_0::SimSlotStatus;
+using ::android::hardware::radio::V1_0::RadioResponseInfo;
+using ::android::hardware::radio::V1_0::RadioResponseType;
 
 #define TIMEOUT_PERIOD 75
 #define RADIO_SERVICE_NAME "slot1"
@@ -46,6 +54,36 @@
 class RadioHidlTest_v1_2;
 extern ::android::hardware::radio::V1_2::CardStatus cardStatus;
 
+/* Callback class for radio config response */
+class RadioConfigResponse : public IRadioConfigResponse {
+  protected:
+    RadioHidlTest_v1_2& parent_v1_2;
+
+  public:
+    RadioResponseInfo rspInfo;
+    PhoneCapability phoneCap;
+    hidl_vec<SimSlotStatus> simSlotStatus;
+
+    RadioConfigResponse(RadioHidlTest_v1_2& parent_v1_2);
+    virtual ~RadioConfigResponse() = default;
+
+    Return<void> getSimSlotsStatusResponse(
+            const RadioResponseInfo& info,
+            const ::android::hardware::hidl_vec<SimSlotStatus>& slotStatus);
+
+    Return<void> setSimSlotsMappingResponse(const RadioResponseInfo& info);
+
+    Return<void> getPhoneCapabilityResponse(const RadioResponseInfo& info,
+                                            const PhoneCapability& phoneCapability);
+
+    Return<void> setPreferredDataModemResponse(const RadioResponseInfo& info);
+
+    Return<void> getModemsConfigResponse(const RadioResponseInfo& info,
+                                         const ModemsConfig& mConfig);
+
+    Return<void> setModemsConfigResponse(const RadioResponseInfo& info);
+};
+
 /* Callback class for radio response v1_2*/
 class RadioResponse_v1_2 : public ::android::hardware::radio::V1_2::IRadioResponse {
    protected:
@@ -616,15 +654,27 @@
     std::condition_variable cv_;
     int count_;
 
+    /* Preferred data sim id */
+    const int DDS_LOGICAL_SLOT_INDEX = 0;
+
     /* Serial number for radio request */
     int serial;
 
+    /* Current logical slot id */
+    int logicalSlotId;
+
     /* Update Sim Card Status */
     void updateSimCardStatus();
 
     /* Stop Network Scan Command */
     void stopNetworkScan();
 
+    /* Set preferred data modem */
+    void setPreferredDataModem();
+
+    /* get current logical sim id */
+    void getLogicalSimId();
+
   public:
     virtual void SetUp() override;
 
@@ -642,4 +692,10 @@
 
     /* radio indication handle */
     sp<RadioIndication_v1_2> radioInd_v1_2;
+
+    /* radio config response handle */
+    sp<RadioConfigResponse> radioConfigRsp;
+
+    /* radio config service handle */
+    sp<IRadioConfig> radioConfig;
 };
\ No newline at end of file