Allow setting the device start time

This commit is contained in:
Christoph Hagen 2023-06-08 09:27:14 +02:00
parent 675fb4f771
commit da3cf8d4fd

View File

@ -29,6 +29,8 @@ uint8_t* bluetoothDataBuffer;
uint8_t* bluetoothResponse; uint8_t* bluetoothResponse;
size_t bluetoothDataCount = 0; size_t bluetoothDataCount = 0;
RTC_DATA_ATTR uint32_t deviceStartTime = 0;
void bluetoothStartAdvertising(); void bluetoothStartAdvertising();
void bluetoothDidReceiveData(uint8_t* buffer, uint16_t count); 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 * This may happen when a new temperature recording is performed in between calls
*/ */
clearRecordingBuffer = 2, 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 { enum class BluetoothResponse: uint8_t {
@ -184,6 +197,9 @@ enum class BluetoothResponse: uint8_t {
unknownCommand = 3, unknownCommand = 3,
invalidNumberOfBytesToDelete = 4, invalidNumberOfBytesToDelete = 4,
/* The request has not been completed yet */
responseInProgress = 5,
}; };
void setResponse(BluetoothResponse response) { void setResponse(BluetoothResponse response) {
@ -220,9 +236,6 @@ uint16_t readNumberFromReceivedBuffer(uint8_t* buffer) {
void fillInfo() { void fillInfo() {
uint16_t value; uint16_t value;
// BluetoothResponse::success
setResponse(BluetoothResponse::success);
// the number of bytes as a uint16_t (2 bytes) // the number of bytes as a uint16_t (2 bytes)
value = getTotalNumberOfStoredBytes(); value = getTotalNumberOfStoredBytes();
memcpy(bluetoothDataBuffer, &value, sizeof(uint16_t)); memcpy(bluetoothDataBuffer, &value, sizeof(uint16_t));
@ -275,9 +288,16 @@ void fillInfo() {
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += sizeof(uint16_t); bluetoothDataCount += sizeof(uint16_t);
// Total storage size of device
value = totalStorageSize; value = totalStorageSize;
memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t)); memcpy(bluetoothDataBuffer + bluetoothDataCount, &value, sizeof(uint16_t));
bluetoothDataCount += 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) { 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)); memcpy(&byteOffset, buffer + sizeof(uint16_t), sizeof(uint16_t));
bluetoothDataCount = getRecordedBytesAtOffset(bluetoothDataBuffer, byteOffset, byteCount); bluetoothDataCount = getRecordedBytesAtOffset(bluetoothDataBuffer, byteOffset, byteCount);
setResponseWithoutData(BluetoothResponse::success);
} }
void clearRecordingBuffer(uint8_t* buffer, uint16_t count) { void clearRecordingBuffer(uint8_t* buffer, uint16_t count) {
@ -314,24 +335,40 @@ void clearRecordingBuffer(uint8_t* buffer, uint16_t count) {
setResponseWithoutData(BluetoothResponse::success); 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) { void bluetoothDidReceiveData(uint8_t* buffer, uint16_t count) {
if (count < 1) { if (count < 1) {
setResponseWithoutData(BluetoothResponse::invalidCommand); setResponseWithoutData(BluetoothResponse::invalidCommand);
return; return;
} }
setResponseWithoutData(BluetoothResponse::responseInProgress);
BluetoothRequest request = static_cast<BluetoothRequest>(buffer[0]); BluetoothRequest request = static_cast<BluetoothRequest>(buffer[0]);
uint8_t* payload = buffer + sizeof(uint8_t);
uint16_t payloadSize = count - sizeof(uint8_t);
switch (request) { switch (request) {
case BluetoothRequest::getInfo: case BluetoothRequest::getInfo:
fillInfo(); fillInfo();
break; break;
case BluetoothRequest::clearRecordingBuffer: case BluetoothRequest::clearRecordingBuffer:
clearRecordingBuffer(buffer + sizeof(uint8_t), count - sizeof(uint8_t)); clearRecordingBuffer(payload, payloadSize);
break; break;
case BluetoothRequest::getRecordingData: case BluetoothRequest::getRecordingData:
getRecordingData(buffer + sizeof(uint8_t), count - sizeof(uint8_t)); getRecordingData(payload, payloadSize);
break;
case BluetoothRequest::setDeviceStartTime:
setDeviceStartTime(payload, payloadSize);
break; break;
default: default: