Skip to content

Commit

Permalink
Rework local network pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
dshil committed Nov 20, 2024
1 parent 5074eef commit ec38db4
Show file tree
Hide file tree
Showing 19 changed files with 370 additions and 307 deletions.
20 changes: 16 additions & 4 deletions components/ocs_net/ap_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ ApNetwork::~ApNetwork() {
configASSERT(esp_netif_deinit() == ESP_ERR_NOT_SUPPORTED);
}

IApNetwork::Info ApNetwork::get_info() {
core::LockGuard lock(mu_);

return IApNetwork::Info {
.channel = params_.channel,
.cur_connection = connection_count_,
.max_connection = params_.max_connection,
};
}

status::StatusCode ApNetwork::start() {
const auto err = esp_wifi_start();
if (err != ESP_OK) {
Expand Down Expand Up @@ -104,10 +114,6 @@ status::StatusCode ApNetwork::wait(TickType_t wait) {
return code_;
}

std::optional<ip_addr_t> ApNetwork::get_ip_addr() const {
return std::nullopt;
}

void ApNetwork::handle_event_(void* event_arg,
esp_event_base_t event_base,
int32_t event_id,
Expand Down Expand Up @@ -172,6 +178,9 @@ void ApNetwork::handle_wifi_event_ap_sta_stop_() {
}

void ApNetwork::handle_wifi_event_ap_sta_connected_(void* event_data) {
core::LockGuard lock(mu_);
++connection_count_;

wifi_event_ap_staconnected_t event =
*static_cast<wifi_event_ap_staconnected_t*>(event_data);

Expand All @@ -180,6 +189,9 @@ void ApNetwork::handle_wifi_event_ap_sta_connected_(void* event_data) {
}

void ApNetwork::handle_wifi_event_ap_sta_disconnected_(void* event_data) {
core::LockGuard lock(mu_);
--connection_count_;

wifi_event_ap_staconnected_t event =
*static_cast<wifi_event_ap_staconnected_t*>(event_data);

Expand Down
23 changes: 12 additions & 11 deletions components/ocs_net/ap_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "ocs_core/noncopyable.h"
#include "ocs_core/static_event_group.h"
#include "ocs_core/static_mutex.h"
#include "ocs_net/basic_network.h"
#include "ocs_net/iap_network.h"
#include "ocs_net/inetwork_handler.h"
#include "ocs_net/netif_builder.h"
#include "ocs_status/code.h"
Expand All @@ -25,7 +25,7 @@ namespace ocs {
namespace net {

//! Handle WiFi AP (access-point) mode network operations.
class ApNetwork : public BasicNetwork, public core::NonCopyable<> {
class ApNetwork : public IApNetwork, public core::NonCopyable<> {
public:
struct Params {
//! WiFi SSID.
Expand All @@ -35,10 +35,10 @@ class ApNetwork : public BasicNetwork, public core::NonCopyable<> {
std::string password;

//! WiFi channel.
int channel { 0 };
uint8_t channel { 0 };

//! Maximum number of simultaneous STA connection to the AP.
int max_connection { 0 };
//! Maximum number of simultaneous STA connections to the AP.
uint8_t max_connection { 0 };
};

//! Initialize.
Expand All @@ -50,17 +50,17 @@ class ApNetwork : public BasicNetwork, public core::NonCopyable<> {
//! Destroy.
~ApNetwork();

//! Return various network characteristics.
IApNetwork::Info get_info() override;

//! Start the WiFi connection process.
status::StatusCode start() override;
status::StatusCode start();

//! Stop the WiFi connection process.
status::StatusCode stop() override;
status::StatusCode stop();

//! Wait for the WiFi connection to be established.
status::StatusCode wait(TickType_t wait = portMAX_DELAY) override;

//! Return received IP address.
std::optional<ip_addr_t> get_ip_addr() const override;
status::StatusCode wait(TickType_t wait = portMAX_DELAY);

private:
static void handle_event_(void* event_arg,
Expand All @@ -85,6 +85,7 @@ class ApNetwork : public BasicNetwork, public core::NonCopyable<> {
core::StaticMutex mu_;
core::Cond cond_;
status::StatusCode code_ { status::StatusCode::Last };
uint8_t connection_count_ { 0 };
};

} // namespace net
Expand Down
41 changes: 0 additions & 41 deletions components/ocs_net/basic_network.h

This file was deleted.

37 changes: 37 additions & 0 deletions components/ocs_net/iap_network.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2024, Open Control Systems authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#pragma once

#include <cstdint>

namespace ocs {
namespace net {

class IApNetwork {
public:
struct Info {
//! WiFi channel.
uint8_t channel { 0 };

//! Current number of STA connections to the AP.
uint8_t cur_connection { 0 };

//! Maximum number of simultaneous STA connections to the AP.
uint8_t max_connection { 0 };
};

//! Destroy.
virtual ~IApNetwork() = default;

//! Return various network characteristics.
virtual Info get_info() = 0;
};

} // namespace net
} // namespace ocs
39 changes: 39 additions & 0 deletions components/ocs_net/ista_network.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024, Open Control Systems authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#pragma once

#include <cstdint>
#include <string>

#include "lwip/ip_addr.h"

namespace ocs {
namespace net {

class IStaNetwork {
public:
struct Info {
//! AP SSID to which STA is connected.
std::string ssid;

//! Signal strength of the AP.
int8_t rssi { 0 };

ip_addr_t ip_addr;
};

//! Destroy.
virtual ~IStaNetwork() = default;

//! Return various network characteristics.
virtual Info get_info() = 0;
};

} // namespace net
} // namespace ocs
45 changes: 26 additions & 19 deletions components/ocs_net/sta_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ StaNetwork::~StaNetwork() {
configASSERT(esp_netif_deinit() == ESP_ERR_NOT_SUPPORTED);
}

IStaNetwork::Info StaNetwork::get_info() {
return IStaNetwork::Info {
.ssid = params_.ssid,
.rssi = get_rssi(),
.ip_addr = get_ip_addr_(),
};
}

status::StatusCode StaNetwork::start() {
const auto err = esp_wifi_start();
if (err != ESP_OK) {
Expand Down Expand Up @@ -128,25 +136,6 @@ status::StatusCode StaNetwork::wait(TickType_t wait) {
configASSERT(false);
}

std::optional<ip_addr_t> StaNetwork::get_ip_addr() const {
const EventBits_t bits = xEventGroupGetBits(event_group_.get());
if (!(bits & EVENT_BIT_CONNECTED)) {
return std::nullopt;
}

esp_netif_ip_info_t ip_info;
memset(&ip_info, 0, sizeof(ip_info));

const auto err = esp_netif_get_ip_info(netif_.get(), &ip_info);
if (err != ESP_OK) {
ocs_loge(log_tag, "esp_netif_get_ip_info(): %s", esp_err_to_name(err));
return std::nullopt;
}

const ip_addr_t addr = IPADDR4_INIT(ip_info.ip.addr);
return addr;
}

void StaNetwork::handle_event_(void* event_arg,
esp_event_base_t event_base,
int32_t event_id,
Expand All @@ -165,6 +154,24 @@ void StaNetwork::handle_event_(void* event_arg,
}
}

ip_addr_t StaNetwork::get_ip_addr_() const {
const EventBits_t bits = xEventGroupGetBits(event_group_.get());
if (!(bits & EVENT_BIT_CONNECTED)) {
return IPADDR4_INIT(0);
}

esp_netif_ip_info_t ip_info;
memset(&ip_info, 0, sizeof(ip_info));

const auto err = esp_netif_get_ip_info(netif_.get(), &ip_info);
if (err != ESP_OK) {
ocs_loge(log_tag, "esp_netif_get_ip_info(): %s", esp_err_to_name(err));
return IPADDR4_INIT(0);
}

return IPADDR4_INIT(ip_info.ip.addr);
}

void StaNetwork::handle_wifi_event_(int32_t event_id, void* event_data) {
switch (event_id) {
case WIFI_EVENT_STA_START:
Expand Down
18 changes: 10 additions & 8 deletions components/ocs_net/sta_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@

#include "ocs_core/noncopyable.h"
#include "ocs_core/static_event_group.h"
#include "ocs_net/basic_network.h"
#include "ocs_net/inetwork_handler.h"
#include "ocs_net/ista_network.h"
#include "ocs_net/netif_builder.h"
#include "ocs_status/code.h"

namespace ocs {
namespace net {

//! Handle WiFi STA (station) network operations.
class StaNetwork : public BasicNetwork, public core::NonCopyable<> {
class StaNetwork : public IStaNetwork, public core::NonCopyable<> {
public:
struct Params {
//! Maximum number of attempts to establish a WiFi connection.
Expand All @@ -45,24 +45,26 @@ class StaNetwork : public BasicNetwork, public core::NonCopyable<> {
//! Destroy.
~StaNetwork();

//! Return various network characteristics.
IStaNetwork::Info get_info() override;

//! Start the WiFi connection process.
status::StatusCode start() override;
status::StatusCode start();

//! Stop the WiFi connection process.
status::StatusCode stop() override;
status::StatusCode stop();

//! Wait for the WiFi connection to be established.
status::StatusCode wait(TickType_t wait = portMAX_DELAY) override;

//! Return received IP address.
std::optional<ip_addr_t> get_ip_addr() const override;
status::StatusCode wait(TickType_t wait = portMAX_DELAY);

private:
static void handle_event_(void* event_arg,
esp_event_base_t event_base,
int32_t event_id,
void* event_data);

ip_addr_t get_ip_addr_() const;

void handle_wifi_event_(int32_t event_id, void* event_data);
void handle_wifi_event_sta_start_();
void handle_wifi_event_sta_disconnected_(void* event_data);
Expand Down
4 changes: 3 additions & 1 deletion components/ocs_pipeline/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ idf_component_register(
"httpserver/system_state_handler.cpp"

"jsonfmt/data_pipeline.cpp"
"jsonfmt/network_formatter.cpp"
"jsonfmt/ap_network_formatter.cpp"
"jsonfmt/sta_network_formatter.cpp"
"jsonfmt/system_formatter.cpp"
"jsonfmt/counter_formatter.cpp"
"jsonfmt/telemetry_formatter.cpp"
Expand All @@ -28,6 +29,7 @@ idf_component_register(

"network/mdns_pipeline.cpp"
"network/local_network_pipeline.cpp"
"network/local_network_json_pipeline.cpp"

REQUIRES
"ocs_io"
Expand Down
7 changes: 0 additions & 7 deletions components/ocs_pipeline/httpserver/http_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "ocs_pipeline/httpserver/http_pipeline.h"
#include "ocs_core/log.h"
#include "ocs_pipeline/jsonfmt/network_formatter.h"
#include "ocs_status/code_to_str.h"

namespace ocs {
Expand All @@ -24,7 +23,6 @@ const char* log_tag = "http_pipeline";
HttpPipeline::HttpPipeline(scheduler::ITask& reboot_task,
system::FanoutSuspender& suspender,
net::FanoutNetworkHandler& network_handler,
net::BasicNetwork& network,
net::MdnsProvider& mdns_provider,
fmt::json::IFormatter& telemetry_formatter,
fmt::json::FanoutFormatter& registration_formatter,
Expand Down Expand Up @@ -58,11 +56,6 @@ HttpPipeline::HttpPipeline(scheduler::ITask& reboot_task,
SystemHandler(*http_server_, mdns_provider_, reboot_task));
configASSERT(system_handler_);

network_formatter_.reset(new (std::nothrow) jsonfmt::NetworkFormatter(network));
configASSERT(network_formatter_);

registration_formatter.add(*network_formatter_);

#ifdef CONFIG_FREERTOS_USE_TRACE_FACILITY
system_state_handler_.reset(
new (std::nothrow) SystemStateHandler(*http_server_, mdns_provider_, 1024 * 2));
Expand Down
Loading

0 comments on commit ec38db4

Please sign in to comment.