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 #ifndef WIFICOND_OFFLOAD_SCAN_MANAGER_H_
17 #define WIFICOND_OFFLOAD_SCAN_MANAGER_H_
18 
19 #include <android/hardware/wifi/offload/1.0/IOffload.h>
20 #include "wificond/scanning/offload/offload_callback.h"
21 #include "wificond/scanning/offload/offload_callback_handlers.h"
22 #include "wificond/scanning/offload_scan_callback_interface_impl.h"
23 
24 #include <vector>
25 
26 namespace com {
27 namespace android {
28 namespace server {
29 namespace wifi {
30 namespace wificond {
31 
32 class NativeScanResult;
33 class NativeScanStats;
34 
35 }  // namespace wificond
36 }  // namespace wifi
37 }  // namespace server
38 }  // namespace android
39 }  // namespace com
40 
41 namespace android {
42 namespace wificond {
43 
44 class OffloadScanManager;
45 class OffloadDeathRecipient;
46 class OffloadServiceUtils;
47 
48 // Provides callback interface implementation from Offload HAL
49 class OffloadCallbackHandlersImpl : public OffloadCallbackHandlers {
50  public:
51   OffloadCallbackHandlersImpl(OffloadScanManager* parent);
52   ~OffloadCallbackHandlersImpl() override;
53 
54   void OnScanResultHandler(
55       const std::vector<android::hardware::wifi::offload::V1_0::ScanResult>&
56           scanResult) override;
57   void OnErrorHandler(
58       const android::hardware::wifi::offload::V1_0::OffloadStatus& status)
59       override;
60 
61  private:
62   OffloadScanManager* offload_scan_manager_;
63 };
64 
65 // Provides methods to interact with Offload HAL
66 class OffloadScanManager {
67  public:
68   enum StatusCode {
69     /* Corresponds to OffloadStatusCode::OK */
70     kNoError,
71     /* Offload HAL service not avaialble */
72     kNoService,
73     /* Corresponds to OffloadStatusCode::NO_CONNECTION */
74     kNotConnected,
75     /* Corresponds to OffloadStatusCode::TIMEOUT */
76     kTimeOut,
77     /* Corresponds to OffloadStatusCode::ERROR */
78     kError
79   };
80 
81   enum ReasonCode {
82     /* Default value */
83     kNone,
84     /* Offload HAL scans is not available */
85     kNotAvailable,
86     /* Offload HAL requested operation failure */
87     kOperationFailed,
88     /* Binder failed to deliver message to Offload HAL*/
89     kTransactionFailed,
90   };
91 
92   explicit OffloadScanManager(
93       std::weak_ptr<OffloadServiceUtils> utils,
94       std::shared_ptr<OffloadScanCallbackInterface> callback);
95   virtual ~OffloadScanManager();
96   /* Request start of offload scans with scan parameters and scan filter
97    * settings. Internally calls Offload HAL service with configureScans()
98    * and subscribeScanResults() APIs. Reason code indicates failure reason.
99    */
100   virtual bool startScan(
101       uint32_t /* interval_ms */, int32_t /* rssi_threshold */,
102       const std::vector<std::vector<uint8_t>>& /* scan_ssids */,
103       const std::vector<std::vector<uint8_t>>& /* match_ssids */,
104       const std::vector<uint8_t>& /* match_security */,
105       const std::vector<uint32_t>& /* freqs */,
106       ReasonCode* /* failure reason */);
107   /* Request stop of offload scans, returns true if the operation succeeds
108    * Otherwise, returns false. Reason code is updated in case of failure.
109    */
110   virtual bool stopScan(ReasonCode* /* failure reason */);
111   /* Get statistics for scans performed by Offload HAL */
112   virtual bool getScanStats(
113       ::com::android::server::wifi::wificond::NativeScanStats* /* scanStats */);
114   /* Otain status of the Offload HAL service */
115   virtual StatusCode getOffloadStatus() const;
116   /* Returns the most recent scan result available from Offload HAL */
117   virtual bool getScanResults(
118       std::vector<::com::android::server::wifi::wificond::NativeScanResult>*
119           out_scan_results);
120 
121  private:
122   void ReportScanResults(
123       const std::vector<android::hardware::wifi::offload::V1_0::ScanResult>&
124           scanResult);
125   void ReportError(
126       const android::hardware::wifi::offload::V1_0::OffloadStatus& status);
127   bool VerifyAndConvertHIDLStatus(
128       std::pair<android::hardware::wifi::offload::V1_0::OffloadStatus, bool>
129           result,
130       OffloadScanManager::ReasonCode* reason_code);
131   bool GetScanStats(
132       ::com::android::server::wifi::wificond::NativeScanStats* stats);
133   bool SubscribeScanResults(
134       OffloadScanManager::ReasonCode* reason_code);
135   bool ConfigureScans(
136       android::hardware::wifi::offload::V1_0::ScanParam,
137       android::hardware::wifi::offload::V1_0::ScanFilter,
138       OffloadScanManager::ReasonCode* reason_code);
139   bool InitServiceIfNeeded();
140   bool InitService();
141 
142   /* Handle binder death */
143   void OnObjectDeath(uint64_t /* cookie */);
144 
145   android::sp<android::hardware::wifi::offload::V1_0::IOffload>
146       wifi_offload_hal_;
147   android::sp<OffloadCallback> wifi_offload_callback_;
148   android::sp<OffloadDeathRecipient> death_recipient_;
149   StatusCode offload_status_;
150   std::vector<::com::android::server::wifi::wificond::NativeScanResult>
151       cached_scan_results_;
152   bool service_available_;
153 
154   const std::weak_ptr<OffloadServiceUtils> offload_service_utils_;
155   const std::shared_ptr<OffloadCallbackHandlersImpl> offload_callback_handlers_;
156   std::shared_ptr<OffloadScanCallbackInterface> event_callback_;
157 
158   friend class OffloadCallbackHandlersImpl;
159 };
160 
161 }  // namespace wificond
162 }  // namespace android
163 
164 #endif  // WIFICOND_OFFLOAD_SCAN_MANAGER_H_
165