Skip to content

Commit 0eabafa

Browse files
authored
Merge pull request #9135 from yossi2le/yossi_get_type_for_bd
Get type for BlockDevice
2 parents 4ec7dd7 + b17d13e commit 0eabafa

36 files changed

+347
-4
lines changed

components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@ bd_size_t DataFlashBlockDevice::size() const
527527
return device_size;
528528
}
529529

530+
const char *DataFlashBlockDevice::get_type() const
531+
{
532+
return "DATAFLASH";
533+
}
534+
530535
/**
531536
* @brief Function for reading a specific register.
532537
* @details Used for reading either the Status Register or Manufacture and ID Register.

components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ class DataFlashBlockDevice : public mbed::BlockDevice {
153153
*/
154154
virtual mbed::bd_size_t size() const;
155155

156+
/** Get the BlockDevice class type.
157+
*
158+
* @return A string represent the BlockDevice class type.
159+
*/
160+
virtual const char *get_type() const;
161+
156162
private:
157163
// Master side hardware
158164
mbed::SPI _spi;

components/storage/blockdevice/COMPONENT_FLASHIAP/FlashIAPBlockDevice.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,9 @@ bd_size_t FlashIAPBlockDevice::size() const
247247
return _size;
248248
}
249249

250+
const char *FlashIAPBlockDevice::get_type() const
251+
{
252+
return "FLASHIAP";
253+
}
254+
250255
#endif /* DEVICE_FLASH */

components/storage/blockdevice/COMPONENT_FLASHIAP/FlashIAPBlockDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ class FlashIAPBlockDevice : public mbed::BlockDevice {
121121
*/
122122
virtual mbed::bd_size_t size() const;
123123

124+
/** Get the BlockDevice class type.
125+
*
126+
* @return A string represent the BlockDevice class type.
127+
*/
128+
virtual const char *get_type() const;
129+
124130
private:
125131
// Device configuration
126132
mbed::FlashIAP _flash;

components/storage/blockdevice/COMPONENT_QSPIF/QSPIFBlockDevice.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ bd_size_t QSPIFBlockDevice::get_erase_size() const
477477
return _min_common_erase_size;
478478
}
479479

480+
const char *QSPIFBlockDevice::get_type() const
481+
{
482+
return "QSPIF";
483+
}
484+
480485
// Find minimal erase size supported by the region to which the address belongs to
481486
bd_size_t QSPIFBlockDevice::get_erase_size(bd_addr_t addr)
482487
{

components/storage/blockdevice/COMPONENT_QSPIF/QSPIFBlockDevice.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ class QSPIFBlockDevice : public mbed::BlockDevice {
211211
*/
212212
virtual mbed::bd_size_t size() const;
213213

214+
/** Get the BlockDevice class type.
215+
*
216+
* @return A string represent the BlockDevice class type.
217+
*/
218+
virtual const char *get_type() const;
219+
214220
private:
215221
// Internal functions
216222

@@ -307,8 +313,6 @@ class QSPIFBlockDevice : public mbed::BlockDevice {
307313
int _utils_iterate_next_largest_erase_type(uint8_t &bitfield, int size, int offset, int boundry);
308314

309315
private:
310-
// Internal Members
311-
312316
// QSPI Driver Object
313317
mbed::QSPI _qspi;
314318

components/storage/blockdevice/COMPONENT_RSPIF/SPIFReducedBlockDevice.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ enum ops {
4848
#define SPIF_WEL 0x2
4949
#define SPIF_WIP 0x1
5050

51-
5251
SPIFReducedBlockDevice::SPIFReducedBlockDevice(
5352
PinName mosi, PinName miso, PinName sclk, PinName cs, int freq)
5453
: _spi(mosi, miso, sclk), _cs(cs), _size(0)
@@ -344,3 +343,9 @@ bd_size_t SPIFReducedBlockDevice::size() const
344343
{
345344
return _size;
346345
}
346+
347+
const char *SPIFReducedBlockDevice::get_type() const
348+
{
349+
return "SPIFR";
350+
}
351+

components/storage/blockdevice/COMPONENT_RSPIF/SPIFReducedBlockDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ class SPIFReducedBlockDevice : public mbed::BlockDevice {
155155
*/
156156
virtual mbed::bd_size_t size() const;
157157

158+
/** Get the BlockDevice class type.
159+
*
160+
* @return A string represent the BlockDevice class type.
161+
*/
162+
virtual const char *get_type() const;
163+
158164
private:
159165
// Master side hardware
160166
mbed::SPI _spi;

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,11 @@ bd_size_t SDBlockDevice::size() const
632632
return _block_size * _sectors;
633633
}
634634

635+
const char *SDBlockDevice::get_type() const
636+
{
637+
return "SD";
638+
}
639+
635640
void SDBlockDevice::debug(bool dbg)
636641
{
637642
_dbg = dbg;

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ class SDBlockDevice : public mbed::BlockDevice {
116116
*/
117117
virtual int frequency(uint64_t freq);
118118

119+
/** Get the BlockDevice class type.
120+
*
121+
* @return A string represent the BlockDevice class type.
122+
*/
123+
virtual const char *get_type() const;
119124

120125
private:
121126
/* Commands : Listed below are commands supported

components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,11 @@ int SPIFBlockDevice::get_erase_value() const
472472
return 0xFF;
473473
}
474474

475+
const char *SPIFBlockDevice::get_type() const
476+
{
477+
return "SPIF";
478+
}
479+
475480
/***************************************************/
476481
/*********** SPI Driver API Functions **************/
477482
/***************************************************/

components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ class SPIFBlockDevice : public mbed::BlockDevice {
189189
*/
190190
virtual mbed::bd_size_t size() const;
191191

192+
/** Get the BlockDevice class type.
193+
*
194+
* @return A string represent the BlockDevice class type.
195+
*/
196+
virtual const char *get_type() const;
197+
192198
private:
193199

194200
// Internal functions
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Get type method addon to block devices class
2+
3+
### Revision history
4+
| Revision | Date | Authors | Mbed OS version | Comments |
5+
|---------- |-----------------|-------------------------------------------------------------|------------------------|------------------|
6+
| 1.0 | 04/12/2018 | Yossi Levy ([@yossi2le](https://github.com/yossi2le/)) | 5.11+ | Initial revision |
7+
8+
## Introduction
9+
10+
Most storage solutions use block devices, whether it is Filesystems, KVStore or any other solution
11+
most of them will have some BlockDevice class underneath. However, sometimes a storage type or an application needs to know the physical
12+
BlockDevice type in order to work smoothly or the BlockDevice type in order to decide what is
13+
the best storage configuration to use.
14+
To address this an add-on method of getting type is proposed for BlockDevice interface class.
15+
16+
## The Motivation
17+
18+
Below there is a list of some examples to explain the motivation and the need for the adding of get_type to BlockDevice interface.
19+
20+
examples:
21+
- TDBStore needs to know if there are flash characteristics for the block device and if there aren�t it should use
22+
FlashSimBlockDevice to simulate a flash BlockDevice.
23+
- TDBStore should not co-exists with NVStore, but this is true only if TDBStore is running on internal memory. Therefore if TDBStore running on
24+
internal memory and NVStore is also there an error should be raised.
25+
- When creating a file system you would prefer working with FAT on top of SD while LITTLEFS on top of any flash block device.
26+
Those preference in favor of better performance.
27+
28+
To summarize the above, it may be very useful when using block device to know the type of the instance and especially, but not only,
29+
when using get_default_instace. Sometimes applications and tests would like to behave differently depending on the instance that has been created
30+
or provided to them.
31+
32+
In fact it might be worth to consider adding the get_type to any interface with get_default_instance at mbed-os.
33+
34+
## Dive into details
35+
we should add the following method to BlockDevice interface class.
36+
37+
```virtual const char * get_type() const = 0;```
38+
39+
then every physical BlockDevice class which implements the interface should also implement this method and return a string
40+
representing its type. Furthermore, a nonphysical BlockDevice like SlicingBlockDevice should return the underlying physical
41+
BlockDevice type.
42+
43+
### Physical BlockDevice:
44+
```
45+
const char * HeapBlockDevice::get_type() const
46+
{
47+
return "HEAP";
48+
}
49+
```
50+
51+
### Logical BlockDevice:
52+
```
53+
const char * SlicingBlockDevice::get_type() const
54+
{
55+
if (_bd != NULL) {
56+
return _bd->get_type();
57+
}
58+
59+
return NULL;
60+
}
61+
```
62+
63+
### Open issue
64+
The ChainingBlockDevice which chains different type of physical block devices into one block device is unable
65+
to return the underneath physical as it contains two or more types. Therefore it will return CHAINING as its
66+
identity and its left for the user to decide how the application will treat this information.
67+
68+
69+
The below table describes physical BlockDevice and its tyep names
70+
71+
72+
| BlockDevice class | String description |
73+
|-----------------------------|--------------------|
74+
| HeapBlockDevice | "HEAP" |
75+
| SPIFBlockDevice | "SPIF" |
76+
| SPIFReducedBlockDevice | "SPIFR" |
77+
| QSPIFBlockDevice | "QSPIF" |
78+
| SDBlockDevice | "SD" |
79+
| FlashIAPBlockDevice | "FLASHIAP" |
80+
| DataFlashBlockDevice | "DATAFLASH" |
81+
| ChainingBlockDevice | "CHAINING" |

features/storage/TESTS/blockdevice/general_block_device/main.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,30 @@ void test_program_read_small_data_sizes()
466466
delete block_device;
467467
}
468468

469+
void test_get_type_functionality()
470+
{
471+
BlockDevice *block_device = BlockDevice::get_default_instance();
472+
if (block_device == NULL) {
473+
TEST_SKIP_MESSAGE("No block device component is defined for this target");
474+
return;
475+
}
476+
const char *bd_type = block_device->get_type();
477+
TEST_ASSERT_NOT_EQUAL(0, bd_type);
478+
479+
#if COMPONENT_QSPIF
480+
TEST_ASSERT_EQUAL(0, strcmp(bd_type, "QSPIF"));
481+
#elif COMPONENT_SPIF
482+
TEST_ASSERT_EQUAL(0, strcmp(bd_type, "SPIF"));
483+
#elif COMPONENT_DATAFLASH
484+
TEST_ASSERT_EQUAL(0, strcmp(bd_type, "DATAFLASH"));
485+
#elif COMPONENT_SD
486+
TEST_ASSERT_EQUAL(0, strcmp(bd_type, "SD"));
487+
#elif COMPONET_FLASHIAP
488+
TEST_ASSERT_EQUAL(0, strcmp(bd_type, "FLASHIAP"));
489+
#endif
490+
491+
}
492+
469493
utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason)
470494
{
471495
greentea_case_failure_abort_handler(source, reason);
@@ -484,7 +508,8 @@ Case cases[] = {
484508
Case("Testing multi threads erase program read", test_multi_threads, greentea_failure_handler),
485509
Case("Testing contiguous erase, write and read", test_contiguous_erase_write_read, greentea_failure_handler),
486510
Case("Testing BlockDevice::get_erase_value()", test_get_erase_value, greentea_failure_handler),
487-
Case("Testing program read small data sizes", test_program_read_small_data_sizes, greentea_failure_handler)
511+
Case("Testing program read small data sizes", test_program_read_small_data_sizes, greentea_failure_handler),
512+
Case("Testing get type functionality", test_get_type_functionality, greentea_failure_handler)
488513
};
489514

490515
Specification specification(test_setup, cases);

features/storage/TESTS/blockdevice/heap_block_device/main.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,19 @@ void test_read_write()
154154

155155
}
156156

157+
void test_get_type_functionality()
158+
{
159+
uint8_t *dummy = new (std::nothrow) uint8_t[TEST_BLOCK_DEVICE_SIZE];
160+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory for test");
161+
delete[] dummy;
162+
163+
HeapBlockDevice bd(TEST_BLOCK_DEVICE_SIZE, TEST_BLOCK_SIZE);
164+
165+
const char *bd_type = bd.get_type();
166+
TEST_ASSERT_NOT_EQUAL(0, bd_type);
167+
TEST_ASSERT_EQUAL(0, strcmp(bd_type, "HEAP"));
168+
}
169+
157170

158171
// Test setup
159172
utest::v1::status_t test_setup(const size_t number_of_cases)
@@ -164,6 +177,7 @@ utest::v1::status_t test_setup(const size_t number_of_cases)
164177

165178
Case cases[] = {
166179
Case("Testing read write random blocks", test_read_write),
180+
Case("Testing get type functionality", test_get_type_functionality)
167181
};
168182

169183
Specification specification(test_setup, cases);

features/storage/blockdevice/BlockDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ class BlockDevice {
233233
(addr + size) % get_erase_size(addr + size - 1) == 0 &&
234234
addr + size <= this->size());
235235
}
236+
237+
/** Get the BlockDevice class type.
238+
*
239+
* @return A string represent the BlockDevice class type.
240+
*/
241+
virtual const char *get_type() const = 0;
236242
};
237243

238244
} // namespace mbed

features/storage/blockdevice/BufferedBlockDevice.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,13 @@ bd_size_t BufferedBlockDevice::size() const
330330
return _bd_size;
331331
}
332332

333+
const char *BufferedBlockDevice::get_type() const
334+
{
335+
if (_bd != NULL) {
336+
return _bd->get_type();
337+
}
338+
339+
return NULL;
340+
}
341+
333342
} // namespace mbed

features/storage/blockdevice/BufferedBlockDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ class BufferedBlockDevice : public BlockDevice {
152152
*/
153153
virtual bd_size_t size() const;
154154

155+
/** Get the BlockDevice class type.
156+
*
157+
* @return A string represent the BlockDevice class type.
158+
*/
159+
virtual const char *get_type() const;
160+
155161
protected:
156162
BlockDevice *_bd;
157163
bd_size_t _bd_program_size;

features/storage/blockdevice/ChainingBlockDevice.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,9 @@ bd_size_t ChainingBlockDevice::size() const
283283
return _size;
284284
}
285285

286+
const char *ChainingBlockDevice::get_type() const
287+
{
288+
return "CHAINING";
289+
}
290+
286291
} // namespace mbed

features/storage/blockdevice/ChainingBlockDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ class ChainingBlockDevice : public BlockDevice {
172172
*/
173173
virtual bd_size_t size() const;
174174

175+
/** Get the BlockDevice class type.
176+
*
177+
* @return A string represent the BlockDevice class type.
178+
*/
179+
virtual const char *get_type() const;
180+
175181
protected:
176182
BlockDevice **_bds;
177183
size_t _bd_count;

features/storage/blockdevice/ExhaustibleBlockDevice.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,14 @@ bd_size_t ExhaustibleBlockDevice::size() const
194194
return _bd->size();
195195
}
196196

197+
const char *ExhaustibleBlockDevice::get_type() const
198+
{
199+
if (_bd != NULL) {
200+
return _bd->get_type();
201+
}
202+
203+
return NULL;
204+
}
205+
197206
} // namespace mbed
207+

0 commit comments

Comments
 (0)