Skip to content

Commit

Permalink
can or vcan detection also by name
Browse files Browse the repository at this point in the history
  • Loading branch information
jvde-github committed Feb 24, 2025
1 parent b862f5c commit 40eaf07
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions Device/N2KsktCAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,30 @@ namespace Device
uint64_t i = 0;
for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
{

// check for virtual CAN interfaces
if (ifa->ifa_addr == nullptr && (strncmp(ifa->ifa_name, "vcan", 4) != 0))
if (ifa->ifa_name == nullptr)
continue;

// If there is an address, only add if it is a CAN interface.
if (ifa->ifa_addr != nullptr && ifa->ifa_addr->sa_family != AF_CAN)
// Check if it's either a CAN or virtual CAN interface by name
bool is_can_by_name = (strncmp(ifa->ifa_name, "can", 3) == 0);
bool is_vcan = (strncmp(ifa->ifa_name, "vcan", 4) == 0);

// If it has no address but is a CAN or vCAN interface by name, include it
if (ifa->ifa_addr == nullptr)
{
if (is_can_by_name || is_vcan)
{
DeviceList.push_back(Description("NMEA2000", "CANbus", ifa->ifa_name, i++, Type::N2K));
available_intefaces.push_back(ifa->ifa_name);
}
continue;
}
DeviceList.push_back(Description("NMEA2000", "CANbus", ifa->ifa_name, i++, Type::N2K));
available_intefaces.push_back(ifa->ifa_name);

// For interfaces with addresses, check if they're CAN interfaces by address family
if (ifa->ifa_addr->sa_family == AF_CAN)
{
DeviceList.push_back(Description("NMEA2000", "CANbus", ifa->ifa_name, i++, Type::N2K));
available_intefaces.push_back(ifa->ifa_name);
}
}

freeifaddrs(ifaddr);
Expand Down

0 comments on commit 40eaf07

Please sign in to comment.