コード例 #1
0
ファイル: bd_data_manager.c プロジェクト: 1072258106/duband
void save_alarm(void )
{
    uint32_t    alarm_mem[1 + sizeof(alarm_union_t)/sizeof(uint32_t) * MAX_ALARM_NUM];
    uint32_t    err_code;
    uint8_t     read_word_len = 1 + sizeof(alarm_union_t)/sizeof(uint32_t) * MAX_ALARM_NUM;
    uint8_t     alarm_count_stored;

    int         ret;

    err_code = bd_flash_read_page(FLASH_PAGE_ALARM_SETTINGS,alarm_mem,&read_word_len);
    if(err_code == NRF_SUCCESS) {
        alarm_count_stored = (uint8_t)alarm_mem[0];
        if(alarm_count_stored == alarm_num) {
            ret = memcmp((char *)alarms,(char *)(alarm_mem + 1),sizeof(alarm_union_t) * alarm_count_stored);
            if(ret == 0) { //do not need sync
                return ;
            }
        }
    }

    uint32_t *alarms_addr = NULL;
    ble_flash_page_addr((uint8_t) FLASH_PAGE_ALARM_SETTINGS,&alarms_addr);

    if(check_is_flash_erased(alarms_addr,(FLASH_PAGE_HEADER_LEN + 1 + sizeof(alarm_union_t)/sizeof(uint32_t) * MAX_ALARM_NUM)) != NRF_SUCCESS) { //page not erased
        ble_flash_page_delay_erase((uint8_t) FLASH_PAGE_ALARM_SETTINGS);
    } else {
        save_alarms_into_flash();
    }

}
コード例 #2
0
ファイル: bd_private_bond.c プロジェクト: 1072258106/duband
/*********************************************************************
* This function is used to check if the user id has been bonded
**********************************************************************/
uint32_t check_user_id_bonded(const uint8_t* user_id, uint8_t length)
{
    uint32_t err_code = NRF_SUCCESS;

    uint16_t  crc_header;
    uint16_t  m_crc_bond_info;
    uint32_t  *mp_flash_bond_info;
    uint32_t  header;


    if((!user_id) || (length != USER_ID_LENGTH)) {
        err_code = NRF_ERROR_INVALID_PARAM;
        return err_code;
    }

    // Initialize CRC
    m_crc_bond_info = ble_flash_crc16_compute(NULL, 0, NULL);

    // Find pointer to start of bond information flash block
    err_code = ble_flash_page_addr(FLASH_PAGE_USER_ID, &mp_flash_bond_info);
    if (err_code != NRF_SUCCESS) {
        return err_code;
    }

    //get crc
    header = *mp_flash_bond_info;

    if ((header & 0xFFFF0000U) == BLE_FLASH_MAGIC_NUMBER) {
        crc_header = (uint16_t)(header & 0x0000FFFFU);
    } else if (header == 0xFFFFFFFFU) {
        return NRF_ERROR_NOT_FOUND;
    } else {
        return NRF_ERROR_INVALID_DATA;
    }

    /* check crc in the flash */
    // Check CRC
    m_crc_bond_info = ble_flash_crc16_compute((uint8_t *)(mp_flash_bond_info + 1),
                      USER_ID_LENGTH,
                      &m_crc_bond_info);
    if (m_crc_bond_info != crc_header) {
        return NRF_ERROR_INVALID_DATA;
    }

    if (memcmp(user_id,(uint8_t *)(mp_flash_bond_info + 1),length) == 0) {
        err_code = NRF_SUCCESS;
    } else {
        err_code = NRF_ERROR_INVALID_DATA;
    }

    return err_code;
}
コード例 #3
0
ファイル: ble_bondmngr.c プロジェクト: amurlynx/rf-nordic
/**@brief      This function stores in flash the System Attributes related to a specified master.
 *
 * @param[in]  p_sys_attr   System Attributes to be stored.
 *
 * @return     NRF_SUCCESS on success, an error_code otherwise.
 */
static uint32_t sys_attr_store(master_sys_attr_t * p_sys_attr)
{
    uint32_t err_code;

    // Check if flash is full.
    if (m_sys_attr_in_flash_count >= MAX_BONDS_IN_FLASH)
    {
        return NRF_ERROR_NO_MEM;
    }

    // Check if this is the first time any System Attributes is stored.
    if (m_sys_attr_in_flash_count == 0)
    {
        // Initialize CRC
        m_crc_sys_attr = ble_flash_crc16_compute(NULL, 0, NULL);
    
        // Find pointer to start of System Attributes flash block.
        err_code = ble_flash_page_addr(m_bondmngr_config.flash_page_num_sys_attr, 
                                       &mp_flash_sys_attr);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    }
    
    // Write System Attributes in flash.
    err_code = ble_flash_block_write(mp_flash_sys_attr + 1,
                                    (uint32_t *)p_sys_attr,
                                    sizeof(master_sys_attr_t) / sizeof(uint32_t));
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
    m_crc_sys_attr = ble_flash_crc16_compute((uint8_t *)p_sys_attr, 
                                             sizeof(master_sys_attr_t), 
                                             &m_crc_sys_attr);
    
    // Write header
    err_code = ble_flash_word_write(mp_flash_sys_attr, BLE_FLASH_MAGIC_NUMBER | m_crc_sys_attr);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
    
    // Update flash pointer
    mp_flash_sys_attr += ((sizeof(master_sys_attr_t) / sizeof(uint32_t)) + 1);
    
    m_sys_attr_in_flash_count++;
    return NRF_SUCCESS;
}
コード例 #4
0
ファイル: ble_bondmngr.c プロジェクト: amurlynx/rf-nordic
/**@brief      This function stores the Bonding Information of the specified master to the flash.
 *
 * @param[in]  p_bond   Bonding information to be stored.
 *
 * @return     NRF_SUCCESS on success, an error_code otherwise.
 */
static uint32_t bond_info_store(master_bond_t * p_bond)
{
    uint32_t err_code;

    // Check if flash is full
    if (m_bond_info_in_flash_count >= MAX_BONDS_IN_FLASH)
    {
        return NRF_ERROR_NO_MEM;
    }

    // Check if this is the first bond to be stored
    if (m_bond_info_in_flash_count == 0)
    {
        // Initialize CRC
        m_crc_bond_info = ble_flash_crc16_compute(NULL, 0, NULL);
    
        // Find pointer to start of bond information flash block
        err_code = ble_flash_page_addr(m_bondmngr_config.flash_page_num_bond, &mp_flash_bond_info);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    }
    
    // Write Bonding Information
    err_code = ble_flash_block_write(mp_flash_bond_info + 1,
                                     (uint32_t *)p_bond,
                                     sizeof(master_bond_t) / sizeof(uint32_t));
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
    m_crc_bond_info = ble_flash_crc16_compute((uint8_t *)p_bond, 
                                              sizeof(master_bond_t), 
                                              &m_crc_bond_info);
    
    // Write header
    err_code = ble_flash_word_write(mp_flash_bond_info, BLE_FLASH_MAGIC_NUMBER | m_crc_bond_info);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
    
    // Update flash pointer
    mp_flash_bond_info += (sizeof(master_bond_t) / sizeof(uint32_t)) + 1;
    
    m_bond_info_in_flash_count++;
    return NRF_SUCCESS;
}
コード例 #5
0
ファイル: ble_bondmngr.c プロジェクト: amurlynx/rf-nordic
/**@brief      This function loads the System Attributes related to one master from flash.
 *
 * @param[out] p_sys_attr   Loaded System Attributes.
 *
 * @return     NRF_SUCCESS on success, otherwise an error code.
 */
static uint32_t sys_attr_load_from_flash(master_sys_attr_t * p_sys_attr)
{
    uint32_t err_code;
    uint16_t crc_header;

    // Check if this is the first time System Attributes is loaded from flash, in which case the
    // m_sys_attr_in_flash_count variable would have the initial value 0.
    if (m_sys_attr_in_flash_count == 0)
    {
        // Initialize CRC
        m_crc_sys_attr = ble_flash_crc16_compute(NULL, 0, NULL);
    
        // Find pointer to start of System Attributes flash block
        err_code = ble_flash_page_addr(m_bondmngr_config.flash_page_num_sys_attr, 
                                       &mp_flash_sys_attr);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    }
    
    // Extract CRC from header
    err_code = crc_extract(*mp_flash_sys_attr, &crc_header);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
    
    // Read System Attributes from flash
    *p_sys_attr = *(master_sys_attr_t *)(mp_flash_sys_attr + 1);
    
    // Check CRC
    m_crc_sys_attr = ble_flash_crc16_compute((uint8_t *)p_sys_attr, 
                                             sizeof(master_sys_attr_t), 
                                             &m_crc_sys_attr);
    if (m_crc_sys_attr == crc_header)
    {
        m_sys_attr_in_flash_count++;
        mp_flash_sys_attr += (sizeof(master_sys_attr_t) / sizeof(uint32_t)) + 1;
        
        return NRF_SUCCESS;
    }
    else
    {
        return NRF_ERROR_INVALID_DATA;
    }
}
コード例 #6
0
ファイル: ble_bondmngr.c プロジェクト: amurlynx/rf-nordic
/**@brief      This function loads the Bonding Information of one master from flash.
 *
 * @param[out] p_bond   Loaded Bonding Information.
 *
 * @return     NRF_SUCCESS on success, otherwise an error code.
 */
static uint32_t bonding_info_load_from_flash(master_bond_t * p_bond)
{
    uint32_t err_code;
    uint16_t crc_header;

    // Check if this is the first bond to be loaded, in which case the 
    // m_bond_info_in_flash_count variable would have the intial value 0.
    if (m_bond_info_in_flash_count == 0)
    {
        // Initialize CRC
        m_crc_bond_info = ble_flash_crc16_compute(NULL, 0, NULL);
    
        // Find pointer to start of bond information flash block
        err_code = ble_flash_page_addr(m_bondmngr_config.flash_page_num_bond, &mp_flash_bond_info);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    }
    
    // Extract CRC from header
    err_code = crc_extract(*mp_flash_bond_info, &crc_header);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
    
    // Load master
    *p_bond = *(master_bond_t *)(mp_flash_bond_info + 1);
    
    // Check CRC
    m_crc_bond_info = ble_flash_crc16_compute((uint8_t *)p_bond, 
                                              sizeof(master_bond_t), 
                                              &m_crc_bond_info);
    if (m_crc_bond_info == crc_header)
    {
        m_bond_info_in_flash_count++;
        mp_flash_bond_info += (sizeof(master_bond_t) / sizeof(uint32_t)) + 1;
        
        return NRF_SUCCESS;
    }
    else
    {
        return NRF_ERROR_INVALID_DATA;
    }
}
コード例 #7
0
ファイル: bd_data_manager.c プロジェクト: 1072258106/duband
void save_daily_target( void)
{
    uint32_t *dailyTarget_addr;

    if(daily_target_changed) {
        ble_flash_page_addr(FLASH_PAGE_DAILY_TARGET, &dailyTarget_addr);
#ifdef DEBUG_LOG

        LOG(LEVEL_INFO,"oldTarget:%p:%x\r\n",dailyTarget_addr, *dailyTarget_addr);
#endif

        ble_flash_page_delay_erase((uint8_t) FLASH_PAGE_DAILY_TARGET);

        daily_target_changed = false;
    }

}
コード例 #8
0
ファイル: bd_private_bond.c プロジェクト: 1072258106/duband
uint32_t bond_read_user_id(void)
{
    uint32_t  err_code;
    uint16_t  crc_header;
    uint16_t  m_crc_bond_info;
    uint32_t  *mp_flash_bond_info;
    uint32_t  header;


    // Initialize CRC
    m_crc_bond_info = ble_flash_crc16_compute(NULL, 0, NULL);

    // Find pointer to start of bond information flash block
    err_code = ble_flash_page_addr(FLASH_PAGE_USER_ID, &mp_flash_bond_info);
    if (err_code != NRF_SUCCESS) {
        return err_code;
    }

    //get crc
    header = *mp_flash_bond_info;

    if ((header & 0xFFFF0000U) == BLE_FLASH_MAGIC_NUMBER) {
        crc_header = (uint16_t)(header & 0x0000FFFFU);
    } else if (header == 0xFFFFFFFFU) {
        return NRF_ERROR_NOT_FOUND;
    } else {
        return NRF_ERROR_INVALID_DATA;
    }

    // Load master
    memcpy(user_id,mp_flash_bond_info + 1, USER_ID_LENGTH);

    // Check CRC
    m_crc_bond_info = ble_flash_crc16_compute((uint8_t *)user_id,
                      USER_ID_LENGTH,
                      &m_crc_bond_info);
    if (m_crc_bond_info == crc_header) {
        return NRF_SUCCESS;
    } else {
        return NRF_ERROR_INVALID_DATA;
    }
}
コード例 #9
0
ファイル: bd_private_bond.c プロジェクト: 1072258106/duband
uint32_t bond_store_user_id(void)
{
    uint32_t  err_code;
    uint16_t  m_crc_bond_info;
    uint32_t  *mp_flash_bond_info;

    /* inialize crc check */
    m_crc_bond_info = ble_flash_crc16_compute(NULL, 0, NULL);

    // Find pointer to start of bond information flash block
    err_code = ble_flash_page_addr(FLASH_PAGE_USER_ID, &mp_flash_bond_info);
    if (err_code != NRF_SUCCESS) {
        return err_code;
    }

    /*FIXME: erase a page while bluetooth connected may cause disconnect, so this maybe stored when bluetooth disconnected*/
    /* Erase the whole page */
    ble_flash_page_erase(FLASH_PAGE_USER_ID);

    // Write private bonding Information
    err_code = ble_flash_block_write(mp_flash_bond_info + 1, //the first word is used to store crc
                                     (uint32_t *)user_id,
                                     ((USER_ID_LENGTH - 1) / (4) + 1));
    if (err_code != NRF_SUCCESS) {
        return err_code;
    }

    m_crc_bond_info = ble_flash_crc16_compute((uint8_t *)user_id,
                      USER_ID_LENGTH,
                      &m_crc_bond_info);

    // Write header
    err_code = ble_flash_word_write(mp_flash_bond_info, BLE_FLASH_MAGIC_NUMBER | m_crc_bond_info);
    if (err_code != NRF_SUCCESS) {
        return err_code;
    }

    return NRF_SUCCESS;
}
コード例 #10
0
ファイル: bd_data_manager.c プロジェクト: 1072258106/duband
void save_user_profile(void )
{
    uint32_t    origin_profile;
    uint8_t     read_word_len = 1;
    uint32_t    err_code;

    err_code = bd_flash_read_page(FLASH_PAGE_USER_PROFILE,&origin_profile,&read_word_len);
    if((err_code == NRF_SUCCESS) && (read_word_len == 1)) {
        if(user_profile.data == origin_profile) {
            return;
        }
    }

    uint32_t *userProfile_addr;
    ble_flash_page_addr((uint8_t) FLASH_PAGE_USER_PROFILE,&userProfile_addr);

    if(check_is_flash_erased(userProfile_addr,1 + FLASH_PAGE_HEADER_LEN) != NRF_SUCCESS) { //the page is not erased
        ble_flash_page_delay_erase(FLASH_PAGE_USER_PROFILE);
    } else {
        save_profile_into_flash();
    }

}
コード例 #11
0
ファイル: ble_bondmngr.c プロジェクト: amurlynx/rf-nordic
uint32_t ble_bondmngr_bonded_master_delete(uint16_t master_id)
{
    if (!m_is_bondmngr_initialized)
    {
        return NRF_ERROR_INVALID_STATE;
    }

    int8_t  master_handle_to_be_deleted = INVALID_MASTER_HANDLE;
    uint8_t i;

    // Search for the handle of the master.
    for (i = 0; i < m_masters_in_db_count; i++)
    {
        if (m_masters_db[i].bond.master_id_info.div == master_id)
        {
            master_handle_to_be_deleted = i;
            break;
        }
    }

    if (master_handle_to_be_deleted == INVALID_MASTER_HANDLE)
    {
        // Master ID not found.
        return NRF_ERROR_NOT_FOUND;
    }

    // Delete the master in RAM.
    for (i = master_handle_to_be_deleted; i < (m_masters_in_db_count - 1); i++)
    {
        // Overwrite the current master entry with the next one.
        m_masters_db[i] = m_masters_db[i + 1];

        // Decrement the value of handle.
        m_masters_db[i].bond.master_handle--;
        m_masters_db[i].sys_attr.master_handle--;
    }

    // Clear the last database entry.
    memset(&(m_masters_db[m_masters_in_db_count - 1]), 0, sizeof(master_t));

    m_masters_in_db_count--;

    uint32_t err_code;

    // Reinitialize the pointers to the memory where bonding info and System Attributes are stored
    // in flash.
    err_code = ble_flash_page_addr(m_bondmngr_config.flash_page_num_bond, &mp_flash_bond_info);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    err_code = ble_flash_page_addr(m_bondmngr_config.flash_page_num_sys_attr, &mp_flash_sys_attr);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    // Refresh the data in the flash memory (both Bonding Information and System Attributes).
    // Erase and rewrite bonding info and System Attributes.

    err_code = flash_pages_erase();
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    m_bond_info_in_flash_count    = 0;
    m_sys_attr_in_flash_count     = 0;

    for (i = 0; i < m_masters_in_db_count; i++)
    {
        err_code = bond_info_store(&(m_masters_db[i].bond));
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    }

    for (i = 0; i < m_masters_in_db_count; i++)
    {
        err_code = sys_attr_store(&(m_masters_db[i].sys_attr));
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    }

    update_whitelist();

    return NRF_SUCCESS;
}