/* Platform-specific implementation for persistence on the nRF5x. Based on the
 * pstorage module provided by the Nordic SDK. */
bool loadURIBeaconConfigParams(EddystoneService::EddystoneParams_t *paramsP)
{
    static bool pstorageInitied = false;
    if (!pstorageInitied) {
        pstorage_init();

        static pstorage_module_param_t pstorageParams = {
            .cb          = pstorageNotificationCallback,
            .block_size  = sizeof(PersistentParams_t),
            .block_count = 1
        };
        pstorage_register(&pstorageParams, &pstorageHandle);
        pstorageInitied = true;
    }

    PersistentParams_t persistentParams;
    if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) ||
        (persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) {
        // On failure zero out and let the service reset to defaults
        memset(paramsP, 0, sizeof(EddystoneService::EddystoneParams_t));
        return false;
    }

    memcpy(paramsP, &persistentParams.params, sizeof(EddystoneService::EddystoneParams_t));
    persistenceSignature = persistentParams.persistenceSignature;
    return true;
}
Пример #2
0
uint32_t ble_ans_c_service_load(const ble_ans_c_t * p_ans)
{
    uint32_t err_code;
    uint32_t i;

    err_code = pstorage_load((uint8_t *)m_service_db,
                             &m_flash_handle,
                             (DISCOVERED_SERVICE_DB_SIZE * sizeof(uint32_t)),
                             0);

    if (err_code != NRF_SUCCESS)
    {
        // Problem with loading values from flash, initialize the RAM DB with default.
        for (i = 0; i < BLE_ANS_MAX_DISCOVERED_CENTRALS; ++i)
        {
            mp_service_db[i].handle = INVALID_SERVICE_HANDLE;
        }

        if (err_code == NRF_ERROR_NOT_FOUND)
        {
            // The flash does not contain any memorized centrals, set the return code to success.
            err_code = NRF_SUCCESS;
        }
    }
    return err_code;
}
/***
 * The Ladybug stores info that is maintained across restarts of the device.  This info includes the device name, plant info (type of plant and growth stage), as well as
 * calibration values.  This function figures out what block handle has been initialized based on what info has been requested to read, reads the info from flash, then writes
 * the info to the p_bytes_to_read memory buffer.
 * @param data_to_read  		let the function know what type of data to read
 * @param p_bytes_to_read	give the function a buffer to write the data after reading from flash
 * @sa	LADYBUG_ERROR_UNSURE_WHAT_DATA_TO_READ
 */
void ladybug_flash_read(flash_rw_t data_to_read,uint8_t *p_bytes_to_read,void(*did_flash_action)(uint32_t err_code)){
  SEGGER_RTT_WriteString(0,"==> IN ladybug_flash_read\n");
  pstorage_handle_t * p_handle;
  m_flash_return = did_flash_action;
  //set the block handle to the block of flash set aside in flash_init for the data type to read
  //point the bytes to the location holding the bytes in memory.
  switch (data_to_read) {
    case plantInfo:
      p_handle = &m_block_plantInfo_store_handle;
      break;
    case calibrationValues:
      p_handle = &m_block_calibration_store_handle;
      break;
    case deviceName:
      p_handle = &m_block_device_name_store_handle;
      break;
    default:
      //this is an error case.  The function doesn't know what to read.
      APP_ERROR_CHECK(LADYBUG_ERROR_FLASH_UNSURE_WHAT_DATA_TO_READ);
      break;
  }
  // There is a chance the Flash activity doesn't happen so we use a timer to prevent waiting forever.
  m_mypstorage_wait_flag = 1;
  start_timer();
  uint32_t err_code = pstorage_load(p_bytes_to_read, p_handle, BLOCK_SIZE, 0);
  while(m_mypstorage_wait_flag) { }
  APP_ERROR_CHECK(err_code);
  // Flash activity completed so stop timer.
  stop_timer();
}
Пример #4
0
static uint8_t crypto_loadKeys(void)
{
  uint32_t err_code;

  pstorage_handle_t handle;
  err_code = pstorage_block_identifier_get(&crypto_store_handle, 0, &handle);
  APP_ERROR_CHECK(err_code);

  crypto_persistent_keys_t keys = {};
  err_code = pstorage_load((uint8_t*)&keys, &handle, sizeof(keys), 0);
  APP_ERROR_CHECK(err_code);

  if (keys.valid0 == 0x55 && keys.valid1 == 0xAA && keys.instance == CRYPTO_INSTANCE)
  {
    // Valid
    memcpy(srp.b, keys.srp_b, 32);
    memcpy(srp.salt, keys.srp_salt, 16);
    memcpy(srp.v, keys.srp_v, 384);
    memcpy(srp.B, keys.srp_B, 384);
    memcpy(crypto_keys.sign.secret, keys.sign_secret, 64);
    memcpy(crypto_keys.client.name, keys.clientname, 36);
    memcpy(crypto_keys.client.ltpk, keys.ltpk, 32);
    return 1;
  }
  else
  {
    return 0;
  }
}
Пример #5
0
//TODO: read and write can only process data that is a multiple of 4 bytes
//This involves problems when the data buffer has a different size
bool Storage::BufferedRead(u8* data, u32 block, u32 len)
{
	//logt("STORAGE", "Reading len:%u from block:%u", len, block);

	pstorage_load(data, &block_handles[block], len, 0);

	return true;
}
uint32_t pstorage_driver_load(uint8_t * dest_data) 
{
    uint32_t err_code;
    uint16_t tmp_size;
    pstorage_driver_block_t * block;
    
	  // Get pstorage_driver block, based on address of data.
    block = pstorage_driver_get_block(dest_data);
    if(block == NULL) 
    {
        return PS_LOAD_STATUS_NOT_FOUND;                                       // Block with corresponding data not registered.
    }
    
    // Load persistently stored data.
    err_code = pstorage_load(dest_data, &block->block_id, block->size, 0);
    if(err_code != NRF_SUCCESS) 
    {
        return PS_LOAD_STATUS_FAIL;
    }
    
		// Need to check is the last 4 bytes of corresponding block equal to PSTORAGE_DRIVER_MAGIC_NUM.
		
		// If block->size is divisible by 4, PSTORAGE_DRIVER_MAGIC_NUM is located on offset block->size.
    tmp_size = block->size;
		
    if((tmp_size % 4) != 0) 
    {
			  // In case that block->size is not divisible by 4, offset for PSTORAGE_DRIVER_MAGIC_NUM is equal with first larger  number that is divisible by 4.
        tmp_size += 4 - (tmp_size % 4);
    }

		// Load persistently stored data.
    err_code = pstorage_load((uint8_t *)&tmp_magic_number, &block->block_id, 4, tmp_size);
    if(err_code != NRF_SUCCESS) 
    {
        return PS_LOAD_STATUS_FAIL;
    }
    else if(tmp_magic_number != PSTORAGE_DRIVER_MAGIC_NUM) 
    {
        return PS_LOAD_STATUS_EMPTY;                                           // Can be considered that the corresponding block is empty.
    }
    
    return PS_LOAD_STATUS_SUCCESS;
}
void bootloader_settings_get(bootloader_settings_t * const p_settings)
{
    bootloader_settings_t bootloader_settings;

    (void) pstorage_load((uint8_t *) &bootloader_settings, &m_bootsettings_handle, sizeof(bootloader_settings_t), 0);

    p_settings->bank_0      = bootloader_settings.bank_0;
    p_settings->bank_0_crc  = bootloader_settings.bank_0_crc;
    p_settings->bank_0_size = bootloader_settings.bank_0_size;
    p_settings->bank_1      = bootloader_settings.bank_1;
}
Пример #8
0
H_U32 _StorageRead(pstorage_handle_t *flash_handle,H_U32 size,H_U32 offset,H_U8 *date,H_U16 user_block_size)
{
	H_U32 err_code = 0; 
	H_U32 correct_offset = 0;
	pstorage_handle_t redirect_handle;

	wy_memset(&redirect_handle, 0, sizeof(pstorage_handle_t));
	correct_offset = (offset * user_block_size);
	err_code = pstorage_block_identifier_get(flash_handle,0,&redirect_handle); 
	APP_ERROR_CHECK(err_code); 
	err_code = pstorage_load(date, &redirect_handle,size,correct_offset); 
	wy_tools_op()->_delay_us(100);
	return err_code;

}