blob: d0d3da1b156b0d6961c2f9192dd905c013db6ad5 [file] [log] [blame]
Arkadiusz Bałysc76b2582022-11-17 20:15:25 +01001/*
2 *
3 * Copyright (c) 2022 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
Damian Królikbfe87de2023-01-09 19:04:19 +010019
20#include "WiFiManager.h"
21
Arkadiusz Bałysc76b2582022-11-17 20:15:25 +010022#include <platform/NetworkCommissioning.h>
23
24namespace chip {
25namespace DeviceLayer {
26namespace NetworkCommissioning {
27
Karsten Sperlingb15a6e62023-09-16 03:58:46 +120028inline constexpr uint8_t kMaxWiFiNetworks = 1;
29inline constexpr uint8_t kWiFiScanNetworksTimeOutSeconds = 10;
30inline constexpr uint8_t kWiFiConnectNetworkTimeoutSeconds = 35;
Arkadiusz Bałysc76b2582022-11-17 20:15:25 +010031
32class NrfWiFiScanResponseIterator : public Iterator<WiFiScanResponse>
33{
34public:
35 size_t Count() override { return mResultCount; }
36 bool Next(WiFiScanResponse & item) override;
37 void Release() override;
38 void Add(const WiFiScanResponse & result);
39
40private:
41 size_t mResultId = 0;
42 size_t mResultCount = 0;
43 WiFiScanResponse * mResults = nullptr;
44};
45
46class NrfWiFiDriver final : public WiFiDriver
47{
48public:
49 // Define non-volatile storage keys for SSID and password.
50 // The naming convention is aligned with DefaultStorageKeyAllocator class.
Michael Spang547b5872023-12-05 09:25:54 -050051 static constexpr char kSsidKey[] = "g/wi/s";
52 static constexpr char kPassKey[] = "g/wi/p";
Arkadiusz Bałysc76b2582022-11-17 20:15:25 +010053
54 class WiFiNetworkIterator final : public NetworkIterator
55 {
56 public:
57 WiFiNetworkIterator(NrfWiFiDriver * aDriver) : mDriver(aDriver) {}
58 size_t Count() override;
59 bool Next(Network & item) override;
60 void Release() override { delete this; }
61 ~WiFiNetworkIterator() = default;
62
63 private:
64 NrfWiFiDriver * mDriver;
65 bool mExhausted{ false };
66 };
67
Arkadiusz Bałysc76b2582022-11-17 20:15:25 +010068 // BaseDriver
69 NetworkIterator * GetNetworks() override { return new WiFiNetworkIterator(this); }
70 CHIP_ERROR Init(NetworkStatusChangeCallback * networkStatusChangeCallback) override;
71 void Shutdown() override;
72
73 // WirelessDriver
74 uint8_t GetMaxNetworks() override { return kMaxWiFiNetworks; }
75 uint8_t GetScanNetworkTimeoutSeconds() override { return kWiFiScanNetworksTimeOutSeconds; }
76 uint8_t GetConnectNetworkTimeoutSeconds() override { return kWiFiConnectNetworkTimeoutSeconds; }
77
78 CHIP_ERROR CommitConfiguration() override;
79 CHIP_ERROR RevertConfiguration() override;
80
81 Status RemoveNetwork(ByteSpan networkId, MutableCharSpan & outDebugText, uint8_t & outNetworkIndex) override;
82 Status ReorderNetwork(ByteSpan networkId, uint8_t index, MutableCharSpan & outDebugText) override;
83 void ConnectNetwork(ByteSpan networkId, ConnectCallback * callback) override;
84
85 // WiFiDriver
86 Status AddOrUpdateNetwork(ByteSpan ssid, ByteSpan credentials, MutableCharSpan & outDebugText,
87 uint8_t & outNetworkIndex) override;
88 void ScanNetworks(ByteSpan ssid, ScanCallback * callback) override;
89
90 static NrfWiFiDriver & Instance()
91 {
92 static NrfWiFiDriver sInstance;
93 return sInstance;
94 }
95
Arkadiusz Bałysc76b2582022-11-17 20:15:25 +010096 void OnNetworkStatusChanged(Status status);
Damian Królikbfe87de2023-01-09 19:04:19 +010097 void OnScanWiFiNetworkResult(const WiFiScanResponse & result);
98 void OnScanWiFiNetworkDone(WiFiManager::WiFiRequestStatus status);
Arkadiusz Bałysc76b2582022-11-17 20:15:25 +010099
100private:
101 void LoadFromStorage();
102
103 ConnectCallback * mpConnectCallback{ nullptr };
104 NetworkStatusChangeCallback * mpNetworkStatusChangeCallback{ nullptr };
Damian Królikbfe87de2023-01-09 19:04:19 +0100105 WiFiManager::WiFiNetwork mStagingNetwork;
Arkadiusz Bałysc76b2582022-11-17 20:15:25 +0100106 NrfWiFiScanResponseIterator mScanResponseIterator;
107 ScanCallback * mScanCallback{ nullptr };
108};
109
110} // namespace NetworkCommissioning
111} // namespace DeviceLayer
112} // namespace chip