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-08-05 03:46:13 -03:00
|
|
|
#include "AP_Networking_ChibiOS.h"
|
2023-07-06 14:46:33 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2023-07-06 14:46:33 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|
2023-07-07 15:15:26 -03:00
|
|
|
#include <hal_mii.h>
|
|
|
|
#include <lwip/sockets.h>
|
2023-07-06 14:46:33 -03:00
|
|
|
#else
|
2023-07-07 15:15:26 -03:00
|
|
|
#include <arpa/inet.h>
|
2023-07-06 14:46:33 -03:00
|
|
|
#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[] = {
|
|
|
|
// @Param: ENABLED
|
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
|
2023-08-05 03:46:13 -03:00
|
|
|
AP_GROUPINFO_FLAGS("ENABLED", 1, AP_Networking, param.enabled, 0, AP_PARAM_FLAG_ENABLE),
|
2023-07-06 14:46:33 -03:00
|
|
|
|
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
|
|
|
|
|
|
|
// @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-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
|
|
|
|
// @Path: AP_Networking_address.cpp
|
2023-08-05 03:46:13 -03:00
|
|
|
AP_SUBGROUPINFO(param.macaddr, "MACADDR", 6, AP_Networking, AP_Networking_MAC),
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set default MAC Address lower 3 bytes to UUID if possible
|
|
|
|
uint8_t uuid[12];
|
|
|
|
uint8_t uuid_len = sizeof(uuid);
|
|
|
|
const bool udid_is_ok = hal.util->get_system_id_unformatted(uuid, uuid_len) && uuid_len >= 3;
|
|
|
|
if (udid_is_ok) {
|
2023-08-05 00:51:54 -03:00
|
|
|
for (uint8_t i=0; i<3; i++) {
|
2023-08-05 03:46:13 -03:00
|
|
|
param.macaddr.set_default_address_byte(i, uuid[uuid_len-3+i]);
|
2023-08-05 00:51:54 -03:00
|
|
|
}
|
2023-07-06 14:46:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|
2023-08-05 03:46:13 -03:00
|
|
|
backend = new AP_Networking_ChibiOS(*this);
|
2023-07-28 17:29:33 -03: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");
|
|
|
|
backend = nullptr;
|
2023-07-06 14:46:33 -03:00
|
|
|
}
|
|
|
|
|
2023-08-05 03:46:13 -03:00
|
|
|
announce_address_changes();
|
2023-07-06 14:46:33 -03:00
|
|
|
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_DEBUG,"NET: Initialized");
|
|
|
|
}
|
|
|
|
|
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-08-05 03:46:13 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "NET: IP %s", get_ip_active_str());
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "NET: Mask %s", get_netmask_active_str());
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "NET: Gateway %s", get_gateway_active_str());
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (netmask_bitcount > 32) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint32_t netmask_ip = 0;
|
|
|
|
for (uint32_t i=0; i<netmask_bitcount; i++) {
|
|
|
|
netmask_ip |= (1UL << i);
|
|
|
|
}
|
|
|
|
return netmask_ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
uint32_t AP_Networking::convert_str_to_ip(const char* ip_str)
|
2023-07-06 14:46:33 -03:00
|
|
|
{
|
|
|
|
uint32_t ip = 0;
|
|
|
|
inet_pton(AF_INET, ip_str, &ip);
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
2023-08-02 21:53:32 -03:00
|
|
|
const char* AP_Networking::convert_ip_to_str(const uint8_t ip[4])
|
2023-07-06 14:46:33 -03:00
|
|
|
{
|
|
|
|
static char _str_buffer[20];
|
|
|
|
if (hal.util->snprintf(_str_buffer, sizeof(_str_buffer), "%u.%u.%u.%u", (unsigned)ip[0], (unsigned)ip[1], (unsigned)ip[2], (unsigned)ip[3]) == 0) {
|
|
|
|
_str_buffer[0] = '\0';
|
|
|
|
}
|
|
|
|
return _str_buffer;
|
|
|
|
}
|
2023-08-05 03:46:13 -03:00
|
|
|
|
2023-08-02 21:53:32 -03:00
|
|
|
const char* AP_Networking::convert_ip_to_str(const uint32_t ip)
|
2023-07-06 14:46:33 -03:00
|
|
|
{
|
|
|
|
uint8_t ip_array[4];
|
2023-07-07 15:15:26 -03:00
|
|
|
ip_array[3] = ((ip >> 24) & 0xff);
|
|
|
|
ip_array[2] = ((ip >> 16) & 0xff);
|
|
|
|
ip_array[1] = ((ip >> 8) & 0xff);
|
|
|
|
ip_array[0] = (ip & 0xff);
|
2023-07-06 14:46:33 -03:00
|
|
|
|
|
|
|
return convert_ip_to_str(ip_array);
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
#endif // AP_NETWORKING_ENABLED
|