blob: 1b900790424086048dd4249e137935ec890c5660 [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
8#include <string>
Brian Silverman431500a2013-10-28 19:50:15 -07009
Austin Schuh217a9782019-12-21 23:02:50 -080010#include "absl/base/call_once.h"
John Park33858a32018-09-28 23:05:48 -070011#include "aos/logging/logging.h"
12#include "aos/util/string_to_num.h"
Brian Silverman431500a2013-10-28 19:50:15 -070013
14namespace aos {
15namespace network {
Austin Schuhdf5591e2015-12-19 22:36:50 -080016namespace internal {
Austin Schuhbe2456e2015-02-16 02:58:00 -080017int ParseTeamNumber(const std::string &hostname, uint16_t *teamnumber) {
18 for (size_t i = 0; i < hostname.size(); i++) {
19 if (hostname[i] == '-') {
Austin Schuhdf5591e2015-12-19 22:36:50 -080020 const std::string num_as_s =
21 hostname[hostname.size() - 1] == 'C'
22 ? hostname.substr(i + 1, hostname.size() - 5 - i)
23 : hostname.substr(i + 1);
Austin Schuhbe2456e2015-02-16 02:58:00 -080024
25 int num;
26 if (!::aos::util::StringToNumber(num_as_s, &num)) {
27 return -1;
28 }
29 if (hostname.substr(0, i) == "roboRIO" &&
30 std::to_string(num) == num_as_s) {
31 *teamnumber = num;
32 return 0;
33 } else {
34 return -1;
35 }
36 }
37 }
38 return -1;
39}
Austin Schuhdf5591e2015-12-19 22:36:50 -080040} // namespace internal
41
42namespace {
43
44uint16_t override_team;
45
John Parka19d1a92019-11-06 18:28:14 -080046void DoGetTeamNumber(uint16_t *result) {
47 if (override_team != 0) {
48 *result = override_team;
49 return;
50 }
Brian Silverman8a6dac92015-02-21 20:08:24 -050051
52 const char *override_number = getenv("AOS_TEAM_NUMBER");
53 if (override_number != nullptr) {
John Parka19d1a92019-11-06 18:28:14 -080054 if (!::aos::util::StringToNumber(override_number, result)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070055 AOS_LOG(FATAL, "error parsing AOS_TEAM_NUMBER '%s'\n", override_number);
Brian Silverman8a6dac92015-02-21 20:08:24 -050056 }
Austin Schuhf257f3c2019-10-27 21:00:43 -070057 AOS_LOG(WARNING,
John Parka19d1a92019-11-06 18:28:14 -080058 "team number overridden by AOS_TEAM_NUMBER to %" PRIu16 "\n", *result);
Brian Silverman8a6dac92015-02-21 20:08:24 -050059 } else {
John Parka19d1a92019-11-06 18:28:14 -080060 int error = internal::ParseTeamNumber(GetHostname(), result);
Brian Silverman8a6dac92015-02-21 20:08:24 -050061 if (error) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070062 AOS_LOG(FATAL, "Invalid hostname %s\n", GetHostname().c_str());
Brian Silverman8a6dac92015-02-21 20:08:24 -050063 }
John Parka19d1a92019-11-06 18:28:14 -080064 AOS_LOG(INFO, "team number is %" PRIu16 "\n", *result);
Austin Schuhbe2456e2015-02-16 02:58:00 -080065 }
Brian Silverman431500a2013-10-28 19:50:15 -070066}
67
68} // namespace
69
Austin Schuh288479d2019-12-18 19:47:52 -080070::std::string GetHostname() {
71 char buf[256];
72 buf[sizeof(buf) - 1] = '\0';
73 AOS_PCHECK(gethostname(buf, sizeof(buf) - 1));
74 return buf;
75}
76
Brian Silverman431500a2013-10-28 19:50:15 -070077uint16_t GetTeamNumber() {
John Parka19d1a92019-11-06 18:28:14 -080078 static absl::once_flag once;
79 static uint16_t result;
80 absl::call_once(once, DoGetTeamNumber, &result);
81 return result;
Brian Silverman431500a2013-10-28 19:50:15 -070082}
83
Brian Silvermande0a53a2014-02-10 22:27:44 -080084void OverrideTeamNumber(uint16_t team) { override_team = team; }
85
Brian Silverman431500a2013-10-28 19:50:15 -070086} // namespace network
87} // namespace aos