3 * Copyright © 2025 František Kučera (Frantovo.cz, GlobalCode.info)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "MessageCodec.h"
23 Message MessageCodec::decode(std::vector<uint8_t> data) {
24 using ERR = std::invalid_argument;
26 if (data.empty() || data.front() != 0xf0 || data.back() != 0xf7)
27 throw ERR("not a MIDI SysEx message");
29 // Manufacturer MIDI SysEx ID Numbers:
30 // 00H 40H 05H = AlphaTheta Corporation
31 // 00H 40H 06H = Pioneer Corporation
36 throw ERR("wrong message Manufacturer MIDI SysEx ID");
38 if (data.size() < 8) throw ERR("missing model");
40 uint8_t model = data[7];
42 if (data.size() < 10) throw ERR("missing message type");
45 if /**/ (data[9] == 0x11) msgType = MessageType::D11_GREETING;
46 else if (data[9] == 0x12) msgType = MessageType::H12_SEED1;
47 else if (data[9] == 0x13) msgType = MessageType::D13_HASH1_SEED2;
48 else if (data[9] == 0x14) msgType = MessageType::H14_HASH2;
49 else if (data[9] == 0x15) msgType = MessageType::D15_CONFIRMATION;
50 else throw ERR("unsupported message type");
52 if (data.size() < 11) throw ERR("missing message length");
53 if (data[10] != data.size() - 10) throw ERR("wrong message length");
55 std::vector<Field> fields;
57 for (size_t start = 11; data.size() > start + 1;) {
59 if /**/ (data[start] == 0x01) fieldType = FieldType::F01;
60 else if (data[start] == 0x02) fieldType = FieldType::F02;
61 else if (data[start] == 0x03) fieldType = FieldType::F03;
62 else if (data[start] == 0x04) fieldType = FieldType::F04;
63 else if (data[start] == 0x05) fieldType = FieldType::F05;
64 else throw ERR("unsupported field type");
66 size_t fieldSize = data[start + 1];
68 if (data.size() < start + fieldSize + 1) throw ERR("field overflow");
69 // ^ 0xf7 messsage end
71 std::vector<uint8_t> fieldData(
72 data.begin() + start + 2,
73 data.begin() + start + fieldSize);
75 fields.push_back({fieldType, fieldData});
80 return Message(msgType, model, fields);
83 std::vector<uint8_t> MessageCodec::encode(Message msg) {
84 using ERR = std::invalid_argument;
86 std::vector<uint8_t> data;
98 data.push_back(msg.model);
100 data.push_back((uint8_t) msg.type);
102 uint8_t msgLength = 2; // 2 = type + lenght
103 for (const auto& field : msg.fields) msgLength += field.data.size() + 2;
105 data.push_back(msgLength);
107 for (const auto& field : msg.fields) {
108 size_t fieldSize = field.data.size() + 2;
109 if (fieldSize > 256) throw ERR("message field too long");
110 data.push_back((uint8_t) field.type);
111 data.push_back((uint8_t) (fieldSize));
112 data.insert(data.end(), field.data.begin(), field.data.end());
115 data.push_back(0xf7);