blob: bd90b963c86f0eca962da5f0f698abf03d818f09 [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 Schuh99f7c6a2024-06-25 22:07:44 -07009#include "absl/flags/flag.h"
10#include "absl/log/check.h"
11#include "absl/log/log.h"
Austin Schuh4c84abb2021-06-20 18:16:32 -070012#include "absl/strings/numbers.h"
Brian Silverman431500a2013-10-28 19:50:15 -070013
Austin Schuh99f7c6a2024-06-25 22:07:44 -070014ABSL_FLAG(std::string, override_hostname, "",
15 "If set, this forces the hostname of this node to be the provided "
16 "hostname.");
Jim Ostrowski8565b402020-02-29 20:26:53 -080017
Stephan Pleinesf63bde82024-01-13 15:59:33 -080018namespace aos::network {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080019namespace team_number_internal {
20
Austin Schuh4c84abb2021-06-20 18:16:32 -070021std::optional<uint16_t> ParseRoborioTeamNumber(
22 const std::string_view hostname) {
Austin Schuhbe2456e2015-02-16 02:58:00 -080023 for (size_t i = 0; i < hostname.size(); i++) {
24 if (hostname[i] == '-') {
Austin Schuh4c84abb2021-06-20 18:16:32 -070025 const std::string_view num_as_s =
Austin Schuhdf5591e2015-12-19 22:36:50 -080026 hostname[hostname.size() - 1] == 'C'
27 ? hostname.substr(i + 1, hostname.size() - 5 - i)
28 : hostname.substr(i + 1);
Austin Schuhbe2456e2015-02-16 02:58:00 -080029
30 int num;
Austin Schuh4c84abb2021-06-20 18:16:32 -070031 if (!absl::SimpleAtoi(num_as_s, &num)) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080032 return std::nullopt;
Austin Schuhbe2456e2015-02-16 02:58:00 -080033 }
34 if (hostname.substr(0, i) == "roboRIO" &&
35 std::to_string(num) == num_as_s) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080036 return num;
Austin Schuhbe2456e2015-02-16 02:58:00 -080037 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080038 return std::nullopt;
Austin Schuhbe2456e2015-02-16 02:58:00 -080039 }
40 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080041 return std::nullopt;
Austin Schuhbe2456e2015-02-16 02:58:00 -080042}
Brian Silverman3dfbfb12020-02-17 20:35:18 -080043
Jim Ostrowskicb8b4082024-01-21 02:23:46 -080044std::optional<uint16_t> ParsePiOrOrinTeamNumber(
45 const std::string_view hostname) {
46 if ((hostname.substr(0, 3) != "pi-") && (hostname.substr(0, 5) != "orin-")) {
Brian Silverman5f06ed22020-02-17 21:49:42 -080047 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 Schuh4c84abb2021-06-20 18:16:32 -070059 const std::string_view number_string =
Brian Silverman5f06ed22020-02-17 21:49:42 -080060 hostname.substr(first_separator, second_separator - first_separator);
61 int number;
Austin Schuh4c84abb2021-06-20 18:16:32 -070062 if (!absl::SimpleAtoi(number_string, &number)) {
Brian Silverman5f06ed22020-02-17 21:49:42 -080063 return std::nullopt;
64 }
65 return number;
66}
67
Brian Silverman3dfbfb12020-02-17 20:35:18 -080068} // namespace team_number_internal
Austin Schuhdf5591e2015-12-19 22:36:50 -080069
70namespace {
71
72uint16_t override_team;
73
Brian Silverman3dfbfb12020-02-17 20:35:18 -080074uint16_t DoGetTeamNumber() {
John Parka19d1a92019-11-06 18:28:14 -080075 if (override_team != 0) {
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -070076 return override_team;
John Parka19d1a92019-11-06 18:28:14 -080077 }
Brian Silverman8a6dac92015-02-21 20:08:24 -050078
79 const char *override_number = getenv("AOS_TEAM_NUMBER");
80 if (override_number != nullptr) {
Austin Schuh4c84abb2021-06-20 18:16:32 -070081 uint32_t result;
82 if (!absl::SimpleAtoi(override_number, &result)) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080083 LOG(FATAL) << "Error parsing AOS_TEAM_NUMBER: " << override_number;
Brian Silverman8a6dac92015-02-21 20:08:24 -050084 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080085 LOG(WARNING)
86 << "Team number overriden by AOS_TEAM_NUMBER environment variable to "
87 << result;
88 return result;
Austin Schuhbe2456e2015-02-16 02:58:00 -080089 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080090 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 Silverman5f06ed22020-02-17 21:49:42 -080098 {
Jim Ostrowskicb8b4082024-01-21 02:23:46 -080099 const auto result = team_number_internal::ParsePiOrOrinTeamNumber(hostname);
Brian Silverman5f06ed22020-02-17 21:49:42 -0800100 if (result) {
Jim Ostrowskicb8b4082024-01-21 02:23:46 -0800101 LOG(INFO) << "Pi/Orin hostname team number is: " << *result;
Brian Silverman5f06ed22020-02-17 21:49:42 -0800102 return *result;
103 }
104 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -0800105 LOG(FATAL) << "Failed to parse a team number from hostname: " << hostname;
Brian Silverman431500a2013-10-28 19:50:15 -0700106}
107
108} // namespace
109
Austin Schuh288479d2019-12-18 19:47:52 -0800110::std::string GetHostname() {
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700111 if (absl::GetFlag(FLAGS_override_hostname).empty()) {
Jim Ostrowski8565b402020-02-29 20:26:53 -0800112 char buf[256];
113 buf[sizeof(buf) - 1] = '\0';
114 PCHECK(gethostname(buf, sizeof(buf) - 1) == 0);
115 return buf;
116 } else {
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700117 return absl::GetFlag(FLAGS_override_hostname);
Jim Ostrowski8565b402020-02-29 20:26:53 -0800118 }
Austin Schuh288479d2019-12-18 19:47:52 -0800119}
120
Brian Silverman431500a2013-10-28 19:50:15 -0700121uint16_t GetTeamNumber() {
Brian Silverman3dfbfb12020-02-17 20:35:18 -0800122 const static uint16_t result = DoGetTeamNumber();
John Parka19d1a92019-11-06 18:28:14 -0800123 return result;
Brian Silverman431500a2013-10-28 19:50:15 -0700124}
125
Brian Silvermande0a53a2014-02-10 22:27:44 -0800126void OverrideTeamNumber(uint16_t team) { override_team = team; }
127
Jim Ostrowski3dc21642024-01-22 16:08:40 -0800128std::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");
Jim Ostrowski73ffc7a2024-09-21 15:13:18 -0700133 } else if (hostname.substr(0, 4) == "imu-") {
134 return std::string_view("orin");
Jim Ostrowski3dc21642024-01-22 16:08:40 -0800135 } else
136 return std::nullopt;
137}
138
Jim Ostrowskicb8b4082024-01-21 02:23:46 -0800139std::optional<uint16_t> ParsePiOrOrinNumber(const std::string_view hostname) {
140 if ((hostname.substr(0, 3) != "pi-") && (hostname.substr(0, 5) != "orin-")) {
Austin Schuh1f2996e2020-03-15 23:09:00 -0700141 return std::nullopt;
142 }
143 size_t first_separator = hostname.find('-');
144 if (first_separator == hostname.npos ||
145 first_separator >= hostname.size() - 2) {
146 return std::nullopt;
147 }
148 ++first_separator;
149 const size_t second_separator = hostname.find('-', first_separator);
150 if (second_separator == hostname.npos) {
151 return std::nullopt;
152 }
Austin Schuh4c84abb2021-06-20 18:16:32 -0700153 const std::string_view number_string = hostname.substr(
Jim Ostrowski5d5a44f2020-06-24 19:10:15 -0700154 second_separator + 1, hostname.size() - second_separator - 1);
Austin Schuh1f2996e2020-03-15 23:09:00 -0700155 if (number_string.size() == 0) {
156 return std::nullopt;
157 }
158
159 int number;
Austin Schuh4c84abb2021-06-20 18:16:32 -0700160 if (!absl::SimpleAtoi(number_string, &number)) {
Austin Schuh1f2996e2020-03-15 23:09:00 -0700161 return std::nullopt;
162 }
163 return number;
164}
165
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800166} // namespace aos::network