2023-07-06 14:46:33 -03:00
|
|
|
|
2023-08-02 21:53:32 -03:00
|
|
|
#include "AP_Networking_Config.h"
|
2023-07-06 14:46:33 -03:00
|
|
|
|
|
|
|
#if AP_NETWORKING_ENABLED
|
|
|
|
|
2023-08-02 21:53:32 -03:00
|
|
|
#include "AP_Networking.h"
|
2023-11-12 16:38:26 -04:00
|
|
|
#include "AP_Networking_Backend.h"
|
2023-07-06 14:46:33 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2023-08-15 01:50:49 -03:00
|
|
|
#include <AP_Math/crc.h>
|
2023-11-29 18:23:39 -04:00
|
|
|
#include <AP_InternalError/AP_InternalError.h>
|
2023-12-07 14:32:32 -04:00
|
|
|
#include <AP_Filesystem/AP_Filesystem.h>
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2023-11-12 18:09:58 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS
|
|
|
|
#include "AP_Networking_ChibiOS.h"
|
2023-07-07 15:15:26 -03:00
|
|
|
#include <hal_mii.h>
|
2023-07-06 14:46:33 -03:00
|
|
|
#endif
|
|
|
|
|
2023-12-25 15:26:47 -04:00
|
|
|
#include <lwipopts.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
2023-12-07 14:32:32 -04:00
|
|
|
#include <AP_HAL/utility/Socket.h>
|
|
|
|
|
2023-12-09 16:10:16 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_PPP
|
|
|
|
#include "AP_Networking_PPP.h"
|
|
|
|
#endif
|
|
|
|
|
2023-11-13 18:08:36 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_SITL
|
|
|
|
#include "AP_Networking_SITL.h"
|
|
|
|
#endif
|
2023-08-05 00:51:54 -03:00
|
|
|
|
2023-07-06 14:46:33 -03:00
|
|
|
const AP_Param::GroupInfo AP_Networking::var_info[] = {
|
2024-03-19 14:57:40 -03:00
|
|
|
// @Param: ENABLE
|
2023-08-02 21:53:32 -03:00
|
|
|
// @DisplayName: Networking Enable
|
|
|
|
// @Description: Networking Enable
|
2023-07-06 14:46:33 -03:00
|
|
|
// @Values: 0:Disable,1:Enable
|
|
|
|
// @RebootRequired: True
|
|
|
|
// @User: Advanced
|
2024-03-19 14:44:35 -03:00
|
|
|
AP_GROUPINFO_FLAGS("ENABLE", 1, AP_Networking, param.enabled, 0, AP_PARAM_FLAG_ENABLE),
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2023-11-29 18:16:31 -04:00
|
|
|
#if AP_NETWORKING_CONTROLS_HOST_IP_SETTINGS_ENABLED
|
2023-08-05 00:51:54 -03:00
|
|
|
// @Group: IPADDR
|
|
|
|
// @Path: AP_Networking_address.cpp
|
2023-08-05 03:46:13 -03:00
|
|
|
AP_SUBGROUPINFO(param.ipaddr, "IPADDR", 2, AP_Networking, AP_Networking_IPV4),
|
2023-07-06 14:46:33 -03:00
|
|
|
|
|
|
|
// @Param: NETMASK
|
|
|
|
// @DisplayName: IP Subnet mask
|
|
|
|
// @Description: Allows setting static subnet mask. The value is a count of consecutive bits. Examples: 24 = 255.255.255.0, 16 = 255.255.0.0
|
|
|
|
// @Range: 0 32
|
|
|
|
// @RebootRequired: True
|
|
|
|
// @User: Advanced
|
2023-08-05 03:46:13 -03:00
|
|
|
AP_GROUPINFO("NETMASK", 3, AP_Networking, param.netmask, AP_NETWORKING_DEFAULT_NETMASK),
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2023-11-29 18:16:31 -04:00
|
|
|
#if AP_NETWORKING_DHCP_AVAILABLE
|
2023-07-06 14:46:33 -03:00
|
|
|
// @Param: DHCP
|
|
|
|
// @DisplayName: DHCP client
|
|
|
|
// @Description: Enable/Disable DHCP client
|
|
|
|
// @Values: 0:Disable, 1:Enable
|
|
|
|
// @RebootRequired: True
|
|
|
|
// @User: Advanced
|
2023-08-05 03:46:13 -03:00
|
|
|
AP_GROUPINFO("DHCP", 4, AP_Networking, param.dhcp, AP_NETWORKING_DEFAULT_DHCP_ENABLE),
|
2023-11-29 18:16:31 -04:00
|
|
|
#endif
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2023-08-05 00:51:54 -03:00
|
|
|
// @Group: GWADDR
|
|
|
|
// @Path: AP_Networking_address.cpp
|
2023-08-05 03:46:13 -03:00
|
|
|
AP_SUBGROUPINFO(param.gwaddr, "GWADDR", 5, AP_Networking, AP_Networking_IPV4),
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2023-08-05 00:51:54 -03:00
|
|
|
// @Group: MACADDR
|
2023-08-09 23:09:00 -03:00
|
|
|
// @Path: AP_Networking_macaddr.cpp
|
2023-08-05 03:46:13 -03:00
|
|
|
AP_SUBGROUPINFO(param.macaddr, "MACADDR", 6, AP_Networking, AP_Networking_MAC),
|
2023-11-29 18:16:31 -04:00
|
|
|
#endif // AP_NETWORKING_CONTROLS_HOST_IP_SETTINGS_ENABLED
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2023-11-12 21:37:10 -04:00
|
|
|
#if AP_NETWORKING_TESTS_ENABLED
|
|
|
|
// @Param: TESTS
|
|
|
|
// @DisplayName: Test enable flags
|
|
|
|
// @Description: Enable/Disable networking tests
|
2024-03-17 02:42:42 -03:00
|
|
|
// @Bitmask: 0:UDP echo test,1:TCP echo test, 2:TCP discard test, 3:TCP reflect test
|
2023-11-12 21:37:10 -04:00
|
|
|
// @RebootRequired: True
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO("TESTS", 7, AP_Networking, param.tests, 0),
|
2023-11-13 18:07:25 -04:00
|
|
|
|
|
|
|
// @Group: TEST_IP
|
|
|
|
// @Path: AP_Networking_address.cpp
|
|
|
|
AP_SUBGROUPINFO(param.test_ipaddr, "TEST_IP", 8, AP_Networking, AP_Networking_IPV4),
|
2023-11-12 21:37:10 -04:00
|
|
|
#endif
|
|
|
|
|
2024-01-08 16:27:31 -04:00
|
|
|
// @Param: OPTIONS
|
|
|
|
// @DisplayName: Networking options
|
|
|
|
// @Description: Networking options
|
|
|
|
// @Bitmask: 0:EnablePPP Ethernet gateway
|
|
|
|
// @RebootRequired: True
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO("OPTIONS", 9, AP_Networking, param.options, 0),
|
|
|
|
|
|
|
|
#if AP_NETWORKING_PPP_GATEWAY_ENABLED
|
|
|
|
// @Group: REMPPP_IP
|
|
|
|
// @Path: AP_Networking_address.cpp
|
|
|
|
AP_SUBGROUPINFO(param.remote_ppp_ip, "REMPPP_IP", 10, AP_Networking, AP_Networking_IPV4),
|
|
|
|
#endif
|
|
|
|
|
2023-07-06 14:46:33 -03:00
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
/*
|
|
|
|
constructor
|
|
|
|
*/
|
2023-07-06 14:46:33 -03:00
|
|
|
AP_Networking::AP_Networking(void)
|
|
|
|
{
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2023-08-05 03:46:13 -03:00
|
|
|
if (singleton != nullptr) {
|
2023-07-06 14:46:33 -03:00
|
|
|
AP_HAL::panic("AP_Networking must be singleton");
|
|
|
|
}
|
|
|
|
#endif
|
2023-08-05 03:46:13 -03:00
|
|
|
singleton = this;
|
2023-07-06 14:46:33 -03:00
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
}
|
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
/*
|
|
|
|
initialise networking subsystem
|
|
|
|
*/
|
2023-07-06 14:46:33 -03:00
|
|
|
void AP_Networking::init()
|
|
|
|
{
|
2023-08-05 03:46:13 -03:00
|
|
|
if (!param.enabled || backend != nullptr) {
|
2023-07-06 14:46:33 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-29 18:16:31 -04:00
|
|
|
#if AP_NETWORKING_CONTROLS_HOST_IP_SETTINGS_ENABLED
|
2023-08-15 01:50:49 -03:00
|
|
|
// set default MAC Address as lower 3 bytes of the CRC of the UID
|
|
|
|
uint8_t uid[50];
|
|
|
|
uint8_t uid_len = sizeof(uid);
|
|
|
|
if (hal.util->get_system_id_unformatted(uid, uid_len)) {
|
|
|
|
union {
|
|
|
|
uint8_t bytes[4];
|
|
|
|
uint32_t value32;
|
|
|
|
} crc;
|
|
|
|
crc.value32 = crc_crc32(0, uid, uid_len);
|
|
|
|
|
|
|
|
param.macaddr.set_default_address_byte(3, crc.bytes[0]);
|
|
|
|
param.macaddr.set_default_address_byte(4, crc.bytes[1]);
|
|
|
|
param.macaddr.set_default_address_byte(5, crc.bytes[2]);
|
2023-07-06 14:46:33 -03:00
|
|
|
}
|
2023-11-29 18:16:31 -04:00
|
|
|
#endif
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2024-01-08 16:27:31 -04:00
|
|
|
#if AP_NETWORKING_PPP_GATEWAY_ENABLED
|
|
|
|
if (option_is_set(OPTION::PPP_ETHERNET_GATEWAY)) {
|
|
|
|
/*
|
|
|
|
when we are a PPP/Ethernet gateway we bring up the ethernet first
|
|
|
|
*/
|
2024-05-26 22:24:13 -03:00
|
|
|
backend = NEW_NOTHROW AP_Networking_ChibiOS(*this);
|
|
|
|
backend_PPP = NEW_NOTHROW AP_Networking_PPP(*this);
|
2024-01-08 16:27:31 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2023-12-09 16:10:16 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_PPP
|
2024-01-08 16:27:31 -04:00
|
|
|
if (backend == nullptr && AP::serialmanager().have_serial(AP_SerialManager::SerialProtocol_PPP, 0)) {
|
2024-05-26 22:24:13 -03:00
|
|
|
backend = NEW_NOTHROW AP_Networking_PPP(*this);
|
2023-12-09 16:10:16 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-11-12 18:09:58 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS
|
2023-12-09 05:23:15 -04:00
|
|
|
if (backend == nullptr) {
|
2024-05-26 22:24:13 -03:00
|
|
|
backend = NEW_NOTHROW AP_Networking_ChibiOS(*this);
|
2023-12-09 05:23:15 -04:00
|
|
|
}
|
2023-07-28 17:29:33 -03:00
|
|
|
#endif
|
2023-11-13 18:08:36 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_SITL
|
2023-12-09 05:23:15 -04:00
|
|
|
if (backend == nullptr) {
|
2024-05-26 22:24:13 -03:00
|
|
|
backend = NEW_NOTHROW AP_Networking_SITL(*this);
|
2023-12-09 05:23:15 -04:00
|
|
|
}
|
2023-11-13 18:08:36 -04:00
|
|
|
#endif
|
2023-08-05 03:46:13 -03:00
|
|
|
|
|
|
|
if (backend == nullptr) {
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "NET: backend failed");
|
2023-07-06 14:46:33 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
if (!backend->init()) {
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "NET: backend init failed");
|
2023-08-22 13:24:21 -03:00
|
|
|
// the backend init function creates a thread which references the backend pointer; that thread may be running so don't remove the backend allocation.
|
2023-08-05 03:46:13 -03:00
|
|
|
backend = nullptr;
|
2023-08-17 20:40:49 -03:00
|
|
|
return;
|
2023-07-06 14:46:33 -03:00
|
|
|
}
|
|
|
|
|
2024-01-08 16:27:31 -04:00
|
|
|
#if AP_NETWORKING_PPP_GATEWAY_ENABLED
|
|
|
|
if (backend_PPP != nullptr && !backend_PPP->init()) {
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "NET: backend_PPP init failed");
|
|
|
|
backend_PPP = nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
announce_address_changes();
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2023-08-06 13:23:30 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO,"NET: Initialized");
|
2023-11-12 21:37:10 -04:00
|
|
|
|
|
|
|
#if AP_NETWORKING_TESTS_ENABLED
|
|
|
|
start_tests();
|
|
|
|
#endif
|
2023-11-15 23:46:08 -04:00
|
|
|
|
2024-08-12 06:09:26 -03:00
|
|
|
#if AP_NETWORKING_REGISTER_PORT_ENABLED
|
2023-11-15 23:46:08 -04:00
|
|
|
// init network mapped serialmanager ports
|
|
|
|
ports_init();
|
2024-08-12 06:09:26 -03:00
|
|
|
#endif
|
2023-07-06 14:46:33 -03:00
|
|
|
}
|
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
/*
|
|
|
|
check if we should announce changes to IP addresses
|
|
|
|
*/
|
2023-07-06 14:46:33 -03:00
|
|
|
void AP_Networking::announce_address_changes()
|
|
|
|
{
|
2023-11-29 18:16:31 -04:00
|
|
|
const auto &as = backend->activeSettings;
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
if (as.last_change_ms == 0 || as.last_change_ms == announce_ms) {
|
2023-07-06 14:46:33 -03:00
|
|
|
// nothing changed and we've already printed it at least once. Nothing to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-13 01:29:09 -04:00
|
|
|
#if AP_HAVE_GCS_SEND_TEXT
|
2023-12-25 17:19:15 -04:00
|
|
|
char ipstr[16];
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "NET: IP %s", SocketAPM::inet_addr_to_str(get_ip_active(), ipstr, sizeof(ipstr)));
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "NET: Mask %s", SocketAPM::inet_addr_to_str(get_netmask_active(), ipstr, sizeof(ipstr)));
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "NET: Gateway %s", SocketAPM::inet_addr_to_str(get_gateway_active(), ipstr, sizeof(ipstr)));
|
2024-01-13 01:29:09 -04:00
|
|
|
#endif
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
announce_ms = as.last_change_ms;
|
2023-07-06 14:46:33 -03:00
|
|
|
}
|
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
/*
|
|
|
|
update called at 10Hz
|
|
|
|
*/
|
2023-07-06 14:46:33 -03:00
|
|
|
void AP_Networking::update()
|
|
|
|
{
|
|
|
|
if (!is_healthy()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-05 03:46:13 -03:00
|
|
|
backend->update();
|
2023-07-06 14:46:33 -03:00
|
|
|
announce_address_changes();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AP_Networking::convert_netmask_bitcount_to_ip(const uint32_t netmask_bitcount)
|
|
|
|
{
|
2023-08-05 04:08:52 -03:00
|
|
|
if (netmask_bitcount >= 32) {
|
|
|
|
return 0xFFFFFFFFU;
|
2023-07-06 14:46:33 -03:00
|
|
|
}
|
2023-08-05 04:08:52 -03:00
|
|
|
return ~((1U<<(32U-netmask_bitcount))-1U);
|
2023-07-06 14:46:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t AP_Networking::convert_netmask_ip_to_bitcount(const uint32_t netmask_ip)
|
|
|
|
{
|
|
|
|
uint32_t netmask_bitcount = 0;
|
|
|
|
for (uint32_t i=0; i<32; i++) {
|
|
|
|
// note, netmask LSB is IP MSB
|
|
|
|
if ((netmask_ip & (1UL<<i)) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
netmask_bitcount++;
|
|
|
|
}
|
|
|
|
return netmask_bitcount;
|
|
|
|
}
|
|
|
|
|
2023-08-05 00:51:54 -03:00
|
|
|
/*
|
|
|
|
convert a string to an ethernet MAC address
|
|
|
|
*/
|
|
|
|
bool AP_Networking::convert_str_to_macaddr(const char *mac_str, uint8_t addr[6])
|
|
|
|
{
|
|
|
|
if (strlen(mac_str) != 17) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
char s2[18];
|
|
|
|
strncpy(s2, mac_str, sizeof(s2)-1);
|
|
|
|
s2[17] = 0;
|
|
|
|
char *ptr = nullptr;
|
|
|
|
const char *s = strtok_r(s2, ":", &ptr);
|
|
|
|
for (uint8_t i=0; i<6; i++) {
|
|
|
|
if (s == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto v = strtoul(s, nullptr, 16);
|
|
|
|
if (v > 255) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
addr[i] = v;
|
|
|
|
s = strtok_r(nullptr, ":", &ptr);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-12 18:09:58 -04:00
|
|
|
// returns the 32bit value of the active IP address that is currently in use
|
|
|
|
uint32_t AP_Networking::get_ip_active() const
|
|
|
|
{
|
|
|
|
return backend?backend->activeSettings.ip:0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the 32bit value of the active Netmask that is currently in use
|
|
|
|
uint32_t AP_Networking::get_netmask_active() const
|
|
|
|
{
|
|
|
|
return backend?backend->activeSettings.nm:0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AP_Networking::get_gateway_active() const
|
|
|
|
{
|
|
|
|
return backend?backend->activeSettings.gw:0;
|
|
|
|
}
|
|
|
|
|
2023-11-29 18:23:39 -04:00
|
|
|
/*
|
|
|
|
wait for networking to be active
|
|
|
|
*/
|
|
|
|
void AP_Networking::startup_wait(void) const
|
|
|
|
{
|
|
|
|
if (hal.scheduler->in_main_thread()) {
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (!hal.scheduler->is_system_initialized()) {
|
|
|
|
hal.scheduler->delay(100);
|
|
|
|
}
|
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS
|
|
|
|
do {
|
|
|
|
hal.scheduler->delay(250);
|
|
|
|
} while (get_ip_active() == 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-12-07 14:32:32 -04:00
|
|
|
/*
|
|
|
|
send the rest of a file to a socket
|
|
|
|
*/
|
|
|
|
bool AP_Networking::sendfile(SocketAPM *sock, int fd)
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
if (sendfile_buf == nullptr) {
|
2024-01-04 22:37:02 -04:00
|
|
|
uint32_t bufsize = AP_NETWORKING_SENDFILE_BUFSIZE;
|
|
|
|
do {
|
|
|
|
sendfile_buf = (uint8_t *)hal.util->malloc_type(bufsize, AP_HAL::Util::MEM_FILESYSTEM);
|
|
|
|
if (sendfile_buf != nullptr) {
|
|
|
|
sendfile_bufsize = bufsize;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bufsize /= 2;
|
|
|
|
} while (bufsize >= 4096);
|
2023-12-07 14:32:32 -04:00
|
|
|
if (sendfile_buf == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-12-25 15:26:47 -04:00
|
|
|
if (!sendfile_thread_started) {
|
|
|
|
if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&AP_Networking::sendfile_check, void),
|
|
|
|
"sendfile",
|
|
|
|
2048, AP_HAL::Scheduler::PRIORITY_UART, 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sendfile_thread_started = true;
|
|
|
|
}
|
2023-12-07 14:32:32 -04:00
|
|
|
for (auto &s : sendfiles) {
|
|
|
|
if (s.sock == nullptr) {
|
|
|
|
s.sock = sock->duplicate();
|
|
|
|
if (s.sock == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
s.fd = fd;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Networking::SendFile::close(void)
|
|
|
|
{
|
|
|
|
AP::FS().close(fd);
|
|
|
|
delete sock;
|
|
|
|
sock = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
/*
|
|
|
|
check for sendfile updates
|
|
|
|
*/
|
|
|
|
void AP_Networking::sendfile_check(void)
|
|
|
|
{
|
2023-12-25 15:26:47 -04:00
|
|
|
while (true) {
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
bool none_active = true;
|
|
|
|
for (auto &s : sendfiles) {
|
|
|
|
if (s.sock == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
none_active = false;
|
|
|
|
if (!s.sock->pollout(0)) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-04 22:37:02 -04:00
|
|
|
const auto nread = AP::FS().read(s.fd, sendfile_buf, sendfile_bufsize);
|
2023-12-25 15:26:47 -04:00
|
|
|
if (nread <= 0) {
|
|
|
|
s.close();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto nsent = s.sock->send(sendfile_buf, nread);
|
|
|
|
if (nsent <= 0) {
|
|
|
|
s.close();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (nsent < nread) {
|
|
|
|
AP::FS().lseek(s.fd, nsent - nread, SEEK_CUR);
|
|
|
|
}
|
2023-12-07 14:32:32 -04:00
|
|
|
}
|
2023-12-25 15:26:47 -04:00
|
|
|
if (none_active) {
|
|
|
|
free(sendfile_buf);
|
|
|
|
sendfile_buf = nullptr;
|
2023-12-07 14:32:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
AP_Networking *AP_Networking::singleton;
|
2023-07-06 14:46:33 -03:00
|
|
|
|
2023-07-07 15:15:26 -03:00
|
|
|
namespace AP
|
|
|
|
{
|
|
|
|
AP_Networking &network()
|
|
|
|
{
|
|
|
|
return *AP_Networking::get_singleton();
|
|
|
|
}
|
2023-07-06 14:46:33 -03:00
|
|
|
}
|
|
|
|
|
2023-12-07 01:37:56 -04:00
|
|
|
/*
|
|
|
|
debug printfs from LWIP
|
|
|
|
*/
|
|
|
|
int ap_networking_printf(const char *fmt, ...)
|
|
|
|
{
|
2023-12-25 15:26:47 -04:00
|
|
|
WITH_SEMAPHORE(AP::network().get_semaphore());
|
|
|
|
#ifdef AP_NETWORKING_LWIP_DEBUG_FILE
|
|
|
|
static int fd = -1;
|
|
|
|
if (fd == -1) {
|
|
|
|
fd = AP::FS().open(AP_NETWORKING_LWIP_DEBUG_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0644);
|
|
|
|
if (fd == -1) {
|
2023-12-07 01:37:56 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2023-12-25 15:26:47 -04:00
|
|
|
vdprintf(fd, fmt, ap);
|
2023-12-07 01:37:56 -04:00
|
|
|
va_end(ap);
|
|
|
|
#else
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
hal.console->vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-01-12 21:20:51 -04:00
|
|
|
// address to string using a static return buffer
|
|
|
|
const char *AP_Networking::address_to_str(uint32_t addr)
|
|
|
|
{
|
|
|
|
static char buf[16]; // 16 for aaa.bbb.ccc.ddd
|
|
|
|
return SocketAPM::inet_addr_to_str(addr, buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
2023-12-25 15:26:47 -04:00
|
|
|
#ifdef LWIP_PLATFORM_ASSERT
|
|
|
|
void ap_networking_platform_assert(const char *msg, int line, const char *file)
|
|
|
|
{
|
|
|
|
AP_HAL::panic("LWIP: %s: %s:%u", msg, file, line);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-07-06 14:46:33 -03:00
|
|
|
#endif // AP_NETWORKING_ENABLED
|