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
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::joinRpcThreadpool;
33 using ::android::hardware::hidl_string;
34 using ::android::hardware::hidl_vec;
35 using ::android::hardware::Return;
36 using ::android::hardware::Void;
37 using ::android::hardware::wifi::V1_0::ChipModeId;
38 using ::android::hardware::wifi::V1_0::IWifiChip;
39 using ::android::hardware::wifi::hostapd::V1_0::IHostapd;
40 using ::android::hardware::wifi::hostapd::V1_0::HostapdStatus;
41 using ::android::hardware::wifi::hostapd::V1_0::HostapdStatusCode;
42 using ::android::hidl::manager::V1_0::IServiceNotification;
43 using ::android::wifi_system::HostapdManager;
44
45 extern WifiHostapdHidlEnvironment* gEnv;
46
47 namespace {
48 // Helper function to initialize the driver and firmware to AP mode
49 // using the vendor HAL HIDL interface.
initilializeDriverAndFirmware()50 void initilializeDriverAndFirmware() {
51 sp<IWifiChip> wifi_chip = getWifiChip();
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()59 void deInitilializeDriverAndFirmware() { stopWifi(); }
60 } // namespace
61
62 // Utility class to wait for wpa_hostapd's HIDL service registration.
63 class ServiceNotificationListener : public IServiceNotification {
64 public:
onRegistration(const hidl_string & fully_qualified_name,const hidl_string & instance_name,bool pre_existing)65 Return<void> onRegistration(const hidl_string& fully_qualified_name,
66 const hidl_string& instance_name,
67 bool pre_existing) override {
68 if (pre_existing) {
69 return Void();
70 }
71 std::unique_lock<std::mutex> lock(mutex_);
72 registered_.push_back(std::string(fully_qualified_name.c_str()) + "/" +
73 instance_name.c_str());
74 lock.unlock();
75 condition_.notify_one();
76 return Void();
77 }
78
registerForHidlServiceNotifications(const std::string & instance_name)79 bool registerForHidlServiceNotifications(const std::string& instance_name) {
80 if (!IHostapd::registerForNotifications(instance_name, this)) {
81 return false;
82 }
83 configureRpcThreadpool(2, false);
84 return true;
85 }
86
waitForHidlService(uint32_t timeout_in_millis,const std::string & instance_name)87 bool waitForHidlService(uint32_t timeout_in_millis,
88 const std::string& instance_name) {
89 std::unique_lock<std::mutex> lock(mutex_);
90 condition_.wait_for(lock, std::chrono::milliseconds(timeout_in_millis),
91 [&]() { return registered_.size() >= 1; });
92 if (registered_.size() != 1) {
93 return false;
94 }
95 std::string expected_registered =
96 std::string(IHostapd::descriptor) + "/" + instance_name;
97 if (registered_[0] != expected_registered) {
98 LOG(ERROR) << "Expected: " << expected_registered
99 << ", Got: " << registered_[0];
100 return false;
101 }
102 return true;
103 }
104
105 private:
106 std::vector<std::string> registered_{};
107 std::mutex mutex_;
108 std::condition_variable condition_;
109 };
110
stopHostapd()111 void stopHostapd() {
112 HostapdManager hostapd_manager;
113
114 ASSERT_TRUE(hostapd_manager.StopHostapd());
115 deInitilializeDriverAndFirmware();
116 }
117
startHostapdAndWaitForHidlService()118 void startHostapdAndWaitForHidlService() {
119 initilializeDriverAndFirmware();
120
121 android::sp<ServiceNotificationListener> notification_listener =
122 new ServiceNotificationListener();
123 string service_name = gEnv->getServiceName<IHostapd>();
124 ASSERT_TRUE(notification_listener->registerForHidlServiceNotifications(
125 service_name));
126
127 HostapdManager hostapd_manager;
128 ASSERT_TRUE(hostapd_manager.StartHostapd());
129
130 ASSERT_TRUE(notification_listener->waitForHidlService(200, service_name));
131 }
132
getHostapd()133 sp<IHostapd> getHostapd() {
134 return ::testing::VtsHalHidlTargetTestBase::getService<IHostapd>(
135 gEnv->getServiceName<IHostapd>());
136 }
137