14547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran/*
24547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran * Copyright (C) 2017 The Android Open Source Project
34547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran *
44547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran * Licensed under the Apache License, Version 2.0 (the "License");
54547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran * you may not use this file except in compliance with the License.
64547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran * You may obtain a copy of the License at
74547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran *
84547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran *      http://www.apache.org/licenses/LICENSE-2.0
94547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran *
104547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran * Unless required by applicable law or agreed to in writing, software
114547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran * distributed under the License is distributed on an "AS IS" BASIS,
124547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran * See the License for the specific language governing permissions and
144547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran * limitations under the License.
154547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran */
164547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran
174547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran#include <radio_hidl_hal_utils_v1_2.h>
184547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran
194547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindranvoid RadioHidlTest_v1_2::SetUp() {
2079bafb943bb53e48351cd159724164c7526976c4sqian    radio_v1_2 =
2179bafb943bb53e48351cd159724164c7526976c4sqian        ::testing::VtsHalHidlTargetTestBase::getService<::android::hardware::radio::V1_2::IRadio>(
2279bafb943bb53e48351cd159724164c7526976c4sqian            RadioHidlEnvironment::Instance()
2379bafb943bb53e48351cd159724164c7526976c4sqian                ->getServiceName<::android::hardware::radio::V1_2::IRadio>(
2479bafb943bb53e48351cd159724164c7526976c4sqian                    hidl_string(RADIO_SERVICE_NAME)));
25fe32af21bdc1ced4a2736d2f3f5fe9ccdf0e36f1sqian    if (radio_v1_2 == NULL) {
26fe32af21bdc1ced4a2736d2f3f5fe9ccdf0e36f1sqian        sleep(60);
2779bafb943bb53e48351cd159724164c7526976c4sqian        radio_v1_2 = ::testing::VtsHalHidlTargetTestBase::getService<
2879bafb943bb53e48351cd159724164c7526976c4sqian            ::android::hardware::radio::V1_2::IRadio>(
2979bafb943bb53e48351cd159724164c7526976c4sqian            RadioHidlEnvironment::Instance()
3079bafb943bb53e48351cd159724164c7526976c4sqian                ->getServiceName<::android::hardware::radio::V1_2::IRadio>(
3179bafb943bb53e48351cd159724164c7526976c4sqian                    hidl_string(RADIO_SERVICE_NAME)));
32fe32af21bdc1ced4a2736d2f3f5fe9ccdf0e36f1sqian    }
334547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    ASSERT_NE(nullptr, radio_v1_2.get());
344547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran
354547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    radioRsp_v1_2 = new (std::nothrow) RadioResponse_v1_2(*this);
364547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    ASSERT_NE(nullptr, radioRsp_v1_2.get());
374547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran
384547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    count_ = 0;
394547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran
404547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    radioInd_v1_2 = new (std::nothrow) RadioIndication_v1_2(*this);
414547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    ASSERT_NE(nullptr, radioInd_v1_2.get());
424547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran
434547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    radio_v1_2->setResponseFunctions(radioRsp_v1_2, radioInd_v1_2);
444547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran
45788eb8083059ff0dca686734ceebf942042c4893sqian    updateSimCardStatus();
464547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_v1_2->rspInfo.type);
474547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    EXPECT_EQ(serial, radioRsp_v1_2->rspInfo.serial);
484547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    EXPECT_EQ(RadioError::NONE, radioRsp_v1_2->rspInfo.error);
49788eb8083059ff0dca686734ceebf942042c4893sqian
50788eb8083059ff0dca686734ceebf942042c4893sqian    /* Enforce Vts Testing with Sim Status Present only. */
51788eb8083059ff0dca686734ceebf942042c4893sqian    EXPECT_EQ(CardState::PRESENT, cardStatus.base.cardState);
524547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran}
534547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran
544547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran/*
554547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran * Notify that the response message is received.
564547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran */
57788eb8083059ff0dca686734ceebf942042c4893sqianvoid RadioHidlTest_v1_2::notify(int receivedSerial) {
584547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    std::unique_lock<std::mutex> lock(mtx_);
59788eb8083059ff0dca686734ceebf942042c4893sqian    if (serial == receivedSerial) {
60788eb8083059ff0dca686734ceebf942042c4893sqian        count_++;
61788eb8083059ff0dca686734ceebf942042c4893sqian        cv_.notify_one();
62788eb8083059ff0dca686734ceebf942042c4893sqian    }
634547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran}
644547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran
654547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran/*
664547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran * Wait till the response message is notified or till TIMEOUT_PERIOD.
674547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran */
684547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindranstd::cv_status RadioHidlTest_v1_2::wait() {
694547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    std::unique_lock<std::mutex> lock(mtx_);
704547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran
714547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    std::cv_status status = std::cv_status::no_timeout;
724547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    auto now = std::chrono::system_clock::now();
734547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    while (count_ == 0) {
744547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran        status = cv_.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
754547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran        if (status == std::cv_status::timeout) {
764547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran            return status;
774547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran        }
784547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    }
794547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    count_--;
804547419e6c0615b06b5b74832f24ca4027fe8e75Sooraj Sasindran    return status;
81788eb8083059ff0dca686734ceebf942042c4893sqian}
82788eb8083059ff0dca686734ceebf942042c4893sqian
83788eb8083059ff0dca686734ceebf942042c4893sqianvoid RadioHidlTest_v1_2::updateSimCardStatus() {
84788eb8083059ff0dca686734ceebf942042c4893sqian    serial = GetRandomSerialNumber();
85788eb8083059ff0dca686734ceebf942042c4893sqian    radio_v1_2->getIccCardStatus(serial);
86788eb8083059ff0dca686734ceebf942042c4893sqian    EXPECT_EQ(std::cv_status::no_timeout, wait());
8779bafb943bb53e48351cd159724164c7526976c4sqian}