branch | v_0 |
changeset 6 | bddcf2bf29f2 |
parent 5 | ef8f4023e32e |
child 7 | 889b4b8737bd |
5:ef8f4023e32e | 6:bddcf2bf29f2 |
---|---|
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 */ |
16 */ |
17 #include <iostream> |
17 #include <iostream> |
18 #include <iomanip> |
18 #include <iomanip> |
19 #include <thread> |
19 #include <thread> |
20 #include <mutex> |
|
20 #include <atomic> |
21 #include <atomic> |
21 #include <chrono> |
22 #include <chrono> |
22 #include <stdexcept> |
23 #include <stdexcept> |
24 #include <vector> |
|
23 |
25 |
24 #include "DJMFix.h" |
26 #include "DJMFix.h" |
25 |
27 |
26 namespace djmfix { |
28 namespace djmfix { |
27 |
29 |
30 using Bytes = std::vector<uint8_t>; |
|
31 |
|
28 class DJMFixImpl : public DJMFix { |
32 class DJMFixImpl : public DJMFix { |
29 private: |
33 private: |
30 MidiSender* midiSender; |
34 MidiSender* midiSender; |
31 std::thread keepAliveThread; |
35 std::thread keepAliveThread; |
36 std::recursive_mutex midiMutex; |
|
32 std::atomic<bool> running{false}; |
37 std::atomic<bool> running{false}; |
33 std::atomic<bool> stopped{false}; |
38 std::atomic<bool> stopped{false}; |
34 |
39 |
35 void run() { |
40 void run() { |
36 while (!stopped) { |
41 while (!stopped) { |
38 // TODO: send keep-alive messages |
43 // TODO: send keep-alive messages |
39 std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
44 std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
40 } |
45 } |
41 } |
46 } |
42 |
47 |
43 // TODO: remove |
48 void send(const MidiMessage& midiMessage) { |
44 std::string toString(const MidiMessage& midiMessage) { |
49 std::lock_guard<std::recursive_mutex> lock(midiMutex); |
50 midiSender->send(midiMessage); |
|
51 } |
|
52 |
|
53 std::string toString(const Bytes& midiMessage) { |
|
45 std::stringstream result; |
54 std::stringstream result; |
46 for (uint8_t b : midiMessage) result << std::hex << std::setw(2) << std::setfill('0') << (int) b; |
55 for (uint8_t b : midiMessage) result << std::hex << std::setw(2) << std::setfill('0') << (int) b; |
47 return result.str(); |
56 return result.str(); |
57 } |
|
58 |
|
59 Bytes normalize(const Bytes& data) { |
|
60 if (data.size() % 2) throw std::invalid_argument("data before normalization must have even number of bytes"); |
|
61 Bytes result; |
|
62 result.reserve(data.size() / 2); |
|
63 for (size_t i = 0; i < data.size() / 2; i++) result.push_back((data[i * 2] & 0x0F) << 4 | (data[i * 2 + 1] & 0x0F)); |
|
64 return result; |
|
65 } |
|
66 |
|
67 uint32_t fnv32hash(const Bytes& buff) { |
|
68 uint32_t hash = 0x811c9dc5; |
|
69 for (uint8_t b : buff) hash = ((b^hash) * 0x1000193); |
|
70 return hash; |
|
71 } |
|
72 |
|
73 Bytes toBytes(const uint32_t value) { |
|
74 Bytes result; |
|
75 result.reserve(4); |
|
76 result.push_back(value >> 24); |
|
77 result.push_back(value >> 16); |
|
78 result.push_back(value >> 8); |
|
79 result.push_back(value >> 0); |
|
80 return result; |
|
81 } |
|
82 |
|
83 bool equals(Bytes a, Bytes b) { |
|
84 if (a.size() != b.size()) return false; |
|
85 for (size_t i = 0; i < a.size(); i++) if (a[i] != b[i]) return false; |
|
86 return true; |
|
87 } |
|
88 |
|
89 template<typename T> std::vector<T> concat(const std::vector<T>& a, const std::vector<T>& b, const std::vector<T>& c = {}) { |
|
90 std::vector<T> result; |
|
91 result.reserve(a.size() + b.size() + c.size()); |
|
92 for (size_t i = 0; i < a.size(); i++) result.push_back(a[i]); |
|
93 for (size_t i = 0; i < b.size(); i++) result.push_back(b[i]); |
|
94 for (size_t i = 0; i < c.size(); i++) result.push_back(c[i]); |
|
95 return result; |
|
96 } |
|
97 |
|
98 template<typename T> std::vector<T> xOR(const std::vector<T>& a, const std::vector<T>& b) { |
|
99 if (a.size() != b.size()) throw std::invalid_argument("xor: both must be the same length"); |
|
100 std::vector<T> result; |
|
101 result.reserve(a.size()); |
|
102 for (size_t i = 0; i < a.size(); i++) result.push_back(a[i] ^ b[i]); |
|
103 return result; |
|
48 } |
104 } |
49 |
105 |
50 public: |
106 public: |
51 |
107 |
52 virtual ~DJMFixImpl() override { |
108 virtual ~DJMFixImpl() override { |
57 void setMidiSender(MidiSender* midiSender) { |
113 void setMidiSender(MidiSender* midiSender) { |
58 std::cerr << "DJMFixImpl::setMidiSender()" << std::endl; // TODO: do not mess STDIO |
114 std::cerr << "DJMFixImpl::setMidiSender()" << std::endl; // TODO: do not mess STDIO |
59 this->midiSender = midiSender; |
115 this->midiSender = midiSender; |
60 } |
116 } |
61 |
117 |
62 virtual void receive(MidiMessage midiMessage) override { |
118 virtual void receive(const MidiMessage& midiMessage) override { |
63 std::cerr << "DJMFixImpl::receive(): size = " << midiMessage.size() << " data = " << toString(midiMessage) << std::endl; // TODO: do not mess STDIO |
119 std::cerr << "DJMFixImpl::receive(): size = " << midiMessage.size() << " data = " << toString(midiMessage) << std::endl; // TODO: do not mess STDIO |
120 std::lock_guard<std::recursive_mutex> lock(midiMutex); |
|
64 |
121 |
65 if (midiMessage.size() == 54 && midiMessage[9] == 0x13) { |
122 if (midiMessage.size() == 54 && midiMessage[9] == 0x13 && midiMessage[33] == 0x04 && midiMessage[43] == 0x03) { |
66 std::cerr << "DJMFixImpl::receive(): got message with HashA and SeedE" << std::endl; // TODO: do not mess STDIO |
123 Bytes hash1(midiMessage.begin() + 35, midiMessage.begin() + 35 + 8); |
124 Bytes seed2(midiMessage.begin() + 45, midiMessage.begin() + 45 + 8); |
|
125 hash1 = normalize(hash1); |
|
126 seed2 = normalize(seed2); |
|
127 std::cerr << "DJMFixImpl::receive(): got message with hash1 = " << toString(hash1) << " and seed2 = " << toString(seed2) << std::endl; // TODO: do not mess STDIO |
|
128 |
|
129 Bytes seed0 = {0x68, 0x01, 0x31, 0xFB}; |
|
130 Bytes seed1 = {0x29, 0x00, 0x00, 0x00, 0x23, 0x48, 0x00, 0x00}; |
|
131 |
|
132 Bytes hash1check = toBytes(fnv32hash(concat(seed1, xOR(seed0, seed2)))); |
|
133 |
|
134 if (equals(hash1, hash1check)) { |
|
135 std::cerr << "DJMFixImpl::receive(): hash1 verification: OK" << std::endl; |
|
136 } else { |
|
137 std::cerr << "DJMFixImpl::receive(): hash1 verification: ERROR: check = " << toString(hash1check) << std::endl; |
|
138 } |
|
67 } |
139 } |
68 |
140 |
69 } |
141 } |
70 |
142 |
71 void start() override { |
143 void start() override { |
72 std::cerr << "DJMFixImpl::start()" << std::endl; // TODO: do not mess STDIO |
144 std::cerr << "DJMFixImpl::start()" << std::endl; // TODO: do not mess STDIO |
73 if (midiSender == nullptr) throw std::logic_error("need a midiSender when starting DJMFix"); |
145 if (midiSender == nullptr) throw std::logic_error("need a midiSender when starting DJMFix"); |
74 |
146 |
75 // TODO: methods for parsing and constructing messages from parts (TLV) |
147 // TODO: methods for parsing and constructing messages from parts (TLV) |
76 midiSender->send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x50, 0x01, 0xf7}); |
148 send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x50, 0x01, 0xf7}); |
77 std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
149 std::this_thread::sleep_for(std::chrono::milliseconds(30)); // TODO: wait until we got the response |
78 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}); |
150 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}); |
79 |
151 |
80 keepAliveThread = std::thread(&DJMFixImpl::run, this); |
152 keepAliveThread = std::thread(&DJMFixImpl::run, this); |
81 running = true; |
153 running = true; |
82 |
154 |
83 } |
155 } |