Ocean
Loading...
Searching...
No Matches
system/usb/Descriptor.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_SYSTEM_USB_DESCRIPTOR_H
9#define META_OCEAN_SYSTEM_USB_DESCRIPTOR_H
10
12
13namespace Ocean
14{
15
16namespace System
17{
18
19namespace USB
20{
21
22constexpr uint8_t CS_INTERFACE = 0x24u;
23
24/**
25 * This class implements the base class for all interface descriptors.
26 * @ingroup systemusb
27 */
28class OCEAN_SYSTEM_USB_EXPORT Descriptor
29{
30 public:
31
32 /**
33 * Returns a custom globally unique identifier (GUID).
34 * @return The defined GUID
35 * @tparam tValue0 The first 4 bytes of the GUID, with range [0, infinity)
36 * @tparam tValue1 The next 2 bytes of the GUID, with range [0, infinity)
37 * @tparam tValue2 The next 2 bytes of the GUID, with range [0, infinity)
38 * @tparam tValue3 The next 2 bytes of the GUID, with range [0, infinity)
39 * @tparam tValue4 The last 6 bytes of the GUID, with range [0, 2^48-1]
40 */
41 template <uint32_t tValue0, uint16_t tValue1, uint16_t tValue2, uint16_t tValue3, uint64_t tValue4>
42 static constexpr std::array<uint8_t, 16u> createGUID();
43
44 /**
45 * Returns a custom globally unique identifier (GUID).
46 * @param value0 The first 4 bytes of the GUID, with range [0, infinity)
47 * @param value1 The next 2 bytes of the GUID, with range [0, infinity)
48 * @param value2 The next 2 bytes of the GUID, with range [0, infinity)
49 * @param value3 The next 2 bytes of the GUID, with range [0, infinity)
50 * @param value4 The last 6 bytes of the GUID, with range [0, 2^48-1]
51 * @return The defined GUID
52 */
53 static constexpr std::array<uint8_t, 16u> createGUID(const uint32_t value0, const uint16_t value1, const uint16_t value2, const uint16_t value3, const uint64_t value4);
54
55 /**
56 * Returns a string representation of the given GUID.
57 * @param guid The GUID to return the string representation for
58 * @return The string representation of the GUID
59 */
60 static std::string guid2string(const uint8_t* guid);
61
62 /**
63 * Converts a time interval (in 100ns) to frequency in Hz.
64 * @param timeInterval The time interval to convert, in 100ns, with range [1, infinity)
65 * @return The resulting frequency in Hz, -1 if the interval is invalid
66 */
67 static double interval2frequency(const uint32_t timeInterval);
68
69 /**
70 * Converts a frequency in Hz to time interval in 100ns.
71 * @param frequency The frequency to convert, with range (0.01, infinity)
72 * @return The resulting time interval in 100ns, 0 if the frequency is invalid
73 */
74 static uint32_t frequency2interval(const double frequency);
75
76 protected:
77
78 /**
79 * Copies the value from a given buffer.
80 * @param offset The byte offset in the buffer, with range [0, bufferSizeBytes - 1]
81 * @param value The value to which the buffer memory is copied
82 * @param buffer The buffer from which the value is copied, must be valid
83 * @tparam T The data type of the value
84 */
85 template <typename T>
86 static void value(const size_t offset, T& value, const void* buffer);
87
88 /**
89 * Invalidates a descriptor.
90 * @param descriptor The invalidated descriptor
91 * @tparam TDescriptor The type of the descriptor
92 */
93 template <typename TDescriptor>
94 static void invalidate(TDescriptor& descriptor);
95
96 /**
97 * Returns 8 bits from a given value which can be shifted right.
98 * @param value The value from which the bits are extracted
99 * @param bytes The number of bytes the value will be shifted right, with range [0, infinity)
100 * @return The extracted 8 bits (a byte)
101 */
102 template <typename T>
103 static constexpr uint8_t shift(const T& value, const unsigned int bytes);
104};
105
106template <uint32_t tValue0, uint16_t tValue1, uint16_t tValue2, uint16_t tValue3, uint64_t tValue4>
107constexpr std::array<uint8_t, 16u> Descriptor::createGUID()
108{
109 static_assert(tValue4 <= 0xFFFFFFFFFFFFull);
110
111 return {shift(tValue0, 3), shift(tValue0, 2), shift(tValue0, 1), shift(tValue0, 0),
112 shift(tValue1, 1), shift(tValue1, 0),
113 shift(tValue2, 1), shift(tValue2, 0),
114 shift(tValue3, 1), shift(tValue3, 0),
115 shift(tValue4, 5), shift(tValue4, 4), shift(tValue4, 3), shift(tValue4, 2), shift(tValue4, 1), shift(tValue4, 0)};
116}
117
118constexpr std::array<uint8_t, 16u> Descriptor::createGUID(const uint32_t value0, const uint16_t value1, const uint16_t value2, const uint16_t value3, const uint64_t value4)
119{
120 ocean_assert(value4 <= 0xFFFFFFFFFFFFull);
121
122 return {shift(value0, 3), shift(value0, 2), shift(value0, 1), shift(value0, 0),
123 shift(value1, 1), shift(value1, 0),
124 shift(value2, 1), shift(value2, 0),
125 shift(value3, 1), shift(value3, 0),
126 shift(value4, 5), shift(value4, 4), shift(value4, 3), shift(value4, 2), shift(value4, 1), shift(value4, 0)};
127}
128
129template <typename T>
130void Descriptor::value(const size_t offset, T& value, const void* buffer)
131{
132 memcpy(&value, (const uint8_t*)(buffer) + offset, sizeof(T));
133}
134
135template <typename TDescriptor>
136void Descriptor::invalidate(TDescriptor& descriptor)
137{
138 descriptor.bLength_ = 0u;
139 descriptor.bDescriptorType_ = 0u;
140 descriptor.bDescriptorSubtype_ = 0u;
141
142 ocean_assert(!descriptor.isValid());
143}
144
145template <typename T>
146constexpr uint8_t Descriptor::shift(const T& value, const unsigned int bytes)
147{
148 return uint8_t((value >> T(bytes * 8)) & T(0xFF));
149}
150
151}
152
153}
154
155}
156
157#endif // META_OCEAN_SYSTEM_USB_DESCRIPTOR_H
This class implements the base class for all interface descriptors.
Definition system/usb/Descriptor.h:29
static std::string guid2string(const uint8_t *guid)
Returns a string representation of the given GUID.
static constexpr uint8_t shift(const T &value, const unsigned int bytes)
Returns 8 bits from a given value which can be shifted right.
Definition system/usb/Descriptor.h:146
static double interval2frequency(const uint32_t timeInterval)
Converts a time interval (in 100ns) to frequency in Hz.
static constexpr std::array< uint8_t, 16u > createGUID()
Returns a custom globally unique identifier (GUID).
Definition system/usb/Descriptor.h:107
static void value(const size_t offset, T &value, const void *buffer)
Copies the value from a given buffer.
Definition system/usb/Descriptor.h:130
static void invalidate(TDescriptor &descriptor)
Invalidates a descriptor.
Definition system/usb/Descriptor.h:136
static uint32_t frequency2interval(const double frequency)
Converts a frequency in Hz to time interval in 100ns.
constexpr uint8_t CS_INTERFACE
Definition system/usb/Descriptor.h:22
The namespace covering the entire Ocean framework.
Definition Accessor.h:15