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