blob: 4aa762639bcf3c5f1f9108700c0320a6e9b057b3 [file] [log] [blame]
Brian Silverman431500a2013-10-28 19:50:15 -07001#include "aos/common/network/team_number.h"
2
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
10#include "aos/common/once.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -080011#include "aos/linux_code/configuration.h"
Brian Silverman8f8b06f2013-10-30 22:04:27 -070012#include "aos/common/logging/logging.h"
Austin Schuhbe2456e2015-02-16 02:58:00 -080013#include "aos/common/util/string_to_num.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
47::std::string GetHostname() {
48 char buf[256];
49 buf[sizeof(buf) - 1] = '\0';
50 PCHECK(gethostname(buf, sizeof(buf) - 1));
51 return buf;
52}
Austin Schuhbe2456e2015-02-16 02:58:00 -080053
Brian Silverman431500a2013-10-28 19:50:15 -070054uint16_t *DoGetTeamNumber() {
Brian Silvermande0a53a2014-02-10 22:27:44 -080055 if (override_team != 0) return &override_team;
Brian Silverman8a6dac92015-02-21 20:08:24 -050056
Austin Schuhbe2456e2015-02-16 02:58:00 -080057 static uint16_t r;
Brian Silverman8a6dac92015-02-21 20:08:24 -050058
59 const char *override_number = getenv("AOS_TEAM_NUMBER");
60 if (override_number != nullptr) {
61 if (!::aos::util::StringToNumber(override_number, &r)) {
62 LOG(FATAL, "error parsing AOS_TEAM_NUMBER '%s'\n", override_number);
63 }
64 LOG(WARNING, "team number overridden by AOS_TEAM_NUMBER to %" PRIu16 "\n",
65 r);
66 } else {
Austin Schuhdf5591e2015-12-19 22:36:50 -080067 int error = internal::ParseTeamNumber(GetHostname(), &r);
Brian Silverman8a6dac92015-02-21 20:08:24 -050068 if (error) {
69 LOG(FATAL, "Invalid hostname %s\n", GetHostname().c_str());
70 }
71 LOG(INFO, "team number is %" PRIu16 "\n", r);
Austin Schuhbe2456e2015-02-16 02:58:00 -080072 }
Brian Silverman431500a2013-10-28 19:50:15 -070073 return &r;
74}
75
76} // namespace
77
78uint16_t GetTeamNumber() {
79 static Once<uint16_t> once(DoGetTeamNumber);
80 return *once.Get();
81}
82
Brian Silvermande0a53a2014-02-10 22:27:44 -080083void OverrideTeamNumber(uint16_t team) { override_team = team; }
84
Brian Silverman431500a2013-10-28 19:50:15 -070085} // namespace network
86} // namespace aos