Adds way to reset and version all HAL handles (#545)

Useful in the sim to force a full reset. On roboRIO, the information is
still created and added, but is not checked because of speed
considerations.
This commit is contained in:
Thad House
2017-06-30 16:28:28 -07:00
committed by Peter Johnson
parent 0cd03c66e3
commit e824b1129e
10 changed files with 187 additions and 45 deletions

View File

@@ -18,6 +18,7 @@ model {
binaries.all {
tasks.withType(CppCompile) {
cppCompiler.args "-DNAMESPACED_PRIORITY"
cppCompiler.args "-DCONFIG_ATHENA"
addNiLibraryLinks(linker, targetPlatform)
addWpiUtilLibraryLinks(it, linker, targetPlatform)
}

View File

@@ -33,7 +33,7 @@ namespace hal {
*
*/
template <typename THandle, typename TStruct, int16_t size>
class DigitalHandleResource {
class DigitalHandleResource : public HandleBase {
friend class DigitalHandleResourceTest;
public:
@@ -44,6 +44,7 @@ class DigitalHandleResource {
THandle Allocate(int16_t index, HAL_HandleEnum enumValue, int32_t* status);
std::shared_ptr<TStruct> Get(THandle handle, HAL_HandleEnum enumValue);
void Free(THandle handle, HAL_HandleEnum enumValue);
void ResetHandles() override;
private:
std::array<std::shared_ptr<TStruct>, size> m_structures;
@@ -65,14 +66,14 @@ THandle DigitalHandleResource<THandle, TStruct, size>::Allocate(
return HAL_kInvalidHandle;
}
m_structures[index] = std::make_shared<TStruct>();
return static_cast<THandle>(hal::createHandle(index, enumValue));
return static_cast<THandle>(hal::createHandle(index, enumValue, m_version));
}
template <typename THandle, typename TStruct, int16_t size>
std::shared_ptr<TStruct> DigitalHandleResource<THandle, TStruct, size>::Get(
THandle handle, HAL_HandleEnum enumValue) {
// get handle index, and fail early if index out of range or wrong handle
int16_t index = getHandleTypedIndex(handle, enumValue);
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) {
return nullptr;
}
@@ -86,10 +87,19 @@ template <typename THandle, typename TStruct, int16_t size>
void DigitalHandleResource<THandle, TStruct, size>::Free(
THandle handle, HAL_HandleEnum enumValue) {
// get handle index, and fail early if index out of range or wrong handle
int16_t index = getHandleTypedIndex(handle, enumValue);
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) return;
// lock and deallocated handle
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
m_structures[index].reset();
}
template <typename THandle, typename TStruct, int16_t size>
void DigitalHandleResource<THandle, TStruct, size>::ResetHandles() {
for (int i = 0; i < size; i++) {
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
m_structures[i].reset();
}
HandleBase::ResetHandles();
}
} // namespace hal

View File

@@ -13,7 +13,7 @@
/* General Handle Data Layout
* Bits 0-15: Handle Index
* Bits 16-23: Unused
* Bits 16-23: 8 bit rolling reset detection
* Bits 24-30: Handle Type
* Bit 31: 1 if handle error, 0 if no error
*
@@ -23,6 +23,19 @@
namespace hal {
class HandleBase {
public:
HandleBase();
~HandleBase();
HandleBase(const HandleBase&) = delete;
HandleBase& operator=(const HandleBase&) = delete;
virtual void ResetHandles();
static void ResetGlobalHandles();
protected:
int16_t m_version;
};
constexpr int16_t InvalidHandleIndex = -1;
enum class HAL_HandleEnum {
@@ -57,9 +70,16 @@ static inline HAL_HandleEnum getHandleType(HAL_Handle handle) {
static inline bool isHandleType(HAL_Handle handle, HAL_HandleEnum handleType) {
return handleType == getHandleType(handle);
}
static inline bool isHandleCorrectVersion(HAL_Handle handle, int16_t version) {
return (((handle & 0xFF0000) >> 16) & version) == version;
}
static inline int16_t getHandleTypedIndex(HAL_Handle handle,
HAL_HandleEnum enumType) {
HAL_HandleEnum enumType,
int16_t version) {
if (!isHandleType(handle, enumType)) return InvalidHandleIndex;
#if !defined(CONFIG_ATHENA)
if (!isHandleCorrectVersion(handle, version)) return InvalidHandleIndex;
#endif
return getHandleIndex(handle);
}
@@ -94,5 +114,6 @@ HAL_PortHandle createPortHandle(uint8_t channel, uint8_t module);
HAL_PortHandle createPortHandleForSPI(uint8_t channel);
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType);
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType,
int16_t version);
} // namespace hal

View File

@@ -35,7 +35,7 @@ namespace hal {
*/
template <typename THandle, typename TStruct, int16_t size,
HAL_HandleEnum enumValue>
class IndexedClassedHandleResource {
class IndexedClassedHandleResource : public HandleBase {
friend class IndexedClassedHandleResourceTest;
public:
@@ -50,9 +50,8 @@ class IndexedClassedHandleResource {
void Free(THandle handle);
private:
// Dynamic array to shrink HAL file size.
std::unique_ptr<std::shared_ptr<TStruct>[]> m_structures;
std::unique_ptr<hal::priority_mutex[]> m_handleMutexes;
std::array<std::shared_ptr<TStruct>[], size> m_structures;
std::array<hal::priority_mutex[], size> m_handleMutexes;
};
template <typename THandle, typename TStruct, int16_t size,
@@ -80,7 +79,7 @@ IndexedClassedHandleResource<THandle, TStruct, size, enumValue>::Allocate(
return HAL_kInvalidHandle;
}
m_structures[index] = toSet;
return static_cast<THandle>(hal::createHandle(index, enumValue));
return static_cast<THandle>(hal::createHandle(index, enumValue, m_version));
}
template <typename THandle, typename TStruct, int16_t size,
@@ -88,7 +87,7 @@ template <typename THandle, typename TStruct, int16_t size,
std::shared_ptr<TStruct> IndexedClassedHandleResource<
THandle, TStruct, size, enumValue>::Get(THandle handle) {
// get handle index, and fail early if index out of range or wrong handle
int16_t index = getHandleTypedIndex(handle, enumValue);
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) {
return nullptr;
}
@@ -103,10 +102,21 @@ template <typename THandle, typename TStruct, int16_t size,
void IndexedClassedHandleResource<THandle, TStruct, size, enumValue>::Free(
THandle handle) {
// get handle index, and fail early if index out of range or wrong handle
int16_t index = getHandleTypedIndex(handle, enumValue);
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) return;
// lock and deallocated handle
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
m_structures[index].reset();
}
template <typename THandle, typename TStruct, int16_t size,
HAL_HandleEnum enumValue>
void IndexedClassedHandleResource<THandle, TStruct, size,
enumValue>::ResetHandles() {
for (int i = 0; i < size; i++) {
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
m_structures[i].reset();
}
HandleBase::ResetHandles();
}
} // namespace hal

View File

@@ -34,7 +34,7 @@ namespace hal {
*/
template <typename THandle, typename TStruct, int16_t size,
HAL_HandleEnum enumValue>
class IndexedHandleResource {
class IndexedHandleResource : public HandleBase {
friend class IndexedHandleResourceTest;
public:
@@ -45,6 +45,7 @@ class IndexedHandleResource {
THandle Allocate(int16_t index, int32_t* status);
std::shared_ptr<TStruct> Get(THandle handle);
void Free(THandle handle);
void ResetHandles() override;
private:
std::array<std::shared_ptr<TStruct>, size> m_structures;
@@ -67,7 +68,7 @@ THandle IndexedHandleResource<THandle, TStruct, size, enumValue>::Allocate(
return HAL_kInvalidHandle;
}
m_structures[index] = std::make_shared<TStruct>();
return static_cast<THandle>(hal::createHandle(index, enumValue));
return static_cast<THandle>(hal::createHandle(index, enumValue, m_version));
}
template <typename THandle, typename TStruct, int16_t size,
@@ -75,7 +76,7 @@ template <typename THandle, typename TStruct, int16_t size,
std::shared_ptr<TStruct>
IndexedHandleResource<THandle, TStruct, size, enumValue>::Get(THandle handle) {
// get handle index, and fail early if index out of range or wrong handle
int16_t index = getHandleTypedIndex(handle, enumValue);
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) {
return nullptr;
}
@@ -90,10 +91,20 @@ template <typename THandle, typename TStruct, int16_t size,
void IndexedHandleResource<THandle, TStruct, size, enumValue>::Free(
THandle handle) {
// get handle index, and fail early if index out of range or wrong handle
int16_t index = getHandleTypedIndex(handle, enumValue);
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) return;
// lock and deallocated handle
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
m_structures[index].reset();
}
template <typename THandle, typename TStruct, int16_t size,
HAL_HandleEnum enumValue>
void IndexedHandleResource<THandle, TStruct, size, enumValue>::ResetHandles() {
for (int i = 0; i < size; i++) {
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
m_structures[i].reset();
}
HandleBase::ResetHandles();
}
} // namespace hal

View File

@@ -32,7 +32,7 @@ namespace hal {
*/
template <typename THandle, typename TStruct, int16_t size,
HAL_HandleEnum enumValue>
class LimitedClassedHandleResource {
class LimitedClassedHandleResource : public HandleBase {
friend class LimitedClassedHandleResourceTest;
public:
@@ -44,6 +44,7 @@ class LimitedClassedHandleResource {
THandle Allocate(std::shared_ptr<TStruct> toSet);
std::shared_ptr<TStruct> Get(THandle handle);
void Free(THandle handle);
void ResetHandles() override;
private:
std::array<std::shared_ptr<TStruct>, size> m_structures;
@@ -64,7 +65,7 @@ LimitedClassedHandleResource<THandle, TStruct, size, enumValue>::Allocate(
// and allocate it.
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
m_structures[i] = toSet;
return static_cast<THandle>(createHandle(i, enumValue));
return static_cast<THandle>(createHandle(i, enumValue, m_version));
}
}
return HAL_kInvalidHandle;
@@ -75,7 +76,7 @@ template <typename THandle, typename TStruct, int16_t size,
std::shared_ptr<TStruct> LimitedClassedHandleResource<
THandle, TStruct, size, enumValue>::Get(THandle handle) {
// get handle index, and fail early if index out of range or wrong handle
int16_t index = getHandleTypedIndex(handle, enumValue);
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) {
return nullptr;
}
@@ -90,11 +91,25 @@ template <typename THandle, typename TStruct, int16_t size,
void LimitedClassedHandleResource<THandle, TStruct, size, enumValue>::Free(
THandle handle) {
// get handle index, and fail early if index out of range or wrong handle
int16_t index = getHandleTypedIndex(handle, enumValue);
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) return;
// lock and deallocated handle
std::lock_guard<hal::priority_mutex> sync(m_allocateMutex);
std::lock_guard<hal::priority_mutex> lock(m_handleMutexes[index]);
m_structures[index].reset();
}
template <typename THandle, typename TStruct, int16_t size,
HAL_HandleEnum enumValue>
void LimitedClassedHandleResource<THandle, TStruct, size,
enumValue>::ResetHandles() {
{
std::lock_guard<hal::priority_mutex> lock(m_allocateMutex);
for (int i = 0; i < size; i++) {
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
m_structures[i].reset();
}
}
HandleBase::ResetHandles();
}
} // namespace hal

View File

@@ -31,7 +31,7 @@ namespace hal {
*/
template <typename THandle, typename TStruct, int16_t size,
HAL_HandleEnum enumValue>
class LimitedHandleResource {
class LimitedHandleResource : public HandleBase {
friend class LimitedHandleResourceTest;
public:
@@ -42,6 +42,7 @@ class LimitedHandleResource {
THandle Allocate();
std::shared_ptr<TStruct> Get(THandle handle);
void Free(THandle handle);
void ResetHandles() override;
private:
std::array<std::shared_ptr<TStruct>, size> m_structures;
@@ -60,7 +61,7 @@ THandle LimitedHandleResource<THandle, TStruct, size, enumValue>::Allocate() {
// and allocate it.
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
m_structures[i] = std::make_shared<TStruct>();
return static_cast<THandle>(createHandle(i, enumValue));
return static_cast<THandle>(createHandle(i, enumValue, m_version));
}
}
return HAL_kInvalidHandle;
@@ -71,7 +72,7 @@ template <typename THandle, typename TStruct, int16_t size,
std::shared_ptr<TStruct>
LimitedHandleResource<THandle, TStruct, size, enumValue>::Get(THandle handle) {
// get handle index, and fail early if index out of range or wrong handle
int16_t index = getHandleTypedIndex(handle, enumValue);
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) {
return nullptr;
}
@@ -86,11 +87,24 @@ template <typename THandle, typename TStruct, int16_t size,
void LimitedHandleResource<THandle, TStruct, size, enumValue>::Free(
THandle handle) {
// get handle index, and fail early if index out of range or wrong handle
int16_t index = getHandleTypedIndex(handle, enumValue);
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) return;
// lock and deallocated handle
std::lock_guard<hal::priority_mutex> sync(m_allocateMutex);
std::lock_guard<hal::priority_mutex> lock(m_handleMutexes[index]);
m_structures[index].reset();
}
template <typename THandle, typename TStruct, int16_t size,
HAL_HandleEnum enumValue>
void LimitedHandleResource<THandle, TStruct, size, enumValue>::ResetHandles() {
{
std::lock_guard<hal::priority_mutex> lock(m_allocateMutex);
for (int i = 0; i < size; i++) {
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
m_structures[i].reset();
}
}
HandleBase::ResetHandles();
}
} // namespace hal

View File

@@ -33,7 +33,7 @@ namespace hal {
*
*/
template <typename THandle, typename TStruct, HAL_HandleEnum enumValue>
class UnlimitedHandleResource {
class UnlimitedHandleResource : public HandleBase {
friend class UnlimitedHandleResourceTest;
public:
@@ -44,6 +44,7 @@ class UnlimitedHandleResource {
THandle Allocate(std::shared_ptr<TStruct> structure);
std::shared_ptr<TStruct> Get(THandle handle);
void Free(THandle handle);
void ResetHandles() override;
private:
std::vector<std::shared_ptr<TStruct>> m_structures;
@@ -58,19 +59,20 @@ THandle UnlimitedHandleResource<THandle, TStruct, enumValue>::Allocate(
for (i = 0; i < m_structures.size(); i++) {
if (m_structures[i] == nullptr) {
m_structures[i] = structure;
return static_cast<THandle>(createHandle(i, enumValue));
return static_cast<THandle>(createHandle(i, enumValue, m_version));
}
}
if (i >= INT16_MAX) return HAL_kInvalidHandle;
m_structures.push_back(structure);
return static_cast<THandle>(createHandle(static_cast<int16_t>(i), enumValue));
return static_cast<THandle>(
createHandle(static_cast<int16_t>(i), enumValue, m_version));
}
template <typename THandle, typename TStruct, HAL_HandleEnum enumValue>
std::shared_ptr<TStruct>
UnlimitedHandleResource<THandle, TStruct, enumValue>::Get(THandle handle) {
int16_t index = getHandleTypedIndex(handle, enumValue);
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
std::lock_guard<hal::priority_mutex> sync(m_handleMutex);
if (index < 0 || index >= static_cast<int16_t>(m_structures.size()))
return nullptr;
@@ -80,9 +82,20 @@ UnlimitedHandleResource<THandle, TStruct, enumValue>::Get(THandle handle) {
template <typename THandle, typename TStruct, HAL_HandleEnum enumValue>
void UnlimitedHandleResource<THandle, TStruct, enumValue>::Free(
THandle handle) {
int16_t index = getHandleTypedIndex(handle, enumValue);
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
std::lock_guard<hal::priority_mutex> sync(m_handleMutex);
if (index < 0 || index >= static_cast<int16_t>(m_structures.size())) return;
m_structures[index].reset();
}
template <typename THandle, typename TStruct, HAL_HandleEnum enumValue>
void UnlimitedHandleResource<THandle, TStruct, enumValue>::ResetHandles() {
{
std::lock_guard<hal::priority_mutex> lock(m_handleMutex);
for (size_t i = 0; i < m_structures.size(); i++) {
m_structures[i].reset();
}
}
HandleBase::ResetHandles();
}
} // namespace hal

View File

@@ -28,7 +28,7 @@ HAL_CompressorHandle HAL_InitializeCompressor(int32_t module, int32_t* status) {
// handle with the module number as the index.
return (HAL_CompressorHandle)createHandle(static_cast<int16_t>(module),
HAL_HandleEnum::Compressor);
HAL_HandleEnum::Compressor, 0);
}
HAL_Bool HAL_CheckCompressorModule(int32_t module) {
@@ -38,7 +38,7 @@ HAL_Bool HAL_CheckCompressorModule(int32_t module) {
HAL_Bool HAL_GetCompressor(HAL_CompressorHandle compressorHandle,
int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor);
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return false;
@@ -53,7 +53,7 @@ HAL_Bool HAL_GetCompressor(HAL_CompressorHandle compressorHandle,
void HAL_SetCompressorClosedLoopControl(HAL_CompressorHandle compressorHandle,
HAL_Bool value, int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor);
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return;
@@ -65,7 +65,7 @@ void HAL_SetCompressorClosedLoopControl(HAL_CompressorHandle compressorHandle,
HAL_Bool HAL_GetCompressorClosedLoopControl(
HAL_CompressorHandle compressorHandle, int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor);
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return false;
@@ -80,7 +80,7 @@ HAL_Bool HAL_GetCompressorClosedLoopControl(
HAL_Bool HAL_GetCompressorPressureSwitch(HAL_CompressorHandle compressorHandle,
int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor);
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return false;
@@ -95,7 +95,7 @@ HAL_Bool HAL_GetCompressorPressureSwitch(HAL_CompressorHandle compressorHandle,
double HAL_GetCompressorCurrent(HAL_CompressorHandle compressorHandle,
int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor);
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return 0;
@@ -109,7 +109,7 @@ double HAL_GetCompressorCurrent(HAL_CompressorHandle compressorHandle,
HAL_Bool HAL_GetCompressorCurrentTooHighFault(
HAL_CompressorHandle compressorHandle, int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor);
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return false;
@@ -123,7 +123,7 @@ HAL_Bool HAL_GetCompressorCurrentTooHighFault(
HAL_Bool HAL_GetCompressorCurrentTooHighStickyFault(
HAL_CompressorHandle compressorHandle, int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor);
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return false;
@@ -137,7 +137,7 @@ HAL_Bool HAL_GetCompressorCurrentTooHighStickyFault(
HAL_Bool HAL_GetCompressorShortedStickyFault(
HAL_CompressorHandle compressorHandle, int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor);
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return false;
@@ -151,7 +151,7 @@ HAL_Bool HAL_GetCompressorShortedStickyFault(
HAL_Bool HAL_GetCompressorShortedFault(HAL_CompressorHandle compressorHandle,
int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor);
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return false;
@@ -165,7 +165,7 @@ HAL_Bool HAL_GetCompressorShortedFault(HAL_CompressorHandle compressorHandle,
HAL_Bool HAL_GetCompressorNotConnectedStickyFault(
HAL_CompressorHandle compressorHandle, int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor);
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return false;
@@ -179,7 +179,7 @@ HAL_Bool HAL_GetCompressorNotConnectedStickyFault(
HAL_Bool HAL_GetCompressorNotConnectedFault(
HAL_CompressorHandle compressorHandle, int32_t* status) {
int16_t index =
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor);
getHandleTypedIndex(compressorHandle, HAL_HandleEnum::Compressor, 0);
if (index == InvalidHandleIndex) {
*status = HAL_HANDLE_ERROR;
return false;

View File

@@ -7,7 +7,51 @@
#include "HAL/handles/HandlesInternal.h"
#include <algorithm>
#include "HAL/cpp/priority_mutex.h"
#include "llvm/SmallVector.h"
namespace hal {
static llvm::SmallVector<HandleBase*, 32> globalHandles;
static priority_mutex globalHandleMutex;
HandleBase::HandleBase() {
std::lock_guard<priority_mutex> lock(globalHandleMutex);
auto index = std::find(globalHandles.begin(), globalHandles.end(), this);
if (index == globalHandles.end()) {
globalHandles.push_back(this);
} else {
*index = this;
}
}
HandleBase::~HandleBase() {
std::lock_guard<priority_mutex> lock(globalHandleMutex);
auto index = std::find(globalHandles.begin(), globalHandles.end(), this);
if (index != globalHandles.end()) {
*index = nullptr;
}
}
void HandleBase::ResetHandles() {
m_version++;
if (m_version > 255) {
m_version = 0;
}
}
void HandleBase::ResetGlobalHandles() {
std::unique_lock<priority_mutex> lock(globalHandleMutex);
for (auto&& i : globalHandles) {
if (i != nullptr) {
lock.unlock();
i->ResetHandles();
lock.lock();
}
}
}
HAL_PortHandle createPortHandle(uint8_t channel, uint8_t module) {
// set last 8 bits, then shift to first 8 bits
HAL_PortHandle handle = static_cast<HAL_PortHandle>(HAL_HandleEnum::Port);
@@ -36,13 +80,16 @@ HAL_PortHandle createPortHandleForSPI(uint8_t channel) {
return handle;
}
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType) {
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType,
int16_t version) {
if (index < 0) return HAL_kInvalidHandle;
uint8_t hType = static_cast<uint8_t>(handleType);
if (hType == 0 || hType > 127) return HAL_kInvalidHandle;
// set last 8 bits, then shift to first 8 bits
HAL_Handle handle = hType;
handle = handle << 24;
handle = handle << 8;
handle += static_cast<uint8_t>(version);
handle = handle << 16;
// add index to set last 16 bits
handle += index;
return handle;