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.

707 lines
23KB

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