Arkadiusz Bałys | c76b258 | 2022-11-17 20:15:25 +0100 | [diff] [blame] | 1 | /* |
| 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ólik | bfe87de | 2023-01-09 19:04:19 +0100 | [diff] [blame] | 19 | |
| 20 | #include "WiFiManager.h" |
| 21 | |
Arkadiusz Bałys | c76b258 | 2022-11-17 20:15:25 +0100 | [diff] [blame] | 22 | #include <platform/NetworkCommissioning.h> |
| 23 | |
| 24 | namespace chip { |
| 25 | namespace DeviceLayer { |
| 26 | namespace NetworkCommissioning { |
| 27 | |
Karsten Sperling | b15a6e6 | 2023-09-16 03:58:46 +1200 | [diff] [blame] | 28 | inline constexpr uint8_t kMaxWiFiNetworks = 1; |
| 29 | inline constexpr uint8_t kWiFiScanNetworksTimeOutSeconds = 10; |
| 30 | inline constexpr uint8_t kWiFiConnectNetworkTimeoutSeconds = 35; |
Arkadiusz Bałys | c76b258 | 2022-11-17 20:15:25 +0100 | [diff] [blame] | 31 | |
| 32 | class NrfWiFiScanResponseIterator : public Iterator<WiFiScanResponse> |
| 33 | { |
| 34 | public: |
| 35 | size_t Count() override { return mResultCount; } |
| 36 | bool Next(WiFiScanResponse & item) override; |
| 37 | void Release() override; |
| 38 | void Add(const WiFiScanResponse & result); |
| 39 | |
| 40 | private: |
| 41 | size_t mResultId = 0; |
| 42 | size_t mResultCount = 0; |
| 43 | WiFiScanResponse * mResults = nullptr; |
| 44 | }; |
| 45 | |
| 46 | class NrfWiFiDriver final : public WiFiDriver |
| 47 | { |
| 48 | public: |
| 49 | // Define non-volatile storage keys for SSID and password. |
| 50 | // The naming convention is aligned with DefaultStorageKeyAllocator class. |
Michael Spang | 547b587 | 2023-12-05 09:25:54 -0500 | [diff] [blame] | 51 | static constexpr char kSsidKey[] = "g/wi/s"; |
| 52 | static constexpr char kPassKey[] = "g/wi/p"; |
Arkadiusz Bałys | c76b258 | 2022-11-17 20:15:25 +0100 | [diff] [blame] | 53 | |
| 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łys | c76b258 | 2022-11-17 20:15:25 +0100 | [diff] [blame] | 68 | // 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łys | c76b258 | 2022-11-17 20:15:25 +0100 | [diff] [blame] | 96 | void OnNetworkStatusChanged(Status status); |
Damian Królik | bfe87de | 2023-01-09 19:04:19 +0100 | [diff] [blame] | 97 | void OnScanWiFiNetworkResult(const WiFiScanResponse & result); |
| 98 | void OnScanWiFiNetworkDone(WiFiManager::WiFiRequestStatus status); |
Arkadiusz Bałys | c76b258 | 2022-11-17 20:15:25 +0100 | [diff] [blame] | 99 | |
| 100 | private: |
| 101 | void LoadFromStorage(); |
| 102 | |
| 103 | ConnectCallback * mpConnectCallback{ nullptr }; |
| 104 | NetworkStatusChangeCallback * mpNetworkStatusChangeCallback{ nullptr }; |
Damian Królik | bfe87de | 2023-01-09 19:04:19 +0100 | [diff] [blame] | 105 | WiFiManager::WiFiNetwork mStagingNetwork; |
Arkadiusz Bałys | c76b258 | 2022-11-17 20:15:25 +0100 | [diff] [blame] | 106 | NrfWiFiScanResponseIterator mScanResponseIterator; |
| 107 | ScanCallback * mScanCallback{ nullptr }; |
| 108 | }; |
| 109 | |
| 110 | } // namespace NetworkCommissioning |
| 111 | } // namespace DeviceLayer |
| 112 | } // namespace chip |