add BLE.info() (#20682)

This commit is contained in:
Christian Baars 2024-02-08 11:31:48 +01:00 committed by GitHub
parent f06a8b2bdb
commit b02de6de6f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 52 additions and 1 deletions

View File

@ -46,6 +46,8 @@ module MI32 (scope: global) {
extern int be_BLE_init(bvm *vm);
extern int be_BLE_info(bvm *vm);
extern void be_BLE_loop(void);
BE_FUNC_CTYPE_DECLARE(be_BLE_loop, "", "");
@ -83,6 +85,7 @@ BE_FUNC_CTYPE_DECLARE(be_BLE_adv_block, "", "@(bytes)~[i]");
module BLE (scope: global) {
init, func(be_BLE_init)
loop, ctype_func(be_BLE_loop)
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)

View File

@ -18,6 +18,11 @@
*/
#ifdef USE_MI_ESP32
#pragma once
#include <NimBLEDevice.h>
/*********************************************************************************************\
* structs and types
\*********************************************************************************************/

View File

@ -24,6 +24,8 @@
#if defined(USE_MI_ESP32) && !defined(USE_BLE_ESP32)
#include "include/xsns_62_esp32_mi.h"
/*********************************************************************************************\
* Native functions mapped to Berry functions
*
@ -206,6 +208,48 @@ extern "C" {
be_raisef(vm, "ble_error", "BLE: could not add MAC to watch list");
}
// BLE.info(void) -> map
int32_t be_BLE_info(struct bvm *vm);
int32_t be_BLE_info(struct bvm *vm) {
be_newobject(vm, "map");
char _Role[16];
GetTextIndexed(_Role, sizeof(_Role), MI32.role, HTTP_MI32_PARENT_BLE_ROLE);
char _role[16];
LowerCase(_role,_Role);
be_map_insert_str(vm, "role", _role);
be_map_insert_str(vm, "local_addr", NimBLEDevice::toString().c_str());
be_map_insert_int(vm, "power", NimBLEDevice::getPower());
be_map_insert_int(vm, "MTU", NimBLEDevice::getMTU());
#ifdef CONFIG_BT_NIMBLE_EXT_ADV // TODO - BLE 5 is very unsharp
be_map_insert_int(vm, "version", 5);
#else
be_map_insert_int(vm, "version", 4);
#endif
#ifdef CONFIG_BT_NIMBLE_PERSIST
be_map_insert_int(vm, "bonds", NimBLEDevice::getNumBonds());
#else
be_map_insert_nil(vm, "bonds");
#endif
if(MI32.mode.connected == 1){
be_pushstring(vm, "connection");
be_newobject(vm, "map");
auto _info = NimBLEDevice::getClientList()->front()->getConnInfo();
be_map_insert_str(vm, "peer_addr", _info.getAddress().toString().c_str());
be_map_insert_int(vm, "RSSI", NimBLEDevice::getClientList()->front()->getRssi());
be_map_insert_int(vm, "MTU", _info.getMTU());
be_map_insert_bool(vm, "bonded", _info.isBonded());
be_map_insert_bool(vm, "master", _info.isMaster());
be_map_insert_bool(vm, "encrypted", _info.isEncrypted());
be_map_insert_bool(vm, "authenticated", _info.isAuthenticated());
be_pop(vm, 1);
be_data_insert(vm, -3);
be_pop(vm, 2);
}
be_pop(vm, 1);
be_return(vm);
}
} //extern "C"

View File

@ -60,7 +60,6 @@
#define XSNS_62 62
#include <NimBLEDevice.h>
// undefine "trash" from the NimBLE stack, that collides with Tasmotas enum
#undef LOG_LEVEL_DEBUG
#undef LOG_LEVEL_NONE