blob: d54470e70cb7fe9952f412edca2d5eeac03f4401 [file] [log] [blame]
Song GUO1d06bb22021-12-21 08:59:48 +08001/*
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
23namespace chip {
24namespace DeviceLayer {
25namespace NetworkCommissioning {
26
27template <typename T>
28class LinuxScanResponseIterator : public Iterator<T>
29{
30public:
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
48private:
49 size_t currentIterating = 0;
Arkadiusz Bokowy83deb472022-07-21 17:30:14 +020050 // Note: We cannot post a event in ScheduleLambda since std::vector is not trivial copyable.
Song GUO1d06bb22021-12-21 08:59:48 +080051 std::vector<T> * mpScanResponse;
52};
53
54#if CHIP_DEVICE_CONFIG_ENABLE_WPA
55class LinuxWiFiDriver final : public WiFiDriver
56{
57public:
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 Litvind80d5932022-03-16 21:15:16 -040065 ~WiFiNetworkIterator() override = default;
Song GUO1d06bb22021-12-21 08:59:48 +080066
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 GUO32c10152022-03-16 19:51:02 +080082 CHIP_ERROR Init(BaseDriver::NetworkStatusChangeCallback * networkStatusChangeCallback) override;
Michael Spang63870492022-06-28 08:41:08 -040083 void Shutdown() override;
Song GUO1d06bb22021-12-21 08:59:48 +080084
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 GUO40059492022-03-30 23:43:46 +080093 Status RemoveNetwork(ByteSpan networkId, MutableCharSpan & outDebugText, uint8_t & outNetworkIndex) override;
94 Status ReorderNetwork(ByteSpan networkId, uint8_t index, MutableCharSpan & outDebugText) override;
Song GUO1d06bb22021-12-21 08:59:48 +080095 void ConnectNetwork(ByteSpan networkId, ConnectCallback * callback) override;
96
97 // WiFiDriver
Song GUO40059492022-03-30 23:43:46 +080098 Status AddOrUpdateNetwork(ByteSpan ssid, ByteSpan credentials, MutableCharSpan & outDebugText,
99 uint8_t & outNetworkIndex) override;
Song GUO1d06bb22021-12-21 08:59:48 +0800100 void ScanNetworks(ByteSpan ssid, ScanCallback * callback) override;
101
102private:
103 bool NetworkMatch(const WiFiNetwork & network, ByteSpan networkId);
104
Song GUO1d06bb22021-12-21 08:59:48 +0800105 WiFiNetwork mSavedNetwork;
106 WiFiNetwork mStagingNetwork;
107};
108#endif // CHIP_DEVICE_CONFIG_ENABLE_WPA
109
110#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
111class LinuxThreadDriver final : public ThreadDriver
112{
113public:
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 Litvind80d5932022-03-16 21:15:16 -0400121 ~ThreadNetworkIterator() override = default;
Song GUO1d06bb22021-12-21 08:59:48 +0800122
123 private:
124 LinuxThreadDriver * driver;
125 bool exhausted = false;
126 };
127
128 // BaseDriver
129 NetworkIterator * GetNetworks() override { return new ThreadNetworkIterator(this); }
Song GUO32c10152022-03-16 19:51:02 +0800130 CHIP_ERROR Init(BaseDriver::NetworkStatusChangeCallback * networkStatusChangeCallback) override;
Michael Spang63870492022-06-28 08:41:08 -0400131 void Shutdown() override;
Song GUO1d06bb22021-12-21 08:59:48 +0800132
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 GUO40059492022-03-30 23:43:46 +0800141 Status RemoveNetwork(ByteSpan networkId, MutableCharSpan & outDebugText, uint8_t & outNetworkIndex) override;
142 Status ReorderNetwork(ByteSpan networkId, uint8_t index, MutableCharSpan & outDebugText) override;
Song GUO1d06bb22021-12-21 08:59:48 +0800143 void ConnectNetwork(ByteSpan networkId, ConnectCallback * callback) override;
144
145 // ThreadDriver
Song GUO40059492022-03-30 23:43:46 +0800146 Status AddOrUpdateNetwork(ByteSpan operationalDataset, MutableCharSpan & outDebugText, uint8_t & outNetworkIndex) override;
Song GUO32c10152022-03-16 19:51:02 +0800147 void ScanNetworks(ThreadDriver::ScanCallback * callback) override;
Song GUO1d06bb22021-12-21 08:59:48 +0800148
149private:
150 ThreadNetworkIterator mThreadIterator = ThreadNetworkIterator(this);
151 Thread::OperationalDataset mSavedNetwork;
152 Thread::OperationalDataset mStagingNetwork;
153};
154
155#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
156
Song GUO1f862e32022-04-27 22:52:39 +0800157class LinuxEthernetDriver final : public EthernetDriver
158{
159public:
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 GUO1d06bb22021-12-21 08:59:48 +0800189} // namespace NetworkCommissioning
190} // namespace DeviceLayer
191} // namespace chip