1 /*
2 * Copyright (C) 2016 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 <VtsHalHidlTargetTestBase.h>
18 #include <android-base/logging.h>
19
20 #include <android/hidl/manager/1.0/IServiceManager.h>
21 #include <android/hidl/manager/1.0/IServiceNotification.h>
22 #include <hidl/HidlTransportSupport.h>
23
24 #include <wifi_system/hostapd_manager.h>
25 #include <wifi_system/interface_tool.h>
26 #include <wifi_system/supplicant_manager.h>
27
28 #include "hostapd_hidl_test_utils.h"
29 #include "wifi_hidl_test_utils.h"
30
31 using ::android::sp;
32 using ::android::hardware::configureRpcThreadpool;
33 using ::android::hardware::hidl_string;
34 using ::android::hardware::hidl_vec;
35 using ::android::hardware::joinRpcThreadpool;
36 using ::android::hardware::Return;
37 using ::android::hardware::Void;
38 using ::android::hardware::wifi::hostapd::V1_0::HostapdStatus;
39 using ::android::hardware::wifi::hostapd::V1_0::HostapdStatusCode;
40 using ::android::hardware::wifi::hostapd::V1_0::IHostapd;
41 using ::android::hardware::wifi::V1_0::ChipModeId;
42 using ::android::hardware::wifi::V1_0::IWifiChip;
43 using ::android::hidl::manager::V1_0::IServiceNotification;
44 using ::android::wifi_system::HostapdManager;
45 using ::android::wifi_system::SupplicantManager;
46
47 extern WifiHostapdHidlEnvironment* gEnv;
48
49 namespace {
50 // Helper function to initialize the driver and firmware to AP mode
51 // using the vendor HAL HIDL interface.
initilializeDriverAndFirmware()52 void initilializeDriverAndFirmware() {
53 sp<IWifiChip> wifi_chip = getWifiChip();
54 ChipModeId mode_id;
55 EXPECT_TRUE(configureChipToSupportIfaceType(
56 wifi_chip, ::android::hardware::wifi::V1_0::IfaceType::AP, &mode_id));
57 }
58
59 // Helper function to deinitialize the driver and firmware
60 // using the vendor HAL HIDL interface.
deInitilializeDriverAndFirmware()61 void deInitilializeDriverAndFirmware() { stopWifi(); }
62 } // namespace
63
64 // Utility class to wait for wpa_hostapd's HIDL service registration.
65 class ServiceNotificationListener : public IServiceNotification {
66 public:
onRegistration(const hidl_string & fully_qualified_name,const hidl_string & instance_name,bool pre_existing)67 Return<void> onRegistration(const hidl_string& fully_qualified_name,
68 const hidl_string& instance_name,
69 bool pre_existing) override {
70 if (pre_existing) {
71 return Void();
72 }
73 std::unique_lock<std::mutex> lock(mutex_);
74 registered_.push_back(std::string(fully_qualified_name.c_str()) + "/" +
75 instance_name.c_str());
76 lock.unlock();
77 condition_.notify_one();
78 return Void();
79 }
80
registerForHidlServiceNotifications(const std::string & instance_name)81 bool registerForHidlServiceNotifications(const std::string& instance_name) {
82 if (!IHostapd::registerForNotifications(instance_name, this)) {
83 return false;
84 }
85 configureRpcThreadpool(2, false);
86 return true;
87 }
88
waitForHidlService(uint32_t timeout_in_millis,const std::string & instance_name)89 bool waitForHidlService(uint32_t timeout_in_millis,
90 const std::string& instance_name) {
91 std::unique_lock<std::mutex> lock(mutex_);
92 condition_.wait_for(lock, std::chrono::milliseconds(timeout_in_millis),
93 [&]() { return registered_.size() >= 1; });
94 if (registered_.size() != 1) {
95 return false;
96 }
97 std::string expected_registered =
98 std::string(IHostapd::descriptor) + "/" + instance_name;
99 if (registered_[0] != expected_registered) {
100 LOG(ERROR) << "Expected: " << expected_registered
101 << ", Got: " << registered_[0];
102 return false;
103 }
104 return true;
105 }
106
107 private:
108 std::vector<std::string> registered_{};
109 std::mutex mutex_;
110 std::condition_variable condition_;
111 };
112
stopSupplicantIfNeeded()113 void stopSupplicantIfNeeded() {
114 SupplicantManager supplicant_manager;
115 if (supplicant_manager.IsSupplicantRunning()) {
116 LOG(INFO) << "Supplicant is running, stop supplicant first.";
117 ASSERT_TRUE(supplicant_manager.StopSupplicant());
118 deInitilializeDriverAndFirmware();
119 ASSERT_FALSE(supplicant_manager.IsSupplicantRunning());
120 }
121 }
122
stopHostapd()123 void stopHostapd() {
124 HostapdManager hostapd_manager;
125
126 ASSERT_TRUE(hostapd_manager.StopHostapd());
127 deInitilializeDriverAndFirmware();
128 }
129
startHostapdAndWaitForHidlService()130 void startHostapdAndWaitForHidlService() {
131 initilializeDriverAndFirmware();
132
133 android::sp<ServiceNotificationListener> notification_listener =
134 new ServiceNotificationListener();
135 string service_name = gEnv->getServiceName<IHostapd>();
136 ASSERT_TRUE(notification_listener->registerForHidlServiceNotifications(
137 service_name));
138
139 HostapdManager hostapd_manager;
140 ASSERT_TRUE(hostapd_manager.StartHostapd());
141
142 ASSERT_TRUE(notification_listener->waitForHidlService(200, service_name));
143 }
144
is_1_1(const sp<IHostapd> & hostapd)145 bool is_1_1(const sp<IHostapd>& hostapd) {
146 sp<::android::hardware::wifi::hostapd::V1_1::IHostapd> hostapd_1_1 =
147 ::android::hardware::wifi::hostapd::V1_1::IHostapd::castFrom(hostapd);
148 return hostapd_1_1.get() != nullptr;
149 }
150
getHostapd()151 sp<IHostapd> getHostapd() {
152 return ::testing::VtsHalHidlTargetTestBase::getService<IHostapd>(
153 gEnv->getServiceName<IHostapd>());
154 }
155