Skip to content

Commit fb53687

Browse files
author
Seppo Takalo
committed
Add EmulatedSD stubs that allow using files as a BlockDevice
1 parent 9294aab commit fb53687

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

UNITTESTS/stubs/EmulatedSD.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include "EmulatedSD.h"
2+
#include <stdio.h>
3+
4+
class EmulatedSD_Private {
5+
public:
6+
~EmulatedSD_Private() {
7+
if (fs) {
8+
fclose(fs);
9+
}
10+
}
11+
const char *path;
12+
FILE *fs;
13+
bd_size_t size;
14+
};
15+
16+
EmulatedSD::EmulatedSD(const char *path)
17+
{
18+
_p = new EmulatedSD_Private;
19+
_p->path = path;
20+
}
21+
22+
EmulatedSD::~EmulatedSD()
23+
{
24+
delete _p;
25+
}
26+
27+
int EmulatedSD::init()
28+
{
29+
_p->fs = fopen(_p->path, "r+");
30+
if (!_p->fs) {
31+
perror("fdopen():");
32+
return -1;
33+
}
34+
if (fseek(_p->fs, 0, SEEK_END)) {
35+
perror("fseek()");
36+
fclose(_p->fs);
37+
_p->fs = nullptr;
38+
return -1;
39+
}
40+
_p->size = ftell(_p->fs);
41+
rewind(_p->fs);
42+
return 0;
43+
}
44+
45+
int EmulatedSD::deinit()
46+
{
47+
fclose(_p->fs);
48+
_p->fs = nullptr;
49+
}
50+
51+
int EmulatedSD::read(void *buffer, bd_addr_t addr, bd_size_t size)
52+
{
53+
if (!_p->fs) {
54+
return -1;
55+
}
56+
if (fseek(_p->fs, addr, SEEK_SET)) {
57+
perror("fseek()");
58+
return -1;
59+
}
60+
size_t r = fread(buffer, size, 1, _p->fs);
61+
if (r < 1) {
62+
perror("fread()");
63+
return -1;
64+
}
65+
return 0;
66+
}
67+
68+
int EmulatedSD::program(const void *buffer, bd_addr_t addr, bd_size_t size)
69+
{
70+
if (!_p->fs) {
71+
return -1;
72+
}
73+
if (fseek(_p->fs, addr, SEEK_SET)) {
74+
perror("fseek()");
75+
return -1;
76+
}
77+
size_t r = fwrite(buffer, size, 1, _p->fs);
78+
if (r < 1) {
79+
perror("fread()");
80+
return -1;
81+
}
82+
return 0;
83+
}
84+
85+
bd_size_t EmulatedSD::get_read_size() const
86+
{
87+
return 512;
88+
}
89+
bd_size_t EmulatedSD::get_program_size() const
90+
{
91+
return 512;
92+
}
93+
bd_size_t EmulatedSD::size() const
94+
{
95+
if (!_p->fs) {
96+
return -1;
97+
}
98+
return _p->size;
99+
}
100+
const char *EmulatedSD::get_type() const
101+
{
102+
return "SD";
103+
}

UNITTESTS/stubs/EmulatedSD.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#ifndef EMULATEDSD_H
2+
#define EMULATEDSD_H
3+
4+
#include "features/storage/blockdevice/BlockDevice.h"
5+
6+
class EmulatedSD_Private;
7+
8+
class EmulatedSD : public mbed::BlockDevice {
9+
public:
10+
EmulatedSD(const char *path);
11+
~EmulatedSD();
12+
13+
/** Initialize a block device
14+
*
15+
* @return 0 on success or a negative error code on failure
16+
*/
17+
virtual int init();
18+
19+
/** Deinitialize a block device
20+
*
21+
* @return 0 on success or a negative error code on failure
22+
*/
23+
virtual int deinit();
24+
25+
/** Read blocks from a block device
26+
*
27+
* If a failure occurs, it is not possible to determine how many bytes succeeded
28+
*
29+
* @param buffer Buffer to write blocks to
30+
* @param addr Address of block to begin reading from
31+
* @param size Size to read in bytes, must be a multiple of the read block size
32+
* @return 0 on success or a negative error code on failure
33+
*/
34+
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
35+
36+
/** Program blocks to a block device
37+
*
38+
* The blocks must have been erased prior to being programmed
39+
*
40+
* If a failure occurs, it is not possible to determine how many bytes succeeded
41+
*
42+
* @param buffer Buffer of data to write to blocks
43+
* @param addr Address of block to begin writing to
44+
* @param size Size to write in bytes, must be a multiple of the program block size
45+
* @return 0 on success or a negative error code on failure
46+
*/
47+
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
48+
49+
/** Get the size of a readable block
50+
*
51+
* @return Size of a readable block in bytes
52+
*/
53+
virtual bd_size_t get_read_size() const;
54+
55+
/** Get the size of a programmable block
56+
*
57+
* @return Size of a programmable block in bytes
58+
* @note Must be a multiple of the read size
59+
*/
60+
virtual bd_size_t get_program_size() const;
61+
62+
/** Get the total size of the underlying device
63+
*
64+
* @return Size of the underlying device in bytes
65+
*/
66+
virtual bd_size_t size() const;
67+
68+
/** Get the BlockDevice class type.
69+
*
70+
* @return A string represent the BlockDevice class type.
71+
*/
72+
virtual const char *get_type() const;
73+
private:
74+
EmulatedSD_Private *_p;
75+
};
76+
77+
#endif // EMULATEDSD_H

0 commit comments

Comments
 (0)