Kris Scholte Lubberink
/
SSS_Ble
Ble for smart sOlutions
source/InterestService.cpp
- Committer:
- kris@kris-X682X
- Date:
- 2019-06-11
- Revision:
- 9:92d861703f96
- Parent:
- 7:9cda1b0f25ae
- Child:
- 10:d845189d146e
File content as of revision 9:92d861703f96:
// // Created by kris on 7-5-19. // #include <wait_api.h> #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 interestService( CustomUUIDs::UUID_INTEREST_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); ble.gattServer().addService(interestService); 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 20 integers."); // color = writeParams->data; int index = 0; for (int i=0; i < 20; i++) { interests[i] = writeParams->data[i]; index++; printf("Index: %d, val %08X \r\n", index, interests[i]); } printf("[PERIPHERAL]\t Writing that to the FS..\r\n"); writeToFs(); } } int InterestService::readFromFs() { enum { MAGIC_VALUE = 0xC0FFEE20 }; 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[5]; flash_device.read(buffer, address, 5); 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]); for (int i=0; i < 20; i++) { interests[i] = int((unsigned char)(ptr->value[i] &0xff) << 24 | (unsigned char)(ptr->value[i+1]&0xff) << 16 | (unsigned char)(ptr->value[i+2]&0xff) << 8 | (unsigned char)(ptr->value[i+3]&0xff));//ptr->value[i] << 24 | (ptr->value[i+1] & 0xff) << 16 | (ptr->value[i+2] & 0xff) << 8| (ptr->value[i+3] & 0xff); } } else { printf("Uninitialized, setting up first time"); packed_A newmemory; newmemory.magic = MAGIC_VALUE; for (int i=0; i < 20; i++) { newmemory.value[i] = 2; } 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 = 0xC0FFEE20 }; mbed::FlashIAP flash_device; flash_device.init(); printf("[PERIPHERAL]\t Inited FS.. \r\n"); 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); printf("[PERIPHERAL]\t Ready?"); newmemory.magic = MAGIC_VALUE; for (int i=0; i < 5; i++) { printf("[PERIPHERAL]\t U wrote: %d", interests[i]); newmemory.value[i] = interests[i]; } printf("[PERIPHERAL]\t %d", sizeof(newmemory)); printf("[PERIPHERAL]\t %d", address); printf("[PERIPHERAL]\t %d", sizeof(interests)); flash_device.erase(address, flash_device.get_sector_size(address)); wait(0.2); // return 0; flash_device.program(&newmemory, address, sizeof(newmemory)); printf("Flashed"); return 0; } int InterestService::createFs(){ return true; } InterestService::~InterestService() { printf("[PERIPHERAL]\t Destructing InterestService\r\n"); }