The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. class BlockSerialReader
  20. : private MIDIDeviceConnection::Listener,
  21. private Timer
  22. {
  23. public:
  24. //==============================================================================
  25. BlockSerialReader (MIDIDeviceConnection& deviceConnectionToUse) : deviceConnection (deviceConnectionToUse)
  26. {
  27. deviceConnection.addListener (this);
  28. startTimer (10);
  29. }
  30. ~BlockSerialReader() override
  31. {
  32. deviceConnection.removeListener (this);
  33. }
  34. bool hasSerial() const { return serial.isNotEmpty(); }
  35. String getSerial() const { return serial; }
  36. private:
  37. MIDIDeviceConnection& deviceConnection;
  38. String serial;
  39. bool shouldStop() { return hasSerial(); }
  40. //==============================================================================
  41. void timerCallback() override
  42. {
  43. if (shouldStop())
  44. {
  45. stopTimer();
  46. return;
  47. }
  48. sendRequest();
  49. startTimer (300);
  50. }
  51. void sendRequest()
  52. {
  53. const uint8 dumpRequest[] = { 0xf0, 0x00, 0x21, 0x10, 0x78, 0x3f, 0xf7 };
  54. deviceConnection.sendMessageToDevice (dumpRequest, sizeof (dumpRequest));
  55. }
  56. void handleIncomingMidiMessage (const MidiMessage& message) override
  57. {
  58. if (hasSerial())
  59. return;
  60. if (isResponse (message))
  61. parseResponse (message);
  62. }
  63. void connectionBeingDeleted (const MIDIDeviceConnection&) override
  64. {
  65. stopTimer();
  66. }
  67. bool isResponse (const MidiMessage message)
  68. {
  69. const uint8 roliDumpHeader[] = { 0xf0, 0x00, 0x21, 0x10, 0x78};
  70. return memcmp (message.getRawData(), roliDumpHeader, sizeof (roliDumpHeader)) == 0;
  71. }
  72. void parseResponse (const MidiMessage& message)
  73. {
  74. int index = findMacAddressStart (message);
  75. if (index >= 0)
  76. {
  77. const int macSize = 17;
  78. const int offset = index + macSize;
  79. const int serialSize = 16;
  80. if (message.getRawDataSize() - offset < serialSize)
  81. {
  82. jassertfalse;
  83. return;
  84. }
  85. serial = String ((const char*)message.getRawData() + offset, serialSize);
  86. }
  87. }
  88. int findMacAddressStart (const MidiMessage& message)
  89. {
  90. const uint8 macStart[] = { '4', '8', ':', 'B', '6', ':', '2', '0', ':' };
  91. return findSequence (macStart, sizeof (macStart), message);
  92. }
  93. int findSequence (const uint8* sequence, int sequenceSize, const MidiMessage& message)
  94. {
  95. for (int i = 0; i < message.getRawDataSize() - sequenceSize; i++)
  96. {
  97. if (memcmp (message.getRawData() + i, sequence, size_t (sequenceSize)) == 0)
  98. return i;
  99. }
  100. return -1;
  101. }
  102. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BlockSerialReader)
  103. };
  104. }