NVProperty generic key value store using the MCU flash area.

Dependents:   Turtle_RadioShuttle

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NVProperty_SRAM.cpp Source File

NVProperty_SRAM.cpp

00001 /*
00002  * This is an unpublished work copyright
00003  * (c) 2019 Helmut Tschemernjak
00004  * 30826 Garbsen (Hannover) Germany
00005  *
00006  *
00007  * Use is granted to registered RadioShuttle licensees only.
00008  * Licensees must own a valid serial number and product code.
00009  * Details see: www.radioshuttle.de
00010  */
00011 
00012 #include <string.h>
00013 #include <stdlib.h>
00014 #include <stdint.h>
00015 #ifndef ARDUINO
00016 #include "arch.h"
00017 #endif
00018 #include <NVPropertyProviderInterface.h>
00019 #include <NVProperty.h>
00020 #include <NVProperty_SRAM.h>
00021 
00022 NVProperty_SRAM::NVProperty_SRAM()
00023 {
00024     
00025 }
00026 
00027 NVProperty_SRAM::~NVProperty_SRAM()
00028 {
00029     std::map<int, PropertyEntry>::iterator re;
00030     for(re = _props.begin(); re != _props.end(); re++) {
00031         if (re->second.type == NVProperty::T_STR)
00032             free(re->second.str);
00033         if (re->second.type == NVProperty::T_BLOB)
00034             delete[] (char *)re->second.data;
00035     }
00036     
00037     _props.clear();
00038 }
00039 
00040 int
00041 NVProperty_SRAM::GetProperty(int key)
00042 {
00043     std::map<int, PropertyEntry>::iterator it = _props.find(key);
00044     if(it != _props.end()) {
00045         switch (it->second.type) {
00046             case NVProperty::T_STR:
00047                 return NVProperty::NVP_ENOENT;
00048             case NVProperty::T_BLOB:
00049                 return NVProperty::NVP_ENOENT;
00050             default:
00051                 return it->second.val32;
00052         }
00053     }
00054     return NVProperty::NVP_ENOENT;
00055 };
00056 
00057 
00058 int64_t
00059 NVProperty_SRAM::GetProperty64(int key)
00060 {
00061     std::map<int, PropertyEntry>::iterator it = _props.find(key);
00062     if(it != _props.end()) {
00063         switch (it->second.type) {
00064             case NVProperty::T_STR:
00065                 return NVProperty::NVP_ENOENT;
00066             case NVProperty::T_BLOB:
00067                 return NVProperty::NVP_ENOENT;
00068             default:
00069                 return it->second.val64;
00070         }
00071     }
00072     return NVProperty::NVP_ENOENT;
00073 }
00074 
00075 const char *
00076 NVProperty_SRAM::GetPropertyStr(int key)
00077 {
00078     std::map<int, PropertyEntry>::iterator it = _props.find(key);
00079     if(it != _props.end()) {
00080         if (it->second.type == NVProperty::T_STR) {
00081             return (const char *)it->second.str;
00082         }
00083     }
00084     return NULL;
00085 }
00086 
00087 int
00088 
00089 NVProperty_SRAM::GetPropertyBlob(int key, const void *blob, int *size)
00090 {
00091     if (!blob || *size <= 0)
00092         return NVProperty::NVP_INVALD_PARM;
00093     
00094     std::map<int, PropertyEntry>::iterator it = _props.find(key);
00095     if(it != _props.end()) {
00096         switch (it->second.type) {
00097             case NVProperty::T_BLOB:
00098                 *size = std::min(*size, (int)it->second.size);
00099                 if (blob)
00100                     memcpy((void *)blob, it->second.data, *size);
00101                 return *size;
00102             default:
00103                 break;
00104         }
00105     }
00106     return NVProperty::NVP_ENOENT;
00107 }
00108 
00109 
00110 int
00111 NVProperty_SRAM::SetProperty(int key, int64_t value, int type)
00112 {
00113     std::map<int, PropertyEntry>::iterator it = _props.find(key);
00114     if(it != _props.end()) {
00115         it->second.val32 = value;
00116         return 0;
00117     }
00118     
00119     struct PropertyEntry r;
00120     memset(&r, 0, sizeof(r));
00121     r.key = key;
00122     r.type = type;
00123     if (type <= NVProperty::T_32BIT) {
00124         r.size = sizeof(r.val32);
00125         r.val32 = value;
00126     } else if (type == NVProperty::T_64BIT) {
00127         r.size = sizeof(r.val64);
00128         r.val64 = value;
00129     }
00130     
00131     _props.insert(std::pair<int,PropertyEntry> (key, r));
00132     
00133     return NVProperty::NVP_OK;
00134 }
00135 
00136 int
00137 NVProperty_SRAM::SetPropertyStr(int key, const char *str, int type)
00138 {
00139     std::map<int, PropertyEntry>::iterator it = _props.find(key);
00140     if(it != _props.end()) {
00141         if (it->second.str)
00142             free(it->second.str);
00143         it->second.str = strdup(str);
00144         it->second.size = strlen(str)+1;
00145         return NVProperty::NVP_OK;
00146     }
00147     
00148     struct PropertyEntry r;
00149     memset(&r, 0, sizeof(r));
00150     r.key = key;
00151     r.type = type;
00152     r.size = strlen(str)+1;
00153     r.str = strdup(str);
00154     
00155     _props.insert(std::pair<int,PropertyEntry> (key, r));
00156         
00157     return NVProperty::NVP_OK;
00158 }
00159 
00160 int
00161 NVProperty_SRAM::SetPropertyBlob(int key, const void *blob, int size, int type)
00162 {
00163     std::map<int, PropertyEntry>::iterator it = _props.find(key);
00164     if(it != _props.end()) {
00165         if (it->second.data)
00166             delete[] (char *)it->second.data;
00167         it->second.size = size;
00168         it->second.data = new char[size];
00169         memcpy(it->second.data, blob, size);
00170 
00171         return NVProperty::NVP_OK;
00172     }
00173     
00174     struct PropertyEntry r;
00175     memset(&r, 0, sizeof(r));
00176     r.key = key;
00177     r.type = type;
00178     r.size = size;
00179     r.data = new char[size];
00180     memcpy(r.data, blob, size);
00181     _props.insert(std::pair<int,PropertyEntry> (key, r));
00182     
00183     return NVProperty::NVP_OK;
00184 }
00185 
00186 int
00187 NVProperty_SRAM::EraseProperty(int key)
00188 {
00189     std::map<int, PropertyEntry>::iterator it = _props.find(key);
00190     if(it == _props.end()) {
00191         return NVProperty::NVP_ENOENT;
00192     }
00193     if (it->second.type == NVProperty::T_STR)
00194         free((char *)it->second.data);
00195     if (it->second.type == NVProperty::T_BLOB)
00196         delete[] (char *)it->second.data;
00197 
00198     _props.erase(it->first);
00199     return NVProperty::NVP_OK;
00200 }
00201 
00202 int
00203 NVProperty_SRAM::ReorgProperties(void)
00204 {
00205     return NVProperty::NVP_OK;
00206 }
00207 
00208 int
00209 NVProperty_SRAM::OpenPropertyStore(bool forWrite)
00210 {
00211     UNUSED(forWrite);
00212     return NVProperty::NVP_OK;
00213 }
00214 
00215 int
00216 NVProperty_SRAM::ClosePropertyStore(bool flush)
00217 {
00218     UNUSED(flush);
00219     return NVProperty::NVP_OK;
00220 }