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.

362 lines
15KB

  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 BlocksProtocol
  20. {
  21. /**
  22. Parses data packets from a BLOCKS device, and translates them into callbacks
  23. on a handler object
  24. */
  25. template <typename Handler>
  26. struct HostPacketDecoder
  27. {
  28. static void processNextPacket (Handler& handler, TopologyIndex deviceIndex, const void* data, int size)
  29. {
  30. if (Packed7BitArrayReader::checksumIsOK (static_cast<const uint8*> (data), (uint32) size))
  31. {
  32. Packed7BitArrayReader reader (data, size - 1);
  33. if (reader.getRemainingBits() < (int) PacketTimestamp::bits)
  34. {
  35. jassertfalse; // not a valid message..
  36. return;
  37. }
  38. auto packetTimestamp = reader.read<PacketTimestamp>();
  39. deviceIndex &= 63; // top bit is used as a direction indicator
  40. while (processNextMessage (handler, reader, deviceIndex, packetTimestamp))
  41. {}
  42. }
  43. }
  44. static bool processNextMessage (Handler& handler, Packed7BitArrayReader& reader,
  45. TopologyIndex deviceIndex, PacketTimestamp packetTimestamp)
  46. {
  47. if (reader.getRemainingBits() < MessageType::bits)
  48. return false;
  49. auto messageType = reader.read<MessageType>().get();
  50. if (messageType == 0)
  51. return false;
  52. switch ((MessageFromDevice) messageType)
  53. {
  54. case MessageFromDevice::deviceTopology: return handleTopology (handler, reader, true);
  55. case MessageFromDevice::deviceTopologyExtend: return handleTopology (handler, reader, false);
  56. case MessageFromDevice::deviceTopologyEnd: return handleTopologyEnd (handler, reader);
  57. case MessageFromDevice::deviceVersionList: return handleVersion (handler, reader);
  58. case MessageFromDevice::deviceNameList: return handleName (handler, reader);
  59. case MessageFromDevice::touchStart: return handleTouch (handler, reader, deviceIndex, packetTimestamp, true, false);
  60. case MessageFromDevice::touchMove: return handleTouch (handler, reader, deviceIndex, packetTimestamp, false, false);
  61. case MessageFromDevice::touchEnd: return handleTouch (handler, reader, deviceIndex, packetTimestamp, false, true);
  62. case MessageFromDevice::touchStartWithVelocity: return handleTouchWithVelocity (handler, reader, deviceIndex, packetTimestamp, true, false);
  63. case MessageFromDevice::touchMoveWithVelocity: return handleTouchWithVelocity (handler, reader, deviceIndex, packetTimestamp, false, false);
  64. case MessageFromDevice::touchEndWithVelocity: return handleTouchWithVelocity (handler, reader, deviceIndex, packetTimestamp, false, true);
  65. case MessageFromDevice::controlButtonDown: return handleButtonDownOrUp (handler, reader, deviceIndex, packetTimestamp, true);
  66. case MessageFromDevice::controlButtonUp: return handleButtonDownOrUp (handler, reader, deviceIndex, packetTimestamp, false);
  67. case MessageFromDevice::programEventMessage: return handleCustomMessage (handler, reader, deviceIndex, packetTimestamp);
  68. case MessageFromDevice::packetACK: return handlePacketACK (handler, reader, deviceIndex);
  69. case MessageFromDevice::firmwareUpdateACK: return handleFirmwareUpdateACK (handler, reader, deviceIndex);
  70. case MessageFromDevice::configMessage: return handleConfigMessage (handler, reader, deviceIndex);
  71. case MessageFromDevice::logMessage: return handleLogMessage (handler, reader, deviceIndex);
  72. default:
  73. jassertfalse; // got an invalid message type, could be a corrupt packet, or a
  74. // message type that the host doesn't expect to get
  75. return false;
  76. }
  77. }
  78. static bool handleTopology (Handler& handler, Packed7BitArrayReader& reader, bool newTopology)
  79. {
  80. if (reader.getRemainingBits() < DeviceCount::bits + ConnectionCount::bits)
  81. {
  82. jassertfalse; // not enough data available for this message type!
  83. return false;
  84. }
  85. auto deviceProtocolVersion = reader.read<ProtocolVersion>();
  86. if (deviceProtocolVersion > currentProtocolVersion)
  87. {
  88. jassertfalse;
  89. return false;
  90. }
  91. const uint32 numDevices = reader.read<DeviceCount>();
  92. const uint32 numConnections = reader.read<ConnectionCount>();
  93. if ((uint32) reader.getRemainingBits() < numDevices * BitSizes::topologyDeviceInfo
  94. + numConnections * BitSizes::topologyConnectionInfo)
  95. {
  96. jassertfalse; // not enough data available for this message type!
  97. return false;
  98. }
  99. if (newTopology)
  100. handler.beginTopology ((int) numDevices, (int) numConnections);
  101. else
  102. handler.extendTopology ((int) numDevices, (int) numConnections);
  103. for (uint32 i = 0; i < numDevices; ++i)
  104. handleTopologyDevice (handler, reader);
  105. for (uint32 i = 0; i < numConnections; ++i)
  106. handleTopologyConnection (handler, reader);
  107. // Packet must be last in topology, otherwise wait for topology end message
  108. if (numDevices < maxBlocksInTopologyPacket && numConnections < maxConnectionsInTopologyPacket)
  109. handler.endTopology();
  110. return true;
  111. }
  112. static bool handleTopologyEnd (Handler& handler, Packed7BitArrayReader& reader)
  113. {
  114. auto deviceProtocolVersion = reader.read<ProtocolVersion>();
  115. if (deviceProtocolVersion > currentProtocolVersion)
  116. {
  117. jassertfalse;
  118. return false;
  119. }
  120. handler.endTopology();
  121. return true;
  122. }
  123. static void handleTopologyDevice (Handler& handler, Packed7BitArrayReader& reader)
  124. {
  125. DeviceStatus status;
  126. for (uint32 i = 0; i < sizeof (BlockSerialNumber); ++i)
  127. status.serialNumber.serial[i] = (uint8) reader.readBits (7);
  128. status.index = (TopologyIndex) reader.readBits (topologyIndexBits);
  129. status.batteryLevel = reader.read<BatteryLevel>();
  130. status.batteryCharging = reader.read<BatteryCharging>();
  131. handler.handleTopologyDevice (status);
  132. }
  133. static void handleTopologyConnection (Handler& handler, Packed7BitArrayReader& reader)
  134. {
  135. DeviceConnection connection;
  136. connection.device1 = (uint8) reader.readBits (topologyIndexBits);
  137. connection.port1 = reader.read<ConnectorPort>();
  138. connection.device2 = (uint8) reader.readBits (topologyIndexBits);
  139. connection.port2 = reader.read<ConnectorPort>();
  140. handler.handleTopologyConnection (connection);
  141. }
  142. static bool handleVersion (Handler& handler, Packed7BitArrayReader& reader)
  143. {
  144. DeviceVersion version;
  145. version.index = (TopologyIndex) reader.readBits (topologyIndexBits);
  146. version.version.length = (uint8) reader.readBits (7);
  147. for (uint32 i = 0; i < version.version.length; ++i)
  148. version.version.version[i] = (uint8) reader.readBits (7);
  149. handler.handleVersion (version);
  150. return true;
  151. }
  152. static bool handleName (Handler& handler, Packed7BitArrayReader& reader)
  153. {
  154. DeviceName name;
  155. name.index = (TopologyIndex) reader.readBits (topologyIndexBits);
  156. name.name.length = (uint8) reader.readBits (7);
  157. for (uint32 i = 0; i < name.name.length; ++i)
  158. name.name.name[i] = (uint8) reader.readBits (7);
  159. handler.handleName (name);
  160. return true;
  161. }
  162. static bool handleTouch (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex,
  163. PacketTimestamp packetTimestamp, bool isStart, bool isEnd)
  164. {
  165. if (reader.getRemainingBits() < BitSizes::touchMessage - MessageType::bits)
  166. {
  167. jassertfalse; // not enough data available for this message type!
  168. return false;
  169. }
  170. auto timeOffset = reader.read<PacketTimestampOffset>();
  171. auto touchIndex = reader.read<TouchIndex>();
  172. auto x = reader.read<TouchPosition::Xcoord>();
  173. auto y = reader.read<TouchPosition::Ycoord>();
  174. auto z = reader.read<TouchPosition::Zcoord>();
  175. handleTouch (handler, deviceIndex, packetTimestamp.get() + timeOffset.get(),
  176. touchIndex, { x, y, z }, { 0, 0, 0 }, isStart, isEnd);
  177. return true;
  178. }
  179. static bool handleTouchWithVelocity (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex,
  180. PacketTimestamp packetTimestamp, bool isStart, bool isEnd)
  181. {
  182. if (reader.getRemainingBits() < BitSizes::touchMessageWithVelocity - MessageType::bits)
  183. {
  184. jassertfalse; // not enough data available for this message type!
  185. return false;
  186. }
  187. auto timeOffset = reader.read<PacketTimestampOffset>();
  188. auto touchIndex = reader.read<TouchIndex>();
  189. auto x = reader.read<TouchPosition::Xcoord>();
  190. auto y = reader.read<TouchPosition::Ycoord>();
  191. auto z = reader.read<TouchPosition::Zcoord>();
  192. auto vx = reader.read<TouchVelocity::VXcoord>();
  193. auto vy = reader.read<TouchVelocity::VYcoord>();
  194. auto vz = reader.read<TouchVelocity::VZcoord>();
  195. handleTouch (handler, deviceIndex, packetTimestamp.get() + timeOffset.get(),
  196. touchIndex, { x, y, z }, { vx, vy, vz }, isStart, isEnd);
  197. return true;
  198. }
  199. static void handleTouch (Handler& handler, TopologyIndex deviceIndex, uint32 timestamp, TouchIndex touchIndex,
  200. TouchPosition position, TouchVelocity velocity, bool isStart, bool isEnd)
  201. {
  202. handler.handleTouchChange (deviceIndex, timestamp, touchIndex, position, velocity, isStart, isEnd);
  203. }
  204. static bool handleButtonDownOrUp (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex,
  205. PacketTimestamp packetTimestamp, bool isDown)
  206. {
  207. if (reader.getRemainingBits() < BitSizes::controlButtonMessage - MessageType::bits)
  208. {
  209. jassertfalse; // not enough data available for this message type!
  210. return false;
  211. }
  212. auto timeOffset = reader.read<PacketTimestampOffset>();
  213. auto buttonID = reader.read<ControlButtonID>();
  214. handler.handleControlButtonUpDown (deviceIndex, packetTimestamp.get() + timeOffset.get(), buttonID, isDown);
  215. return true;
  216. }
  217. static bool handleCustomMessage (Handler& handler, Packed7BitArrayReader& reader,
  218. TopologyIndex deviceIndex, PacketTimestamp packetTimestamp)
  219. {
  220. if (reader.getRemainingBits() < BitSizes::programEventMessage - MessageType::bits)
  221. {
  222. jassertfalse; // not enough data available for this message type!
  223. return false;
  224. }
  225. int32 data[numProgramMessageInts] = {};
  226. for (uint32 i = 0; i < numProgramMessageInts; ++i)
  227. data[i] = (int32) reader.read<IntegerWithBitSize<32>>().get();
  228. handler.handleCustomMessage (deviceIndex, packetTimestamp.get(), data);
  229. return true;
  230. }
  231. static bool handlePacketACK (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex)
  232. {
  233. if (reader.getRemainingBits() < BitSizes::packetACK - MessageType::bits)
  234. {
  235. jassertfalse; // not enough data available for this message type!
  236. return false;
  237. }
  238. handler.handlePacketACK (deviceIndex, reader.read<PacketCounter>());
  239. return true;
  240. }
  241. static bool handleFirmwareUpdateACK (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex)
  242. {
  243. if (reader.getRemainingBits() < FirmwareUpdateACKCode::bits)
  244. {
  245. jassertfalse; // not enough data available for this message type!
  246. return false;
  247. }
  248. handler.handleFirmwareUpdateACK (deviceIndex, reader.read<FirmwareUpdateACKCode>(), reader.read<FirmwareUpdateACKDetail>());
  249. return true;
  250. }
  251. static bool handleConfigMessage (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex)
  252. {
  253. ConfigCommand type = reader.read<ConfigCommand>().get();
  254. if (type == updateConfig)
  255. {
  256. auto item = (int32) reader.read<IntegerWithBitSize<8>>().get();
  257. auto value = (int32) reader.read<IntegerWithBitSize<32>>().get();
  258. auto min = (int32) reader.read<IntegerWithBitSize<32>>().get();
  259. auto max = (int32) reader.read<IntegerWithBitSize<32>>().get();
  260. handler.handleConfigUpdateMessage (deviceIndex, item, value, min, max);
  261. return true;
  262. }
  263. if (type == setConfig)
  264. {
  265. auto item = (int32) reader.read<IntegerWithBitSize<8>>().get();
  266. auto value = (int32) reader.read<IntegerWithBitSize<32>>().get();
  267. handler.handleConfigSetMessage (deviceIndex, item, value);
  268. return true;
  269. }
  270. if (type == factorySyncEnd)
  271. {
  272. handler.handleConfigFactorySyncEndMessage (deviceIndex);
  273. }
  274. return true;
  275. }
  276. static bool handleLogMessage (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex)
  277. {
  278. String message;
  279. while (reader.getRemainingBits() >= 7)
  280. {
  281. uint32 c = reader.read<IntegerWithBitSize<7>>();
  282. message << (char) c;
  283. }
  284. handler.handleLogMessage (deviceIndex, message);
  285. return true;
  286. }
  287. };
  288. } // namespace BlocksProtocol
  289. } // namespace juce