DJMFix.cpp
branchv_0
changeset 6 bddcf2bf29f2
parent 5 ef8f4023e32e
child 7 889b4b8737bd
     1.1 --- a/DJMFix.cpp	Sat Dec 19 17:33:16 2020 +0100
     1.2 +++ b/DJMFix.cpp	Sat Dec 19 23:59:39 2020 +0100
     1.3 @@ -17,18 +17,23 @@
     1.4  #include <iostream>
     1.5  #include <iomanip>
     1.6  #include <thread>
     1.7 +#include <mutex>
     1.8  #include <atomic>
     1.9  #include <chrono>
    1.10  #include <stdexcept>
    1.11 +#include <vector>
    1.12  
    1.13  #include "DJMFix.h"
    1.14  
    1.15  namespace djmfix {
    1.16  
    1.17 +using Bytes = std::vector<uint8_t>;
    1.18 +
    1.19  class DJMFixImpl : public DJMFix {
    1.20  private:
    1.21  	MidiSender* midiSender;
    1.22  	std::thread keepAliveThread;
    1.23 +	std::recursive_mutex midiMutex;
    1.24  	std::atomic<bool> running{false};
    1.25  	std::atomic<bool> stopped{false};
    1.26  
    1.27 @@ -40,13 +45,64 @@
    1.28  		}
    1.29  	}
    1.30  
    1.31 -	// TODO: remove
    1.32 -	std::string toString(const MidiMessage& midiMessage) {
    1.33 +	void send(const MidiMessage& midiMessage) {
    1.34 +		std::lock_guard<std::recursive_mutex> lock(midiMutex);
    1.35 +		midiSender->send(midiMessage);
    1.36 +	}
    1.37 +
    1.38 +	std::string toString(const Bytes& midiMessage) {
    1.39  		std::stringstream result;
    1.40  		for (uint8_t b : midiMessage) result << std::hex << std::setw(2) << std::setfill('0') << (int) b;
    1.41  		return result.str();
    1.42  	}
    1.43  
    1.44 +	Bytes normalize(const Bytes& data) {
    1.45 +		if (data.size() % 2) throw std::invalid_argument("data before normalization must have even number of bytes");
    1.46 +		Bytes result;
    1.47 +		result.reserve(data.size() / 2);
    1.48 +		for (size_t i = 0; i < data.size() / 2; i++) result.push_back((data[i * 2] & 0x0F) << 4 | (data[i * 2 + 1] & 0x0F));
    1.49 +		return result;
    1.50 +	}
    1.51 +
    1.52 +	uint32_t fnv32hash(const Bytes& buff) {
    1.53 +		uint32_t hash = 0x811c9dc5;
    1.54 +		for (uint8_t b : buff) hash = ((b^hash) * 0x1000193);
    1.55 +		return hash;
    1.56 +	}
    1.57 +
    1.58 +	Bytes toBytes(const uint32_t value) {
    1.59 +		Bytes result;
    1.60 +		result.reserve(4);
    1.61 +		result.push_back(value >> 24);
    1.62 +		result.push_back(value >> 16);
    1.63 +		result.push_back(value >> 8);
    1.64 +		result.push_back(value >> 0);
    1.65 +		return result;
    1.66 +	}
    1.67 +
    1.68 +	bool equals(Bytes a, Bytes b) {
    1.69 +		if (a.size() != b.size()) return false;
    1.70 +		for (size_t i = 0; i < a.size(); i++) if (a[i] != b[i]) return false;
    1.71 +		return true;
    1.72 +	}
    1.73 +
    1.74 +	template<typename T> std::vector<T> concat(const std::vector<T>& a, const std::vector<T>& b, const std::vector<T>& c = {}) {
    1.75 +		std::vector<T> result;
    1.76 +		result.reserve(a.size() + b.size() + c.size());
    1.77 +		for (size_t i = 0; i < a.size(); i++) result.push_back(a[i]);
    1.78 +		for (size_t i = 0; i < b.size(); i++) result.push_back(b[i]);
    1.79 +		for (size_t i = 0; i < c.size(); i++) result.push_back(c[i]);
    1.80 +		return result;
    1.81 +	}
    1.82 +
    1.83 +	template<typename T> std::vector<T> xOR(const std::vector<T>& a, const std::vector<T>& b) {
    1.84 +		if (a.size() != b.size()) throw std::invalid_argument("xor: both must be the same length");
    1.85 +		std::vector<T> result;
    1.86 +		result.reserve(a.size());
    1.87 +		for (size_t i = 0; i < a.size(); i++) result.push_back(a[i] ^ b[i]);
    1.88 +		return result;
    1.89 +	}
    1.90 +
    1.91  public:
    1.92  
    1.93  	virtual ~DJMFixImpl() override {
    1.94 @@ -59,11 +115,27 @@
    1.95  		this->midiSender = midiSender;
    1.96  	}
    1.97  
    1.98 -	virtual void receive(MidiMessage midiMessage) override {
    1.99 +	virtual void receive(const MidiMessage& midiMessage) override {
   1.100  		std::cerr << "DJMFixImpl::receive(): size = " << midiMessage.size() << " data = " << toString(midiMessage) << std::endl; // TODO: do not mess STDIO
   1.101 +		std::lock_guard<std::recursive_mutex> lock(midiMutex);
   1.102  
   1.103 -		if (midiMessage.size() == 54 && midiMessage[9] == 0x13) {
   1.104 -			std::cerr << "DJMFixImpl::receive(): got message with HashA and SeedE" << std::endl; // TODO: do not mess STDIO
   1.105 +		if (midiMessage.size() == 54 && midiMessage[9] == 0x13 && midiMessage[33] == 0x04 && midiMessage[43] == 0x03) {
   1.106 +			Bytes hash1(midiMessage.begin() + 35, midiMessage.begin() + 35 + 8);
   1.107 +			Bytes seed2(midiMessage.begin() + 45, midiMessage.begin() + 45 + 8);
   1.108 +			hash1 = normalize(hash1);
   1.109 +			seed2 = normalize(seed2);
   1.110 +			std::cerr << "DJMFixImpl::receive(): got message with hash1 = " << toString(hash1) << " and seed2 = " << toString(seed2) << std::endl; // TODO: do not mess STDIO
   1.111 +
   1.112 +			Bytes seed0 = {0x68, 0x01, 0x31, 0xFB};
   1.113 +			Bytes seed1 = {0x29, 0x00, 0x00, 0x00, 0x23, 0x48, 0x00, 0x00};
   1.114 +
   1.115 +			Bytes hash1check = toBytes(fnv32hash(concat(seed1, xOR(seed0, seed2))));
   1.116 +
   1.117 +			if (equals(hash1, hash1check)) {
   1.118 +				std::cerr << "DJMFixImpl::receive(): hash1 verification: OK" << std::endl;
   1.119 +			} else {
   1.120 +				std::cerr << "DJMFixImpl::receive(): hash1 verification: ERROR: check = " << toString(hash1check) << std::endl;
   1.121 +			}
   1.122  		}
   1.123  
   1.124  	}
   1.125 @@ -73,9 +145,9 @@
   1.126  		if (midiSender == nullptr) throw std::logic_error("need a midiSender when starting DJMFix");
   1.127  
   1.128  		// TODO: methods for parsing and constructing messages from parts (TLV)
   1.129 -		midiSender->send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x50, 0x01, 0xf7});
   1.130 -		std::this_thread::sleep_for(std::chrono::milliseconds(10));
   1.131 -		midiSender->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});
   1.132 +		send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x50, 0x01, 0xf7});
   1.133 +		std::this_thread::sleep_for(std::chrono::milliseconds(30)); // TODO: wait until we got the response
   1.134 +		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});
   1.135  
   1.136  		keepAliveThread = std::thread(&DJMFixImpl::run, this);
   1.137  		running = true;