blob: d2fbc8e62573d86fd9aac5666bba75b50e16f1b1 [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
3#include <netinet/in.h>
Brian Silverman8f8b06f2013-10-30 22:04:27 -07004#include <inttypes.h>
Austin Schuhbe2456e2015-02-16 02:58:00 -08005#include <unistd.h>
Brian Silverman8a6dac92015-02-21 20:08:24 -05006#include <stdlib.h>
Austin Schuhbe2456e2015-02-16 02:58:00 -08007
Brian Silverman3dfbfb12020-02-17 20:35:18 -08008#include "glog/logging.h"
Brian Silverman431500a2013-10-28 19:50:15 -07009
John Park33858a32018-09-28 23:05:48 -070010#include "aos/util/string_to_num.h"
Brian Silverman431500a2013-10-28 19:50:15 -070011
Jim Ostrowski8565b402020-02-29 20:26:53 -080012DECLARE_string(override_hostname);
13
Brian Silverman431500a2013-10-28 19:50:15 -070014namespace aos {
15namespace network {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080016namespace team_number_internal {
17
18std::optional<uint16_t> ParseRoborioTeamNumber(const std::string &hostname) {
Austin Schuhbe2456e2015-02-16 02:58:00 -080019 for (size_t i = 0; i < hostname.size(); i++) {
20 if (hostname[i] == '-') {
Austin Schuhdf5591e2015-12-19 22:36:50 -080021 const std::string num_as_s =
22 hostname[hostname.size() - 1] == 'C'
23 ? hostname.substr(i + 1, hostname.size() - 5 - i)
24 : hostname.substr(i + 1);
Austin Schuhbe2456e2015-02-16 02:58:00 -080025
26 int num;
27 if (!::aos::util::StringToNumber(num_as_s, &num)) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080028 return std::nullopt;
Austin Schuhbe2456e2015-02-16 02:58:00 -080029 }
30 if (hostname.substr(0, i) == "roboRIO" &&
31 std::to_string(num) == num_as_s) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080032 return num;
Austin Schuhbe2456e2015-02-16 02:58:00 -080033 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080034 return std::nullopt;
Austin Schuhbe2456e2015-02-16 02:58:00 -080035 }
36 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080037 return std::nullopt;
Austin Schuhbe2456e2015-02-16 02:58:00 -080038}
Brian Silverman3dfbfb12020-02-17 20:35:18 -080039
Brian Silverman5f06ed22020-02-17 21:49:42 -080040std::optional<uint16_t> ParsePiTeamNumber(const std::string &hostname) {
41 if (hostname.substr(0, 3) != "pi-") {
42 return std::nullopt;
43 }
44 size_t first_separator = hostname.find('-');
45 if (first_separator == hostname.npos ||
46 first_separator >= hostname.size() - 2) {
47 return std::nullopt;
48 }
49 ++first_separator;
50 const size_t second_separator = hostname.find('-', first_separator);
51 if (second_separator == hostname.npos) {
52 return std::nullopt;
53 }
54 const std::string number_string =
55 hostname.substr(first_separator, second_separator - first_separator);
56 int number;
57 if (!util::StringToNumber(number_string, &number)) {
58 return std::nullopt;
59 }
60 return number;
61}
62
Brian Silverman3dfbfb12020-02-17 20:35:18 -080063} // namespace team_number_internal
Austin Schuhdf5591e2015-12-19 22:36:50 -080064
65namespace {
66
67uint16_t override_team;
68
Brian Silverman3dfbfb12020-02-17 20:35:18 -080069uint16_t DoGetTeamNumber() {
John Parka19d1a92019-11-06 18:28:14 -080070 if (override_team != 0) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080071 return override_team;
John Parka19d1a92019-11-06 18:28:14 -080072 }
Brian Silverman8a6dac92015-02-21 20:08:24 -050073
74 const char *override_number = getenv("AOS_TEAM_NUMBER");
75 if (override_number != nullptr) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080076 uint16_t result;
77 if (!::aos::util::StringToNumber(override_number, &result)) {
78 LOG(FATAL) << "Error parsing AOS_TEAM_NUMBER: " << override_number;
Brian Silverman8a6dac92015-02-21 20:08:24 -050079 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080080 LOG(WARNING)
81 << "Team number overriden by AOS_TEAM_NUMBER environment variable to "
82 << result;
83 return result;
Austin Schuhbe2456e2015-02-16 02:58:00 -080084 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080085 const auto hostname = GetHostname();
86 {
87 const auto result = team_number_internal::ParseRoborioTeamNumber(hostname);
88 if (result) {
89 LOG(INFO) << "roboRIO hostname team number is: " << *result;
90 return *result;
91 }
92 }
Brian Silverman5f06ed22020-02-17 21:49:42 -080093 {
94 const auto result = team_number_internal::ParsePiTeamNumber(hostname);
95 if (result) {
96 LOG(INFO) << "Pi hostname team number is: " << *result;
97 return *result;
98 }
99 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -0800100 LOG(FATAL) << "Failed to parse a team number from hostname: " << hostname;
Brian Silverman431500a2013-10-28 19:50:15 -0700101}
102
103} // namespace
104
Austin Schuh288479d2019-12-18 19:47:52 -0800105::std::string GetHostname() {
Jim Ostrowski8565b402020-02-29 20:26:53 -0800106 if (FLAGS_override_hostname.empty()) {
107 char buf[256];
108 buf[sizeof(buf) - 1] = '\0';
109 PCHECK(gethostname(buf, sizeof(buf) - 1) == 0);
110 return buf;
111 } else {
112 return FLAGS_override_hostname;
113 }
Austin Schuh288479d2019-12-18 19:47:52 -0800114}
115
Brian Silverman431500a2013-10-28 19:50:15 -0700116uint16_t GetTeamNumber() {
Brian Silverman3dfbfb12020-02-17 20:35:18 -0800117 const static uint16_t result = DoGetTeamNumber();
John Parka19d1a92019-11-06 18:28:14 -0800118 return result;
Brian Silverman431500a2013-10-28 19:50:15 -0700119}
120
Brian Silvermande0a53a2014-02-10 22:27:44 -0800121void OverrideTeamNumber(uint16_t team) { override_team = team; }
122
Brian Silverman431500a2013-10-28 19:50:15 -0700123} // namespace network
124} // namespace aos