Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include <atomic> |
| 6 | #include <chrono> |
| 7 | #include <cstdlib> |
| 8 | #include <cstring> |
| 9 | #include <limits> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <string_view> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 12 | |
| 13 | #include <FRC_NetworkCommunication/FRCComm.h> |
| 14 | #include <FRC_NetworkCommunication/NetCommRPCProxy_Occur.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 15 | #include <fmt/format.h> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 16 | #include <wpi/EventVector.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | #include <wpi/SafeThread.h> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 18 | #include <wpi/SmallVector.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 19 | #include <wpi/condition_variable.h> |
| 20 | #include <wpi/mutex.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 21 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 22 | #include "HALInitializer.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 23 | #include "hal/DriverStation.h" |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 24 | #include "hal/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 25 | |
| 26 | static_assert(sizeof(int32_t) >= sizeof(int), |
| 27 | "FRC_NetworkComm status variable is larger than 32 bits"); |
| 28 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 29 | namespace { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 30 | struct HAL_JoystickAxesInt { |
| 31 | int16_t count; |
| 32 | int16_t axes[HAL_kMaxJoystickAxes]; |
| 33 | }; |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 34 | } // namespace |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 35 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 36 | namespace { |
| 37 | struct JoystickDataCache { |
| 38 | JoystickDataCache() { std::memset(this, 0, sizeof(*this)); } |
| 39 | void Update(); |
| 40 | |
| 41 | HAL_JoystickAxes axes[HAL_kMaxJoysticks]; |
| 42 | HAL_JoystickPOVs povs[HAL_kMaxJoysticks]; |
| 43 | HAL_JoystickButtons buttons[HAL_kMaxJoysticks]; |
| 44 | HAL_AllianceStationID allianceStation; |
| 45 | float matchTime; |
| 46 | }; |
| 47 | static_assert(std::is_standard_layout_v<JoystickDataCache>); |
| 48 | // static_assert(std::is_trivial_v<JoystickDataCache>); |
| 49 | |
| 50 | struct FRCDriverStation { |
| 51 | wpi::EventVector newDataEvents; |
| 52 | }; |
| 53 | } // namespace |
| 54 | |
| 55 | static ::FRCDriverStation* driverStation; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 56 | |
| 57 | // Message and Data variables |
| 58 | static wpi::mutex msgMutex; |
| 59 | |
| 60 | static int32_t HAL_GetJoystickAxesInternal(int32_t joystickNum, |
| 61 | HAL_JoystickAxes* axes) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 62 | HAL_JoystickAxesInt netcommAxes; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 63 | |
| 64 | int retVal = FRC_NetworkCommunication_getJoystickAxes( |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 65 | joystickNum, reinterpret_cast<JoystickAxes_t*>(&netcommAxes), |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 66 | HAL_kMaxJoystickAxes); |
| 67 | |
| 68 | // copy integer values to double values |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 69 | axes->count = netcommAxes.count; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 70 | // current scaling is -128 to 127, can easily be patched in the future by |
| 71 | // changing this function. |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 72 | for (int32_t i = 0; i < netcommAxes.count; i++) { |
| 73 | int8_t value = netcommAxes.axes[i]; |
| 74 | axes->raw[i] = value; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 75 | if (value < 0) { |
| 76 | axes->axes[i] = value / 128.0; |
| 77 | } else { |
| 78 | axes->axes[i] = value / 127.0; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return retVal; |
| 83 | } |
| 84 | |
| 85 | static int32_t HAL_GetJoystickPOVsInternal(int32_t joystickNum, |
| 86 | HAL_JoystickPOVs* povs) { |
| 87 | return FRC_NetworkCommunication_getJoystickPOVs( |
| 88 | joystickNum, reinterpret_cast<JoystickPOV_t*>(povs), |
| 89 | HAL_kMaxJoystickPOVs); |
| 90 | } |
| 91 | |
| 92 | static int32_t HAL_GetJoystickButtonsInternal(int32_t joystickNum, |
| 93 | HAL_JoystickButtons* buttons) { |
| 94 | return FRC_NetworkCommunication_getJoystickButtons( |
| 95 | joystickNum, &buttons->buttons, &buttons->count); |
| 96 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 97 | |
| 98 | void JoystickDataCache::Update() { |
| 99 | for (int i = 0; i < HAL_kMaxJoysticks; i++) { |
| 100 | HAL_GetJoystickAxesInternal(i, &axes[i]); |
| 101 | HAL_GetJoystickPOVsInternal(i, &povs[i]); |
| 102 | HAL_GetJoystickButtonsInternal(i, &buttons[i]); |
| 103 | } |
| 104 | FRC_NetworkCommunication_getAllianceStation( |
| 105 | reinterpret_cast<AllianceStationID_t*>(&allianceStation)); |
| 106 | FRC_NetworkCommunication_getMatchTime(&matchTime); |
| 107 | } |
| 108 | |
| 109 | #define CHECK_JOYSTICK_NUMBER(stickNum) \ |
| 110 | if ((stickNum) < 0 || (stickNum) >= HAL_kMaxJoysticks) \ |
| 111 | return PARAMETER_OUT_OF_RANGE |
| 112 | |
| 113 | static HAL_ControlWord newestControlWord; |
| 114 | static JoystickDataCache caches[3]; |
| 115 | static JoystickDataCache* currentRead = &caches[0]; |
| 116 | static JoystickDataCache* currentReadLocal = &caches[0]; |
| 117 | static std::atomic<JoystickDataCache*> currentCache{&caches[1]}; |
| 118 | static JoystickDataCache* lastGiven = &caches[1]; |
| 119 | static JoystickDataCache* cacheToUpdate = &caches[2]; |
| 120 | |
| 121 | static wpi::mutex cacheMutex; |
| 122 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 123 | /** |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 124 | * Retrieve the Joystick Descriptor for particular slot. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 125 | * |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 126 | * @param[out] desc descriptor (data transfer object) to fill in. desc is filled |
| 127 | * in regardless of success. In other words, if descriptor is |
| 128 | * not available, desc is filled in with default values |
| 129 | * matching the init-values in Java and C++ Driverstation for |
| 130 | * when caller requests a too-large joystick index. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 131 | * @return error code reported from Network Comm back-end. Zero is good, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 132 | * nonzero is bad. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 133 | */ |
| 134 | static int32_t HAL_GetJoystickDescriptorInternal(int32_t joystickNum, |
| 135 | HAL_JoystickDescriptor* desc) { |
| 136 | desc->isXbox = 0; |
| 137 | desc->type = (std::numeric_limits<uint8_t>::max)(); |
| 138 | desc->name[0] = '\0'; |
| 139 | desc->axisCount = |
| 140 | HAL_kMaxJoystickAxes; /* set to the desc->axisTypes's capacity */ |
| 141 | desc->buttonCount = 0; |
| 142 | desc->povCount = 0; |
| 143 | int retval = FRC_NetworkCommunication_getJoystickDesc( |
| 144 | joystickNum, &desc->isXbox, &desc->type, |
| 145 | reinterpret_cast<char*>(&desc->name), &desc->axisCount, |
| 146 | reinterpret_cast<uint8_t*>(&desc->axisTypes), &desc->buttonCount, |
| 147 | &desc->povCount); |
| 148 | /* check the return, if there is an error and the RIOimage predates FRC2017, |
| 149 | * then axisCount needs to be cleared */ |
| 150 | if (retval != 0) { |
| 151 | /* set count to zero so downstream code doesn't decode invalid axisTypes. */ |
| 152 | desc->axisCount = 0; |
| 153 | } |
| 154 | return retval; |
| 155 | } |
| 156 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 157 | static int32_t HAL_GetMatchInfoInternal(HAL_MatchInfo* info) { |
| 158 | MatchType_t matchType = MatchType_t::kMatchType_none; |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 159 | info->gameSpecificMessageSize = sizeof(info->gameSpecificMessage); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 160 | int status = FRC_NetworkCommunication_getMatchInfo( |
| 161 | info->eventName, &matchType, &info->matchNumber, &info->replayNumber, |
| 162 | info->gameSpecificMessage, &info->gameSpecificMessageSize); |
| 163 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 164 | if (info->gameSpecificMessageSize > sizeof(info->gameSpecificMessage)) { |
| 165 | info->gameSpecificMessageSize = 0; |
| 166 | } |
| 167 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 168 | info->matchType = static_cast<HAL_MatchType>(matchType); |
| 169 | |
| 170 | *(std::end(info->eventName) - 1) = '\0'; |
| 171 | |
| 172 | return status; |
| 173 | } |
| 174 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 175 | namespace { |
| 176 | struct TcpCache { |
| 177 | TcpCache() { std::memset(this, 0, sizeof(*this)); } |
| 178 | void Update(uint32_t mask); |
| 179 | void CloneTo(TcpCache* other) { std::memcpy(other, this, sizeof(*this)); } |
| 180 | |
| 181 | HAL_MatchInfo matchInfo; |
| 182 | HAL_JoystickDescriptor descriptors[HAL_kMaxJoysticks]; |
| 183 | }; |
| 184 | static_assert(std::is_standard_layout_v<TcpCache>); |
| 185 | } // namespace |
| 186 | |
| 187 | static std::atomic_uint32_t tcpMask{0xFFFFFFFF}; |
| 188 | static TcpCache tcpCache; |
| 189 | static TcpCache tcpCurrent; |
| 190 | static wpi::mutex tcpCacheMutex; |
| 191 | |
| 192 | constexpr uint32_t combinedMatchInfoMask = kTcpRecvMask_MatchInfoOld | |
| 193 | kTcpRecvMask_MatchInfo | |
| 194 | kTcpRecvMask_GameSpecific; |
| 195 | |
| 196 | void TcpCache::Update(uint32_t mask) { |
| 197 | if ((mask & combinedMatchInfoMask) != 0) { |
| 198 | HAL_GetMatchInfoInternal(&matchInfo); |
| 199 | } |
| 200 | for (int i = 0; i < HAL_kMaxJoysticks; i++) { |
| 201 | if ((mask & (1 << i)) != 0) { |
| 202 | HAL_GetJoystickDescriptorInternal(i, &descriptors[i]); |
| 203 | } |
| 204 | } |
| 205 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 206 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 207 | namespace hal::init { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 208 | void InitializeFRCDriverStation() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 209 | std::memset(&newestControlWord, 0, sizeof(newestControlWord)); |
| 210 | static FRCDriverStation ds; |
| 211 | driverStation = &ds; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 212 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 213 | } // namespace hal::init |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 214 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 215 | namespace hal { |
| 216 | static void DefaultPrintErrorImpl(const char* line, size_t size) { |
| 217 | std::fwrite(line, size, 1, stderr); |
| 218 | } |
| 219 | } // namespace hal |
| 220 | |
| 221 | static std::atomic<void (*)(const char* line, size_t size)> gPrintErrorImpl{ |
| 222 | hal::DefaultPrintErrorImpl}; |
| 223 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 224 | extern "C" { |
| 225 | |
| 226 | int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode, |
| 227 | const char* details, const char* location, |
| 228 | const char* callStack, HAL_Bool printMsg) { |
| 229 | // Avoid flooding console by keeping track of previous 5 error |
| 230 | // messages and only printing again if they're longer than 1 second old. |
| 231 | static constexpr int KEEP_MSGS = 5; |
| 232 | std::scoped_lock lock(msgMutex); |
| 233 | static std::string prevMsg[KEEP_MSGS]; |
| 234 | static std::chrono::time_point<std::chrono::steady_clock> |
| 235 | prevMsgTime[KEEP_MSGS]; |
| 236 | static bool initialized = false; |
| 237 | if (!initialized) { |
| 238 | for (int i = 0; i < KEEP_MSGS; i++) { |
| 239 | prevMsgTime[i] = |
| 240 | std::chrono::steady_clock::now() - std::chrono::seconds(2); |
| 241 | } |
| 242 | initialized = true; |
| 243 | } |
| 244 | |
| 245 | auto curTime = std::chrono::steady_clock::now(); |
| 246 | int i; |
| 247 | for (i = 0; i < KEEP_MSGS; ++i) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 248 | if (prevMsg[i] == details) { |
| 249 | break; |
| 250 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 251 | } |
| 252 | int retval = 0; |
| 253 | if (i == KEEP_MSGS || (curTime - prevMsgTime[i]) >= std::chrono::seconds(1)) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 254 | std::string_view detailsRef{details}; |
| 255 | std::string_view locationRef{location}; |
| 256 | std::string_view callStackRef{callStack}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 257 | |
| 258 | // 1 tag, 4 timestamp, 2 seqnum |
| 259 | // 2 numOccur, 4 error code, 1 flags, 6 strlen |
| 260 | // 1 extra needed for padding on Netcomm end. |
| 261 | size_t baseLength = 21; |
| 262 | |
| 263 | if (baseLength + detailsRef.size() + locationRef.size() + |
| 264 | callStackRef.size() <= |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 265 | 65535) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 266 | // Pass through |
| 267 | retval = FRC_NetworkCommunication_sendError(isError, errorCode, isLVCode, |
| 268 | details, location, callStack); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 269 | } else if (baseLength + detailsRef.size() > 65535) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 270 | // Details too long, cut both location and stack |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 271 | auto newLen = 65535 - baseLength; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 272 | std::string newDetails{details, newLen}; |
| 273 | char empty = '\0'; |
| 274 | retval = FRC_NetworkCommunication_sendError( |
| 275 | isError, errorCode, isLVCode, newDetails.c_str(), &empty, &empty); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 276 | } else if (baseLength + detailsRef.size() + locationRef.size() > 65535) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 277 | // Location too long, cut stack |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 278 | auto newLen = 65535 - baseLength - detailsRef.size(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 279 | std::string newLocation{location, newLen}; |
| 280 | char empty = '\0'; |
| 281 | retval = FRC_NetworkCommunication_sendError( |
| 282 | isError, errorCode, isLVCode, details, newLocation.c_str(), &empty); |
| 283 | } else { |
| 284 | // Stack too long |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 285 | auto newLen = 65535 - baseLength - detailsRef.size() - locationRef.size(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 286 | std::string newCallStack{callStack, newLen}; |
| 287 | retval = FRC_NetworkCommunication_sendError(isError, errorCode, isLVCode, |
| 288 | details, location, |
| 289 | newCallStack.c_str()); |
| 290 | } |
| 291 | if (printMsg) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 292 | fmt::memory_buffer buf; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 293 | if (location && location[0] != '\0') { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 294 | fmt::format_to(fmt::appender{buf}, |
| 295 | "{} at {}: ", isError ? "Error" : "Warning", location); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 296 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 297 | fmt::format_to(fmt::appender{buf}, "{}\n", details); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 298 | if (callStack && callStack[0] != '\0') { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 299 | fmt::format_to(fmt::appender{buf}, "{}\n", callStack); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 300 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 301 | auto printError = gPrintErrorImpl.load(); |
| 302 | printError(buf.data(), buf.size()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 303 | } |
| 304 | if (i == KEEP_MSGS) { |
| 305 | // replace the oldest one |
| 306 | i = 0; |
| 307 | auto first = prevMsgTime[0]; |
| 308 | for (int j = 1; j < KEEP_MSGS; ++j) { |
| 309 | if (prevMsgTime[j] < first) { |
| 310 | first = prevMsgTime[j]; |
| 311 | i = j; |
| 312 | } |
| 313 | } |
| 314 | prevMsg[i] = details; |
| 315 | } |
| 316 | prevMsgTime[i] = curTime; |
| 317 | } |
| 318 | return retval; |
| 319 | } |
| 320 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 321 | void HAL_SetPrintErrorImpl(void (*func)(const char* line, size_t size)) { |
| 322 | gPrintErrorImpl.store(func ? func : hal::DefaultPrintErrorImpl); |
| 323 | } |
| 324 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 325 | int32_t HAL_SendConsoleLine(const char* line) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 326 | std::string_view lineRef{line}; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 327 | if (lineRef.size() <= 65535) { |
| 328 | // Send directly |
| 329 | return FRC_NetworkCommunication_sendConsoleLine(line); |
| 330 | } else { |
| 331 | // Need to truncate |
| 332 | std::string newLine{line, 65535}; |
| 333 | return FRC_NetworkCommunication_sendConsoleLine(newLine.c_str()); |
| 334 | } |
| 335 | } |
| 336 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 337 | int32_t HAL_GetControlWord(HAL_ControlWord* controlWord) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 338 | std::scoped_lock lock{cacheMutex}; |
| 339 | *controlWord = newestControlWord; |
| 340 | return 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | int32_t HAL_GetJoystickAxes(int32_t joystickNum, HAL_JoystickAxes* axes) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 344 | CHECK_JOYSTICK_NUMBER(joystickNum); |
| 345 | std::scoped_lock lock{cacheMutex}; |
| 346 | *axes = currentRead->axes[joystickNum]; |
| 347 | return 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | int32_t HAL_GetJoystickPOVs(int32_t joystickNum, HAL_JoystickPOVs* povs) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 351 | CHECK_JOYSTICK_NUMBER(joystickNum); |
| 352 | std::scoped_lock lock{cacheMutex}; |
| 353 | *povs = currentRead->povs[joystickNum]; |
| 354 | return 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | int32_t HAL_GetJoystickButtons(int32_t joystickNum, |
| 358 | HAL_JoystickButtons* buttons) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 359 | CHECK_JOYSTICK_NUMBER(joystickNum); |
| 360 | std::scoped_lock lock{cacheMutex}; |
| 361 | *buttons = currentRead->buttons[joystickNum]; |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | void HAL_GetAllJoystickData(HAL_JoystickAxes* axes, HAL_JoystickPOVs* povs, |
| 366 | HAL_JoystickButtons* buttons) { |
| 367 | std::scoped_lock lock{cacheMutex}; |
| 368 | std::memcpy(axes, currentRead->axes, sizeof(currentRead->axes)); |
| 369 | std::memcpy(povs, currentRead->povs, sizeof(currentRead->povs)); |
| 370 | std::memcpy(buttons, currentRead->buttons, sizeof(currentRead->buttons)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | int32_t HAL_GetJoystickDescriptor(int32_t joystickNum, |
| 374 | HAL_JoystickDescriptor* desc) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 375 | CHECK_JOYSTICK_NUMBER(joystickNum); |
| 376 | std::scoped_lock lock{tcpCacheMutex}; |
| 377 | *desc = tcpCurrent.descriptors[joystickNum]; |
| 378 | return 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | int32_t HAL_GetMatchInfo(HAL_MatchInfo* info) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 382 | std::scoped_lock lock{tcpCacheMutex}; |
| 383 | *info = tcpCurrent.matchInfo; |
| 384 | return 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | HAL_AllianceStationID HAL_GetAllianceStation(int32_t* status) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 388 | std::scoped_lock lock{cacheMutex}; |
| 389 | return currentRead->allianceStation; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | HAL_Bool HAL_GetJoystickIsXbox(int32_t joystickNum) { |
| 393 | HAL_JoystickDescriptor joystickDesc; |
| 394 | if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) { |
| 395 | return 0; |
| 396 | } else { |
| 397 | return joystickDesc.isXbox; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | int32_t HAL_GetJoystickType(int32_t joystickNum) { |
| 402 | HAL_JoystickDescriptor joystickDesc; |
| 403 | if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) { |
| 404 | return -1; |
| 405 | } else { |
| 406 | return joystickDesc.type; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | char* HAL_GetJoystickName(int32_t joystickNum) { |
| 411 | HAL_JoystickDescriptor joystickDesc; |
| 412 | if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) { |
| 413 | char* name = static_cast<char*>(std::malloc(1)); |
| 414 | name[0] = '\0'; |
| 415 | return name; |
| 416 | } else { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 417 | const size_t len = std::strlen(joystickDesc.name) + 1; |
| 418 | char* name = static_cast<char*>(std::malloc(len)); |
| 419 | std::memcpy(name, joystickDesc.name, len); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 420 | return name; |
| 421 | } |
| 422 | } |
| 423 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 424 | void HAL_FreeJoystickName(char* name) { |
| 425 | std::free(name); |
| 426 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 427 | |
| 428 | int32_t HAL_GetJoystickAxisType(int32_t joystickNum, int32_t axis) { |
| 429 | HAL_JoystickDescriptor joystickDesc; |
| 430 | if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) { |
| 431 | return -1; |
| 432 | } else { |
| 433 | return joystickDesc.axisTypes[axis]; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | int32_t HAL_SetJoystickOutputs(int32_t joystickNum, int64_t outputs, |
| 438 | int32_t leftRumble, int32_t rightRumble) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 439 | CHECK_JOYSTICK_NUMBER(joystickNum); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 440 | return FRC_NetworkCommunication_setJoystickOutputs(joystickNum, outputs, |
| 441 | leftRumble, rightRumble); |
| 442 | } |
| 443 | |
| 444 | double HAL_GetMatchTime(int32_t* status) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 445 | std::scoped_lock lock{cacheMutex}; |
| 446 | return currentRead->matchTime; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | void HAL_ObserveUserProgramStarting(void) { |
| 450 | FRC_NetworkCommunication_observeUserProgramStarting(); |
| 451 | } |
| 452 | |
| 453 | void HAL_ObserveUserProgramDisabled(void) { |
| 454 | FRC_NetworkCommunication_observeUserProgramDisabled(); |
| 455 | } |
| 456 | |
| 457 | void HAL_ObserveUserProgramAutonomous(void) { |
| 458 | FRC_NetworkCommunication_observeUserProgramAutonomous(); |
| 459 | } |
| 460 | |
| 461 | void HAL_ObserveUserProgramTeleop(void) { |
| 462 | FRC_NetworkCommunication_observeUserProgramTeleop(); |
| 463 | } |
| 464 | |
| 465 | void HAL_ObserveUserProgramTest(void) { |
| 466 | FRC_NetworkCommunication_observeUserProgramTest(); |
| 467 | } |
| 468 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 469 | // Constant number to be used for our occur handle |
| 470 | constexpr int32_t refNumber = 42; |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 471 | constexpr int32_t tcpRefNumber = 94; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 472 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 473 | static void tcpOccur(void) { |
| 474 | uint32_t mask = FRC_NetworkCommunication_getNewTcpRecvMask(); |
| 475 | tcpMask.fetch_or(mask); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 476 | } |
| 477 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 478 | static void udpOccur(void) { |
| 479 | cacheToUpdate->Update(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 480 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 481 | JoystickDataCache* given = cacheToUpdate; |
| 482 | JoystickDataCache* prev = currentCache.exchange(cacheToUpdate); |
| 483 | if (prev == nullptr) { |
| 484 | cacheToUpdate = currentReadLocal; |
| 485 | currentReadLocal = lastGiven; |
| 486 | } else { |
| 487 | // Current read local does not update |
| 488 | cacheToUpdate = prev; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 489 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 490 | lastGiven = given; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 491 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 492 | driverStation->newDataEvents.Wakeup(); |
| 493 | } |
| 494 | |
| 495 | static void newDataOccur(uint32_t refNum) { |
| 496 | switch (refNum) { |
| 497 | case refNumber: |
| 498 | udpOccur(); |
| 499 | break; |
| 500 | |
| 501 | case tcpRefNumber: |
| 502 | tcpOccur(); |
| 503 | break; |
| 504 | |
| 505 | default: |
| 506 | std::printf("Unknown occur %u\n", refNum); |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | void HAL_RefreshDSData(void) { |
| 512 | HAL_ControlWord controlWord; |
| 513 | std::memset(&controlWord, 0, sizeof(controlWord)); |
| 514 | FRC_NetworkCommunication_getControlWord( |
| 515 | reinterpret_cast<ControlWord_t*>(&controlWord)); |
| 516 | std::scoped_lock lock{cacheMutex}; |
| 517 | JoystickDataCache* prev = currentCache.exchange(nullptr); |
| 518 | if (prev != nullptr) { |
| 519 | currentRead = prev; |
| 520 | } |
| 521 | newestControlWord = controlWord; |
| 522 | |
| 523 | uint32_t mask = tcpMask.exchange(0); |
| 524 | if (mask != 0) { |
| 525 | tcpCache.Update(mask); |
| 526 | std::scoped_lock tcpLock(tcpCacheMutex); |
| 527 | tcpCache.CloneTo(&tcpCurrent); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | void HAL_ProvideNewDataEventHandle(WPI_EventHandle handle) { |
| 532 | hal::init::CheckInit(); |
| 533 | driverStation->newDataEvents.Add(handle); |
| 534 | } |
| 535 | |
| 536 | void HAL_RemoveNewDataEventHandle(WPI_EventHandle handle) { |
| 537 | driverStation->newDataEvents.Remove(handle); |
| 538 | } |
| 539 | |
| 540 | HAL_Bool HAL_GetOutputsEnabled(void) { |
| 541 | return FRC_NetworkCommunication_getWatchdogActive(); |
| 542 | } |
| 543 | |
| 544 | } // extern "C" |
| 545 | |
| 546 | namespace hal { |
| 547 | void InitializeDriverStation() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 548 | // Set up the occur function internally with NetComm |
| 549 | NetCommRPCProxy_SetOccurFuncPointer(newDataOccur); |
| 550 | // Set up our occur reference number |
| 551 | setNewDataOccurRef(refNumber); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 552 | FRC_NetworkCommunication_setNewTcpDataOccurRef(tcpRefNumber); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 553 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 554 | } // namespace hal |