Prvni odevzdani - netestovano!!!

This commit is contained in:
2018-12-11 08:21:23 +01:00
parent 03264a797c
commit 5d3c79fe91
6 changed files with 194 additions and 0 deletions

View File

@ -0,0 +1,84 @@
#if defined(ESP8266)
extern "C" {
#include "user_interface.h"
}
#define ESP_getChipId() (ESP.getChipId())
#else
#include <esp_wifi.h>
#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;
}

View File

@ -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);