Mistake on this page?
Report an issue in GitHub or email us
ObservingBlockDevice.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2017-2017 ARM Limited
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 /** \addtogroup storage */
24 /** @{*/
25 
26 #ifndef MBED_OBSERVING_BLOCK_DEVICE_H
27 #define MBED_OBSERVING_BLOCK_DEVICE_H
28 
29 #include "BlockDevice.h"
30 #include "platform/PlatformMutex.h"
31 #include "platform/Callback.h"
32 
33 namespace mbed {
34 
36 public:
37 
38  /** Lifetime of the block device
39  *
40  * @param bd Block device to observe
41  */
43  virtual ~ObservingBlockDevice();
44 
45  /** Attach a callback which is called on change
46  *
47  * @param cb Function to call on filesystem change (erase or program)
48  */
49  void attach(mbed::Callback<void(BlockDevice *)> cb);
50 
51  /** Initialize a block device
52  *
53  * @return 0 on success or a negative error code on failure
54  */
55  virtual int init();
56 
57  /** Deinitialize a block device
58  *
59  * @return 0 on success or a negative error code on failure
60  */
61  virtual int deinit();
62 
63  /** Ensure data on storage is in sync with the driver
64  *
65  * @return 0 on success or a negative error code on failure
66  */
67  virtual int sync();
68 
69  /** Read blocks from a block device
70  *
71  * @param buffer Buffer to read blocks into
72  * @param addr Address of block to begin reading from
73  * @param size Size to read in bytes, must be a multiple of read block size
74  * @return 0 on success, negative error code on failure
75  */
76  virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
77 
78  /** Program blocks to a block device
79  *
80  * The blocks must have been erased prior to being programmed
81  *
82  * @param buffer Buffer of data to write to blocks
83  * @param addr Address of block to begin writing to
84  * @param size Size to write in bytes, must be a multiple of program block size
85  * @return 0 on success, negative error code on failure
86  */
87  virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
88 
89  /** Erase blocks on a block device
90  *
91  * The state of an erased block is undefined until it has been programmed,
92  * unless get_erase_value returns a non-negative byte value
93  *
94  * @param addr Address of block to begin erasing
95  * @param size Size to erase in bytes, must be a multiple of erase block size
96  * @return 0 on success, negative error code on failure
97  */
98  virtual int erase(bd_addr_t addr, bd_size_t size);
99 
100  /** Get the size of a readable block
101  *
102  * @return Size of a readable block in bytes
103  */
104  virtual bd_size_t get_read_size() const;
105 
106  /** Get the size of a programmable block
107  *
108  * @return Size of a programmable block in bytes
109  */
110  virtual bd_size_t get_program_size() const;
111 
112  /** Get the size of an erasable block
113  *
114  * @return Size of an erasable block in bytes
115  */
116  virtual bd_size_t get_erase_size() const;
117 
118  /** Get the size of an erasable block given address
119  *
120  * @param addr Address within the erasable block
121  * @return Size of an erasable block in bytes
122  * @note Must be a multiple of the program size
123  */
124  virtual bd_size_t get_erase_size(bd_addr_t addr) const;
125 
126  /** Get the value of storage when erased
127  *
128  * If get_erase_value returns a non-negative byte value, the underlying
129  * storage is set to that value when erased, and storage containing
130  * that value can be programmed without another erase.
131  *
132  * @return The value of storage when erased, or -1 if you can't
133  * rely on the value of erased storage
134  */
135  virtual int get_erase_value() const;
136 
137  /** Get the total size of the underlying device
138  *
139  * @return Size of the underlying device in bytes
140  */
141  virtual bd_size_t size() const;
142 
143  /** Get the BlockDevice class type.
144  *
145  * @return A string represent the BlockDevice class type.
146  */
147  virtual const char *get_type() const;
148 
149 private:
150  BlockDevice *_bd;
152 };
153 
154 } // namespace mbed
155 
156 // Added "using" for backwards compatibility
157 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
159 #endif
160 
161 #endif
162 
163 /** @}*/
virtual int erase(bd_addr_t addr, bd_size_t size)
Erase blocks on a block device.
virtual int get_erase_value() const
Get the value of storage when erased.
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:48
void attach(mbed::Callback< void(BlockDevice *)> cb)
Attach a callback which is called on change.
virtual int init()
Initialize a block device.
ObservingBlockDevice(BlockDevice *bd)
Lifetime of the block device.
virtual bd_size_t size() const
Get the total size of the underlying device.
virtual const char * get_type() const
Get the BlockDevice class type.
virtual int deinit()
Deinitialize a block device.
virtual bd_size_t get_erase_size() const
Get the size of an erasable block.
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size)
Read blocks from a block device.
virtual bd_size_t get_read_size() const
Get the size of a readable block.
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size)
Program blocks to a block device.
virtual int sync()
Ensure data on storage is in sync with the driver.
Callback class based on template specialization.
Definition: Callback.h:53
Definition: ATHandler.h:46
virtual bd_size_t get_program_size() const
Get the size of a programmable block.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.