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.

140 lines
5.3KB

  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. /**
  20. Firmware below 0.3.0 does not report its version over the Blocks API.
  21. This class can make requests and process responses to retrieve 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() override
  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 { highestVersion.asString() };
  48. const BlocksVersion test { result[i].asString() };
  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. 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, 0x03, 0xf7 }, // Bootloader
  68. { 0xf0, 0x00, 0x21, 0x10, 0x47, 0x03, 0x01, 0xf7 }}; // Stm32
  69. static const BlocksVersion depreciatedVersion ("0.3.0");
  70. if (currentRequest.get() == numFirmwareApps - 1
  71. && (BlocksVersion (result[0].asString()) >= depreciatedVersion
  72. || BlocksVersion (result[1].asString()) >= depreciatedVersion))
  73. {
  74. stopTimer();
  75. }
  76. else
  77. {
  78. deviceConnection.sendMessageToDevice (&requests[currentRequest.get()][0], requestSize);
  79. }
  80. }
  81. //==============================================================================
  82. void processVersionMessage (const uint8* data, const size_t size)
  83. {
  84. if (currentRequest.get() >= numFirmwareApps || size < 1 || size - 1 > VersionNumber::maxLength)
  85. return;
  86. result[currentRequest.get()].length = uint8 (size - 1);
  87. memcpy (result[currentRequest.get()].data, data, result[currentRequest.get()].length);
  88. ++currentRequest;
  89. allRequestsComplete() ? stopTimer() : startTimer (10);
  90. }
  91. //==============================================================================
  92. void handleIncomingMidiMessage (const MidiMessage& message) override
  93. {
  94. const uint8 roliVersionHeader[] = { 0xf0, 0x00, 0x21, 0x10, 0x47, 0x03 };
  95. const auto headerSize = sizeof (roliVersionHeader);
  96. const auto data = message.getRawData();
  97. const auto size = size_t (message.getRawDataSize());
  98. if (memcmp (data, roliVersionHeader, headerSize) == 0)
  99. processVersionMessage (data + headerSize, size - headerSize);
  100. }
  101. void connectionBeingDeleted (const MIDIDeviceConnection&) override {}
  102. //==============================================================================
  103. void timerCallback() override
  104. {
  105. startTimer (200);
  106. makeNextRequest();
  107. }
  108. //==============================================================================
  109. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DepreciatedVersionReader)
  110. };
  111. } // namespace juce