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.

722 lines
24KB

  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. namespace
  20. {
  21. static Block::Timestamp deviceTimestampToHost (uint32 timestamp) noexcept
  22. {
  23. return static_cast<Block::Timestamp> (timestamp);
  24. }
  25. template <typename V>
  26. static int removeUnusedBlocksFromMap (std::map<Block::UID, V>& mapToClean, const juce::Array<DeviceInfo>& currentDevices)
  27. {
  28. int numRemoved = 0;
  29. for (auto iter = mapToClean.begin(); iter != mapToClean.end();)
  30. {
  31. bool found = false;
  32. for (auto& info : currentDevices)
  33. {
  34. if (info.uid == iter->first)
  35. {
  36. found = true;
  37. break;
  38. }
  39. }
  40. if (found)
  41. {
  42. ++iter;
  43. }
  44. else
  45. {
  46. mapToClean.erase (iter++);
  47. ++numRemoved;
  48. }
  49. }
  50. return numRemoved;
  51. }
  52. /* Returns true new element added to map or value of existing key changed */
  53. template <typename V>
  54. static bool insertOrAssign (std::map<Block::UID, V>& map, const Block::UID& key, const V& value)
  55. {
  56. const auto result = map.emplace (std::make_pair (key, value));
  57. if (! result.second)
  58. {
  59. if (result.first->second == value)
  60. return false;
  61. result.first->second = value;
  62. }
  63. return true;
  64. }
  65. }
  66. template <typename Detector>
  67. struct ConnectedDeviceGroup : private juce::AsyncUpdater,
  68. private juce::Timer
  69. {
  70. //==============================================================================
  71. ConnectedDeviceGroup (Detector& d, const juce::String& name, PhysicalTopologySource::DeviceConnection* connection)
  72. : detector (d), deviceName (name), deviceConnection (connection)
  73. {
  74. if (auto midiDeviceConnection = static_cast<MIDIDeviceConnection*> (deviceConnection.get()))
  75. {
  76. depreciatedVersionReader = std::make_unique<DepreciatedVersionReader> (*midiDeviceConnection);
  77. juce::ScopedLock lock (midiDeviceConnection->criticalSecton);
  78. setMidiMessageCallback();
  79. }
  80. else
  81. {
  82. setMidiMessageCallback();
  83. }
  84. startTimer (200);
  85. sendTopologyRequest();
  86. }
  87. bool isStillConnected (const juce::StringArray& detectedDevices) const noexcept
  88. {
  89. return detectedDevices.contains (deviceName) && ! failedToGetTopology();
  90. }
  91. int getIndexFromDeviceID (Block::UID uid) const noexcept
  92. {
  93. for (auto& d : currentDeviceInfo)
  94. if (d.uid == uid)
  95. return d.index;
  96. return -1;
  97. }
  98. const BlocksProtocol::DeviceStatus* getLastStatus (Block::UID deviceID) const noexcept
  99. {
  100. for (auto&& status : currentTopologyDevices)
  101. if (getBlockUIDFromSerialNumber (status.serialNumber) == deviceID)
  102. return &status;
  103. return nullptr;
  104. }
  105. void notifyBlockIsRestarting (Block::UID deviceID)
  106. {
  107. forceApiDisconnected (deviceID);
  108. }
  109. //==============================================================================
  110. // The following methods will be called by the HostPacketDecoder:
  111. void beginTopology (int numDevices, int numConnections)
  112. {
  113. incomingTopologyDevices.clearQuick();
  114. incomingTopologyDevices.ensureStorageAllocated (numDevices);
  115. incomingTopologyConnections.clearQuick();
  116. incomingTopologyConnections.ensureStorageAllocated (numConnections);
  117. }
  118. void extendTopology (int numDevices, int numConnections)
  119. {
  120. incomingTopologyDevices.ensureStorageAllocated (incomingTopologyDevices.size() + numDevices);
  121. incomingTopologyConnections.ensureStorageAllocated (incomingTopologyConnections.size() + numConnections);
  122. }
  123. void handleTopologyDevice (BlocksProtocol::DeviceStatus status)
  124. {
  125. incomingTopologyDevices.add (status);
  126. }
  127. void handleTopologyConnection (BlocksProtocol::DeviceConnection connection)
  128. {
  129. incomingTopologyConnections.add (connection);
  130. }
  131. void endTopology()
  132. {
  133. lastTopologyReceiveTime = juce::Time::getCurrentTime();
  134. if (incomingTopologyDevices.isEmpty())
  135. {
  136. jassertfalse;
  137. return;
  138. }
  139. currentTopologyDevices.swapWith (incomingTopologyDevices);
  140. currentTopologyConnections.swapWith (incomingTopologyConnections);
  141. incomingTopologyDevices.clearQuick();
  142. incomingTopologyConnections.clearQuick();
  143. refreshCurrentDeviceInfo();
  144. refreshCurrentDeviceConnections();
  145. removeUnusedBlocksFromMap (versionNumbers, currentDeviceInfo);
  146. removeUnusedBlocksFromMap (blockNames, currentDeviceInfo);
  147. removePingForDisconnectedBlocks();
  148. detector.handleTopologyChange();
  149. }
  150. void handleVersion (BlocksProtocol::DeviceVersion version)
  151. {
  152. const auto uid = getDeviceIDFromIndex (version.index);
  153. if (uid == Block::UID() || version.version.length <= 1)
  154. return;
  155. setVersion (uid, version.version);
  156. }
  157. void handleName (BlocksProtocol::DeviceName name)
  158. {
  159. const auto uid = getDeviceIDFromIndex (name.index);
  160. if (uid == Block::UID() || name.name.length <= 1)
  161. return;
  162. if (insertOrAssign (blockNames, uid, name.name))
  163. {
  164. refreshCurrentDeviceInfo();
  165. detector.handleTopologyChange();
  166. }
  167. }
  168. void handleControlButtonUpDown (BlocksProtocol::TopologyIndex deviceIndex, uint32 timestamp,
  169. BlocksProtocol::ControlButtonID buttonID, bool isDown)
  170. {
  171. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  172. detector.handleButtonChange (deviceID, deviceTimestampToHost (timestamp), buttonID.get(), isDown);
  173. }
  174. void handleCustomMessage (BlocksProtocol::TopologyIndex deviceIndex, uint32 timestamp, const int32* data)
  175. {
  176. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  177. detector.handleCustomMessage (deviceID, deviceTimestampToHost (timestamp), data);
  178. }
  179. void handleTouchChange (BlocksProtocol::TopologyIndex deviceIndex,
  180. uint32 timestamp,
  181. BlocksProtocol::TouchIndex touchIndex,
  182. BlocksProtocol::TouchPosition position,
  183. BlocksProtocol::TouchVelocity velocity,
  184. bool isStart, bool isEnd)
  185. {
  186. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  187. {
  188. TouchSurface::Touch touch;
  189. touch.index = (int) touchIndex.get();
  190. touch.x = position.x.toUnipolarFloat();
  191. touch.y = position.y.toUnipolarFloat();
  192. touch.z = position.z.toUnipolarFloat();
  193. touch.xVelocity = velocity.vx.toBipolarFloat();
  194. touch.yVelocity = velocity.vy.toBipolarFloat();
  195. touch.zVelocity = velocity.vz.toBipolarFloat();
  196. touch.eventTimestamp = deviceTimestampToHost (timestamp);
  197. touch.isTouchStart = isStart;
  198. touch.isTouchEnd = isEnd;
  199. touch.blockUID = deviceID;
  200. setTouchStartPosition (touch);
  201. detector.handleTouchChange (deviceID, touch);
  202. }
  203. }
  204. void setTouchStartPosition (TouchSurface::Touch& touch)
  205. {
  206. auto& startPos = touchStartPositions.getValue (touch);
  207. if (touch.isTouchStart)
  208. startPos = { touch.x, touch.y };
  209. touch.startX = startPos.x;
  210. touch.startY = startPos.y;
  211. }
  212. void handlePacketACK (BlocksProtocol::TopologyIndex deviceIndex,
  213. BlocksProtocol::PacketCounter counter)
  214. {
  215. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  216. {
  217. detector.handleSharedDataACK (deviceID, counter);
  218. updateApiPing (deviceID);
  219. }
  220. }
  221. void handleFirmwareUpdateACK (BlocksProtocol::TopologyIndex deviceIndex,
  222. BlocksProtocol::FirmwareUpdateACKCode resultCode,
  223. BlocksProtocol::FirmwareUpdateACKDetail resultDetail)
  224. {
  225. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  226. {
  227. detector.handleFirmwareUpdateACK (deviceID, (uint8) resultCode.get(), (uint32) resultDetail.get());
  228. updateApiPing (deviceID);
  229. }
  230. }
  231. void handleConfigUpdateMessage (BlocksProtocol::TopologyIndex deviceIndex,
  232. int32 item, int32 value, int32 min, int32 max)
  233. {
  234. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  235. detector.handleConfigUpdateMessage (deviceID, item, value, min, max);
  236. }
  237. void handleConfigSetMessage (BlocksProtocol::TopologyIndex deviceIndex,
  238. int32 item, int32 value)
  239. {
  240. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  241. detector.handleConfigSetMessage (deviceID, item, value);
  242. }
  243. void handleConfigFactorySyncEndMessage (BlocksProtocol::TopologyIndex deviceIndex)
  244. {
  245. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  246. detector.handleConfigFactorySyncEndMessage (deviceID);
  247. }
  248. void handleConfigFactorySyncResetMessage (BlocksProtocol::TopologyIndex deviceIndex)
  249. {
  250. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  251. detector.handleConfigFactorySyncResetMessage (deviceID);
  252. }
  253. void handleLogMessage (BlocksProtocol::TopologyIndex deviceIndex, const String& message)
  254. {
  255. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  256. detector.handleLogMessage (deviceID, message);
  257. }
  258. //==============================================================================
  259. template <typename PacketBuilder>
  260. bool sendMessageToDevice (const PacketBuilder& builder) const
  261. {
  262. if (deviceConnection->sendMessageToDevice (builder.getData(), (size_t) builder.size()))
  263. {
  264. #if DUMP_BANDWIDTH_STATS
  265. registerBytesOut (builder.size());
  266. #endif
  267. return true;
  268. }
  269. return false;
  270. }
  271. PhysicalTopologySource::DeviceConnection* getDeviceConnection()
  272. {
  273. return deviceConnection.get();
  274. }
  275. juce::Array<DeviceInfo> getCurrentDeviceInfo()
  276. {
  277. auto blocks = currentDeviceInfo;
  278. blocks.removeIf ([this] (DeviceInfo& info) { return ! isApiConnected (info.uid); });
  279. return blocks;
  280. }
  281. juce::Array<BlockDeviceConnection> getCurrentDeviceConnections()
  282. {
  283. auto connections = currentDeviceConnections;
  284. connections.removeIf ([this] (BlockDeviceConnection& c) { return ! isApiConnected (c.device1) || ! isApiConnected (c.device2); });
  285. return connections;
  286. }
  287. Detector& detector;
  288. juce::String deviceName;
  289. static constexpr double pingTimeoutSeconds = 6.0;
  290. private:
  291. //==============================================================================
  292. juce::Array<DeviceInfo> currentDeviceInfo;
  293. juce::Array<BlockDeviceConnection> currentDeviceConnections;
  294. std::unique_ptr<PhysicalTopologySource::DeviceConnection> deviceConnection;
  295. juce::Array<BlocksProtocol::DeviceStatus> incomingTopologyDevices, currentTopologyDevices;
  296. juce::Array<BlocksProtocol::DeviceConnection> incomingTopologyConnections, currentTopologyConnections;
  297. juce::CriticalSection incomingPacketLock;
  298. juce::Array<juce::MemoryBlock> incomingPackets;
  299. std::map<Block::UID, BlocksProtocol::VersionNumber> versionNumbers;
  300. std::map<Block::UID, BlocksProtocol::BlockName> blockNames;
  301. std::unique_ptr<DepreciatedVersionReader> depreciatedVersionReader;
  302. struct TouchStart { float x, y; };
  303. TouchList<TouchStart> touchStartPositions;
  304. Block::UID masterBlock = 0;
  305. //==============================================================================
  306. void setMidiMessageCallback()
  307. {
  308. deviceConnection->handleMessageFromDevice = [this] (const void* data, size_t dataSize)
  309. {
  310. this->handleIncomingMessage (data, dataSize);
  311. };
  312. }
  313. //==============================================================================
  314. juce::Time lastTopologyRequestTime, lastTopologyReceiveTime;
  315. int numTopologyRequestsSent = 0;
  316. void scheduleNewTopologyRequest()
  317. {
  318. numTopologyRequestsSent = 0;
  319. lastTopologyReceiveTime = juce::Time();
  320. lastTopologyRequestTime = juce::Time::getCurrentTime();
  321. }
  322. void sendTopologyRequest()
  323. {
  324. ++numTopologyRequestsSent;
  325. lastTopologyRequestTime = juce::Time::getCurrentTime();
  326. sendCommandMessage (0, BlocksProtocol::requestTopologyMessage);
  327. }
  328. void timerCallback() override
  329. {
  330. const auto now = juce::Time::getCurrentTime();
  331. if ((now > lastTopologyReceiveTime + juce::RelativeTime::seconds (30.0))
  332. && now > lastTopologyRequestTime + juce::RelativeTime::seconds (1.0)
  333. && numTopologyRequestsSent < 4)
  334. sendTopologyRequest();
  335. checkApiTimeouts (now);
  336. startApiModeOnConnectedBlocks();
  337. requestMasterBlockVersionIfNeeded();
  338. }
  339. bool failedToGetTopology() const noexcept
  340. {
  341. return numTopologyRequestsSent >= 4 && lastTopologyReceiveTime == juce::Time();
  342. }
  343. bool sendCommandMessage (BlocksProtocol::TopologyIndex deviceIndex, uint32 commandID) const
  344. {
  345. BlocksProtocol::HostPacketBuilder<64> p;
  346. p.writePacketSysexHeaderBytes (deviceIndex);
  347. p.deviceControlMessage (commandID);
  348. p.writePacketSysexFooter();
  349. return sendMessageToDevice (p);
  350. }
  351. //==============================================================================
  352. void requestMasterBlockVersionIfNeeded()
  353. {
  354. if (depreciatedVersionReader == nullptr)
  355. return;
  356. const auto masterVersion = depreciatedVersionReader->getVersionNumber();
  357. if (masterVersion.isNotEmpty())
  358. setVersion (masterBlock, masterVersion);
  359. }
  360. void setVersion (const Block::UID uid, const BlocksProtocol::VersionNumber versionNumber)
  361. {
  362. if (uid == masterBlock)
  363. depreciatedVersionReader.reset();
  364. if (insertOrAssign (versionNumbers, uid, versionNumber))
  365. {
  366. refreshCurrentDeviceInfo();
  367. detector.handleTopologyChange();
  368. }
  369. }
  370. //==============================================================================
  371. struct BlockPingTime
  372. {
  373. Block::UID blockUID;
  374. juce::Time lastPing;
  375. juce::Time connected;
  376. };
  377. juce::Array<BlockPingTime> blockPings;
  378. void updateApiPing (Block::UID uid)
  379. {
  380. const auto now = juce::Time::getCurrentTime();
  381. if (auto* ping = getPing (uid))
  382. {
  383. LOG_PING ("Ping: " << uid << " " << now.formatted ("%Mm %Ss"));
  384. ping->lastPing = now;
  385. }
  386. else
  387. {
  388. LOG_CONNECTIVITY ("API Connected " << uid);
  389. blockPings.add ({ uid, now, now });
  390. detector.handleTopologyChange();
  391. }
  392. }
  393. BlockPingTime* getPing (Block::UID uid)
  394. {
  395. for (auto& ping : blockPings)
  396. if (uid == ping.blockUID)
  397. return &ping;
  398. return nullptr;
  399. }
  400. void removeDeviceInfo (Block::UID uid)
  401. {
  402. currentDeviceInfo.removeIf ([uid] (DeviceInfo& info) { return uid == info.uid; });
  403. }
  404. bool isApiConnected (Block::UID uid)
  405. {
  406. return getPing (uid) != nullptr;
  407. }
  408. void forceApiDisconnected (Block::UID uid)
  409. {
  410. if (isApiConnected (uid))
  411. {
  412. // Clear all known API connections and broadcast an empty topology,
  413. // as DNA blocks connected to the restarting block may be offline.
  414. LOG_CONNECTIVITY ("API Disconnected " << uid << ", re-probing topology");
  415. currentDeviceInfo.clearQuick();
  416. blockPings.clearQuick();
  417. blockNames.clear();
  418. versionNumbers.clear();
  419. detector.handleTopologyChange();
  420. scheduleNewTopologyRequest();
  421. }
  422. }
  423. void checkApiTimeouts (juce::Time now)
  424. {
  425. const auto timedOut = [this, now] (BlockPingTime& ping)
  426. {
  427. if (ping.lastPing >= now - juce::RelativeTime::seconds (pingTimeoutSeconds))
  428. return false;
  429. LOG_CONNECTIVITY ("Ping timeout: " << ping.blockUID);
  430. removeDeviceInfo (ping.blockUID);
  431. return true;
  432. };
  433. if (blockPings.removeIf (timedOut) > 0)
  434. {
  435. scheduleNewTopologyRequest();
  436. detector.handleTopologyChange();
  437. }
  438. }
  439. /* Returns true is ping was removed */
  440. void removePingForDisconnectedBlocks()
  441. {
  442. const auto removed = [this] (auto& ping)
  443. {
  444. for (auto& info : currentDeviceInfo)
  445. if (info.uid == ping.blockUID)
  446. return false;
  447. LOG_CONNECTIVITY ("API Disconnected by topology update " << ping.blockUID);
  448. return true;
  449. };
  450. blockPings.removeIf (removed);
  451. }
  452. void startApiModeOnConnectedBlocks()
  453. {
  454. for (auto& info : currentDeviceInfo)
  455. {
  456. if (! isApiConnected (info.uid))
  457. {
  458. LOG_CONNECTIVITY ("API Try " << info.uid);
  459. sendCommandMessage (info.index, BlocksProtocol::endAPIMode);
  460. sendCommandMessage (info.index, BlocksProtocol::beginAPIMode);
  461. }
  462. }
  463. }
  464. //==============================================================================
  465. void checkVersionNumberTimeouts()
  466. {
  467. for (const auto& device : currentDeviceInfo)
  468. {
  469. const auto version = versionNumbers.find (device.uid);
  470. if (version == versionNumbers.end())
  471. {
  472. auto* ping = getPing (device.uid);
  473. }
  474. }
  475. }
  476. //==============================================================================
  477. Block::UID getDeviceIDFromIndex (BlocksProtocol::TopologyIndex index) const noexcept
  478. {
  479. for (auto& d : currentDeviceInfo)
  480. if (d.index == index)
  481. return d.uid;
  482. return {};
  483. }
  484. Block::UID getDeviceIDFromMessageIndex (BlocksProtocol::TopologyIndex index) noexcept
  485. {
  486. const auto uid = getDeviceIDFromIndex (index);
  487. // re-request topology if we get an event from an unknown block
  488. if (uid == Block::UID())
  489. scheduleNewTopologyRequest();
  490. return uid;
  491. }
  492. //==============================================================================
  493. void handleIncomingMessage (const void* data, size_t dataSize)
  494. {
  495. juce::MemoryBlock mb (data, dataSize);
  496. {
  497. const juce::ScopedLock sl (incomingPacketLock);
  498. incomingPackets.add (std::move (mb));
  499. }
  500. triggerAsyncUpdate();
  501. #if DUMP_BANDWIDTH_STATS
  502. registerBytesIn ((int) dataSize);
  503. #endif
  504. }
  505. void handleAsyncUpdate() override
  506. {
  507. juce::Array<juce::MemoryBlock> packets;
  508. packets.ensureStorageAllocated (32);
  509. {
  510. const juce::ScopedLock sl (incomingPacketLock);
  511. incomingPackets.swapWith (packets);
  512. }
  513. for (auto& packet : packets)
  514. {
  515. auto data = static_cast<const uint8*> (packet.getData());
  516. BlocksProtocol::HostPacketDecoder<ConnectedDeviceGroup>
  517. ::processNextPacket (*this, *data, data + 1, (int) packet.getSize() - 1);
  518. }
  519. }
  520. //==============================================================================
  521. BlocksProtocol::VersionNumber getVersionNumber (Block::UID uid)
  522. {
  523. const auto version = versionNumbers.find (uid);
  524. return version == versionNumbers.end() ? BlocksProtocol::VersionNumber() : version->second;
  525. }
  526. BlocksProtocol::BlockName getName (Block::UID uid)
  527. {
  528. const auto name = blockNames.find (uid);
  529. return name == blockNames.end() ? BlocksProtocol::BlockName() : name->second;
  530. }
  531. //==============================================================================
  532. const DeviceInfo* getDeviceInfoFromUID (Block::UID uid) const noexcept
  533. {
  534. for (auto& d : currentDeviceInfo)
  535. if (d.uid == uid)
  536. return &d;
  537. return nullptr;
  538. }
  539. Block::ConnectionPort convertConnectionPort (Block::UID uid, BlocksProtocol::ConnectorPort p) noexcept
  540. {
  541. if (auto* info = getDeviceInfoFromUID (uid))
  542. return BlocksProtocol::BlockDataSheet (info->serial).convertPortIndexToConnectorPort (p);
  543. jassertfalse;
  544. return { Block::ConnectionPort::DeviceEdge::north, 0 };
  545. }
  546. //==============================================================================
  547. void refreshCurrentDeviceInfo()
  548. {
  549. currentDeviceInfo.clearQuick();
  550. for (auto& device : currentTopologyDevices)
  551. {
  552. const auto uid = getBlockUIDFromSerialNumber (device.serialNumber);
  553. const auto version = getVersionNumber (uid);
  554. const auto name = getName (uid);
  555. // For backwards compatibility we assume the first device we see in a group is the master and won't change
  556. if (masterBlock == 0)
  557. masterBlock = uid;
  558. currentDeviceInfo.add ({ uid,
  559. device.index,
  560. device.serialNumber,
  561. version,
  562. name,
  563. masterBlock == uid });
  564. }
  565. }
  566. void refreshCurrentDeviceConnections()
  567. {
  568. currentDeviceConnections.clearQuick();
  569. for (auto&& c : currentTopologyConnections)
  570. {
  571. BlockDeviceConnection dc;
  572. dc.device1 = getDeviceIDFromIndex (c.device1);
  573. dc.device2 = getDeviceIDFromIndex (c.device2);
  574. if (dc.device1 <= 0 || dc.device2 <= 0)
  575. continue;
  576. dc.connectionPortOnDevice1 = convertConnectionPort (dc.device1, c.port1);
  577. dc.connectionPortOnDevice2 = convertConnectionPort (dc.device2, c.port2);
  578. currentDeviceConnections.add (dc);
  579. }
  580. }
  581. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConnectedDeviceGroup)
  582. };
  583. } // namespace juce