Пример #1
0
static void initSettings(uint32_t aBase, uint32_t aFlag)
{
    uint32_t address      = aBase;
    uint32_t settingsSize = SETTINGS_CONFIG_PAGE_SIZE * SETTINGS_CONFIG_PAGE_NUM / 2;

    while (address < (aBase + settingsSize))
    {
        utilsFlashErasePage(address);
        utilsFlashStatusWait(1000);
        address += SETTINGS_CONFIG_PAGE_SIZE;
    }

    setSettingsFlag(aBase, aFlag);
}
Пример #2
0
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;
}
Пример #3
0
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;
}