/**
* DJM-Fix
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <iomanip>
#include <thread>
#include <mutex>
#include <atomic>
#include <chrono>
#include <stdexcept>
#include <vector>
#include "DJMFix.h"
namespace djmfix {
using Bytes = std::vector<uint8_t>;
class DJMFixImpl : public DJMFix {
private:
MidiSender* midiSender;
std::thread keepAliveThread;
std::recursive_mutex midiMutex;
std::atomic<bool> running{false};
std::atomic<bool> stopped{false};
void run() {
while (!stopped) {
std::cerr << "DJMFixImpl::run()" << std::endl; // TODO: do not mess STDIO
// TODO: send keep-alive messages
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
}
void send(const MidiMessage& midiMessage) {
std::lock_guard<std::recursive_mutex> lock(midiMutex);
midiSender->send(midiMessage);
}
std::string toString(const Bytes& midiMessage) {
std::stringstream result;
for (uint8_t b : midiMessage) result << std::hex << std::setw(2) << std::setfill('0') << (int) b;
return result.str();
}
Bytes normalize(const Bytes& data) {
if (data.size() % 2) throw std::invalid_argument("data before normalization must have even number of bytes");
Bytes result;
result.reserve(data.size() / 2);
for (size_t i = 0; i < data.size() / 2; i++) result.push_back((data[i * 2] & 0x0F) << 4 | (data[i * 2 + 1] & 0x0F));
return result;
}
uint32_t fnv32hash(const Bytes& buff) {
uint32_t hash = 0x811c9dc5;
for (uint8_t b : buff) hash = ((b^hash) * 0x1000193);
return hash;
}
Bytes toBytes(const uint32_t value) {
Bytes result;
result.reserve(4);
result.push_back(value >> 24);
result.push_back(value >> 16);
result.push_back(value >> 8);
result.push_back(value >> 0);
return result;
}
bool equals(Bytes a, Bytes b) {
if (a.size() != b.size()) return false;
for (size_t i = 0; i < a.size(); i++) if (a[i] != b[i]) return false;
return true;
}
template<typename T> std::vector<T> concat(const std::vector<T>& a, const std::vector<T>& b, const std::vector<T>& c = {}) {
std::vector<T> result;
result.reserve(a.size() + b.size() + c.size());
for (size_t i = 0; i < a.size(); i++) result.push_back(a[i]);
for (size_t i = 0; i < b.size(); i++) result.push_back(b[i]);
for (size_t i = 0; i < c.size(); i++) result.push_back(c[i]);
return result;
}
template<typename T> std::vector<T> xOR(const std::vector<T>& a, const std::vector<T>& b) {
if (a.size() != b.size()) throw std::invalid_argument("xor: both must be the same length");
std::vector<T> result;
result.reserve(a.size());
for (size_t i = 0; i < a.size(); i++) result.push_back(a[i] ^ b[i]);
return result;
}
public:
virtual ~DJMFixImpl() override {
std::cerr << "~DJMFixImpl()" << std::endl; // TODO: do not mess STDIO
if (running) stop();
}
void setMidiSender(MidiSender* midiSender) {
std::cerr << "DJMFixImpl::setMidiSender()" << std::endl; // TODO: do not mess STDIO
this->midiSender = midiSender;
}
virtual void receive(const MidiMessage& midiMessage) override {
std::cerr << "DJMFixImpl::receive(): size = " << midiMessage.size() << " data = " << toString(midiMessage) << std::endl; // TODO: do not mess STDIO
std::lock_guard<std::recursive_mutex> lock(midiMutex);
if (midiMessage.size() == 12 && midiMessage[9] == 0x11) {
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});
} else if (midiMessage.size() == 54 && midiMessage[9] == 0x13 && midiMessage[33] == 0x04 && midiMessage[43] == 0x03) {
Bytes hash1(midiMessage.begin() + 35, midiMessage.begin() + 35 + 8);
Bytes seed2(midiMessage.begin() + 45, midiMessage.begin() + 45 + 8);
hash1 = normalize(hash1);
seed2 = normalize(seed2);
std::cerr << "DJMFixImpl::receive(): got message with hash1 = " << toString(hash1) << " and seed2 = " << toString(seed2) << std::endl; // TODO: do not mess STDIO
Bytes seed0 = {0x68, 0x01, 0x31, 0xFB};
Bytes seed1 = {0x29, 0x00, 0x00, 0x00, 0x23, 0x48, 0x00, 0x00};
Bytes hash1check = toBytes(fnv32hash(concat(seed1, xOR(seed0, seed2))));
if (equals(hash1, hash1check)) {
std::cerr << "DJMFixImpl::receive(): hash1 verification: OK" << std::endl;
} else {
std::cerr << "DJMFixImpl::receive(): hash1 verification: ERROR: check = " << toString(hash1check) << std::endl;
}
}
}
void start() override {
std::cerr << "DJMFixImpl::start()" << std::endl; // TODO: do not mess STDIO
if (midiSender == nullptr) throw std::logic_error("need a midiSender when starting DJMFix");
// TODO: methods for parsing and constructing messages from parts (TLV)
send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x50, 0x01, 0xf7});
keepAliveThread = std::thread(&DJMFixImpl::run, this);
running = true;
}
void stop() override {
stopped = true;
keepAliveThread.join();
running = false;
std::cerr << "DJMFixImpl::stop()" << std::endl; // TODO: do not mess STDIO
}
};
DJMFix* create() {
return new DJMFixImpl();
}
}