Skip to content

Commit 8319a9c

Browse files
committed
Tidy up the midi2::ci namespace.
Move ci_message to the ci namespace and rename just "message". Remove excess namespace qualifications. Move from_le7() etc./ and rename to_array() as to_byte_array() etc.
1 parent fbb1446 commit 8319a9c

File tree

9 files changed

+383
-368
lines changed

9 files changed

+383
-368
lines changed

demo/ci_discovery/ci_discovery.cpp

+21-20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===------------------------------------------------------------------------------------===//
88

99
#include <cstddef>
10+
#include <cstdlib>
1011
#include <format>
1112
#include <iostream>
1213
#include <iterator>
@@ -16,28 +17,28 @@
1617
#include "midi2/ci_types.hpp"
1718
#include "midi2/utils.hpp"
1819

19-
namespace {
20-
21-
auto discovery() {
22-
constexpr midi2::ci::header hdr{
23-
.device_id = 0x7F, .version = 2, .remote_muid = 0, .local_muid = midi2::ci::broadcast_muid};
24-
constexpr midi2::ci::discovery discovery{.manufacturer = {0x12, 0x23, 0x34},
25-
.family = 0x1779,
26-
.model = 0x2B5D,
27-
.version = {0x4E, 0x3C, 0x2A, 0x18},
28-
.capability = 0x7F,
29-
.max_sysex_size = 256,
30-
.output_path_id = 0x71};
31-
std::vector<std::byte> message;
32-
midi2::ci::create_message(std::back_inserter(message), midi2::ci::trivial_sentinel{}, hdr, discovery);
33-
return message;
34-
}
35-
36-
} // end anonymous namespace
37-
3820
int main() {
39-
for (auto const b : discovery()) {
21+
constexpr std::size_t buffer_size = 256;
22+
constexpr midi2::ci::header header{
23+
.device_id = 0x7F, .version = 2, .remote_muid = 0, .local_muid = midi2::ci::broadcast_muid};
24+
constexpr midi2::ci::discovery_reply discovery{.manufacturer = {0x12, 0x23, 0x34},
25+
.family = 0x1779,
26+
.model = 0x2B5D,
27+
.version = {0x4E, 0x3C, 0x2A, 0x18},
28+
.capability = 0x7F,
29+
.max_sysex_size = buffer_size,
30+
.output_path_id = 0};
31+
std::array<std::byte, buffer_size> message{};
32+
auto const first = std::begin(message);
33+
auto const last = std::end(message);
34+
auto const pos = midi2::ci::create_message(first, last, header, discovery);
35+
if (pos == last) {
36+
std::cerr << "Buffer too small\n";
37+
return EXIT_FAILURE;
38+
}
39+
for (auto const b : std::ranges::subrange(first, pos)) {
4040
std::cout << std::format("{:02X} ", midi2::to_underlying(b));
4141
}
4242
std::cout << '\n';
43+
return EXIT_SUCCESS;
4344
}

demo/ci_dispatcher/ci_dispatcher.cpp

+40-41
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55
#include <ctime>
66
#include <format>
77
#include <iostream>
8+
#include <random>
9+
#include <ranges>
810

911
namespace {
1012

11-
consteval std::byte operator""_b(unsigned long long arg) noexcept {
12-
assert(arg < 256);
13-
return static_cast<std::byte>(arg);
14-
}
15-
16-
template <typename Element, std::size_t Size> std::string as_string(std::array<Element, Size> const &arr) {
13+
template <std::ranges::input_range Range> std::string as_string(Range const &range) {
1714
std::string result;
1815
char const *separator = "";
19-
for (auto const v : arr) {
16+
for (auto const v : range) {
2017
result += std::format("{}0x{:X}", separator, v);
2118
separator = ", ";
2219
}
@@ -26,39 +23,41 @@ template <typename Element, std::size_t Size> std::string as_string(std::array<E
2623
} // end anonymous namespace
2724

2825
int main() {
29-
try {
30-
std::srand(static_cast<unsigned>(std::time(nullptr))); // use current time as seed for random generator
31-
auto const local_muid = std::uint32_t{static_cast<unsigned>(std::rand()) % midi2::ci::max_user_muid};
32-
33-
struct context {};
34-
auto dispatcher = midi2::ci::make_function_dispatcher(context{});
35-
dispatcher.config().system.on_check_muid(
36-
[&local_muid](context, std::uint8_t /*group*/, std::uint32_t const muid) { return local_muid == muid; });
37-
dispatcher.config().management.on_discovery(
38-
[](context, midi2::ci::header const &hdr, midi2::ci::discovery const &d) {
39-
std::cout << std::format("device-id=0x{:X}\nversion=0x{:X}\n", hdr.device_id, hdr.version)
40-
<< std::format("remote-MUID=0x{:08X}\nlocal-MUID=0x{:08X}\n\n", hdr.remote_muid, hdr.local_muid);
41-
42-
std::cout << std::format("manufacturer=[{}]\nfamily=0x{:X}\nmodel=0x{:X}\n", as_string(d.manufacturer),
43-
d.family, d.model)
44-
<< std::format("version=[{}]\n", as_string(d.version))
45-
<< std::format("capability=0x{:X}\nmax-sysex-size=0x{:X}\noutput-path-id=0x{:X}\n", d.capability,
46-
d.max_sysex_size, d.output_path_id);
47-
});
48-
49-
std::array const message{0x7E_b, 0x7F_b, 0x0D_b, 0x70_b, 0x02_b, 0x00_b, 0x00_b, 0x00_b, 0x00_b, 0x7F_b,
50-
0x7F_b, 0x7F_b, 0x7F_b, 0x12_b, 0x23_b, 0x34_b, 0x79_b, 0x2E_b, 0x5D_b, 0x56_b,
51-
0x4E_b, 0x3C_b, 0x2A_b, 0x18_b, 0x7F_b, 0x00_b, 0x02_b, 0x00_b, 0x00_b, 0x71_b};
52-
constexpr auto group = std::uint8_t{0xFF};
53-
constexpr auto device_id = 0x7F_b;
54-
dispatcher.start(group, device_id);
55-
for (auto const b : message) {
56-
dispatcher.processMIDICI(b);
57-
}
58-
dispatcher.finish();
59-
} catch (std::exception const &ex) {
60-
std::cerr << "An error occurred: " << ex.what() << '\n';
61-
} catch (...) {
62-
std::cerr << "An unknown error occurred\n";
26+
// Generate a random local MUID.
27+
std::mt19937 engine{std::random_device{}()};
28+
std::uniform_int_distribution<std::uint32_t> distribution;
29+
auto const local_muid = distribution(engine) % midi2::ci::max_user_muid;
30+
31+
struct context {};
32+
// Create a CI dispatcher instance using std::function<> for all of its handler functions.
33+
auto dispatcher = midi2::ci::make_function_dispatcher(context{});
34+
auto &config = dispatcher.config();
35+
// Register a handler for checking whether a message is address to this MUID.
36+
config.system.on_check_muid(
37+
[&local_muid](context, std::uint8_t /*group*/, std::uint32_t const muid) { return local_muid == muid; });
38+
// Register a handler for Discovery messages.
39+
config.management.on_discovery([](context, midi2::ci::header const &hdr, midi2::ci::discovery const &d) {
40+
// Display the header fields
41+
std::cout << std::format("device-id=0x{:X}\nversion=0x{:X}\n", hdr.device_id, hdr.version)
42+
<< std::format("remote-MUID=0x{:08X}\nlocal-MUID=0x{:08X}\n\n", hdr.remote_muid, hdr.local_muid);
43+
44+
// Display the discovery data fields
45+
std::cout << std::format("manufacturer=[{}]\nfamily=0x{:X}\nmodel=0x{:X}\n", as_string(d.manufacturer), d.family,
46+
d.model)
47+
<< std::format("version=[{}]\n", as_string(d.version))
48+
<< std::format("capability=0x{:X}\nmax-sysex-size=0x{:X}\noutput-path-id=0x{:X}\n", d.capability,
49+
d.max_sysex_size, d.output_path_id);
50+
// Send a reply to this message...
51+
});
52+
53+
constexpr std::array message{0x7E, 0x7F, 0x0D, 0x70, 0x02, 0x00, 0x00, 0x00, 0x00, 0x7F,
54+
0x7F, 0x7F, 0x7F, 0x12, 0x23, 0x34, 0x79, 0x2E, 0x5D, 0x56,
55+
0x4E, 0x3C, 0x2A, 0x18, 0x7F, 0x00, 0x02, 0x00, 0x00, 0x71};
56+
constexpr auto group = std::uint8_t{0};
57+
constexpr auto device_id = std::byte{0};
58+
dispatcher.start(group, device_id);
59+
for (auto const b : message) {
60+
dispatcher.processMIDICI(static_cast<std::byte>(b));
6361
}
62+
dispatcher.finish();
6463
}

include/midi2/ci_create_message.hpp

+34-34
Original file line numberDiff line numberDiff line change
@@ -28,143 +28,143 @@ template <typename T> struct type_to_packed {};
2828
struct empty {};
2929
struct not_available {};
3030
template <> struct type_to_packed<discovery> {
31-
static constexpr auto id = ci_message::discovery;
31+
static constexpr auto id = message::discovery;
3232
using v1 = packed::discovery_v1;
3333
using v2 = packed::discovery_v2;
3434
};
3535
template <> struct type_to_packed<discovery_reply> {
36-
static constexpr auto id = ci_message::discovery_reply;
36+
static constexpr auto id = message::discovery_reply;
3737
using v1 = packed::discovery_reply_v1;
3838
using v2 = packed::discovery_reply_v2;
3939
};
4040
template <> struct type_to_packed<endpoint_info> {
41-
static constexpr auto id = ci_message::endpoint_info;
41+
static constexpr auto id = message::endpoint_info;
4242
using v1 = packed::endpoint_info_v1;
4343
using v2 = packed::endpoint_info_v1;
4444
};
4545
template <> struct type_to_packed<endpoint_info_reply> {
46-
static constexpr auto id = ci_message::endpoint_info_reply;
46+
static constexpr auto id = message::endpoint_info_reply;
4747
};
4848
template <> struct type_to_packed<invalidate_muid> {
49-
static constexpr auto id = ci_message::invalidate_muid;
49+
static constexpr auto id = message::invalidate_muid;
5050
using v1 = packed::invalidate_muid_v1;
5151
using v2 = packed::invalidate_muid_v1;
5252
};
5353
template <> struct type_to_packed<ack> {
54-
static constexpr auto id = ci_message::ack;
54+
static constexpr auto id = message::ack;
5555
};
5656
template <> struct type_to_packed<nak> {
57-
static constexpr auto id = ci_message::nak;
57+
static constexpr auto id = message::nak;
5858
using v1 = empty;
5959
using v2 = packed::nak_v2;
6060
};
6161
template <> struct type_to_packed<profile_configuration::added> {
62-
static constexpr auto id = ci_message::profile_added;
62+
static constexpr auto id = message::profile_added;
6363
using v1 = profile_configuration::packed::added_v1;
6464
using v2 = profile_configuration::packed::added_v1;
6565
};
6666
template <> struct type_to_packed<profile_configuration::removed> {
67-
static constexpr auto id = ci_message::profile_removed;
67+
static constexpr auto id = message::profile_removed;
6868
using v1 = profile_configuration::packed::removed_v1;
6969
using v2 = profile_configuration::packed::removed_v1;
7070
};
7171
template <> struct type_to_packed<profile_configuration::details> {
72-
static constexpr auto id = ci_message::profile_details;
72+
static constexpr auto id = message::profile_details;
7373
using v1 = profile_configuration::packed::details_v1;
7474
using v2 = profile_configuration::packed::details_v1;
7575
};
7676
template <> struct type_to_packed<profile_configuration::details_reply> {
77-
static constexpr auto id = ci_message::profile_details_reply;
77+
static constexpr auto id = message::profile_details_reply;
7878
using v1 = profile_configuration::packed::details_reply_v1;
7979
using v2 = profile_configuration::packed::details_reply_v1;
8080
};
8181
template <> struct type_to_packed<profile_configuration::inquiry> {
82-
static constexpr auto id = ci_message::profile_inquiry;
82+
static constexpr auto id = message::profile_inquiry;
8383
using v1 = empty;
8484
using v2 = empty;
8585
};
8686
template <> struct type_to_packed<profile_configuration::inquiry_reply> {
87-
static constexpr auto id = ci_message::profile_inquiry_reply;
87+
static constexpr auto id = message::profile_inquiry_reply;
8888
};
8989
template <> struct type_to_packed<profile_configuration::on> {
90-
static constexpr auto id = ci_message::profile_set_on;
90+
static constexpr auto id = message::profile_set_on;
9191
using v1 = profile_configuration::packed::on_v1;
9292
using v2 = profile_configuration::packed::on_v2;
9393
};
9494
template <> struct type_to_packed<profile_configuration::off> {
95-
static constexpr auto id = ci_message::profile_set_off;
95+
static constexpr auto id = message::profile_set_off;
9696
using v1 = profile_configuration::packed::off_v1;
9797
using v2 = profile_configuration::packed::off_v2;
9898
};
9999
template <> struct type_to_packed<profile_configuration::enabled> {
100-
static constexpr auto id = ci_message::profile_enabled;
100+
static constexpr auto id = message::profile_enabled;
101101
using v1 = profile_configuration::packed::enabled_v1;
102102
using v2 = profile_configuration::packed::enabled_v2;
103103
};
104104
template <> struct type_to_packed<profile_configuration::disabled> {
105-
static constexpr auto id = ci_message::profile_disabled;
105+
static constexpr auto id = message::profile_disabled;
106106
using v1 = profile_configuration::packed::disabled_v1;
107107
using v2 = profile_configuration::packed::disabled_v2;
108108
};
109109
template <> struct type_to_packed<profile_configuration::specific_data> {
110-
static constexpr auto id = ci_message::profile_specific_data;
110+
static constexpr auto id = message::profile_specific_data;
111111
using v1 = profile_configuration::packed::specific_data_v1;
112112
using v2 = profile_configuration::packed::specific_data_v1;
113113
};
114114
template <> struct type_to_packed<property_exchange::capabilities> {
115-
static constexpr auto id = ci_message::pe_capability;
115+
static constexpr auto id = message::pe_capability;
116116
using v1 = property_exchange::packed::capabilities_v1;
117117
using v2 = property_exchange::packed::capabilities_v2;
118118
};
119119
template <> struct type_to_packed<property_exchange::capabilities_reply> {
120-
static constexpr auto id = ci_message::pe_capability_reply;
120+
static constexpr auto id = message::pe_capability_reply;
121121
using v1 = property_exchange::packed::capabilities_reply_v1;
122122
using v2 = property_exchange::packed::capabilities_reply_v2;
123123
};
124124
template <> struct type_to_packed<property_exchange::get> {
125-
static constexpr auto id = ci_message::pe_get;
125+
static constexpr auto id = message::pe_get;
126126
};
127127
template <> struct type_to_packed<property_exchange::get_reply> {
128-
static constexpr auto id = ci_message::pe_get_reply;
128+
static constexpr auto id = message::pe_get_reply;
129129
};
130130
template <> struct type_to_packed<property_exchange::set> {
131-
static constexpr auto id = ci_message::pe_set;
131+
static constexpr auto id = message::pe_set;
132132
};
133133
template <> struct type_to_packed<property_exchange::set_reply> {
134-
static constexpr auto id = ci_message::pe_set_reply;
134+
static constexpr auto id = message::pe_set_reply;
135135
};
136136
template <> struct type_to_packed<property_exchange::subscription> {
137-
static constexpr auto id = ci_message::pe_sub;
137+
static constexpr auto id = message::pe_sub;
138138
};
139139
template <> struct type_to_packed<property_exchange::subscription_reply> {
140-
static constexpr auto id = ci_message::pe_sub_reply;
140+
static constexpr auto id = message::pe_sub_reply;
141141
};
142142
template <> struct type_to_packed<property_exchange::notify> {
143-
static constexpr auto id = ci_message::pe_notify;
143+
static constexpr auto id = message::pe_notify;
144144
};
145145

146146
template <> struct type_to_packed<process_inquiry::capabilities> {
147-
static constexpr auto id = ci_message::pi_capability;
147+
static constexpr auto id = message::pi_capability;
148148
using v1 = empty;
149149
using v2 = empty;
150150
};
151151
template <> struct type_to_packed<process_inquiry::capabilities_reply> {
152-
static constexpr auto id = ci_message::pi_capability_reply;
152+
static constexpr auto id = message::pi_capability_reply;
153153
using v1 = not_available;
154154
using v2 = process_inquiry::packed::capabilities_reply_v2;
155155
};
156156
template <> struct type_to_packed<process_inquiry::midi_message_report> {
157-
static constexpr auto id = ci_message::pi_mm_report;
157+
static constexpr auto id = message::pi_mm_report;
158158
using v1 = not_available;
159159
using v2 = process_inquiry::packed::midi_message_report_v2;
160160
};
161161
template <> struct type_to_packed<process_inquiry::midi_message_report_reply> {
162-
static constexpr auto id = ci_message::pi_mm_report_reply;
162+
static constexpr auto id = message::pi_mm_report_reply;
163163
using v1 = not_available;
164164
using v2 = process_inquiry::packed::midi_message_report_reply_v2;
165165
};
166166
template <> struct type_to_packed<process_inquiry::midi_message_report_end> {
167-
static constexpr auto id = ci_message::pi_mm_report_end;
167+
static constexpr auto id = message::pi_mm_report_end;
168168
using v1 = not_available;
169169
using v2 = empty;
170170
};
@@ -197,7 +197,7 @@ constexpr O write_packed_with_tail(O first, S const last, std::byte const *ptr,
197197
}
198198

199199
template <std::output_iterator<std::byte> O, std::sentinel_for<O> S>
200-
constexpr O write_header(O first, S const last, struct header const &h, ci_message const id) {
200+
constexpr O write_header(O first, S const last, struct header const &h, message const id) {
201201
auto hdr = static_cast<packed::header>(h);
202202
hdr.sub_id_2 = static_cast<std::byte>(id);
203203
return details::safe_copy(first, last, hdr);
@@ -214,7 +214,7 @@ constexpr O write_header_body(O first, S const last, header const &hdr, Internal
214214

215215
template <std::output_iterator<std::byte> O, std::sentinel_for<O> S, property_exchange::property_exchange_type Pet>
216216
constexpr O write_pe(O first, S const last, header const &hdr, property_exchange::property_exchange<Pet> const &pe,
217-
ci_message const id) {
217+
message const id) {
218218
first = details::write_header(first, last, hdr, id);
219219

220220
using property_exchange::packed::property_exchange_pt1;

0 commit comments

Comments
 (0)