Add newer DriverStation methods to ahal

Change-Id: I30c5bf4ccbf72cb135dedc08ef7ee23e101c90fc
Signed-off-by: Lee Mracek <lee@valkyrierobotics.com>
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/frc971/input/joystick_state.fbs b/frc971/input/joystick_state.fbs
index 2f48bdf..49d2690 100644
--- a/frc971/input/joystick_state.fbs
+++ b/frc971/input/joystick_state.fbs
@@ -1,5 +1,7 @@
 namespace aos;
 
+enum MatchType : byte { kNone, kPractice, kQualification, kElimination }
+
 table Joystick {
   // A bitmask of the butotn state.
   buttons:ushort (id: 0);
@@ -44,6 +46,14 @@
 
   // String corresponding to the game data string
   game_data:string (id: 10);
+
+  // Driver station location.
+  location:ubyte (id: 11);
+
+  match_number:int (id: 12);
+  replay_number:int (id: 13);
+  match_type:MatchType (id: 14);
+  event_name:string (id: 15);
 }
 
 root_type JoystickState;
diff --git a/frc971/wpilib/ahal/DriverStation.cc b/frc971/wpilib/ahal/DriverStation.cc
index 2ed0a97..8b31af7 100644
--- a/frc971/wpilib/ahal/DriverStation.cc
+++ b/frc971/wpilib/ahal/DriverStation.cc
@@ -272,6 +272,54 @@
 }
 
 /**
+  * Returns the game specific message provided by the FMS.
+  *
+  * @return A string containing the game specific message.
+  */
+std::string_view DriverStation::GetGameSpecificMessage() const {
+  return std::string_view(
+      reinterpret_cast<const char *>(info_.gameSpecificMessage),
+      info_.gameSpecificMessageSize);
+}
+
+/**
+  * Returns the name of the competition event provided by the FMS.
+  *
+  * @return A string containing the event name
+  */
+std::string_view DriverStation::GetEventName() const {
+  return info_.eventName;
+}
+
+/**
+  * Returns the match number provided by the FMS.
+  *
+  * @return The number of the match
+  */
+DriverStation::MatchType DriverStation::GetMatchType() const {
+  return static_cast<DriverStation::MatchType>(info_.matchType);
+}
+
+/**
+  * Returns the match number provided by the FMS.
+  *
+  * @return The number of the match
+  */
+int DriverStation::GetMatchNumber() const {
+  return info_.matchNumber;
+}
+
+/**
+  * Returns the number of times the current match has been replayed from the
+  * FMS.
+  *
+  * @return The number of replays
+  */
+int DriverStation::GetReplayNumber() const {
+  return info_.replayNumber;
+}
+
+/**
  * Return the alliance that the driver station says it is on.
  *
  * This could return kRed or kBlue.
@@ -374,10 +422,14 @@
 
   HAL_ControlWord control_word;
   HAL_GetControlWord(&control_word);
-  is_enabled_ = control_word.enabled;
+  is_enabled_ = control_word.enabled && control_word.dsAttached;
   is_autonomous_ = control_word.autonomous;
   is_test_mode_ = control_word.test;
   is_fms_attached_ = control_word.fmsAttached;
+  is_ds_attached_ = control_word.dsAttached;
+  is_teleop_ = !(control_word.autonomous || control_word.test);
+
+  HAL_GetMatchInfo(&info_);
 }
 
 /**
diff --git a/frc971/wpilib/ahal/DriverStation.h b/frc971/wpilib/ahal/DriverStation.h
index 5150360..8bcdcf1 100644
--- a/frc971/wpilib/ahal/DriverStation.h
+++ b/frc971/wpilib/ahal/DriverStation.h
@@ -27,6 +27,7 @@
 class DriverStation {
  public:
   enum Alliance { kRed, kBlue, kInvalid };
+  enum MatchType { kNone, kPractice, kQualification, kElimination };
 
   virtual ~DriverStation();
   static DriverStation &GetInstance();
@@ -55,10 +56,18 @@
   bool IsTestMode() const { return is_test_mode_; }
   bool IsFmsAttached() const { return is_fms_attached_; }
   bool IsAutonomous() const { return is_autonomous_; }
+  bool IsTeleop() const { return is_teleop_; }
+  bool IsDSAttached() const { return is_ds_attached_; }
 
   bool IsSysActive() const;
   bool IsBrownedOut() const;
 
+  std::string_view GetGameSpecificMessage() const;
+
+  std::string_view GetEventName() const;
+  MatchType GetMatchType() const;
+  int GetMatchNumber() const;
+  int GetReplayNumber() const;
   Alliance GetAlliance() const;
   int GetLocation() const;
   double GetMatchTime() const;
@@ -88,6 +97,11 @@
   bool is_test_mode_ = false;
   bool is_autonomous_ = false;
   bool is_fms_attached_ = false;
+  bool is_teleop_ = false;
+  bool is_ds_attached_ = false;
+
+  // statically allocated match info so we can return string_views into it.
+  HAL_MatchInfo info_;
 };
 
 }  // namespace frc
diff --git a/frc971/wpilib/joystick_sender.cc b/frc971/wpilib/joystick_sender.cc
index fab3d05..9137a99 100644
--- a/frc971/wpilib/joystick_sender.cc
+++ b/frc971/wpilib/joystick_sender.cc
@@ -30,9 +30,6 @@
       ds->RunIteration([&]() {
         auto builder = joystick_state_sender_.MakeBuilder();
 
-        HAL_MatchInfo match_info;
-        auto status = HAL_GetMatchInfo(&match_info);
-
         std::array<flatbuffers::Offset<Joystick>,
                    frc971::input::driver_station::JoystickFeature::kJoysticks>
             joysticks;
@@ -67,32 +64,26 @@
             joysticks_offset = builder.fbb()->CreateVector(joysticks.begin(),
                                                            joysticks.size());
 
-        flatbuffers::Offset<flatbuffers::String> game_data_offset;
-        if (status == 0) {
-          static_assert(sizeof(match_info.gameSpecificMessage) == 64,
-                        "Check that the match info game specific message size "
-                        "hasn't changed and is still sane.");
-          CHECK_LE(match_info.gameSpecificMessageSize,
-                   sizeof(match_info.gameSpecificMessage));
-          game_data_offset = builder.fbb()->CreateString(
-              reinterpret_cast<const char *>(match_info.gameSpecificMessage),
-              match_info.gameSpecificMessageSize);
-        }
+        flatbuffers::Offset<flatbuffers::String> game_data_offset =
+            builder.fbb()->CreateString(ds->GetGameSpecificMessage());
+
+        flatbuffers::Offset<flatbuffers::String> event_name_offset =
+            builder.fbb()->CreateString(ds->GetEventName());
 
         aos::JoystickState::Builder joystick_state_builder =
             builder.MakeBuilder<aos::JoystickState>();
 
         joystick_state_builder.add_joysticks(joysticks_offset);
 
-        if (status == 0) {
+        if (ds->GetGameSpecificMessage().size() >= 2u) {
           joystick_state_builder.add_switch_left(
-              match_info.gameSpecificMessage[0] == 'L' ||
-              match_info.gameSpecificMessage[0] == 'l');
+              ds->GetGameSpecificMessage()[0] == 'L' ||
+              ds->GetGameSpecificMessage()[0] == 'l');
           joystick_state_builder.add_scale_left(
-              match_info.gameSpecificMessage[1] == 'L' ||
-              match_info.gameSpecificMessage[1] == 'l');
-          joystick_state_builder.add_game_data(game_data_offset);
+              ds->GetGameSpecificMessage()[1] == 'L' ||
+              ds->GetGameSpecificMessage()[1] == 'l');
         }
+        joystick_state_builder.add_game_data(game_data_offset);
 
         joystick_state_builder.add_test_mode(ds->IsTestMode());
         joystick_state_builder.add_fms_attached(ds->IsFmsAttached());
@@ -109,7 +100,28 @@
             joystick_state_builder.add_alliance(aos::Alliance::kInvalid);
             break;
         }
+        joystick_state_builder.add_location(ds->GetLocation());
+
         joystick_state_builder.add_team_id(team_id_);
+        joystick_state_builder.add_match_number(ds->GetMatchNumber());
+        joystick_state_builder.add_replay_number(ds->GetReplayNumber());
+
+        switch (ds->GetMatchType()) {
+          case frc::DriverStation::kNone:
+            joystick_state_builder.add_match_type(aos::MatchType::kNone);
+            break;
+          case frc::DriverStation::kPractice:
+            joystick_state_builder.add_match_type(aos::MatchType::kPractice);
+            break;
+          case frc::DriverStation::kQualification:
+            joystick_state_builder.add_match_type(
+                aos::MatchType::kQualification);
+            break;
+          case frc::DriverStation::kElimination:
+            joystick_state_builder.add_match_type(aos::MatchType::kElimination);
+            break;
+        }
+        joystick_state_builder.add_event_name(event_name_offset);
 
         if (builder.Send(joystick_state_builder.Finish()) !=
             aos::RawSender::Error::kOk) {