Skip to content

Commit

Permalink
Update data_flash_access.cpp
Browse files Browse the repository at this point in the history
fix bug in the length calculation in the dfWriteBytes()
  • Loading branch information
asilichenko committed Aug 5, 2024
1 parent 9a5ea7e commit 11b6529
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions data_flash_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,25 @@ void dfReadBytes(word addr, byte *retval, int len) {
void dfWriteBytes(word addr, byte *data, int len) {
if (!_isAddrValid(addr) || !isAllowedRequestPayloadSize(len) || _isDeviceSealed()) return;

const byte workloadSize = BlockProtocol::ADDR_SIZE + len;
byte workload[workloadSize];
byte buf[BlockProtocol::ADDR_SIZE + len];

// put the DF address into buffer in Little Endian:
workload[0] = addr & 0xFF;
workload[1] = (addr >> 8) & 0xFF;
buf[0] = addr & 0xFF;
buf[1] = (addr >> 8) & 0xFF;

// fill the buffer with the data:
for (int i = 0; i < len; i++) workload[BlockProtocol::ADDR_SIZE + i] = data[i];
for (int i = 0; i < len; i++) buf[BlockProtocol::ADDR_SIZE + i] = data[i];

// send buffer to the 0x3E AltManufacturerAccess()
sendData(StdCommands::ALT_MANUFACTURER_ACCESS, workload, workloadSize);
sendData(StdCommands::ALT_MANUFACTURER_ACCESS, buf, sizeof(buf));

// calculate the checksum and length:
const byte _checksum = checksum(workload, workloadSize);
const byte _length = workloadSize;
const byte checksumAndLength[BlockProtocol::SERVICE_SIZE] = {_checksum, _length};
const byte _checksum = checksum(buf, sizeof(buf));
const byte _length = len + BlockProtocol::SERVICE_SIZE; // address len + data len + checksum len + length len
const byte checksumAndLength[] = {_checksum, _length};

// send the checksum and length at once:
sendData(StdCommands::MAC_DATA_CHECKSUM, checksumAndLength, BlockProtocol::SERVICE_SIZE);
sendData(StdCommands::MAC_DATA_CHECKSUM, checksumAndLength, sizeof(checksumAndLength));

delay(200);
}
Expand Down

0 comments on commit 11b6529

Please sign in to comment.