Song GUO | 1d06bb2 | 2021-12-21 08:59:48 +0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) 2021 Project CHIP Authors |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #pragma once |
| 19 | |
| 20 | #include <platform/NetworkCommissioning.h> |
| 21 | #include <vector> |
| 22 | |
| 23 | namespace chip { |
| 24 | namespace DeviceLayer { |
| 25 | namespace NetworkCommissioning { |
| 26 | |
| 27 | template <typename T> |
| 28 | class LinuxScanResponseIterator : public Iterator<T> |
| 29 | { |
| 30 | public: |
| 31 | LinuxScanResponseIterator(std::vector<T> * apScanResponse) : mpScanResponse(apScanResponse) {} |
| 32 | size_t Count() override { return mpScanResponse != nullptr ? mpScanResponse->size() : 0; } |
| 33 | bool Next(T & item) override |
| 34 | { |
| 35 | if (mpScanResponse == nullptr || currentIterating >= mpScanResponse->size()) |
| 36 | { |
| 37 | return false; |
| 38 | } |
| 39 | item = (*mpScanResponse)[currentIterating]; |
| 40 | currentIterating++; |
| 41 | return true; |
| 42 | } |
| 43 | void Release() override |
| 44 | { /* nothing to do, we don't hold the ownership of the vector, and users is not expected to hold the ownership in OnFinished for |
| 45 | scan. */ |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | size_t currentIterating = 0; |
Arkadiusz Bokowy | 83deb47 | 2022-07-21 17:30:14 +0200 | [diff] [blame] | 50 | // Note: We cannot post a event in ScheduleLambda since std::vector is not trivial copyable. |
Song GUO | 1d06bb2 | 2021-12-21 08:59:48 +0800 | [diff] [blame] | 51 | std::vector<T> * mpScanResponse; |
| 52 | }; |
| 53 | |
| 54 | #if CHIP_DEVICE_CONFIG_ENABLE_WPA |
| 55 | class LinuxWiFiDriver final : public WiFiDriver |
| 56 | { |
| 57 | public: |
| 58 | class WiFiNetworkIterator final : public NetworkIterator |
| 59 | { |
| 60 | public: |
| 61 | WiFiNetworkIterator(LinuxWiFiDriver * aDriver) : driver(aDriver) {} |
| 62 | size_t Count() override; |
| 63 | bool Next(Network & item) override; |
| 64 | void Release() override { delete this; } |
Andrei Litvin | d80d593 | 2022-03-16 21:15:16 -0400 | [diff] [blame] | 65 | ~WiFiNetworkIterator() override = default; |
Song GUO | 1d06bb2 | 2021-12-21 08:59:48 +0800 | [diff] [blame] | 66 | |
| 67 | private: |
| 68 | LinuxWiFiDriver * driver; |
| 69 | bool exhausted = false; |
| 70 | }; |
| 71 | |
| 72 | struct WiFiNetwork |
| 73 | { |
| 74 | uint8_t ssid[DeviceLayer::Internal::kMaxWiFiSSIDLength]; |
| 75 | uint8_t ssidLen = 0; |
| 76 | uint8_t credentials[DeviceLayer::Internal::kMaxWiFiKeyLength]; |
| 77 | uint8_t credentialsLen = 0; |
| 78 | }; |
| 79 | |
| 80 | // BaseDriver |
| 81 | NetworkIterator * GetNetworks() override { return new WiFiNetworkIterator(this); } |
Song GUO | 32c1015 | 2022-03-16 19:51:02 +0800 | [diff] [blame] | 82 | CHIP_ERROR Init(BaseDriver::NetworkStatusChangeCallback * networkStatusChangeCallback) override; |
Michael Spang | 6387049 | 2022-06-28 08:41:08 -0400 | [diff] [blame] | 83 | void Shutdown() override; |
Song GUO | 1d06bb2 | 2021-12-21 08:59:48 +0800 | [diff] [blame] | 84 | |
| 85 | // WirelessDriver |
| 86 | uint8_t GetMaxNetworks() override { return 1; } |
| 87 | uint8_t GetScanNetworkTimeoutSeconds() override { return 10; } |
| 88 | uint8_t GetConnectNetworkTimeoutSeconds() override { return 20; } |
| 89 | |
| 90 | CHIP_ERROR CommitConfiguration() override; |
| 91 | CHIP_ERROR RevertConfiguration() override; |
| 92 | |
Song GUO | 4005949 | 2022-03-30 23:43:46 +0800 | [diff] [blame] | 93 | Status RemoveNetwork(ByteSpan networkId, MutableCharSpan & outDebugText, uint8_t & outNetworkIndex) override; |
| 94 | Status ReorderNetwork(ByteSpan networkId, uint8_t index, MutableCharSpan & outDebugText) override; |
Song GUO | 1d06bb2 | 2021-12-21 08:59:48 +0800 | [diff] [blame] | 95 | void ConnectNetwork(ByteSpan networkId, ConnectCallback * callback) override; |
| 96 | |
| 97 | // WiFiDriver |
Song GUO | 4005949 | 2022-03-30 23:43:46 +0800 | [diff] [blame] | 98 | Status AddOrUpdateNetwork(ByteSpan ssid, ByteSpan credentials, MutableCharSpan & outDebugText, |
| 99 | uint8_t & outNetworkIndex) override; |
Song GUO | 1d06bb2 | 2021-12-21 08:59:48 +0800 | [diff] [blame] | 100 | void ScanNetworks(ByteSpan ssid, ScanCallback * callback) override; |
| 101 | |
| 102 | private: |
| 103 | bool NetworkMatch(const WiFiNetwork & network, ByteSpan networkId); |
| 104 | |
Song GUO | 1d06bb2 | 2021-12-21 08:59:48 +0800 | [diff] [blame] | 105 | WiFiNetwork mSavedNetwork; |
| 106 | WiFiNetwork mStagingNetwork; |
| 107 | }; |
| 108 | #endif // CHIP_DEVICE_CONFIG_ENABLE_WPA |
| 109 | |
| 110 | #if CHIP_DEVICE_CONFIG_ENABLE_THREAD |
| 111 | class LinuxThreadDriver final : public ThreadDriver |
| 112 | { |
| 113 | public: |
| 114 | class ThreadNetworkIterator final : public NetworkIterator |
| 115 | { |
| 116 | public: |
| 117 | ThreadNetworkIterator(LinuxThreadDriver * aDriver) : driver(aDriver) {} |
| 118 | size_t Count() override; |
| 119 | bool Next(Network & item) override; |
| 120 | void Release() override { delete this; } |
Andrei Litvin | d80d593 | 2022-03-16 21:15:16 -0400 | [diff] [blame] | 121 | ~ThreadNetworkIterator() override = default; |
Song GUO | 1d06bb2 | 2021-12-21 08:59:48 +0800 | [diff] [blame] | 122 | |
| 123 | private: |
| 124 | LinuxThreadDriver * driver; |
| 125 | bool exhausted = false; |
| 126 | }; |
| 127 | |
| 128 | // BaseDriver |
| 129 | NetworkIterator * GetNetworks() override { return new ThreadNetworkIterator(this); } |
Song GUO | 32c1015 | 2022-03-16 19:51:02 +0800 | [diff] [blame] | 130 | CHIP_ERROR Init(BaseDriver::NetworkStatusChangeCallback * networkStatusChangeCallback) override; |
Michael Spang | 6387049 | 2022-06-28 08:41:08 -0400 | [diff] [blame] | 131 | void Shutdown() override; |
Song GUO | 1d06bb2 | 2021-12-21 08:59:48 +0800 | [diff] [blame] | 132 | |
| 133 | // WirelessDriver |
| 134 | uint8_t GetMaxNetworks() override { return 1; } |
| 135 | uint8_t GetScanNetworkTimeoutSeconds() override { return 10; } |
| 136 | uint8_t GetConnectNetworkTimeoutSeconds() override { return 20; } |
| 137 | |
| 138 | CHIP_ERROR CommitConfiguration() override; |
| 139 | CHIP_ERROR RevertConfiguration() override; |
| 140 | |
Song GUO | 4005949 | 2022-03-30 23:43:46 +0800 | [diff] [blame] | 141 | Status RemoveNetwork(ByteSpan networkId, MutableCharSpan & outDebugText, uint8_t & outNetworkIndex) override; |
| 142 | Status ReorderNetwork(ByteSpan networkId, uint8_t index, MutableCharSpan & outDebugText) override; |
Song GUO | 1d06bb2 | 2021-12-21 08:59:48 +0800 | [diff] [blame] | 143 | void ConnectNetwork(ByteSpan networkId, ConnectCallback * callback) override; |
| 144 | |
| 145 | // ThreadDriver |
Song GUO | 4005949 | 2022-03-30 23:43:46 +0800 | [diff] [blame] | 146 | Status AddOrUpdateNetwork(ByteSpan operationalDataset, MutableCharSpan & outDebugText, uint8_t & outNetworkIndex) override; |
Song GUO | 32c1015 | 2022-03-16 19:51:02 +0800 | [diff] [blame] | 147 | void ScanNetworks(ThreadDriver::ScanCallback * callback) override; |
Song GUO | 1d06bb2 | 2021-12-21 08:59:48 +0800 | [diff] [blame] | 148 | |
| 149 | private: |
| 150 | ThreadNetworkIterator mThreadIterator = ThreadNetworkIterator(this); |
| 151 | Thread::OperationalDataset mSavedNetwork; |
| 152 | Thread::OperationalDataset mStagingNetwork; |
| 153 | }; |
| 154 | |
| 155 | #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD |
| 156 | |
Song GUO | 1f862e3 | 2022-04-27 22:52:39 +0800 | [diff] [blame] | 157 | class LinuxEthernetDriver final : public EthernetDriver |
| 158 | { |
| 159 | public: |
| 160 | struct EthernetNetworkIterator final : public NetworkIterator |
| 161 | { |
| 162 | EthernetNetworkIterator() = default; |
| 163 | size_t Count() override { return interfaceNameLen > 0 ? 1 : 0; } |
| 164 | bool Next(Network & item) override |
| 165 | { |
| 166 | if (exhausted) |
| 167 | { |
| 168 | return false; |
| 169 | } |
| 170 | exhausted = true; |
| 171 | memcpy(item.networkID, interfaceName, interfaceNameLen); |
| 172 | item.networkIDLen = interfaceNameLen; |
| 173 | item.connected = true; |
| 174 | return true; |
| 175 | } |
| 176 | void Release() override { delete this; } |
| 177 | ~EthernetNetworkIterator() override = default; |
| 178 | |
| 179 | // Public, but cannot be accessed via NetworkIterator interface. |
| 180 | uint8_t interfaceName[kMaxNetworkIDLen]; |
| 181 | uint8_t interfaceNameLen = 0; |
| 182 | bool exhausted = false; |
| 183 | }; |
| 184 | |
| 185 | uint8_t GetMaxNetworks() override { return 1; }; |
| 186 | NetworkIterator * GetNetworks() override; |
| 187 | }; |
| 188 | |
Song GUO | 1d06bb2 | 2021-12-21 08:59:48 +0800 | [diff] [blame] | 189 | } // namespace NetworkCommissioning |
| 190 | } // namespace DeviceLayer |
| 191 | } // namespace chip |