From da3cf8d4fd0a2fc2c447d4fbbe97f215071ff01a Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Thu, 8 Jun 2023 09:27:14 +0200 Subject: [PATCH] Allow setting the device start time --- src/bluetooth.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/src/bluetooth.cpp b/src/bluetooth.cpp index f97d441..c9f2371 100644 --- a/src/bluetooth.cpp +++ b/src/bluetooth.cpp @@ -29,6 +29,8 @@ uint8_t* bluetoothDataBuffer; uint8_t* bluetoothResponse; size_t bluetoothDataCount = 0; +RTC_DATA_ATTR uint32_t deviceStartTime = 0; + void bluetoothStartAdvertising(); void bluetoothDidReceiveData(uint8_t* buffer, uint16_t count); @@ -169,6 +171,17 @@ enum class BluetoothRequest: uint8_t { * This may happen when a new temperature recording is performed in between calls */ clearRecordingBuffer = 2, + + /** + * @brief Set the absolute start time of the device + * + * Request: + * - Bytes 1-4: Number of seconds since 1970 (uint32_t) + * + * Response: + * - BluetoothResponse::success + */ + setDeviceStartTime = 3, }; enum class BluetoothResponse: uint8_t { @@ -184,6 +197,9 @@ enum class BluetoothResponse: uint8_t { unknownCommand = 3, invalidNumberOfBytesToDelete = 4, + + /* The request has not been completed yet */ + responseInProgress = 5, }; void setResponse(BluetoothResponse response) { @@ -220,9 +236,6 @@ uint16_t readNumberFromReceivedBuffer(uint8_t* buffer) { void fillInfo() { uint16_t value; - // BluetoothResponse::success - setResponse(BluetoothResponse::success); - // the number of bytes as a uint16_t (2 bytes) value = getTotalNumberOfStoredBytes(); memcpy(bluetoothDataBuffer, &value, sizeof(uint16_t)); @@ -275,9 +288,16 @@ void fillInfo() { memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); bluetoothDataCount += sizeof(uint16_t); + // Total storage size of device value = totalStorageSize; memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); bluetoothDataCount += sizeof(uint16_t); + + // Configured device start time + memcpy(bluetoothDataBuffer + bluetoothDataCount, &deviceStartTime, sizeof(uint32_t)); + bluetoothDataCount += sizeof(uint32_t); + + setResponse(BluetoothResponse::success); } void getRecordingData(uint8_t* buffer, uint16_t count) { @@ -296,6 +316,7 @@ void getRecordingData(uint8_t* buffer, uint16_t count) { memcpy(&byteOffset, buffer + sizeof(uint16_t), sizeof(uint16_t)); bluetoothDataCount = getRecordedBytesAtOffset(bluetoothDataBuffer, byteOffset, byteCount); + setResponseWithoutData(BluetoothResponse::success); } void clearRecordingBuffer(uint8_t* buffer, uint16_t count) { @@ -314,24 +335,40 @@ void clearRecordingBuffer(uint8_t* buffer, uint16_t count) { setResponseWithoutData(BluetoothResponse::success); } +void setDeviceStartTime(uint8_t* buffer, uint16_t count) { + if (count != sizeof(uint32_t)) { + setResponseWithoutData(BluetoothResponse::invalidCommand); + return; + } + memcpy(&deviceStartTime, buffer, sizeof(uint32_t)); + setResponseWithoutData(BluetoothResponse::success); +} + void bluetoothDidReceiveData(uint8_t* buffer, uint16_t count) { if (count < 1) { setResponseWithoutData(BluetoothResponse::invalidCommand); return; } + setResponseWithoutData(BluetoothResponse::responseInProgress); BluetoothRequest request = static_cast(buffer[0]); + uint8_t* payload = buffer + sizeof(uint8_t); + uint16_t payloadSize = count - sizeof(uint8_t); switch (request) { case BluetoothRequest::getInfo: fillInfo(); break; case BluetoothRequest::clearRecordingBuffer: - clearRecordingBuffer(buffer + sizeof(uint8_t), count - sizeof(uint8_t)); + clearRecordingBuffer(payload, payloadSize); break; case BluetoothRequest::getRecordingData: - getRecordingData(buffer + sizeof(uint8_t), count - sizeof(uint8_t)); + getRecordingData(payload, payloadSize); + break; + + case BluetoothRequest::setDeviceStartTime: + setDeviceStartTime(payload, payloadSize); break; default: