Dependencies: DM_FATFileSystem EthernetInterface HTTPClient mbed-rtos mbed-src
Fork of DMSupport by
Registry.h@34:fc366bab393f, 2015-03-09 (annotated)
- Committer:
- embeddedartists
- Date:
- Mon Mar 09 11:15:56 2015 +0100
- Revision:
- 34:fc366bab393f
- Parent:
- 20:9df19da50290
- Added size guards in InternalEEPROM
- Added documentation in missing classes
- Added option to redirect RtosLog to USBSerial (a USB Device Serial port)
- Moved the mbed_mac_address function from the SDK to the BIOS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 20:9df19da50290 | 1 | /* |
embeddedartists | 20:9df19da50290 | 2 | * Copyright 2014 Embedded Artists AB |
embeddedartists | 20:9df19da50290 | 3 | * |
embeddedartists | 20:9df19da50290 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
embeddedartists | 20:9df19da50290 | 5 | * you may not use this file except in compliance with the License. |
embeddedartists | 20:9df19da50290 | 6 | * You may obtain a copy of the License at |
embeddedartists | 20:9df19da50290 | 7 | * |
embeddedartists | 20:9df19da50290 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
embeddedartists | 20:9df19da50290 | 9 | * |
embeddedartists | 20:9df19da50290 | 10 | * Unless required by applicable law or agreed to in writing, software |
embeddedartists | 20:9df19da50290 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
embeddedartists | 20:9df19da50290 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
embeddedartists | 20:9df19da50290 | 13 | * See the License for the specific language governing permissions and |
embeddedartists | 20:9df19da50290 | 14 | * limitations under the License. |
embeddedartists | 20:9df19da50290 | 15 | */ |
embeddedartists | 20:9df19da50290 | 16 | |
embeddedartists | 20:9df19da50290 | 17 | #ifndef REGISTRY_H |
embeddedartists | 20:9df19da50290 | 18 | #define REGISTRY_H |
embeddedartists | 20:9df19da50290 | 19 | |
embeddedartists | 20:9df19da50290 | 20 | #include "mbed.h" |
embeddedartists | 20:9df19da50290 | 21 | #include "rtos.h" |
embeddedartists | 20:9df19da50290 | 22 | #include "InternalEEPROM.h" |
embeddedartists | 20:9df19da50290 | 23 | |
embeddedartists | 20:9df19da50290 | 24 | /** |
embeddedartists | 20:9df19da50290 | 25 | * Example of using the Registry class: |
embeddedartists | 20:9df19da50290 | 26 | * |
embeddedartists | 20:9df19da50290 | 27 | * @code |
embeddedartists | 20:9df19da50290 | 28 | * #include "mbed.h" |
embeddedartists | 20:9df19da50290 | 29 | * #include "DMBoard.h" |
embeddedartists | 20:9df19da50290 | 30 | * |
embeddedartists | 20:9df19da50290 | 31 | * int main(void) { |
embeddedartists | 20:9df19da50290 | 32 | * DMBoard* board = &DMBoard::instance(); |
embeddedartists | 20:9df19da50290 | 33 | * board->init(); |
embeddedartists | 20:9df19da50290 | 34 | * ... |
embeddedartists | 20:9df19da50290 | 35 | * board->registry() |
embeddedartists | 20:9df19da50290 | 36 | * } |
embeddedartists | 20:9df19da50290 | 37 | * @endcode |
embeddedartists | 20:9df19da50290 | 38 | */ |
embeddedartists | 20:9df19da50290 | 39 | class Registry { |
embeddedartists | 20:9df19da50290 | 40 | public: |
embeddedartists | 20:9df19da50290 | 41 | |
embeddedartists | 20:9df19da50290 | 42 | enum RegistryError { |
embeddedartists | 20:9df19da50290 | 43 | Ok = 0, |
embeddedartists | 20:9df19da50290 | 44 | ReadOnlyError, |
embeddedartists | 20:9df19da50290 | 45 | MemoryError, |
embeddedartists | 20:9df19da50290 | 46 | EEPROMReadError, |
embeddedartists | 20:9df19da50290 | 47 | EEPROMWriteError, |
embeddedartists | 20:9df19da50290 | 48 | NoSuchKeyError, |
embeddedartists | 20:9df19da50290 | 49 | KeyLenError, |
embeddedartists | 20:9df19da50290 | 50 | ValueLenError, |
embeddedartists | 20:9df19da50290 | 51 | InvalidPositionError, |
embeddedartists | 20:9df19da50290 | 52 | RegistryFullError, |
embeddedartists | 20:9df19da50290 | 53 | }; |
embeddedartists | 20:9df19da50290 | 54 | |
embeddedartists | 20:9df19da50290 | 55 | /** Get the only instance of the Registry |
embeddedartists | 20:9df19da50290 | 56 | * |
embeddedartists | 20:9df19da50290 | 57 | * @returns The Registry |
embeddedartists | 20:9df19da50290 | 58 | */ |
embeddedartists | 20:9df19da50290 | 59 | static Registry& instance() |
embeddedartists | 20:9df19da50290 | 60 | { |
embeddedartists | 20:9df19da50290 | 61 | static Registry singleton; |
embeddedartists | 20:9df19da50290 | 62 | return singleton; |
embeddedartists | 20:9df19da50290 | 63 | } |
embeddedartists | 20:9df19da50290 | 64 | |
embeddedartists | 20:9df19da50290 | 65 | /** Loads all (if any) values from the internal EEPROM |
embeddedartists | 20:9df19da50290 | 66 | * |
embeddedartists | 20:9df19da50290 | 67 | * @returns |
embeddedartists | 20:9df19da50290 | 68 | * Ok on success |
embeddedartists | 20:9df19da50290 | 69 | * An error code on failure |
embeddedartists | 20:9df19da50290 | 70 | */ |
embeddedartists | 20:9df19da50290 | 71 | RegistryError load(); |
embeddedartists | 20:9df19da50290 | 72 | |
embeddedartists | 34:fc366bab393f | 73 | /** Sets (or replaces if existing) the value for the key |
embeddedartists | 34:fc366bab393f | 74 | * |
embeddedartists | 34:fc366bab393f | 75 | * The key/value parameters are copied so it is ok to free them |
embeddedartists | 34:fc366bab393f | 76 | * after calling this function. |
embeddedartists | 34:fc366bab393f | 77 | * |
embeddedartists | 34:fc366bab393f | 78 | * @param key the key to create or update |
embeddedartists | 34:fc366bab393f | 79 | * @param val the value to store for the key |
embeddedartists | 34:fc366bab393f | 80 | * |
embeddedartists | 34:fc366bab393f | 81 | * @returns |
embeddedartists | 34:fc366bab393f | 82 | * Ok on success |
embeddedartists | 34:fc366bab393f | 83 | * An error code on failure |
embeddedartists | 34:fc366bab393f | 84 | */ |
embeddedartists | 20:9df19da50290 | 85 | RegistryError setValue(const char* key, const char* val); |
embeddedartists | 34:fc366bab393f | 86 | |
embeddedartists | 34:fc366bab393f | 87 | /** Gets (if any) the the value for the key |
embeddedartists | 34:fc366bab393f | 88 | * |
embeddedartists | 34:fc366bab393f | 89 | * If Ok is returned then pVal will point to allocated memory |
embeddedartists | 34:fc366bab393f | 90 | * that must be deallocated with free() by the caller. |
embeddedartists | 34:fc366bab393f | 91 | * |
embeddedartists | 34:fc366bab393f | 92 | * @param key the key to look for |
embeddedartists | 34:fc366bab393f | 93 | * @param pVal will hold the value if successful |
embeddedartists | 34:fc366bab393f | 94 | * |
embeddedartists | 34:fc366bab393f | 95 | * @returns |
embeddedartists | 34:fc366bab393f | 96 | * Ok on success |
embeddedartists | 34:fc366bab393f | 97 | * NoSuchKeyError if there is no value for the key |
embeddedartists | 34:fc366bab393f | 98 | * An error code on failure |
embeddedartists | 34:fc366bab393f | 99 | */ |
embeddedartists | 20:9df19da50290 | 100 | RegistryError getValue(const char* key, char** pVal); |
embeddedartists | 34:fc366bab393f | 101 | |
embeddedartists | 34:fc366bab393f | 102 | /** Retrieves the key-value pair at the specified index |
embeddedartists | 34:fc366bab393f | 103 | * |
embeddedartists | 34:fc366bab393f | 104 | * If Ok is returned then pKey and pVal will point to allocated |
embeddedartists | 34:fc366bab393f | 105 | * memory that must be deallocated with free() by the caller. |
embeddedartists | 34:fc366bab393f | 106 | * |
embeddedartists | 34:fc366bab393f | 107 | * @param pos the index to look for |
embeddedartists | 34:fc366bab393f | 108 | * @param pKey will hold the key if successful |
embeddedartists | 34:fc366bab393f | 109 | * @param pVal will hold the value if successful |
embeddedartists | 34:fc366bab393f | 110 | * |
embeddedartists | 34:fc366bab393f | 111 | * @returns |
embeddedartists | 34:fc366bab393f | 112 | * Ok on success |
embeddedartists | 34:fc366bab393f | 113 | * An error code on failure |
embeddedartists | 34:fc366bab393f | 114 | */ |
embeddedartists | 20:9df19da50290 | 115 | RegistryError entryAt(int pos, char** pKey, char** pVal); |
embeddedartists | 34:fc366bab393f | 116 | |
embeddedartists | 34:fc366bab393f | 117 | /** Returns the number of key-value pairs in the registry |
embeddedartists | 34:fc366bab393f | 118 | * |
embeddedartists | 34:fc366bab393f | 119 | * @returns the number of key-value pairs |
embeddedartists | 34:fc366bab393f | 120 | */ |
embeddedartists | 20:9df19da50290 | 121 | int numEntries() { return _numEntries; } |
embeddedartists | 34:fc366bab393f | 122 | |
embeddedartists | 20:9df19da50290 | 123 | RegistryError registerListener(); |
embeddedartists | 20:9df19da50290 | 124 | |
embeddedartists | 20:9df19da50290 | 125 | /** Stores the registry in the internal EEPROM |
embeddedartists | 20:9df19da50290 | 126 | * |
embeddedartists | 20:9df19da50290 | 127 | * @returns |
embeddedartists | 20:9df19da50290 | 128 | * Ok on success |
embeddedartists | 20:9df19da50290 | 129 | * An error code on failure |
embeddedartists | 20:9df19da50290 | 130 | */ |
embeddedartists | 20:9df19da50290 | 131 | RegistryError store(); |
embeddedartists | 20:9df19da50290 | 132 | |
embeddedartists | 20:9df19da50290 | 133 | |
embeddedartists | 20:9df19da50290 | 134 | private: |
embeddedartists | 20:9df19da50290 | 135 | enum Constants { |
embeddedartists | 20:9df19da50290 | 136 | NumEntries = InternalEEPROM::EEPROM_NUM_PAGES, |
embeddedartists | 20:9df19da50290 | 137 | EntryLen = 32, |
embeddedartists | 20:9df19da50290 | 138 | EntrySize = 2*EntryLen, |
embeddedartists | 20:9df19da50290 | 139 | }; |
embeddedartists | 20:9df19da50290 | 140 | |
embeddedartists | 20:9df19da50290 | 141 | typedef struct { |
embeddedartists | 20:9df19da50290 | 142 | char key[EntryLen]; |
embeddedartists | 20:9df19da50290 | 143 | char val[EntryLen]; |
embeddedartists | 20:9df19da50290 | 144 | } reg_entry_t; |
embeddedartists | 20:9df19da50290 | 145 | |
embeddedartists | 20:9df19da50290 | 146 | uint8_t* _data; |
embeddedartists | 20:9df19da50290 | 147 | int _numEntries; |
embeddedartists | 20:9df19da50290 | 148 | reg_entry_t* _entries; |
embeddedartists | 20:9df19da50290 | 149 | bool _modified[NumEntries]; |
embeddedartists | 20:9df19da50290 | 150 | Mutex _mutex; |
embeddedartists | 20:9df19da50290 | 151 | |
embeddedartists | 20:9df19da50290 | 152 | explicit Registry(); |
embeddedartists | 20:9df19da50290 | 153 | // hide copy constructor |
embeddedartists | 20:9df19da50290 | 154 | Registry(const Registry&); |
embeddedartists | 20:9df19da50290 | 155 | // hide assign operator |
embeddedartists | 20:9df19da50290 | 156 | Registry& operator=(const Registry&); |
embeddedartists | 20:9df19da50290 | 157 | ~Registry(); |
embeddedartists | 20:9df19da50290 | 158 | |
embeddedartists | 20:9df19da50290 | 159 | RegistryError fromEEPROM(); |
embeddedartists | 20:9df19da50290 | 160 | RegistryError toEEPROM(); |
embeddedartists | 20:9df19da50290 | 161 | int find(const char* key); |
embeddedartists | 20:9df19da50290 | 162 | }; |
embeddedartists | 20:9df19da50290 | 163 | |
embeddedartists | 20:9df19da50290 | 164 | #endif |