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 "hal/DriverStation.h" |
| 6 | |
| 7 | #ifdef __APPLE__ |
| 8 | #include <pthread.h> |
| 9 | #endif |
| 10 | |
| 11 | #include <cstdio> |
| 12 | #include <cstdlib> |
| 13 | #include <cstring> |
| 14 | #include <string> |
| 15 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 16 | #include <fmt/format.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | #include <wpi/condition_variable.h> |
| 18 | #include <wpi/mutex.h> |
| 19 | |
| 20 | #include "HALInitializer.h" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 21 | #include "hal/cpp/fpga_clock.h" |
| 22 | #include "hal/simulation/MockHooks.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 23 | #include "mockdata/DriverStationDataInternal.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 24 | |
| 25 | static wpi::mutex msgMutex; |
| 26 | static wpi::condition_variable* newDSDataAvailableCond; |
| 27 | static wpi::mutex newDSDataAvailableMutex; |
| 28 | static int newDSDataAvailableCounter{0}; |
| 29 | static std::atomic_bool isFinalized{false}; |
| 30 | static std::atomic<HALSIM_SendErrorHandler> sendErrorHandler{nullptr}; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 31 | static std::atomic<HALSIM_SendConsoleLineHandler> sendConsoleLineHandler{ |
| 32 | nullptr}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 33 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 34 | namespace hal::init { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 35 | void InitializeDriverStation() { |
| 36 | static wpi::condition_variable nddaC; |
| 37 | newDSDataAvailableCond = &nddaC; |
| 38 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 39 | } // namespace hal::init |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | |
| 41 | using namespace hal; |
| 42 | |
| 43 | extern "C" { |
| 44 | |
| 45 | void HALSIM_SetSendError(HALSIM_SendErrorHandler handler) { |
| 46 | sendErrorHandler.store(handler); |
| 47 | } |
| 48 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 49 | void HALSIM_SetSendConsoleLine(HALSIM_SendConsoleLineHandler handler) { |
| 50 | sendConsoleLineHandler.store(handler); |
| 51 | } |
| 52 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 53 | int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode, |
| 54 | const char* details, const char* location, |
| 55 | const char* callStack, HAL_Bool printMsg) { |
| 56 | auto errorHandler = sendErrorHandler.load(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 57 | if (errorHandler) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 58 | return errorHandler(isError, errorCode, isLVCode, details, location, |
| 59 | callStack, printMsg); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 60 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 61 | // Avoid flooding console by keeping track of previous 5 error |
| 62 | // messages and only printing again if they're longer than 1 second old. |
| 63 | static constexpr int KEEP_MSGS = 5; |
| 64 | std::scoped_lock lock(msgMutex); |
| 65 | static std::string prevMsg[KEEP_MSGS]; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 66 | static fpga_clock::time_point prevMsgTime[KEEP_MSGS]; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 67 | static bool initialized = false; |
| 68 | if (!initialized) { |
| 69 | for (int i = 0; i < KEEP_MSGS; i++) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 70 | prevMsgTime[i] = fpga_clock::now() - std::chrono::seconds(2); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 71 | } |
| 72 | initialized = true; |
| 73 | } |
| 74 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 75 | auto curTime = fpga_clock::now(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 76 | int i; |
| 77 | for (i = 0; i < KEEP_MSGS; ++i) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 78 | if (prevMsg[i] == details) { |
| 79 | break; |
| 80 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 81 | } |
| 82 | int retval = 0; |
| 83 | if (i == KEEP_MSGS || (curTime - prevMsgTime[i]) >= std::chrono::seconds(1)) { |
| 84 | printMsg = true; |
| 85 | if (printMsg) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 86 | fmt::memory_buffer buf; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 87 | if (location && location[0] != '\0') { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 88 | fmt::format_to(fmt::appender{buf}, |
| 89 | "{} at {}: ", isError ? "Error" : "Warning", location); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 90 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 91 | fmt::format_to(fmt::appender{buf}, "{}\n", details); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 92 | if (callStack && callStack[0] != '\0') { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 93 | fmt::format_to(fmt::appender{buf}, "{}\n", callStack); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 94 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 95 | std::fwrite(buf.data(), buf.size(), 1, stderr); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 96 | } |
| 97 | if (i == KEEP_MSGS) { |
| 98 | // replace the oldest one |
| 99 | i = 0; |
| 100 | auto first = prevMsgTime[0]; |
| 101 | for (int j = 1; j < KEEP_MSGS; ++j) { |
| 102 | if (prevMsgTime[j] < first) { |
| 103 | first = prevMsgTime[j]; |
| 104 | i = j; |
| 105 | } |
| 106 | } |
| 107 | prevMsg[i] = details; |
| 108 | } |
| 109 | prevMsgTime[i] = curTime; |
| 110 | } |
| 111 | return retval; |
| 112 | } |
| 113 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 114 | int32_t HAL_SendConsoleLine(const char* line) { |
| 115 | auto handler = sendConsoleLineHandler.load(); |
| 116 | if (handler) { |
| 117 | return handler(line); |
| 118 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 119 | std::puts(line); |
| 120 | std::fflush(stdout); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 124 | int32_t HAL_GetControlWord(HAL_ControlWord* controlWord) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 125 | std::memset(controlWord, 0, sizeof(HAL_ControlWord)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 126 | controlWord->enabled = SimDriverStationData->enabled; |
| 127 | controlWord->autonomous = SimDriverStationData->autonomous; |
| 128 | controlWord->test = SimDriverStationData->test; |
| 129 | controlWord->eStop = SimDriverStationData->eStop; |
| 130 | controlWord->fmsAttached = SimDriverStationData->fmsAttached; |
| 131 | controlWord->dsAttached = SimDriverStationData->dsAttached; |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | HAL_AllianceStationID HAL_GetAllianceStation(int32_t* status) { |
| 136 | *status = 0; |
| 137 | return SimDriverStationData->allianceStationId; |
| 138 | } |
| 139 | |
| 140 | int32_t HAL_GetJoystickAxes(int32_t joystickNum, HAL_JoystickAxes* axes) { |
| 141 | SimDriverStationData->GetJoystickAxes(joystickNum, axes); |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | int32_t HAL_GetJoystickPOVs(int32_t joystickNum, HAL_JoystickPOVs* povs) { |
| 146 | SimDriverStationData->GetJoystickPOVs(joystickNum, povs); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int32_t HAL_GetJoystickButtons(int32_t joystickNum, |
| 151 | HAL_JoystickButtons* buttons) { |
| 152 | SimDriverStationData->GetJoystickButtons(joystickNum, buttons); |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | int32_t HAL_GetJoystickDescriptor(int32_t joystickNum, |
| 157 | HAL_JoystickDescriptor* desc) { |
| 158 | SimDriverStationData->GetJoystickDescriptor(joystickNum, desc); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | HAL_Bool HAL_GetJoystickIsXbox(int32_t joystickNum) { |
| 163 | HAL_JoystickDescriptor desc; |
| 164 | SimDriverStationData->GetJoystickDescriptor(joystickNum, &desc); |
| 165 | return desc.isXbox; |
| 166 | } |
| 167 | |
| 168 | int32_t HAL_GetJoystickType(int32_t joystickNum) { |
| 169 | HAL_JoystickDescriptor desc; |
| 170 | SimDriverStationData->GetJoystickDescriptor(joystickNum, &desc); |
| 171 | return desc.type; |
| 172 | } |
| 173 | |
| 174 | char* HAL_GetJoystickName(int32_t joystickNum) { |
| 175 | HAL_JoystickDescriptor desc; |
| 176 | SimDriverStationData->GetJoystickDescriptor(joystickNum, &desc); |
| 177 | size_t len = std::strlen(desc.name); |
| 178 | char* name = static_cast<char*>(std::malloc(len + 1)); |
| 179 | std::memcpy(name, desc.name, len + 1); |
| 180 | return name; |
| 181 | } |
| 182 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 183 | void HAL_FreeJoystickName(char* name) { |
| 184 | std::free(name); |
| 185 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 186 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 187 | int32_t HAL_GetJoystickAxisType(int32_t joystickNum, int32_t axis) { |
| 188 | return 0; |
| 189 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 190 | |
| 191 | int32_t HAL_SetJoystickOutputs(int32_t joystickNum, int64_t outputs, |
| 192 | int32_t leftRumble, int32_t rightRumble) { |
| 193 | SimDriverStationData->SetJoystickOutputs(joystickNum, outputs, leftRumble, |
| 194 | rightRumble); |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | double HAL_GetMatchTime(int32_t* status) { |
| 199 | return SimDriverStationData->matchTime; |
| 200 | } |
| 201 | |
| 202 | int32_t HAL_GetMatchInfo(HAL_MatchInfo* info) { |
| 203 | SimDriverStationData->GetMatchInfo(info); |
| 204 | return 0; |
| 205 | } |
| 206 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 207 | void HAL_ObserveUserProgramStarting(void) { |
| 208 | HALSIM_SetProgramStarted(); |
| 209 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 210 | |
| 211 | void HAL_ObserveUserProgramDisabled(void) { |
| 212 | // TODO |
| 213 | } |
| 214 | |
| 215 | void HAL_ObserveUserProgramAutonomous(void) { |
| 216 | // TODO |
| 217 | } |
| 218 | |
| 219 | void HAL_ObserveUserProgramTeleop(void) { |
| 220 | // TODO |
| 221 | } |
| 222 | |
| 223 | void HAL_ObserveUserProgramTest(void) { |
| 224 | // TODO |
| 225 | } |
| 226 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 227 | static int& GetThreadLocalLastCount() { |
| 228 | // There is a rollover error condition here. At Packet# = n * (uintmax), this |
| 229 | // will return false when instead it should return true. However, this at a |
| 230 | // 20ms rate occurs once every 2.7 years of DS connected runtime, so not |
| 231 | // worth the cycles to check. |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 232 | thread_local int lastCount{0}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 233 | return lastCount; |
| 234 | } |
| 235 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 236 | HAL_Bool HAL_IsNewControlData(void) { |
| 237 | std::scoped_lock lock(newDSDataAvailableMutex); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 238 | int& lastCount = GetThreadLocalLastCount(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 239 | int currentCount = newDSDataAvailableCounter; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 240 | if (lastCount == currentCount) { |
| 241 | return false; |
| 242 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 243 | lastCount = currentCount; |
| 244 | return true; |
| 245 | } |
| 246 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 247 | void HAL_WaitForDSData(void) { |
| 248 | HAL_WaitForDSDataTimeout(0); |
| 249 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 250 | |
| 251 | HAL_Bool HAL_WaitForDSDataTimeout(double timeout) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 252 | std::unique_lock lock(newDSDataAvailableMutex); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 253 | int& lastCount = GetThreadLocalLastCount(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 254 | int currentCount = newDSDataAvailableCounter; |
| 255 | if (lastCount != currentCount) { |
| 256 | lastCount = currentCount; |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | if (isFinalized.load()) { |
| 261 | return false; |
| 262 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 263 | auto timeoutTime = |
| 264 | std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout); |
| 265 | |
| 266 | while (newDSDataAvailableCounter == currentCount) { |
| 267 | if (timeout > 0) { |
| 268 | auto timedOut = newDSDataAvailableCond->wait_until(lock, timeoutTime); |
| 269 | if (timedOut == std::cv_status::timeout) { |
| 270 | return false; |
| 271 | } |
| 272 | } else { |
| 273 | newDSDataAvailableCond->wait(lock); |
| 274 | } |
| 275 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 276 | lastCount = newDSDataAvailableCounter; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 277 | return true; |
| 278 | } |
| 279 | |
| 280 | // Constant number to be used for our occur handle |
| 281 | constexpr int32_t refNumber = 42; |
| 282 | |
| 283 | static int32_t newDataOccur(uint32_t refNum) { |
| 284 | // Since we could get other values, require our specific handle |
| 285 | // to signal our threads |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 286 | if (refNum != refNumber) { |
| 287 | return 0; |
| 288 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 289 | SimDriverStationData->CallNewDataCallbacks(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 290 | std::scoped_lock lock(newDSDataAvailableMutex); |
| 291 | // Nofify all threads |
| 292 | newDSDataAvailableCounter++; |
| 293 | newDSDataAvailableCond->notify_all(); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | void HAL_InitializeDriverStation(void) { |
| 298 | hal::init::CheckInit(); |
| 299 | static std::atomic_bool initialized{false}; |
| 300 | static wpi::mutex initializeMutex; |
| 301 | // Initial check, as if it's true initialization has finished |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 302 | if (initialized) { |
| 303 | return; |
| 304 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 305 | |
| 306 | std::scoped_lock lock(initializeMutex); |
| 307 | // Second check in case another thread was waiting |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 308 | if (initialized) { |
| 309 | return; |
| 310 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 311 | |
| 312 | SimDriverStationData->ResetData(); |
| 313 | |
| 314 | std::atexit([]() { |
| 315 | isFinalized.store(true); |
| 316 | HAL_ReleaseDSMutex(); |
| 317 | }); |
| 318 | |
| 319 | initialized = true; |
| 320 | } |
| 321 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 322 | void HAL_ReleaseDSMutex(void) { |
| 323 | newDataOccur(refNumber); |
| 324 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 325 | |
| 326 | } // extern "C" |