Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partition Wizard can be loaded dynamically #19980

Merged
merged 4 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
- NeoPool sensor delta trigger (command ``NPTelePeriod``) (#19973)
- NeoPool store settings on unified file system (#19973)
- NeoPool command ``NPBoost`` (#19973)
- Partition Wizard can be loaded dynamically

### Breaking Changed

Expand Down
35 changes: 20 additions & 15 deletions lib/libesp32/berry/src/be_bytecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,29 +596,34 @@ void load_global_info(bvm *vm, void *fp)
be_global_release_space(vm);
}

bclosure* be_bytecode_load_from_fs(bvm *vm, void *fp)
{
int version = load_head(fp);
if (version == BYTECODE_VERSION) {
bclosure *cl = be_newclosure(vm, 0);
var_setclosure(vm->top, cl);
be_stackpush(vm);
load_global_info(vm, fp);
load_proto(vm, fp, &cl->proto, -1, version);
be_stackpop(vm, 2); /* pop the closure and list */
be_fclose(fp);
return cl;
}
bytecode_error(vm, be_pushfstring(vm,
"invalid bytecode version."));
return NULL;
}

bclosure* be_bytecode_load(bvm *vm, const char *filename)
{
void *fp = be_fopen(filename, "rb");
if (fp == NULL) {
bytecode_error(vm, be_pushfstring(vm,
"can not open file '%s'.", filename));
} else {
int version = load_head(fp);
if (version == BYTECODE_VERSION) {
bclosure *cl = be_newclosure(vm, 0);
var_setclosure(vm->top, cl);
be_stackpush(vm);
load_global_info(vm, fp);
load_proto(vm, fp, &cl->proto, -1, version);
be_stackpop(vm, 2); /* pop the closure and list */
be_fclose(fp);
return cl;
}
bytecode_error(vm, be_pushfstring(vm,
"invalid bytecode version '%s'.", filename));
return be_bytecode_load_from_fs(vm, fp);
}
bytecode_error(vm, be_pushfstring(vm,
"invalid bytecode file '%s'.", filename));
return NULL;
}

#endif /* BE_USE_BYTECODE_LOADER */
1 change: 1 addition & 0 deletions lib/libesp32/berry/src/be_bytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

void be_bytecode_save(bvm *vm, const char *filename, bproto *proto);
bclosure* be_bytecode_load(bvm *vm, const char *filename);
bclosure* be_bytecode_load_from_fs(bvm *vm, void *fp);
bbool be_bytecode_check(const char *path);

#endif
Binary file not shown.
2 changes: 2 additions & 0 deletions tasmota/my_user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,8 @@
// Note that only one cipher is enabled: ECDHE_RSA_WITH_AES_128_GCM_SHA256 which is very commonly used and highly secure
#define USE_BERRY_WEBCLIENT_USERAGENT "TasmotaClient" // default user-agent used, can be changed with `wc.set_useragent()`
#define USE_BERRY_WEBCLIENT_TIMEOUT 2000 // Default timeout in milliseconds
#define USE_BERRY_PARTITION_WIZARD // Add a button to dynamically load the Partion Wizard from a bec file online (+1.3KB Flash)
#define USE_BERRY_PARTITION_WIZARD_URL "http://ota.tasmota.com/tapp/partition_wizard.bec"
#define USE_BERRY_TCPSERVER // Enable TCP socket server (+0.6k)
// #define USE_BERRY_ULP // Enable ULP (Ultra Low Power) support (+4.9k)
// Berry crypto extensions below:
Expand Down
8 changes: 7 additions & 1 deletion tasmota/tasmota_xdrv_driver/xdrv_23_zigbee_7_6_flash_fs.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef USE_ZIGBEE
#if defined(USE_ZIGBEE) || defined(USE_BERRY)

#ifdef ESP32
#include <vfs_api.h>
Expand All @@ -43,6 +43,12 @@ public:
_seek = 0;
}

FlashFileImpl(const void* buf, size_t len) {
_buf = (const char*)buf;
_len = len;
_seek = 0;
}

virtual ~FlashFileImpl() {}

size_t write(const uint8_t *buf, size_t size) {
Expand Down
3 changes: 3 additions & 0 deletions tasmota/tasmota_xdrv_driver/xdrv_52_0_berry_struct.ino
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public:
int32_t timeout = 0; // Berry heartbeat timeout, preventing code to run for too long. `0` means not enabled
bool rules_busy = false; // are we already processing rules, avoid infinite loop
bool web_add_handler_done = false; // did we already sent `web_add_handler` event
#ifdef USE_BERRY_PARTITION_WIZARD
bool partition_wizard_loaded = false; // did we already load Parition_Wizard
#endif // USE_BERRY_PARTITION_WIZARD
bool autoexec_done = false; // do we still need to load 'autoexec.be'
bool repl_active = false; // is REPL running (activates log recording)
// output log is stored as a LinkedList of buffers
Expand Down
89 changes: 89 additions & 0 deletions tasmota/tasmota_xdrv_driver/xdrv_52_9_berry.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#define XDRV_52 52

#include <berry.h>
extern "C" {
#include "be_bytecode.h"
#include "be_var.h"
}
#include "berry_tasmota.h"
#ifdef USE_MATTER_DEVICE
#include "berry_matter.h"
Expand Down Expand Up @@ -757,6 +761,85 @@ void HandleBerryConsole(void)
WSContentStop();
}

// Display a Button to dynamically load the Partition Wizard
void HandleBerryPartiionWizardLoaderButton(void) {
bvm * vm = berry.vm;
static const char PARTITION_WIZARD_NAME[] = "partition_wizard";
if (!berry.partition_wizard_loaded) {
if (be_global_find(vm, be_newstr(vm, PARTITION_WIZARD_NAME)) < 0) { // the global name `partition_wizard` doesn't exist
WSContentSend_P("<form id=but_part_mgr style='display: block;' action='tapp' method='get'><input type='hidden' name='n' value='Partition_Wizard'/><button>[Load Partition Wizard]</button></form><p></p>");
} else {
berry.partition_wizard_loaded = true;
}
}
}

void HandleBerryPartitionWizardLoader(void) {
if (BerryBECLoader(USE_BERRY_PARTITION_WIZARD_URL)) {
// All good, redirect
Webserver->sendHeader("Location", "/part_wiz", true);
Webserver->send(302, "text/plain", "");
berry.partition_wizard_loaded = true;
} else {
Webserver->sendHeader("Location", "/mn?", true);
Webserver->send(302, "text/plain", "");
}
}

// return true if successful
bool BerryBECLoader(const char * url) {
bvm *vm = berry.vm;

HTTPClientLight cl;
cl.setUserAgent(USE_BERRY_WEBCLIENT_USERAGENT);
cl.setConnectTimeout(USE_BERRY_WEBCLIENT_TIMEOUT); // set default timeout
cl.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);

if (!cl.begin(url)) {
AddLog(LOG_LEVEL_INFO, "BRY: unable to load URL '%s'", url);
// cl.end();
return false;
}

uint32_t http_connect_time = millis();
int32_t httpCode = cl.GET();
if (httpCode != 200) {
AddLog(LOG_LEVEL_INFO, "BRY: unable to load URL '%s' code %i", url, httpCode);
// cl.end();
return false;
}

int32_t sz = cl.getSize();
AddLog(LOG_LEVEL_DEBUG, "BRY: Response http_code %i size %i bytes in %i ms", httpCode, sz, millis() - http_connect_time);
// abort if we exceed 32KB size, things will not go well otherwise
if (sz >= 32767 || sz <= 0) {
AddLog(LOG_LEVEL_DEBUG, "BRY: Response size too big %i bytes", sz);
return false;
}

// create a bytes object at top of stack.
// the streamwriter knows how to get it.
uint8_t * buf = (uint8_t*) be_pushbytes(vm, nullptr, sz);
StreamBeBytesWriter memory_writer(vm);
int32_t written = cl.writeToStream(&memory_writer);
cl.end(); // free allocated memory ~16KB

size_t loaded_sz = 0;
const void * loaded_buf = be_tobytes(vm, -1, &loaded_sz);

FlashFileImplPtr fp = FlashFileImplPtr(new FlashFileImpl(loaded_buf, loaded_sz));
File * f_ptr = new File(fp); // we need to allocate dynamically because be_close calls `delete` on it
bclosure* loaded_bec = be_bytecode_load_from_fs(vm, f_ptr);
be_pop(vm, 1);
if (loaded_bec != NULL) {
be_pushclosure(vm, loaded_bec);
be_call(vm, 0);
be_pop(vm, 1);
}
be_gc_collect(vm); // force a GC to free the buffer now
return true;
}

#endif // USE_WEBSERVER

/*********************************************************************************************\
Expand Down Expand Up @@ -839,6 +922,9 @@ bool Xdrv52(uint32_t function)
XdrvMailbox.index++;
} else {
WSContentSend_P(HTTP_BTN_BERRY_CONSOLE);
#ifdef USE_BERRY_PARTITION_WIZARD
HandleBerryPartiionWizardLoaderButton();
#endif // USE_BERRY_PARTITION_WIZARD
callBerryEventDispatcher(PSTR("web_add_button"), nullptr, 0, nullptr);
callBerryEventDispatcher(PSTR("web_add_console_button"), nullptr, 0, nullptr);
}
Expand All @@ -858,6 +944,9 @@ bool Xdrv52(uint32_t function)
berry.web_add_handler_done = true;
}
WebServer_on(PSTR("/bc"), HandleBerryConsole);
#ifdef USE_BERRY_PARTITION_WIZARD
Webserver->on("/tapp", HTTP_GET, HandleBerryPartitionWizardLoader);
#endif // USE_BERRY_PARTITION_WIZARD
break;
#endif // USE_WEBSERVER
case FUNC_SAVE_BEFORE_RESTART:
Expand Down