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.

298 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. /**
  24. Parses data packets from a BLOCKS device, and translates them into callbacks
  25. on a handler object
  26. */
  27. template <typename Handler>
  28. struct HostPacketDecoder
  29. {
  30. static void processNextPacket (Handler& handler, TopologyIndex deviceIndex, const void* data, int size)
  31. {
  32. if (Packed7BitArrayReader::checksumIsOK (static_cast<const uint8*> (data), (uint32) size))
  33. {
  34. Packed7BitArrayReader reader (data, size - 1);
  35. if (reader.getRemainingBits() < (int) PacketTimestamp::bits)
  36. {
  37. jassertfalse; // not a valid message..
  38. return;
  39. }
  40. auto packetTimestamp = reader.read<PacketTimestamp>();
  41. deviceIndex &= 63; // top bit is used as a direction indicator
  42. while (processNextMessage (handler, reader, deviceIndex, packetTimestamp))
  43. {}
  44. }
  45. }
  46. static bool processNextMessage (Handler& handler, Packed7BitArrayReader& reader,
  47. TopologyIndex deviceIndex, PacketTimestamp packetTimestamp)
  48. {
  49. if (reader.getRemainingBits() < MessageType::bits)
  50. return false;
  51. auto messageType = reader.read<MessageType>().get();
  52. if (messageType == 0)
  53. return false;
  54. switch ((MessageFromDevice) messageType)
  55. {
  56. case MessageFromDevice::deviceTopology: return handleTopology (handler, reader, true);
  57. case MessageFromDevice::deviceTopologyExtend: return handleTopology (handler, reader, false);
  58. case MessageFromDevice::deviceTopologyEnd: return handleTopologyEnd (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::logMessage: return handleLogMessage (handler, reader, deviceIndex);
  71. default:
  72. jassertfalse; // got an invalid message type, could be a corrupt packet, or a
  73. // message type that the host doesn't expect to get
  74. return false;
  75. }
  76. }
  77. static bool handleTopology (Handler& handler, Packed7BitArrayReader& reader, bool newTopology)
  78. {
  79. if (reader.getRemainingBits() < DeviceCount::bits + ConnectionCount::bits)
  80. {
  81. jassertfalse; // not enough data available for this message type!
  82. return false;
  83. }
  84. auto deviceProtocolVersion = reader.read<ProtocolVersion>();
  85. if (deviceProtocolVersion > currentProtocolVersion)
  86. {
  87. jassertfalse;
  88. return false;
  89. }
  90. const uint32 numDevices = reader.read<DeviceCount>();
  91. const uint32 numConnections = reader.read<ConnectionCount>();
  92. if ((uint32) reader.getRemainingBits() < numDevices * BitSizes::topologyDeviceInfo
  93. + numConnections * BitSizes::topologyConnectionInfo)
  94. {
  95. jassertfalse; // not enough data available for this message type!
  96. return false;
  97. }
  98. if (newTopology)
  99. handler.beginTopology ((int) numDevices, (int) numConnections);
  100. for (uint32 i = 0; i < numDevices; ++i)
  101. handleTopologyDevice (handler, reader);
  102. for (uint32 i = 0; i < numConnections; ++i)
  103. handleTopologyConnection (handler, reader);
  104. // Packet must be last in topology, otherwise wait for topology end message
  105. if (numDevices < maxBlocksInTopologyPacket && numConnections < maxConnectionsInTopologyPacket)
  106. handler.endTopology();
  107. return true;
  108. }
  109. static bool handleTopologyEnd (Handler& handler, Packed7BitArrayReader& reader)
  110. {
  111. auto deviceProtocolVersion = reader.read<ProtocolVersion>();
  112. if (deviceProtocolVersion > currentProtocolVersion)
  113. {
  114. jassertfalse;
  115. return false;
  116. }
  117. handler.endTopology();
  118. return true;
  119. }
  120. static void handleTopologyDevice (Handler& handler, Packed7BitArrayReader& reader)
  121. {
  122. DeviceStatus status;
  123. for (uint32 i = 0; i < sizeof (BlockSerialNumber); ++i)
  124. status.serialNumber.serial[i] = (uint8) reader.readBits (7);
  125. status.index = (TopologyIndex) reader.readBits (topologyIndexBits);
  126. status.batteryLevel = reader.read<BatteryLevel>();
  127. status.batteryCharging = reader.read<BatteryCharging>();
  128. handler.handleTopologyDevice (status);
  129. }
  130. static void handleTopologyConnection (Handler& handler, Packed7BitArrayReader& reader)
  131. {
  132. DeviceConnection connection;
  133. connection.device1 = (uint8) reader.readBits (topologyIndexBits);
  134. connection.port1 = reader.read<ConnectorPort>();
  135. connection.device2 = (uint8) reader.readBits (topologyIndexBits);
  136. connection.port2 = reader.read<ConnectorPort>();
  137. handler.handleTopologyConnection (connection);
  138. }
  139. static bool handleTouch (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex,
  140. PacketTimestamp packetTimestamp, bool isStart, bool isEnd)
  141. {
  142. if (reader.getRemainingBits() < BitSizes::touchMessage - MessageType::bits)
  143. {
  144. jassertfalse; // not enough data available for this message type!
  145. return false;
  146. }
  147. auto timeOffset = reader.read<PacketTimestampOffset>();
  148. auto touchIndex = reader.read<TouchIndex>();
  149. auto x = reader.read<TouchPosition::Xcoord>();
  150. auto y = reader.read<TouchPosition::Ycoord>();
  151. auto z = reader.read<TouchPosition::Zcoord>();
  152. handleTouch (handler, deviceIndex, packetTimestamp.get() + timeOffset.get(),
  153. touchIndex, { x, y, z }, { 0, 0, 0 }, isStart, isEnd);
  154. return true;
  155. }
  156. static bool handleTouchWithVelocity (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex,
  157. PacketTimestamp packetTimestamp, bool isStart, bool isEnd)
  158. {
  159. if (reader.getRemainingBits() < BitSizes::touchMessageWithVelocity - MessageType::bits)
  160. {
  161. jassertfalse; // not enough data available for this message type!
  162. return false;
  163. }
  164. auto timeOffset = reader.read<PacketTimestampOffset>();
  165. auto touchIndex = reader.read<TouchIndex>();
  166. auto x = reader.read<TouchPosition::Xcoord>();
  167. auto y = reader.read<TouchPosition::Ycoord>();
  168. auto z = reader.read<TouchPosition::Zcoord>();
  169. auto vx = reader.read<TouchVelocity::VXcoord>();
  170. auto vy = reader.read<TouchVelocity::VYcoord>();
  171. auto vz = reader.read<TouchVelocity::VZcoord>();
  172. handleTouch (handler, deviceIndex, packetTimestamp.get() + timeOffset.get(),
  173. touchIndex, { x, y, z }, { vx, vy, vz }, isStart, isEnd);
  174. return true;
  175. }
  176. static void handleTouch (Handler& handler, TopologyIndex deviceIndex, uint32 timestamp, TouchIndex touchIndex,
  177. TouchPosition position, TouchVelocity velocity, bool isStart, bool isEnd)
  178. {
  179. handler.handleTouchChange (deviceIndex, timestamp, touchIndex, position, velocity, isStart, isEnd);
  180. }
  181. static bool handleButtonDownOrUp (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex,
  182. PacketTimestamp packetTimestamp, bool isDown)
  183. {
  184. if (reader.getRemainingBits() < BitSizes::controlButtonMessage - MessageType::bits)
  185. {
  186. jassertfalse; // not enough data available for this message type!
  187. return false;
  188. }
  189. auto timeOffset = reader.read<PacketTimestampOffset>();
  190. auto buttonID = reader.read<ControlButtonID>();
  191. handler.handleControlButtonUpDown (deviceIndex, packetTimestamp.get() + timeOffset.get(), buttonID, isDown);
  192. return true;
  193. }
  194. static bool handleCustomMessage (Handler& handler, Packed7BitArrayReader& reader,
  195. TopologyIndex deviceIndex, PacketTimestamp packetTimestamp)
  196. {
  197. if (reader.getRemainingBits() < BitSizes::programEventMessage - MessageType::bits)
  198. {
  199. jassertfalse; // not enough data available for this message type!
  200. return false;
  201. }
  202. int32 data[numProgramMessageInts] = {};
  203. for (uint32 i = 0; i < numProgramMessageInts; ++i)
  204. data[i] = (int32) reader.read<IntegerWithBitSize<32>>().get();
  205. handler.handleCustomMessage (deviceIndex, packetTimestamp.get(), data);
  206. return true;
  207. }
  208. static bool handlePacketACK (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex)
  209. {
  210. if (reader.getRemainingBits() < BitSizes::packetACK - MessageType::bits)
  211. {
  212. jassertfalse; // not enough data available for this message type!
  213. return false;
  214. }
  215. handler.handlePacketACK (deviceIndex, reader.read<PacketCounter>());
  216. return true;
  217. }
  218. static bool handleFirmwareUpdateACK (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex)
  219. {
  220. if (reader.getRemainingBits() < FirmwareUpdateACKCode::bits)
  221. {
  222. jassertfalse; // not enough data available for this message type!
  223. return false;
  224. }
  225. handler.handleFirmwareUpdateACK (deviceIndex, reader.read<FirmwareUpdateACKCode>());
  226. return true;
  227. }
  228. static bool handleLogMessage (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex)
  229. {
  230. String message;
  231. while (reader.getRemainingBits() >= 7)
  232. {
  233. uint32 c = reader.read<IntegerWithBitSize<7>>();
  234. message << (char) c;
  235. }
  236. handler.handleLogMessage (deviceIndex, message);
  237. return true;
  238. }
  239. };