refactor MI32 and BLE in Berry to module

This commit is contained in:
Christian Baars 2022-09-10 16:38:12 +02:00
parent feaf610ed1
commit d57a1cadc9
3 changed files with 124 additions and 116 deletions

View File

@ -54,6 +54,9 @@ be_extern_native_module(ULP);
#ifdef USE_ZIGBEE #ifdef USE_ZIGBEE
be_extern_native_module(zigbee); be_extern_native_module(zigbee);
#endif // USE_ZIGBEE #endif // USE_ZIGBEE
// BLE
be_extern_native_module(MI32);
be_extern_native_module(BLE);
#ifdef USE_LVGL #ifdef USE_LVGL
be_extern_native_module(lv); be_extern_native_module(lv);
be_extern_native_module(lv_extra); be_extern_native_module(lv_extra);
@ -166,7 +169,10 @@ BERRY_LOCAL const bntvmodule* const be_module_table[] = {
#if defined(USE_BERRY_ULP) && defined(CONFIG_IDF_TARGET_ESP32) #if defined(USE_BERRY_ULP) && defined(CONFIG_IDF_TARGET_ESP32)
&be_native_module(ULP), &be_native_module(ULP),
#endif // USE_BERRY_ULP #endif // USE_BERRY_ULP
#if defined(USE_MI_ESP32) && !defined(USE_BLE_ESP32)
&be_native_module(MI32),
&be_native_module(BLE),
#endif //USE_MI_ESP32
/* user-defined modules register end */ /* user-defined modules register end */
NULL /* do not remove */ NULL /* do not remove */
}; };
@ -201,9 +207,6 @@ be_extern_native_class(webclient);
be_extern_native_class(tcpclient); be_extern_native_class(tcpclient);
be_extern_native_class(tcpserver); be_extern_native_class(tcpserver);
be_extern_native_class(energy_struct); be_extern_native_class(energy_struct);
// BLE
be_extern_native_class(MI32);
be_extern_native_class(BLE);
// LVGL core classes // LVGL core classes
be_extern_native_class(lv_color); be_extern_native_class(lv_color);
be_extern_native_class(lv_font); be_extern_native_class(lv_font);
@ -287,10 +290,6 @@ BERRY_LOCAL bclass_array be_class_table = {
#ifdef USE_BERRY_INT64 #ifdef USE_BERRY_INT64
&be_native_class(int64), &be_native_class(int64),
#endif #endif
#if defined(USE_MI_ESP32) && !defined(USE_BLE_ESP32)
&be_native_class(MI32),
&be_native_class(BLE),
#endif //USE_MI_ESP32
NULL, /* do not remove */ NULL, /* do not remove */
}; };

View File

@ -4,6 +4,7 @@
* To use: `import MI32` * To use: `import MI32`
*******************************************************************/ *******************************************************************/
#include "be_constobj.h" #include "be_constobj.h"
#include "be_mapping.h"
#ifdef USE_MI_ESP32 #ifdef USE_MI_ESP32
extern int be_MI32_devices(bvm *vm); extern int be_MI32_devices(bvm *vm);
@ -13,10 +14,10 @@ extern int be_MI32_get_MAC(bvm *vm);
extern int be_MI32_set_hum(bvm *vm); extern int be_MI32_set_hum(bvm *vm);
extern int be_MI32_set_temp(bvm *vm); extern int be_MI32_set_temp(bvm *vm);
#include "be_fixed_be_class_MI32.h" #include "be_fixed_MI32.h"
/* @const_object_info_begin /* @const_object_info_begin
class be_class_MI32 (scope: global, name: MI32) { module MI32 (scope: global) {
devices, func(be_MI32_devices) devices, func(be_MI32_devices)
get_name, func(be_MI32_get_name) get_name, func(be_MI32_get_name)
get_MAC, func(be_MI32_get_MAC) get_MAC, func(be_MI32_get_MAC)
@ -26,27 +27,49 @@ class be_class_MI32 (scope: global, name: MI32) {
} }
@const_object_info_end */ @const_object_info_end */
extern int be_BLE_reg_conn_cb(bvm *vm); /********************************************************************
extern int be_BLE_reg_adv_cb(bvm *vm); * Tasmota lib
extern int be_BLE_set_MAC(bvm *vm); *
extern int be_BLE_set_characteristic(bvm *vm); * To use: `import BLE`
extern int be_BLE_run(bvm *vm); *******************************************************************/
extern int be_BLE_set_service(bvm *vm);
extern int be_BLE_adv_watch(bvm *vm);
extern int be_BLE_adv_block(bvm *vm);
#include "be_fixed_be_class_BLE.h" extern void be_BLE_reg_conn_cb(void* function, uint8_t *buffer);
BE_FUNC_CTYPE_DECLARE(be_BLE_reg_conn_cb, "", "cc");
extern void be_BLE_reg_adv_cb(void* function, uint8_t *buffer);
BE_FUNC_CTYPE_DECLARE(be_BLE_reg_adv_cb, "", "c[c]");
extern void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type);
BE_FUNC_CTYPE_DECLARE(be_BLE_set_MAC, "", "@(bytes)~[i]");
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);
BE_FUNC_CTYPE_DECLARE(be_BLE_run, "", "@i[b]");
extern void be_BLE_set_service(struct bvm *vm, const char *Svc, bbool discoverAttributes);
BE_FUNC_CTYPE_DECLARE(be_BLE_set_service, "", "@s[b]");
extern void be_BLE_adv_watch(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type);
BE_FUNC_CTYPE_DECLARE(be_BLE_adv_watch, "", "@(bytes)~[i]");
extern void be_BLE_adv_block(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type);
BE_FUNC_CTYPE_DECLARE(be_BLE_adv_block, "", "@(bytes)~[i]");
#include "be_fixed_BLE.h"
/* @const_object_info_begin /* @const_object_info_begin
class be_class_BLE (scope: global, name: BLE, strings: weak) { module BLE (scope: global) {
conn_cb, func(be_BLE_reg_conn_cb) conn_cb, ctype_func(be_BLE_reg_conn_cb)
set_svc, func(be_BLE_set_service) set_svc, ctype_func(be_BLE_set_service)
run, func(be_BLE_run) run, ctype_func(be_BLE_run)
set_chr, func(be_BLE_set_characteristic) set_chr, ctype_func(be_BLE_set_characteristic)
adv_cb, func(be_BLE_reg_adv_cb) adv_cb, ctype_func(be_BLE_reg_adv_cb)
set_MAC, func(be_BLE_set_MAC) set_MAC, ctype_func(be_BLE_set_MAC)
adv_watch, func(be_BLE_adv_watch) adv_watch, ctype_func(be_BLE_adv_watch)
adv_block, func(be_BLE_adv_block) adv_block, ctype_func(be_BLE_adv_block)
} }
@const_object_info_end */ @const_object_info_end */

View File

@ -118,121 +118,107 @@ extern "C" {
********************************************************************/ ********************************************************************/
extern void MI32setBerryAdvCB(void* function, uint8_t *buffer); extern void MI32setBerryAdvCB(void* function, uint8_t *buffer);
extern void MI32setBerryConnCB(void* function, uint8_t *buffer); extern void MI32setBerryConnCB(void* function, uint8_t *buffer);
extern bool MI32runBerryConnection(uint8_t operation, bool response); extern bool MI32runBerryConnection(uint8_t operation, bbool response);
extern bool MI32setBerryCtxSvc(const char *Svc, bool discoverAttributes); extern bool MI32setBerryCtxSvc(const char *Svc, bbool discoverAttributes);
extern bool MI32setBerryCtxChr(const char *Chr); extern bool MI32setBerryCtxChr(const char *Chr);
extern bool MI32setBerryCtxMAC(uint8_t *MAC, uint8_t type); extern bool MI32setBerryCtxMAC(uint8_t *MAC, uint8_t type);
extern bool MI32addMACtoBlockList(uint8_t *MAC, uint8_t type); extern bool MI32addMACtoBlockList(uint8_t *MAC, uint8_t type);
extern bool MI32addMACtoWatchList(uint8_t *MAC, uint8_t type); extern bool MI32addMACtoWatchList(uint8_t *MAC, uint8_t type);
int be_BLE_reg_conn_cb(bvm *vm); void be_BLE_reg_conn_cb(void* function, uint8_t *buffer);
int be_BLE_reg_conn_cb(bvm *vm){ void be_BLE_reg_conn_cb(void* function, uint8_t *buffer){
int32_t argc = be_top(vm); // Get the number of arguments MI32setBerryConnCB(function,buffer);
if (argc == 3 && be_iscomptr(vm, 2)) {
void* cb = be_tocomptr(vm, 2);
size_t len;
uint8_t * buf = (uint8_t*)be_tobytes(vm, 3, &len);
MI32setBerryConnCB(cb,buf);
be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
} }
int be_BLE_reg_adv_cb(bvm *vm); void be_BLE_reg_adv_cb(void* function, uint8_t *buffer);
int be_BLE_reg_adv_cb(bvm *vm){ void be_BLE_reg_adv_cb(void* function, uint8_t *buffer){
int32_t argc = be_top(vm); // Get the number of arguments if(function == 0){
if (argc == 3 && be_iscomptr(vm, 2)) {
void* cb = be_tocomptr(vm, 2);
size_t len;
uint8_t * buf = (uint8_t*)be_tobytes(vm, 3, &len);
MI32setBerryAdvCB(cb,buf);
be_return(vm); // Return
}
else if(argc == 2 && be_isint(vm, 2)){
if(be_toint(vm, 2) == 0){
MI32setBerryAdvCB(NULL,NULL); MI32setBerryAdvCB(NULL,NULL);
be_return(vm); // Return
} }
else if(buffer){
MI32setBerryAdvCB(function,buffer);
} }
be_raise(vm, kTypeError, nullptr);
} }
int be_BLE_set_MAC(bvm *vm); bool be_BLE_MAC_size(struct bvm *vm, size_t size){
int be_BLE_set_MAC(bvm *vm){ if(size != 6){
int32_t argc = be_top(vm); // Get the number of arguments be_raisef(vm, "ble_error", "BLE: wrong size of MAC");
if (argc > 1 && be_isbytes(vm, 2)) { return false;
size_t len = 6;
uint8_t type = 0;
if(argc == 3 && be_isint(vm, 3)){
type = be_toint(vm,3);
} }
if (MI32setBerryCtxMAC((uint8_t*)be_tobytes(vm, 2, &len),type)) be_return(vm); return true;
}
be_raise(vm, kTypeError, nullptr);
} }
int be_BLE_set_service(bvm *vm); void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type);
int be_BLE_set_service(bvm *vm){ void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type){
int32_t argc = be_top(vm); // Get the number of arguments if(!be_BLE_MAC_size(vm, size)){
if (argc > 1 && be_isstring(vm, 2)) { return;
bool discoverAttributes = false;
if(argc == 3 && be_isint(vm, 3)){
discoverAttributes = be_toint(vm,3)>0;
} }
if (MI32setBerryCtxSvc(be_tostring(vm, 2),discoverAttributes)) be_return(vm); uint8_t _type = 0;
if(type){
_type = type;
} }
be_raise(vm, kTypeError, nullptr); if (MI32setBerryCtxMAC(buf,_type)) return;
be_raisef(vm, "ble_error", "BLE: could not set MAC");
} }
int be_BLE_set_characteristic(bvm *vm); void be_BLE_set_service(struct bvm *vm, const char *Svc, bbool discoverAttributes);
int be_BLE_set_characteristic(bvm *vm){ void be_BLE_set_service(struct bvm *vm, const char *Svc, bbool discoverAttributes){
int32_t argc = be_top(vm); // Get the number of arguments bool _discoverAttributes = false;
if (argc == 2 && be_isstring(vm, 2)) { if(discoverAttributes){
if (MI32setBerryCtxChr(be_tostring(vm, 2))) be_return(vm); _discoverAttributes = discoverAttributes ;
} }
be_raise(vm, kTypeError, nullptr); if (MI32setBerryCtxSvc(Svc,_discoverAttributes)) return;
be_raisef(vm, "ble_error", "BLE: could not set service");
} }
int be_BLE_run(bvm *vm); void be_BLE_set_characteristic(struct bvm *vm, const char *Chr);
int be_BLE_run(bvm *vm){ void be_BLE_set_characteristic(struct bvm *vm, const char *Chr){
int32_t argc = be_top(vm); // Get the number of arguments
if ((argc > 1) && be_isint(vm, 2)) { if (MI32setBerryCtxChr(Chr)) return;
bool response = false;
if(argc == 3 && be_isint(vm, 3)){ be_raisef(vm, "ble_error", "BLE: could not set characteristic");
response = be_toint(vm,3)>0;
}
if (MI32runBerryConnection(be_toint(vm, 2),response)) be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
} }
int be_BLE_adv_block(bvm *vm); void be_BLE_run(struct bvm *vm, uint8_t operation, bbool response);
int be_BLE_adv_block(bvm *vm){ void be_BLE_run(struct bvm *vm, uint8_t operation, bbool response){
int32_t argc = be_top(vm); // Get the number of arguments bool _response = false;
if (argc > 1 && be_isbytes(vm, 2)) { if(response){
size_t len = 6; _response = response;
uint8_t type = 0;
if(argc == 3 && be_isint(vm, 3)){
type = be_toint(vm,3);
} }
if(MI32addMACtoBlockList((uint8_t*)be_tobytes(vm, 2, &len),type)) be_return(vm); if (MI32runBerryConnection(operation,_response)) return;
}
be_raise(vm, kTypeError, nullptr); be_raisef(vm, "ble_error", "BLE: could not run operation");
} }
int be_BLE_adv_watch(bvm *vm); void be_BLE_adv_block(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type);
int be_BLE_adv_watch(bvm *vm){ void be_BLE_adv_block(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type){
int32_t argc = be_top(vm); // Get the number of arguments if(!be_BLE_MAC_size(vm, size)){
if (argc > 1 && be_isbytes(vm, 2)) { return;
size_t len = 6;
uint8_t type = 0;
if(argc == 3 && be_isint(vm, 3)){
type = be_toint(vm,3);
} }
if (MI32addMACtoWatchList((uint8_t*)be_tobytes(vm, 2, &len),type)) be_return(vm); uint8_t _type = 0;
if(type){
_type = type;
} }
be_raise(vm, kTypeError, nullptr); if(MI32addMACtoBlockList(buf, _type)) return;
be_raisef(vm, "ble_error", "BLE: could not block MAC");
}
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)){
return;
}
uint8_t _type = 0;
if(type){
_type = type;
}
if(MI32addMACtoWatchList(buf, _type)) return;
be_raisef(vm, "ble_error", "BLE: could not add MAC to watch list");
} }
} //extern "C" } //extern "C"