static otError addSetting(otInstance * aInstance, uint16_t aKey, bool aIndex0, const uint8_t *aValue, uint16_t aValueLength) { otError error = OT_ERROR_NONE; OT_TOOL_PACKED_BEGIN struct addSettingsBlock { struct settingsBlock block; uint8_t data[kSettingsBlockDataSize]; } OT_TOOL_PACKED_END addBlock; uint32_t settingsSize = SETTINGS_CONFIG_PAGE_NUM > 1 ? SETTINGS_CONFIG_PAGE_SIZE * SETTINGS_CONFIG_PAGE_NUM / 2 : SETTINGS_CONFIG_PAGE_SIZE; addBlock.block.flag = 0xff; addBlock.block.key = aKey; if (aIndex0) { addBlock.block.flag &= (~kBlockIndex0Flag); } addBlock.block.flag &= (~kBlockAddBeginFlag); addBlock.block.length = aValueLength; if ((sSettingsUsedSize + getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)) >= settingsSize) { otEXPECT_ACTION(swapSettingsBlock(aInstance) >= (getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)), error = OT_ERROR_NO_BUFS); } utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize, reinterpret_cast<uint8_t *>(&addBlock.block), sizeof(struct settingsBlock)); memset(addBlock.data, 0xff, kSettingsBlockDataSize); memcpy(addBlock.data, aValue, addBlock.block.length); utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize + sizeof(struct settingsBlock), reinterpret_cast<uint8_t *>(addBlock.data), getAlignLength(addBlock.block.length)); addBlock.block.flag &= (~kBlockAddCompleteFlag); utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize, reinterpret_cast<uint8_t *>(&addBlock.block), sizeof(struct settingsBlock)); sSettingsUsedSize += (sizeof(struct settingsBlock) + getAlignLength(addBlock.block.length)); exit: return error; }
static otError addSetting(otInstance * aInstance, uint16_t aKey, bool aIndex0, const uint8_t *aValue, uint16_t aValueLength) { otError error = OT_ERROR_NONE; OT_TOOL_PACKED_BEGIN struct addSettingsBlock { struct settingsBlock block; uint8_t data[OT_SETTINGS_BLOCK_DATA_SIZE]; } OT_TOOL_PACKED_END addBlock; uint32_t settingsSize = SETTINGS_CONFIG_PAGE_SIZE * SETTINGS_CONFIG_PAGE_NUM / 2; addBlock.block.flag = 0xff; addBlock.block.key = aKey; if (aIndex0) { addBlock.block.flag &= (~OT_FLASH_BLOCK_INDEX_0_FLAG); } addBlock.block.flag &= (~OT_FLASH_BLOCK_ADD_BEGIN_FLAG); addBlock.block.length = aValueLength; if ((sSettingsUsedSize + getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)) >= settingsSize) { otEXPECT_ACTION(swapSettingsBlock(aInstance) >= (getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)), error = OT_ERROR_NO_BUFS); } utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize, (uint8_t *)(&addBlock.block), sizeof(struct settingsBlock)); memset(addBlock.data, 0xff, OT_SETTINGS_BLOCK_DATA_SIZE); memcpy(addBlock.data, aValue, addBlock.block.length); utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize + sizeof(struct settingsBlock), (uint8_t *)(addBlock.data), getAlignLength(addBlock.block.length)); addBlock.block.flag &= (~OT_FLASH_BLOCK_ADD_COMPLETE_FLAG); utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize, (uint8_t *)(&addBlock.block), sizeof(struct settingsBlock)); sSettingsUsedSize += (sizeof(struct settingsBlock) + getAlignLength(addBlock.block.length)); exit: return error; }
otError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex) { otError error = OT_ERROR_NOT_FOUND; uint32_t address = sSettingsBaseAddress + kSettingsFlagSize; int index = 0; (void)aInstance; while (address < (sSettingsBaseAddress + sSettingsUsedSize)) { struct settingsBlock block; utilsFlashRead(address, reinterpret_cast<uint8_t *>(&block), sizeof(block)); if (block.key == aKey) { if (!(block.flag & kBlockIndex0Flag)) { index = 0; } if (!(block.flag & kBlockAddCompleteFlag) && (block.flag & kBlockDeleteFlag)) { if (aIndex == index || aIndex == -1) { error = OT_ERROR_NONE; block.flag &= (~kBlockDeleteFlag); utilsFlashWrite(address, reinterpret_cast<uint8_t *>(&block), sizeof(block)); } if (index == 1 && aIndex == 0) { block.flag &= (~kBlockIndex0Flag); utilsFlashWrite(address, reinterpret_cast<uint8_t *>(&block), sizeof(block)); } index++; } } address += (getAlignLength(block.length) + sizeof(struct settingsBlock)); } return error; }
otError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex) { OT_UNUSED_VARIABLE(aInstance); otError error = OT_ERROR_NOT_FOUND; uint32_t address = sSettingsBaseAddress + OT_SETTINGS_FLAG_SIZE; int index = 0; while (address < (sSettingsBaseAddress + sSettingsUsedSize)) { struct settingsBlock block; utilsFlashRead(address, (uint8_t *)(&block), sizeof(block)); if (block.key == aKey) { if (!(block.flag & OT_FLASH_BLOCK_INDEX_0_FLAG)) { index = 0; } if (!(block.flag & OT_FLASH_BLOCK_ADD_COMPLETE_FLAG) && (block.flag & OT_FLASH_BLOCK_DELETE_FLAG)) { if (aIndex == index || aIndex == -1) { error = OT_ERROR_NONE; block.flag &= (~OT_FLASH_BLOCK_DELETE_FLAG); utilsFlashWrite(address, (uint8_t *)(&block), sizeof(block)); } if (index == 1 && aIndex == 0) { block.flag &= (~OT_FLASH_BLOCK_INDEX_0_FLAG); utilsFlashWrite(address, (uint8_t *)(&block), sizeof(block)); } index++; } } address += (getAlignLength(block.length) + sizeof(struct settingsBlock)); } return error; }
static uint32_t swapSettingsBlock(otInstance *aInstance) { uint32_t oldBase = sSettingsBaseAddress; uint32_t swapAddress = oldBase; uint32_t usedSize = sSettingsUsedSize; uint8_t pageNum = SETTINGS_CONFIG_PAGE_NUM; uint32_t settingsSize = pageNum > 1 ? SETTINGS_CONFIG_PAGE_SIZE * pageNum / 2 : SETTINGS_CONFIG_PAGE_SIZE; (void)aInstance; otEXPECT(pageNum > 1); sSettingsBaseAddress = (swapAddress == SETTINGS_CONFIG_BASE_ADDRESS) ? (swapAddress + settingsSize) : SETTINGS_CONFIG_BASE_ADDRESS; initSettings(sSettingsBaseAddress, static_cast<uint32_t>(kSettingsInSwap)); sSettingsUsedSize = kSettingsFlagSize; swapAddress += kSettingsFlagSize; while (swapAddress < (oldBase + usedSize)) { OT_TOOL_PACKED_BEGIN struct addSettingsBlock { struct settingsBlock block; uint8_t data[kSettingsBlockDataSize]; } OT_TOOL_PACKED_END addBlock; bool valid = true; utilsFlashRead(swapAddress, reinterpret_cast<uint8_t *>(&addBlock.block), sizeof(struct settingsBlock)); swapAddress += sizeof(struct settingsBlock); if (!(addBlock.block.flag & kBlockAddCompleteFlag) && (addBlock.block.flag & kBlockDeleteFlag)) { uint32_t address = swapAddress + getAlignLength(addBlock.block.length); while (address < (oldBase + usedSize)) { struct settingsBlock block; utilsFlashRead(address, reinterpret_cast<uint8_t *>(&block), sizeof(block)); if (!(block.flag & kBlockAddCompleteFlag) && (block.flag & kBlockDeleteFlag) && !(block.flag & kBlockIndex0Flag) && (block.key == addBlock.block.key)) { valid = false; break; } address += (getAlignLength(block.length) + sizeof(struct settingsBlock)); } if (valid) { utilsFlashRead(swapAddress, addBlock.data, getAlignLength(addBlock.block.length)); utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize, reinterpret_cast<uint8_t *>(&addBlock), getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)); sSettingsUsedSize += (sizeof(struct settingsBlock) + getAlignLength(addBlock.block.length)); } } else if (addBlock.block.flag == 0xff) { break; } swapAddress += getAlignLength(addBlock.block.length); } setSettingsFlag(sSettingsBaseAddress, static_cast<uint32_t>(kSettingsInUse)); setSettingsFlag(oldBase, static_cast<uint32_t>(kSettingsNotUse)); exit: return settingsSize - sSettingsUsedSize; }
static void setSettingsFlag(uint32_t aBase, uint32_t aFlag) { utilsFlashWrite(aBase, reinterpret_cast<uint8_t *>(&aFlag), sizeof(aFlag)); }
static uint32_t swapSettingsBlock(otInstance *aInstance) { OT_UNUSED_VARIABLE(aInstance); uint32_t oldBase = sSettingsBaseAddress; uint32_t swapAddress = oldBase; uint32_t usedSize = sSettingsUsedSize; uint32_t settingsSize = SETTINGS_CONFIG_PAGE_SIZE * SETTINGS_CONFIG_PAGE_NUM / 2; sSettingsBaseAddress = (swapAddress == SETTINGS_CONFIG_BASE_ADDRESS) ? (swapAddress + settingsSize) : SETTINGS_CONFIG_BASE_ADDRESS; initSettings(sSettingsBaseAddress, (uint32_t)OT_SETTINGS_IN_SWAP); sSettingsUsedSize = OT_SETTINGS_FLAG_SIZE; swapAddress += OT_SETTINGS_FLAG_SIZE; while (swapAddress < (oldBase + usedSize)) { OT_TOOL_PACKED_BEGIN struct addSettingsBlock { struct settingsBlock block; uint8_t data[OT_SETTINGS_BLOCK_DATA_SIZE]; } OT_TOOL_PACKED_END addBlock; bool valid = true; utilsFlashRead(swapAddress, (uint8_t *)(&addBlock.block), sizeof(struct settingsBlock)); swapAddress += sizeof(struct settingsBlock); if (!(addBlock.block.flag & OT_FLASH_BLOCK_ADD_COMPLETE_FLAG) && (addBlock.block.flag & OT_FLASH_BLOCK_DELETE_FLAG)) { uint32_t address = swapAddress + getAlignLength(addBlock.block.length); while (address < (oldBase + usedSize)) { struct settingsBlock block; utilsFlashRead(address, (uint8_t *)(&block), sizeof(block)); if (!(block.flag & OT_FLASH_BLOCK_ADD_COMPLETE_FLAG) && (block.flag & OT_FLASH_BLOCK_DELETE_FLAG) && !(block.flag & OT_FLASH_BLOCK_INDEX_0_FLAG) && (block.key == addBlock.block.key)) { valid = false; break; } address += (getAlignLength(block.length) + sizeof(struct settingsBlock)); } if (valid) { utilsFlashRead(swapAddress, addBlock.data, getAlignLength(addBlock.block.length)); utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize, (uint8_t *)(&addBlock), getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)); sSettingsUsedSize += (sizeof(struct settingsBlock) + getAlignLength(addBlock.block.length)); } } else if (addBlock.block.flag == 0xff) { break; } swapAddress += getAlignLength(addBlock.block.length); } setSettingsFlag(sSettingsBaseAddress, (uint32_t)OT_SETTINGS_IN_USE); setSettingsFlag(oldBase, (uint32_t)OT_SETTINGS_NOT_USED); return settingsSize - sSettingsUsedSize; }
static void setSettingsFlag(uint32_t aBase, uint32_t aFlag) { utilsFlashWrite(aBase, (uint8_t *)&aFlag, sizeof(aFlag)); }