blob: cc81e269c3ade2b9295d550416e424877ecf6dd4 [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
Jim Ostrowskicb8b4082024-01-21 02:23:46 -080042std::optional<uint16_t> ParsePiOrOrinTeamNumber(
43 const std::string_view hostname) {
44 if ((hostname.substr(0, 3) != "pi-") && (hostname.substr(0, 5) != "orin-")) {
Brian Silverman5f06ed22020-02-17 21:49:42 -080045 return std::nullopt;
46 }
47 size_t first_separator = hostname.find('-');
48 if (first_separator == hostname.npos ||
49 first_separator >= hostname.size() - 2) {
50 return std::nullopt;
51 }
52 ++first_separator;
53 const size_t second_separator = hostname.find('-', first_separator);
54 if (second_separator == hostname.npos) {
55 return std::nullopt;
56 }
Austin Schuh4c84abb2021-06-20 18:16:32 -070057 const std::string_view number_string =
Brian Silverman5f06ed22020-02-17 21:49:42 -080058 hostname.substr(first_separator, second_separator - first_separator);
59 int number;
Austin Schuh4c84abb2021-06-20 18:16:32 -070060 if (!absl::SimpleAtoi(number_string, &number)) {
Brian Silverman5f06ed22020-02-17 21:49:42 -080061 return std::nullopt;
62 }
63 return number;
64}
65
Brian Silverman3dfbfb12020-02-17 20:35:18 -080066} // namespace team_number_internal
Austin Schuhdf5591e2015-12-19 22:36:50 -080067
68namespace {
69
70uint16_t override_team;
71
Brian Silverman3dfbfb12020-02-17 20:35:18 -080072uint16_t DoGetTeamNumber() {
John Parka19d1a92019-11-06 18:28:14 -080073 if (override_team != 0) {
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -070074 return override_team;
John Parka19d1a92019-11-06 18:28:14 -080075 }
Brian Silverman8a6dac92015-02-21 20:08:24 -050076
77 const char *override_number = getenv("AOS_TEAM_NUMBER");
78 if (override_number != nullptr) {
Austin Schuh4c84abb2021-06-20 18:16:32 -070079 uint32_t result;
80 if (!absl::SimpleAtoi(override_number, &result)) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080081 LOG(FATAL) << "Error parsing AOS_TEAM_NUMBER: " << override_number;
Brian Silverman8a6dac92015-02-21 20:08:24 -050082 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080083 LOG(WARNING)
84 << "Team number overriden by AOS_TEAM_NUMBER environment variable to "
85 << result;
86 return result;
Austin Schuhbe2456e2015-02-16 02:58:00 -080087 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080088 const auto hostname = GetHostname();
89 {
90 const auto result = team_number_internal::ParseRoborioTeamNumber(hostname);
91 if (result) {
92 LOG(INFO) << "roboRIO hostname team number is: " << *result;
93 return *result;
94 }
95 }
Brian Silverman5f06ed22020-02-17 21:49:42 -080096 {
Jim Ostrowskicb8b4082024-01-21 02:23:46 -080097 const auto result = team_number_internal::ParsePiOrOrinTeamNumber(hostname);
Brian Silverman5f06ed22020-02-17 21:49:42 -080098 if (result) {
Jim Ostrowskicb8b4082024-01-21 02:23:46 -080099 LOG(INFO) << "Pi/Orin hostname team number is: " << *result;
Brian Silverman5f06ed22020-02-17 21:49:42 -0800100 return *result;
101 }
102 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -0800103 LOG(FATAL) << "Failed to parse a team number from hostname: " << hostname;
Brian Silverman431500a2013-10-28 19:50:15 -0700104}
105
106} // namespace
107
Austin Schuh288479d2019-12-18 19:47:52 -0800108::std::string GetHostname() {
Jim Ostrowski8565b402020-02-29 20:26:53 -0800109 if (FLAGS_override_hostname.empty()) {
110 char buf[256];
111 buf[sizeof(buf) - 1] = '\0';
112 PCHECK(gethostname(buf, sizeof(buf) - 1) == 0);
113 return buf;
114 } else {
115 return FLAGS_override_hostname;
116 }
Austin Schuh288479d2019-12-18 19:47:52 -0800117}
118
Brian Silverman431500a2013-10-28 19:50:15 -0700119uint16_t GetTeamNumber() {
Brian Silverman3dfbfb12020-02-17 20:35:18 -0800120 const static uint16_t result = DoGetTeamNumber();
John Parka19d1a92019-11-06 18:28:14 -0800121 return result;
Brian Silverman431500a2013-10-28 19:50:15 -0700122}
123
Brian Silvermande0a53a2014-02-10 22:27:44 -0800124void OverrideTeamNumber(uint16_t team) { override_team = team; }
125
Jim Ostrowski3dc21642024-01-22 16:08:40 -0800126std::optional<std::string_view> ParsePiOrOrin(const std::string_view hostname) {
127 if (hostname.substr(0, 3) == "pi-") {
128 return std::string_view("pi");
129 } else if (hostname.substr(0, 5) == "orin-") {
130 return std::string_view("orin");
131 } else
132 return std::nullopt;
133}
134
Jim Ostrowskicb8b4082024-01-21 02:23:46 -0800135std::optional<uint16_t> ParsePiOrOrinNumber(const std::string_view hostname) {
136 if ((hostname.substr(0, 3) != "pi-") && (hostname.substr(0, 5) != "orin-")) {
Austin Schuh1f2996e2020-03-15 23:09:00 -0700137 return std::nullopt;
138 }
139 size_t first_separator = hostname.find('-');
140 if (first_separator == hostname.npos ||
141 first_separator >= hostname.size() - 2) {
142 return std::nullopt;
143 }
144 ++first_separator;
145 const size_t second_separator = hostname.find('-', first_separator);
146 if (second_separator == hostname.npos) {
147 return std::nullopt;
148 }
Austin Schuh4c84abb2021-06-20 18:16:32 -0700149 const std::string_view number_string = hostname.substr(
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -0700150 second_separator + 1, hostname.size() - second_separator - 1);
Austin Schuh1f2996e2020-03-15 23:09:00 -0700151 if (number_string.size() == 0) {
152 return std::nullopt;
153 }
154
155 int number;
Austin Schuh4c84abb2021-06-20 18:16:32 -0700156 if (!absl::SimpleAtoi(number_string, &number)) {
Austin Schuh1f2996e2020-03-15 23:09:00 -0700157 return std::nullopt;
158 }
159 return number;
160}
161
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800162} // namespace aos::network