Skip to content

Commit 9ad5f8d

Browse files
author
Seppo Takalo
committed
Add EmulatedSD stubs that allow using files as a BlockDevice
1 parent 4e079ec commit 9ad5f8d

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

UNITTESTS/stubs/EmulatedSD.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "EmulatedSD.h"
17+
#include <stdio.h>
18+
19+
class EmulatedSD_Private {
20+
public:
21+
~EmulatedSD_Private() {
22+
if (fs) {
23+
fclose(fs);
24+
}
25+
}
26+
const char *path;
27+
FILE *fs;
28+
bd_size_t size;
29+
};
30+
31+
EmulatedSD::EmulatedSD(const char *path)
32+
{
33+
_p = new EmulatedSD_Private;
34+
_p->path = path;
35+
}
36+
37+
EmulatedSD::~EmulatedSD()
38+
{
39+
delete _p;
40+
}
41+
42+
int EmulatedSD::init()
43+
{
44+
_p->fs = fopen(_p->path, "r+");
45+
if (!_p->fs) {
46+
perror("fdopen():");
47+
return -1;
48+
}
49+
if (fseek(_p->fs, 0, SEEK_END)) {
50+
perror("fseek()");
51+
fclose(_p->fs);
52+
_p->fs = nullptr;
53+
return -1;
54+
}
55+
_p->size = ftell(_p->fs);
56+
rewind(_p->fs);
57+
return 0;
58+
}
59+
60+
int EmulatedSD::deinit()
61+
{
62+
fclose(_p->fs);
63+
_p->fs = nullptr;
64+
}
65+
66+
int EmulatedSD::read(void *buffer, bd_addr_t addr, bd_size_t size)
67+
{
68+
if (!_p->fs) {
69+
return -1;
70+
}
71+
if (fseek(_p->fs, addr, SEEK_SET)) {
72+
perror("fseek()");
73+
return -1;
74+
}
75+
size_t r = fread(buffer, size, 1, _p->fs);
76+
if (r < 1) {
77+
perror("fread()");
78+
return -1;
79+
}
80+
return 0;
81+
}
82+
83+
int EmulatedSD::program(const void *buffer, bd_addr_t addr, bd_size_t size)
84+
{
85+
if (!_p->fs) {
86+
return -1;
87+
}
88+
if (fseek(_p->fs, addr, SEEK_SET)) {
89+
perror("fseek()");
90+
return -1;
91+
}
92+
size_t r = fwrite(buffer, size, 1, _p->fs);
93+
if (r < 1) {
94+
perror("fread()");
95+
return -1;
96+
}
97+
return 0;
98+
}
99+
100+
bd_size_t EmulatedSD::get_read_size() const
101+
{
102+
return 512;
103+
}
104+
bd_size_t EmulatedSD::get_program_size() const
105+
{
106+
return 512;
107+
}
108+
bd_size_t EmulatedSD::size() const
109+
{
110+
if (!_p->fs) {
111+
return -1;
112+
}
113+
return _p->size;
114+
}
115+
const char *EmulatedSD::get_type() const
116+
{
117+
return "SD";
118+
}

UNITTESTS/stubs/EmulatedSD.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef EMULATEDSD_H
17+
#define EMULATEDSD_H
18+
19+
#include "features/storage/blockdevice/BlockDevice.h"
20+
21+
class EmulatedSD_Private;
22+
23+
class EmulatedSD : public mbed::BlockDevice {
24+
public:
25+
EmulatedSD(const char *path);
26+
~EmulatedSD();
27+
28+
/** Initialize a block device
29+
*
30+
* @return 0 on success or a negative error code on failure
31+
*/
32+
virtual int init();
33+
34+
/** Deinitialize a block device
35+
*
36+
* @return 0 on success or a negative error code on failure
37+
*/
38+
virtual int deinit();
39+
40+
/** Read blocks from a block device
41+
*
42+
* If a failure occurs, it is not possible to determine how many bytes succeeded
43+
*
44+
* @param buffer Buffer to write blocks to
45+
* @param addr Address of block to begin reading from
46+
* @param size Size to read in bytes, must be a multiple of the read block size
47+
* @return 0 on success or a negative error code on failure
48+
*/
49+
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
50+
51+
/** Program blocks to a block device
52+
*
53+
* The blocks must have been erased prior to being programmed
54+
*
55+
* If a failure occurs, it is not possible to determine how many bytes succeeded
56+
*
57+
* @param buffer Buffer of data to write to blocks
58+
* @param addr Address of block to begin writing to
59+
* @param size Size to write in bytes, must be a multiple of the program block size
60+
* @return 0 on success or a negative error code on failure
61+
*/
62+
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
63+
64+
/** Get the size of a readable block
65+
*
66+
* @return Size of a readable block in bytes
67+
*/
68+
virtual bd_size_t get_read_size() const;
69+
70+
/** Get the size of a programmable block
71+
*
72+
* @return Size of a programmable block in bytes
73+
* @note Must be a multiple of the read size
74+
*/
75+
virtual bd_size_t get_program_size() const;
76+
77+
/** Get the total size of the underlying device
78+
*
79+
* @return Size of the underlying device in bytes
80+
*/
81+
virtual bd_size_t size() const;
82+
83+
/** Get the BlockDevice class type.
84+
*
85+
* @return A string represent the BlockDevice class type.
86+
*/
87+
virtual const char *get_type() const;
88+
private:
89+
EmulatedSD_Private *_p;
90+
};
91+
92+
#endif // EMULATEDSD_H

0 commit comments

Comments
 (0)