Kris Scholte Lubberink
/
SSS_Ble
Ble for smart sOlutions
Diff: source/InterestService.cpp
- Revision:
- 6:ee9c86f06eae
- Child:
- 7:9cda1b0f25ae
diff -r 94adaf1f8d51 -r ee9c86f06eae source/InterestService.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/InterestService.cpp Mon May 20 09:55:38 2019 +0200 @@ -0,0 +1,127 @@ +// +// Created by kris on 7-5-19. +// + +#include "InterestService.h" +#pragma pack(push,1) +struct packed_A { + uint32_t magic; + int value[5]; +}; +#pragma pack(pop) + +InterestService::InterestService(BLE& _ble) : + ble(_ble), + interestCharacteristic( (UUID)CustomUUIDs::UUID_INTEREST_CHAR, + &interests, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NONE) +{ + + static bool serviceAdded = false; /* We should only ever need to add the information service once. */ + if (serviceAdded) { + return; + } + printf("[PERIPHERAL] Adding interest service.. \r\n"); + GattCharacteristic *charTable[] = { &interestCharacteristic }; + + GattService colorService( + CustomUUIDs::UUID_INTEREST_SERVICE, + charTable, + sizeof(charTable) / sizeof(GattCharacteristic *)); + ble.gattServer().addService(colorService); + ble.gattServer().onDataWritten(this, &InterestService::onDataWritten); + readFromFs(); + + serviceAdded = true; +} + +void InterestService::onDataWritten(const GattWriteCallbackParams *writeParams) { + //TODO: You gave me some data... Let's store it! + uint16_t handle = writeParams->handle; + printf("You wrote some data... But for which char? \r\n"); + printf("%d == %d", handle, interestCharacteristic.getValueHandle());//colorCharacteristic.getValueHandle()); + if(handle == interestCharacteristic.getValueHandle()){ + printf("You wrote interests!"); + printf("The value should be an array of 5 integers."); +// color = writeParams->data; + int index = 0; + for (int i=0; i < 5; i++) { + interests[i] = writeParams->data[i];//+3] << 24 | (writeParams->data[i+2] & 0xff) << 16 | (writeParams->data[i+1] & 0xff) << 8| (writeParams->data[i] & 0xff); + index++; + printf("Index: %d, val %08X \r\n", index, interests[i]); + } + writeToFs(); + } +} + +int InterestService::readFromFs() { + enum { MAGIC_VALUE = 0xDEADBEEF }; + mbed::FlashIAP flash_device; + flash_device.init(); + int sector_size = flash_device.get_sector_size(flash_device.get_flash_start() + flash_device.get_flash_size() - 1UL); + int page_size = flash_device.get_page_size(); + int address = (flash_device.get_flash_start() + flash_device.get_flash_size()) - (sector_size); + printf("Sector size: %d \r\n", sector_size); + printf("Page size: %d \r\n", page_size); + printf("Address: %d \r\n", address); + + int buffer[24]; + flash_device.read(buffer, address, 24); + packed_A const *ptr = (packed_A const *) buffer; + + if(ptr->magic == MAGIC_VALUE) { + printf("Address initialized, current list %d,%d,%d,%d,%d \r\n", ptr->value[0], + ptr->value[1], + ptr->value[2], + ptr->value[3], + ptr->value[4]); + interests[0] = ptr->value[0]; + interests[1] = ptr->value[1]; + interests[2] = ptr->value[2]; + interests[3] = ptr->value[3]; + interests[4] = ptr->value[4]; + } else { + printf("Uninitialized, setting up first time"); + packed_A newmemory; + newmemory.magic = MAGIC_VALUE; + newmemory.value[0] = 2; + newmemory.value[1] = 3; + newmemory.value[2] = 4; + newmemory.value[3] = 5; + newmemory.value[4] = 6; + flash_device.erase(address, flash_device.get_sector_size(address)); + flash_device.program(&newmemory, address, sizeof(newmemory)); + printf("Flashed"); + } + return 0; +} + + +int InterestService::writeToFs() { + enum { MAGIC_VALUE = 0xDEADBEEF }; + mbed::FlashIAP flash_device; + flash_device.init(); + packed_A newmemory; + int sector_size = flash_device.get_sector_size(flash_device.get_flash_start() + flash_device.get_flash_size() - 1UL); + int page_size = flash_device.get_page_size(); + int address = (flash_device.get_flash_start() + flash_device.get_flash_size()) - (sector_size); + + newmemory.magic = MAGIC_VALUE; + newmemory.value[0] = interests[0]; + newmemory.value[1] = interests[1]; + newmemory.value[2] = interests[2]; + newmemory.value[3] = interests[3]; + newmemory.value[4] = interests[4]; + flash_device.erase(address, flash_device.get_sector_size(address)); + flash_device.program(&newmemory, address, sizeof(newmemory)); + printf("Flashed"); + return 0; +} + +int InterestService::createFs(){ + return true; +} + +InterestService::~InterestService() { + printf("[PERIPHERAL]\t Destructing InterestService"); +}