Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ if(CMAKE_CROSSCOMPILING)
${CMAKE_CURRENT_LIST_DIR}/STM32CubeH7/Middlewares/Third_Party/LwIP/src/core/ipv4/ip4.c
${CMAKE_CURRENT_LIST_DIR}/STM32CubeH7/Middlewares/Third_Party/LwIP/src/core/ipv4/ip4_addr.c
${CMAKE_CURRENT_LIST_DIR}/STM32CubeH7/Middlewares/Third_Party/LwIP/src/core/ipv4/ip4_frag.c
${CMAKE_CURRENT_LIST_DIR}/STM32CubeH7/Middlewares/Third_Party/LwIP/src/apps/sntp/sntp.c
${CMAKE_CURRENT_LIST_DIR}/STM32CubeH7/Middlewares/Third_Party/LwIP/src/netif/ethernet.c
)

Expand Down
1 change: 1 addition & 0 deletions Inc/HALAL/Models/Packets/OrderProtocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Order;

class OrderProtocol {
public:
virtual ~OrderProtocol() = default;
virtual bool send_order(Order& order) = 0;
static vector<OrderProtocol*> sockets;

Expand Down
6 changes: 3 additions & 3 deletions Inc/HALAL/Models/Packets/PacketValue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class PacketValue<Type> : public PacketValue<> {
void set_pointer(void* pointer) { src = (Type*)pointer; }

size_t get_size() override { return sizeof(Type); }
void parse(uint8_t* data) override { *src = *((Type*)data); }
void copy_to(uint8_t* data) override { *((Type*)data) = *src; }
void parse(uint8_t* data) override { memcpy(src, data, get_size()); }
void copy_to(uint8_t* data) override { memcpy(data, src, get_size()); }
};

template <> class PacketValue<double> : public PacketValue<> {
Expand All @@ -49,7 +49,7 @@ template <> class PacketValue<double> : public PacketValue<> {
void set_pointer(void* pointer) { src = (double*)pointer; }

size_t get_size() override { return sizeof(double); }
void parse(uint8_t* data) override { *src = *((double*)data); }
void parse(uint8_t* data) override { memcpy(src, data, get_size()); }
void copy_to(uint8_t* data) override { memcpy(data, src, get_size()); }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
class ServerSocket : public OrderProtocol {
public:
enum ServerState { INACTIVE, LISTENING, ACCEPTED, CLOSING, CLOSED };
static constexpr size_t MAX_TX_QUEUE_DEPTH = 24;
static constexpr size_t MAX_TX_QUEUE_DEPTH = 64;

static unordered_map<uint32_t, ServerSocket*> listening_sockets;
IPV4 local_ip;
Expand Down Expand Up @@ -110,32 +110,7 @@ class ServerSocket : public OrderProtocol {
* message
* @return true if the data was sent successfully, false otherwise
*/
bool send_order(Order& order) override {
if (state != ACCEPTED || client_control_block == nullptr) {
return false;
}
send();
if (tx_packet_buffer.size() >= MAX_TX_QUEUE_DEPTH) {
return false;
}

uint8_t* order_buffer = order.build();
if (order.get_size() > tcp_sndbuf(client_control_block)) {
return false;
}

struct pbuf* packet = pbuf_alloc(PBUF_TRANSPORT, order.get_size(), PBUF_RAM);
if (packet == nullptr) {
return false;
}
if (pbuf_take(packet, order_buffer, order.get_size()) != ERR_OK) {
pbuf_free(packet);
return false;
}
tx_packet_buffer.push(packet);
send();
return true;
}
bool send_order(Order& order) override;

/**
* @brief sends all the binary data saved in the tx_packet_buffer to the
Expand All @@ -160,12 +135,16 @@ class ServerSocket : public OrderProtocol {
* otherwise
*/
bool is_connected();
bool is_listening() const;

private:
struct tcp_pcb* server_control_block = nullptr;
queue<struct pbuf*> tx_packet_buffer;
queue<struct pbuf*> rx_packet_buffer;
struct tcp_pcb* client_control_block;
vector<uint8_t> rx_stream_buffer;
struct tcp_pcb* client_control_block = nullptr;
void clear_packet_queues();
bool try_send_immediately(Order& order);

/**
* @brief process the data received by the client orders. It is meant to be
Expand Down
40 changes: 8 additions & 32 deletions Inc/HALAL/Services/Communication/Ethernet/LWIP/TCP/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

class Socket : public OrderProtocol {
private:
tcp_pcb* connection_control_block;
tcp_pcb* socket_control_block;
tcp_pcb* connection_control_block = nullptr;
tcp_pcb* socket_control_block = nullptr;
queue<struct pbuf*> tx_packet_buffer;
queue<struct pbuf*> rx_packet_buffer;
vector<uint8_t> rx_stream_buffer;
void clear_packet_queues();
void process_data();
bool try_send_immediately(Order& order);
static err_t connect_callback(void* arg, struct tcp_pcb* client_control_block, err_t error);
static err_t receive_callback(
void* arg,
Expand All @@ -39,6 +42,7 @@ class Socket : public OrderProtocol {

public:
enum SocketState { INACTIVE, CONNECTED, CLOSING };
static constexpr size_t MAX_TX_QUEUE_DEPTH = 64;

IPV4 local_ip;
uint32_t local_port;
Expand All @@ -49,6 +53,7 @@ class Socket : public OrderProtocol {

static unordered_map<EthernetNode, Socket*> connecting_sockets;
bool pending_connection_reset = false;
uint16_t connect_poll_ticks = 0;
bool use_keep_alives{true};
struct KeepaliveConfig {
uint32_t inactivity_time_until_keepalive_ms = TCP_INACTIVITY_TIME_UNTIL_KEEPALIVE_MS;
Expand Down Expand Up @@ -95,36 +100,7 @@ class Socket : public OrderProtocol {
* @return true if the data was sent successfully, false otherwise
*/

bool send_order(Order& order) override {
if (state != CONNECTED) {
reconnect();
return false;
}
struct memp* next_memory_pointer_in_packet_buffer_pool =
(*(memp_pools[PBUF_POOL_MEMORY_DESC_POSITION]->tab))->next;
if (next_memory_pointer_in_packet_buffer_pool == nullptr) {
if (socket_control_block->unsent != nullptr) {
tcp_output(socket_control_block);
} else {
memp_free_pool(
memp_pools[PBUF_POOL_MEMORY_DESC_POSITION],
next_memory_pointer_in_packet_buffer_pool
);
}
return false;
}

uint8_t* order_buffer = order.build();
if (order.get_size() > tcp_sndbuf(socket_control_block)) {
return false;
}

struct pbuf* packet = pbuf_alloc(PBUF_TRANSPORT, order.get_size(), PBUF_POOL);
pbuf_take(packet, order_buffer, order.get_size());
tx_packet_buffer.push(packet);
send();
return true;
}
bool send_order(Order& order) override;
void send();
bool is_connected();
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include "HALAL/Models/IPV4/IPV4.hpp"
#include "HALAL/Models/Packets/Order.hpp"

#ifdef HAL_ETH_MODULE_ENABLED

namespace TcpOrderStreamParser {

inline constexpr size_t MAX_RX_STREAM_BUFFER_BYTES = 8192;

inline void process(OrderProtocol* protocol, IPV4& remote_ip, vector<uint8_t>& stream_buffer) {
if (stream_buffer.empty()) {
return;
}
size_t parsed_bytes = 0;

while (stream_buffer.size() - parsed_bytes >= sizeof(uint16_t)) {
uint8_t* packet_ptr = stream_buffer.data() + parsed_bytes;
uint16_t order_id = Packet::get_id(packet_ptr);
auto order_it = Order::orders.find(order_id);
if (order_it == Order::orders.end()) {
parsed_bytes += 1;
continue;
}

const size_t order_size = order_it->second->get_size();
if (order_size < sizeof(uint16_t)) {
parsed_bytes += 1;
continue;
}
if (stream_buffer.size() - parsed_bytes < order_size) {
break;
}

order_it->second->store_ip_order(remote_ip.string_address);
Order::process_data(protocol, packet_ptr);
parsed_bytes += order_size;
}

if (parsed_bytes > 0) {
stream_buffer.erase(stream_buffer.begin(), stream_buffer.begin() + parsed_bytes);
}

if (stream_buffer.size() > MAX_RX_STREAM_BUFFER_BYTES) {
const size_t trim_count = stream_buffer.size() - MAX_RX_STREAM_BUFFER_BYTES;
stream_buffer.erase(stream_buffer.begin(), stream_buffer.begin() + trim_count);
}
}

} // namespace TcpOrderStreamParser

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class DatagramSocket {
public:
struct udp_pcb* udp_control_block;
struct udp_pcb* udp_control_block = nullptr;

IPV4 local_ip;
uint32_t local_port;
Expand Down Expand Up @@ -36,16 +36,17 @@ class DatagramSocket {
if (is_disconnected || udp_control_block == nullptr) {
return false;
}
const size_t packet_size = packet.get_size();
uint8_t* packet_buffer = packet.build();
if (packet_buffer == nullptr || packet.size == 0) {
if (packet_buffer == nullptr || packet_size == 0) {
return false;
}

struct pbuf* tx_buffer = pbuf_alloc(PBUF_TRANSPORT, packet.size, PBUF_RAM);
struct pbuf* tx_buffer = pbuf_alloc(PBUF_TRANSPORT, packet_size, PBUF_RAM);
if (tx_buffer == nullptr) {
return false;
}
if (pbuf_take(tx_buffer, packet_buffer, packet.size) != ERR_OK) {
if (pbuf_take(tx_buffer, packet_buffer, packet_size) != ERR_OK) {
pbuf_free(tx_buffer);
return false;
}
Expand Down
24 changes: 22 additions & 2 deletions Inc/HALAL/Services/Communication/Ethernet/NewEthernet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include "HALAL/Services/Communication/Ethernet/LWIP/Ethernet.hpp"
#include "HALAL/Services/Communication/Ethernet/LWIP/EthernetHelper.hpp"
#include "HALAL/Services/Communication/Ethernet/LWIP/EthernetNode.hpp"
#include "HALAL/Services/Communication/SNTP/SNTP.hpp"
#include "ErrorHandler/ErrorHandler.hpp"
#include "HALAL/Services/InfoWarning/InfoWarning.hpp"
extern "C" {
#include "ethernetif.h"
#include "lwip.h"
Expand Down Expand Up @@ -79,6 +82,7 @@ struct EthernetDomain {
const char* local_ip;
const char* subnet_mask;
const char* gateway;
const char* sntp_server;

size_t phy_reset_id;
};
Expand All @@ -97,9 +101,10 @@ struct EthernetDomain {
const char* local_mac,
const char* local_ip,
const char* subnet_mask = "255.255.0.0",
const char* gateway = "192.168.1.1"
const char* gateway = "192.168.1.1",
const char* sntp_server = SNTP::DEFAULT_SERVER_IP
)
: pins{pins}, e{local_mac, local_ip, subnet_mask, gateway},
: pins{pins}, e{local_mac, local_ip, subnet_mask, gateway, sntp_server},
rmii_gpios{
GPIODomain::GPIO(
pins.MDC,
Expand Down Expand Up @@ -185,6 +190,7 @@ struct EthernetDomain {
.local_ip = this->e.local_ip,
.subnet_mask = this->e.subnet_mask,
.gateway = this->e.gateway,
.sntp_server = this->e.sntp_server,
.phy_reset_id = phy_reset_id,
};

Expand All @@ -199,6 +205,7 @@ struct EthernetDomain {
const char* local_ip;
const char* subnet_mask;
const char* gateway;
const char* sntp_server;

size_t phy_reset_id;
};
Expand All @@ -214,16 +221,22 @@ struct EthernetDomain {
cfgs[0].local_ip = e.local_ip;
cfgs[0].subnet_mask = e.subnet_mask;
cfgs[0].gateway = e.gateway;
cfgs[0].sntp_server = e.sntp_server;
cfgs[0].phy_reset_id = e.phy_reset_id;

return cfgs;
}
// Runtime object
struct Instance {
const char* sntp_server{nullptr};
bool sntp_started{false};

constexpr Instance() {}
void update() {
ethernetif_input(&gnetif);
sys_check_timeouts();
ErrorHandlerModel::ErrorHandlerUpdate();
InfoWarning::InfoWarningUpdate();

if (HAL_GetTick() - EthernetLinkTimer >= 100) {
EthernetLinkTimer = HAL_GetTick();
Expand All @@ -233,6 +246,12 @@ struct EthernetDomain {
netif_set_up(&gnetif);
}
}

if (!sntp_started && sntp_server != nullptr && sntp_server[0] != '\0' &&
netif_is_link_up(&gnetif)) {
SNTP::sntp_update(sntp_server);
sntp_started = true;
}
};
};

Expand Down Expand Up @@ -316,6 +335,7 @@ struct EthernetDomain {
::Ethernet::is_running = true;

instances[0] = Instance{};
instances[0].sntp_server = e.sntp_server;
}
};
};
Expand Down
4 changes: 3 additions & 1 deletion Inc/HALAL/Services/Communication/SNTP/SNTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

#pragma once

#include "sntp.h"
#include "lwip/apps/sntp.h"
#include "HALAL/Models/IPV4/IPV4.hpp"
#include "C++Utilities/CppUtils.hpp"

class SNTP {
public:
static constexpr const char* DEFAULT_SERVER_IP = "192.168.0.9";

static void sntp_update(
uint8_t address_head,
uint8_t address_second,
Expand Down
1 change: 1 addition & 0 deletions Inc/HALAL/Services/InfoWarning/InfoWarning.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class InfoWarning {

public:
static bool warning_triggered;
static bool warning_to_communicate;

/**
* @brief Triggers WarningHandler and format the warning message. The format works
Expand Down
2 changes: 2 additions & 0 deletions Inc/HALAL/Services/Time/RTC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Global_RTC {
public:
static RTCData global_RTC;
static void start_rtc();
static bool ensure_started();
static bool has_valid_time();
static void update_rtc_data();
static RTCData get_rtc_timestamp();
static void set_rtc_data(
Expand Down
4 changes: 4 additions & 0 deletions Inc/ST-LIB.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ template <auto&... devs> struct Board {
HALconfig::system_clock();
HALconfig::peripheral_clock();

#ifdef HAL_RTC_MODULE_ENABLED
(void)Global_RTC::ensure_started();
#endif

MPUDomain::Init<mpuN, cfg.mpu_cfgs>::init();
GPIODomain::Init<gpioN>::init(cfg.gpio_cfgs);
TimerDomain::Init<timN>::init(cfg.tim_cfgs);
Expand Down
Loading