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 <hidl/HidlTransportSupport.h>
21 
22 #include <wifi_system/hostapd_manager.h>
23 #include <wifi_system/interface_tool.h>
24 #include <wifi_system/supplicant_manager.h>
25 
26 #include "hostapd_hidl_test_utils.h"
27 #include "wifi_hidl_test_utils.h"
28 
29 using ::android::sp;
30 using ::android::hardware::configureRpcThreadpool;
31 using ::android::hardware::hidl_string;
32 using ::android::hardware::hidl_vec;
33 using ::android::hardware::joinRpcThreadpool;
34 using ::android::hardware::Return;
35 using ::android::hardware::Void;
36 using ::android::hardware::wifi::hostapd::V1_0::HostapdStatus;
37 using ::android::hardware::wifi::hostapd::V1_0::HostapdStatusCode;
38 using ::android::hardware::wifi::hostapd::V1_0::IHostapd;
39 using ::android::hardware::wifi::V1_0::ChipModeId;
40 using ::android::hardware::wifi::V1_0::IWifiChip;
41 using ::android::wifi_system::HostapdManager;
42 using ::android::wifi_system::SupplicantManager;
43 
44 namespace {
45 // Helper function to initialize the driver and firmware to AP mode
46 // using the vendor HAL HIDL interface.
initilializeDriverAndFirmware(const std::string & wifi_instance_name)47 void initilializeDriverAndFirmware(const std::string& wifi_instance_name) {
48     if (getWifi(wifi_instance_name) != nullptr) {
49         sp<IWifiChip> wifi_chip = getWifiChip(wifi_instance_name);
50         ChipModeId mode_id;
51         EXPECT_TRUE(configureChipToSupportIfaceType(
52             wifi_chip, ::android::hardware::wifi::V1_0::IfaceType::AP, &mode_id));
53     } else {
54         LOG(WARNING) << __func__ << ": Vendor HAL not supported";
55     }
56 }
57 
58 // Helper function to deinitialize the driver and firmware
59 // using the vendor HAL HIDL interface.
deInitilializeDriverAndFirmware(const std::string & wifi_instance_name)60 void deInitilializeDriverAndFirmware(const std::string& wifi_instance_name) {
61     if (getWifi(wifi_instance_name) != nullptr) {
62         stopWifi(wifi_instance_name);
63     } else {
64         LOG(WARNING) << __func__ << ": Vendor HAL not supported";
65     }
66 }
67 }  // namespace
68 
stopSupplicantIfNeeded(const std::string & instance_name)69 void stopSupplicantIfNeeded(const std::string& instance_name) {
70     SupplicantManager supplicant_manager;
71     if (supplicant_manager.IsSupplicantRunning()) {
72         LOG(INFO) << "Supplicant is running, stop supplicant first.";
73         ASSERT_TRUE(supplicant_manager.StopSupplicant());
74         deInitilializeDriverAndFirmware(instance_name);
75         ASSERT_FALSE(supplicant_manager.IsSupplicantRunning());
76     }
77 }
78 
stopHostapd(const std::string & instance_name)79 void stopHostapd(const std::string& instance_name) {
80     HostapdManager hostapd_manager;
81 
82     ASSERT_TRUE(hostapd_manager.StopHostapd());
83     deInitilializeDriverAndFirmware(instance_name);
84 }
85 
startHostapdAndWaitForHidlService(const std::string & wifi_instance_name,const std::string & hostapd_instance_name)86 void startHostapdAndWaitForHidlService(
87     const std::string& wifi_instance_name,
88     const std::string& hostapd_instance_name) {
89     initilializeDriverAndFirmware(wifi_instance_name);
90 
91     HostapdManager hostapd_manager;
92     ASSERT_TRUE(hostapd_manager.StartHostapd());
93 
94     // Wait for hostapd service to come up.
95     IHostapd::getService(hostapd_instance_name);
96 }
97 
is_1_1(const sp<IHostapd> & hostapd)98 bool is_1_1(const sp<IHostapd>& hostapd) {
99     sp<::android::hardware::wifi::hostapd::V1_1::IHostapd> hostapd_1_1 =
100         ::android::hardware::wifi::hostapd::V1_1::IHostapd::castFrom(hostapd);
101     return hostapd_1_1.get() != nullptr;
102 }
103