blob: 14fadab9b8147c77e701e5ce4557dcc53d6de2ff [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
12namespace aos {
13namespace network {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080014namespace team_number_internal {
15
16std::optional<uint16_t> ParseRoborioTeamNumber(const std::string &hostname) {
Austin Schuhbe2456e2015-02-16 02:58:00 -080017 for (size_t i = 0; i < hostname.size(); i++) {
18 if (hostname[i] == '-') {
Austin Schuhdf5591e2015-12-19 22:36:50 -080019 const std::string num_as_s =
20 hostname[hostname.size() - 1] == 'C'
21 ? hostname.substr(i + 1, hostname.size() - 5 - i)
22 : hostname.substr(i + 1);
Austin Schuhbe2456e2015-02-16 02:58:00 -080023
24 int num;
25 if (!::aos::util::StringToNumber(num_as_s, &num)) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080026 return std::nullopt;
Austin Schuhbe2456e2015-02-16 02:58:00 -080027 }
28 if (hostname.substr(0, i) == "roboRIO" &&
29 std::to_string(num) == num_as_s) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080030 return num;
Austin Schuhbe2456e2015-02-16 02:58:00 -080031 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080032 return std::nullopt;
Austin Schuhbe2456e2015-02-16 02:58:00 -080033 }
34 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080035 return std::nullopt;
Austin Schuhbe2456e2015-02-16 02:58:00 -080036}
Brian Silverman3dfbfb12020-02-17 20:35:18 -080037
Brian Silverman5f06ed22020-02-17 21:49:42 -080038std::optional<uint16_t> ParsePiTeamNumber(const std::string &hostname) {
39 if (hostname.substr(0, 3) != "pi-") {
40 return std::nullopt;
41 }
42 size_t first_separator = hostname.find('-');
43 if (first_separator == hostname.npos ||
44 first_separator >= hostname.size() - 2) {
45 return std::nullopt;
46 }
47 ++first_separator;
48 const size_t second_separator = hostname.find('-', first_separator);
49 if (second_separator == hostname.npos) {
50 return std::nullopt;
51 }
52 const std::string number_string =
53 hostname.substr(first_separator, second_separator - first_separator);
54 int number;
55 if (!util::StringToNumber(number_string, &number)) {
56 return std::nullopt;
57 }
58 return number;
59}
60
Brian Silverman3dfbfb12020-02-17 20:35:18 -080061} // namespace team_number_internal
Austin Schuhdf5591e2015-12-19 22:36:50 -080062
63namespace {
64
65uint16_t override_team;
66
Brian Silverman3dfbfb12020-02-17 20:35:18 -080067uint16_t DoGetTeamNumber() {
John Parka19d1a92019-11-06 18:28:14 -080068 if (override_team != 0) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080069 return override_team;
John Parka19d1a92019-11-06 18:28:14 -080070 }
Brian Silverman8a6dac92015-02-21 20:08:24 -050071
72 const char *override_number = getenv("AOS_TEAM_NUMBER");
73 if (override_number != nullptr) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080074 uint16_t result;
75 if (!::aos::util::StringToNumber(override_number, &result)) {
76 LOG(FATAL) << "Error parsing AOS_TEAM_NUMBER: " << override_number;
Brian Silverman8a6dac92015-02-21 20:08:24 -050077 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080078 LOG(WARNING)
79 << "Team number overriden by AOS_TEAM_NUMBER environment variable to "
80 << result;
81 return result;
Austin Schuhbe2456e2015-02-16 02:58:00 -080082 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080083 const auto hostname = GetHostname();
84 {
85 const auto result = team_number_internal::ParseRoborioTeamNumber(hostname);
86 if (result) {
87 LOG(INFO) << "roboRIO hostname team number is: " << *result;
88 return *result;
89 }
90 }
Brian Silverman5f06ed22020-02-17 21:49:42 -080091 {
92 const auto result = team_number_internal::ParsePiTeamNumber(hostname);
93 if (result) {
94 LOG(INFO) << "Pi hostname team number is: " << *result;
95 return *result;
96 }
97 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080098 LOG(FATAL) << "Failed to parse a team number from hostname: " << hostname;
Brian Silverman431500a2013-10-28 19:50:15 -070099}
100
101} // namespace
102
Austin Schuh288479d2019-12-18 19:47:52 -0800103::std::string GetHostname() {
104 char buf[256];
105 buf[sizeof(buf) - 1] = '\0';
Brian Silverman3dfbfb12020-02-17 20:35:18 -0800106 PCHECK(gethostname(buf, sizeof(buf) - 1) == 0);
Austin Schuh288479d2019-12-18 19:47:52 -0800107 return buf;
108}
109
Brian Silverman431500a2013-10-28 19:50:15 -0700110uint16_t GetTeamNumber() {
Brian Silverman3dfbfb12020-02-17 20:35:18 -0800111 const static uint16_t result = DoGetTeamNumber();
John Parka19d1a92019-11-06 18:28:14 -0800112 return result;
Brian Silverman431500a2013-10-28 19:50:15 -0700113}
114
Brian Silvermande0a53a2014-02-10 22:27:44 -0800115void OverrideTeamNumber(uint16_t team) { override_team = team; }
116
Brian Silverman431500a2013-10-28 19:50:15 -0700117} // namespace network
118} // namespace aos