blob: b03129903604fb2e682d527d9175d7048897ad27 [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
38} // namespace team_number_internal
Austin Schuhdf5591e2015-12-19 22:36:50 -080039
40namespace {
41
42uint16_t override_team;
43
Brian Silverman3dfbfb12020-02-17 20:35:18 -080044uint16_t DoGetTeamNumber() {
John Parka19d1a92019-11-06 18:28:14 -080045 if (override_team != 0) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080046 return override_team;
John Parka19d1a92019-11-06 18:28:14 -080047 }
Brian Silverman8a6dac92015-02-21 20:08:24 -050048
49 const char *override_number = getenv("AOS_TEAM_NUMBER");
50 if (override_number != nullptr) {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080051 uint16_t result;
52 if (!::aos::util::StringToNumber(override_number, &result)) {
53 LOG(FATAL) << "Error parsing AOS_TEAM_NUMBER: " << override_number;
Brian Silverman8a6dac92015-02-21 20:08:24 -050054 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080055 LOG(WARNING)
56 << "Team number overriden by AOS_TEAM_NUMBER environment variable to "
57 << result;
58 return result;
Austin Schuhbe2456e2015-02-16 02:58:00 -080059 }
Brian Silverman3dfbfb12020-02-17 20:35:18 -080060 const auto hostname = GetHostname();
61 {
62 const auto result = team_number_internal::ParseRoborioTeamNumber(hostname);
63 if (result) {
64 LOG(INFO) << "roboRIO hostname team number is: " << *result;
65 return *result;
66 }
67 }
68 LOG(FATAL) << "Failed to parse a team number from hostname: " << hostname;
Brian Silverman431500a2013-10-28 19:50:15 -070069}
70
71} // namespace
72
Austin Schuh288479d2019-12-18 19:47:52 -080073::std::string GetHostname() {
74 char buf[256];
75 buf[sizeof(buf) - 1] = '\0';
Brian Silverman3dfbfb12020-02-17 20:35:18 -080076 PCHECK(gethostname(buf, sizeof(buf) - 1) == 0);
Austin Schuh288479d2019-12-18 19:47:52 -080077 return buf;
78}
79
Brian Silverman431500a2013-10-28 19:50:15 -070080uint16_t GetTeamNumber() {
Brian Silverman3dfbfb12020-02-17 20:35:18 -080081 const static uint16_t result = DoGetTeamNumber();
John Parka19d1a92019-11-06 18:28:14 -080082 return result;
Brian Silverman431500a2013-10-28 19:50:15 -070083}
84
Brian Silvermande0a53a2014-02-10 22:27:44 -080085void OverrideTeamNumber(uint16_t team) { override_team = team; }
86
Brian Silverman431500a2013-10-28 19:50:15 -070087} // namespace network
88} // namespace aos