/* Function for calculating storage offset for receiving SoftDevice image. 
When a new SoftDevice is received it will be temporary stored in flash before moved to address 0x0. In order to succesfully validate transfer and relocation it is important that temporary image and final installed image does not ovwerlap hence an offset must be calculated in case new image is larger than currently installed SoftDevice.  */
uint32_t offset_calculate(uint32_t sd_image_size)
/* Function for calculating storage offset for receiving SoftDevice image. 
When a new SoftDevice is received it will be temporary stored in flash before moved to address 0x0. In order to succesfully validate transfer and relocation it is important that temporary image and final installed image does not ovwerlap hence an offset must be calculated in case new image is larger than currently installed SoftDevice.  */
uint32_t offset_calculate(uint32_t sd_image_size)
{
    uint32_t offset = 0;
    if (m_start_packet.sd_image_size > DFU_BANK_0_REGION_START)
    {
        uint32_t page_mask = (CODE_PAGE_SIZE - 1);
        uint32_t diff = m_start_packet.sd_image_size - DFU_BANK_0_REGION_START;
        offset = diff & ~page_mask;
        if ((diff & page_mask) > 0)
        {
            offset += CODE_PAGE_SIZE;
        }
    }
    return offset;
}