WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
HandlesInternal.h
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
10
11#include "hal/Types.h"
12
13/* General Handle Data Layout
14 * Bits 0-15: Handle Index
15 * Bits 16-23: 8 bit rolling reset detection
16 * Bits 24-30: Handle Type
17 * Bit 31: 1 if handle error, 0 if no error
18 *
19 * Other specialized handles will use different formats, however Bits 24-31 are
20 * always reserved for type and error handling.
21 */
22
23namespace hal {
24
25/**
26 * Base for all HAL Handles.
27 */
29 public:
32 HandleBase(const HandleBase&) = delete;
33 HandleBase& operator=(const HandleBase&) = delete;
34 virtual void ResetHandles();
35 static void ResetGlobalHandles();
36
37 protected:
38 int16_t m_version;
39};
40
41constexpr int16_t InvalidHandleIndex = -1;
42
43/**
44 * Enum of HAL handle types. Vendors/Teams should use Vendor (17).
45 */
46enum class HAL_HandleEnum {
47 Undefined = 0,
49 Port = 2,
50 Notifier = 3,
51 Interrupt = 4,
52 AnalogOutput = 5,
53 AnalogInput = 6,
54 AnalogTrigger = 7,
55 Relay = 8,
56 PWM = 9,
57 DigitalPWM = 10,
58 Counter = 11,
59 FPGAEncoder = 12,
60 Encoder = 13,
61 Compressor = 14,
62 Solenoid = 15,
63 AnalogGyro = 16,
64 Vendor = 17,
65 SimulationJni = 18,
66 CAN = 19,
67 SerialPort = 20,
68 DutyCycle = 21,
69 DMA = 22,
70 AddressableLED = 23,
71 CTREPCM = 24,
72 CTREPDP = 25,
73 REVPDH = 26,
74 REVPH = 27,
75 CANStream = 28,
76};
77
78/**
79 * Get the handle index from a handle.
80 *
81 * @param handle the handle
82 * @return the index
83 */
84static inline int16_t getHandleIndex(HAL_Handle handle) {
85 // mask and return last 16 bits
86 return static_cast<int16_t>(handle & 0xffff);
87}
88
89/**
90 * Get the handle type from a handle.
91 *
92 * @param handle the handle
93 * @return the type
94 */
96 // mask first 8 bits and cast to enum
97 return static_cast<HAL_HandleEnum>((handle >> 24) & 0xff);
98}
99
100/**
101 * Get if the handle is a specific type.
102 *
103 * @param handle the handle
104 * @param handleType the type to check
105 * @return true if the type is correct, otherwise false
106 */
107static inline bool isHandleType(HAL_Handle handle, HAL_HandleEnum handleType) {
108 return handleType == getHandleType(handle);
109}
110
111/**
112 * Get if the version of the handle is correct.
113 *
114 * Do not use on the roboRIO, used specifically for the sim to handle resets.
115 *
116 * @param handle the handle
117 * @param version the handle version to check
118 * @return true if the handle is the right version, otherwise false
119 */
120static inline bool isHandleCorrectVersion(HAL_Handle handle, int16_t version) {
121 return (((handle & 0xFF0000) >> 16) & version) == version;
122}
123
124/**
125 * Get if the handle is a correct type and version.
126 *
127 * Note the version is not checked on the roboRIO.
128 *
129 * @param handle the handle
130 * @param enumType the type to check
131 * @param version the handle version to check
132 * @return true if the handle is proper version and type, otherwise
133 * false.
134 */
135inline int16_t getHandleTypedIndex(HAL_Handle handle, HAL_HandleEnum enumType,
136 int16_t version) {
137 if (!isHandleType(handle, enumType)) {
138 return InvalidHandleIndex;
139 }
140#if !defined(__FRC_SYSTEMCORE__)
141 if (!isHandleCorrectVersion(handle, version)) {
142 return InvalidHandleIndex;
143 }
144#endif
145 return getHandleIndex(handle);
146}
147
148/* specialized functions for Port handle
149 * Port Handle Data Layout
150 * Bits 0-7: Channel Number
151 * Bits 8-15: Module Number
152 * Bits 16-23: Unused
153 * Bits 24-30: Handle Type
154 * Bit 31: 1 if handle error, 0 if no error
155 */
156
157/**
158 * Create a handle for a specific index, type and version.
159 *
160 * Note the version is not checked on the roboRIO.
161 *
162 * @param index the index
163 * @param handleType the handle type
164 * @param version the handle version
165 * @return the created handle
166 */
168 int16_t version);
169} // namespace hal
Base for all HAL Handles.
Definition HandlesInternal.h:28
HandleBase & operator=(const HandleBase &)=delete
virtual void ResetHandles()
static void ResetGlobalHandles()
HandleBase(const HandleBase &)=delete
int16_t m_version
Definition HandlesInternal.h:38
int32_t HAL_Handle
Definition Types.h:17
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition SimCallbackRegistry.h:16
static bool isHandleCorrectVersion(HAL_Handle handle, int16_t version)
Get if the version of the handle is correct.
Definition HandlesInternal.h:120
HAL_HandleEnum
Enum of HAL handle types.
Definition HandlesInternal.h:46
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType, int16_t version)
Create a handle for a specific index, type and version.
constexpr int16_t InvalidHandleIndex
Definition HandlesInternal.h:41
static bool isHandleType(HAL_Handle handle, HAL_HandleEnum handleType)
Get if the handle is a specific type.
Definition HandlesInternal.h:107
static HAL_HandleEnum getHandleType(HAL_Handle handle)
Get the handle type from a handle.
Definition HandlesInternal.h:95
static int16_t getHandleIndex(HAL_Handle handle)
Get the handle index from a handle.
Definition HandlesInternal.h:84
int16_t getHandleTypedIndex(HAL_Handle handle, HAL_HandleEnum enumType, int16_t version)
Get if the handle is a correct type and version.
Definition HandlesInternal.h:135
constexpr int kHandleTypeHALBase
Definition Synchronization.h:45