1 /*
2  * Copyright (C) 2018 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 "nfc_hidl_hal_test"
18 #include <android-base/logging.h>
19 
20 #include <android/hardware/nfc/1.1/INfcClientCallback.h>
21 #include <android/hardware/nfc/1.2/INfc.h>
22 #include <android/hardware/nfc/1.2/types.h>
23 #include <gtest/gtest.h>
24 #include <hardware/nfc.h>
25 #include <hidl/GtestPrinter.h>
26 #include <hidl/ServiceManagement.h>
27 
28 #include <VtsHalHidlTargetCallbackBase.h>
29 
30 using ::android::sp;
31 using ::android::hardware::hidl_vec;
32 using ::android::hardware::Return;
33 using ::android::hardware::Void;
34 using ::android::hardware::nfc::V1_0::NfcData;
35 using ::android::hardware::nfc::V1_0::NfcStatus;
36 using ::android::hardware::nfc::V1_1::INfcClientCallback;
37 using ::android::hardware::nfc::V1_1::NfcEvent;
38 using ::android::hardware::nfc::V1_2::INfc;
39 using ::android::hardware::nfc::V1_2::NfcConfig;
40 
41 // Range of valid off host route ids
42 constexpr unsigned int MIN_OFFHOST_ROUTE_ID = 0x01;
43 constexpr unsigned int MAX_OFFHOST_ROUTE_ID = 0xFE;
44 
45 constexpr char kCallbackNameSendEvent[] = "sendEvent";
46 constexpr char kCallbackNameSendData[] = "sendData";
47 
48 class NfcClientCallbackArgs {
49    public:
50     NfcEvent last_event_;
51     NfcStatus last_status_;
52     NfcData last_data_;
53 };
54 
55 /* Callback class for data & Event. */
56 class NfcClientCallback : public ::testing::VtsHalHidlTargetCallbackBase<NfcClientCallbackArgs>,
57                           public INfcClientCallback {
58    public:
59     virtual ~NfcClientCallback() = default;
60 
61     /* sendEvent callback function - Records the Event & Status
62      * and notifies the TEST
63      **/
sendEvent_1_1(NfcEvent event,NfcStatus event_status)64     Return<void> sendEvent_1_1(NfcEvent event, NfcStatus event_status) override {
65         NfcClientCallbackArgs args;
66         args.last_event_ = event;
67         args.last_status_ = event_status;
68         NotifyFromCallback(kCallbackNameSendEvent, args);
69         return Void();
70     };
71 
72     /** NFC 1.1 HAL shouldn't send 1.0 callbacks */
sendEvent(::android::hardware::nfc::V1_0::NfcEvent event,NfcStatus event_status)73     Return<void> sendEvent(__attribute__((unused))::android::hardware::nfc::V1_0::NfcEvent event,
74                            __attribute__((unused)) NfcStatus event_status) override {
75         return Void();
76     }
77 
78     /* sendData callback function. Records the data and notifies the TEST*/
sendData(const NfcData & data)79     Return<void> sendData(const NfcData& data) override {
80         NfcClientCallbackArgs args;
81         args.last_data_ = data;
82         NotifyFromCallback(kCallbackNameSendData, args);
83         return Void();
84     };
85 };
86 
87 // The main test class for NFC HIDL HAL.
88 class NfcHidlTest : public ::testing::TestWithParam<std::string> {
89    public:
SetUp()90     virtual void SetUp() override {
91         nfc_ = INfc::getService(GetParam());
92         ASSERT_NE(nfc_, nullptr);
93 
94         nfc_cb_ = new NfcClientCallback();
95         ASSERT_NE(nfc_cb_, nullptr);
96 
97         EXPECT_EQ(NfcStatus::OK, nfc_->open_1_1(nfc_cb_));
98         // Wait for OPEN_CPLT event
99         auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
100         EXPECT_TRUE(res.no_timeout);
101         EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
102         EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
103 
104         /*
105          * Close the hal and then re-open to make sure we are in a predictable
106          * state for all the tests.
107          */
108         EXPECT_EQ(NfcStatus::OK, nfc_->close());
109         // Wait for CLOSE_CPLT event
110         res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
111         EXPECT_TRUE(res.no_timeout);
112         EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
113         EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
114 
115         EXPECT_EQ(NfcStatus::OK, nfc_->open_1_1(nfc_cb_));
116         // Wait for OPEN_CPLT event
117         res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
118         EXPECT_TRUE(res.no_timeout);
119         EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
120         EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
121     }
122 
TearDown()123     virtual void TearDown() override {
124         EXPECT_EQ(NfcStatus::OK, nfc_->close());
125         // Wait for CLOSE_CPLT event
126         auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
127         EXPECT_TRUE(res.no_timeout);
128         EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
129         EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
130     }
131 
132     sp<INfc> nfc_;
133     sp<NfcClientCallback> nfc_cb_;
134 };
135 
136 /*
137  * getConfig:
138  * Calls getConfig()
139  * checks if fields in NfcConfig are populated correctly
140  */
TEST_P(NfcHidlTest,GetExtendedConfig)141 TEST_P(NfcHidlTest, GetExtendedConfig) {
142     nfc_->getConfig_1_2([](NfcConfig config) {
143         for (uint8_t uicc : config.offHostRouteUicc) {
144             EXPECT_GE(uicc, MIN_OFFHOST_ROUTE_ID);
145             EXPECT_LE(uicc, MAX_OFFHOST_ROUTE_ID);
146         }
147         for (uint8_t ese : config.offHostRouteEse) {
148             EXPECT_GE(ese, MIN_OFFHOST_ROUTE_ID);
149             EXPECT_LE(ese, MAX_OFFHOST_ROUTE_ID);
150         }
151         if (config.defaultIsoDepRoute != 0) {
152             EXPECT_GE(config.defaultIsoDepRoute, MIN_OFFHOST_ROUTE_ID);
153             EXPECT_LE(config.defaultIsoDepRoute, MAX_OFFHOST_ROUTE_ID);
154         }
155     });
156 }
157 
158 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NfcHidlTest);
159 INSTANTIATE_TEST_SUITE_P(
160         PerInstance, NfcHidlTest,
161         testing::ValuesIn(android::hardware::getAllHalInstanceNames(INfc::descriptor)),
162         android::hardware::PrintInstanceNameToString);
163 
main(int argc,char ** argv)164 int main(int argc, char** argv) {
165     ::testing::InitGoogleTest(&argc, argv);
166 
167     std::system("svc nfc disable"); /* Turn off NFC */
168     sleep(5);
169 
170     int status = RUN_ALL_TESTS();
171     LOG(INFO) << "Test result = " << status;
172 
173     std::system("svc nfc enable"); /* Turn on NFC */
174     sleep(5);
175 
176     return status;
177 }
178