MI32: unify connection task (#24769)

This commit is contained in:
Christian Baars
2026-05-20 12:27:50 +02:00
committed by GitHub
parent fb424dca50
commit 68635b9668
5 changed files with 588 additions and 530 deletions
+2 -3
View File
@@ -70,8 +70,7 @@ BE_FUNC_CTYPE_DECLARE(be_BLE_set_MAC, "", "@(bytes)~[ii]");
extern void be_BLE_set_characteristic(struct bvm *vm, const char *Chr);
BE_FUNC_CTYPE_DECLARE(be_BLE_set_characteristic, "", "@s");
extern void be_BLE_run(struct bvm *vm, uint8_t operation, bbool response, int32_t arg1);
BE_FUNC_CTYPE_DECLARE(be_BLE_run, "", "@i[bi]");
extern void be_BLE_run(struct bvm *vm);
extern void be_BLE_store(uint8_t *buf, size_t size);
BE_FUNC_CTYPE_DECLARE(be_BLE_store, "", "(bytes)~");
@@ -92,7 +91,7 @@ module BLE (scope: global) {
info, func(be_BLE_info)
conn_cb, ctype_func(be_BLE_reg_conn_cb)
set_svc, ctype_func(be_BLE_set_service)
run, ctype_func(be_BLE_run)
run, func(be_BLE_run)
store, ctype_func(be_BLE_store)
set_chr, ctype_func(be_BLE_set_characteristic)
adv_cb, ctype_func(be_BLE_reg_adv_cb)
+22 -13
View File
@@ -172,7 +172,14 @@ struct MI32connectionContextBerry_t{
NimBLEUUID charUUID;
uint16_t returnCharUUID;
uint16_t handle;
uint16_t itvl_min;
uint16_t itvl_max;
uint8_t * buffer;
// Reverse-role client (peripheral side). Singleton owned by NimBLEServer
// (do NOT pass to NimBLEDevice::deleteClient). Use only for server-side
// ACNS-style queries toward the connected central. Set in
// MI32ServerCallbacks::onConnect, cleared (not freed) in onDisconnect.
NimBLEClient * serverPeer = nullptr;
uint8_t MAC[6];
uint8_t operation;
uint8_t addrType;
@@ -188,7 +195,6 @@ struct {
// uint32_t period; // set manually in addition to TELE-period, is set to TELE-period after start
TaskHandle_t ScanTask = nullptr;
TaskHandle_t ConnTask = nullptr;
TaskHandle_t ServerTask = nullptr;
MI32connectionContextBerry_t *conCtx = nullptr;
uint16_t connID;
union {
@@ -214,14 +220,11 @@ struct {
uint32_t triggerBerryAdvCB:1;
uint32_t triggerBerryConnCB:1;
uint32_t triggerNextConnJob:1;
uint32_t readyForNextConnJob:1;
uint32_t triggerNextJob:1;
uint32_t readyForNextJob:1;
uint32_t discoverAttributes:1;
uint32_t triggerNextServerJob:1;
uint32_t readyForNextServerJob:1;
uint32_t triggerBerryServerCB:1;
uint32_t deleteServerTask:1;
uint32_t deleteConnectionTask:1; // request server-task teardown (renamed from deleteServerTask)
};
uint32_t all = 0;
} mode;
@@ -245,10 +248,9 @@ struct {
void *beConnCB;
void *beAdvCB;
void *beServerCB;
uint8_t *beAdvBuf;
uint8_t infoMsg = 0;
uint8_t role = 0;
uint8_t role = 0; // bitfield of MI32Role values (concurrently active roles)
} MI32;
struct mi_sensor_t{
@@ -419,8 +421,6 @@ enum MI32_Commands { // commands useable in console or rules
enum MI32_TASK {
MI32_TASK_SCAN = 0,
MI32_TASK_CONN = 1,
MI32_TASK_SERV = 2,
};
enum BLE_CLIENT_OP {
@@ -463,6 +463,15 @@ enum MI32_ConnErrorMsg {
MI32_CONN_NOTIFY_TIMEOUT
};
// MI32.role bit layout (concurrently-active roles, not mutually exclusive)
enum MI32Role : uint8_t {
MI32_ROLE_NONE = 0x00,
MI32_ROLE_SCAN = 0x01,
MI32_ROLE_CLIENT = 0x02,
MI32_ROLE_SERVER = 0x04,
MI32_ROLE_ADVERTISER = 0x08,
};
enum MI32_BLEInfoMsg {
MI32_SCAN_ENDED = 1,
MI32_GOT_NOTIFICATION,
@@ -511,8 +520,8 @@ const char HTTP_MI32_POWER_WIDGET[] PROGMEM =
"<div class='box' id='box%u'>"
"<h2 style='margin-top:0em;'>Energy"
"</h2>"
"<p>" D_VOLTAGE ": %.1f " D_UNIT_VOLT "</p>"
"<p>" D_CURRENT ": %.3f " D_UNIT_AMPERE "</p>";
"<p>" D_VOLTAGE ": %s " D_UNIT_VOLT "</p>"
"<p>" D_CURRENT ": %s " D_UNIT_AMPERE "</p>";
#endif //USE_MI_ESP32_ENERGY
/*
@@ -805,9 +805,6 @@ bool HttpCheckPriviledgedAccess(bool autorequestauth = true) {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_HTTP "Referer '%s' denied. Use 'SO128 1' for HTTP API commands. 'Webpassword' is recommended."), referer.c_str());
return false;
} else {
#if defined(USE_MI_ESP32) && !defined(USE_BLE_ESP32)
MI32suspendScanTask();
#endif // defined(USE_MI_ESP32) && !defined(USE_BLE_ESP32)
return true;
}
}
@@ -1212,9 +1209,6 @@ void WSContentEnd(void) {
}
Webserver->client().stop();
#if defined(USE_MI_ESP32) && !defined(USE_BLE_ESP32)
MI32resumeScanTask();
#endif // defined(USE_MI_ESP32) && !defined(USE_BLE_ESP32)
}
/*-------------------------------------------------------------------------------------------*/
@@ -107,7 +107,7 @@ extern "C" {
extern void MI32setBerryAdvCB(void* function, uint8_t *buffer);
extern void MI32setBerryConnCB(void* function, uint8_t *buffer);
extern void MI32setBerryServerCB(void* function, uint8_t *buffer);
extern bool MI32runBerryConnection(uint8_t operation, bbool response, int32_t *arg1);
extern bool MI32runBerryConnection(uint8_t operation, bbool response, bbool hasArg1, int32_t arg1);
extern bool MI32setBerryCtxSvc(const char *Svc, bbool discoverAttributes);
extern bool MI32setBerryCtxChr(const char *Chr);
extern bool MI32setBerryCtxMAC(uint8_t *MAC, uint8_t type, uint32_t pin);
@@ -196,23 +196,37 @@ extern "C" {
be_raisef(vm, "ble_error", "BLE: could not set characteristic");
}
void be_BLE_run(struct bvm *vm, uint8_t operation, bbool response, int32_t arg1);
void be_BLE_run(struct bvm *vm, uint8_t operation, bbool response, int32_t arg1){
int32_t argc = be_top(vm); // Get the number of arguments
bool _response = false;
if(response){
_response = response;
}
int32_t *ptr_arg1 = nullptr;
int32_t _arg1 = arg1;
if(argc == 3){
ptr_arg1 = &_arg1;
}
if (MI32runBerryConnection(operation, _response, ptr_arg1)) return;
int be_BLE_run(bvm *vm);
int be_BLE_run(bvm *vm) {
int argc = be_top(vm);
be_raisef(vm, "ble_error", "BLE: could not run operation");
if (argc < 1 || !be_isint(vm, 1)) {
be_raise(vm, "value_error", "BLE.run: wrong arguments");
be_return_nil(vm);
}
uint8_t operation = (uint8_t) be_toint(vm, 1);
bool response = false;
if (argc >= 2) {
response = be_tobool(vm, 2);
}
int32_t arg1 = 0;
bool hasArg1 = false;
if (argc >= 3) {
arg1 = (int32_t) be_toint(vm, 3);
hasArg1 = true;
}
if (MI32runBerryConnection(operation, response, hasArg1, arg1)) {
be_return_nil(vm);
}
be_raisef(vm, "ble_error", "BLE: could not run operation");
be_return_nil(vm);
}
void be_BLE_adv_watch(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type);
void be_BLE_adv_watch(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type){
if(!be_BLE_MAC_size(vm, size)){
@@ -278,7 +292,7 @@ extern "C" {
// #else
// be_map_insert_nil(vm, "bonds");
// #endif
if(MI32.mode.connected == 1 || MI32.ServerTask != nullptr){
if(MI32.mode.connected == 1 || (MI32.role & MI32_ROLE_SERVER)){
NimBLEClient* _device = nullptr;
if(MI32.mode.connected == 1){
_device = NimBLEDevice::getClientByHandle(MI32.connID);
File diff suppressed because it is too large Load Diff