1 /*
2  * Copyright (C) 2017 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 #include <android-base/logging.h>
18 
19 #include <chrono>
20 #include <condition_variable>
21 #include <mutex>
22 
23 #include <android/hardware/radio/1.0/ISap.h>
24 #include <android/hardware/radio/1.0/ISapCallback.h>
25 #include <android/hardware/radio/1.0/types.h>
26 #include <gtest/gtest.h>
27 
28 #include "vts_test_util.h"
29 
30 using namespace ::android::hardware::radio::V1_0;
31 
32 using ::android::hardware::hidl_string;
33 using ::android::hardware::hidl_vec;
34 using ::android::hardware::Return;
35 using ::android::hardware::Void;
36 using ::android::sp;
37 
38 #define TIMEOUT_PERIOD 40
39 
40 // HAL instance name for SIM slot 1 or single SIM device
41 #define SAP_SERVICE_SLOT1_NAME "slot1"
42 
43 // HAL instance name for SIM slot 2 on dual SIM device
44 #define SAP_SERVICE_SLOT2_NAME "slot2"
45 
46 // HAL instance name for SIM slot 3 on triple SIM device
47 #define SAP_SERVICE_SLOT3_NAME "slot3"
48 
49 class SapHidlTest;
50 
51 /* Callback class for sap response */
52 class SapCallback : public ISapCallback {
53    private:
54     SapHidlTest& parent;
55 
56    public:
57     SapResultCode sapResultCode;
58     int32_t sapResponseToken;
59 
60     SapCallback(SapHidlTest& parent);
61 
62     virtual ~SapCallback() = default;
63 
64     Return<void> connectResponse(int32_t token, SapConnectRsp sapConnectRsp, int32_t maxMsgSize);
65 
66     Return<void> disconnectResponse(int32_t token);
67 
68     Return<void> disconnectIndication(int32_t token, SapDisconnectType disconnectType);
69 
70     Return<void> apduResponse(int32_t token, SapResultCode resultCode,
71                               const ::android::hardware::hidl_vec<uint8_t>& apduRsp);
72 
73     Return<void> transferAtrResponse(int32_t token, SapResultCode resultCode,
74                                      const ::android::hardware::hidl_vec<uint8_t>& atr);
75 
76     Return<void> powerResponse(int32_t token, SapResultCode resultCode);
77 
78     Return<void> resetSimResponse(int32_t token, SapResultCode resultCode);
79 
80     Return<void> statusIndication(int32_t token, SapStatus status);
81 
82     Return<void> transferCardReaderStatusResponse(int32_t token, SapResultCode resultCode,
83                                                   int32_t cardReaderStatus);
84 
85     Return<void> errorResponse(int32_t token);
86 
87     Return<void> transferProtocolResponse(int32_t token, SapResultCode resultCode);
88 };
89 
90 // The main test class for Sap HIDL.
91 class SapHidlTest : public ::testing::TestWithParam<std::string> {
92   private:
93     std::mutex mtx;
94     std::condition_variable cv;
95     int count;
96 
97    public:
98     virtual void SetUp() override;
99 
100     virtual void TearDown() override;
101 
102     /* Used as a mechanism to inform the test about data/event callback */
103     void notify(int receivedToken);
104 
105     /* Test code calls this function to wait for response */
106     std::cv_status wait();
107 
108     /* Sap service */
109     sp<ISap> sap;
110 
111     /* Sap Callback object */
112     sp<SapCallback> sapCb;
113 
114     /* Token for sap request */
115     int32_t token;
116 };
117