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.cpp Source File

NVProperty.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 #ifdef ARDUINO
00013 #include <Arduino.h>
00014 #elif __MBED__
00015 #include <mbed.h>
00016 #else
00017 #error "Unkown operating system"
00018 #endif
00019 
00020 
00021 #include <NVPropertyProviderInterface.h>
00022 #include <NVProperty_SRAM.h>
00023 #ifdef ARDUINO_ARCH_ESP32
00024   #include <NVProperty_ESP32NVS.h>
00025   #include <NVProperty_ESP32efuse.h>
00026 #elif defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_ARCH_SAMD)
00027   #include <NVProperty_D21Flash.h>
00028 #elif __MBED__
00029   #include <mbed.h>
00030   #include <NVProperty_MBEDFlash.h>
00031   #ifdef TARGET_STM32L4
00032     #include <NVProperty_L4OTP.h>
00033   #endif
00034 #else
00035 #error "Unkown implementation"
00036 #endif
00037 #include <NVProperty.h>
00038 
00039 
00040 NVProperty::NVProperty(int propSizekB, bool erase)
00041 {
00042     _flash = NULL;
00043     _otp = NULL;
00044     _ram = new NVProperty_SRAM();
00045     
00046 #ifdef ARDUINO_ARCH_ESP32
00047     _flash = new NVProperty_ESP32NVS();
00048 #elif defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_ARCH_SAMD)
00049     _flash = new NVProperty_D21Flash(propSizekB, erase);
00050 #elif __MBED__
00051     _flash = new NVProperty_MBEDFlash(propSizekB, erase);
00052 #else
00053  #error "unkown platform"
00054 #endif
00055 
00056 #ifdef ARDUINO_ARCH_ESP32
00057     _otp = new NVProperty_ESP32efuse();
00058 #elif TARGET_STM32L4
00059     _otp = new NVProperty_L4OTP();
00060 #endif
00061     _allowWrite = false;
00062     _didOpen = false;
00063 }
00064 
00065 NVProperty::~NVProperty()
00066 {
00067     if (_ram) {
00068         delete _ram;
00069     }
00070     if (_flash)
00071         delete _flash;
00072     if (_otp)
00073         delete _otp;
00074 }
00075 
00076 
00077 int
00078 NVProperty::GetProperty(int key, int defaultValue)
00079 {
00080     int res;
00081     
00082     if (!_didOpen)
00083         OpenPropertyStore();
00084     
00085     if (_ram) {
00086         res = _ram->GetProperty(key);
00087         if (res != NVP_ENOENT)
00088             return res;
00089     }
00090     if (_flash) {
00091         res = _flash->GetProperty(key);
00092         if (res != NVP_ENOENT)
00093             return res;
00094     }
00095     if (_otp) {
00096         res = _otp->GetProperty(key);
00097         if (res != NVP_ENOENT)
00098             return res;
00099     }
00100     return defaultValue;
00101 }
00102 
00103 
00104 int64_t
00105 NVProperty::GetProperty64(int key, int64_t defaultValue)
00106 {
00107     int64_t res;
00108     
00109     if (!_didOpen)
00110         OpenPropertyStore();
00111 
00112     if (_ram) {
00113         res = _ram->GetProperty64(key);
00114         if (res != NVP_ENOENT)
00115             return res;
00116     }
00117     if (_flash) {
00118         res = _flash->GetProperty64(key);
00119         if (res != NVP_ENOENT)
00120             return res;
00121     }
00122     if (_otp) {
00123         res = _otp->GetProperty64(key);
00124         if (res != NVP_ENOENT)
00125             return res;
00126     }
00127     return defaultValue;
00128 }
00129 
00130 const char *
00131 NVProperty::GetProperty(int key, const char *defaultValue)
00132 {
00133     const char *res;
00134     
00135     if (!_didOpen)
00136         OpenPropertyStore();
00137 
00138     if (_ram) {
00139         res = _ram->GetPropertyStr(key);
00140         if (res != NULL)
00141             return res;
00142     }
00143     if (_flash) {
00144         res = _flash->GetPropertyStr(key);
00145         if (res != NULL)
00146             return res;
00147     }
00148     if (_otp) {
00149         res = _otp->GetPropertyStr(key);
00150         if (res != NULL)
00151             return res;
00152     }
00153     if (res != NULL)
00154         return res;
00155     
00156     return defaultValue;
00157 }
00158 
00159 int
00160 NVProperty::GetProperty(int key, void *buffer, int *size)
00161 {
00162     int res;
00163     int maxsize = *size;
00164     
00165     if (!_didOpen)
00166         OpenPropertyStore();
00167 
00168     if (_ram) {
00169         res = _ram->GetPropertyBlob(key, buffer, &maxsize);
00170         if (res == NVP_OK)
00171             return res;
00172     }
00173     if (_flash) {
00174         res = _flash->GetPropertyBlob(key, buffer, &maxsize);
00175         if (res == NVP_OK)
00176             return res;
00177     }
00178     if (_otp) {
00179         res = _otp->GetPropertyBlob(key, buffer, &maxsize);
00180         if (res == NVP_OK)
00181             return res;
00182     }
00183  
00184     return NVP_ENOENT;
00185 }
00186 
00187 
00188 int
00189 NVProperty::SetProperty(int key, NVPType ptype, int64_t value, NVPStore store)
00190 {
00191     int res = NVP_OK;
00192     
00193     if (!_didOpen)
00194         OpenPropertyStore();
00195 
00196     if (!_allowWrite)
00197         return NVP_NO_PERM;
00198     
00199     if (store == S_RAM && _ram) {
00200             res = _ram->SetProperty(key, value, ptype);
00201     } else if (store == S_FLASH && _flash) {
00202             res = _flash->SetProperty(key, value, ptype);
00203     } else if (store == S_OTP && _otp) {
00204             res = _otp->SetProperty(key, value, ptype);
00205     } else {
00206         return NVP_NO_STORE;
00207     }
00208     return res;
00209 }
00210 
00211 
00212 int
00213 NVProperty::SetProperty(int key, NVPType ptype, const char *value, NVPStore store)
00214 {
00215     int res = NVP_OK;
00216     
00217     if (!_didOpen)
00218         OpenPropertyStore();
00219 
00220     if (!_allowWrite)
00221         return NVP_NO_PERM;
00222 
00223     if (store == S_RAM && _ram) {
00224         res = _ram->SetPropertyStr(key, value, ptype);
00225     } else if (store == S_FLASH && _flash) {
00226         res = _flash->SetPropertyStr(key, value, ptype);
00227     } else if (store == S_OTP && _otp) {
00228         res = _otp->SetPropertyStr(key, value, ptype);
00229     } else {
00230         return NVP_NO_STORE;
00231     }
00232     
00233     return res;
00234 }
00235 
00236 // NVProperty_SRAM::SetPropertyBlob(int key, const void *blob, int size, int type)
00237 
00238 
00239 int
00240 NVProperty::SetProperty(int key, NVPType ptype,  const void *blob, int length, NVPStore store)
00241 {
00242     int res = NVP_OK;
00243 
00244     if (!_didOpen)
00245         OpenPropertyStore();
00246 
00247     if (!_allowWrite) {
00248         return NVP_NO_PERM;
00249     }
00250     
00251     if (store == S_RAM && _ram) {
00252         res = _ram->SetPropertyBlob(key, blob, length, ptype);
00253     } else if (store == S_FLASH && _flash) {
00254         res = _flash->SetPropertyBlob(key, blob, length, ptype);
00255     } else if (store == S_OTP && _otp) {
00256         res = _otp->SetPropertyBlob(key, blob, length, ptype);
00257     } else {
00258         return NVP_NO_STORE;
00259     }
00260 
00261     return res;
00262 }
00263 
00264 int
00265 NVProperty::EraseProperty(int key, NVPStore store)
00266 {
00267     if (!_didOpen)
00268         OpenPropertyStore();
00269 
00270     int res = NVP_OK;
00271     
00272     if (!_allowWrite)
00273         return NVP_NO_PERM;
00274 
00275     if (store == S_RAM && _ram) {
00276         res = _ram->EraseProperty(key);
00277     } else if (store == S_FLASH && _flash) {
00278         res = _flash->EraseProperty(key);
00279     } else if (store == S_OTP && _otp) {
00280         res = _otp->EraseProperty(key);
00281     } else {
00282         return NVP_NO_STORE;
00283     }
00284     
00285     return res;
00286 }
00287 
00288 int
00289 NVProperty::ReorgProperties(NVPStore store)
00290 {
00291     int res = NVP_OK;
00292     
00293     if (!_didOpen)
00294         OpenPropertyStore();
00295 
00296     if (!_allowWrite)
00297         return NVP_NO_PERM;
00298 
00299     if (store == S_RAM && _ram) {
00300         res = _ram->ReorgProperties();
00301     } else if (store == S_FLASH && _flash) {
00302         res = _flash->ReorgProperties();
00303     } else if (store == S_OTP && _otp) {
00304         res = _otp->ReorgProperties();
00305     } else {
00306         return NVP_NO_STORE;
00307     }
00308     
00309     return res;
00310 }
00311 
00312 int
00313 NVProperty::OpenPropertyStore(bool forWrite)
00314 {
00315     int res = NVP_OK;
00316 
00317     if (_didOpen) {
00318         if (_ram)
00319             _ram->ClosePropertyStore();
00320         if (_flash)
00321             _flash->ClosePropertyStore();
00322         if (_otp)
00323             _otp->ClosePropertyStore();
00324     }
00325     
00326     if (_ram)
00327         _ram->OpenPropertyStore(forWrite);
00328     if (_flash)
00329         res = _flash->OpenPropertyStore(forWrite);
00330     if (_otp)
00331         _otp->OpenPropertyStore(forWrite);
00332     _didOpen = true;
00333     if(forWrite)
00334         _allowWrite = true;
00335     
00336 
00337     return res;
00338 }
00339 
00340 int
00341 NVProperty::ClosePropertyStore(bool flush)
00342 {
00343     int res = NVP_OK;
00344 
00345     if (_didOpen)
00346         return NVP_NO_PERM;
00347 
00348     if (_ram)
00349         _ram->ClosePropertyStore(flush);
00350     if (_flash)
00351         res = _flash->ClosePropertyStore(flush);
00352     if (_otp)
00353         _otp->ClosePropertyStore(flush);
00354     return res;
00355 }
00356 
00357