From cf752ffcdc3e9996e74e9e03215f038a2a58aac5 Mon Sep 17 00:00:00 2001 From: Ryan Friedman Date: Mon, 13 May 2024 22:10:30 -0600 Subject: [PATCH] AP_Networking: add AutoIP support * This allows link local addressing for zero-config setup Signed-off-by: Ryan Friedman --- libraries/AP_Networking/AP_Networking.cpp | 2 +- libraries/AP_Networking/AP_Networking.h | 15 +++++++++++++++ libraries/AP_Networking/AP_Networking_ChibiOS.cpp | 10 ++++++++++ libraries/AP_Networking/AP_Networking_Config.h | 3 +++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/libraries/AP_Networking/AP_Networking.cpp b/libraries/AP_Networking/AP_Networking.cpp index 19016f3f7044e..762e31204fb48 100644 --- a/libraries/AP_Networking/AP_Networking.cpp +++ b/libraries/AP_Networking/AP_Networking.cpp @@ -89,7 +89,7 @@ const AP_Param::GroupInfo AP_Networking::var_info[] = { // @Param: OPTIONS // @DisplayName: Networking options // @Description: Networking options - // @Bitmask: 0:EnablePPP Ethernet gateway + // @Bitmask: 0:EnablePPP Ethernet gateway,1:Enable AUTOIP (Link Local IPv4 Addressing) // @RebootRequired: True // @User: Advanced AP_GROUPINFO("OPTIONS", 9, AP_Networking, param.options, 0), diff --git a/libraries/AP_Networking/AP_Networking.h b/libraries/AP_Networking/AP_Networking.h index 128e6c2c29398..a80c43d9c5de8 100644 --- a/libraries/AP_Networking/AP_Networking.h +++ b/libraries/AP_Networking/AP_Networking.h @@ -70,6 +70,20 @@ class AP_Networking #endif } + + // returns true if AUTOIP is enabled + bool get_autoip_enabled() const + { +#if AP_NETWORKING_AUTOIP_AVAILABLE + return option_is_set(OPTION::AUTOIP); +#else + // AUTOIP 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 link local IPv4 addressing is enabled + return false; +#endif + } + // Sets DHCP to be enabled or disabled void set_dhcp_enable(const bool enable) { @@ -155,6 +169,7 @@ class AP_Networking enum class OPTION { PPP_ETHERNET_GATEWAY=(1U<<0), + AUTOIP=(1u<<1) }; bool option_is_set(OPTION option) const { return (param.options.get() & int32_t(option)) != 0; diff --git a/libraries/AP_Networking/AP_Networking_ChibiOS.cpp b/libraries/AP_Networking/AP_Networking_ChibiOS.cpp index 889a8526be752..57ca043b41640 100644 --- a/libraries/AP_Networking/AP_Networking_ChibiOS.cpp +++ b/libraries/AP_Networking/AP_Networking_ChibiOS.cpp @@ -148,6 +148,11 @@ void AP_Networking_ChibiOS::link_up_cb(void *p) if (driver->frontend.get_dhcp_enabled()) { dhcp_start(driver->thisif); } +# if LWIP_AUTOIP + if(driver->frontend.get_autoip_enabled()) { + autoip_start(driver->thisif); + } +#endif // LWIP_AUTOIP #endif } @@ -158,6 +163,11 @@ void AP_Networking_ChibiOS::link_down_cb(void *p) if (driver->frontend.get_dhcp_enabled()) { dhcp_stop(driver->thisif); } +# if LWIP_AUTOIP + if(driver->frontend.get_autoip_enabled()) { + autoip_stop(driver->thisif); + } +#endif // LWIP_AUTOIP #endif } diff --git a/libraries/AP_Networking/AP_Networking_Config.h b/libraries/AP_Networking/AP_Networking_Config.h index 5b66134957849..897edb7f22d2d 100644 --- a/libraries/AP_Networking/AP_Networking_Config.h +++ b/libraries/AP_Networking/AP_Networking_Config.h @@ -65,6 +65,9 @@ #define AP_NETWORKING_DHCP_AVAILABLE (AP_NETWORKING_CONTROLS_HOST_IP_SETTINGS_ENABLED || AP_NETWORKING_BACKEND_CHIBIOS) #endif +#ifndef AP_NETWORKING_AUTOIP_AVAILABLE +#define AP_NETWORKING_AUTOIP_AVAILABLE AP_NETWORKING_DHCP_AVAILABLE +#endif // --------------------------- // Below are default params