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