MessageCodec.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 01 Jun 2025 13:18:10 +0200
branchv_0
changeset 20 a08e30243b95
parent 19 4ed672cecc25
permissions -rw-r--r--
Added tag v0.1 for changeset 334b727f7516
     1 /**
     2  * djm-fix
     3  * Copyright © 2025 František Kučera (Frantovo.cz, GlobalCode.info)
     4  *
     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.
     8  *
     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.
    13  *
    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/>.
    16  */
    17 
    18 #include <iostream>
    19 #include <iomanip>
    20 
    21 #include "MessageCodec.h"
    22 
    23 Message MessageCodec::decode(std::vector<uint8_t> data) {
    24 	using ERR = std::invalid_argument;
    25 
    26 	if (data.empty() || data.front() != 0xf0 || data.back() != 0xf7)
    27 		throw ERR("not a MIDI SysEx message");
    28 
    29 	// Manufacturer MIDI SysEx ID Numbers:
    30 	//   00H 40H 05H = AlphaTheta Corporation
    31 	//   00H 40H 06H = Pioneer Corporation
    32 	if (data.size() < 4
    33 			|| data[1] != 0x00
    34 			|| data[2] != 0x40
    35 			|| data[3] != 0x05)
    36 		throw ERR("wrong message Manufacturer MIDI SysEx ID");
    37 
    38 	if (data.size() < 8) throw ERR("missing model");
    39 
    40 	uint8_t model = data[7];
    41 
    42 	if (data.size() < 10) throw ERR("missing message type");
    43 
    44 	MessageType msgType;
    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");
    51 
    52 	if (data.size() < 11) throw ERR("missing message length");
    53 	if (data[10] != data.size() - 10) throw ERR("wrong message length");
    54 
    55 	std::vector<Field> fields;
    56 
    57 	for (size_t start = 11; data.size() > start + 1;) {
    58 		FieldType fieldType;
    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");
    65 
    66 		size_t fieldSize = data[start + 1];
    67 
    68 		if (data.size() < start + fieldSize + 1) throw ERR("field overflow");
    69 		//                                    ^ 0xf7 messsage end
    70 
    71 		std::vector<uint8_t> fieldData(
    72 				data.begin() + start + 2,
    73 				data.begin() + start + fieldSize);
    74 
    75 		fields.push_back({fieldType, fieldData});
    76 
    77 		start += fieldSize;
    78 	}
    79 
    80 	return Message(msgType, model, fields);
    81 }
    82 
    83 std::vector<uint8_t> MessageCodec::encode(Message msg) {
    84 	using ERR = std::invalid_argument;
    85 
    86 	std::vector<uint8_t> data;
    87 
    88 	data.push_back(0xf0);
    89 
    90 	data.push_back(0x00);
    91 	data.push_back(0x40);
    92 	data.push_back(0x05);
    93 
    94 	data.push_back(0x00);
    95 	data.push_back(0x00);
    96 	data.push_back(0x00);
    97 
    98 	data.push_back(msg.model);
    99 	data.push_back(0x00);
   100 	data.push_back((uint8_t) msg.type);
   101 
   102 	uint8_t msgLength = 2; // 2 = type + lenght
   103 	for (const auto& field : msg.fields) msgLength += field.data.size() + 2;
   104 
   105 	data.push_back(msgLength);
   106 
   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());
   113 	}
   114 
   115 	data.push_back(0xf7);
   116 
   117 	return data;
   118 }