Bump version to v11.1.0.4

- Restructure tasmota
This commit is contained in:
Theo Arends 2022-06-02 14:17:39 +02:00
parent 6d58e801f6
commit c08561f67c
332 changed files with 2111 additions and 2112 deletions

View File

@ -21,6 +21,6 @@
#define __TASMOTA_INCLUDE__
#include "../tasmota/my_user_config.h"
#include "../tasmota/tasmota_configurations.h"
#include "../tasmota/include/tasmota_configurations.h"
#endif // __TASMOTA_INCLUDE__

View File

@ -12,7 +12,7 @@
#ifdef COMPILE_BERRY_LIB
#include "my_user_config.h"
#include "tasmota_configurations.h"
#include "include/tasmota_configurations.h"
#endif
/* Macro: BE_DEBUG

View File

@ -6,7 +6,7 @@ Import("env")
def FindInoNodes(env):
src_dir = glob.escape(env.subst("$PROJECT_SRC_DIR"))
return env.Glob(os.path.join(src_dir, "*.ino")) + env.Glob(
os.path.join(src_dir, "ino_*", "*.ino")
os.path.join(src_dir, "tasmota_*", "*.ino")
)
env.AddMethod(FindInoNodes)

View File

@ -1,4 +1,4 @@
// Configuration
#include "my_user_config.h"
#include "tasmota_configurations.h"
#include "include/tasmota_configurations.h"

View File

@ -37,15 +37,15 @@ Be sure to generate both files: local_ca_data.h, and local_ca_descriptor.h
//
// this is what the result will look like, except there will be
// a lot of data bytes defined in the first three arrays
//
static const unsigned char PROGMEM TA0_DN[] = {
// variable number of bytes go here (typically 100-140 or so) for the DN
};
static const unsigned char PROGMEM TA0_RSA_N[] = {
// 256 bytes go here for the public key modulus
};
static const unsigned char PROGMEM TA0_RSA_E[] = {
// 3 bytes go here for the public key exponent
};
//
static const unsigned char PROGMEM TA0_DN[] = {
// variable number of bytes go here (typically 100-140 or so) for the DN
};
static const unsigned char PROGMEM TA0_RSA_N[] = {
// 256 bytes go here for the public key modulus
};
static const unsigned char PROGMEM TA0_RSA_E[] = {
// 3 bytes go here for the public key exponent
};

View File

@ -37,14 +37,14 @@ Be sure to generate both files: local_ca_data.h, and local_ca_descriptor.h
//
// this is what the result will look like
//
{
{ (unsigned char *)TA0_DN, sizeof TA0_DN },
BR_X509_TA_CA,
{
BR_KEYTYPE_RSA,
{ .rsa = {
(unsigned char *)TA0_RSA_N, sizeof TA0_RSA_N,
(unsigned char *)TA0_RSA_E, sizeof TA0_RSA_E,
} }
}
}
{
{ (unsigned char *)TA0_DN, sizeof TA0_DN },
BR_X509_TA_CA,
{
BR_KEYTYPE_RSA,
{ .rsa = {
(unsigned char *)TA0_RSA_N, sizeof TA0_RSA_N,
(unsigned char *)TA0_RSA_E, sizeof TA0_RSA_E,
} }
}
}

View File

@ -63,7 +63,7 @@ String EthernetMacAddress(void);
* Preconfigured configurations
\*********************************************************************************************/
#include "tasmota_configurations.h" // Preconfigured configurations
#include "include/tasmota_configurations.h" // Preconfigured configurations
/*********************************************************************************************\
* ESP8266 specific parameters

View File

@ -1239,7 +1239,7 @@ typedef struct MYTMPLT {
//********************************************************************************************
#ifdef ESP8266
#include "tasmota_template_legacy.h"
#include "include/tasmota_template_legacy.h"
/********************************************************************************************\
* ESP8266 Module Templates

View File

@ -1,5 +1,5 @@
/*
settings.h - setting variables for Tasmota
tasmota_types.h - setting variables for Tasmota
Copyright (C) 2021 Theo Arends
@ -17,10 +17,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _SETTINGS_H_
#define _SETTINGS_H_
const uint8_t PARAM8_SIZE = 18; // Number of param bytes (SetOption)
#ifndef _TASMOTA_TYPES_H_
#define _TASMOTA_TYPES_H_
// Bitfield to be used for any SetOption0 .. SetOption31 persistent single bit
typedef union { // Restricted by MISRA-C Rule 18.4 but so useful...
@ -490,6 +488,7 @@ typedef union {
const uint32_t settings_text_size = 699; // Settings->text_pool[size] = Settings->display_model (2D2) - Settings->text_pool (017)
const uint8_t MAX_TUYA_FUNCTIONS = 16;
const uint8_t PARAM8_SIZE = 18; // Number of param bytes (SetOption)
typedef struct {
uint16_t cfg_holder; // 000 v6 header
@ -867,70 +866,6 @@ typedef struct {
static_assert(sizeof(TSettings) == 4096, "TSettings Size is not correct");
typedef struct {
uint16_t valid; // 280 (RTC memory offset 100 - sizeof(RTCRBT))
uint8_t fast_reboot_count; // 282
uint8_t free_003[1]; // 283
} TRtcReboot;
TRtcReboot RtcReboot;
#ifdef ESP32
RTC_NOINIT_ATTR TRtcReboot RtcDataReboot;
#endif // ESP32
typedef struct {
uint16_t valid; // 290 (RTC memory offset 100)
uint8_t oswatch_blocked_loop; // 292
uint8_t ota_loader; // 293
uint32_t energy_kWhtoday; // 294
uint32_t energy_kWhtotal; // 298
volatile uint32_t pulse_counter[MAX_COUNTERS]; // 29C - See #9521 why volatile
power_t power; // 2AC
EnergyUsage energy_usage; // 2B0
uint32_t nextwakeup; // 2C8
uint32_t baudrate; // 2CC
uint32_t ultradeepsleep; // 2D0
uint16_t deepsleep_slip; // 2D4
uint8_t improv_state; // 2D6
uint8_t free_2d7[1]; // 2D7
int32_t energy_kWhtoday_ph[3]; // 2D8
int32_t energy_kWhtotal_ph[3]; // 2E4
int32_t energy_kWhexport_ph[3]; // 2F0
uint8_t free_2fc[4]; // 2FC
} TRtcSettings;
TRtcSettings RtcSettings;
#ifdef ESP32
RTC_NOINIT_ATTR TRtcSettings RtcDataSettings;
#endif // ESP32
struct TIME_T {
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t day_of_week; // sunday is day 1
uint8_t day_of_month;
uint8_t month;
char name_of_month[4];
uint16_t day_of_year;
uint16_t year;
uint32_t days;
uint32_t valid;
} RtcTime;
struct XDRVMAILBOX {
bool grpflg;
bool usridx;
uint16_t command_code;
uint32_t index;
uint32_t data_len;
int32_t payload;
char *topic;
char *data;
char *command;
} XdrvMailbox;
typedef union { // Restricted by MISRA-C Rule 18.4 but so useful...
uint16_t data; // Allow bit manipulation
struct {
@ -974,4 +909,4 @@ typedef union {
#endif
#endif
#endif // _SETTINGS_H_
#endif // _TASMOTA_TYPES_H_

View File

@ -20,6 +20,6 @@
#ifndef _TASMOTA_VERSION_H_
#define _TASMOTA_VERSION_H_
const uint32_t VERSION = 0x0B010003; // 11.1.0.3
const uint32_t VERSION = 0x0B010004; // 11.1.0.4
#endif // _TASMOTA_VERSION_H_

View File

@ -21,16 +21,16 @@
#ifndef ESP32_STAGE // ESP32 Stage has no core_version.h file. Disable include via PlatformIO Option
#include <core_version.h> // Arduino_Esp8266 version information (ARDUINO_ESP8266_RELEASE and ARDUINO_ESP8266_RELEASE_2_7_1)
#endif // ESP32_STAGE
#include "tasmota_compat.h"
#include "tasmota_version.h" // Tasmota version information
#include "tasmota.h" // Enumeration used in my_user_config.h
#include "include/tasmota_compat.h"
#include "include/tasmota_version.h" // Tasmota version information
#include "include/tasmota.h" // Enumeration used in my_user_config.h
#include "my_user_config.h" // Fixed user configurable options
#ifdef USE_TLS
#include <t_bearssl.h> // We need to include before "tasmota_globals.h" to take precedence over the BearSSL version in Arduino
#endif // USE_TLS
#include "tasmota_globals.h" // Function prototypes and global configuration
#include "i18n.h" // Language support configured by my_user_config.h
#include "tasmota_template.h" // Hardware configuration
#include "include/tasmota_globals.h" // Function prototypes and global configuration
#include "include/i18n.h" // Language support configured by my_user_config.h
#include "include/tasmota_template.h" // Hardware configuration
// Libraries
#include <ESP8266HTTPClient.h> // Ota
@ -83,7 +83,7 @@
#endif // USE_UFILESYS
// Structs
#include "settings.h"
#include "include/tasmota_types.h"
#ifdef CONFIG_IDF_TARGET_ESP32
#include "soc/efuse_reg.h"
@ -95,6 +95,70 @@
const uint32_t VERSION_MARKER[] PROGMEM = { 0x5AA55AA5, 0xFFFFFFFF, 0xA55AA55A };
typedef struct {
uint16_t valid; // 280 (RTC memory offset 100 - sizeof(RTCRBT))
uint8_t fast_reboot_count; // 282
uint8_t free_003[1]; // 283
} TRtcReboot;
TRtcReboot RtcReboot;
#ifdef ESP32
RTC_NOINIT_ATTR TRtcReboot RtcDataReboot;
#endif // ESP32
typedef struct {
uint16_t valid; // 290 (RTC memory offset 100)
uint8_t oswatch_blocked_loop; // 292
uint8_t ota_loader; // 293
uint32_t energy_kWhtoday; // 294
uint32_t energy_kWhtotal; // 298
volatile uint32_t pulse_counter[MAX_COUNTERS]; // 29C - See #9521 why volatile
power_t power; // 2AC
EnergyUsage energy_usage; // 2B0
uint32_t nextwakeup; // 2C8
uint32_t baudrate; // 2CC
uint32_t ultradeepsleep; // 2D0
uint16_t deepsleep_slip; // 2D4
uint8_t improv_state; // 2D6
uint8_t free_2d7[1]; // 2D7
int32_t energy_kWhtoday_ph[3]; // 2D8
int32_t energy_kWhtotal_ph[3]; // 2E4
int32_t energy_kWhexport_ph[3]; // 2F0
uint8_t free_2fc[4]; // 2FC
} TRtcSettings;
TRtcSettings RtcSettings;
#ifdef ESP32
RTC_NOINIT_ATTR TRtcSettings RtcDataSettings;
#endif // ESP32
struct TIME_T {
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t day_of_week; // sunday is day 1
uint8_t day_of_month;
uint8_t month;
char name_of_month[4];
uint16_t day_of_year;
uint16_t year;
uint32_t days;
uint32_t valid;
} RtcTime;
struct XDRVMAILBOX {
bool grpflg;
bool usridx;
uint16_t command_code;
uint32_t index;
uint32_t data_len;
int32_t payload;
char *topic;
char *data;
char *command;
} XdrvMailbox;
WiFiUDP PortUdp; // UDP Syslog and Alexa
#ifdef ESP32

View File

View File

@ -1,6 +1,6 @@
#ifdef ESP8266
#ifdef USE_SENDMAIL
#include "sendemail_ESP8266.h"
#include "include/sendemail_ESP8266.h"
// enable serial debugging
//#define DEBUG_EMAIL_PORT

View File

@ -1,430 +1,430 @@
/*
support_flash_log.ino - log to flash support for Sonoff-Tasmota
Copyright (C) 2021 Theo Arends & Christian Baars
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------------------------
Version Date Action Description
--------------------------------------------------------------------------------------------
---
1.0.0.0 20190923 started - further development by Christian Baars - https://github.com/Staars/Sonoff-Tasmota
forked - from arendst/tasmota - https://github.com/arendst/Sonoff-Tasmota
base - code base from arendst and - written from scratch
*/
/********************************************************************************************\
| * Generic helper class to log arbitrary data to the OTA-partition
| * Working principle: Add preferrable small chunks of data to the sector buffer, which will
| * be written to FLASH when full automatically. The next sector will be
| * erased and is the anchor point for downloading and state configuration
| * after reboot.
\*********************************************************************************************/
#ifdef USE_FLOG
#ifdef ESP8266
class FLOG
#define MAGIC_WORD_FL 0x464c //F, L
{
struct header_t{
uint16_t magic_word; // FL
uint16_t padding; // leave something for the future
uint32_t physical_start_sector:10; //first used sector of the current FLOG
uint32_t number:10; // number of this sector, starting with 0 for the first sector
uint32_t buf_pointer:12; //internal pointer to the next free position in the buffer = first empty byte when reading
}; // should be 4-byte-aligned
private:
void _readSector(uint8_t one_sector);
void _eraseSector(uint8_t one_sector);
void _writeSector(uint8_t one_sector);
void _clearBuffer(void);
void _searchSaves(void);
void _findFirstErasedSector(void);
void _showBuffer(void);
void _initBuffer(void);
void _saveBufferToSector(void);
header_t _saved_header;
public:
uint32_t size; // size of OTA-partition
uint32_t start; // start position of OTA-partition in bytes
uint32_t end; // end position of OTA-partition in bytes
uint16_t num_sectors; // calculated number of sectors with a size of 4096 bytes
uint16_t first_erased_sector; // this will be our new start
uint16_t current_sector; // always point to next sector, where data from the buffer will be written to
uint16_t bytes_left; // byte per buffer (of sector size 4096 bytes - 8 byte header size)
uint16_t sectors_left; // number of saved sectors for download
uint8_t mode = 0; // 0 - write once on all sectors, then stop, 1 - write infinitely through the sectors
bool found_saved_data = false; // possible saved data has been found
bool ready = false; // the FLOG is initialized
bool running_download = false; // a download operation is running
bool recording = false; // ready for recording
union sector_t{
uint32_t dword_buffer[FLASH_SECTOR_SIZE/4];
uint8_t byte_buffer[FLASH_SECTOR_SIZE];
header_t header; // should be 4-byte-aligned
} sector; // the global buffer of 4096 bytes, used for reading and writing
void init(void);
void addToBuffer(uint8_t src[], uint32_t size);
void startRecording(bool append);
void stopRecording(void);
typedef void (*CallbackNoArgs) (); // simple typedef for a callback
typedef void (*CallbackWithArgs) (uint8_t *_record); // typedef for a callback with one argument
void startDownload(size_t size, CallbackNoArgs sendHeader, CallbackWithArgs sendRecord, CallbackNoArgs sendFooter);
};
extern "C" uint32_t _SPIFFS_start; // we make shure later, that only one of the two is really used ...
extern "C" uint32_t _FS_start; // ... depending on core-sdk-version
/**
* @brief Will examine the start and end of the OTA-partition. Then the sector size will be computed, saved data should be found and the initial state will be configured.
*/
void FLOG::init(void)
{
DEBUG_SENSOR_LOG(PSTR("FLOG: init ..."));
size = ESP.getSketchSize();
// round one sector up
start = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
end = (uint32_t)&_FS_start - 0x40200000;
num_sectors = (end - start)/FLASH_SECTOR_SIZE;
DEBUG_SENSOR_LOG(PSTR("FLOG: size: 0x%lx, start: 0x%lx, end: 0x%lx, num_sectors(dec): %lu"), size, start, end, num_sectors );
_findFirstErasedSector();
if(first_erased_sector == 0xffff){
_eraseSector(0);
first_erased_sector = 0; // start with sector 0, could be first run or after crash
}
_searchSaves();
_initBuffer();
ready = true;
}
/********************************************************************************************\
| *
| * private helper functions
| *
\*********************************************************************************************/
/**
* @brief Read a sector into the global buffer
*
* @param one_sector as an uint8_t
*/
void FLOG::_readSector(uint8_t one_sector){
DEBUG_SENSOR_LOG(PSTR("FLOG: read sector number: %u" ), one_sector);
ESP.flashRead(start+(one_sector * FLASH_SECTOR_SIZE),(uint32_t *)&sector.dword_buffer, FLASH_SECTOR_SIZE);
}
/**
* @brief Erase the given sector og the OTA-partition
*
* @param one_sector as an uint8_t
*/
void FLOG::_eraseSector(uint8_t one_sector){ // Erase sector of FLOG/OTA
DEBUG_SENSOR_LOG(PSTR("FLOG: erasing sector number: %u" ), one_sector);
ESP.flashEraseSector((start/FLASH_SECTOR_SIZE)+one_sector);
}
/**
* @brief Write the global buffer to the given sector
*
* @param one_sector as an uint8_t
*/
void FLOG::_writeSector(uint8_t one_sector){ // Write sector of FLOG/OTA
DEBUG_SENSOR_LOG(PSTR("FLOG: write buffer to sector number: %u" ), one_sector);
ESP.flashWrite(start+(one_sector * FLASH_SECTOR_SIZE),(uint32_t *)&sector.dword_buffer, FLASH_SECTOR_SIZE);
}
/**
* @brief Clear the global buffer, but leave the header intact
*
*/
void FLOG::_clearBuffer(){ //not the header
for (uint32_t i = sizeof(sector.header)/4; i<(sizeof(sector.dword_buffer)/4); i++){
sector.dword_buffer[i] = 0;
}
sector.header.buf_pointer = sizeof(sector.header);
// _showBuffer();
}
/**
* @brief Write global buffer to FLASH and set the current sector to the next valid position, maybe to 0
*
*/
void FLOG::_saveBufferToSector(){ // save buffer to already erased(!) sector, erase next sector, clear buffer, increment number
DEBUG_SENSOR_LOG(PSTR("FLOG: write buffer to current sector: %u" ),current_sector);
_writeSector(current_sector);
if(current_sector == num_sectors){ // 1 MB means ~110 sectors in OTA-partition, if we reach this, start a again
current_sector = 0;
}
else{
current_sector++;
}
_eraseSector(current_sector); // we always erase the next sector, to find out were we are after restart
_clearBuffer();
sector.header.number++;
DEBUG_SENSOR_LOG(PSTR("FLOG: new sector header number: %u" ),sector.header.number);
}
/**
* @brief Typically after restart find the first erased sector as a starting point for further operations
*
*/
void FLOG::_findFirstErasedSector(){
for (uint32_t i = 0; i<num_sectors; i++){
bool success = true;
DEBUG_SENSOR_LOG(PSTR("FLOG: read sector: %u"), i);
_readSector(i);
for (uint32_t j = 0; j<(sizeof(sector.dword_buffer)/4); j++){
if(sector.dword_buffer[j]!=0xffffffff){
// DEBUG_SENSOR_LOG(PSTR("FLOG: buffer_dword: %u"), sector.dword_buffer[j]);
success = false;
}
}
if(success){
first_erased_sector = i; // save this for the whole next write operation
sector.header.physical_start_sector = i; // save to header for every sector
current_sector = i; // this is our actual sector to write to
DEBUG_SENSOR_LOG(PSTR("FLOG: first erased sector: %u, now init ..."), first_erased_sector);
return;
}
}
DEBUG_SENSOR_LOG(PSTR("FLOG: no erased sector found"));
first_erased_sector = 0xffff; // this will not happen unless we have 256 MByte FLASH
}
/**
* @brief Look at the sector before the first erased sector to check, if there could be saved data
*
*/
void FLOG::_searchSaves(void){
//check if old Data is present
found_saved_data = false;
uint32_t s;
if(first_erased_sector==0){
DEBUG_SENSOR_LOG(PSTR("FLOG: sector 0 was erased before, examine sector: %u"), num_sectors);
s = num_sectors; //count back to the highest possible sector
}
else{
s = first_erased_sector-1;
DEBUG_SENSOR_LOG(PSTR("FLOG: examine sector: %u"), s);
}
_readSector(s); //read the sector before the first erased sector
if(sector.header.magic_word!=MAGIC_WORD_FL){
DEBUG_SENSOR_LOG(PSTR("FLOG: wrong magic number, no saved data found"));
return;
}
sectors_left = sector.header.number + 1; // this might be wrong, but this less important
_saved_header = sector.header; // back this up for appending mode
s = sector.header.physical_start_sector;
DEBUG_SENSOR_LOG(PSTR("FLOG: will check pysical start sector: %u"), s);
_readSector(s); //read the physical_start_sector
_showBuffer();
if(sector.header.magic_word!=MAGIC_WORD_FL){ //F, L
DEBUG_SENSOR_LOG(PSTR("FLOG: wrong magic number, no saved data found"));
sectors_left = 0;
return;
}
if(sector.header.number==0){ //physical_start_sector should have number 0
DEBUG_SENSOR_LOG(PSTR("FLOG: possible saved data found"));
found_saved_data = true; // TODO: this is only a very rough check and should be completed later
}
else{
DEBUG_SENSOR_LOG(PSTR("FLOG: number: %u should be 0"), sector.header.number);
sectors_left = 0;
}
}
/**
* @brief Start with a new buffer to be able to start a write session
*
*/
void FLOG::_initBuffer(void){
if(!found_saved_data){ // we must re-init this, because the buffer is in an undefined state
sector.header.physical_start_sector = (uint16_t)first_erased_sector;
}
DEBUG_SENSOR_LOG(PSTR("FLOG: init header"));
sector.header.magic_word = MAGIC_WORD_FL; //F, L
sector.header.number = 0;
sector.header.buf_pointer = (uint16_t)sizeof(sector.header);
current_sector = first_erased_sector;
ready = true;
_clearBuffer();
}
/**
* @brief - a pure debug function
*
*/
void FLOG::_showBuffer(void){
DEBUG_SENSOR_LOG(PSTR("FLOG: Header: %c %c"), sector.byte_buffer[0],sector.byte_buffer[1]);
DEBUG_SENSOR_LOG(PSTR("FLOG: V_Start_sector: %u, sector number: %u, pointer: %u "), sector.header.physical_start_sector, sector.header.number, sector.header.buf_pointer);
uint32_t j = 0;
for (uint32_t i = sector.header.buf_pointer-16; i<(sizeof(sector.byte_buffer)); i+=8){
// DEBUG_SENSOR_LOG(PSTR("FLOG: buffer: %c %c %c %c %c %c %c %c "), sector.byte_buffer[i], sector.byte_buffer[i+1], sector.byte_buffer[i+2], sector.byte_buffer[i+3], sector.byte_buffer[i+4], sector.byte_buffer[i+5], sector.byte_buffer[i+6], sector.byte_buffer[i+7]);
DEBUG_SENSOR_LOG(PSTR("FLOG: buffer: %u %u %u %u %u %u %u %u "), sector.byte_buffer[i], sector.byte_buffer[i+1], sector.byte_buffer[i+2], sector.byte_buffer[i+3], sector.byte_buffer[i+4], sector.byte_buffer[i+5], sector.byte_buffer[i+6], sector.byte_buffer[i+7]);
j++;
if(j>3){
break;
}
}
}
/**
* @brief pass a data entry/record as uint8_t array with its size
*
* @param src uint8_t array
* @param size uint32_t size of the array
*/
void FLOG::addToBuffer(uint8_t src[], uint32_t size){
if(mode == 0){
if(sector.header.number == num_sectors && !ready){
return; // we ignore additional calls and are done, TODO: maybe use meaningful return values
}
}
if((FLASH_SECTOR_SIZE-sector.header.buf_pointer-sizeof(sector.header))>size){
// DEBUG_SENSOR_LOG(PSTR("FLOG: enough space left in buffer: %u"), FLASH_SECTOR_SIZE - sector.header.buf_pointer - sizeof(sector.header));
// DEBUG_SENSOR_LOG(PSTR("FLOG: current buf_pointer: %u, size of added: %u"), sector.header.buf_pointer, size);
memcpy(sector.byte_buffer + sector.header.buf_pointer, src, size);
sector.header.buf_pointer+=size; // this is the next free spot
// DEBUG_SENSOR_LOG(PSTR("FLOG: current buf_pointer: %u"), sector.header.buf_pointer);
}
else{
DEBUG_SENSOR_LOG(PSTR("FLOG: save buffer to flash sector: %u"), current_sector);
DEBUG_SENSOR_LOG(PSTR("FLOG: current buf_pointer: %u"), sector.header.buf_pointer);
_saveBufferToSector();
sectors_left++;
// but now save the data to the fresh buffer
if((FLASH_SECTOR_SIZE-sector.header.buf_pointer-sizeof(sector.header))>size){
memcpy(sector.byte_buffer + sector.header.buf_pointer, src, size);
sector.header.buf_pointer+=size; // this is the next free spot
}
}
}
/**
* @brief shows that it is ready to accept recording
*
* @param append - if true append to current log, else start a new log
*/
void FLOG::startRecording(bool append){
if(recording){
DEBUG_SENSOR_LOG(PSTR("FLOG: already recording"));
return;
}
recording = true;
DEBUG_SENSOR_LOG(PSTR("FLOG: start recording"));
_initBuffer();
if(!found_saved_data) {
append = false; // nothing to append to, we silently start a new log
}
if(append){
sector.header.number = _saved_header.number+1; // continue with the next number
sector.header.physical_start_sector = _saved_header.physical_start_sector; // keep the old start sector
}
else{ //new log, old data is lost
sector.header.physical_start_sector = (uint16_t)first_erased_sector;
found_saved_data = false;
sectors_left = 0;
}
}
/**
* @brief stop recording including saving current buffer to FLASH
*
*/
void FLOG::stopRecording(void){
_saveBufferToSector();
_findFirstErasedSector();
_searchSaves();
_initBuffer();
recording = false;
found_saved_data = true;
}
/**
* @brief Will start a downloads, needs the correct implementation of 3 callback functions
*
* @param size: size of the data entry/record in bytes, i.e. sizeof(myStruct)
* @param sendHeader: should implement at least something like:
* @example Webserver->setContentLength(CONTENT_LENGTH_UNKNOWN); // This is very likely unknown!!
* Webserver->sendHeader(F("Content-Disposition"), F("attachment; filename=myfile.txt"));
* @param sendRecord: will receive the memory address as "uint8_t* addr" and should consume the current entry/record
* @example myStruct_t *entry = (myStruct_t*)addr;
* Then make useful Strings and send it, i.e.: Webserver->sendContent_P(myString);
* @param sendFooter: finish the download, should implement at least:
* @example Webserver->sendContent("");
*/
void FLOG::startDownload(size_t size, CallbackNoArgs sendHeader, CallbackWithArgs sendRecord, CallbackNoArgs sendFooter){
_readSector(sector.header.physical_start_sector);
uint32_t next_sector = sector.header.physical_start_sector;
bytes_left = sector.header.buf_pointer - sizeof(sector.header);
DEBUG_SENSOR_LOG(PSTR("FLOG: create file for download, will process %u bytes"), bytes_left);
running_download = true;
// Callback 1: Create the header incl. file name, content length (probably unknown!!) and additional header stuff
sendHeader();
while(sectors_left){
DEBUG_SENSOR_LOG(PSTR("FLOG: next sector: %u"), next_sector);
//initially we have the first sector already loaded, so we do it at the bottom
uint32_t k = sizeof(sector.header);
while(bytes_left){
// DEBUG_SENSOR_LOG(PSTR("FLOG: DL %u %u"), Flog->sector.byte_buffer[k],Flog->sector.byte_buffer[k+1]);
uint8_t *_record_start = (uint8_t*)&sector.byte_buffer[k]; // this is basically the start address of the current record/entry of the Log
// Callback 2: send the pointer for consuming the next record/entry and doing something useful to create a file
sendRecord(_record_start);
if(k%128 == 0){ // give control to the system every x iteration, TODO: This will fail, when record/entry-size is not 8
// DEBUG_SENSOR_LOG(PSTR("FLOG: now loop(), %u bytes left"), Flog->bytes_left);
OsWatchLoop();
delay(TasmotaGlobal.sleep);
}
k+=size;
if(bytes_left>7){
bytes_left-=size;
}
else{
bytes_left = 0;
DEBUG_SENSOR_LOG(PSTR("FLOG: Flog->bytes_left not dividable by 8 ??????"));
}
}
next_sector++;
if(next_sector>num_sectors){
next_sector = 0;
}
sectors_left--;
_readSector(next_sector);
bytes_left = sector.header.buf_pointer - sizeof(sector.header);
OsWatchLoop();
delay(TasmotaGlobal.sleep);
}
running_download = false;
// Callback 3: create a footer or simply finish the download with an empty payload
sendFooter();
// refresh settings for another download
_searchSaves();
_initBuffer();
}
#endif // ESP8266
/*
support_flash_log.ino - log to flash support for Sonoff-Tasmota
Copyright (C) 2021 Theo Arends & Christian Baars
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------------------------
Version Date Action Description
--------------------------------------------------------------------------------------------
---
1.0.0.0 20190923 started - further development by Christian Baars - https://github.com/Staars/Sonoff-Tasmota
forked - from arendst/tasmota - https://github.com/arendst/Sonoff-Tasmota
base - code base from arendst and - written from scratch
*/
/********************************************************************************************\
| * Generic helper class to log arbitrary data to the OTA-partition
| * Working principle: Add preferrable small chunks of data to the sector buffer, which will
| * be written to FLASH when full automatically. The next sector will be
| * erased and is the anchor point for downloading and state configuration
| * after reboot.
\*********************************************************************************************/
#ifdef USE_FLOG
#ifdef ESP8266
class FLOG
#define MAGIC_WORD_FL 0x464c //F, L
{
struct header_t{
uint16_t magic_word; // FL
uint16_t padding; // leave something for the future
uint32_t physical_start_sector:10; //first used sector of the current FLOG
uint32_t number:10; // number of this sector, starting with 0 for the first sector
uint32_t buf_pointer:12; //internal pointer to the next free position in the buffer = first empty byte when reading
}; // should be 4-byte-aligned
private:
void _readSector(uint8_t one_sector);
void _eraseSector(uint8_t one_sector);
void _writeSector(uint8_t one_sector);
void _clearBuffer(void);
void _searchSaves(void);
void _findFirstErasedSector(void);
void _showBuffer(void);
void _initBuffer(void);
void _saveBufferToSector(void);
header_t _saved_header;
public:
uint32_t size; // size of OTA-partition
uint32_t start; // start position of OTA-partition in bytes
uint32_t end; // end position of OTA-partition in bytes
uint16_t num_sectors; // calculated number of sectors with a size of 4096 bytes
uint16_t first_erased_sector; // this will be our new start
uint16_t current_sector; // always point to next sector, where data from the buffer will be written to
uint16_t bytes_left; // byte per buffer (of sector size 4096 bytes - 8 byte header size)
uint16_t sectors_left; // number of saved sectors for download
uint8_t mode = 0; // 0 - write once on all sectors, then stop, 1 - write infinitely through the sectors
bool found_saved_data = false; // possible saved data has been found
bool ready = false; // the FLOG is initialized
bool running_download = false; // a download operation is running
bool recording = false; // ready for recording
union sector_t{
uint32_t dword_buffer[FLASH_SECTOR_SIZE/4];
uint8_t byte_buffer[FLASH_SECTOR_SIZE];
header_t header; // should be 4-byte-aligned
} sector; // the global buffer of 4096 bytes, used for reading and writing
void init(void);
void addToBuffer(uint8_t src[], uint32_t size);
void startRecording(bool append);
void stopRecording(void);
typedef void (*CallbackNoArgs) (); // simple typedef for a callback
typedef void (*CallbackWithArgs) (uint8_t *_record); // typedef for a callback with one argument
void startDownload(size_t size, CallbackNoArgs sendHeader, CallbackWithArgs sendRecord, CallbackNoArgs sendFooter);
};
extern "C" uint32_t _SPIFFS_start; // we make shure later, that only one of the two is really used ...
extern "C" uint32_t _FS_start; // ... depending on core-sdk-version
/**
* @brief Will examine the start and end of the OTA-partition. Then the sector size will be computed, saved data should be found and the initial state will be configured.
*/
void FLOG::init(void)
{
DEBUG_SENSOR_LOG(PSTR("FLOG: init ..."));
size = ESP.getSketchSize();
// round one sector up
start = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
end = (uint32_t)&_FS_start - 0x40200000;
num_sectors = (end - start)/FLASH_SECTOR_SIZE;
DEBUG_SENSOR_LOG(PSTR("FLOG: size: 0x%lx, start: 0x%lx, end: 0x%lx, num_sectors(dec): %lu"), size, start, end, num_sectors );
_findFirstErasedSector();
if(first_erased_sector == 0xffff){
_eraseSector(0);
first_erased_sector = 0; // start with sector 0, could be first run or after crash
}
_searchSaves();
_initBuffer();
ready = true;
}
/********************************************************************************************\
| *
| * private helper functions
| *
\*********************************************************************************************/
/**
* @brief Read a sector into the global buffer
*
* @param one_sector as an uint8_t
*/
void FLOG::_readSector(uint8_t one_sector){
DEBUG_SENSOR_LOG(PSTR("FLOG: read sector number: %u" ), one_sector);
ESP.flashRead(start+(one_sector * FLASH_SECTOR_SIZE),(uint32_t *)&sector.dword_buffer, FLASH_SECTOR_SIZE);
}
/**
* @brief Erase the given sector og the OTA-partition
*
* @param one_sector as an uint8_t
*/
void FLOG::_eraseSector(uint8_t one_sector){ // Erase sector of FLOG/OTA
DEBUG_SENSOR_LOG(PSTR("FLOG: erasing sector number: %u" ), one_sector);
ESP.flashEraseSector((start/FLASH_SECTOR_SIZE)+one_sector);
}
/**
* @brief Write the global buffer to the given sector
*
* @param one_sector as an uint8_t
*/
void FLOG::_writeSector(uint8_t one_sector){ // Write sector of FLOG/OTA
DEBUG_SENSOR_LOG(PSTR("FLOG: write buffer to sector number: %u" ), one_sector);
ESP.flashWrite(start+(one_sector * FLASH_SECTOR_SIZE),(uint32_t *)&sector.dword_buffer, FLASH_SECTOR_SIZE);
}
/**
* @brief Clear the global buffer, but leave the header intact
*
*/
void FLOG::_clearBuffer(){ //not the header
for (uint32_t i = sizeof(sector.header)/4; i<(sizeof(sector.dword_buffer)/4); i++){
sector.dword_buffer[i] = 0;
}
sector.header.buf_pointer = sizeof(sector.header);
// _showBuffer();
}
/**
* @brief Write global buffer to FLASH and set the current sector to the next valid position, maybe to 0
*
*/
void FLOG::_saveBufferToSector(){ // save buffer to already erased(!) sector, erase next sector, clear buffer, increment number
DEBUG_SENSOR_LOG(PSTR("FLOG: write buffer to current sector: %u" ),current_sector);
_writeSector(current_sector);
if(current_sector == num_sectors){ // 1 MB means ~110 sectors in OTA-partition, if we reach this, start a again
current_sector = 0;
}
else{
current_sector++;
}
_eraseSector(current_sector); // we always erase the next sector, to find out were we are after restart
_clearBuffer();
sector.header.number++;
DEBUG_SENSOR_LOG(PSTR("FLOG: new sector header number: %u" ),sector.header.number);
}
/**
* @brief Typically after restart find the first erased sector as a starting point for further operations
*
*/
void FLOG::_findFirstErasedSector(){
for (uint32_t i = 0; i<num_sectors; i++){
bool success = true;
DEBUG_SENSOR_LOG(PSTR("FLOG: read sector: %u"), i);
_readSector(i);
for (uint32_t j = 0; j<(sizeof(sector.dword_buffer)/4); j++){
if(sector.dword_buffer[j]!=0xffffffff){
// DEBUG_SENSOR_LOG(PSTR("FLOG: buffer_dword: %u"), sector.dword_buffer[j]);
success = false;
}
}
if(success){
first_erased_sector = i; // save this for the whole next write operation
sector.header.physical_start_sector = i; // save to header for every sector
current_sector = i; // this is our actual sector to write to
DEBUG_SENSOR_LOG(PSTR("FLOG: first erased sector: %u, now init ..."), first_erased_sector);
return;
}
}
DEBUG_SENSOR_LOG(PSTR("FLOG: no erased sector found"));
first_erased_sector = 0xffff; // this will not happen unless we have 256 MByte FLASH
}
/**
* @brief Look at the sector before the first erased sector to check, if there could be saved data
*
*/
void FLOG::_searchSaves(void){
//check if old Data is present
found_saved_data = false;
uint32_t s;
if(first_erased_sector==0){
DEBUG_SENSOR_LOG(PSTR("FLOG: sector 0 was erased before, examine sector: %u"), num_sectors);
s = num_sectors; //count back to the highest possible sector
}
else{
s = first_erased_sector-1;
DEBUG_SENSOR_LOG(PSTR("FLOG: examine sector: %u"), s);
}
_readSector(s); //read the sector before the first erased sector
if(sector.header.magic_word!=MAGIC_WORD_FL){
DEBUG_SENSOR_LOG(PSTR("FLOG: wrong magic number, no saved data found"));
return;
}
sectors_left = sector.header.number + 1; // this might be wrong, but this less important
_saved_header = sector.header; // back this up for appending mode
s = sector.header.physical_start_sector;
DEBUG_SENSOR_LOG(PSTR("FLOG: will check pysical start sector: %u"), s);
_readSector(s); //read the physical_start_sector
_showBuffer();
if(sector.header.magic_word!=MAGIC_WORD_FL){ //F, L
DEBUG_SENSOR_LOG(PSTR("FLOG: wrong magic number, no saved data found"));
sectors_left = 0;
return;
}
if(sector.header.number==0){ //physical_start_sector should have number 0
DEBUG_SENSOR_LOG(PSTR("FLOG: possible saved data found"));
found_saved_data = true; // TODO: this is only a very rough check and should be completed later
}
else{
DEBUG_SENSOR_LOG(PSTR("FLOG: number: %u should be 0"), sector.header.number);
sectors_left = 0;
}
}
/**
* @brief Start with a new buffer to be able to start a write session
*
*/
void FLOG::_initBuffer(void){
if(!found_saved_data){ // we must re-init this, because the buffer is in an undefined state
sector.header.physical_start_sector = (uint16_t)first_erased_sector;
}
DEBUG_SENSOR_LOG(PSTR("FLOG: init header"));
sector.header.magic_word = MAGIC_WORD_FL; //F, L
sector.header.number = 0;
sector.header.buf_pointer = (uint16_t)sizeof(sector.header);
current_sector = first_erased_sector;
ready = true;
_clearBuffer();
}
/**
* @brief - a pure debug function
*
*/
void FLOG::_showBuffer(void){
DEBUG_SENSOR_LOG(PSTR("FLOG: Header: %c %c"), sector.byte_buffer[0],sector.byte_buffer[1]);
DEBUG_SENSOR_LOG(PSTR("FLOG: V_Start_sector: %u, sector number: %u, pointer: %u "), sector.header.physical_start_sector, sector.header.number, sector.header.buf_pointer);
uint32_t j = 0;
for (uint32_t i = sector.header.buf_pointer-16; i<(sizeof(sector.byte_buffer)); i+=8){
// DEBUG_SENSOR_LOG(PSTR("FLOG: buffer: %c %c %c %c %c %c %c %c "), sector.byte_buffer[i], sector.byte_buffer[i+1], sector.byte_buffer[i+2], sector.byte_buffer[i+3], sector.byte_buffer[i+4], sector.byte_buffer[i+5], sector.byte_buffer[i+6], sector.byte_buffer[i+7]);
DEBUG_SENSOR_LOG(PSTR("FLOG: buffer: %u %u %u %u %u %u %u %u "), sector.byte_buffer[i], sector.byte_buffer[i+1], sector.byte_buffer[i+2], sector.byte_buffer[i+3], sector.byte_buffer[i+4], sector.byte_buffer[i+5], sector.byte_buffer[i+6], sector.byte_buffer[i+7]);
j++;
if(j>3){
break;
}
}
}
/**
* @brief pass a data entry/record as uint8_t array with its size
*
* @param src uint8_t array
* @param size uint32_t size of the array
*/
void FLOG::addToBuffer(uint8_t src[], uint32_t size){
if(mode == 0){
if(sector.header.number == num_sectors && !ready){
return; // we ignore additional calls and are done, TODO: maybe use meaningful return values
}
}
if((FLASH_SECTOR_SIZE-sector.header.buf_pointer-sizeof(sector.header))>size){
// DEBUG_SENSOR_LOG(PSTR("FLOG: enough space left in buffer: %u"), FLASH_SECTOR_SIZE - sector.header.buf_pointer - sizeof(sector.header));
// DEBUG_SENSOR_LOG(PSTR("FLOG: current buf_pointer: %u, size of added: %u"), sector.header.buf_pointer, size);
memcpy(sector.byte_buffer + sector.header.buf_pointer, src, size);
sector.header.buf_pointer+=size; // this is the next free spot
// DEBUG_SENSOR_LOG(PSTR("FLOG: current buf_pointer: %u"), sector.header.buf_pointer);
}
else{
DEBUG_SENSOR_LOG(PSTR("FLOG: save buffer to flash sector: %u"), current_sector);
DEBUG_SENSOR_LOG(PSTR("FLOG: current buf_pointer: %u"), sector.header.buf_pointer);
_saveBufferToSector();
sectors_left++;
// but now save the data to the fresh buffer
if((FLASH_SECTOR_SIZE-sector.header.buf_pointer-sizeof(sector.header))>size){
memcpy(sector.byte_buffer + sector.header.buf_pointer, src, size);
sector.header.buf_pointer+=size; // this is the next free spot
}
}
}
/**
* @brief shows that it is ready to accept recording
*
* @param append - if true append to current log, else start a new log
*/
void FLOG::startRecording(bool append){
if(recording){
DEBUG_SENSOR_LOG(PSTR("FLOG: already recording"));
return;
}
recording = true;
DEBUG_SENSOR_LOG(PSTR("FLOG: start recording"));
_initBuffer();
if(!found_saved_data) {
append = false; // nothing to append to, we silently start a new log
}
if(append){
sector.header.number = _saved_header.number+1; // continue with the next number
sector.header.physical_start_sector = _saved_header.physical_start_sector; // keep the old start sector
}
else{ //new log, old data is lost
sector.header.physical_start_sector = (uint16_t)first_erased_sector;
found_saved_data = false;
sectors_left = 0;
}
}
/**
* @brief stop recording including saving current buffer to FLASH
*
*/
void FLOG::stopRecording(void){
_saveBufferToSector();
_findFirstErasedSector();
_searchSaves();
_initBuffer();
recording = false;
found_saved_data = true;
}
/**
* @brief Will start a downloads, needs the correct implementation of 3 callback functions
*
* @param size: size of the data entry/record in bytes, i.e. sizeof(myStruct)
* @param sendHeader: should implement at least something like:
* @example Webserver->setContentLength(CONTENT_LENGTH_UNKNOWN); // This is very likely unknown!!
* Webserver->sendHeader(F("Content-Disposition"), F("attachment; filename=myfile.txt"));
* @param sendRecord: will receive the memory address as "uint8_t* addr" and should consume the current entry/record
* @example myStruct_t *entry = (myStruct_t*)addr;
* Then make useful Strings and send it, i.e.: Webserver->sendContent_P(myString);
* @param sendFooter: finish the download, should implement at least:
* @example Webserver->sendContent("");
*/
void FLOG::startDownload(size_t size, CallbackNoArgs sendHeader, CallbackWithArgs sendRecord, CallbackNoArgs sendFooter){
_readSector(sector.header.physical_start_sector);
uint32_t next_sector = sector.header.physical_start_sector;
bytes_left = sector.header.buf_pointer - sizeof(sector.header);
DEBUG_SENSOR_LOG(PSTR("FLOG: create file for download, will process %u bytes"), bytes_left);
running_download = true;
// Callback 1: Create the header incl. file name, content length (probably unknown!!) and additional header stuff
sendHeader();
while(sectors_left){
DEBUG_SENSOR_LOG(PSTR("FLOG: next sector: %u"), next_sector);
//initially we have the first sector already loaded, so we do it at the bottom
uint32_t k = sizeof(sector.header);
while(bytes_left){
// DEBUG_SENSOR_LOG(PSTR("FLOG: DL %u %u"), Flog->sector.byte_buffer[k],Flog->sector.byte_buffer[k+1]);
uint8_t *_record_start = (uint8_t*)&sector.byte_buffer[k]; // this is basically the start address of the current record/entry of the Log
// Callback 2: send the pointer for consuming the next record/entry and doing something useful to create a file
sendRecord(_record_start);
if(k%128 == 0){ // give control to the system every x iteration, TODO: This will fail, when record/entry-size is not 8
// DEBUG_SENSOR_LOG(PSTR("FLOG: now loop(), %u bytes left"), Flog->bytes_left);
OsWatchLoop();
delay(TasmotaGlobal.sleep);
}
k+=size;
if(bytes_left>7){
bytes_left-=size;
}
else{
bytes_left = 0;
DEBUG_SENSOR_LOG(PSTR("FLOG: Flog->bytes_left not dividable by 8 ??????"));
}
}
next_sector++;
if(next_sector>num_sectors){
next_sector = 0;
}
sectors_left--;
_readSector(next_sector);
bytes_left = sector.header.buf_pointer - sizeof(sector.header);
OsWatchLoop();
delay(TasmotaGlobal.sleep);
}
running_download = false;
// Callback 3: create a footer or simply finish the download with an empty payload
sendFooter();
// refresh settings for another download
_searchSaves();
_initBuffer();
}
#endif // ESP8266
#endif // USE_FLOG

View File

@ -20,17 +20,17 @@
//
// Certificates are stored in flash (PROGMEM) to avoid consuming valuable RAM.
//
// To save space in flash, the Let's Encrypt and Amazon AWS certificates may be
// individually omitted if TLS is enabled, but the certificates are not used.
// This is typically the case when a locally generated root certificate is used
// To save space in flash, the Let's Encrypt and Amazon AWS certificates may be
// individually omitted if TLS is enabled, but the certificates are not used.
// This is typically the case when a locally generated root certificate is used
// for TLS authentication.
//
// To omit these certificates, define one or both of the
//
// To omit these certificates, define one or both of the
// OMIT_LETS_ENCRYPT_CERT and/or OMIT_AWS_CERT macros in user_config_override.h.
//
// To include a locally generated root certificate, define the
// INCLUDE_LOCAL_CERT macro in user_config_override.h.
//
//
// See the files tasmota/local_ca_data_sample.h and tasmota/local_ca_descriptor_sample.h
// in the Tasmota source for instructions for generating them from a local root
// certificate in .crt format.
@ -145,7 +145,7 @@ static const unsigned char PROGMEM AmazonRootCA1_RSA_E[] = {
#endif
#if defined(INCLUDE_LOCAL_CERT)
#include <local_ca_data.h>
#include <include/local_ca_data.h>
#endif
//
// ========== cumulative CA =================

Some files were not shown because too many files have changed in this diff Show More