Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions components/core/Bus/RaftBusDevicesIF.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ class RaftBusDevicesIF
uint16_t maxRecCount, RaftBusDeviceDecodeState& decodeState) const = 0;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Get the latest decoded poll response if one has arrived since the previous call
/// @brief Get the latest decoded poll response (its newest record, for a poll holding several) if one has
/// arrived since the previous call
/// (from any caller). Does not drain the stored results, but each value is reported once,
/// so two readers of the same device see it only between them - use
/// peekLatestDecodedPollResponse() for the current value regardless.
Expand All @@ -157,7 +158,7 @@ class RaftBusDevicesIF
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Get the most recent decoded poll response whether or not it has been read before
/// @brief Get the most recent decoded poll response (its newest record) whether or not it has been read before
/// (a true peek: it leaves what getLatestDecodedPollResponse() reports untouched)
/// @param address address of device to get data from
/// @param pStructOut pointer to structure to receive decoded data
Expand Down
49 changes: 20 additions & 29 deletions components/core/DeviceManager/DeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2900,7 +2900,7 @@ bool DeviceManager::resolveDeviceNameToID(const String& name, RaftDeviceID& devi
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Get a named value from a bus-discovered device using its cached poll data
/// @param deviceID Device identifier on the bus
/// @param paramName Attribute name (e.g. "ax", "temperature")
/// @param paramName Attribute name (e.g. "ax", "temperature"), or an array attribute's element ("diffs[3]")
/// @param isValid (out) true if value was found and valid
/// @return attribute value (with divisor/addend applied)
double DeviceManager::getBusDeviceNamedValue(RaftDeviceID deviceID, const String& paramName, bool& isValid)
Expand All @@ -2927,17 +2927,25 @@ double DeviceManager::getBusDeviceNamedValue(RaftDeviceID deviceID, const String
if (!devTypeRec.pollFieldDescs || devTypeRec.pollFieldCount == 0 || devTypeRec.pollStructSize == 0)
return 0.0;

// Find the matching field by name
// Find the matching field by name, with an element index for an array attribute ("diffs[3]")
String fieldName = paramName;
uint16_t elemIdx = 0;
int bracketPos = paramName.indexOf('[');
if ((bracketPos > 0) && paramName.endsWith("]"))
{
fieldName = paramName.substring(0, bracketPos);
elemIdx = (uint16_t)strtoul(paramName.substring(bracketPos + 1, paramName.length() - 1).c_str(), nullptr, 10);
}
const AttrFieldDesc* pMatchedField = nullptr;
for (uint16_t i = 0; i < devTypeRec.pollFieldCount; i++)
{
if (paramName.equalsIgnoreCase(devTypeRec.pollFieldDescs[i].name))
if (fieldName.equalsIgnoreCase(devTypeRec.pollFieldDescs[i].name))
{
pMatchedField = &devTypeRec.pollFieldDescs[i];
break;
}
}
if (!pMatchedField)
if (!pMatchedField || (elemIdx >= pMatchedField->elemCount()))
return 0.0;

// Get decoded poll data from the bus
Expand All @@ -2952,36 +2960,19 @@ double DeviceManager::getBusDeviceNamedValue(RaftDeviceID deviceID, const String
uint8_t decodedBuf[devTypeRec.pollStructSize];
memset(decodedBuf, 0, devTypeRec.pollStructSize);

// Peek the latest value: neither drains the ring buffer (which would starve the publish
// pipeline) nor takes the "new" state another reader of getLatestDecodedPollResponse() uses,
// and gives the current value between polls rather than nothing
// Peek the latest value (the newest record of the latest poll): neither drains the ring buffer (which would
// starve the publish pipeline) nor takes the "new" state another reader of getLatestDecodedPollResponse()
// uses, and gives the current value between polls rather than nothing
RaftBusDeviceDecodeState decodeState;
if (!pDevicesIF->peekLatestDecodedPollResponse(addr, decodedBuf, devTypeRec.pollStructSize, decodeState))
return 0.0;

// Read the value from the decoded buffer at the field's offset
double rawVal = 0.0;
const uint8_t* pField = decodedBuf + pMatchedField->offset;
switch (pMatchedField->type)
{
case AttrType::Float: { float v; memcpy(&v, pField, sizeof(v)); rawVal = v; break; }
case AttrType::Int32: { int32_t v; memcpy(&v, pField, sizeof(v)); rawVal = v; break; }
case AttrType::Uint32: { uint32_t v; memcpy(&v, pField, sizeof(v)); rawVal = v; break; }
case AttrType::Int16: { int16_t v; memcpy(&v, pField, sizeof(v)); rawVal = v; break; }
case AttrType::Uint16: { uint16_t v; memcpy(&v, pField, sizeof(v)); rawVal = v; break; }
case AttrType::Int8: { int8_t v = *reinterpret_cast<const int8_t*>(pField); rawVal = v; break; }
case AttrType::Uint8: { rawVal = *pField; break; }
case AttrType::Bool: { rawVal = (*pField) ? 1.0 : 0.0; break; }
}

// Apply what is still to be applied - the field table's divisor and addend are 1 and 0
// where the decode has already scaled the value (see DecodeGenerator get_field_desc_def)
if (pMatchedField->divisor != 0.0f && pMatchedField->divisor != 1.0f)
rawVal /= pMatchedField->divisor;
rawVal += pMatchedField->addend;

// The value, as every on-device consumer reads it
double value = 0.0;
if (!pMatchedField->getValue(decodedBuf, devTypeRec.pollStructSize, elemIdx, value))
return 0.0;
isValid = true;
return rawVal;
return value;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
58 changes: 58 additions & 0 deletions components/core/DeviceTypes/DeviceTypeRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,64 @@ struct AttrFieldDesc
float divisor; // Divisor from resp.a[].d (1.0 = no division)
float addend; // Addend from resp.a[].a (applied after division)
uint16_t count; // Elements in the field (>1 for array attributes; 0 treated as 1)

// Reading a value from a decoded record. Every on-device consumer (DeviceManager named values, RaftMicroPy,
// RaftROS) uses these so that they all give the same value for the same field

/// @brief Number of elements (1 for a scalar attribute)
uint16_t elemCount() const { return count ? count : 1; }

/// @brief Bytes per element in the decoded struct
/// @note the type is the struct field's C type except for the few wider fields: int64 and uint64 are described
/// as Int32 and Uint32 and double as Float (no RaftCore record has a double; BLEBTHome has a uint64).
/// Such a field is read as its low 4 bytes, an element of an array of them at the wrong stride
uint32_t elemBytes() const
{
switch (type)
{
case AttrType::Float: case AttrType::Int32: case AttrType::Uint32: return 4;
case AttrType::Int16: case AttrType::Uint16: return 2;
default: return 1;
}
}

/// @brief True if the value is a whole number: an integer field with nothing still to scale
bool isIntegral() const
{
return (type != AttrType::Float) && ((divisor == 0.0f) || (divisor == 1.0f)) && (addend == 0.0f);
}

/// @brief Get one element's value from a decoded record (one poll struct)
/// @param pRecord the decoded record
/// @param recordSize the record's size (the record type's pollStructSize)
/// @param elemIdx element index (0 for a scalar attribute)
/// @param value (out) the value, with the field's divisor and addend applied - what is still to be applied
/// (1 and 0 where the decode has scaled the value already, see DecodeGenerator get_field_desc_def)
/// @return false if the element is outside the record
bool getValue(const void* pRecord, uint32_t recordSize, uint16_t elemIdx, double& value) const
{
value = 0.0;
uint32_t bytes = elemBytes();
uint32_t pos = offset + (uint32_t)elemIdx * bytes;
if (!pRecord || (elemIdx >= elemCount()) || (pos + bytes > recordSize))
return false;
const uint8_t* p = static_cast<const uint8_t*>(pRecord) + pos;
switch (type)
{
case AttrType::Float: { float v; memcpy(&v, p, sizeof(v)); value = v; break; }
case AttrType::Int32: { int32_t v; memcpy(&v, p, sizeof(v)); value = v; break; }
case AttrType::Uint32: { uint32_t v; memcpy(&v, p, sizeof(v)); value = v; break; }
case AttrType::Int16: { int16_t v; memcpy(&v, p, sizeof(v)); value = v; break; }
case AttrType::Uint16: { uint16_t v; memcpy(&v, p, sizeof(v)); value = v; break; }
case AttrType::Int8: { value = (int8_t)*p; break; }
case AttrType::Uint8: { value = *p; break; }
case AttrType::Bool: { value = (*p != 0) ? 1.0 : 0.0; break; }
}
if ((divisor != 0.0f) && (divisor != 1.0f))
value /= divisor;
value += addend;
return true;
}
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
86 changes: 86 additions & 0 deletions components/core/DeviceTypes/DeviceTypeRecords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,92 @@ DeviceTypeRecordDecodeFn DeviceTypeRecords::getPollDecodeFn(DeviceTypeIndexType
return pRec ? __atomic_load_n(&pRec->pollResultDecodeFn, __ATOMIC_ACQUIRE) : nullptr;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Decode poll data of a device type, padding a record cut short (see the header)
uint32_t DeviceTypeRecords::decodePollData(DeviceTypeIndexType deviceTypeIndex, const uint8_t* pData, uint32_t dataLen,
void* pStructOut, uint32_t structOutSize, uint16_t maxRecCount, RaftBusDeviceDecodeState& decodeState) const
{
// The decode function is published after the fields it uses (release), so a record whose function is seen
// has its sizes set
DeviceTypeRecordDecodeFn decodeFn = getPollDecodeFn(deviceTypeIndex);
const DeviceTypeRecord* pRec = getRecord(deviceTypeIndex);
if (!decodeFn || !pRec)
return 0;
return decodePollData(decodeFn, pRec->pollDataSizeBytes, pData, dataLen, pStructOut, structOutSize,
maxRecCount, decodeState);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Decode poll data of a device type and keep only the newest record (see the header)
uint32_t DeviceTypeRecords::decodeNewestPollRecord(DeviceTypeIndexType deviceTypeIndex, const uint8_t* pData,
uint32_t dataLen, void* pStructOut, uint32_t structOutSize, RaftBusDeviceDecodeState& decodeState) const
{
DeviceTypeRecordDecodeFn decodeFn = getPollDecodeFn(deviceTypeIndex);
const DeviceTypeRecord* pRec = getRecord(deviceTypeIndex);
if (!decodeFn || !pRec)
return 0;
return decodeNewestPollRecord(decodeFn, pRec->pollDataSizeBytes, pRec->pollStructSize, pData, dataLen,
pStructOut, structOutSize, decodeState);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Decode poll data with a given decode function, padding a record cut short (see the header)
uint32_t DeviceTypeRecords::decodePollData(DeviceTypeRecordDecodeFn decodeFn, uint32_t pollDataSizeBytes,
const uint8_t* pData, uint32_t dataLen, void* pStructOut, uint32_t structOutSize,
uint16_t maxRecCount, RaftBusDeviceDecodeState& decodeState)
{
if (!decodeFn || !pData || !pStructOut || (dataLen == 0) || (maxRecCount == 0))
return 0;

// Whole records are decoded as they are
const uint32_t recBytes = pollDataSizeBytes + DevicePollingInfo::POLL_RESULT_TIMESTAMP_SIZE;
const uint32_t partBytes = dataLen % recBytes;
if (partBytes == 0)
return decodeFn(pData, dataLen, pStructOut, structOutSize, maxRecCount, decodeState);

// A last record without even its timestamp isn't a record
if (partBytes < DevicePollingInfo::POLL_RESULT_TIMESTAMP_SIZE)
return (dataLen > partBytes) ?
decodeFn(pData, dataLen - partBytes, pStructOut, structOutSize, maxRecCount, decodeState) : 0;

// Pad the last record with zeros to the record size, as the poll store does
std::vector<uint8_t> padded(pData, pData + dataLen);
padded.resize(dataLen - partBytes + recBytes, 0);
return decodeFn(padded.data(), padded.size(), pStructOut, structOutSize, maxRecCount, decodeState);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Decode poll data with a given decode function and keep only the newest record (see the header)
uint32_t DeviceTypeRecords::decodeNewestPollRecord(DeviceTypeRecordDecodeFn decodeFn, uint32_t pollDataSizeBytes,
uint32_t pollStructSize, const uint8_t* pData, uint32_t dataLen, void* pStructOut,
uint32_t structOutSize, RaftBusDeviceDecodeState& decodeState, uint8_t* pScratch, uint32_t scratchSize)
{
if (!decodeFn || (pollStructSize == 0) || !pStructOut || (structOutSize < pollStructSize))
return 0;

// Working space for every record the data holds
std::vector<uint8_t> allocated;
if (!pScratch)
{
uint32_t maxRecs = std::min<uint32_t>(NEWEST_DECODE_MAX_RECORDS,
std::max<uint32_t>(1, NEWEST_DECODE_MAX_BYTES / pollStructSize));
allocated.resize(maxRecs * pollStructSize);
pScratch = allocated.data();
scratchSize = allocated.size();
}
uint32_t maxRecs = std::min<uint32_t>(scratchSize / pollStructSize, UINT16_MAX);
if (maxRecs == 0)
return 0;

// Decode, then keep the newest
uint32_t numRecs = decodePollData(decodeFn, pollDataSizeBytes, pData, dataLen, pScratch,
maxRecs * pollStructSize, (uint16_t)maxRecs, decodeState);
if ((numRecs == 0) || (numRecs > maxRecs))
return 0;
memcpy(pStructOut, pScratch + (numRecs - 1) * pollStructSize, pollStructSize);
return numRecs;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Prepare the runtime decoder of a record just added (not yet published) with no compiled decoder:
/// with policy OnIdentify, check that it can be decoded from its attribute list and mark it Pending (its
Expand Down
50 changes: 50 additions & 0 deletions components/core/DeviceTypes/DeviceTypeRecords.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,56 @@ class DeviceTypeRecords
/// @return poll decode function
DeviceTypeRecordDecodeFn getPollDecodeFn(uint16_t deviceTypeIndex) const;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Decoding poll data. Decoders read whole records of the record type's size (resp.b plus the timestamp), as
// the bus's poll store keeps them. But a data subscriber (DeviceManager::registerForDeviceData) and the bus's
// latest value get each poll result at its actual length, which is shorter for a poll with a variable read
// length (e.g. a FIFO read, r{...}): given to a decoder as it is, it decodes to nothing. These functions pad a
// record cut short with zeros, as the poll store does. Every on-device consumer decodes through them

/// @brief Decode poll data of a device type
/// @param deviceTypeIndex device type index
/// @param pData poll data: one or more poll results, the last of which may be short
/// @param dataLen length of the data
/// @param pStructOut (out) decoded records (the record type's poll struct)
/// @param structOutSize size of pStructOut in bytes
/// @param maxRecCount maximum number of records to decode (a poll result can hold several, e.g. a FIFO read)
/// @param decodeState decode state, kept between calls for the same device
/// @return number of records decoded (0 if the type has no decoder)
uint32_t decodePollData(DeviceTypeIndexType deviceTypeIndex, const uint8_t* pData, uint32_t dataLen,
void* pStructOut, uint32_t structOutSize, uint16_t maxRecCount,
RaftBusDeviceDecodeState& decodeState) const;

/// @brief Decode poll data and keep only the newest record
/// @param pStructOut (out) the newest record (one poll struct)
/// @param structOutSize size of pStructOut in bytes (at least the record type's pollStructSize)
/// @return number of records the data held (0 if none, or the type has no decoder); pStructOut has the newest
/// @note a poll result can hold up to NEWEST_DECODE_MAX_RECORDS records; beyond that the newest are dropped
uint32_t decodeNewestPollRecord(DeviceTypeIndexType deviceTypeIndex, const uint8_t* pData, uint32_t dataLen,
void* pStructOut, uint32_t structOutSize, RaftBusDeviceDecodeState& decodeState) const;

/// @brief Decode poll data with a given decode function (for a consumer that keeps a record's decoder)
/// @param decodeFn the record type's decode function
/// @param pollDataSizeBytes the record type's pollDataSizeBytes (resp.b)
static uint32_t decodePollData(DeviceTypeRecordDecodeFn decodeFn, uint32_t pollDataSizeBytes,
const uint8_t* pData, uint32_t dataLen, void* pStructOut, uint32_t structOutSize,
uint16_t maxRecCount, RaftBusDeviceDecodeState& decodeState);

/// @brief Decode poll data with a given decode function and keep only the newest record
/// @param pollStructSize the record type's pollStructSize
/// @param pScratch working space for the records decoded (nullptr to allocate it); a consumer on the bus task
/// passes its own so that nothing is allocated there
/// @param scratchSize size of pScratch in bytes (records beyond what fits are dropped, the newest first)
static uint32_t decodeNewestPollRecord(DeviceTypeRecordDecodeFn decodeFn, uint32_t pollDataSizeBytes,
uint32_t pollStructSize, const uint8_t* pData, uint32_t dataLen, void* pStructOut,
uint32_t structOutSize, RaftBusDeviceDecodeState& decodeState,
uint8_t* pScratch = nullptr, uint32_t scratchSize = 0);

// The working space decodeNewestPollRecord allocates: up to NEWEST_DECODE_MAX_RECORDS records (an LSM6DS FIFO
// read holds up to 20), within NEWEST_DECODE_MAX_BYTES
static constexpr uint32_t NEWEST_DECODE_MAX_RECORDS = 32;
static constexpr uint32_t NEWEST_DECODE_MAX_BYTES = 4096;

// Where a record's poll decoder comes from: compiled into the firmware (generated, or inherited by an override
// with the same response layout), made at runtime from its attribute list (RAFT_RUNTIME_DECODE), or none
enum class DecoderKind : uint8_t
Expand Down
Loading
Loading