Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 2 | /* Copyright (c) FIRST 2008-2016. All Rights Reserved. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "Notifier.h" |
| 9 | #include "Timer.h" |
| 10 | #include "Utility.h" |
| 11 | #include "WPIErrors.h" |
| 12 | #include "HAL/HAL.hpp" |
| 13 | |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 14 | /** |
| 15 | * Create a Notifier for timer event notification. |
| 16 | * @param handler The handler is called at the notification time which is set |
| 17 | * using StartSingle or StartPeriodic. |
| 18 | */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 19 | Notifier::Notifier(TimerEventHandler handler) { |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 20 | if (handler == nullptr) |
| 21 | wpi_setWPIErrorWithContext(NullParameter, "handler must not be nullptr"); |
| 22 | m_handler = handler; |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 23 | int32_t status = 0; |
| 24 | m_notifier = initializeNotifier(&Notifier::Notify, this, &status); |
| 25 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Free the resources for a timer event. |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 30 | */ |
| 31 | Notifier::~Notifier() { |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 32 | int32_t status = 0; |
| 33 | cleanNotifier(m_notifier, &status); |
| 34 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 35 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 36 | /* Acquire the mutex; this makes certain that the handler is not being |
| 37 | * executed by the interrupt manager. |
| 38 | */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 39 | std::lock_guard<priority_mutex> lock(m_handlerMutex); |
| 40 | } |
| 41 | |
| 42 | /** |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 43 | * Update the HAL alarm time. |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 44 | */ |
| 45 | void Notifier::UpdateAlarm() { |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 46 | int32_t status = 0; |
| 47 | updateNotifierAlarm(m_notifier, (uint64_t)(m_expirationTime * 1e6), &status); |
| 48 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 52 | * Notify is called by the HAL layer. We simply need to pass it through to |
| 53 | * the user handler. |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 54 | */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 55 | void Notifier::Notify(uint64_t currentTimeInt, void *param) { |
| 56 | Notifier* notifier = static_cast<Notifier*>(param); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 57 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 58 | notifier->m_processMutex.lock(); |
| 59 | if (notifier->m_periodic) { |
| 60 | notifier->m_expirationTime += notifier->m_period; |
| 61 | notifier->UpdateAlarm(); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 62 | } |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 63 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 64 | auto handler = notifier->m_handler; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 65 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 66 | notifier->m_handlerMutex.lock(); |
| 67 | notifier->m_processMutex.unlock(); |
| 68 | |
| 69 | if (handler) handler(); |
| 70 | notifier->m_handlerMutex.unlock(); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Register for single event notification. |
| 75 | * A timer event is queued for a single event after the specified delay. |
| 76 | * @param delay Seconds to wait before the handler is called. |
| 77 | */ |
| 78 | void Notifier::StartSingle(double delay) { |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 79 | std::lock_guard<priority_mutex> sync(m_processMutex); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 80 | m_periodic = false; |
| 81 | m_period = delay; |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 82 | m_expirationTime = GetClock() + m_period; |
| 83 | UpdateAlarm(); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Register for periodic event notification. |
| 88 | * A timer event is queued for periodic event notification. Each time the |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 89 | * interrupt occurs, the event will be immediately requeued for the same time |
| 90 | * interval. |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 91 | * @param period Period in seconds to call the handler starting one period after |
| 92 | * the call to this method. |
| 93 | */ |
| 94 | void Notifier::StartPeriodic(double period) { |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 95 | std::lock_guard<priority_mutex> sync(m_processMutex); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 96 | m_periodic = true; |
| 97 | m_period = period; |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 98 | m_expirationTime = GetClock() + m_period; |
| 99 | UpdateAlarm(); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Stop timer events from occuring. |
| 104 | * Stop any repeating timer events from occuring. This will also remove any |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 105 | * single notification events from the queue. |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 106 | * If a timer-based call to the registered handler is in progress, this function |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 107 | * will block until the handler call is complete. |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 108 | */ |
| 109 | void Notifier::Stop() { |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 110 | int32_t status = 0; |
| 111 | stopNotifierAlarm(m_notifier, &status); |
| 112 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 113 | |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 114 | // Wait for a currently executing handler to complete before returning from |
| 115 | // Stop() |
| 116 | std::lock_guard<priority_mutex> sync(m_handlerMutex); |
| 117 | } |