From 86e6f0db4d8349a7efbd98ac5fe5453fb58bd82c Mon Sep 17 00:00:00 2001 From: Dien-Nhung Nguyen-Phu Date: Sun, 23 Jun 2024 19:44:32 +0700 Subject: feat: save received data from BLE to flash --- src/ble/profile/legacy.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'src/ble/profile/legacy.c') diff --git a/src/ble/profile/legacy.c b/src/ble/profile/legacy.c index fc775e5..2957aa5 100644 --- a/src/ble/profile/legacy.c +++ b/src/ble/profile/legacy.c @@ -1,5 +1,9 @@ #include "utils.h" +#include "../../data.h" +#include "../../power.h" +#include "../../leddrv.h" + static const uint16_t ServiceUUID = 0xFEE0; static const gattAttrType_t service = {2, (uint8_t *)&ServiceUUID}; @@ -16,7 +20,35 @@ static gattAttribute_t attr_table[] = { static bStatus_t receive(uint8_t *val, uint16_t len) { - /* TODO: implement data receiving here*/ + static uint16_t c, data_len, n; + static uint8_t *data; + if (len != LEGACY_TRANSFER_WIDTH) { + return ATT_ERR_INVALID_VALUE_SIZE; + } + if (c == 0) { + if (memcmp(val, "wang\0\0", 6)) { + return ATT_ERR_INVALID_VALUE; + } else { + data = malloc(sizeof(data_legacy_t)); + } + } + + memcpy(data + c * len, val, len); + + if (c == 1) { + data_legacy_t *d = (data_legacy_t *)data; + n = bigendian16_sum(d->sizes, 8); + data_len = LEGACY_HEADER_SIZE + LED_ROWS * n; + data = realloc(data, data_len); + } + + if (c > 2 && ((c+1) * LEGACY_TRANSFER_WIDTH) >= data_len) { + data_flatSave(data, data_len); + reset_jump(); + } + + c++; + return SUCCESS; } static bStatus_t write_handler(uint16 connHandle, gattAttribute_t *pAttr, -- cgit v1.2.3