1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
namespace BlocksProtocol
{
/**
Parses data packets from a BLOCKS device, and translates them into callbacks
on a handler object
@tags{Blocks}
*/
template <typename Handler>
struct HostPacketDecoder
{
static void processNextPacket (Handler& handler, TopologyIndex deviceIndex, const void* data, int size)
{
if (Packed7BitArrayReader::checksumIsOK (static_cast<const uint8*> (data), (uint32) size))
{
Packed7BitArrayReader reader (data, size - 1);
if (reader.getRemainingBits() < (int) PacketTimestamp::bits)
{
jassertfalse; // not a valid message..
return;
}
auto packetTimestamp = reader.read<PacketTimestamp>();
deviceIndex &= 63; // top bit is used as a direction indicator
for (;;)
{
auto nextMessageType = getMessageType (reader);
if (nextMessageType == 0)
break;
if (! processNextMessage (handler, reader, (MessageFromDevice) nextMessageType, deviceIndex, packetTimestamp))
break;
}
}
}
static uint32 getMessageType (Packed7BitArrayReader& reader)
{
if (reader.getRemainingBits() < MessageType::bits)
return 0;
return reader.read<MessageType>().get();
}
static bool processNextMessage (Handler& handler, Packed7BitArrayReader& reader,
MessageFromDevice messageType, TopologyIndex deviceIndex,
PacketTimestamp packetTimestamp)
{
switch (messageType)
{
case MessageFromDevice::deviceTopology: return handleTopology (handler, reader, true);
case MessageFromDevice::deviceTopologyExtend: return handleTopology (handler, reader, false);
case MessageFromDevice::deviceTopologyEnd: return handleTopologyEnd (handler, reader);
case MessageFromDevice::deviceVersion: return handleVersion (handler, reader);
case MessageFromDevice::deviceName: return handleName (handler, reader);
case MessageFromDevice::touchStart: return handleTouch (handler, reader, deviceIndex, packetTimestamp, true, false);
case MessageFromDevice::touchMove: return handleTouch (handler, reader, deviceIndex, packetTimestamp, false, false);
case MessageFromDevice::touchEnd: return handleTouch (handler, reader, deviceIndex, packetTimestamp, false, true);
case MessageFromDevice::touchStartWithVelocity: return handleTouchWithVelocity (handler, reader, deviceIndex, packetTimestamp, true, false);
case MessageFromDevice::touchMoveWithVelocity: return handleTouchWithVelocity (handler, reader, deviceIndex, packetTimestamp, false, false);
case MessageFromDevice::touchEndWithVelocity: return handleTouchWithVelocity (handler, reader, deviceIndex, packetTimestamp, false, true);
case MessageFromDevice::controlButtonDown: return handleButtonDownOrUp (handler, reader, deviceIndex, packetTimestamp, true);
case MessageFromDevice::controlButtonUp: return handleButtonDownOrUp (handler, reader, deviceIndex, packetTimestamp, false);
case MessageFromDevice::programEventMessage: return handleCustomMessage (handler, reader, deviceIndex, packetTimestamp);
case MessageFromDevice::packetACK: return handlePacketACK (handler, reader, deviceIndex);
case MessageFromDevice::firmwareUpdateACK: return handleFirmwareUpdateACK (handler, reader, deviceIndex);
case MessageFromDevice::configMessage: return handleConfigMessage (handler, reader, deviceIndex);
case MessageFromDevice::logMessage: return handleLogMessage (handler, reader, deviceIndex);
default:
jassertfalse; // got an invalid message type, could be a corrupt packet, or a
// message type that the host doesn't expect to get
return false;
}
}
static bool handleTopology (Handler& handler, Packed7BitArrayReader& reader, bool newTopology)
{
if (reader.getRemainingBits() < DeviceCount::bits + ConnectionCount::bits)
{
jassertfalse; // not enough data available for this message type!
return false;
}
auto deviceProtocolVersion = reader.read<ProtocolVersion>();
if (deviceProtocolVersion > currentProtocolVersion)
{
jassertfalse;
return false;
}
const uint32 numDevices = reader.read<DeviceCount>();
const uint32 numConnections = reader.read<ConnectionCount>();
if ((uint32) reader.getRemainingBits() < numDevices * BitSizes::topologyDeviceInfo
+ numConnections * BitSizes::topologyConnectionInfo)
{
jassertfalse; // not enough data available for this message type!
return false;
}
if (newTopology)
handler.beginTopology ((int) numDevices, (int) numConnections);
else
handler.extendTopology ((int) numDevices, (int) numConnections);
for (uint32 i = 0; i < numDevices; ++i)
handleTopologyDevice (handler, reader);
for (uint32 i = 0; i < numConnections; ++i)
handleTopologyConnection (handler, reader);
// Packet must be last in topology, otherwise wait for topology end message
if (numDevices < maxBlocksInTopologyPacket && numConnections < maxConnectionsInTopologyPacket)
handler.endTopology();
return true;
}
static bool handleTopologyEnd (Handler& handler, Packed7BitArrayReader& reader)
{
auto deviceProtocolVersion = reader.read<ProtocolVersion>();
if (deviceProtocolVersion > currentProtocolVersion)
{
jassertfalse;
return false;
}
handler.endTopology();
return true;
}
static void handleTopologyDevice (Handler& handler, Packed7BitArrayReader& reader)
{
DeviceStatus status;
for (uint32 i = 0; i < sizeof (BlockSerialNumber); ++i)
status.serialNumber.serial[i] = (uint8) reader.readBits (7);
status.index = (TopologyIndex) reader.readBits (topologyIndexBits);
status.batteryLevel = reader.read<BatteryLevel>();
status.batteryCharging = reader.read<BatteryCharging>();
handler.handleTopologyDevice (status);
}
static void handleTopologyConnection (Handler& handler, Packed7BitArrayReader& reader)
{
DeviceConnection connection;
connection.device1 = (uint8) reader.readBits (topologyIndexBits);
connection.port1 = reader.read<ConnectorPort>();
connection.device2 = (uint8) reader.readBits (topologyIndexBits);
connection.port2 = reader.read<ConnectorPort>();
handler.handleTopologyConnection (connection);
}
static bool handleVersion (Handler& handler, Packed7BitArrayReader& reader)
{
DeviceVersion version;
version.index = (TopologyIndex) reader.readBits (topologyIndexBits);
version.version.length = (uint8) reader.readBits (7);
for (uint32 i = 0; i < version.version.length; ++i)
version.version.version[i] = (uint8) reader.readBits (7);
handler.handleVersion (version);
return true;
}
static bool handleName (Handler& handler, Packed7BitArrayReader& reader)
{
DeviceName name;
name.index = (TopologyIndex) reader.readBits (topologyIndexBits);
name.name.length = (uint8) reader.readBits (7);
for (uint32 i = 0; i < name.name.length; ++i)
name.name.name[i] = (uint8) reader.readBits (7);
handler.handleName (name);
return true;
}
static bool handleTouch (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex,
PacketTimestamp packetTimestamp, bool isStart, bool isEnd)
{
if (reader.getRemainingBits() < BitSizes::touchMessage - MessageType::bits)
{
jassertfalse; // not enough data available for this message type!
return false;
}
auto timeOffset = reader.read<PacketTimestampOffset>();
auto touchIndex = reader.read<TouchIndex>();
auto x = reader.read<TouchPosition::Xcoord>();
auto y = reader.read<TouchPosition::Ycoord>();
auto z = reader.read<TouchPosition::Zcoord>();
handleTouch (handler, deviceIndex, packetTimestamp.get() + timeOffset.get(),
touchIndex, { x, y, z }, { 0, 0, 0 }, isStart, isEnd);
return true;
}
static bool handleTouchWithVelocity (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex,
PacketTimestamp packetTimestamp, bool isStart, bool isEnd)
{
if (reader.getRemainingBits() < BitSizes::touchMessageWithVelocity - MessageType::bits)
{
jassertfalse; // not enough data available for this message type!
return false;
}
auto timeOffset = reader.read<PacketTimestampOffset>();
auto touchIndex = reader.read<TouchIndex>();
auto x = reader.read<TouchPosition::Xcoord>();
auto y = reader.read<TouchPosition::Ycoord>();
auto z = reader.read<TouchPosition::Zcoord>();
auto vx = reader.read<TouchVelocity::VXcoord>();
auto vy = reader.read<TouchVelocity::VYcoord>();
auto vz = reader.read<TouchVelocity::VZcoord>();
handleTouch (handler, deviceIndex, packetTimestamp.get() + timeOffset.get(),
touchIndex, { x, y, z }, { vx, vy, vz }, isStart, isEnd);
return true;
}
static void handleTouch (Handler& handler, TopologyIndex deviceIndex, uint32 timestamp, TouchIndex touchIndex,
TouchPosition position, TouchVelocity velocity, bool isStart, bool isEnd)
{
handler.handleTouchChange (deviceIndex, timestamp, touchIndex, position, velocity, isStart, isEnd);
}
static bool handleButtonDownOrUp (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex,
PacketTimestamp packetTimestamp, bool isDown)
{
if (reader.getRemainingBits() < BitSizes::controlButtonMessage - MessageType::bits)
{
jassertfalse; // not enough data available for this message type!
return false;
}
auto timeOffset = reader.read<PacketTimestampOffset>();
auto buttonID = reader.read<ControlButtonID>();
handler.handleControlButtonUpDown (deviceIndex, packetTimestamp.get() + timeOffset.get(), buttonID, isDown);
return true;
}
static bool handleCustomMessage (Handler& handler, Packed7BitArrayReader& reader,
TopologyIndex deviceIndex, PacketTimestamp packetTimestamp)
{
if (reader.getRemainingBits() < BitSizes::programEventMessage - MessageType::bits)
{
jassertfalse; // not enough data available for this message type!
return false;
}
int32 data[numProgramMessageInts] = {};
for (uint32 i = 0; i < numProgramMessageInts; ++i)
data[i] = (int32) reader.read<IntegerWithBitSize<32>>().get();
handler.handleCustomMessage (deviceIndex, packetTimestamp.get(), data);
return true;
}
static bool handlePacketACK (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex)
{
if (reader.getRemainingBits() < BitSizes::packetACK - MessageType::bits)
{
jassertfalse; // not enough data available for this message type!
return false;
}
handler.handlePacketACK (deviceIndex, reader.read<PacketCounter>());
return true;
}
static bool handleFirmwareUpdateACK (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex)
{
if (reader.getRemainingBits() < FirmwareUpdateACKCode::bits)
{
jassertfalse; // not enough data available for this message type!
return false;
}
auto ackCode = reader.read<FirmwareUpdateACKCode>();
auto ackDetail = reader.read<FirmwareUpdateACKDetail>();
handler.handleFirmwareUpdateACK (deviceIndex, ackCode, ackDetail);
return true;
}
static bool handleConfigMessage (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex)
{
ConfigCommand type = reader.read<ConfigCommand>().get();
if (type == updateConfig)
{
auto item = (int32) reader.read<IntegerWithBitSize<8>>().get();
auto value = (int32) reader.read<IntegerWithBitSize<32>>().get();
auto min = (int32) reader.read<IntegerWithBitSize<32>>().get();
auto max = (int32) reader.read<IntegerWithBitSize<32>>().get();
handler.handleConfigUpdateMessage (deviceIndex, item, value, min, max);
return true;
}
if (type == setConfig)
{
auto item = (int32) reader.read<IntegerWithBitSize<8>>().get();
auto value = (int32) reader.read<IntegerWithBitSize<32>>().get();
handler.handleConfigSetMessage (deviceIndex, item, value);
return true;
}
if (type == factorySyncEnd)
{
handler.handleConfigFactorySyncEndMessage (deviceIndex);
}
if (type == factorySyncReset)
{
handler.handleConfigFactorySyncResetMessage (deviceIndex);
}
return true;
}
static bool handleLogMessage (Handler& handler, Packed7BitArrayReader& reader, TopologyIndex deviceIndex)
{
String message;
while (reader.getRemainingBits() >= 7)
{
uint32 c = reader.read<IntegerWithBitSize<7>>();
message << (char) c;
}
handler.handleLogMessage (deviceIndex, message);
return true;
}
};
} // namespace BlocksProtocol
} // namespace juce
|