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