Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers storage_volume_manager.h Source File

storage_volume_manager.h

00001 /*
00002  * Copyright (c) 2006-2016, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef __STORAGE_VOLUME_MANAGER_H__
00019 #define __STORAGE_VOLUME_MANAGER_H__
00020 
00021 #ifndef __cplusplus
00022 #error "This abstraction requires a C++ toolchain"
00023 #endif // __cplusplus
00024 
00025 #include "storage_abstraction/Driver_Storage.h"
00026 #include "mbed_toolchain.h"                     /* required for MBED_DEPRECATED_SINCE */
00027 
00028 #if !defined(YOTTA_CFG_STORAGE_VOLUME_MANAGER_MAX_VOLUMES)
00029 #define MAX_VOLUMES 4
00030 #else
00031 #define MAX_VOLUMES YOTTA_CFG_STORAGE_VOLUME_MANAGER_MAX_VOLUMES
00032 #endif
00033 /**<
00034  * A static assert to ensure that the size of SequentialJournal is smaller than
00035  * FlashJournal_t. The caller will only allocate a FlashJournal_t and expect the
00036  * Sequential Strategy to reuse that space for a SequentialFlashJournal_t.
00037  */
00038 #ifndef __ICCARM__
00039 typedef char AssertStorageVolumeManagerMaxVolumesIsSane[(((MAX_VOLUMES) > 0) && ((MAX_VOLUMES) <= 8)) ? 0:-1];
00040 #endif
00041 
00042 #define CONCATENATE(A, B) A ## B
00043 #define EXPAND(X) X /* this adds a level of indirection needed to allow macro-expansion following a token-paste operation (see use of CONCATENATE() below). */
00044 
00045 #define STORAGE_API_EXTERN_C_DECLARATIONS_FOR_VOLUME(N)                                                               \
00046     extern "C" ARM_DRIVER_VERSION       GetVersion_ ## N(void);                                                       \
00047     extern "C" ARM_STORAGE_CAPABILITIES GetCapabilities_ ## N(void);                                                  \
00048     extern "C" int32_t                  Initialize_ ## N(ARM_Storage_Callback_t callback);                            \
00049     extern "C" int32_t                  Uninitialize_ ## N(void);                                                     \
00050     extern "C" int32_t                  PowerControl_ ## N(ARM_POWER_STATE state);                                    \
00051     extern "C" int32_t                  ReadData_ ## N(uint64_t addr, void *data, uint32_t size);                     \
00052     extern "C" int32_t                  ProgramData_ ## N(uint64_t addr, const void *data, uint32_t size);            \
00053     extern "C" int32_t                  Erase_ ## N(uint64_t addr, uint32_t size);                                    \
00054     extern "C" int32_t                  EraseAll_ ## N(void);                                                         \
00055     extern "C" ARM_STORAGE_STATUS       GetStatus_ ## N(void);                                                        \
00056     extern "C" int32_t                  GetInfo_ ## N(ARM_STORAGE_INFO *infoP);                                       \
00057     extern "C" uint32_t                 ResolveAddress_ ## N(uint64_t addr);                                          \
00058     extern "C" int32_t                  GetNextBlock_ ## N(const ARM_STORAGE_BLOCK* prevP, ARM_STORAGE_BLOCK *nextP); \
00059     extern "C" int32_t                  GetBlock_ ## N(uint64_t addr, ARM_STORAGE_BLOCK *blockP);
00060 
00061 #define STORAGE_API_EXTERN_C_DECLARATIONS_LIST_FOR_1 STORAGE_API_EXTERN_C_DECLARATIONS_FOR_VOLUME(0)
00062 #define STORAGE_API_EXTERN_C_DECLARATIONS_LIST_FOR_2 STORAGE_API_EXTERN_C_DECLARATIONS_LIST_FOR_1 STORAGE_API_EXTERN_C_DECLARATIONS_FOR_VOLUME(1)
00063 #define STORAGE_API_EXTERN_C_DECLARATIONS_LIST_FOR_3 STORAGE_API_EXTERN_C_DECLARATIONS_LIST_FOR_2 STORAGE_API_EXTERN_C_DECLARATIONS_FOR_VOLUME(2)
00064 #define STORAGE_API_EXTERN_C_DECLARATIONS_LIST_FOR_4 STORAGE_API_EXTERN_C_DECLARATIONS_LIST_FOR_3 STORAGE_API_EXTERN_C_DECLARATIONS_FOR_VOLUME(3)
00065 /* ... add more of the above if ever needed */
00066 
00067 #define STORAGE_API_EXTERN_C_DECLARATIONS_LIST(N) EXPAND(CONCATENATE(STORAGE_API_EXTERN_C_DECLARATIONS_LIST_FOR_, N))
00068 
00069 STORAGE_API_EXTERN_C_DECLARATIONS_LIST(MAX_VOLUMES);
00070 
00071 /**
00072  * Error return codes specific to the Storage volume manager. These extend the
00073  * common error codes from ARM_DRIVER_STORAGE. All Volume-manager APIs return an
00074  * int32_t to allow for both error and success status returns. This enumeration
00075  * contains all possible error status values.
00076  */
00077 typedef enum _StorageVolumeManager_Status
00078 {
00079     STORAGE_VOLUME_MANAGER_STATUS_ERROR_EXHASTED_VOLUMES     = -7,  ///< exhausted the supply of available volumes
00080     STORAGE_VOLUME_MANAGER_STATUS_ERROR_NOT_ERASABLE         = -8,  ///< Part (or all) of the range provided to Erase() isn't erasable.
00081     STORAGE_VOLUME_MANAGER_STATUS_ERROR_NOT_PROGRAMMABLE     = -9,  ///< Part (or all) of the range provided to ProgramData() isn't programmable.
00082     STORAGE_VOLUME_MANAGER_STATUS_ERROR_PROTECTED            = -10, ///< Part (or all) of the range to Erase() or ProgramData() is protected.
00083     STORAGE_VOLUME_MANAGER_STATUS_ERROR_NOT_INITIALIZED      = -11, ///< underlying storage not initialized
00084     STORAGE_VOLUME_MANAGER_STATUS_ERROR_VOLUME_NOT_ALLOCATED = -12, ///< attempt to operate on an unallocated volume
00085 } StorageVolumeManager_Status_t;
00086 
00087 typedef void (*InitializeCallback_t)(int32_t status);
00088 class StorageVolumeManager; /* forward declaration */
00089 
00090 class StorageVolume {
00091 public:
00092     MBED_DEPRECATED_SINCE("mbed-os-5.5", "StorageVolume is deprecated. "
00093                           "Use MBRBlockDevice for volumes instead")
00094     StorageVolume() : allocated(false) { /* empty */ }
00095 
00096 public:
00097     void setup(uint64_t addr, uint64_t size, StorageVolumeManager *volumeManager);
00098 
00099     /*
00100      * Mimic the API of ARM_DRIVER_STORAGE
00101      */
00102 public:
00103     ARM_DRIVER_VERSION       GetVersion(void);
00104     ARM_STORAGE_CAPABILITIES GetCapabilities(void);
00105     int32_t                  Initialize(ARM_Storage_Callback_t callback);
00106     int32_t                  Uninitialize(void);
00107     int32_t                  PowerControl(ARM_POWER_STATE state);
00108     int32_t                  ReadData(uint64_t addr, void *data, uint32_t size);
00109     int32_t                  ProgramData(uint64_t addr, const void *data, uint32_t size);
00110     int32_t                  Erase(uint64_t addr, uint32_t size);
00111     int32_t                  EraseAll(void);
00112     ARM_STORAGE_STATUS       GetStatus(void);
00113     int32_t                  GetInfo(ARM_STORAGE_INFO *infoP);
00114     uint32_t                 ResolveAddress(uint64_t addr);
00115     int32_t                  GetNextBlock(const ARM_STORAGE_BLOCK* prevP, ARM_STORAGE_BLOCK *nextP);
00116     int32_t                  GetBlock(uint64_t addr, ARM_STORAGE_BLOCK *blockP);
00117 
00118 public:
00119     bool isAllocated(void) const {
00120         return allocated;
00121     }
00122 
00123     void deallocate(void) {
00124         allocated = false;
00125     }
00126 
00127     /*
00128      * Accessor methods.
00129      */
00130 
00131     uint64_t getVolumeOffset(void) const {
00132         return volumeOffset;
00133     }
00134     uint64_t getVolumeSize(void) const {
00135         return volumeSize;
00136     }
00137     const ARM_Storage_Callback_t &getCallback(void) const {
00138         return callback;
00139     }
00140 
00141 private:
00142     bool overlapsWithBlock(const ARM_STORAGE_BLOCK* blockP) const {
00143         return (((blockP->addr + blockP->size) <= volumeOffset) || ((volumeOffset + volumeSize) <= blockP->addr)) ? false : true;
00144     }
00145 
00146     void transformBlockToVolume(ARM_STORAGE_BLOCK *blockP) const {
00147         if (blockP->addr < volumeOffset) {
00148             blockP->addr = volumeOffset;
00149         }
00150         if ((blockP->addr + blockP->size) > (volumeOffset + volumeSize)) {
00151             blockP->size = (volumeOffset + volumeSize) - blockP->addr;
00152         }
00153 
00154         blockP->addr -= volumeOffset;
00155     }
00156 
00157 private:
00158     bool                    allocated;
00159     uint64_t                volumeOffset;
00160     uint64_t                volumeSize;
00161     ARM_Storage_Callback_t  callback;
00162     StorageVolumeManager   *volumeManager;
00163 };
00164 
00165 class StorageVolumeManager {
00166 public:
00167     MBED_DEPRECATED_SINCE("mbed-os-5.5", "StorageVolumeManager is deprecated. "
00168                           "Use MBRBlockDevice to manage volumes instead")
00169     StorageVolumeManager()  { /* empty */ }
00170     ~StorageVolumeManager() { /* empty */ }
00171 
00172     /**
00173      * Initialize the storage MTD and prepare it for operation within the context of the volume manager.
00174      *
00175      * @param[in] storage
00176      *              The underlying MTD.
00177      * @param[in] callback
00178      *              A callback to be invoked upon completion of initialization.
00179      *
00180      * @return If asynchronous activity is launched, an invocation returns
00181      *     ARM_DRIVER_OK, and the caller can expect to receive a
00182      *     callback in the future with a status value of ARM_DRIVER_OK or an error-
00183      *     code. In the case of synchronous execution, control returns after
00184      *     completion with a value of 1. Return values less than
00185      *     ARM_DRIVER_OK (0) signify errors.
00186      */
00187     int32_t initialize(ARM_DRIVER_STORAGE *mtd, InitializeCallback_t callback);
00188 
00189     int32_t addVolume(uint64_t addr, uint64_t size, StorageVolume **volumePP);
00190     int32_t addVolume_C(uint64_t addr, uint64_t size, _ARM_DRIVER_STORAGE *mtd);
00191     int32_t lookupVolume(uint64_t addr, StorageVolume **volumePP);
00192 
00193     /*
00194      * Accessor methods.
00195      */
00196 
00197     bool isInitialized() const {
00198         return initialized;
00199     }
00200     ARM_DRIVER_STORAGE *getStorage(void) const {
00201         return storage;
00202     }
00203     const ARM_STORAGE_INFO &getStorageInfo(void) const {
00204         return storageInfo;
00205     }
00206     const ARM_STORAGE_CAPABILITIES &getStorageCapabilities(void) const {
00207         return storageCapabilities;
00208     }
00209     StorageVolume *volumeAtIndex(size_t index) {
00210         return &volumes[index];
00211     }
00212 
00213 public:
00214     static void storageCallback(int32_t status, ARM_STORAGE_OPERATION operation);
00215 
00216 private:
00217     friend int32_t StorageVolume::PowerControl(ARM_POWER_STATE state);
00218     friend int32_t StorageVolume::ReadData(uint64_t addr, void *data, uint32_t size);
00219     friend int32_t StorageVolume::ProgramData(uint64_t addr, const void *data, uint32_t size);
00220     friend int32_t StorageVolume::Erase(uint64_t addr, uint32_t size);
00221     friend int32_t StorageVolume::EraseAll(void);
00222     friend ARM_STORAGE_STATUS StorageVolume::GetStatus(void);
00223     StorageVolume *activeVolume; /* This state-variable is set to point to a volume
00224                                   * while there is pending activity. It tracks
00225                                   * the volume which is at the source of the
00226                                   * activity. Once the activity finishes, this
00227                                   * variable is reset. Having this variable set
00228                                   * might be used as an indication that the
00229                                   * underlying storage is busy. */
00230 
00231 #define FRIEND_DECLARATIONS_FOR_VOLUME(N) \
00232   friend ARM_DRIVER_VERSION       GetVersion_ ## N(void);                                                       \
00233   friend ARM_STORAGE_CAPABILITIES GetCapabilities_ ## N(void);                                                  \
00234   friend int32_t                  Initialize_ ## N(ARM_Storage_Callback_t callback);                            \
00235   friend int32_t                  Uninitialize_ ## N(void);                                                     \
00236   friend int32_t                  PowerControl_ ## N(ARM_POWER_STATE state);                                    \
00237   friend int32_t                  ReadData_ ## N(uint64_t addr, void *data, uint32_t size);                     \
00238   friend int32_t                  ProgramData_ ## N(uint64_t addr, const void *data, uint32_t size);            \
00239   friend int32_t                  Erase_ ## N(uint64_t addr, uint32_t size);                                    \
00240   friend int32_t                  EraseAll_ ## N(void);                                                         \
00241   friend ARM_STORAGE_STATUS       GetStatus_ ## N(void);                                                        \
00242   friend int32_t                  GetInfo_ ## N(ARM_STORAGE_INFO *infoP);                                       \
00243   friend uint32_t                 ResolveAddress_ ## N(uint64_t addr);                                          \
00244   friend int32_t                  GetNextBlock_ ## N(const ARM_STORAGE_BLOCK* prevP, ARM_STORAGE_BLOCK *nextP); \
00245   friend int32_t                  GetBlock_ ## N(uint64_t addr, ARM_STORAGE_BLOCK *blockP);
00246 
00247 #define FRIEND_DECLARATIONS_FOR_STORAGE_API_INSTANCES_FOR_1 FRIEND_DECLARATIONS_FOR_VOLUME(0)
00248 #define FRIEND_DECLARATIONS_FOR_STORAGE_API_INSTANCES_FOR_2 FRIEND_DECLARATIONS_FOR_STORAGE_API_INSTANCES_FOR_1 FRIEND_DECLARATIONS_FOR_VOLUME(1)
00249 #define FRIEND_DECLARATIONS_FOR_STORAGE_API_INSTANCES_FOR_3 FRIEND_DECLARATIONS_FOR_STORAGE_API_INSTANCES_FOR_2 FRIEND_DECLARATIONS_FOR_VOLUME(2)
00250 #define FRIEND_DECLARATIONS_FOR_STORAGE_API_INSTANCES_FOR_4 FRIEND_DECLARATIONS_FOR_STORAGE_API_INSTANCES_FOR_3 FRIEND_DECLARATIONS_FOR_VOLUME(3)
00251 /* ... add more of the above if ever needed */
00252 
00253 #define FRIEND_DECLARATIONS_FOR_STORAGE_API_INSTANCES(N) EXPAND(CONCATENATE(FRIEND_DECLARATIONS_FOR_STORAGE_API_INSTANCES_FOR_, N))
00254 
00255     //todo: remove FRIEND_DECLARATIONS_FOR_STORAGE_API_INSTANCES(MAX_VOLUMES);
00256     FRIEND_DECLARATIONS_FOR_STORAGE_API_INSTANCES(MAX_VOLUMES)
00257 
00258 private:
00259     size_t findIndexOfUnusedVolume(void) const;
00260 
00261 private:
00262     bool                      initialized;
00263     ARM_DRIVER_STORAGE       *storage;
00264     ARM_STORAGE_INFO          storageInfo;
00265     ARM_STORAGE_CAPABILITIES  storageCapabilities;
00266     StorageVolume             volumes[MAX_VOLUMES];
00267 };
00268 
00269 #endif /* __STORAGE_VOLUME_MANAGER_H__ */