1 /*
2 * Copyright (C) 2021 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 #pragma once
18
19 #include "supplicant_aidl_test_utils.h"
20 #include "supplicant_legacy_test_utils.h"
21
22 using aidl::android::hardware::wifi::supplicant::IfaceInfo;
23 using aidl::android::hardware::wifi::supplicant::ISupplicant;
24 using aidl::android::hardware::wifi::supplicant::ISupplicantP2pIface;
25 using aidl::android::hardware::wifi::supplicant::ISupplicantStaIface;
26 using aidl::android::hardware::wifi::supplicant::KeyMgmtMask;
27
getStaIfaceName()28 std::string getStaIfaceName() {
29 std::array<char, PROPERTY_VALUE_MAX> buffer;
30 property_get("wifi.interface", buffer.data(), "wlan0");
31 return std::string(buffer.data());
32 }
33
getP2pIfaceName()34 std::string getP2pIfaceName() {
35 std::array<char, PROPERTY_VALUE_MAX> buffer;
36 property_get("wifi.direct.interface", buffer.data(), "p2p0");
37 return std::string(buffer.data());
38 }
39
keyMgmtSupported(std::shared_ptr<ISupplicantStaIface> iface,KeyMgmtMask expected)40 bool keyMgmtSupported(std::shared_ptr<ISupplicantStaIface> iface, KeyMgmtMask expected) {
41 KeyMgmtMask caps;
42 if (!iface->getKeyMgmtCapabilities(&caps).isOk()) {
43 return false;
44 }
45 return !!(static_cast<uint32_t>(caps) & static_cast<uint32_t>(expected));
46 }
47
isFilsSupported(std::shared_ptr<ISupplicantStaIface> iface)48 bool isFilsSupported(std::shared_ptr<ISupplicantStaIface> iface) {
49 KeyMgmtMask filsMask = static_cast<KeyMgmtMask>(
50 static_cast<uint32_t>(KeyMgmtMask::FILS_SHA256) |
51 static_cast<uint32_t>(KeyMgmtMask::FILS_SHA384));
52 return keyMgmtSupported(iface, filsMask);
53 }
54
stopSupplicantService()55 void stopSupplicantService() {
56 // Select method based on whether the HIDL or AIDL
57 // Vendor HAL is available.
58 if (SupplicantAidlTestUtils::useAidlService()) {
59 SupplicantAidlTestUtils::stopSupplicantService();
60 } else {
61 SupplicantLegacyTestUtils::stopSupplicantService();
62 }
63 }
64
initializeService()65 void initializeService() {
66 if (SupplicantAidlTestUtils::useAidlService()) {
67 SupplicantAidlTestUtils::initializeService();
68 } else {
69 SupplicantLegacyTestUtils::initializeService();
70 }
71 }
72
addStaIface(const std::shared_ptr<ISupplicant> supplicant)73 void addStaIface(const std::shared_ptr<ISupplicant> supplicant) {
74 ASSERT_TRUE(supplicant.get());
75 std::shared_ptr<ISupplicantStaIface> iface;
76 ASSERT_TRUE(supplicant->addStaInterface(getStaIfaceName(), &iface).isOk());
77 }
78
addP2pIface(const std::shared_ptr<ISupplicant> supplicant)79 void addP2pIface(const std::shared_ptr<ISupplicant> supplicant) {
80 ASSERT_TRUE(supplicant.get());
81 std::shared_ptr<ISupplicantP2pIface> iface;
82 ASSERT_TRUE(supplicant->addP2pInterface(getP2pIfaceName(), &iface).isOk());
83 }
84
getSupplicant(const char * supplicant_name)85 std::shared_ptr<ISupplicant> getSupplicant(const char* supplicant_name) {
86 std::shared_ptr<ISupplicant> supplicant = ISupplicant::fromBinder(
87 ndk::SpAIBinder(AServiceManager_waitForService(supplicant_name)));
88 addStaIface(supplicant);
89 if (testing::deviceSupportsFeature("android.hardware.wifi.direct")) {
90 addP2pIface(supplicant);
91 }
92 return supplicant;
93 }
94
vecToArrayMacAddr(std::vector<uint8_t> vectorAddr)95 std::array<uint8_t, 6> vecToArrayMacAddr(std::vector<uint8_t> vectorAddr) {
96 std::array<uint8_t, 6> arrayAddr;
97 std::copy(vectorAddr.begin(), vectorAddr.begin() + 6, arrayAddr.begin());
98 return arrayAddr;
99 }
100