John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/network/team_number.h" |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 2 | |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 3 | #include <netinet/in.h> |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 4 | #include <unistd.h> |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 5 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 6 | #include <cinttypes> |
| 7 | #include <cstdlib> |
| 8 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 9 | #include "absl/flags/flag.h" |
| 10 | #include "absl/log/check.h" |
| 11 | #include "absl/log/log.h" |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 12 | #include "absl/strings/numbers.h" |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 13 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 14 | ABSL_FLAG(std::string, override_hostname, "", |
| 15 | "If set, this forces the hostname of this node to be the provided " |
| 16 | "hostname."); |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 17 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 18 | namespace aos::network { |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 19 | namespace team_number_internal { |
| 20 | |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 21 | std::optional<uint16_t> ParseRoborioTeamNumber( |
| 22 | const std::string_view hostname) { |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 23 | for (size_t i = 0; i < hostname.size(); i++) { |
| 24 | if (hostname[i] == '-') { |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 25 | const std::string_view num_as_s = |
Austin Schuh | df5591e | 2015-12-19 22:36:50 -0800 | [diff] [blame] | 26 | hostname[hostname.size() - 1] == 'C' |
| 27 | ? hostname.substr(i + 1, hostname.size() - 5 - i) |
| 28 | : hostname.substr(i + 1); |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 29 | |
| 30 | int num; |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 31 | if (!absl::SimpleAtoi(num_as_s, &num)) { |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 32 | return std::nullopt; |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 33 | } |
| 34 | if (hostname.substr(0, i) == "roboRIO" && |
| 35 | std::to_string(num) == num_as_s) { |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 36 | return num; |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 37 | } |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 38 | return std::nullopt; |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 39 | } |
| 40 | } |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 41 | return std::nullopt; |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 42 | } |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 43 | |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 44 | std::optional<uint16_t> ParsePiOrOrinTeamNumber( |
| 45 | const std::string_view hostname) { |
| 46 | if ((hostname.substr(0, 3) != "pi-") && (hostname.substr(0, 5) != "orin-")) { |
Brian Silverman | 5f06ed2 | 2020-02-17 21:49:42 -0800 | [diff] [blame] | 47 | return std::nullopt; |
| 48 | } |
| 49 | size_t first_separator = hostname.find('-'); |
| 50 | if (first_separator == hostname.npos || |
| 51 | first_separator >= hostname.size() - 2) { |
| 52 | return std::nullopt; |
| 53 | } |
| 54 | ++first_separator; |
| 55 | const size_t second_separator = hostname.find('-', first_separator); |
| 56 | if (second_separator == hostname.npos) { |
| 57 | return std::nullopt; |
| 58 | } |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 59 | const std::string_view number_string = |
Brian Silverman | 5f06ed2 | 2020-02-17 21:49:42 -0800 | [diff] [blame] | 60 | hostname.substr(first_separator, second_separator - first_separator); |
| 61 | int number; |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 62 | if (!absl::SimpleAtoi(number_string, &number)) { |
Brian Silverman | 5f06ed2 | 2020-02-17 21:49:42 -0800 | [diff] [blame] | 63 | return std::nullopt; |
| 64 | } |
| 65 | return number; |
| 66 | } |
| 67 | |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 68 | } // namespace team_number_internal |
Austin Schuh | df5591e | 2015-12-19 22:36:50 -0800 | [diff] [blame] | 69 | |
| 70 | namespace { |
| 71 | |
| 72 | uint16_t override_team; |
| 73 | |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 74 | uint16_t DoGetTeamNumber() { |
John Park | a19d1a9 | 2019-11-06 18:28:14 -0800 | [diff] [blame] | 75 | if (override_team != 0) { |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 76 | return override_team; |
John Park | a19d1a9 | 2019-11-06 18:28:14 -0800 | [diff] [blame] | 77 | } |
Brian Silverman | 8a6dac9 | 2015-02-21 20:08:24 -0500 | [diff] [blame] | 78 | |
| 79 | const char *override_number = getenv("AOS_TEAM_NUMBER"); |
| 80 | if (override_number != nullptr) { |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 81 | uint32_t result; |
| 82 | if (!absl::SimpleAtoi(override_number, &result)) { |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 83 | LOG(FATAL) << "Error parsing AOS_TEAM_NUMBER: " << override_number; |
Brian Silverman | 8a6dac9 | 2015-02-21 20:08:24 -0500 | [diff] [blame] | 84 | } |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 85 | LOG(WARNING) |
| 86 | << "Team number overriden by AOS_TEAM_NUMBER environment variable to " |
| 87 | << result; |
| 88 | return result; |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 89 | } |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 90 | const auto hostname = GetHostname(); |
| 91 | { |
| 92 | const auto result = team_number_internal::ParseRoborioTeamNumber(hostname); |
| 93 | if (result) { |
| 94 | LOG(INFO) << "roboRIO hostname team number is: " << *result; |
| 95 | return *result; |
| 96 | } |
| 97 | } |
Brian Silverman | 5f06ed2 | 2020-02-17 21:49:42 -0800 | [diff] [blame] | 98 | { |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 99 | const auto result = team_number_internal::ParsePiOrOrinTeamNumber(hostname); |
Brian Silverman | 5f06ed2 | 2020-02-17 21:49:42 -0800 | [diff] [blame] | 100 | if (result) { |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 101 | LOG(INFO) << "Pi/Orin hostname team number is: " << *result; |
Brian Silverman | 5f06ed2 | 2020-02-17 21:49:42 -0800 | [diff] [blame] | 102 | return *result; |
| 103 | } |
| 104 | } |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 105 | LOG(FATAL) << "Failed to parse a team number from hostname: " << hostname; |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } // namespace |
| 109 | |
Austin Schuh | 288479d | 2019-12-18 19:47:52 -0800 | [diff] [blame] | 110 | ::std::string GetHostname() { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 111 | if (absl::GetFlag(FLAGS_override_hostname).empty()) { |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 112 | char buf[256]; |
| 113 | buf[sizeof(buf) - 1] = '\0'; |
| 114 | PCHECK(gethostname(buf, sizeof(buf) - 1) == 0); |
| 115 | return buf; |
| 116 | } else { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 117 | return absl::GetFlag(FLAGS_override_hostname); |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 118 | } |
Austin Schuh | 288479d | 2019-12-18 19:47:52 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 121 | uint16_t GetTeamNumber() { |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 122 | const static uint16_t result = DoGetTeamNumber(); |
John Park | a19d1a9 | 2019-11-06 18:28:14 -0800 | [diff] [blame] | 123 | return result; |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Brian Silverman | de0a53a | 2014-02-10 22:27:44 -0800 | [diff] [blame] | 126 | void OverrideTeamNumber(uint16_t team) { override_team = team; } |
| 127 | |
Jim Ostrowski | 3dc2164 | 2024-01-22 16:08:40 -0800 | [diff] [blame] | 128 | std::optional<std::string_view> ParsePiOrOrin(const std::string_view hostname) { |
| 129 | if (hostname.substr(0, 3) == "pi-") { |
| 130 | return std::string_view("pi"); |
| 131 | } else if (hostname.substr(0, 5) == "orin-") { |
| 132 | return std::string_view("orin"); |
| 133 | } else |
| 134 | return std::nullopt; |
| 135 | } |
| 136 | |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 137 | std::optional<uint16_t> ParsePiOrOrinNumber(const std::string_view hostname) { |
| 138 | if ((hostname.substr(0, 3) != "pi-") && (hostname.substr(0, 5) != "orin-")) { |
Austin Schuh | 1f2996e | 2020-03-15 23:09:00 -0700 | [diff] [blame] | 139 | return std::nullopt; |
| 140 | } |
| 141 | size_t first_separator = hostname.find('-'); |
| 142 | if (first_separator == hostname.npos || |
| 143 | first_separator >= hostname.size() - 2) { |
| 144 | return std::nullopt; |
| 145 | } |
| 146 | ++first_separator; |
| 147 | const size_t second_separator = hostname.find('-', first_separator); |
| 148 | if (second_separator == hostname.npos) { |
| 149 | return std::nullopt; |
| 150 | } |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 151 | const std::string_view number_string = hostname.substr( |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 152 | second_separator + 1, hostname.size() - second_separator - 1); |
Austin Schuh | 1f2996e | 2020-03-15 23:09:00 -0700 | [diff] [blame] | 153 | if (number_string.size() == 0) { |
| 154 | return std::nullopt; |
| 155 | } |
| 156 | |
| 157 | int number; |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 158 | if (!absl::SimpleAtoi(number_string, &number)) { |
Austin Schuh | 1f2996e | 2020-03-15 23:09:00 -0700 | [diff] [blame] | 159 | return std::nullopt; |
| 160 | } |
| 161 | return number; |
| 162 | } |
| 163 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 164 | } // namespace aos::network |