Skip to content

Commit a2f7548

Browse files
committed
Rework WiFi AP network configuration
1 parent ef79ae9 commit a2f7548

File tree

7 files changed

+144
-73
lines changed

7 files changed

+144
-73
lines changed

components/ocs_pipeline/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ idf_component_register(
3535
"network/local_network_json_pipeline.cpp"
3636

3737
"config/mdns_config.cpp"
38+
"config/ap_network_config.cpp"
3839

3940
REQUIRES
4041
"spiffs"
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2025, Open Control Systems authors
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
#include <cstring>
10+
11+
#include "ocs_algo/storage_ops.h"
12+
#include "ocs_core/log.h"
13+
#include "ocs_pipeline/config/ap_network_config.h"
14+
#include "ocs_status/code_to_str.h"
15+
16+
namespace ocs {
17+
namespace pipeline {
18+
namespace config {
19+
20+
namespace {
21+
22+
const char* log_tag = "ap_network_config";
23+
24+
} // namespace
25+
26+
ApNetworkConfig::ApNetworkConfig(storage::IStorage& storage,
27+
const system::DeviceInfo& device_info) {
28+
memset(ssid_, 0, sizeof(ssid_));
29+
memset(password_, 0, sizeof(password_));
30+
31+
std::string builtin_ssid = device_info.get_fw_name();
32+
builtin_ssid += "-";
33+
builtin_ssid += device_info.get_device_id();
34+
35+
strncpy(ssid_, builtin_ssid.c_str(), sizeof(ssid_));
36+
37+
const auto code = algo::StorageOps::prob_read(storage, password_key_, password_,
38+
max_password_size_);
39+
if (code != status::StatusCode::OK) {
40+
if (code != status::StatusCode::NoData) {
41+
ocs_loge(log_tag, "failed to read WiFi AP password from storage: %s",
42+
status::code_to_str(code));
43+
}
44+
45+
std::string builtin_password = device_info.get_fw_name();
46+
builtin_password += "-";
47+
builtin_password += std::string(device_info.get_device_id(), 7);
48+
49+
strncpy(password_, builtin_password.c_str(), sizeof(password_));
50+
}
51+
}
52+
53+
const char* ApNetworkConfig::get_ssid() const {
54+
return ssid_;
55+
}
56+
57+
const char* ApNetworkConfig::get_password() const {
58+
return password_;
59+
}
60+
61+
uint8_t ApNetworkConfig::get_channel() const {
62+
return CONFIG_OCS_NETWORK_WIFI_AP_CHANNEL;
63+
}
64+
65+
uint8_t ApNetworkConfig::get_max_conn() const {
66+
return CONFIG_OCS_NETWORK_WIFI_AP_MAX_CONN;
67+
}
68+
69+
} // namespace config
70+
} // namespace pipeline
71+
} // namespace ocs
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2025, Open Control Systems authors
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
#pragma once
10+
11+
#include "ocs_core/noncopyable.h"
12+
#include "ocs_storage/istorage.h"
13+
#include "ocs_system/device_info.h"
14+
15+
namespace ocs {
16+
namespace pipeline {
17+
namespace config {
18+
19+
class ApNetworkConfig : public core::NonCopyable<> {
20+
public:
21+
//! Initialize.
22+
//!
23+
//! @params
24+
//! - @p storage to persist WiFi AP configuration.
25+
//! - @p device_info to use as a fallback for WiFi AP configuration.
26+
ApNetworkConfig(storage::IStorage& storage, const system::DeviceInfo& device_info);
27+
28+
//! Return WiFi AP SSID.
29+
const char* get_ssid() const;
30+
31+
//! Return WiFi AP password.
32+
const char* get_password() const;
33+
34+
//! Return WiFi AP channel.
35+
uint8_t get_channel() const;
36+
37+
//! Return the maximum number of simultaneous WiFi STA connections to WiFi AP.
38+
uint8_t get_max_conn() const;
39+
40+
private:
41+
static constexpr const char* password_key_ = "net_ap_pswd";
42+
43+
static const unsigned max_ssid_size_ = 31;
44+
static const unsigned max_password_size_ = 63;
45+
46+
char ssid_[max_ssid_size_ + 1];
47+
char password_[max_password_size_ + 1];
48+
};
49+
50+
} // namespace config
51+
} // namespace pipeline
52+
} // namespace ocs

components/ocs_pipeline/network/local_network_json_pipeline.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ namespace pipeline {
1616
namespace network {
1717

1818
LocalNetworkJsonPipeline::LocalNetworkJsonPipeline(
19-
storage::StorageBuilder& storage_builder,
2019
net::INetworkHandler& handler,
2120
fmt::json::FanoutFormatter& registraton_formatter,
22-
const system::DeviceInfo& device_info) {
23-
network_pipeline_.reset(
24-
new (std::nothrow) LocalNetworkPipeline(storage_builder, handler, device_info));
21+
const config::ApNetworkConfig& config) {
22+
network_pipeline_.reset(new (std::nothrow) LocalNetworkPipeline(handler, config));
2523
configASSERT(network_pipeline_);
2624

2725
network_formatter_.reset(

components/ocs_pipeline/network/local_network_json_pipeline.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
#include "ocs_core/noncopyable.h"
1414
#include "ocs_fmt/json/fanout_formatter.h"
15+
#include "ocs_pipeline/config/ap_network_config.h"
1516
#include "ocs_pipeline/network/local_network_pipeline.h"
1617
#include "ocs_status/code.h"
17-
#include "ocs_storage/storage_builder.h"
1818
#include "ocs_system/device_info.h"
1919

2020
namespace ocs {
@@ -26,14 +26,12 @@ class LocalNetworkJsonPipeline : public core::NonCopyable<> {
2626
//! Initialize.
2727
//!
2828
//! @params
29-
//! - @p storage_builder to register storages for WiFi AP.
3029
//! - @p handler to notify about network connection status.
3130
//! - @p registraton_formatter to format network characteristics.
32-
//! - @p device_info to create a unique SSID for WiFi AP.
33-
LocalNetworkJsonPipeline(storage::StorageBuilder& storage_builder,
34-
net::INetworkHandler& handler,
31+
//! - @p config to read network settings.
32+
LocalNetworkJsonPipeline(net::INetworkHandler& handler,
3533
fmt::json::FanoutFormatter& registraton_formatter,
36-
const system::DeviceInfo& device_info);
34+
const config::ApNetworkConfig& config);
3735

3836
LocalNetworkPipeline& get_network_pipeline();
3937

components/ocs_pipeline/network/local_network_pipeline.cpp

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include <cstring>
1010
#include <string>
1111

12-
#include "freertos/FreeRTOSConfig.h"
13-
1412
#include "ocs_algo/storage_ops.h"
1513
#include "ocs_core/log.h"
1614
#include "ocs_net/ap_network.h"
@@ -28,13 +26,16 @@ const char* log_tag = "local_network_pipeline";
2826

2927
} // namespace
3028

31-
LocalNetworkPipeline::LocalNetworkPipeline(storage::StorageBuilder& storage_builder,
32-
net::INetworkHandler& handler,
33-
const system::DeviceInfo& device_info) {
34-
storage_ = storage_builder.make("net_local_wifi");
35-
configASSERT(storage_);
36-
37-
initialize_network_(handler, device_info);
29+
LocalNetworkPipeline::LocalNetworkPipeline(net::INetworkHandler& handler,
30+
const config::ApNetworkConfig& config) {
31+
network_.reset(new (std::nothrow)
32+
net::ApNetwork(handler,
33+
net::ApNetwork::Params {
34+
.ssid = config.get_ssid(),
35+
.password = config.get_password(),
36+
.channel = config.get_channel(),
37+
.max_connection = config.get_max_conn(),
38+
}));
3839
}
3940

4041
net::IApNetwork& LocalNetworkPipeline::get_network() {
@@ -53,45 +54,6 @@ status::StatusCode LocalNetworkPipeline::start() {
5354
return code;
5455
}
5556

56-
void LocalNetworkPipeline::initialize_network_(net::INetworkHandler& handler,
57-
const system::DeviceInfo& device_info) {
58-
char ssid[max_ssid_size_ + 1];
59-
memset(ssid, 0, sizeof(ssid));
60-
61-
std::string builtin_ssid = device_info.get_fw_name();
62-
builtin_ssid += "-";
63-
builtin_ssid += device_info.get_device_id();
64-
65-
strncpy(ssid, builtin_ssid.c_str(), sizeof(ssid));
66-
67-
char password[max_password_size_ + 1];
68-
memset(password, 0, sizeof(password));
69-
70-
const auto code = algo::StorageOps::prob_read(*storage_, "net_ap_pswd", password,
71-
max_password_size_);
72-
if (code != status::StatusCode::OK) {
73-
if (code != status::StatusCode::NoData) {
74-
ocs_loge(log_tag, "failed to read WiFi AP password from storage: %s",
75-
status::code_to_str(code));
76-
}
77-
78-
std::string builtin_password = device_info.get_fw_name();
79-
builtin_password += "-";
80-
builtin_password += std::string(device_info.get_device_id(), 7);
81-
82-
strncpy(password, builtin_password.c_str(), sizeof(password));
83-
}
84-
85-
network_.reset(new (std::nothrow) net::ApNetwork(
86-
handler,
87-
net::ApNetwork::Params {
88-
.ssid = ssid,
89-
.password = password,
90-
.channel = CONFIG_OCS_NETWORK_WIFI_AP_CHANNEL,
91-
.max_connection = CONFIG_OCS_NETWORK_WIFI_AP_MAX_CONN,
92-
}));
93-
}
94-
9557
status::StatusCode LocalNetworkPipeline::start_() {
9658
auto code = network_->start();
9759
if (code != status::StatusCode::OK) {

components/ocs_pipeline/network/local_network_pipeline.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
#include "ocs_core/noncopyable.h"
1414
#include "ocs_net/ap_network.h"
1515
#include "ocs_net/inetwork_handler.h"
16-
#include "ocs_storage/storage_builder.h"
17-
#include "ocs_system/device_info.h"
16+
#include "ocs_pipeline/config/ap_network_config.h"
1817

1918
namespace ocs {
2019
namespace pipeline {
@@ -26,31 +25,21 @@ class LocalNetworkPipeline : public core::NonCopyable<> {
2625
//! Initialize.
2726
//!
2827
//! @params
29-
//! - @p storage_builder to create storages for network configuration.
3028
//! - @p handler to notify about network connection status.
31-
//! - @p device_info to create a unique access point SSID.
32-
LocalNetworkPipeline(storage::StorageBuilder& storage_builder,
33-
net::INetworkHandler& handler,
34-
const system::DeviceInfo& device_info);
29+
//! - @p config to read network settings.
30+
LocalNetworkPipeline(net::INetworkHandler& handler,
31+
const config::ApNetworkConfig& config);
3532

3633
net::IApNetwork& get_network();
3734

3835
//! Start the WiFi network.
3936
status::StatusCode start();
4037

4138
private:
42-
void initialize_network_(net::INetworkHandler& handler,
43-
const system::DeviceInfo& device_info);
44-
4539
status::StatusCode start_();
4640

47-
static const unsigned max_ssid_size_ = 31;
48-
static const unsigned max_password_size_ = 63;
49-
5041
static const TickType_t wait_start_interval_ = pdMS_TO_TICKS(1000 * 60 * 10);
5142

52-
storage::StorageBuilder::IStoragePtr storage_;
53-
5443
std::unique_ptr<net::ApNetwork> network_;
5544
};
5645

0 commit comments

Comments
 (0)