Dependencies: DM_FATFileSystem EthernetInterface HTTPClient mbed-rtos mbed-src
Fork of DMSupport by
Diff: Registry.h
- Revision:
- 20:9df19da50290
- Child:
- 34:fc366bab393f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Registry.h Mon Jan 12 10:33:53 2015 +0100 @@ -0,0 +1,118 @@ +/* + * Copyright 2014 Embedded Artists AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef REGISTRY_H +#define REGISTRY_H + +#include "mbed.h" +#include "rtos.h" +#include "InternalEEPROM.h" + +/** + * Example of using the Registry class: + * + * @code + * #include "mbed.h" + * #include "DMBoard.h" + * + * int main(void) { + * DMBoard* board = &DMBoard::instance(); + * board->init(); + * ... + * board->registry() + * } + * @endcode + */ +class Registry { +public: + + enum RegistryError { + Ok = 0, + ReadOnlyError, + MemoryError, + EEPROMReadError, + EEPROMWriteError, + NoSuchKeyError, + KeyLenError, + ValueLenError, + InvalidPositionError, + RegistryFullError, + }; + + /** Get the only instance of the Registry + * + * @returns The Registry + */ + static Registry& instance() + { + static Registry singleton; + return singleton; + } + + /** Loads all (if any) values from the internal EEPROM + * + * @returns + * Ok on success + * An error code on failure + */ + RegistryError load(); + + RegistryError setValue(const char* key, const char* val); + RegistryError getValue(const char* key, char** pVal); + RegistryError entryAt(int pos, char** pKey, char** pVal); + int numEntries() { return _numEntries; } + RegistryError registerListener(); + + /** Stores the registry in the internal EEPROM + * + * @returns + * Ok on success + * An error code on failure + */ + RegistryError store(); + + +private: + enum Constants { + NumEntries = InternalEEPROM::EEPROM_NUM_PAGES, + EntryLen = 32, + EntrySize = 2*EntryLen, + }; + + typedef struct { + char key[EntryLen]; + char val[EntryLen]; + } reg_entry_t; + + uint8_t* _data; + int _numEntries; + reg_entry_t* _entries; + bool _modified[NumEntries]; + Mutex _mutex; + + explicit Registry(); + // hide copy constructor + Registry(const Registry&); + // hide assign operator + Registry& operator=(const Registry&); + ~Registry(); + + RegistryError fromEEPROM(); + RegistryError toEEPROM(); + int find(const char* key); +}; + +#endif