Skip to content

Commit e009b4e

Browse files
committed
change report selector table into properties table, including size
1 parent a4e56ef commit e009b4e

6 files changed

Lines changed: 54 additions & 25 deletions

File tree

hid-rp/hid/rdf/exception.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ struct ex_report_table_invalid_size : public exception
5555
{}
5656
};
5757

58+
struct ex_report_invalid_size : public exception
59+
{
60+
constexpr ex_report_invalid_size()
61+
: exception("report size invalid")
62+
{}
63+
};
64+
5865
/// @brief This class is trying to follow the conventions established by this document:
5966
/// https://usb.org/sites/default/files/hidpar.pdf
6067
class parser_exception : public exception

hid-rp/hid/report.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ class selector
103103
std::array<std::uint8_t, 2> storage_{};
104104
};
105105

106+
struct properties
107+
{
108+
report::selector selector;
109+
std::uint16_t size; // in bytes
110+
};
111+
106112
struct id_base
107113
{
108114
report::id id;

hid-rp/hid/report_protocol.hpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ struct report_protocol_properties
152152
auto& report_sizes = bit_sizes_by_type(type);
153153
auto begin = report_sizes.begin();
154154
size_type max_size = *std::max_element(++begin, report_sizes.end()) / 8;
155-
return (max_size > 0) ? sizeof(report::id) + max_size : 0UL;
155+
return (max_size > 0) ? sizeof(report::id) + max_size : 0;
156156
}
157157

158158
[[nodiscard]] constexpr bool uses_report_ids() const { return max_report_id() > 0; }
@@ -185,7 +185,7 @@ struct report_protocol_properties
185185
}
186186

187187
template <std::size_t N>
188-
constexpr void fill_report_selector_table(std::array<report::selector, N>& table) const
188+
constexpr void fill_report_properties_table(std::array<report::properties, N>& table) const
189189
{
190190
HID_RP_ASSERT(table.size() == report_count(), ex_report_table_invalid_size);
191191
auto table_it = table.begin();
@@ -195,7 +195,13 @@ struct report_protocol_properties
195195
{
196196
if (report_bit_sizes_[type][id] > 0)
197197
{
198-
*table_it = report::selector(static_cast<report::type>(type + 1), id);
198+
std::size_t byte_size =
199+
(id != 0) * sizeof(report::id) + report_bit_sizes_[type][id] / 8;
200+
HID_RP_ASSERT(byte_size <= std::numeric_limits<std::uint16_t>::max(),
201+
ex_report_invalid_size);
202+
*table_it = report::properties{
203+
report::selector(static_cast<report::type>(type + 1), id),
204+
static_cast<std::uint16_t>(byte_size)};
199205
++table_it;
200206
}
201207
}
@@ -457,17 +463,17 @@ struct report_protocol : public report_protocol_properties
457463
}
458464
};
459465

460-
/// @brief Create a table that contains all report selectors defined by the report descriptor,
466+
/// @brief Create a table that contains all report properties defined by the report descriptor,
461467
/// for correctly sizing and filling GATT HID attributes.
462468
/// @tparam Data: the descriptor array, acquired e.g. from a @ref hid::rdf::descriptor call
463-
/// @return a std::array<hid::report::selector, N> table listing the report selectors used by the
469+
/// @return a std::array<hid::report::properties, N> table listing the report properties used by the
464470
/// report descriptor
465471
template <auto Data>
466-
consteval auto make_report_selector_table()
472+
consteval auto make_report_properties_table()
467473
{
468474
constexpr report_protocol::parser<> parser{rdf::ce_descriptor_view::from_descriptor<Data>()};
469-
std::array<report::selector, parser.report_count()> table;
470-
parser.fill_report_selector_table(table);
475+
std::array<report::properties, parser.report_count()> table;
476+
parser.fill_report_properties_table(table);
471477
return table;
472478
}
473479

test/keyboard.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ SUITE(keyboard_)
2121
static_assert(sizeof(output_report<0>) == 1);
2222
static_assert(not rp0.uses_report_ids());
2323

24-
constexpr auto table0 = hid::make_report_selector_table<app_report_descriptor<0>()>();
24+
constexpr auto table0 = hid::make_report_properties_table<app_report_descriptor<0>()>();
2525
static_assert(table0.size() == 2);
26-
static_assert(table0[0] == keys_input_report<0>::selector());
27-
static_assert(table0[1] == output_report<0>::selector());
26+
static_assert(table0[0].selector == keys_input_report<0>::selector());
27+
static_assert(table0[0].size == sizeof(keys_input_report<0>));
28+
static_assert(table0[1].selector == output_report<0>::selector());
29+
static_assert(table0[1].size == sizeof(output_report<0>));
2830

2931
// test both compile-time and runtime
3032
static_assert(hid::rdf::get_application_usage_id(rp0.descriptor) ==
@@ -44,10 +46,12 @@ SUITE(keyboard_)
4446
static_assert(rp5.max_output_size == 2);
4547
static_assert(rp5.uses_report_ids());
4648

47-
constexpr auto table5 = hid::make_report_selector_table<app_report_descriptor<5>()>();
49+
constexpr auto table5 = hid::make_report_properties_table<app_report_descriptor<5>()>();
4850
static_assert(table5.size() == 2);
49-
static_assert(table5[0] == keys_input_report<5>::selector());
50-
static_assert(table5[1] == output_report<5>::selector());
51+
static_assert(table5[0].selector == keys_input_report<5>::selector());
52+
static_assert(table5[0].size == sizeof(keys_input_report<5>));
53+
static_assert(table5[1].selector == output_report<5>::selector());
54+
static_assert(table5[1].size == sizeof(output_report<5>));
5155

5256
// test both compile-time and runtime
5357
static_assert(hid::rdf::get_application_usage_id(rp5.descriptor) ==

test/lamparray.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ SUITE(lamparray_)
3333
static_assert(rp.max_output_size == 0);
3434
static_assert(rp.uses_report_ids());
3535

36-
constexpr auto table0 = hid::make_report_selector_table<desc>();
36+
constexpr auto table0 = hid::make_report_properties_table<desc>();
3737
static_assert(table0.size() == rp.feature_report_count);
3838

3939
// test both compile-time and runtime

test/mouse.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ SUITE(mouse_)
5959
static_assert(rp0.max_output_size == 0);
6060
static_assert(not rp0.uses_report_ids());
6161

62-
constexpr auto table0 = hid::make_report_selector_table<app_report_descriptor<0>()>();
62+
constexpr auto table0 = hid::make_report_properties_table<app_report_descriptor<0>()>();
6363
static_assert(table0.size() == 1);
64-
static_assert(table0[0] == report<0>::selector());
64+
static_assert(table0[0].selector == report<0>::selector());
65+
static_assert(table0[0].size == sizeof(report<0>));
6566

6667
// test both compile-time and runtime
6768
static_assert(hid::rdf::get_application_usage_id(rp0.descriptor) ==
@@ -80,9 +81,10 @@ SUITE(mouse_)
8081
static_assert(rp5.max_output_size == 0);
8182
static_assert(rp5.uses_report_ids());
8283

83-
constexpr auto table5 = hid::make_report_selector_table<app_report_descriptor<5>()>();
84+
constexpr auto table5 = hid::make_report_properties_table<app_report_descriptor<5>()>();
8485
static_assert(table5.size() == 1);
85-
static_assert(table5[0] == report<5>::selector());
86+
static_assert(table5[0].selector == report<5>::selector());
87+
static_assert(table5[0].size == sizeof(report<5>));
8688

8789
// test both compile-time and runtime
8890
static_assert(hid::rdf::get_application_usage_id(rp5.descriptor) ==
@@ -104,10 +106,12 @@ SUITE(mouse_)
104106
static_assert(rp0.max_output_size == 0);
105107
static_assert(not rp0.uses_report_ids());
106108

107-
constexpr auto table0 = hid::make_report_selector_table<high_res_mouse_desc<0>()>();
109+
constexpr auto table0 = hid::make_report_properties_table<high_res_mouse_desc<0>()>();
108110
static_assert(table0.size() == 2);
109-
static_assert(table0[0] == report<0>::selector());
110-
static_assert(table0[1] == resolution_multiplier_report<120, 0>::selector());
111+
static_assert(table0[0].selector == report<0>::selector());
112+
static_assert(table0[0].size == rp0.max_input_size);
113+
static_assert(table0[1].selector == resolution_multiplier_report<120, 0>::selector());
114+
static_assert(table0[1].size == sizeof(resolution_multiplier_report<120, 0>));
111115

112116
// test both compile-time and runtime
113117
static_assert(hid::rdf::get_application_usage_id(rp0.descriptor) ==
@@ -126,10 +130,12 @@ SUITE(mouse_)
126130
static_assert(rp5.max_output_size == 0);
127131
static_assert(rp5.uses_report_ids());
128132

129-
constexpr auto table5 = hid::make_report_selector_table<high_res_mouse_desc<5>()>();
133+
constexpr auto table5 = hid::make_report_properties_table<high_res_mouse_desc<5>()>();
130134
static_assert(table5.size() == 2);
131-
static_assert(table5[0] == report<5>::selector());
132-
static_assert(table5[1] == resolution_multiplier_report<120, 5>::selector());
135+
static_assert(table5[0].selector == report<5>::selector());
136+
static_assert(table5[0].size == rp5.max_input_size);
137+
static_assert(table5[1].selector == resolution_multiplier_report<120, 5>::selector());
138+
static_assert(table5[1].size == sizeof(resolution_multiplier_report<120, 5>));
133139

134140
// test both compile-time and runtime
135141
static_assert(hid::rdf::get_application_usage_id(rp5.descriptor) ==

0 commit comments

Comments
 (0)