WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
HandlesInternal.hpp
Go to the documentation of this file.
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.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <concepts>
10#include <string_view>
11
12#include "wpi/hal/Types.h"
14
15/* General Handle Data Layout
16 * Bits 0-15: Handle Index
17 * Bits 16-23: 8 bit rolling reset detection
18 * Bits 24-30: Handle Type
19 * Bit 31: 1 if handle error, 0 if no error
20 *
21 * Other specialized handles will use different formats, however Bits 24-31 are
22 * always reserved for type and error handling.
23 */
24
25namespace wpi::hal {
26
27namespace detail {
28template <typename T>
29concept HasPreviousAllocation = requires(T a) {
30 { a.previousAllocation } -> std::convertible_to<std::string_view>;
31};
32} // namespace detail
33
34/**
35 * Base for all HAL Handles.
36 */
38 public:
41 HandleBase(const HandleBase&) = delete;
42 HandleBase& operator=(const HandleBase&) = delete;
43 virtual void ResetHandles();
44 static void ResetGlobalHandles();
45
46 protected:
47 int16_t m_version;
48};
49
50constexpr int16_t INVALID_HANDLE_INDEX = -1;
51
52/**
53 * Enum of HAL handle types. Vendors/Teams should use Vendor (12).
54 */
81
82/**
83 * Get the handle index from a handle.
84 *
85 * @param handle the handle
86 * @return the index
87 */
88static inline int16_t getHandleIndex(HAL_Handle handle) {
89 // mask and return last 16 bits
90 return static_cast<int16_t>(handle & 0xffff);
91}
92
93/**
94 * Get the handle type from a handle.
95 *
96 * @param handle the handle
97 * @return the type
98 */
100 // mask first 8 bits and cast to enum
101 return static_cast<HAL_HandleEnum>((handle >> 24) & 0xff);
102}
103
104/**
105 * Get if the handle is a specific type.
106 *
107 * @param handle the handle
108 * @param handleType the type to check
109 * @return true if the type is correct, otherwise false
110 */
111static inline bool isHandleType(HAL_Handle handle, HAL_HandleEnum handleType) {
112 return handleType == getHandleType(handle);
113}
114
115/**
116 * Get if the version of the handle is correct.
117 *
118 * Do not use on the roboRIO, used specifically for the sim to handle resets.
119 *
120 * @param handle the handle
121 * @param version the handle version to check
122 * @return true if the handle is the right version, otherwise false
123 */
124static inline bool isHandleCorrectVersion(HAL_Handle handle, int16_t version) {
125 return (((handle & 0xFF0000) >> 16) & version) == version;
126}
127
128/**
129 * Get if the handle is a correct type and version.
130 *
131 * Note the version is not checked on the roboRIO.
132 *
133 * @param handle the handle
134 * @param enumType the type to check
135 * @param version the handle version to check
136 * @return true if the handle is proper version and type, otherwise
137 * false.
138 */
139inline int16_t getHandleTypedIndex(HAL_Handle handle, HAL_HandleEnum enumType,
140 int16_t version) {
141 if (!isHandleType(handle, enumType)) {
143 }
144#if !defined(__FIRST_SYSTEMCORE__)
145 if (!isHandleCorrectVersion(handle, version)) {
147 }
148#endif
149 return getHandleIndex(handle);
150}
151
152/* specialized functions for Port handle
153 * Port Handle Data Layout
154 * Bits 0-7: Channel Number
155 * Bits 8-15: Module Number
156 * Bits 16-23: Unused
157 * Bits 24-30: Handle Type
158 * Bit 31: 1 if handle error, 0 if no error
159 */
160
161/**
162 * Create a handle for a specific index, type and version.
163 *
164 * Note the version is not checked on the roboRIO.
165 *
166 * @param index the index
167 * @param handleType the handle type
168 * @param version the handle version
169 * @return the created handle
170 */
172 int16_t version);
173} // namespace wpi::hal
@ index
Definition base.h:690
High level class for interfacing with CAN devices conforming to the standard CAN spec.
Definition CAN.hpp:24
Class for sending pulse-width modulation (PWM) signals.
Definition PWM.hpp:21
static void ResetGlobalHandles()
HandleBase & operator=(const HandleBase &)=delete
HandleBase(const HandleBase &)=delete
virtual void ResetHandles()
int16_t m_version
Definition HandlesInternal.hpp:47
Definition HandlesInternal.hpp:29
int32_t HAL_Handle
Definition Types.h:17
Converts a string literal into a format string that will be parsed at compile time and converted into...
Definition printf.h:50
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition Types.hpp:9
HAL_HandleEnum
Enum of HAL handle types.
Definition HandlesInternal.hpp:55
@ UNDEFINED
Definition HandlesInternal.hpp:56
@ COUNTER
Definition HandlesInternal.hpp:63
@ NOTIFIER
Definition HandlesInternal.hpp:59
@ DUTY_CYCLE
Definition HandlesInternal.hpp:72
@ ALERT
Definition HandlesInternal.hpp:79
@ PORT
Definition HandlesInternal.hpp:58
@ COMPRESSOR
Definition HandlesInternal.hpp:66
@ VENDOR
Definition HandlesInternal.hpp:68
@ REV_PH
Definition HandlesInternal.hpp:77
@ CAN_STREAM
Definition HandlesInternal.hpp:78
@ DIO
Definition HandlesInternal.hpp:57
@ CTRE_PDP
Definition HandlesInternal.hpp:75
@ SIMULATION_JNI
Definition HandlesInternal.hpp:69
@ ENCODER
Definition HandlesInternal.hpp:65
@ FPGA_ENCODER
Definition HandlesInternal.hpp:64
@ SOLENOID
Definition HandlesInternal.hpp:67
@ REV_PDH
Definition HandlesInternal.hpp:76
@ DIGITAL_PWM
Definition HandlesInternal.hpp:62
@ ADDRESSABLE_LED
Definition HandlesInternal.hpp:73
@ CTRE_PCM
Definition HandlesInternal.hpp:74
@ SERIAL_PORT
Definition HandlesInternal.hpp:71
@ ANALOG_INPUT
Definition HandlesInternal.hpp:60
int16_t getHandleTypedIndex(HAL_Handle handle, HAL_HandleEnum enumType, int16_t version)
Get if the handle is a correct type and version.
Definition HandlesInternal.hpp:139
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType, int16_t version)
Create a handle for a specific index, type and version.
static bool isHandleCorrectVersion(HAL_Handle handle, int16_t version)
Get if the version of the handle is correct.
Definition HandlesInternal.hpp:124
static int16_t getHandleIndex(HAL_Handle handle)
Get the handle index from a handle.
Definition HandlesInternal.hpp:88
static HAL_HandleEnum getHandleType(HAL_Handle handle)
Get the handle type from a handle.
Definition HandlesInternal.hpp:99
constexpr int16_t INVALID_HANDLE_INDEX
Definition HandlesInternal.hpp:50
static bool isHandleType(HAL_Handle handle, HAL_HandleEnum handleType)
Get if the handle is a specific type.
Definition HandlesInternal.hpp:111
constexpr int HANDLE_TYPE_HAL_BASE
Definition Synchronization.hpp:26