DJMFix.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 21 Dec 2020 16:44:39 +0100
branchv_0
changeset 9 ee976a1d1f0a
parent 8 87dfa7c89294
child 12 15d87fdd6e6c
permissions -rw-r--r--
lock in AlsaBridge
     1 /**
     2  * DJM-Fix
     3  * Copyright © 2020 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 #include <iostream>
    18 #include <iomanip>
    19 #include <thread>
    20 #include <mutex>
    21 #include <atomic>
    22 #include <chrono>
    23 #include <stdexcept>
    24 #include <vector>
    25 
    26 #include "DJMFix.h"
    27 
    28 namespace djmfix {
    29 
    30 using Bytes = std::vector<uint8_t>;
    31 
    32 class DJMFixImpl : public DJMFix {
    33 private:
    34 	MidiSender* midiSender;
    35 	std::thread keepAliveThread;
    36 	std::recursive_mutex midiMutex;
    37 	std::atomic<bool> running{false};
    38 	std::atomic<bool> stopped{false};
    39 	std::atomic<bool> sendKeepAlive{false};
    40 	Bytes seed2;
    41 
    42 	void run() {
    43 		while (!stopped) {
    44 			std::cerr << "DJMFixImpl::run()" << std::endl; // TODO: do not mess STDIO
    45 			if (sendKeepAlive) send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x50, 0x01, 0xf7});
    46 			std::this_thread::sleep_for(std::chrono::milliseconds(200));
    47 		}
    48 	}
    49 
    50 	void send(const MidiMessage& midiMessage) {
    51 		std::lock_guard<std::recursive_mutex> lock(midiMutex);
    52 		midiSender->send(midiMessage);
    53 	}
    54 
    55 	std::string toString(const Bytes& midiMessage) {
    56 		std::stringstream result;
    57 		for (uint8_t b : midiMessage) result << std::hex << std::setw(2) << std::setfill('0') << (int) b;
    58 		return result.str();
    59 	}
    60 
    61 	Bytes normalize(const Bytes& data) {
    62 		if (data.size() % 2) throw std::invalid_argument("data before normalization must have even number of bytes");
    63 		Bytes result;
    64 		result.reserve(data.size() / 2);
    65 		for (size_t i = 0; i < data.size() / 2; i++) result.push_back((data[i * 2] & 0x0F) << 4 | (data[i * 2 + 1] & 0x0F));
    66 		return result;
    67 	}
    68 
    69 	Bytes denormalize(const Bytes& data) {
    70 		Bytes result;
    71 		result.reserve(data.size()*2);
    72 		for (size_t i = 0; i < data.size(); i++) {
    73 			result.push_back(data[i] >> 4);
    74 			result.push_back(data[i] & 0x0F);
    75 		}
    76 		return result;
    77 	}
    78 
    79 	uint32_t fnv32hash(const Bytes& buff) {
    80 		uint32_t hash = 0x811c9dc5;
    81 		for (uint8_t b : buff) hash = ((b^hash) * 0x1000193);
    82 		return hash;
    83 	}
    84 
    85 	Bytes toBytes(const uint32_t value) {
    86 		Bytes result;
    87 		result.reserve(4);
    88 		result.push_back(value >> 24);
    89 		result.push_back(value >> 16);
    90 		result.push_back(value >> 8);
    91 		result.push_back(value >> 0);
    92 		return result;
    93 	}
    94 
    95 	bool equals(Bytes a, Bytes b) {
    96 		if (a.size() != b.size()) return false;
    97 		for (size_t i = 0; i < a.size(); i++) if (a[i] != b[i]) return false;
    98 		return true;
    99 	}
   100 
   101 	template<typename T> std::vector<T> concat(const std::vector<T>& a, const std::vector<T>& b, const std::vector<T>& c = {}) {
   102 		std::vector<T> result;
   103 		result.reserve(a.size() + b.size() + c.size());
   104 		for (size_t i = 0; i < a.size(); i++) result.push_back(a[i]);
   105 		for (size_t i = 0; i < b.size(); i++) result.push_back(b[i]);
   106 		for (size_t i = 0; i < c.size(); i++) result.push_back(c[i]);
   107 		return result;
   108 	}
   109 
   110 	template<typename T> std::vector<T> xOR(const std::vector<T>& a, const std::vector<T>& b) {
   111 		if (a.size() != b.size()) throw std::invalid_argument("xor: both must be the same length");
   112 		std::vector<T> result;
   113 		result.reserve(a.size());
   114 		for (size_t i = 0; i < a.size(); i++) result.push_back(a[i] ^ b[i]);
   115 		return result;
   116 	}
   117 
   118 public:
   119 
   120 	virtual ~DJMFixImpl() override {
   121 		std::cerr << "~DJMFixImpl()" << std::endl; // TODO: do not mess STDIO
   122 		if (running) stop();
   123 	}
   124 
   125 	void setMidiSender(MidiSender* midiSender) {
   126 		std::cerr << "DJMFixImpl::setMidiSender()" << std::endl; // TODO: do not mess STDIO
   127 		this->midiSender = midiSender;
   128 	}
   129 
   130 	virtual void receive(const MidiMessage& midiMessage) override {
   131 		std::cerr << "DJMFixImpl::receive(): size = " << midiMessage.size() << " data = " << toString(midiMessage) << std::endl; // TODO: do not mess STDIO
   132 		std::lock_guard<std::recursive_mutex> lock(midiMutex);
   133 
   134 
   135 		if (midiMessage.size() == 12 && midiMessage[9] == 0x11) {
   136 			send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x12, 0x2a, 0x01, 0x0b, 0x50, 0x69, 0x6f, 0x6e, 0x65, 0x65, 0x72, 0x44, 0x4a, 0x02, 0x0b, 0x72, 0x65, 0x6b, 0x6f, 0x72, 0x64, 0x62, 0x6f, 0x78, 0x03, 0x12, 0x02, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x04, 0x08, 0x00, 0x00, 0x00, 0x00, 0xf7});
   137 		} else if (midiMessage.size() == 54 && midiMessage[9] == 0x13 && midiMessage[33] == 0x04 && midiMessage[43] == 0x03) {
   138 			Bytes hash1(midiMessage.begin() + 35, midiMessage.begin() + 35 + 8);
   139 			seed2 = Bytes(midiMessage.begin() + 45, midiMessage.begin() + 45 + 8);
   140 			hash1 = normalize(hash1);
   141 			seed2 = normalize(seed2);
   142 			std::cerr << "DJMFixImpl::receive(): got message with hash1 = " << toString(hash1) << " and seed2 = " << toString(seed2) << std::endl; // TODO: do not mess STDIO
   143 
   144 			Bytes seed0 = {0x68, 0x01, 0x31, 0xFB};
   145 			Bytes seed1 = {0x29, 0x00, 0x00, 0x00, 0x23, 0x48, 0x00, 0x00};
   146 
   147 			Bytes hash1check = toBytes(fnv32hash(concat(seed1, xOR(seed0, seed2))));
   148 
   149 			if (equals(hash1, hash1check)) {
   150 				std::cerr << "DJMFixImpl::receive(): hash1 verification: OK" << std::endl; // TODO: do not mess STDIO
   151 				Bytes hash2 = toBytes(fnv32hash(concat(seed2, xOR(seed0, seed2))));
   152 				send(concat({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x14, 0x38, 0x01, 0x0b, 0x50, 0x69, 0x6f, 0x6e, 0x65, 0x65, 0x72, 0x44, 0x4a, 0x02, 0x0b, 0x72, 0x65, 0x6b, 0x6f, 0x72, 0x64, 0x62, 0x6f, 0x78, 0x04, 0x0a}, concat(denormalize(hash2),{0x05, 0x16, 0x05, 0x09, 0x0b, 0x05, 0x04, 0x0b, 0x0f, 0x0e, 0x0e, 0x04, 0x04, 0x0a, 0x05, 0x0a, 0x0c, 0x08, 0x0e, 0x04, 0x0c, 0x05, 0xf7})));
   153 			} else {
   154 				std::cerr
   155 						<< "DJMFixImpl::receive(): hash1 verification failed: "
   156 						<< " midiMessage = " << toString(midiMessage)
   157 						<< " seed0 = " << toString(seed0)
   158 						<< " seed1 = " << toString(seed1)
   159 						<< " seed2 = " << toString(seed2)
   160 						<< " hash1 = " << toString(hash1)
   161 						<< " hash1check = " << toString(hash1check)
   162 						<< std::endl;
   163 				// TODO: graceful death
   164 			}
   165 		} else if (midiMessage.size() == 12 && midiMessage[9] == 0x15) {
   166 			sendKeepAlive = true;
   167 		}
   168 
   169 	}
   170 
   171 	void start() override {
   172 		std::cerr << "DJMFixImpl::start()" << std::endl; // TODO: do not mess STDIO
   173 		if (midiSender == nullptr) throw std::logic_error("need a midiSender when starting DJMFix");
   174 
   175 		// TODO: methods for parsing and constructing messages from parts (TLV)
   176 		send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x50, 0x01, 0xf7});
   177 
   178 		keepAliveThread = std::thread(&DJMFixImpl::run, this);
   179 		running = true;
   180 
   181 	}
   182 
   183 	void stop() override {
   184 		stopped = true;
   185 		keepAliveThread.join();
   186 		running = false;
   187 		std::cerr << "DJMFixImpl::stop()" << std::endl; // TODO: do not mess STDIO
   188 	}
   189 };
   190 
   191 DJMFix* create() {
   192 	return new DJMFixImpl();
   193 }
   194 
   195 }