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 "hidl_return_util.h"
20 #include "hidl_struct_util.h"
21 #include "wifi_ap_iface.h"
22 #include "wifi_status_util.h"
23 
24 namespace android {
25 namespace hardware {
26 namespace wifi {
27 namespace V1_4 {
28 namespace implementation {
29 using hidl_return_util::validateAndCall;
30 
WifiApIface(const std::string & ifname,const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util)31 WifiApIface::WifiApIface(
32     const std::string& ifname,
33     const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
34     const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util)
35     : ifname_(ifname),
36       legacy_hal_(legacy_hal),
37       iface_util_(iface_util),
38       is_valid_(true) {}
39 
invalidate()40 void WifiApIface::invalidate() {
41     legacy_hal_.reset();
42     is_valid_ = false;
43 }
44 
isValid()45 bool WifiApIface::isValid() { return is_valid_; }
46 
getName()47 std::string WifiApIface::getName() { return ifname_; }
48 
getName(getName_cb hidl_status_cb)49 Return<void> WifiApIface::getName(getName_cb hidl_status_cb) {
50     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
51                            &WifiApIface::getNameInternal, hidl_status_cb);
52 }
53 
getType(getType_cb hidl_status_cb)54 Return<void> WifiApIface::getType(getType_cb hidl_status_cb) {
55     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
56                            &WifiApIface::getTypeInternal, hidl_status_cb);
57 }
58 
setCountryCode(const hidl_array<int8_t,2> & code,setCountryCode_cb hidl_status_cb)59 Return<void> WifiApIface::setCountryCode(const hidl_array<int8_t, 2>& code,
60                                          setCountryCode_cb hidl_status_cb) {
61     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
62                            &WifiApIface::setCountryCodeInternal, hidl_status_cb,
63                            code);
64 }
65 
getValidFrequenciesForBand(V1_0::WifiBand band,getValidFrequenciesForBand_cb hidl_status_cb)66 Return<void> WifiApIface::getValidFrequenciesForBand(
67     V1_0::WifiBand band, getValidFrequenciesForBand_cb hidl_status_cb) {
68     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
69                            &WifiApIface::getValidFrequenciesForBandInternal,
70                            hidl_status_cb, band);
71 }
72 
setMacAddress(const hidl_array<uint8_t,6> & mac,setMacAddress_cb hidl_status_cb)73 Return<void> WifiApIface::setMacAddress(const hidl_array<uint8_t, 6>& mac,
74                                         setMacAddress_cb hidl_status_cb) {
75     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
76                            &WifiApIface::setMacAddressInternal, hidl_status_cb,
77                            mac);
78 }
79 
getFactoryMacAddress(getFactoryMacAddress_cb hidl_status_cb)80 Return<void> WifiApIface::getFactoryMacAddress(
81     getFactoryMacAddress_cb hidl_status_cb) {
82     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
83                            &WifiApIface::getFactoryMacAddressInternal,
84                            hidl_status_cb);
85 }
86 
getNameInternal()87 std::pair<WifiStatus, std::string> WifiApIface::getNameInternal() {
88     return {createWifiStatus(WifiStatusCode::SUCCESS), ifname_};
89 }
90 
getTypeInternal()91 std::pair<WifiStatus, IfaceType> WifiApIface::getTypeInternal() {
92     return {createWifiStatus(WifiStatusCode::SUCCESS), IfaceType::AP};
93 }
94 
setCountryCodeInternal(const std::array<int8_t,2> & code)95 WifiStatus WifiApIface::setCountryCodeInternal(
96     const std::array<int8_t, 2>& code) {
97     legacy_hal::wifi_error legacy_status =
98         legacy_hal_.lock()->setCountryCode(ifname_, code);
99     return createWifiStatusFromLegacyError(legacy_status);
100 }
101 
102 std::pair<WifiStatus, std::vector<WifiChannelInMhz>>
getValidFrequenciesForBandInternal(V1_0::WifiBand band)103 WifiApIface::getValidFrequenciesForBandInternal(V1_0::WifiBand band) {
104     static_assert(sizeof(WifiChannelInMhz) == sizeof(uint32_t),
105                   "Size mismatch");
106     legacy_hal::wifi_error legacy_status;
107     std::vector<uint32_t> valid_frequencies;
108     std::tie(legacy_status, valid_frequencies) =
109         legacy_hal_.lock()->getValidFrequenciesForBand(
110             ifname_, hidl_struct_util::convertHidlWifiBandToLegacy(band));
111     return {createWifiStatusFromLegacyError(legacy_status), valid_frequencies};
112 }
113 
setMacAddressInternal(const std::array<uint8_t,6> & mac)114 WifiStatus WifiApIface::setMacAddressInternal(
115     const std::array<uint8_t, 6>& mac) {
116     bool status = iface_util_.lock()->setMacAddress(ifname_, mac);
117     if (!status) {
118         return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
119     }
120     return createWifiStatus(WifiStatusCode::SUCCESS);
121 }
122 
123 std::pair<WifiStatus, std::array<uint8_t, 6>>
getFactoryMacAddressInternal()124 WifiApIface::getFactoryMacAddressInternal() {
125     std::array<uint8_t, 6> mac =
126         iface_util_.lock()->getFactoryMacAddress(ifname_);
127     if (mac[0] == 0 && mac[1] == 0 && mac[2] == 0 && mac[3] == 0 &&
128         mac[4] == 0 && mac[5] == 0) {
129         return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), mac};
130     }
131     return {createWifiStatus(WifiStatusCode::SUCCESS), mac};
132 }
133 }  // namespace implementation
134 }  // namespace V1_4
135 }  // namespace wifi
136 }  // namespace hardware
137 }  // namespace android
138