2023-07-06 14:46:33 -03:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "AP_Networking_Config.h"
|
|
|
|
|
|
|
|
#if AP_NETWORKING_ENABLED
|
|
|
|
#include <AP_Param/AP_Param.h>
|
|
|
|
|
2023-08-05 00:51:54 -03:00
|
|
|
#include "AP_Networking_address.h"
|
2023-11-12 16:38:26 -04:00
|
|
|
#include "AP_Networking_Backend.h"
|
2023-11-15 23:46:08 -04:00
|
|
|
#include <AP_SerialManager/AP_SerialManager.h>
|
|
|
|
#include <AP_HAL/utility/RingBuffer.h>
|
2023-08-05 00:51:54 -03:00
|
|
|
|
|
|
|
/*
|
2023-08-05 04:08:52 -03:00
|
|
|
Note! all uint32_t IPv4 addresses are in host byte order
|
2023-08-05 00:51:54 -03:00
|
|
|
*/
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2023-11-12 18:09:58 -04:00
|
|
|
// declare backend classes
|
2023-11-12 16:38:26 -04:00
|
|
|
class AP_Networking_Backend;
|
2023-11-12 18:09:58 -04:00
|
|
|
class AP_Networking_ChibiOS;
|
|
|
|
|
2023-11-15 23:46:08 -04:00
|
|
|
class SocketAPM;
|
|
|
|
|
2023-07-06 14:46:33 -03:00
|
|
|
class AP_Networking
|
|
|
|
{
|
|
|
|
public:
|
2023-11-12 16:38:26 -04:00
|
|
|
friend class AP_Networking_Backend;
|
2023-08-05 03:46:13 -03:00
|
|
|
friend class AP_Networking_ChibiOS;
|
2024-01-08 16:27:31 -04:00
|
|
|
friend class AP_Networking_PPP;
|
2023-11-15 23:46:08 -04:00
|
|
|
friend class AP_Vehicle;
|
2023-11-16 14:56:02 -04:00
|
|
|
friend class Networking_Periph;
|
2023-08-05 03:46:13 -03:00
|
|
|
|
2023-07-06 14:46:33 -03:00
|
|
|
AP_Networking();
|
|
|
|
|
|
|
|
/* Do not allow copies */
|
|
|
|
CLASS_NO_COPY(AP_Networking);
|
|
|
|
|
2023-08-02 21:53:32 -03:00
|
|
|
// initialize the library. This should only be run once
|
2023-07-06 14:46:33 -03:00
|
|
|
void init();
|
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
// update task, called at 10Hz
|
2023-07-06 14:46:33 -03:00
|
|
|
void update();
|
|
|
|
|
2023-07-07 15:15:26 -03:00
|
|
|
static AP_Networking *get_singleton(void)
|
|
|
|
{
|
2023-08-05 03:46:13 -03:00
|
|
|
return singleton;
|
2023-07-07 15:15:26 -03:00
|
|
|
}
|
|
|
|
|
2023-12-25 15:26:47 -04:00
|
|
|
HAL_Semaphore &get_semaphore(void)
|
|
|
|
{
|
|
|
|
return sem;
|
|
|
|
}
|
|
|
|
|
2023-08-02 21:53:32 -03:00
|
|
|
// Networking interface is enabled and initialized
|
2023-07-07 15:15:26 -03:00
|
|
|
bool is_healthy() const
|
|
|
|
{
|
2023-08-05 03:46:13 -03:00
|
|
|
return param.enabled && backend != nullptr;
|
2023-07-07 15:15:26 -03:00
|
|
|
}
|
2023-08-02 21:53:32 -03:00
|
|
|
|
|
|
|
// returns true if DHCP is enabled
|
2023-07-07 15:15:26 -03:00
|
|
|
bool get_dhcp_enabled() const
|
|
|
|
{
|
2023-11-29 18:16:31 -04:00
|
|
|
#if AP_NETWORKING_DHCP_AVAILABLE
|
2023-08-05 03:46:13 -03:00
|
|
|
return param.dhcp;
|
2023-11-29 18:16:31 -04:00
|
|
|
#else
|
|
|
|
// DHCP is not available from our scope but could be enabled/controlled
|
|
|
|
// by the OS which is the case on Linux builds, including SITL
|
|
|
|
// TODO: ask the OS if DHCP is enabled
|
|
|
|
return false;
|
|
|
|
#endif
|
2023-07-07 15:15:26 -03:00
|
|
|
}
|
2023-08-02 21:53:32 -03:00
|
|
|
|
|
|
|
// Sets DHCP to be enabled or disabled
|
2023-07-07 15:15:26 -03:00
|
|
|
void set_dhcp_enable(const bool enable)
|
|
|
|
{
|
2023-11-29 18:16:31 -04:00
|
|
|
#if AP_NETWORKING_DHCP_AVAILABLE
|
2023-08-05 03:46:13 -03:00
|
|
|
param.dhcp.set(enable);
|
2023-11-29 18:16:31 -04:00
|
|
|
#endif
|
2023-07-07 15:15:26 -03:00
|
|
|
}
|
|
|
|
|
2023-08-02 21:53:32 -03:00
|
|
|
// returns the 32bit value of the active IP address that is currently in use
|
2023-11-12 18:09:58 -04:00
|
|
|
uint32_t get_ip_active() const;
|
2023-08-02 21:53:32 -03:00
|
|
|
|
|
|
|
// returns the 32bit value of the user-parameter static IP address
|
2023-07-07 15:15:26 -03:00
|
|
|
uint32_t get_ip_param() const
|
|
|
|
{
|
2023-11-29 18:16:31 -04:00
|
|
|
#if AP_NETWORKING_CONTROLS_HOST_IP_SETTINGS_ENABLED
|
2023-08-05 03:46:13 -03:00
|
|
|
return param.ipaddr.get_uint32();
|
2023-11-29 18:16:31 -04:00
|
|
|
#else
|
|
|
|
// TODO: ask the OS for the IP address
|
|
|
|
return 0;
|
|
|
|
#endif
|
2023-07-07 15:15:26 -03:00
|
|
|
}
|
2023-08-02 21:53:32 -03:00
|
|
|
|
|
|
|
// sets the user-parameter static IP address from a 32bit value
|
2023-08-05 00:51:54 -03:00
|
|
|
void set_ip_param(const uint32_t ip)
|
2023-07-07 15:15:26 -03:00
|
|
|
{
|
2023-11-29 18:16:31 -04:00
|
|
|
#if AP_NETWORKING_CONTROLS_HOST_IP_SETTINGS_ENABLED
|
2023-08-05 03:46:13 -03:00
|
|
|
param.ipaddr.set_uint32(ip);
|
2023-11-29 18:16:31 -04:00
|
|
|
#endif
|
2023-07-07 15:15:26 -03:00
|
|
|
}
|
|
|
|
|
2023-08-02 21:53:32 -03:00
|
|
|
// returns the 32bit value of the active Netmask that is currently in use
|
2023-11-12 18:09:58 -04:00
|
|
|
uint32_t get_netmask_active() const;
|
2023-08-02 21:53:32 -03:00
|
|
|
|
|
|
|
// returns the 32bit value of the of the user-parameter static Netmask
|
2023-07-07 15:15:26 -03:00
|
|
|
uint32_t get_netmask_param() const
|
|
|
|
{
|
2023-11-29 18:16:31 -04:00
|
|
|
#if AP_NETWORKING_CONTROLS_HOST_IP_SETTINGS_ENABLED
|
2023-08-05 03:46:13 -03:00
|
|
|
return convert_netmask_bitcount_to_ip(param.netmask.get());
|
2023-11-29 18:16:31 -04:00
|
|
|
#else
|
|
|
|
// TODO: ask the OS for the Netmask
|
|
|
|
return 0;
|
|
|
|
#endif
|
2023-07-07 15:15:26 -03:00
|
|
|
}
|
2023-08-02 21:53:32 -03:00
|
|
|
|
2023-11-12 18:09:58 -04:00
|
|
|
uint32_t get_gateway_active() const;
|
2023-08-05 00:51:54 -03:00
|
|
|
|
2023-07-07 15:15:26 -03:00
|
|
|
uint32_t get_gateway_param() const
|
|
|
|
{
|
2023-11-29 18:16:31 -04:00
|
|
|
#if AP_NETWORKING_CONTROLS_HOST_IP_SETTINGS_ENABLED
|
2023-08-05 03:46:13 -03:00
|
|
|
return param.gwaddr.get_uint32();
|
2023-11-29 18:16:31 -04:00
|
|
|
#else
|
|
|
|
// TODO: ask the OS for the Gateway
|
|
|
|
return 0;
|
|
|
|
#endif
|
2023-07-07 15:15:26 -03:00
|
|
|
}
|
2023-08-05 00:51:54 -03:00
|
|
|
|
|
|
|
void set_gateway_param(const uint32_t gw)
|
2023-07-07 15:15:26 -03:00
|
|
|
{
|
2023-11-29 18:16:31 -04:00
|
|
|
#if AP_NETWORKING_CONTROLS_HOST_IP_SETTINGS_ENABLED
|
2023-08-05 03:46:13 -03:00
|
|
|
param.gwaddr.set_uint32(gw);
|
2023-11-29 18:16:31 -04:00
|
|
|
#endif
|
2023-07-07 15:15:26 -03:00
|
|
|
}
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2023-11-29 18:23:39 -04:00
|
|
|
// wait in a thread for network startup
|
|
|
|
void startup_wait(void) const;
|
|
|
|
|
2023-08-05 00:51:54 -03:00
|
|
|
// convert string to ethernet mac address
|
|
|
|
static bool convert_str_to_macaddr(const char *mac_str, uint8_t addr[6]);
|
|
|
|
|
2024-01-12 21:20:51 -04:00
|
|
|
// address to string using a static return buffer for scripting
|
|
|
|
static const char *address_to_str(uint32_t addr);
|
|
|
|
|
2023-08-02 21:53:32 -03:00
|
|
|
// helper functions to convert between 32bit Netmask and counting consecutive bits and back
|
2023-07-06 14:46:33 -03:00
|
|
|
static uint32_t convert_netmask_bitcount_to_ip(const uint32_t netmask_bitcount);
|
|
|
|
static uint8_t convert_netmask_ip_to_bitcount(const uint32_t netmask_ip);
|
|
|
|
|
2023-12-07 14:32:32 -04:00
|
|
|
/*
|
|
|
|
send contents of a file to a socket then close both socket and file
|
|
|
|
*/
|
|
|
|
bool sendfile(SocketAPM *sock, int fd);
|
|
|
|
|
2023-07-06 14:46:33 -03:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
2024-01-08 16:27:31 -04:00
|
|
|
enum class OPTION {
|
|
|
|
PPP_ETHERNET_GATEWAY=(1U<<0),
|
|
|
|
};
|
|
|
|
bool option_is_set(OPTION option) const {
|
|
|
|
return (param.options.get() & int32_t(option)) != 0;
|
|
|
|
}
|
|
|
|
|
2023-07-06 14:46:33 -03:00
|
|
|
private:
|
2023-08-05 03:46:13 -03:00
|
|
|
static AP_Networking *singleton;
|
2023-07-06 14:46:33 -03:00
|
|
|
|
|
|
|
void announce_address_changes();
|
|
|
|
|
|
|
|
struct {
|
2023-11-29 18:16:31 -04:00
|
|
|
#if AP_NETWORKING_CONTROLS_HOST_IP_SETTINGS_ENABLED
|
2023-08-05 00:51:54 -03:00
|
|
|
AP_Networking_IPV4 ipaddr{AP_NETWORKING_DEFAULT_STATIC_IP_ADDR};
|
2023-07-06 14:46:33 -03:00
|
|
|
AP_Int8 netmask; // bits to mask. example: (16 == 255.255.0.0) and (24 == 255.255.255.0)
|
2023-08-05 00:51:54 -03:00
|
|
|
AP_Networking_IPV4 gwaddr{AP_NETWORKING_DEFAULT_STATIC_GW_ADDR};
|
|
|
|
AP_Networking_MAC macaddr{AP_NETWORKING_DEFAULT_MAC_ADDR};
|
2023-11-29 18:16:31 -04:00
|
|
|
#if AP_NETWORKING_DHCP_AVAILABLE
|
|
|
|
AP_Int8 dhcp;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2023-07-06 14:46:33 -03:00
|
|
|
AP_Int8 enabled;
|
2023-07-07 15:15:26 -03:00
|
|
|
AP_Int32 options;
|
2023-11-29 18:16:31 -04:00
|
|
|
|
2023-11-12 21:37:10 -04:00
|
|
|
#if AP_NETWORKING_TESTS_ENABLED
|
|
|
|
AP_Int32 tests;
|
2023-11-13 18:07:25 -04:00
|
|
|
AP_Networking_IPV4 test_ipaddr{AP_NETWORKING_TEST_IP};
|
2023-11-12 21:37:10 -04:00
|
|
|
#endif
|
2024-01-08 16:27:31 -04:00
|
|
|
|
|
|
|
#if AP_NETWORKING_PPP_GATEWAY_ENABLED
|
|
|
|
AP_Networking_IPV4 remote_ppp_ip{AP_NETWORKING_REMOTE_PPP_IP};
|
|
|
|
#endif
|
2023-08-05 03:46:13 -03:00
|
|
|
} param;
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2023-11-12 16:38:26 -04:00
|
|
|
AP_Networking_Backend *backend;
|
2023-08-05 03:46:13 -03:00
|
|
|
|
2024-01-08 16:27:31 -04:00
|
|
|
#if AP_NETWORKING_PPP_GATEWAY_ENABLED
|
|
|
|
AP_Networking_Backend *backend_PPP;
|
|
|
|
#endif
|
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
HAL_Semaphore sem;
|
|
|
|
|
2023-11-15 23:46:08 -04:00
|
|
|
enum class NetworkPortType {
|
|
|
|
NONE = 0,
|
|
|
|
UDP_CLIENT = 1,
|
2023-11-25 20:46:07 -04:00
|
|
|
UDP_SERVER = 2,
|
|
|
|
TCP_CLIENT = 3,
|
|
|
|
TCP_SERVER = 4,
|
2023-11-15 23:46:08 -04:00
|
|
|
};
|
|
|
|
|
2024-01-24 00:43:53 -04:00
|
|
|
#if AP_NETWORKING_REGISTER_PORT_ENABLED
|
2023-11-15 23:46:08 -04:00
|
|
|
// class for NET_Pn_* parameters
|
|
|
|
class Port : public AP_SerialManager::RegisteredPort {
|
|
|
|
public:
|
|
|
|
/* Do not allow copies */
|
|
|
|
CLASS_NO_COPY(Port);
|
|
|
|
|
|
|
|
Port() {}
|
|
|
|
|
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
AP_Enum<NetworkPortType> type;
|
|
|
|
AP_Networking_IPV4 ip {"0.0.0.0"};
|
|
|
|
AP_Int32 port;
|
|
|
|
SocketAPM *sock;
|
2023-11-25 20:46:07 -04:00
|
|
|
SocketAPM *listen_sock;
|
2023-11-15 23:46:08 -04:00
|
|
|
|
|
|
|
bool is_initialized() override {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool tx_pending() override {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-25 20:46:07 -04:00
|
|
|
void udp_client_init(void);
|
|
|
|
void udp_server_init(void);
|
|
|
|
void tcp_server_init(void);
|
|
|
|
void tcp_client_init(void);
|
|
|
|
|
2023-11-15 23:46:08 -04:00
|
|
|
void udp_client_loop(void);
|
2023-11-25 20:46:07 -04:00
|
|
|
void udp_server_loop(void);
|
|
|
|
void tcp_client_loop(void);
|
|
|
|
void tcp_server_loop(void);
|
|
|
|
|
|
|
|
bool send_receive(void);
|
2023-11-15 23:46:08 -04:00
|
|
|
|
|
|
|
private:
|
2023-11-17 15:20:39 -04:00
|
|
|
bool init_buffers(const uint32_t size_rx, const uint32_t size_tx);
|
2023-11-25 20:46:07 -04:00
|
|
|
void thread_create(AP_HAL::MemberProc);
|
2023-11-15 23:46:08 -04:00
|
|
|
|
|
|
|
uint32_t txspace() override;
|
|
|
|
void _begin(uint32_t b, uint16_t rxS, uint16_t txS) override;
|
|
|
|
size_t _write(const uint8_t *buffer, size_t size) override;
|
|
|
|
ssize_t _read(uint8_t *buffer, uint16_t count) override;
|
|
|
|
uint32_t _available() override;
|
|
|
|
void _end() override {}
|
|
|
|
void _flush() override {}
|
|
|
|
bool _discard_input() override;
|
|
|
|
|
2023-11-25 20:46:07 -04:00
|
|
|
enum flow_control get_flow_control(void) override;
|
|
|
|
|
|
|
|
uint32_t bw_in_bytes_per_second() const override {
|
|
|
|
return 1000000UL;
|
|
|
|
}
|
|
|
|
|
2023-11-15 23:46:08 -04:00
|
|
|
ByteBuffer *readbuffer;
|
|
|
|
ByteBuffer *writebuffer;
|
2023-11-25 20:46:07 -04:00
|
|
|
char thread_name[10];
|
2023-11-18 17:46:46 -04:00
|
|
|
uint32_t last_size_tx;
|
|
|
|
uint32_t last_size_rx;
|
2023-11-25 20:46:07 -04:00
|
|
|
bool packetise;
|
|
|
|
bool connected;
|
2024-02-07 01:27:35 -04:00
|
|
|
uint32_t last_udp_connect_address;
|
|
|
|
uint16_t last_udp_connect_port;
|
2023-11-25 20:46:07 -04:00
|
|
|
bool have_received;
|
|
|
|
bool close_on_recv_error;
|
2024-06-19 23:30:42 -03:00
|
|
|
uint32_t last_udp_srv_recv_time_ms;
|
2023-11-18 17:46:46 -04:00
|
|
|
HAL_Semaphore sem;
|
2023-11-15 23:46:08 -04:00
|
|
|
};
|
2024-01-24 00:43:53 -04:00
|
|
|
#endif // AP_NETWORKING_REGISTER_PORT_ENABLED
|
2023-11-15 23:46:08 -04:00
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
private:
|
|
|
|
uint32_t announce_ms;
|
2023-11-12 21:37:10 -04:00
|
|
|
|
|
|
|
#if AP_NETWORKING_TESTS_ENABLED
|
|
|
|
enum {
|
|
|
|
TEST_UDP_CLIENT = (1U<<0),
|
|
|
|
TEST_TCP_CLIENT = (1U<<1),
|
2023-11-25 20:46:07 -04:00
|
|
|
TEST_TCP_DISCARD = (1U<<2),
|
2023-11-12 21:37:10 -04:00
|
|
|
};
|
|
|
|
void start_tests(void);
|
|
|
|
void test_UDP_client(void);
|
|
|
|
void test_TCP_client(void);
|
2023-11-25 20:46:07 -04:00
|
|
|
void test_TCP_discard(void);
|
2023-11-12 21:37:10 -04:00
|
|
|
#endif // AP_NETWORKING_TESTS_ENABLED
|
2023-11-15 23:46:08 -04:00
|
|
|
|
2024-01-24 00:43:53 -04:00
|
|
|
#if AP_NETWORKING_REGISTER_PORT_ENABLED
|
2023-11-15 23:46:08 -04:00
|
|
|
// ports for registration with serial manager
|
|
|
|
Port ports[AP_NETWORKING_NUM_PORTS];
|
2024-01-24 00:43:53 -04:00
|
|
|
#endif
|
2023-11-15 23:46:08 -04:00
|
|
|
|
2023-12-07 14:32:32 -04:00
|
|
|
// support for sendfile()
|
|
|
|
struct SendFile {
|
|
|
|
SocketAPM *sock;
|
|
|
|
int fd;
|
|
|
|
void close(void);
|
|
|
|
} sendfiles[AP_NETWORKING_NUM_SENDFILES];
|
|
|
|
|
|
|
|
uint8_t *sendfile_buf;
|
2024-01-04 22:37:02 -04:00
|
|
|
uint32_t sendfile_bufsize;
|
2023-12-07 14:32:32 -04:00
|
|
|
void sendfile_check(void);
|
2023-12-25 15:26:47 -04:00
|
|
|
bool sendfile_thread_started;
|
2023-12-07 14:32:32 -04:00
|
|
|
|
2023-11-15 23:46:08 -04:00
|
|
|
void ports_init(void);
|
2023-07-06 14:46:33 -03:00
|
|
|
};
|
|
|
|
|
2023-07-07 15:15:26 -03:00
|
|
|
namespace AP
|
|
|
|
{
|
2023-08-05 03:46:13 -03:00
|
|
|
AP_Networking &network();
|
2023-07-06 14:46:33 -03:00
|
|
|
};
|
|
|
|
|
2023-12-07 01:37:56 -04:00
|
|
|
extern "C" {
|
|
|
|
int ap_networking_printf(const char *fmt, ...);
|
|
|
|
}
|
|
|
|
|
2023-07-06 14:46:33 -03:00
|
|
|
#endif // AP_NETWORKING_ENABLED
|