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.

129 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. /**
  20. Firmware below 0.2.5 does not report its version over the Blocks API.
  21. This class can make requests and process responses to retreive the master Block version.
  22. */
  23. class DepreciatedVersionReader : private MIDIDeviceConnection::Listener,
  24. private Timer
  25. {
  26. public:
  27. //==============================================================================
  28. DepreciatedVersionReader (MIDIDeviceConnection& deviceConnectionToUse)
  29. : deviceConnection (deviceConnectionToUse)
  30. {
  31. deviceConnection.addListener (this);
  32. startTimer (10);
  33. }
  34. //==============================================================================
  35. ~DepreciatedVersionReader()
  36. {
  37. deviceConnection.removeListener (this);
  38. }
  39. //==============================================================================
  40. BlocksProtocol::VersionNumber getVersionNumber()
  41. {
  42. if (! allRequestsComplete())
  43. return {};
  44. auto highestVersion = result[0];
  45. for (size_t i = 1; i < numFirmwareApps; ++i)
  46. {
  47. const BlocksVersion highest { asString (highestVersion) };
  48. const BlocksVersion test { asString ( result[i]) };
  49. if (highest < test)
  50. highestVersion = result[i];
  51. }
  52. return highestVersion;
  53. }
  54. private:
  55. //==============================================================================
  56. static constexpr size_t numFirmwareApps = 3;
  57. BlocksProtocol::VersionNumber result[numFirmwareApps];
  58. MIDIDeviceConnection& deviceConnection;
  59. juce::Atomic<size_t> currentRequest = 0;
  60. //==============================================================================
  61. bool allRequestsComplete() const { return currentRequest.get() >= numFirmwareApps; }
  62. //==============================================================================
  63. void makeNextRequest()
  64. {
  65. static constexpr size_t requestSize { 8 };
  66. static constexpr uint8 requests[][requestSize] = {{ 0xf0, 0x00, 0x21, 0x10, 0x47, 0x03, 0x00, 0xf7 }, // Main App
  67. { 0xf0, 0x00, 0x21, 0x10, 0x47, 0x03, 0x01, 0xf7 }, // Stm32
  68. { 0xf0, 0x00, 0x21, 0x10, 0x47, 0x03, 0x03, 0xf7 }}; // Bootloader
  69. deviceConnection.sendMessageToDevice (&requests[currentRequest.get()][0], requestSize);
  70. }
  71. //==============================================================================
  72. void processVersionMessage (const uint8* data, const size_t size)
  73. {
  74. if (currentRequest.get() >= numFirmwareApps || size < 1 || size - 1 > VersionNumber::maxLength)
  75. return;
  76. result[currentRequest.get()].length = uint8 (size - 1);
  77. memcpy (result[currentRequest.get()].data, data, result[currentRequest.get()].length);
  78. ++currentRequest;
  79. allRequestsComplete() ? stopTimer() : startTimer (10);
  80. }
  81. //==============================================================================
  82. void handleIncomingMidiMessage (const MidiMessage& message) override
  83. {
  84. const uint8 roliVersionHeader[] = { 0xf0, 0x00, 0x21, 0x10, 0x47, 0x03 };
  85. const auto headerSize = sizeof (roliVersionHeader);
  86. const auto data = message.getRawData();
  87. const auto size = size_t (message.getRawDataSize());
  88. if (memcmp (data, roliVersionHeader, headerSize) == 0)
  89. processVersionMessage (data + headerSize, size - headerSize);
  90. }
  91. void connectionBeingDeleted (const MIDIDeviceConnection&) override {}
  92. //==============================================================================
  93. void timerCallback() override
  94. {
  95. startTimer (200);
  96. makeNextRequest();
  97. }
  98. //==============================================================================
  99. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DepreciatedVersionReader)
  100. };
  101. } // namespace juce