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_rtt_controller.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 
WifiRttController(const std::string & iface_name,const sp<IWifiIface> & bound_iface,const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal)31 WifiRttController::WifiRttController(
32     const std::string& iface_name, const sp<IWifiIface>& bound_iface,
33     const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal)
34     : ifname_(iface_name),
35       bound_iface_(bound_iface),
36       legacy_hal_(legacy_hal),
37       is_valid_(true) {}
38 
invalidate()39 void WifiRttController::invalidate() {
40     legacy_hal_.reset();
41     event_callbacks_.clear();
42     is_valid_ = false;
43 }
44 
isValid()45 bool WifiRttController::isValid() { return is_valid_; }
46 
47 std::vector<sp<IWifiRttControllerEventCallback>>
getEventCallbacks()48 WifiRttController::getEventCallbacks() {
49     return event_callbacks_;
50 }
51 
getIfaceName()52 std::string WifiRttController::getIfaceName() { return ifname_; }
53 
getBoundIface(getBoundIface_cb hidl_status_cb)54 Return<void> WifiRttController::getBoundIface(getBoundIface_cb hidl_status_cb) {
55     return validateAndCall(
56         this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
57         &WifiRttController::getBoundIfaceInternal, hidl_status_cb);
58 }
59 
registerEventCallback(const sp<V1_0::IWifiRttControllerEventCallback> & callback,registerEventCallback_cb hidl_status_cb)60 Return<void> WifiRttController::registerEventCallback(
61     const sp<V1_0::IWifiRttControllerEventCallback>& callback,
62     registerEventCallback_cb hidl_status_cb) {
63     return validateAndCall(this,
64                            WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
65                            &WifiRttController::registerEventCallbackInternal,
66                            hidl_status_cb, callback);
67 }
68 
rangeRequest(uint32_t cmd_id,const hidl_vec<V1_0::RttConfig> & rtt_configs,rangeRequest_cb hidl_status_cb)69 Return<void> WifiRttController::rangeRequest(
70     uint32_t cmd_id, const hidl_vec<V1_0::RttConfig>& rtt_configs,
71     rangeRequest_cb hidl_status_cb) {
72     return validateAndCall(this,
73                            WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
74                            &WifiRttController::rangeRequestInternal,
75                            hidl_status_cb, cmd_id, rtt_configs);
76 }
77 
rangeCancel(uint32_t cmd_id,const hidl_vec<hidl_array<uint8_t,6>> & addrs,rangeCancel_cb hidl_status_cb)78 Return<void> WifiRttController::rangeCancel(
79     uint32_t cmd_id, const hidl_vec<hidl_array<uint8_t, 6>>& addrs,
80     rangeCancel_cb hidl_status_cb) {
81     return validateAndCall(
82         this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
83         &WifiRttController::rangeCancelInternal, hidl_status_cb, cmd_id, addrs);
84 }
85 
getCapabilities(getCapabilities_cb hidl_status_cb)86 Return<void> WifiRttController::getCapabilities(
87     getCapabilities_cb hidl_status_cb) {
88     return validateAndCall(
89         this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
90         &WifiRttController::getCapabilitiesInternal, hidl_status_cb);
91 }
92 
setLci(uint32_t cmd_id,const RttLciInformation & lci,setLci_cb hidl_status_cb)93 Return<void> WifiRttController::setLci(uint32_t cmd_id,
94                                        const RttLciInformation& lci,
95                                        setLci_cb hidl_status_cb) {
96     return validateAndCall(
97         this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
98         &WifiRttController::setLciInternal, hidl_status_cb, cmd_id, lci);
99 }
100 
setLcr(uint32_t cmd_id,const RttLcrInformation & lcr,setLcr_cb hidl_status_cb)101 Return<void> WifiRttController::setLcr(uint32_t cmd_id,
102                                        const RttLcrInformation& lcr,
103                                        setLcr_cb hidl_status_cb) {
104     return validateAndCall(
105         this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
106         &WifiRttController::setLcrInternal, hidl_status_cb, cmd_id, lcr);
107 }
108 
getResponderInfo(getResponderInfo_cb hidl_status_cb)109 Return<void> WifiRttController::getResponderInfo(
110     getResponderInfo_cb hidl_status_cb) {
111     return validateAndCall(
112         this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
113         &WifiRttController::getResponderInfoInternal, hidl_status_cb);
114 }
115 
enableResponder(uint32_t cmd_id,const WifiChannelInfo & channel_hint,uint32_t max_duration_seconds,const V1_0::RttResponder & info,enableResponder_cb hidl_status_cb)116 Return<void> WifiRttController::enableResponder(
117     uint32_t cmd_id, const WifiChannelInfo& channel_hint,
118     uint32_t max_duration_seconds, const V1_0::RttResponder& info,
119     enableResponder_cb hidl_status_cb) {
120     return validateAndCall(
121         this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
122         &WifiRttController::enableResponderInternal, hidl_status_cb, cmd_id,
123         channel_hint, max_duration_seconds, info);
124 }
125 
disableResponder(uint32_t cmd_id,disableResponder_cb hidl_status_cb)126 Return<void> WifiRttController::disableResponder(
127     uint32_t cmd_id, disableResponder_cb hidl_status_cb) {
128     return validateAndCall(
129         this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
130         &WifiRttController::disableResponderInternal, hidl_status_cb, cmd_id);
131 }
132 
registerEventCallback_1_4(const sp<IWifiRttControllerEventCallback> & callback,registerEventCallback_1_4_cb hidl_status_cb)133 Return<void> WifiRttController::registerEventCallback_1_4(
134     const sp<IWifiRttControllerEventCallback>& callback,
135     registerEventCallback_1_4_cb hidl_status_cb) {
136     return validateAndCall(
137         this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
138         &WifiRttController::registerEventCallbackInternal_1_4, hidl_status_cb,
139         callback);
140 }
141 
rangeRequest_1_4(uint32_t cmd_id,const hidl_vec<RttConfig> & rtt_configs,rangeRequest_1_4_cb hidl_status_cb)142 Return<void> WifiRttController::rangeRequest_1_4(
143     uint32_t cmd_id, const hidl_vec<RttConfig>& rtt_configs,
144     rangeRequest_1_4_cb hidl_status_cb) {
145     return validateAndCall(this,
146                            WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
147                            &WifiRttController::rangeRequestInternal_1_4,
148                            hidl_status_cb, cmd_id, rtt_configs);
149 }
150 
getCapabilities_1_4(getCapabilities_1_4_cb hidl_status_cb)151 Return<void> WifiRttController::getCapabilities_1_4(
152     getCapabilities_1_4_cb hidl_status_cb) {
153     return validateAndCall(
154         this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
155         &WifiRttController::getCapabilitiesInternal_1_4, hidl_status_cb);
156 }
157 
getResponderInfo_1_4(getResponderInfo_1_4_cb hidl_status_cb)158 Return<void> WifiRttController::getResponderInfo_1_4(
159     getResponderInfo_1_4_cb hidl_status_cb) {
160     return validateAndCall(
161         this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
162         &WifiRttController::getResponderInfoInternal_1_4, hidl_status_cb);
163 }
164 
enableResponder_1_4(uint32_t cmd_id,const WifiChannelInfo & channel_hint,uint32_t max_duration_seconds,const RttResponder & info,enableResponder_1_4_cb hidl_status_cb)165 Return<void> WifiRttController::enableResponder_1_4(
166     uint32_t cmd_id, const WifiChannelInfo& channel_hint,
167     uint32_t max_duration_seconds, const RttResponder& info,
168     enableResponder_1_4_cb hidl_status_cb) {
169     return validateAndCall(
170         this, WifiStatusCode::ERROR_WIFI_RTT_CONTROLLER_INVALID,
171         &WifiRttController::enableResponderInternal_1_4, hidl_status_cb, cmd_id,
172         channel_hint, max_duration_seconds, info);
173 }
174 
175 std::pair<WifiStatus, sp<IWifiIface>>
getBoundIfaceInternal()176 WifiRttController::getBoundIfaceInternal() {
177     return {createWifiStatus(WifiStatusCode::SUCCESS), bound_iface_};
178 }
179 
registerEventCallbackInternal(const sp<V1_0::IWifiRttControllerEventCallback> &)180 WifiStatus WifiRttController::registerEventCallbackInternal(
181     const sp<V1_0::IWifiRttControllerEventCallback>& /* callback */) {
182     // Deprecated support for this api
183     return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
184 }
185 
rangeRequestInternal(uint32_t,const std::vector<V1_0::RttConfig> &)186 WifiStatus WifiRttController::rangeRequestInternal(
187     uint32_t /* cmd_id */,
188     const std::vector<V1_0::RttConfig>& /* rtt_configs */) {
189     // Deprecated support for this api
190     return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
191 }
192 
rangeCancelInternal(uint32_t cmd_id,const std::vector<hidl_array<uint8_t,6>> & addrs)193 WifiStatus WifiRttController::rangeCancelInternal(
194     uint32_t cmd_id, const std::vector<hidl_array<uint8_t, 6>>& addrs) {
195     std::vector<std::array<uint8_t, 6>> legacy_addrs;
196     for (const auto& addr : addrs) {
197         legacy_addrs.push_back(addr);
198     }
199     legacy_hal::wifi_error legacy_status =
200         legacy_hal_.lock()->cancelRttRangeRequest(ifname_, cmd_id,
201                                                   legacy_addrs);
202     return createWifiStatusFromLegacyError(legacy_status);
203 }
204 
205 std::pair<WifiStatus, V1_0::RttCapabilities>
getCapabilitiesInternal()206 WifiRttController::getCapabilitiesInternal() {
207     // Deprecated support for this api
208     return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
209 }
210 
setLciInternal(uint32_t cmd_id,const RttLciInformation & lci)211 WifiStatus WifiRttController::setLciInternal(uint32_t cmd_id,
212                                              const RttLciInformation& lci) {
213     legacy_hal::wifi_lci_information legacy_lci;
214     if (!hidl_struct_util::convertHidlRttLciInformationToLegacy(lci,
215                                                                 &legacy_lci)) {
216         return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
217     }
218     legacy_hal::wifi_error legacy_status =
219         legacy_hal_.lock()->setRttLci(ifname_, cmd_id, legacy_lci);
220     return createWifiStatusFromLegacyError(legacy_status);
221 }
222 
setLcrInternal(uint32_t cmd_id,const RttLcrInformation & lcr)223 WifiStatus WifiRttController::setLcrInternal(uint32_t cmd_id,
224                                              const RttLcrInformation& lcr) {
225     legacy_hal::wifi_lcr_information legacy_lcr;
226     if (!hidl_struct_util::convertHidlRttLcrInformationToLegacy(lcr,
227                                                                 &legacy_lcr)) {
228         return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
229     }
230     legacy_hal::wifi_error legacy_status =
231         legacy_hal_.lock()->setRttLcr(ifname_, cmd_id, legacy_lcr);
232     return createWifiStatusFromLegacyError(legacy_status);
233 }
234 
235 std::pair<WifiStatus, V1_0::RttResponder>
getResponderInfoInternal()236 WifiRttController::getResponderInfoInternal() {
237     // Deprecated support for this api
238     return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
239 }
240 
enableResponderInternal(uint32_t,const WifiChannelInfo &,uint32_t,const V1_0::RttResponder &)241 WifiStatus WifiRttController::enableResponderInternal(
242     uint32_t /* cmd_id */, const WifiChannelInfo& /* channel_hint */,
243     uint32_t /* max_duration_seconds */, const V1_0::RttResponder& /* info */) {
244     // Deprecated support for this api
245     return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED)};
246 }
247 
disableResponderInternal(uint32_t cmd_id)248 WifiStatus WifiRttController::disableResponderInternal(uint32_t cmd_id) {
249     legacy_hal::wifi_error legacy_status =
250         legacy_hal_.lock()->disableRttResponder(ifname_, cmd_id);
251     return createWifiStatusFromLegacyError(legacy_status);
252 }
253 
registerEventCallbackInternal_1_4(const sp<IWifiRttControllerEventCallback> & callback)254 WifiStatus WifiRttController::registerEventCallbackInternal_1_4(
255     const sp<IWifiRttControllerEventCallback>& callback) {
256     // TODO(b/31632518): remove the callback when the client is destroyed
257     event_callbacks_.emplace_back(callback);
258     return createWifiStatus(WifiStatusCode::SUCCESS);
259 }
260 
rangeRequestInternal_1_4(uint32_t cmd_id,const std::vector<RttConfig> & rtt_configs)261 WifiStatus WifiRttController::rangeRequestInternal_1_4(
262     uint32_t cmd_id, const std::vector<RttConfig>& rtt_configs) {
263     std::vector<legacy_hal::wifi_rtt_config> legacy_configs;
264     if (!hidl_struct_util::convertHidlVectorOfRttConfigToLegacy(
265             rtt_configs, &legacy_configs)) {
266         return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
267     }
268     android::wp<WifiRttController> weak_ptr_this(this);
269     const auto& on_results_callback =
270         [weak_ptr_this](
271             legacy_hal::wifi_request_id id,
272             const std::vector<const legacy_hal::wifi_rtt_result*>& results) {
273             const auto shared_ptr_this = weak_ptr_this.promote();
274             if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
275                 LOG(ERROR) << "Callback invoked on an invalid object";
276                 return;
277             }
278             std::vector<RttResult> hidl_results;
279             if (!hidl_struct_util::convertLegacyVectorOfRttResultToHidl(
280                     results, &hidl_results)) {
281                 LOG(ERROR) << "Failed to convert rtt results to HIDL structs";
282                 return;
283             }
284             for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
285                 callback->onResults_1_4(id, hidl_results);
286             }
287         };
288     legacy_hal::wifi_error legacy_status =
289         legacy_hal_.lock()->startRttRangeRequest(
290             ifname_, cmd_id, legacy_configs, on_results_callback);
291     return createWifiStatusFromLegacyError(legacy_status);
292 }
293 
294 std::pair<WifiStatus, RttCapabilities>
getCapabilitiesInternal_1_4()295 WifiRttController::getCapabilitiesInternal_1_4() {
296     legacy_hal::wifi_error legacy_status;
297     legacy_hal::wifi_rtt_capabilities legacy_caps;
298     std::tie(legacy_status, legacy_caps) =
299         legacy_hal_.lock()->getRttCapabilities(ifname_);
300     if (legacy_status != legacy_hal::WIFI_SUCCESS) {
301         return {createWifiStatusFromLegacyError(legacy_status), {}};
302     }
303     RttCapabilities hidl_caps;
304     if (!hidl_struct_util::convertLegacyRttCapabilitiesToHidl(legacy_caps,
305                                                               &hidl_caps)) {
306         return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
307     }
308     return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
309 }
310 
311 std::pair<WifiStatus, RttResponder>
getResponderInfoInternal_1_4()312 WifiRttController::getResponderInfoInternal_1_4() {
313     legacy_hal::wifi_error legacy_status;
314     legacy_hal::wifi_rtt_responder legacy_responder;
315     std::tie(legacy_status, legacy_responder) =
316         legacy_hal_.lock()->getRttResponderInfo(ifname_);
317     if (legacy_status != legacy_hal::WIFI_SUCCESS) {
318         return {createWifiStatusFromLegacyError(legacy_status), {}};
319     }
320     RttResponder hidl_responder;
321     if (!hidl_struct_util::convertLegacyRttResponderToHidl(legacy_responder,
322                                                            &hidl_responder)) {
323         return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
324     }
325     return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_responder};
326 }
327 
enableResponderInternal_1_4(uint32_t cmd_id,const WifiChannelInfo & channel_hint,uint32_t max_duration_seconds,const RttResponder & info)328 WifiStatus WifiRttController::enableResponderInternal_1_4(
329     uint32_t cmd_id, const WifiChannelInfo& channel_hint,
330     uint32_t max_duration_seconds, const RttResponder& info) {
331     legacy_hal::wifi_channel_info legacy_channel_info;
332     if (!hidl_struct_util::convertHidlWifiChannelInfoToLegacy(
333             channel_hint, &legacy_channel_info)) {
334         return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
335     }
336     legacy_hal::wifi_rtt_responder legacy_responder;
337     if (!hidl_struct_util::convertHidlRttResponderToLegacy(info,
338                                                            &legacy_responder)) {
339         return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
340     }
341     legacy_hal::wifi_error legacy_status =
342         legacy_hal_.lock()->enableRttResponder(
343             ifname_, cmd_id, legacy_channel_info, max_duration_seconds,
344             legacy_responder);
345     return createWifiStatusFromLegacyError(legacy_status);
346 }
347 }  // namespace implementation
348 }  // namespace V1_4
349 }  // namespace wifi
350 }  // namespace hardware
351 }  // namespace android
352