/**
 * \callgraph
 *\brief 	Initialize the blocks of flash memory used for reading/writing.
 *\details	Uses Nordic's pstorage software abstraction/API to cleanly access flash when the SoftDevice (for BLE)
 *		is also running.
 */
void ladybug_flash_init() {
  SEGGER_RTT_WriteString(0,"==> IN ladybug_flash_init\n");
  pstorage_module_param_t pstorage_param;   //Used when registering with pstorage
  pstorage_handle_t	  handle;	    //used to access the chunk-o-flash requested when registering
  //First thing is to initialize pstorage
  uint32_t err_code = pstorage_init();
  //if initialization was not successful, the error routine will be called and the code won't proceed.
  APP_ERROR_CHECK(err_code);
  //Next is to register amount of flash needed.  The smallest amount that can be requested is 16 bytes.  I'm requesting
  //32 bytes because this is the number of bytes needed for plant_info. Two blocks are used...one for plant info and one for hydro values.  I must make a
  //block request for the larger amount of bytes
  pstorage_param.block_size = BLOCK_SIZE;
  //request three blocks - one will be for pH, one for plant_info, and one for device name
  pstorage_param.block_count = 3;
  //assign a callback so know when a command has finished.
  pstorage_param.cb = ladybug_flash_handler;
  err_code = pstorage_register(&pstorage_param, &handle);
  APP_ERROR_CHECK(err_code);
  //Then get the handles to the blocks of flash
  pstorage_block_identifier_get(&handle, 0, &m_block_calibration_store_handle);
  pstorage_block_identifier_get(&handle,1,&m_block_plantInfo_store_handle);
  pstorage_block_identifier_get(&handle,2,&m_block_device_name_store_handle);
  // Create the timer.  This will be called before a Flash activity is requested to avoid forever hanging.
  create_timer();

}
Esempio n. 2
0
Storage::Storage()
{
	u32 err;

	//Register with Terminal
	Terminal::AddTerminalCommandListener(this);

	//Initialize queue for queueing store and load tasks
	taskQueue = new SimpleQueue(taskBuffer, TASK_BUFFER_LENGTH);

	//Initialize pstorage library
	pstorage_module_param_t param;
	u8 num_blocks = STORAGE_BLOCK_NUMBER;

	bufferedOperationInProgress = false;

	param.block_size  = STORAGE_BLOCK_SIZE; //Multiple of 4 and divisor of page size (pagesize is usually 1024) in case total size is bigger than page size
	param.block_count = num_blocks;
	param.cb          = PstorageEventHandler;

	err = pstorage_init();
	APP_ERROR_CHECK(err);

	//Register a number of blocks
	if (pstorage_register(&param, &handle) != NRF_SUCCESS){
		logt("STORAGE", "Could not register storage");
	}
	for (u32 i = 0; i < num_blocks; ++i){
		pstorage_block_identifier_get(&handle, i, &block_handles[i]);
	}
}
Esempio n. 3
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;
  }
}
Esempio n. 4
0
void crypto_storeKeys(void)
{
  uint32_t err_code;

  if (crypto_storing)
  {
    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 = {};
    memcpy(keys.srp_b, srp.b, 32);
    memcpy(keys.srp_salt, srp.salt, 16);
    memcpy(keys.srp_v, srp.v, 384);
    memcpy(keys.srp_B, srp.B, 384);
    memcpy(keys.sign_secret, crypto_keys.sign.secret, 64);
    memcpy(keys.clientname, crypto_keys.client.name, 36);
    memcpy(keys.ltpk, crypto_keys.client.ltpk, 32);

    keys.instance = CRYPTO_INSTANCE;
    keys.valid0 = 0x55;
    keys.valid1 = 0xAA;
    err_code = pstorage_update(&handle, (uint8_t*)&keys, sizeof(keys), 0);
    APP_ERROR_CHECK(err_code);

    // Pump events until the store is done
    while (crypto_storing)
    {
      err_code = sd_app_evt_wait();
      APP_ERROR_CHECK(err_code);
      app_sched_execute();
    }
  }
}
Esempio n. 5
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;

}
Esempio n. 6
0
H_U32 _StorageWrite(pstorage_handle_t *flash_handle,H_U8 *data,H_U32 size,H_U32 offset,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); 
	WYPrintf(MODULE_DEBUG_STORAGE,LEVEL_DEBUG,"before write2 for module:%d",flash_handle->module_id);
	err_code = pstorage_store(&redirect_handle,(H_U8 *)data,size,correct_offset); 
	wy_tools_op()->_delay_us(100);
	return err_code;
	//APP_ERROR_CHECK(err_code); 

}
bool pstorage_driver_register_block(uint8_t * data, uint16_t size) 
{
    uint32_t err_code;
    
    pstorage_driver.block[num_of_reg_blocks].data = data;  // Set data field of current block (which is determined by value of num_of_reg_blocks).
    pstorage_driver.block[num_of_reg_blocks].size = size;  // Set size field of current block (which is determined by value of num_of_reg_blocks).
    
	  // Function to get block id with reference to base block id and current block (which is determined by value of num_of_reg_blocks).
    err_code = pstorage_block_identifier_get(&pstorage_driver.base_id, num_of_reg_blocks, &pstorage_driver.block[num_of_reg_blocks].block_id);
    if (err_code != NRF_SUCCESS) 
		{
        return false;
    }
		
		// Incerment value of num_of_reg_blocks.
    num_of_reg_blocks++;
		
    return true;
}