Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,22 @@ private void checkHostnameAndCameraNames() {
String mac = NetworkUtils.getMacAddress();
if (!mac.equals(currentMacAddress)) {
logger.debug("MAC address changed! New MAC address is " + mac + ", was " + currentMacAddress);
kCoprocTable.getSubTable(currentMacAddress).getEntry("hostname").unpublish();
kCoprocTable.getSubTable(currentMacAddress).getEntry("cameraNames").unpublish();
currentMacAddress = mac;
}
if (mac.isEmpty()) {
if (mac.equals("00-00-00-00-00-00")) {
logger.error("Cannot check hostname and camera names, MAC address is not set!");
return;
}

String hostname = ConfigManager.getInstance().getConfig().getNetworkConfig().hostname;
var config = ConfigManager.getInstance().getConfig();
String hostname;
if (config.getNetworkConfig().shouldManage) {
hostname = config.getNetworkConfig().hostname;
} else {
hostname = CameraServerJNI.getHostname();
}
if (hostname == null || hostname.isEmpty()) {
logger.error("Cannot check hostname and camera names, hostname is not set!");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.photonvision.common.networking;

import edu.wpi.first.networktables.NetworkTableInstance;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -218,40 +220,54 @@ public static String getIPAddresses(String iFaceName) {
return String.join(", ", addresses);
}

/**
* Gets a MAC address of a network interface. On devices where networking is managed by
* PhotonVision, this will return the MAC address of the configured interface. Otherwise, this
* will attempt to search for the network interface in current use and use that interface's MAC
* address, and if that fails, it will return a MAC address from the first network interface with
* a MAC address, as sorted by {@link NetworkInterface#networkInterfaces()}.
*
* @return The MAC address.
*/
public static String getMacAddress() {
var config = ConfigManager.getInstance().getConfig().getNetworkConfig();
if (config.networkManagerIface == null || config.networkManagerIface.isBlank()) {
// This is a silly heuristic to find a network interface that PV might be using. It looks like
// it works pretty well, but Hyper-V adapters still show up in the list. But we're using MAC
// address as a semi-unique identifier, not as a source of truth, so this should be fine.
// Hyper-V adapters seem to show up near the end of the list anyways, so it's super likely
// we'll find the right adapter anyways
try {
try {
// Not managed? See if we're connected to a network. General assumption is one interface in
// use at a time
if (config.networkManagerIface == null || config.networkManagerIface.isBlank()) {
// Use NT client IP address to find the interface in use
if (!config.runNTServer) {
var conn = NetworkTableInstance.getDefault().getConnections();
if (conn.length > 0 && !conn[0].remote_ip.equals("127.0.0.1")) {
var addr = InetAddress.getByName(conn[0].remote_ip);
return formatMacAddress(NetworkInterface.getByInetAddress(addr).getHardwareAddress());
}
}
// Connected to a localhost server or we are the server? Try resolving ourselves. Only
// returns a localhost address when there's no other interface available
byte[] mac =
NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();
if (mac != null) {
return formatMacAddress(mac);
}
// Fine. Just find something with a MAC address
for (var iface : NetworkInterface.networkInterfaces().toList()) {
if (iface.isUp() && !iface.isVirtual() && !iface.isLoopback()) {
byte[] mac = iface.getHardwareAddress();
if (mac == null) {
logger.error("No MAC address found for " + iface.getDisplayName());
}
return formatMacAddress(mac);
if (iface.isUp() && iface.getHardwareAddress() != null) {
return formatMacAddress(iface.getHardwareAddress());
}
}
} catch (Exception e) {
logger.error("Error getting MAC address:", e);
}
return "";
}
try {
byte[] mac = NetworkInterface.getByName(config.networkManagerIface).getHardwareAddress();
if (mac == null) {
logger.error("No MAC address found for " + config.networkManagerIface);
return "";
} else { // Managed? We should have a working interface available
byte[] mac = NetworkInterface.getByName(config.networkManagerIface).getHardwareAddress();
if (mac != null) {
return formatMacAddress(mac);
} else {
logger.error("No MAC address found for " + config.networkManagerIface);
}
}
return formatMacAddress(mac);
} catch (Exception e) {
logger.error("Error getting MAC address for " + config.networkManagerIface, e);
return "";
logger.error("Error getting MAC address", e);
}
return "00-00-00-00-00-00";
}

private static String formatMacAddress(byte[] mac) {
Expand Down
Loading