Skip to content

Commit

Permalink
initial flash with app_state
Browse files Browse the repository at this point in the history
  • Loading branch information
atar13 committed Mar 5, 2024
1 parent c8d7268 commit 8b2dc67
Showing 1 changed file with 42 additions and 61 deletions.
103 changes: 42 additions & 61 deletions ot-tock/platform/platform-tock/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,102 +3,83 @@
#include <internal/nonvolatile_storage.h>
#include <openthread/platform/flash.h>
#include <stdio.h>
#include <string.h>

// Force flash operations to be synchronous
static bool done = false;
#include <app_state.h>
#include <tock.h>

// Why did @Tyler not include this?
// uint32_t otPlatFlashGetSwapSize(otInstance *aInstance);
#define OT_TOCK_APP_STATE_SIZE 2048

static void read_done(int length, __attribute__((unused)) int arg1,
__attribute__((unused)) int arg2, __attribute__((unused)) void *ud) {
// printf("\tFinished read! %i\n", length);
done = true;
}
struct ot_tock_app_state_t {
// create a continguous chunk of flash that openthread can freely write to
uint8_t data[OT_TOCK_APP_STATE_SIZE];
uint8_t swapData[OT_TOCK_APP_STATE_SIZE];
};

static void write_done(int length, __attribute__((unused)) int arg1,
__attribute__((unused)) int arg2, __attribute__((unused)) void *ud) {
// printf("\tFinished write! %i\n", length);
done = true;
}
APP_STATE_DECLARE(struct ot_tock_app_state_t, ot_tock_app_state);

// Why did @Tyler not include this?
// uint32_t otPlatFlashGetSwapSize(otInstance *aInstance);

void otPlatFlashInit(otInstance *aInstance) {
OT_UNUSED_VARIABLE(aInstance);
// printf("%s:%d in %s\n", __FILE__, __LINE__, __func__);

// load app state from flash into memory
int ret;

ret = app_state_load_sync();
if (ret < 0) {
printf("Error loading application state: %s\n", tock_strrcode(ret));
}
}

void otPlatFlashErase(otInstance *aInstance, uint8_t aSwapIndex) {
OT_UNUSED_VARIABLE(aInstance);
// printf("%s:%d in %s\n", __FILE__, __LINE__, __func__);

int num_bytes;
nonvolatile_storage_internal_get_number_bytes(&num_bytes);
printf("Have %i bytes of nonvolatile storage\n", num_bytes);

// Write all zeroes to flash
uint8_t *zeroes = calloc(num_bytes, sizeof(uint8_t));

otPlatFlashWrite(aInstance, aSwapIndex, 0, zeroes, num_bytes);

free(zeroes);
// zero out both data and swapData
memset(ot_tock_app_state.data, 0, OT_TOCK_APP_STATE_SIZE);
memset(ot_tock_app_state.swapData, 0, OT_TOCK_APP_STATE_SIZE);
}

void otPlatFlashWrite(otInstance *aInstance, uint8_t aSwapIndex, uint32_t aOffset,
const void *aData, uint32_t aSize) {
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aSwapIndex);
// printf("%s:%d in %s\n", __FILE__, __LINE__, __func__);
int ret;

// Sets up the buffer to store the output of the read
ret = nonvolatile_storage_internal_write_buffer((void *)aData, aSize);
if (ret != RETURNCODE_SUCCESS) {
printf("\tERROR setting write buffer\n");
printf("OT_TOCK_FLASH: Thread wants to write %d bytes to offset %x. Swap? %d\n", aSize, aOffset, aSwapIndex);

if ((aOffset + aSize) > OT_TOCK_APP_STATE_SIZE) {
printf("OT_TOCK_FLASH: ERROR!!! Thread is trying to write to a region outside allocated flash area");
return;
}

ret = nonvolatile_storage_internal_write_done_subscribe(write_done, NULL);
if (ret != RETURNCODE_SUCCESS) {
printf("\tERROR setting write done callback\n");
return;
if (aSwapIndex) {
memcpy(ot_tock_app_state.swapData + aOffset, aData, aSize);
} else {
memcpy(ot_tock_app_state.data + aOffset, aData, aSize);
}

done = false;
ret = nonvolatile_storage_internal_write(aOffset, aSize);
// TODO: no idea if we should call this here
int ret = app_state_save_sync();
if (ret != 0) {
printf("\tERROR calling write\n");
printf("OT_TOCK_FLASH: ERROR!!! saving application state: %s\n", tock_strrcode(ret));
return;
}
// wait until callback fires
yield_for(&done);
}

void otPlatFlashRead(otInstance *aInstance, uint8_t aSwapIndex, uint32_t aOffset, void *aData,
uint32_t aSize) {
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aSwapIndex);
// printf("%s:%d in %s\n", __FILE__, __LINE__, __func__);
int ret;

// Sets up the buffer to store the output of the read
ret = nonvolatile_storage_internal_read_buffer(aData, aSize);
if (ret != RETURNCODE_SUCCESS) {
printf("\tERROR setting read buffer\n");
return;
}
printf("OT_TOCK_FLASH: Thread wants to read %d bytes from offset %x. Swap? %d\n", aSize, aOffset, aSwapIndex);

ret = nonvolatile_storage_internal_read_done_subscribe(read_done, NULL);
if (ret != RETURNCODE_SUCCESS) {
printf("\tERROR setting read done callback\n");
if ((aOffset + aSize) > OT_TOCK_APP_STATE_SIZE) {
printf("OT_TOCK_FLASH: ERROR!!! Thread is trying to read from a region outside allocated flash area");
return;
}

done = false;
ret = nonvolatile_storage_internal_read(aOffset, aSize);
if (ret != 0) {
printf("\tERROR calling read\n");
return;
if (aSwapIndex) {
memcpy(aData, ot_tock_app_state.swapData + aOffset, aSize);
} else {
memcpy(aData, ot_tock_app_state.data + aOffset, aSize);
}
// wait until callback fires
yield_for(&done);
}

0 comments on commit 8b2dc67

Please sign in to comment.