blob: a75cd4e84d731d89c145fb5e32bd9a8441b1c89d [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
John Park33858a32018-09-28 23:05:48 -070010#include "aos/logging/logging.h"
11#include "aos/util/string_to_num.h"
John Park398c74a2018-10-20 21:17:39 -070012#include "aos/configuration.h"
John Parka19d1a92019-11-06 18:28:14 -080013#include "absl/base/call_once.h"
Brian Silverman431500a2013-10-28 19:50:15 -070014
15namespace aos {
16namespace network {
Austin Schuhdf5591e2015-12-19 22:36:50 -080017namespace internal {
Austin Schuhbe2456e2015-02-16 02:58:00 -080018int ParseTeamNumber(const std::string &hostname, uint16_t *teamnumber) {
19 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)) {
28 return -1;
29 }
30 if (hostname.substr(0, i) == "roboRIO" &&
31 std::to_string(num) == num_as_s) {
32 *teamnumber = num;
33 return 0;
34 } else {
35 return -1;
36 }
37 }
38 }
39 return -1;
40}
Austin Schuhdf5591e2015-12-19 22:36:50 -080041} // namespace internal
42
43namespace {
44
45uint16_t override_team;
46
John Parka19d1a92019-11-06 18:28:14 -080047void DoGetTeamNumber(uint16_t *result) {
48 if (override_team != 0) {
49 *result = override_team;
50 return;
51 }
Brian Silverman8a6dac92015-02-21 20:08:24 -050052
53 const char *override_number = getenv("AOS_TEAM_NUMBER");
54 if (override_number != nullptr) {
John Parka19d1a92019-11-06 18:28:14 -080055 if (!::aos::util::StringToNumber(override_number, result)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070056 AOS_LOG(FATAL, "error parsing AOS_TEAM_NUMBER '%s'\n", override_number);
Brian Silverman8a6dac92015-02-21 20:08:24 -050057 }
Austin Schuhf257f3c2019-10-27 21:00:43 -070058 AOS_LOG(WARNING,
John Parka19d1a92019-11-06 18:28:14 -080059 "team number overridden by AOS_TEAM_NUMBER to %" PRIu16 "\n", *result);
Brian Silverman8a6dac92015-02-21 20:08:24 -050060 } else {
John Parka19d1a92019-11-06 18:28:14 -080061 int error = internal::ParseTeamNumber(GetHostname(), result);
Brian Silverman8a6dac92015-02-21 20:08:24 -050062 if (error) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070063 AOS_LOG(FATAL, "Invalid hostname %s\n", GetHostname().c_str());
Brian Silverman8a6dac92015-02-21 20:08:24 -050064 }
John Parka19d1a92019-11-06 18:28:14 -080065 AOS_LOG(INFO, "team number is %" PRIu16 "\n", *result);
Austin Schuhbe2456e2015-02-16 02:58:00 -080066 }
Brian Silverman431500a2013-10-28 19:50:15 -070067}
68
69} // namespace
70
Austin Schuh288479d2019-12-18 19:47:52 -080071::std::string GetHostname() {
72 char buf[256];
73 buf[sizeof(buf) - 1] = '\0';
74 AOS_PCHECK(gethostname(buf, sizeof(buf) - 1));
75 return buf;
76}
77
Brian Silverman431500a2013-10-28 19:50:15 -070078uint16_t GetTeamNumber() {
John Parka19d1a92019-11-06 18:28:14 -080079 static absl::once_flag once;
80 static uint16_t result;
81 absl::call_once(once, DoGetTeamNumber, &result);
82 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