blob: 134113a45c0ac368ddc358ac7477c6525c52e087 [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 {
17namespace {
18
Philipp Schraderf75a8bf2015-02-02 05:30:16 +000019uint16_t override_team;
Brian Silvermande0a53a2014-02-10 22:27:44 -080020
Austin Schuhbe2456e2015-02-16 02:58:00 -080021::std::string GetHostname() {
22 char buf[256];
23 buf[sizeof(buf) - 1] = '\0';
24 PCHECK(gethostname(buf, sizeof(buf) - 1));
25 return buf;
26}
27
28int ParseTeamNumber(const std::string &hostname, uint16_t *teamnumber) {
29 for (size_t i = 0; i < hostname.size(); i++) {
30 if (hostname[i] == '-') {
31 const std::string num_as_s = hostname.substr(i + 1);
32
33 int num;
34 if (!::aos::util::StringToNumber(num_as_s, &num)) {
35 return -1;
36 }
37 if (hostname.substr(0, i) == "roboRIO" &&
38 std::to_string(num) == num_as_s) {
39 *teamnumber = num;
40 return 0;
41 } else {
42 return -1;
43 }
44 }
45 }
46 return -1;
47}
48
Brian Silverman431500a2013-10-28 19:50:15 -070049uint16_t *DoGetTeamNumber() {
Brian Silvermande0a53a2014-02-10 22:27:44 -080050 if (override_team != 0) return &override_team;
Brian Silverman8a6dac92015-02-21 20:08:24 -050051
Austin Schuhbe2456e2015-02-16 02:58:00 -080052 static uint16_t r;
Brian Silverman8a6dac92015-02-21 20:08:24 -050053
54 const char *override_number = getenv("AOS_TEAM_NUMBER");
55 if (override_number != nullptr) {
56 if (!::aos::util::StringToNumber(override_number, &r)) {
57 LOG(FATAL, "error parsing AOS_TEAM_NUMBER '%s'\n", override_number);
58 }
59 LOG(WARNING, "team number overridden by AOS_TEAM_NUMBER to %" PRIu16 "\n",
60 r);
61 } else {
62 int error = ParseTeamNumber(GetHostname(), &r);
63 if (error) {
64 LOG(FATAL, "Invalid hostname %s\n", GetHostname().c_str());
65 }
66 LOG(INFO, "team number is %" PRIu16 "\n", r);
Austin Schuhbe2456e2015-02-16 02:58:00 -080067 }
Brian Silverman431500a2013-10-28 19:50:15 -070068 return &r;
69}
70
71} // namespace
72
73uint16_t GetTeamNumber() {
74 static Once<uint16_t> once(DoGetTeamNumber);
75 return *once.Get();
76}
77
Brian Silvermande0a53a2014-02-10 22:27:44 -080078void OverrideTeamNumber(uint16_t team) { override_team = team; }
79
Brian Silverman431500a2013-10-28 19:50:15 -070080} // namespace network
81} // namespace aos