John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/network/team_number.h" |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 2 | |
| 3 | #include <netinet/in.h> |
Brian Silverman | 8f8b06f | 2013-10-30 22:04:27 -0700 | [diff] [blame] | 4 | #include <inttypes.h> |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 5 | #include <unistd.h> |
Brian Silverman | 8a6dac9 | 2015-02-21 20:08:24 -0500 | [diff] [blame] | 6 | #include <stdlib.h> |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 7 | |
| 8 | #include <string> |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 9 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 10 | #include "aos/logging/logging.h" |
| 11 | #include "aos/util/string_to_num.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 12 | #include "aos/configuration.h" |
John Park | a19d1a9 | 2019-11-06 18:28:14 -0800 | [diff] [blame] | 13 | #include "absl/base/call_once.h" |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 14 | |
| 15 | namespace aos { |
| 16 | namespace network { |
Austin Schuh | df5591e | 2015-12-19 22:36:50 -0800 | [diff] [blame] | 17 | namespace internal { |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 18 | int ParseTeamNumber(const std::string &hostname, uint16_t *teamnumber) { |
| 19 | for (size_t i = 0; i < hostname.size(); i++) { |
| 20 | if (hostname[i] == '-') { |
Austin Schuh | df5591e | 2015-12-19 22:36:50 -0800 | [diff] [blame] | 21 | 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 Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 25 | |
| 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 Schuh | df5591e | 2015-12-19 22:36:50 -0800 | [diff] [blame] | 41 | } // namespace internal |
| 42 | |
| 43 | namespace { |
| 44 | |
| 45 | uint16_t override_team; |
| 46 | |
John Park | a19d1a9 | 2019-11-06 18:28:14 -0800 | [diff] [blame] | 47 | void DoGetTeamNumber(uint16_t *result) { |
| 48 | if (override_team != 0) { |
| 49 | *result = override_team; |
| 50 | return; |
| 51 | } |
Brian Silverman | 8a6dac9 | 2015-02-21 20:08:24 -0500 | [diff] [blame] | 52 | |
| 53 | const char *override_number = getenv("AOS_TEAM_NUMBER"); |
| 54 | if (override_number != nullptr) { |
John Park | a19d1a9 | 2019-11-06 18:28:14 -0800 | [diff] [blame] | 55 | if (!::aos::util::StringToNumber(override_number, result)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 56 | AOS_LOG(FATAL, "error parsing AOS_TEAM_NUMBER '%s'\n", override_number); |
Brian Silverman | 8a6dac9 | 2015-02-21 20:08:24 -0500 | [diff] [blame] | 57 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 58 | AOS_LOG(WARNING, |
John Park | a19d1a9 | 2019-11-06 18:28:14 -0800 | [diff] [blame] | 59 | "team number overridden by AOS_TEAM_NUMBER to %" PRIu16 "\n", *result); |
Brian Silverman | 8a6dac9 | 2015-02-21 20:08:24 -0500 | [diff] [blame] | 60 | } else { |
John Park | a19d1a9 | 2019-11-06 18:28:14 -0800 | [diff] [blame] | 61 | int error = internal::ParseTeamNumber(GetHostname(), result); |
Brian Silverman | 8a6dac9 | 2015-02-21 20:08:24 -0500 | [diff] [blame] | 62 | if (error) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 63 | AOS_LOG(FATAL, "Invalid hostname %s\n", GetHostname().c_str()); |
Brian Silverman | 8a6dac9 | 2015-02-21 20:08:24 -0500 | [diff] [blame] | 64 | } |
John Park | a19d1a9 | 2019-11-06 18:28:14 -0800 | [diff] [blame] | 65 | AOS_LOG(INFO, "team number is %" PRIu16 "\n", *result); |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 66 | } |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | } // namespace |
| 70 | |
Austin Schuh | 288479d | 2019-12-18 19:47:52 -0800 | [diff] [blame^] | 71 | ::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 Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 78 | uint16_t GetTeamNumber() { |
John Park | a19d1a9 | 2019-11-06 18:28:14 -0800 | [diff] [blame] | 79 | static absl::once_flag once; |
| 80 | static uint16_t result; |
| 81 | absl::call_once(once, DoGetTeamNumber, &result); |
| 82 | return result; |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Brian Silverman | de0a53a | 2014-02-10 22:27:44 -0800 | [diff] [blame] | 85 | void OverrideTeamNumber(uint16_t team) { override_team = team; } |
| 86 | |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 87 | } // namespace network |
| 88 | } // namespace aos |