From 5d3c79fe91ee1d13cf88af114f7ba7d55cbaab10 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 11 Dec 2018 08:21:23 +0100 Subject: [PATCH] Prvni odevzdani - netestovano!!! --- .gitignore | 34 +++++++++++++ keywords.txt | 20 ++++++++ library.json | 22 +++++++++ library.properties | 9 ++++ src/encipheredEEPROMStrings.cpp | 84 +++++++++++++++++++++++++++++++++ src/encipheredEEPROMStrings.h | 25 ++++++++++ 6 files changed, 194 insertions(+) create mode 100644 .gitignore create mode 100644 keywords.txt create mode 100644 library.json create mode 100644 library.properties create mode 100644 src/encipheredEEPROMStrings.cpp create mode 100644 src/encipheredEEPROMStrings.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e257658 --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# ---> C++ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..f43af49 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,20 @@ +####################################### +# Syntax Coloring Map +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +####################################### +# Instances (KEYWORD2) +####################################### + + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/library.json b/library.json new file mode 100644 index 0000000..fdc659c --- /dev/null +++ b/library.json @@ -0,0 +1,22 @@ +{ + "name":"encipheredEEPROMStrings", + "description":"Store and retrieve EEPROM strings", + "keywords":"eeprom,storage", + "authors": + { + "name": "Pavel Brychta", + "maintainer": true + }, + "repository": + { + "type": "git", + "url": "http://git.xpablo.cz/xPablo.cz/encipheredEEPROMStrings.git" + }, + "version": "1.0", + "license": "MIT", + "frameworks": "arduino", + "platforms": ["espressif8266","espressif32"], + "build": { + "libCompatMode": 2 + } +} diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..0802fa7 --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=encipheredEEPROMStrings +version=1.0 +author=Pavel Brychta +maintainer=Pavel Brychta +sentence=Store and retrieve EEPROM strings +paragraph=Store and retrieve EEPROM strings +category=Other +url=http://git.xpablo.cz/xPablo.cz/encipheredEEPROMStrings +architectures=esp8266,esp32 diff --git a/src/encipheredEEPROMStrings.cpp b/src/encipheredEEPROMStrings.cpp new file mode 100644 index 0000000..506d60b --- /dev/null +++ b/src/encipheredEEPROMStrings.cpp @@ -0,0 +1,84 @@ +#if defined(ESP8266) + extern "C" { + #include "user_interface.h" + } + #define ESP_getChipId() (ESP.getChipId()) +#else + #include + #define ESP_getChipId() ((uint32_t)ESP.getEfuseMac()) +#endif + +String EES_readString(unsigned int start, size_t maxlen) +{ + uint8_t key[4]; + unsigned int enciphidx = 0; + String result; + unsigned int top = EEPROM.read(start); + unsigned int i; + + result.reserve(maxlen); + uint32_t cid = ESP_getChipId(); + uint32_t *pkey = (uint32_t *)&key[0]; + *pkey = cid; + DEBUG_MSG("Key = %02X:%02X:%02X:%02X\r\n", key[0], key[1], key[2], key[3]); + if ((top > 0) && (top < (maxlen - 2))) + { + uint8_t b; + for (i = start + 1; i < + start + top + 1; ++i) + { + b = EEPROM.read(i); + + b ^= key[enciphidx]; + ++enciphidx; + enciphidx %= sizeof(key); + + result.concat(char(b)); + } + b = EEPROM.read(i) ^ key[enciphidx]; + if (b != 0) + { + DEBUG_MSG("_readString error! (%s)\r\n", result.c_str()); + result = F(""); // spatna ukoncovaci nula - neplatny retezec + } + } + return result; +} + +bool EES_storeString(unsigned int start, size_t maxlen, String &string) +{ + uint8_t key[4]; + unsigned int enciphidx = 0; + unsigned int si = 0; + unsigned int top; + char c; + bool result = false; // retezec nebyl ulozeny cely (nevesel s do bufferu) + unsigned int i; + + if (string.length() > maxlen - 2) + top = maxlen - 2; + else + { + result = true; // retezec se do urceneho mista vejde + top = string.length(); + } + + uint32_t cid = ESP_getChipId(); + uint32_t *pkey = (uint32_t *)&key[0]; + *pkey = cid; + DEBUG_MSG("Key = %02X:%02X:%02X:%02X\r\n", key[0], key[1], key[2], key[3]); + EEPROM.write(start, (uint8_t)top); // ulozime delku retezce (pouzite pr kontrolu pri vycitani) + for (i = start + 1; i < start + top + 1; ++i) + { + c = string[si]; + c ^= key[enciphidx]; + ++enciphidx; + enciphidx %= sizeof(key); + EEPROM.write(i, c); + ++si; + } + c = 0 ^ key[enciphidx]; + EEPROM.write(i, c); // ukoncovaci a kontrolni nula + + return result; +} + diff --git a/src/encipheredEEPROMStrings.h b/src/encipheredEEPROMStrings.h new file mode 100644 index 0000000..43938a9 --- /dev/null +++ b/src/encipheredEEPROMStrings.h @@ -0,0 +1,25 @@ +/* Knihovna pro ukladani a cteni retezcu do/z EEPROM. + * Retezce jsou ulozeny jednoduse sifrovane pomoci XOR sifry. + * @author Pavel Brychta http://www.xpabo.cz + */ + +/** + * @brief Cteni sifrovanych retezcu z EEPROM + * + * @param[in] start Pocatecni adresa retezce v EEPROM + * @param[in] maxlen Maximalni delka retezce + * + * @return Vycteny a desifrovany retezec + */ +String EES_readString(unsigned int start, size_t maxlen); + +/** + * @brief Zapis retezcu do EEPROM + * + * @param[in] start Pocatecni adresa retezce v EEPROM + * @param[in] maxlen Maximalni delka retezce + * @param string Ukladany retezec + * + * @return Priznak, zda se zapis podaril + */ +bool EES_storeString(unsigned int start, size_t maxlen, String &string);