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