1 /*
2  * Copyright (C) 2022 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 #ifndef WIFI_H_
18 #define WIFI_H_
19 
20 #include <aidl/android/hardware/wifi/BnWifi.h>
21 #include <android-base/macros.h>
22 #include <utils/Looper.h>
23 
24 #include <functional>
25 
26 #include "aidl_callback_util.h"
27 #include "wifi_chip.h"
28 #include "wifi_feature_flags.h"
29 #include "wifi_legacy_hal.h"
30 #include "wifi_legacy_hal_factory.h"
31 #include "wifi_mode_controller.h"
32 
33 namespace aidl {
34 namespace android {
35 namespace hardware {
36 namespace wifi {
37 
38 /**
39  * Root AIDL interface object used to control the Wifi HAL.
40  */
41 class Wifi : public BnWifi {
42   public:
43     Wifi(const std::shared_ptr<::android::wifi_system::InterfaceTool> iface_tool,
44          const std::shared_ptr<legacy_hal::WifiLegacyHalFactory> legacy_hal_factory,
45          const std::shared_ptr<mode_controller::WifiModeController> mode_controller,
46          const std::shared_ptr<feature_flags::WifiFeatureFlags> feature_flags);
47 
48     bool isValid();
49 
50     // AIDL methods exposed.
51     ndk::ScopedAStatus registerEventCallback(
52             const std::shared_ptr<IWifiEventCallback>& in_callback) override;
53     ndk::ScopedAStatus isStarted(bool* _aidl_return) override;
54     ndk::ScopedAStatus start() override;
55     ndk::ScopedAStatus stop() override;
56     ndk::ScopedAStatus getChipIds(std::vector<int32_t>* _aidl_return) override;
57     ndk::ScopedAStatus getChip(int32_t in_chipId,
58                                std::shared_ptr<IWifiChip>* _aidl_return) override;
59     binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
60 
61   private:
62     enum class RunState { STOPPED, STARTED, STOPPING };
63 
64     // Corresponding worker functions for the AIDL methods.
65     ndk::ScopedAStatus registerEventCallbackInternal(
66             const std::shared_ptr<IWifiEventCallback>& event_callback __unused);
67     ndk::ScopedAStatus startInternal();
68     ndk::ScopedAStatus stopInternal(std::unique_lock<std::recursive_mutex>* lock);
69     std::pair<std::vector<int32_t>, ndk::ScopedAStatus> getChipIdsInternal();
70     std::pair<std::shared_ptr<IWifiChip>, ndk::ScopedAStatus> getChipInternal(int32_t chip_id);
71 
72     ndk::ScopedAStatus initializeModeControllerAndLegacyHal();
73     ndk::ScopedAStatus stopLegacyHalAndDeinitializeModeController(
74             std::unique_lock<std::recursive_mutex>* lock);
75     int32_t getChipIdFromWifiChip(std::shared_ptr<WifiChip>& chip);
76 
77     // Instance is created in this root level |IWifi| AIDL interface object
78     // and shared with all the child AIDL interface objects.
79     std::shared_ptr<::android::wifi_system::InterfaceTool> iface_tool_;
80     std::shared_ptr<legacy_hal::WifiLegacyHalFactory> legacy_hal_factory_;
81     std::shared_ptr<mode_controller::WifiModeController> mode_controller_;
82     std::vector<std::shared_ptr<legacy_hal::WifiLegacyHal>> legacy_hals_;
83     std::shared_ptr<feature_flags::WifiFeatureFlags> feature_flags_;
84     RunState run_state_;
85     std::vector<std::shared_ptr<WifiChip>> chips_;
86     aidl_callback_util::AidlCallbackHandler<IWifiEventCallback> event_cb_handler_;
87 
88     DISALLOW_COPY_AND_ASSIGN(Wifi);
89 };
90 
91 }  // namespace wifi
92 }  // namespace hardware
93 }  // namespace android
94 }  // namespace aidl
95 
96 #endif  // WIFI_H_
97