blob: 28ee67af955034c2bc7659832793ce2e5b4fc210 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/network/team_number.h"
Brian Silverman431500a2013-10-28 19:50:15 -07002
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -07003#include <netinet/in.h>
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -07004#include <unistd.h>
Austin Schuhbe2456e2015-02-16 02:58:00 -08005
Tyler Chatowbf0609c2021-07-31 16:13:27 -07006#include <cinttypes>
7#include <cstdlib>
8
Austin Schuh4c84abb2021-06-20 18:16:32 -07009#include "absl/strings/numbers.h"
Brian Silverman431500a2013-10-28 19:50:15 -070010
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -070011DEFINE_string(
12 override_hostname, "",
Jim Ostrowski2192ddb2020-06-24 19:07:31 -070013 "If set, this forces the hostname of this node to be the provided "
14 "hostname.");
Jim Ostrowski8565b402020-02-29 20:26:53 -080015
Stephan Pleinesf63bde82024-01-13 15:59:33 -080016namespace aos::network {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080017namespace team_number_internal {
18
Austin Schuh4c84abb2021-06-20 18:16:32 -070019std::optional<uint16_t> ParseRoborioTeamNumber(
20 const std::string_view hostname) {
Austin Schuhbe2456e2015-02-16 02:58:00 -080021 for (size_t i = 0; i < hostname.size(); i++) {
22 if (hostname[i] == '-') {
Austin Schuh4c84abb2021-06-20 18:16:32 -070023 const std::string_view num_as_s =
Austin Schuhdf5591e2015-12-19 22:36:50 -080024 hostname[hostname.size() - 1] == 'C'
25 ? hostname.substr(i + 1, hostname.size() - 5 - i)
26 : hostname.substr(i + 1);
Austin Schuhbe2456e2015-02-16 02:58:00 -080027
28 int num;
Austin Schuh4c84abb2021-06-20 18:16:32 -070029 if (!absl::SimpleAtoi(num_as_s, &num)) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080030 return std::nullopt;
Austin Schuhbe2456e2015-02-16 02:58:00 -080031 }
32 if (hostname.substr(0, i) == "roboRIO" &&
33 std::to_string(num) == num_as_s) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080034 return num;
Austin Schuhbe2456e2015-02-16 02:58:00 -080035 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080036 return std::nullopt;
Austin Schuhbe2456e2015-02-16 02:58:00 -080037 }
38 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080039 return std::nullopt;
Austin Schuhbe2456e2015-02-16 02:58:00 -080040}
Brian Silverman3dfbfb12020-02-17 20:35:18 -080041
Austin Schuh4c84abb2021-06-20 18:16:32 -070042std::optional<uint16_t> ParsePiTeamNumber(const std::string_view hostname) {
Brian Silverman5f06ed22020-02-17 21:49:42 -080043 if (hostname.substr(0, 3) != "pi-") {
44 return std::nullopt;
45 }
46 size_t first_separator = hostname.find('-');
47 if (first_separator == hostname.npos ||
48 first_separator >= hostname.size() - 2) {
49 return std::nullopt;
50 }
51 ++first_separator;
52 const size_t second_separator = hostname.find('-', first_separator);
53 if (second_separator == hostname.npos) {
54 return std::nullopt;
55 }
Austin Schuh4c84abb2021-06-20 18:16:32 -070056 const std::string_view number_string =
Brian Silverman5f06ed22020-02-17 21:49:42 -080057 hostname.substr(first_separator, second_separator - first_separator);
58 int number;
Austin Schuh4c84abb2021-06-20 18:16:32 -070059 if (!absl::SimpleAtoi(number_string, &number)) {
Brian Silverman5f06ed22020-02-17 21:49:42 -080060 return std::nullopt;
61 }
62 return number;
63}
64
Brian Silverman3dfbfb12020-02-17 20:35:18 -080065} // namespace team_number_internal
Austin Schuhdf5591e2015-12-19 22:36:50 -080066
67namespace {
68
69uint16_t override_team;
70
Brian Silverman3dfbfb12020-02-17 20:35:18 -080071uint16_t DoGetTeamNumber() {
John Parka19d1a92019-11-06 18:28:14 -080072 if (override_team != 0) {
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -070073 return override_team;
John Parka19d1a92019-11-06 18:28:14 -080074 }
Brian Silverman8a6dac92015-02-21 20:08:24 -050075
76 const char *override_number = getenv("AOS_TEAM_NUMBER");
77 if (override_number != nullptr) {
Austin Schuh4c84abb2021-06-20 18:16:32 -070078 uint32_t result;
79 if (!absl::SimpleAtoi(override_number, &result)) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080080 LOG(FATAL) << "Error parsing AOS_TEAM_NUMBER: " << override_number;
Brian Silverman8a6dac92015-02-21 20:08:24 -050081 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080082 LOG(WARNING)
83 << "Team number overriden by AOS_TEAM_NUMBER environment variable to "
84 << result;
85 return result;
Austin Schuhbe2456e2015-02-16 02:58:00 -080086 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080087 const auto hostname = GetHostname();
88 {
89 const auto result = team_number_internal::ParseRoborioTeamNumber(hostname);
90 if (result) {
91 LOG(INFO) << "roboRIO hostname team number is: " << *result;
92 return *result;
93 }
94 }
Brian Silverman5f06ed22020-02-17 21:49:42 -080095 {
96 const auto result = team_number_internal::ParsePiTeamNumber(hostname);
97 if (result) {
98 LOG(INFO) << "Pi hostname team number is: " << *result;
99 return *result;
100 }
101 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -0800102 LOG(FATAL) << "Failed to parse a team number from hostname: " << hostname;
Brian Silverman431500a2013-10-28 19:50:15 -0700103}
104
105} // namespace
106
Austin Schuh288479d2019-12-18 19:47:52 -0800107::std::string GetHostname() {
Jim Ostrowski8565b402020-02-29 20:26:53 -0800108 if (FLAGS_override_hostname.empty()) {
109 char buf[256];
110 buf[sizeof(buf) - 1] = '\0';
111 PCHECK(gethostname(buf, sizeof(buf) - 1) == 0);
112 return buf;
113 } else {
114 return FLAGS_override_hostname;
115 }
Austin Schuh288479d2019-12-18 19:47:52 -0800116}
117
Brian Silverman431500a2013-10-28 19:50:15 -0700118uint16_t GetTeamNumber() {
Brian Silverman3dfbfb12020-02-17 20:35:18 -0800119 const static uint16_t result = DoGetTeamNumber();
John Parka19d1a92019-11-06 18:28:14 -0800120 return result;
Brian Silverman431500a2013-10-28 19:50:15 -0700121}
122
Brian Silvermande0a53a2014-02-10 22:27:44 -0800123void OverrideTeamNumber(uint16_t team) { override_team = team; }
124
Austin Schuh4c84abb2021-06-20 18:16:32 -0700125std::optional<uint16_t> ParsePiNumber(const std::string_view hostname) {
Austin Schuh1f2996e2020-03-15 23:09:00 -0700126 if (hostname.substr(0, 3) != "pi-") {
127 return std::nullopt;
128 }
129 size_t first_separator = hostname.find('-');
130 if (first_separator == hostname.npos ||
131 first_separator >= hostname.size() - 2) {
132 return std::nullopt;
133 }
134 ++first_separator;
135 const size_t second_separator = hostname.find('-', first_separator);
136 if (second_separator == hostname.npos) {
137 return std::nullopt;
138 }
Austin Schuh4c84abb2021-06-20 18:16:32 -0700139 const std::string_view number_string = hostname.substr(
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -0700140 second_separator + 1, hostname.size() - second_separator - 1);
Austin Schuh1f2996e2020-03-15 23:09:00 -0700141 if (number_string.size() == 0) {
142 return std::nullopt;
143 }
144
145 int number;
Austin Schuh4c84abb2021-06-20 18:16:32 -0700146 if (!absl::SimpleAtoi(number_string, &number)) {
Austin Schuh1f2996e2020-03-15 23:09:00 -0700147 return std::nullopt;
148 }
149 return number;
150}
151
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800152} // namespace aos::network