Просмотр исходного кода

update:拆分蓝牙源文件功能化区分
update:gatt拆分注册
update:优化lua ble api使用方式

Dozingfiretruck 8 месяцев назад
Родитель
Сommit
1406ebed63

+ 20 - 41
components/bluetooth/luat_bluetooth.h → components/bluetooth/include/luat_ble.h

@@ -1,5 +1,5 @@
-#ifndef LUAT_BLUETOOTH
-#define LUAT_BLUETOOTH
+#ifndef __LUAT_BLE__
+#define __LUAT_BLE__
 
 
 
@@ -133,6 +133,7 @@ typedef enum{
 }luat_ble_uuid_type;
 
 typedef struct luat_bluetooth luat_bluetooth_t;
+typedef struct luat_ble luat_ble_t;
 
 typedef struct{
     uint8_t conn_idx;       /**< The index of the connection */
@@ -143,7 +144,7 @@ typedef struct{
 typedef struct{
     uint8_t conn_idx;       /**< The index of the connection */
     uint16_t service_id;        /**< The id of the gatt service */
-    uint16_t att_idx;       /**< The index of the attribute */
+    uint16_t handle;       /**< The index of the attribute */
     uint8_t *value;         /**< The attribute value */
     uint16_t len;           /**< The length of the attribute value */
     uint16_t size;
@@ -152,7 +153,7 @@ typedef struct{
 typedef struct{
     uint8_t conn_idx;       /**< The index of the connection */
     uint16_t service_id;        /**< The id of the gatt service */
-    uint16_t att_idx;       /**< The index of the attribute */
+    uint16_t handle;       /**< The index of the attribute */
     uint8_t *value;         /**< The attribute value */
     uint16_t len;           /**< The data length read */
     uint16_t size;          /**< The size of attribute value to read */
@@ -172,7 +173,7 @@ typedef struct {
     int len;
 }luat_ble_adv_data_t;
 
-typedef void (*luat_ble_cb_t)(luat_bluetooth_t* luat_bluetooth, luat_ble_event_t ble_event, luat_ble_param_t* ble_param);
+typedef void (*luat_ble_cb_t)(luat_ble_t* luat_ble, luat_ble_event_t ble_event, luat_ble_param_t* ble_param);
 
 typedef struct luat_ble_gatt_chara luat_ble_gatt_chara_t;
 
@@ -214,58 +215,36 @@ typedef struct {
     int len;
 }luat_ble_adv_cfg_t;
 
-typedef struct {
+struct luat_ble{
     luat_ble_actv_state state;
     luat_ble_cb_t cb;
     int lua_cb;
+    int ble_ref;
     void* userdata;
-}luat_ble_t;
-
-typedef struct {
-    void* userdata;
-}luat_bt_t;
-
-typedef struct luat_bluetooth{
-    luat_ble_t* luat_ble;
-    luat_bt_t* luat_bt;
-    int bluetooth_ref;
-}luat_bluetooth_t;
+};
 
 int luat_ble_uuid_swap(uint8_t* uuid_data, luat_ble_uuid_type uuid_type);
 
-int luat_ble_init(luat_bluetooth_t* luat_bluetooth, luat_ble_cb_t luat_ble_cb);
-
-int luat_ble_deinit(luat_bluetooth_t* luat_bluetooth);
-
-int luat_ble_create_advertising(luat_bluetooth_t* luat_bluetooth, luat_ble_adv_cfg_t* adv_cfg);
-
-int luat_ble_set_name(luat_bluetooth_t* luat_bluetooth, char* name, uint8_t len);
-
-int luat_ble_set_adv_data(luat_bluetooth_t* luat_bluetooth, uint8_t* adv_buff, uint8_t adv_len);
-
-int luat_ble_set_scan_rsp_data(luat_bluetooth_t* luat_bluetooth, uint8_t* scan_buff, uint8_t scan_len);
-
-int luat_ble_start_advertising(luat_bluetooth_t* luat_bluetooth);
-
-int luat_ble_stop_advertising(luat_bluetooth_t* luat_bluetooth);
+int luat_ble_init(luat_ble_t* luat_ble, luat_ble_cb_t luat_ble_cb);
 
-int luat_ble_delete_advertising(luat_bluetooth_t* luat_bluetooth);
+int luat_ble_deinit(luat_ble_t* luat_ble);
 
-int luat_ble_create_gatt(luat_bluetooth_t* luat_bluetooth, luat_ble_gatt_service_t* luat_ble_gatt_service);
+int luat_ble_create_advertising(luat_ble_t* luat_ble, luat_ble_adv_cfg_t* adv_cfg);
 
-int luat_ble_read_response(luat_bluetooth_t* luat_bluetooth, uint8_t conn_idx, uint16_t service_id, uint16_t att_handle, uint32_t len, uint8_t *buf);
+int luat_ble_set_name(luat_ble_t* luat_ble, char* name, uint8_t len);
 
+int luat_ble_set_adv_data(luat_ble_t* luat_ble, uint8_t* adv_buff, uint8_t adv_len);
 
-// bt
+int luat_ble_set_scan_rsp_data(luat_ble_t* luat_ble, uint8_t* scan_buff, uint8_t scan_len);
 
+int luat_ble_start_advertising(luat_ble_t* luat_ble);
 
-int luat_bt_get_mac(luat_bluetooth_t* luat_bluetooth, uint8_t *addr);
-int luat_bt_set_mac(luat_bluetooth_t* luat_bluetooth, uint8_t *addr, uint8_t len);
+int luat_ble_stop_advertising(luat_ble_t* luat_ble);
 
+int luat_ble_delete_advertising(luat_ble_t* luat_ble);
 
-// bluetooth
+int luat_ble_create_gatt(luat_ble_t* luat_ble, luat_ble_gatt_service_t* luat_ble_gatt_service);
 
-int luat_bluetooth_init(luat_bluetooth_t* luat_bluetooth);
-int luat_bluetooth_deinit(luat_bluetooth_t* luat_bluetooth);
+int luat_ble_read_response(luat_ble_t* luat_ble, uint8_t conn_idx, uint16_t service_id, uint16_t att_handle, uint32_t len, uint8_t *buf);
 
 #endif

+ 24 - 0
components/bluetooth/include/luat_bluetooth.h

@@ -0,0 +1,24 @@
+#ifndef __LUAT_BLUETOOTH__
+#define __LUAT_BLUETOOTH__
+
+#include "luat_ble.h"
+#include "luat_bt.h"
+
+#define LUAT_BLUETOOTH_TYPE "BLUETOOTH*"
+#define LUAT_BLE_TYPE "BLE*"
+
+typedef struct luat_bluetooth{
+    luat_ble_t* luat_ble;
+    luat_bt_t* luat_bt;
+    int bluetooth_ref;
+}luat_bluetooth_t;
+
+// bluetooth
+
+int luat_bluetooth_init(luat_bluetooth_t* luat_bluetooth);
+int luat_bluetooth_deinit(luat_bluetooth_t* luat_bluetooth);
+
+int luat_bluetooth_get_mac(luat_bluetooth_t* luat_bluetooth, uint8_t *addr);
+int luat_bluetooth_set_mac(luat_bluetooth_t* luat_bluetooth, uint8_t *addr, uint8_t len);
+
+#endif

+ 9 - 0
components/bluetooth/include/luat_bt.h

@@ -0,0 +1,9 @@
+#ifndef __LUAT_BT__
+#define __LUAT_BT__
+
+
+typedef struct {
+    void* userdata;
+}luat_bt_t;
+
+#endif

+ 2 - 2
components/bluetooth/luat_bluetooth.c → components/bluetooth/src/luat_ble.c

@@ -1,9 +1,9 @@
 #include "luat_base.h"
 #include "luat_mem.h"
-#include "luat_bluetooth.h"
+#include "luat_ble.h"
 
 #include "luat_log.h"
-#define LUAT_LOG_TAG "bluetooth"
+#define LUAT_LOG_TAG "ble"
 
 
 int luat_ble_uuid_swap(uint8_t* uuid_data, luat_ble_uuid_type uuid_type){

+ 11 - 0
components/bluetooth/src/luat_bt.c

@@ -0,0 +1,11 @@
+#include "luat_base.h"
+#include "luat_mem.h"
+#include "luat_bt.h"
+
+#include "luat_log.h"
+#define LUAT_LOG_TAG "bt"
+
+
+
+
+

+ 41 - 77
components/bluetooth/luat_lib_bluetooth.c → components/bluetooth/src/luat_lib_ble.c

@@ -2,24 +2,23 @@
 #include "luat_mem.h"
 #include "luat_rtos.h"
 #include "luat_msgbus.h"
-#include "luat_bluetooth.h"
+#include "luat_ble.h"
 
 #include "luat_log.h"
-#define LUAT_LOG_TAG "bluetooth"
+#define LUAT_LOG_TAG "ble"
 
-#define LUAT_BLUETOOTH_TYPE "BLUETOOTH*"
 #define LUAT_BLE_TYPE "BLE*"
 
 static int luatos_ble_callback(lua_State *L, void* ptr){
 	(void)ptr;
     rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
-    luat_bluetooth_t* luat_bluetooth =(luat_bluetooth_t *)msg->ptr;
+    luat_ble_t* luat_ble =(luat_ble_t *)msg->ptr;
     luat_ble_event_t ble_event = (luat_ble_event_t)msg->arg1;
     luat_ble_param_t* luat_ble_param = (luat_ble_param_t*)msg->arg2;
 
-    lua_geti(L, LUA_REGISTRYINDEX, luat_bluetooth->luat_ble->lua_cb);
+    lua_geti(L, LUA_REGISTRYINDEX, luat_ble->lua_cb);
     if (lua_isfunction(L, -1)) {
-        lua_geti(L, LUA_REGISTRYINDEX, luat_bluetooth->bluetooth_ref);
+        lua_geti(L, LUA_REGISTRYINDEX, luat_ble->ble_ref);
         lua_pushinteger(L, ble_event);
     }
 
@@ -32,10 +31,10 @@ static int luatos_ble_callback(lua_State *L, void* ptr){
             lua_pushinteger(L, write_req->conn_idx);
             lua_settable(L, -3);
             lua_pushliteral(L, "service_id"); 
-            lua_pushinteger(L, write_req->service_id+1);
+            lua_pushinteger(L, write_req->service_id);
             lua_settable(L, -3);
-            lua_pushliteral(L, "att_idx"); 
-            lua_pushinteger(L, write_req->att_idx + 1);
+            lua_pushliteral(L, "handle"); 
+            lua_pushinteger(L, write_req->handle);
             lua_settable(L, -3);
             lua_pushliteral(L, "data"); 
             lua_pushlstring(L, (const char *)write_req->value, write_req->len);
@@ -54,10 +53,10 @@ static int luatos_ble_callback(lua_State *L, void* ptr){
             lua_pushinteger(L, read_req->conn_idx);
             lua_settable(L, -3);
             lua_pushliteral(L, "service_id"); 
-            lua_pushinteger(L, read_req->service_id+1);
+            lua_pushinteger(L, read_req->service_id);
             lua_settable(L, -3);
-            lua_pushliteral(L, "att_idx"); 
-            lua_pushinteger(L, read_req->att_idx + 1);
+            lua_pushliteral(L, "handle"); 
+            lua_pushinteger(L, read_req->handle);
             lua_settable(L, -3);
 
             lua_call(L, 3, 0);
@@ -74,7 +73,7 @@ static int luatos_ble_callback(lua_State *L, void* ptr){
     return 0;
 }
 
-static void luat_ble_cb(luat_bluetooth_t* luat_bluetooth, luat_ble_event_t ble_event, luat_ble_param_t* ble_param){
+void luat_ble_cb(luat_ble_t* luat_ble, luat_ble_event_t ble_event, luat_ble_param_t* ble_param){
     LLOGD("ble event: %d", ble_event);
     luat_ble_param_t* luat_ble_param = NULL;
     if (ble_param){
@@ -91,38 +90,19 @@ static void luat_ble_cb(luat_bluetooth_t* luat_bluetooth, luat_ble_event_t ble_e
     
     rtos_msg_t msg = {
         .handler = luatos_ble_callback,
-        .ptr = (void*)luat_bluetooth,
+        .ptr = (void*)luat_ble,
         .arg1 = (int)ble_event,
         .arg2 = (int)luat_ble_param,
     };
     luat_msgbus_put(&msg, 0);
 }
 
-static int l_bluetooth_create_ble(lua_State* L) {
-    if (!lua_isuserdata(L, 1)){
-        return 0;
-    }
-    luat_bluetooth_t* luat_bluetooth = (luat_bluetooth_t *)luaL_checkudata(L, 1, LUAT_BLUETOOTH_TYPE);
-    luat_ble_init(luat_bluetooth, luat_ble_cb);
-
-    if (lua_isfunction(L, 2)) {
-		lua_pushvalue(L, 2);
-		luat_bluetooth->luat_ble->lua_cb = luaL_ref(L, LUA_REGISTRYINDEX);
-	}else{
-        LLOGE("error cb");
-        return 0;
-    }
-
-    lua_pushboolean(L, 1);
-    return 1;
-}
-
 static int l_ble_gatt_create(lua_State* L) {
     if (!lua_isuserdata(L, 1)){
         return 0;
     }
 
-    luat_bluetooth_t* luat_bluetooth = (luat_bluetooth_t *)luaL_checkudata(L, 1, LUAT_BLUETOOTH_TYPE);
+    luat_ble_t* luat_ble = (luat_ble_t *)luaL_checkudata(L, 1, LUAT_BLE_TYPE);
     luat_ble_gatt_service_t luat_ble_gatt_service = {0};
     size_t len = 0;
 
@@ -209,7 +189,7 @@ static int l_ble_gatt_create(lua_State* L) {
         }
         lua_pop(L, 1);
     }
-    luat_ble_create_gatt(luat_bluetooth, &luat_ble_gatt_service);
+    luat_ble_create_gatt(luat_ble, &luat_ble_gatt_service);
     for (size_t i = 0; i < luat_ble_gatt_service.characteristics_num; i++){
         lua_pushinteger(L, luat_ble_gatt_service.characteristics[i].handle);
     }
@@ -227,7 +207,7 @@ static int l_ble_advertising_create(lua_State* L) {
         LLOGE("error param");
         return 0;
     }
-    luat_bluetooth_t* luat_bluetooth = (luat_bluetooth_t *)luaL_checkudata(L, 1, LUAT_BLUETOOTH_TYPE);
+    luat_ble_t* luat_ble = (luat_ble_t *)luaL_checkudata(L, 1, LUAT_BLE_TYPE);
     size_t len = 0;
     uint8_t local_name_set_flag = 0;
     const char complete_local_name[32] = {0};
@@ -263,7 +243,7 @@ static int l_ble_advertising_create(lua_State* L) {
     }
     lua_pop(L, 1);
 
-    luat_ble_create_advertising(luat_bluetooth, &luat_ble_adv_cfg);
+    luat_ble_create_advertising(luat_ble, &luat_ble_adv_cfg);
 
     // 广播内容 (adv data)
     uint8_t adv_data[255] = {0};
@@ -284,7 +264,7 @@ static int l_ble_advertising_create(lua_State* L) {
                         uint8_t adv_type = (uint8_t)luaL_checknumber(L, -1);
                         adv_data[adv_index++] = adv_type;
                         if (adv_type == LUAT_ADV_TYPE_COMPLETE_LOCAL_NAME){
-                            luat_ble_set_name(luat_bluetooth, data, len);
+                            luat_ble_set_name(luat_ble, data, len);
                             local_name_set_flag = 1;
                         }
                     }else{
@@ -309,11 +289,11 @@ static int l_ble_advertising_create(lua_State* L) {
 
     if (!local_name_set_flag){
         sprintf_(complete_local_name, "LuatOS_%s", luat_os_bsp());
-        luat_ble_set_name(luat_bluetooth, complete_local_name, strlen(complete_local_name));
+        luat_ble_set_name(luat_ble, complete_local_name, strlen(complete_local_name));
     }
 
     /* set adv paramters */
-    luat_ble_set_adv_data(luat_bluetooth, adv_data, adv_index);
+    luat_ble_set_adv_data(luat_ble, adv_data, adv_index);
 
     lua_pushboolean(L, 1);
     return 1;
@@ -322,34 +302,22 @@ end:
 }
 
 static int l_ble_advertising_start(lua_State* L) {
-    luat_bluetooth_t* luat_bluetooth = (luat_bluetooth_t *)luaL_checkudata(L, 1, LUAT_BLUETOOTH_TYPE);
-    lua_pushboolean(L, luat_ble_start_advertising(luat_bluetooth)?0:1);
+    luat_ble_t* luat_ble = (luat_ble_t *)luaL_checkudata(L, 1, LUAT_BLE_TYPE);
+    lua_pushboolean(L, luat_ble_start_advertising(luat_ble)?0:1);
     return 1;
 }
 
 static int l_ble_advertising_stop(lua_State* L) {
-    luat_bluetooth_t* luat_bluetooth = (luat_bluetooth_t *)luaL_checkudata(L, 1, LUAT_BLUETOOTH_TYPE);
-    lua_pushboolean(L, luat_ble_stop_advertising(luat_bluetooth)?0:1);
+    luat_ble_t* luat_ble = (luat_ble_t *)luaL_checkudata(L, 1, LUAT_BLE_TYPE);
+    lua_pushboolean(L, luat_ble_stop_advertising(luat_ble)?0:1);
     return 1;
 }
 
-static int l_bluetooth_init(lua_State* L) {
-    luat_bluetooth_t* luat_bluetooth = (luat_bluetooth_t*)lua_newuserdata(L, sizeof(luat_bluetooth_t));
-    if (luat_bluetooth) {
-        luat_bluetooth_init(luat_bluetooth);
-        luaL_setmetatable(L, LUAT_BLUETOOTH_TYPE);
-        lua_pushvalue(L, -1);
-        luat_bluetooth->bluetooth_ref = luaL_ref(L, LUA_REGISTRYINDEX);
-        return 1;
-    }
-    return 0;
-}
-
 static int l_ble_read_response(lua_State* L) {
-    luat_bluetooth_t* luat_bluetooth = (luat_bluetooth_t *)luaL_checkudata(L, 1, LUAT_BLUETOOTH_TYPE);
+    luat_ble_t* luat_ble = (luat_ble_t *)luaL_checkudata(L, 1, LUAT_BLE_TYPE);
     uint8_t conn_idx = 0;
-    uint16_t service_id, att_idx = 0;
-    if (luat_bluetooth) {
+    uint16_t service_id, handle = 0;
+    if (luat_ble) {
         lua_pushstring(L, "conn_idx");
         if (LUA_TNUMBER == lua_gettable(L, 2)) {
             conn_idx = luaL_checknumber(L, -1);
@@ -364,9 +332,9 @@ static int l_ble_read_response(lua_State* L) {
             goto end_error;
         }
         lua_pop(L, 1);
-        lua_pushstring(L, "att_idx");
+        lua_pushstring(L, "handle");
         if (LUA_TNUMBER == lua_gettable(L, 2)) {
-            att_idx = luaL_checknumber(L, -1);
+            handle = luaL_checknumber(L, -1);
         }else{
             goto end_error;
         }
@@ -374,8 +342,8 @@ static int l_ble_read_response(lua_State* L) {
 
         size_t len = 0;
         const char* response_data = luaL_checklstring(L, -1, &len);
-        LLOGD("read response conn_idx:%d service_id:%d att_idx:%d", conn_idx, service_id, att_idx);
-        luat_ble_read_response(luat_bluetooth, conn_idx, service_id-1, att_idx-1, len, (uint8_t *)response_data);
+        // LLOGD("read response conn_idx:%d service_id:%d handle:%d", conn_idx, service_id, handle);
+        luat_ble_read_response(luat_ble, conn_idx, service_id, handle, len, (uint8_t *)response_data);
 
         return 1;
     }
@@ -384,19 +352,17 @@ end_error:
     return 0;
 }
 
-static int _bluetooth_struct_newindex(lua_State *L);
+static int _ble_struct_newindex(lua_State *L);
 
-void luat_bluetooth_struct_init(lua_State *L) {
-    luaL_newmetatable(L, LUAT_BLUETOOTH_TYPE);
-    lua_pushcfunction(L, _bluetooth_struct_newindex);
+void luat_ble_struct_init(lua_State *L) {
+    luaL_newmetatable(L, LUAT_BLE_TYPE);
+    lua_pushcfunction(L, _ble_struct_newindex);
     lua_setfield( L, -2, "__index" );
     lua_pop(L, 1);
 }
 
 #include "rotable2.h"
-static const rotable_Reg_t reg_bluetooth[] = {
-	{"init",                        ROREG_FUNC(l_bluetooth_init)},
-    {"ble",                         ROREG_FUNC(l_bluetooth_create_ble)},
+static const rotable_Reg_t reg_ble[] = {
     {"gatt_create",                 ROREG_FUNC(l_ble_gatt_create)},
     {"adv_create",                  ROREG_FUNC(l_ble_advertising_create)},
     {"adv_start",                   ROREG_FUNC(l_ble_advertising_start)},
@@ -445,8 +411,8 @@ static const rotable_Reg_t reg_bluetooth[] = {
 	{ NULL,                         ROREG_INT(0)}
 };
 
-static int _bluetooth_struct_newindex(lua_State *L) {
-	const rotable_Reg_t* reg = reg_bluetooth;
+static int _ble_struct_newindex(lua_State *L) {
+	const rotable_Reg_t* reg = reg_ble;
     const char* key = luaL_checkstring(L, 2);
 	while (1) {
 		if (reg->name == NULL)
@@ -459,10 +425,8 @@ static int _bluetooth_struct_newindex(lua_State *L) {
 	}
 }
 
-LUAMOD_API int luaopen_bluetooth( lua_State *L ) {
-    rotable2_newlib(L, reg_bluetooth);
-    luat_bluetooth_struct_init(L);
-    lua_pushvalue(L, -1);
-    lua_setglobal(L, "ble");
+LUAMOD_API int luaopen_ble( lua_State *L ) {
+    rotable2_newlib(L, reg_ble);
+    luat_ble_struct_init(L);
     return 1;
 }

+ 83 - 0
components/bluetooth/src/luat_lib_bluetooth.c

@@ -0,0 +1,83 @@
+#include "luat_base.h"
+#include "luat_mem.h"
+#include "luat_rtos.h"
+#include "luat_msgbus.h"
+#include "luat_bluetooth.h"
+#include "luat_ble.h"
+
+#include "luat_log.h"
+#define LUAT_LOG_TAG "bluetooth"
+
+extern void luat_ble_cb(luat_ble_t* luat_ble, luat_ble_event_t ble_event, luat_ble_param_t* ble_param);
+
+static int l_bluetooth_create_ble(lua_State* L) {
+    if (!lua_isuserdata(L, 1)){
+        return 0;
+    }
+    luat_bluetooth_t* luat_bluetooth = (luat_bluetooth_t *)luaL_checkudata(L, 1, LUAT_BLUETOOTH_TYPE);
+
+    luat_bluetooth->luat_ble = (luat_ble_t*)lua_newuserdata(L, sizeof(luat_ble_t));
+
+    luat_ble_init(luat_bluetooth->luat_ble, luat_ble_cb);
+
+    if (lua_isfunction(L, 2)) {
+		lua_pushvalue(L, 2);
+		luat_bluetooth->luat_ble->lua_cb = luaL_ref(L, LUA_REGISTRYINDEX);
+	}else{
+        LLOGE("error cb");
+        return 0;
+    }
+
+    luaL_setmetatable(L, LUAT_BLE_TYPE);
+    lua_pushvalue(L, -1);
+    luat_bluetooth->luat_ble->ble_ref = luaL_ref(L, LUA_REGISTRYINDEX);
+    return 1;
+}
+
+static int l_bluetooth_init(lua_State* L) {
+    luat_bluetooth_t* luat_bluetooth = (luat_bluetooth_t*)lua_newuserdata(L, sizeof(luat_bluetooth_t));
+    if (luat_bluetooth) {
+        luat_bluetooth_init(luat_bluetooth);
+        luaL_setmetatable(L, LUAT_BLUETOOTH_TYPE);
+        lua_pushvalue(L, -1);
+        luat_bluetooth->bluetooth_ref = luaL_ref(L, LUA_REGISTRYINDEX);
+        return 1;
+    }
+    return 0;
+}
+
+static int _bluetooth_struct_newindex(lua_State *L);
+
+void luat_bluetooth_struct_init(lua_State *L) {
+    luaL_newmetatable(L, LUAT_BLUETOOTH_TYPE);
+    lua_pushcfunction(L, _bluetooth_struct_newindex);
+    lua_setfield( L, -2, "__index" );
+    lua_pop(L, 1);
+}
+
+#include "rotable2.h"
+static const rotable_Reg_t reg_bluetooth[] = {
+	{"init",                        ROREG_FUNC(l_bluetooth_init)},
+    {"ble",                         ROREG_FUNC(l_bluetooth_create_ble)},
+	{ NULL,                         ROREG_INT(0)}
+};
+
+static int _bluetooth_struct_newindex(lua_State *L) {
+	const rotable_Reg_t* reg = reg_bluetooth;
+    const char* key = luaL_checkstring(L, 2);
+	while (1) {
+		if (reg->name == NULL)
+			return 0;
+		if (!strcmp(reg->name, key)) {
+			lua_pushcfunction(L, reg->value.value.func);
+			return 1;
+		}
+		reg ++;
+	}
+}
+
+LUAMOD_API int luaopen_bluetooth( lua_State *L ) {
+    rotable2_newlib(L, reg_bluetooth);
+    luat_bluetooth_struct_init(L);
+    return 1;
+}

+ 2 - 0
luat/include/luat_libs.h

@@ -217,6 +217,8 @@ LUAMOD_API int luaopen_icmp( lua_State *L );
 
 // bluetooth
 LUAMOD_API int luaopen_bluetooth( lua_State *L );
+// ble
+LUAMOD_API int luaopen_ble( lua_State *L );
 
 // modbus
 LUAMOD_API int luaopen_modbus( lua_State *L );