001/* 002 * Copyright (C) Photon Vision. 003 * 004 * This program is free software: you can redistribute it and/or modify 005 * it under the terms of the GNU General Public License as published by 006 * the Free Software Foundation, either version 3 of the License, or 007 * (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program. If not, see <https://www.gnu.org/licenses/>. 016 */ 017 018package org.photonvision.common.configuration; 019 020import com.fasterxml.jackson.annotation.JsonAlias; 021import com.fasterxml.jackson.annotation.JsonCreator; 022import com.fasterxml.jackson.annotation.JsonIgnore; 023import com.fasterxml.jackson.annotation.JsonProperty; 024import org.photonvision.common.hardware.Platform; 025import org.photonvision.common.networking.NetworkMode; 026 027public class NetworkConfig { 028 // Can be an integer team number, or an IP address 029 public String ntServerAddress = "0"; 030 public NetworkMode connectionType = NetworkMode.DHCP; 031 public String staticIp = ""; 032 public String hostname = "photonvision"; 033 public boolean runNTServer = false; 034 public boolean shouldManage; 035 public boolean shouldPublishProto = false; 036 037 @JsonIgnore public static final String NM_IFACE_STRING = "${interface}"; 038 @JsonIgnore public static final String NM_IP_STRING = "${ipaddr}"; 039 040 public String networkManagerIface = ""; 041 // TODO: remove these strings if no longer needed 042 public String setStaticCommand = 043 "nmcli con mod ${interface} ipv4.addresses ${ipaddr}/8 ipv4.method \"manual\" ipv6.method \"disabled\""; 044 public String setDHCPcommand = 045 "nmcli con mod ${interface} ipv4.method \"auto\" ipv6.method \"disabled\""; 046 047 public NetworkConfig() { 048 // We can (usually) manage networking on Linux devices, and if we can, we should try to. Command 049 // line inhibitions happen at a level above this class 050 setShouldManage(deviceCanManageNetwork()); 051 } 052 053 @JsonCreator 054 public NetworkConfig( 055 @JsonProperty("ntServerAddress") @JsonAlias({"ntServerAddress", "teamNumber"}) 056 String ntServerAddress, 057 @JsonProperty("connectionType") NetworkMode connectionType, 058 @JsonProperty("staticIp") String staticIp, 059 @JsonProperty("hostname") String hostname, 060 @JsonProperty("runNTServer") boolean runNTServer, 061 @JsonProperty("shouldManage") boolean shouldManage, 062 @JsonProperty("shouldPublishProto") boolean shouldPublishProto, 063 @JsonProperty("networkManagerIface") String networkManagerIface, 064 @JsonProperty("setStaticCommand") String setStaticCommand, 065 @JsonProperty("setDHCPcommand") String setDHCPcommand) { 066 this.ntServerAddress = ntServerAddress; 067 this.connectionType = connectionType; 068 this.staticIp = staticIp; 069 this.hostname = hostname; 070 this.runNTServer = runNTServer; 071 this.shouldPublishProto = shouldPublishProto; 072 this.networkManagerIface = networkManagerIface; 073 this.setStaticCommand = setStaticCommand; 074 this.setDHCPcommand = setDHCPcommand; 075 setShouldManage(shouldManage); 076 } 077 078 public NetworkConfig(NetworkConfig config) { 079 this( 080 config.ntServerAddress, 081 config.connectionType, 082 config.staticIp, 083 config.hostname, 084 config.runNTServer, 085 config.shouldManage, 086 config.shouldPublishProto, 087 config.networkManagerIface, 088 config.setStaticCommand, 089 config.setDHCPcommand); 090 } 091 092 @JsonIgnore 093 public String getPhysicalInterfaceName() { 094 return this.networkManagerIface; 095 } 096 097 @JsonIgnore 098 public String getEscapedInterfaceName() { 099 return "\"" + networkManagerIface + "\""; 100 } 101 102 public void setShouldManage(boolean shouldManage) { 103 this.shouldManage = shouldManage && this.deviceCanManageNetwork(); 104 } 105 106 @JsonIgnore 107 protected boolean deviceCanManageNetwork() { 108 return Platform.isLinux(); 109 } 110 111 @Override 112 public String toString() { 113 return "NetworkConfig [serverAddr=" 114 + ntServerAddress 115 + ", connectionType=" 116 + connectionType 117 + ", staticIp=" 118 + staticIp 119 + ", hostname=" 120 + hostname 121 + ", runNTServer=" 122 + runNTServer 123 + ", networkManagerIface=" 124 + networkManagerIface 125 + ", setStaticCommand=" 126 + setStaticCommand 127 + ", setDHCPcommand=" 128 + setDHCPcommand 129 + ", shouldManage=" 130 + shouldManage 131 + "]"; 132 } 133}