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 namespace {
48 // Helper function to initialize the driver and firmware to AP mode
49 // using the vendor HAL HIDL interface.
initilializeDriverAndFirmware(const std::string & wifi_instance_name)50 void initilializeDriverAndFirmware(const std::string& wifi_instance_name) {
51 sp<IWifiChip> wifi_chip = getWifiChip(wifi_instance_name);
52 ChipModeId mode_id;
53 EXPECT_TRUE(configureChipToSupportIfaceType(
54 wifi_chip, ::android::hardware::wifi::V1_0::IfaceType::AP, &mode_id));
55 }
56
57 // Helper function to deinitialize the driver and firmware
58 // using the vendor HAL HIDL interface.
deInitilializeDriverAndFirmware(const std::string & wifi_instance_name)59 void deInitilializeDriverAndFirmware(const std::string& wifi_instance_name) {
60 stopWifi(wifi_instance_name);
61 }
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(const std::string & instance_name)113 void stopSupplicantIfNeeded(const std::string& instance_name) {
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(instance_name);
119 ASSERT_FALSE(supplicant_manager.IsSupplicantRunning());
120 }
121 }
122
stopHostapd(const std::string & instance_name)123 void stopHostapd(const std::string& instance_name) {
124 HostapdManager hostapd_manager;
125
126 ASSERT_TRUE(hostapd_manager.StopHostapd());
127 deInitilializeDriverAndFirmware(instance_name);
128 }
129
startHostapdAndWaitForHidlService(const std::string & wifi_instance_name,const std::string & hostapd_instance_name)130 void startHostapdAndWaitForHidlService(
131 const std::string& wifi_instance_name,
132 const std::string& hostapd_instance_name) {
133 initilializeDriverAndFirmware(wifi_instance_name);
134
135 android::sp<ServiceNotificationListener> notification_listener =
136 new ServiceNotificationListener();
137 ASSERT_TRUE(notification_listener->registerForHidlServiceNotifications(
138 hostapd_instance_name));
139
140 HostapdManager hostapd_manager;
141 ASSERT_TRUE(hostapd_manager.StartHostapd());
142
143 ASSERT_TRUE(
144 notification_listener->waitForHidlService(500, hostapd_instance_name));
145 }
146
is_1_1(const sp<IHostapd> & hostapd)147 bool is_1_1(const sp<IHostapd>& hostapd) {
148 sp<::android::hardware::wifi::hostapd::V1_1::IHostapd> hostapd_1_1 =
149 ::android::hardware::wifi::hostapd::V1_1::IHostapd::castFrom(hostapd);
150 return hostapd_1_1.get() != nullptr;
151 }