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

add: nimble库支持设置UUID,支持从机模式的扫描阶段

Wendal Chen 3 лет назад
Родитель
Сommit
3939d53214

+ 78 - 1
components/nimble/src/luat_lib_nimble.c

@@ -41,6 +41,8 @@ end)
 
 #include "luat_nimble.h"
 
+#include "host/ble_gatt.h"
+
 #define LUAT_LOG_TAG "nimble"
 #include "luat_log.h"
 
@@ -48,6 +50,16 @@ static uint32_t nimble_mode = 0;
 uint16_t g_ble_state;
 uint16_t g_ble_conn_handle;
 
+// peripheral, 被扫描, 被连接设备的UUID配置
+ble_uuid_any_t ble_peripheral_srv_uuid;
+ble_uuid_any_t ble_peripheral_indicate_uuid;
+ble_uuid_any_t ble_peripheral_write_uuid;
+
+#define WM_GATT_SVC_UUID      0x180D
+// #define WM_GATT_SVC_UUID      0xFFF0
+#define WM_GATT_INDICATE_UUID 0xFFF1
+#define WM_GATT_WRITE_UUID    0xFFF2
+// #define WM_GATT_NOTIFY_UUID    0xFFF3
 
 /*
 初始化BLE上下文,开始对外广播/扫描
@@ -64,7 +76,7 @@ static int l_nimble_init(lua_State* L) {
     if(lua_isstring(L, 1)) {
         name = luaL_checklstring(L, 1, &len);
     }
-    LLOGD("init name %s mode %d", name, nimble_mode);
+    LLOGD("init name %s mode %d", name == NULL ? "-" : name, nimble_mode);
     rc = luat_nimble_init(0xFF, name, nimble_mode);
     if (rc) {
         lua_pushboolean(L, 0);
@@ -147,6 +159,13 @@ static int l_nimble_send_msg(lua_State *L) {
     return 1;
 }
 
+/*
+扫描从机
+@api nimble.scan()
+@return bool 成功与否
+@usage
+-- 参考 demo/nimble
+*/
 static int l_nimble_scan(lua_State *L) {
     int ret = luat_nimble_blecent_scan();
     lua_pushboolean(L, ret == 0 ? 1 : 0);
@@ -154,6 +173,16 @@ static int l_nimble_scan(lua_State *L) {
     return 1;
 }
 
+/*
+设置模式
+@api nimble.mode(tp)
+@int 模式, 默认server/peripheral, 可选 client/central模式 nimble.MODE_BLE_CLIENT
+@return bool 成功与否
+@usage
+-- 参考 demo/nimble
+-- 必须在nimble.init()之前调用
+-- nimble.mode(nimble.MODE_BLE_CLIENT) -- 简称从机模式,未完善
+*/
 static int l_nimble_mode(lua_State *L) {
     if (lua_isinteger(L, 1)) {
         nimble_mode = lua_tointeger(L, 1);
@@ -176,6 +205,50 @@ static int l_nimble_connok(lua_State *L) {
     return 1;
 }
 
+/*
+设置server/peripheral的UUID
+@api nimble.setUUID(tp, addr)
+@string 配置字符串,后面的示例有说明
+@string 地址字符串
+@return bool 成功与否
+@usage
+-- 参考 demo/nimble, 2023-02-25之后编译的固件支持本API
+-- 必须在nimble.init()之前调用
+-- 设置SERVER/Peripheral模式下的UUID, 支持设置3个
+-- 地址支持 2/4/16字节, 需要二进制数据
+-- 2字节地址示例: AABB, 写 string.fromHex("AABB") ,或者 string.char(0xAA, 0xBB)
+-- 4字节地址示例: AABBCCDD , 写 string.fromHex("AABBCCDD") ,或者 string.char(0xAA, 0xBB, 0xCC, 0xDD)
+nimble.setUUID("srv", string.fromHex("380D"))      -- 服务主UUID         ,  默认值 180D
+nimble.setUUID("write", string.fromHex("FF31"))    -- 往本设备写数据的UUID,  默认值 FFF1
+nimble.setUUID("indicate", string.fromHex("FF32")) -- 订阅本设备的数据的UUID,默认值 FFF2
+*/
+static int l_nimble_set_uuid(lua_State *L) {
+    size_t len = 0;
+    ble_uuid_any_t tmp = {0};
+    const char* key = luaL_checkstring(L, 1);
+    const char* uuid = luaL_checklstring(L, 2, &len);
+    int ret = ble_uuid_init_from_buf(&tmp, (const void*)uuid, len);
+    if (ret != 0) {
+        LLOGW("invaild UUID, len must be 2/4/16");
+        return 0;
+    }
+    if (!strcmp("srv", key)) {
+        memcpy(&ble_peripheral_srv_uuid, &tmp, sizeof(ble_uuid_any_t));
+    }
+    else if (!strcmp("write", key)) {
+        memcpy(&ble_peripheral_write_uuid, &tmp, sizeof(ble_uuid_any_t));
+    }
+    else if (!strcmp("indicate", key)) {
+        memcpy(&ble_peripheral_indicate_uuid, &tmp, sizeof(ble_uuid_any_t));
+    }
+    else {
+        LLOGW("only support srv/write/indicate");
+        return 0;
+    }
+    lua_pushboolean(L, 1);
+    return 1;
+}
+
 #include "rotable2.h"
 static const rotable_Reg_t reg_nimble[] =
 {
@@ -189,6 +262,7 @@ static const rotable_Reg_t reg_nimble[] =
     { "server_init",    ROREG_FUNC(l_nimble_server_init)},
     { "server_deinit",  ROREG_FUNC(l_nimble_server_deinit)},
     { "send_msg",       ROREG_FUNC(l_nimble_send_msg)},
+    { "setUUID",        ROREG_FUNC(l_nimble_set_uuid)},
 
     // 中心模式, 扫描并连接外设
     { "scan",           ROREG_FUNC(l_nimble_scan)},
@@ -214,6 +288,9 @@ static const rotable_Reg_t reg_nimble[] =
 };
 
 LUAMOD_API int luaopen_nimble( lua_State *L ) {
+    memcpy(&ble_peripheral_srv_uuid, BLE_UUID16_DECLARE(WM_GATT_SVC_UUID), sizeof(ble_uuid16_t));
+    memcpy(&ble_peripheral_write_uuid, BLE_UUID16_DECLARE(WM_GATT_WRITE_UUID), sizeof(ble_uuid16_t));
+    memcpy(&ble_peripheral_indicate_uuid, BLE_UUID16_DECLARE(WM_GATT_INDICATE_UUID), sizeof(ble_uuid16_t));
     rotable2_newlib(L, reg_nimble);
     return 1;
 }

+ 48 - 26
components/nimble/src/luat_nimble_mode_peripheral.c

@@ -58,16 +58,19 @@ static uint16_t g_ble_attr_write_handle;
 extern uint16_t g_ble_conn_handle;
 extern uint16_t g_ble_state;
 
-#define WM_GATT_SVC_UUID      0xFFF0
-#define WM_GATT_INDICATE_UUID 0xFFF1
-#define WM_GATT_WRITE_UUID    0xFFF2
-#define WM_GATT_NOTIFY_UUID    0xFFF3
+// #define WM_GATT_SVC_UUID      0xFFF0
+// #define WM_GATT_INDICATE_UUID 0xFFF1
+// #define WM_GATT_WRITE_UUID    0xFFF2
+// #define WM_GATT_NOTIFY_UUID    0xFFF3
 
+extern ble_uuid_any_t ble_peripheral_srv_uuid;
+extern ble_uuid_any_t ble_peripheral_indicate_uuid;
+extern ble_uuid_any_t ble_peripheral_write_uuid;
 
 #define LUAT_LOG_TAG "nimble"
 #include "luat_log.h"
 
-static char selfname[32];
+static uint8_t ble_use_custom_name;
 // extern uint16_t g_ble_conn_handle;
 
 typedef struct ble_write_msg {
@@ -89,15 +92,17 @@ gatt_svr_chr_access_device_info(uint16_t conn_handle, uint16_t attr_handle,
 static int
 gatt_svr_chr_access_func(uint16_t conn_handle, uint16_t attr_handle,
                                struct ble_gatt_access_ctxt *ctxt, void *arg);
-
-static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
+#if 1
+static struct ble_gatt_svc_def gatt_svr_svcs[] = {
     {
         /* Service: Heart-rate */
         .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid = BLE_UUID16_DECLARE(GATT_HRS_UUID),
+        // .uuid = BLE_UUID16_DECLARE(GATT_HRS_UUID),
+        // .uuid = BLE_UUID16_DECLARE(WM_GATT_SVC_UUID),
+        .uuid = &ble_peripheral_srv_uuid,
         .characteristics = (struct ble_gatt_chr_def[])
         { {
-#if 1
+#if 0
                 /* Characteristic: Heart-rate measurement */
                 .uuid = BLE_UUID16_DECLARE(GATT_HRS_MEASUREMENT_UUID),
                 .access_cb = gatt_svr_chr_access_heart_rate,
@@ -111,13 +116,15 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
             }, {
 #endif
                 /* Characteristic: Body sensor location */
-                .uuid = BLE_UUID16_DECLARE(WM_GATT_WRITE_UUID),
+                // .uuid = BLE_UUID16_DECLARE(WM_GATT_WRITE_UUID),
+                .uuid = &ble_peripheral_write_uuid,
                 .val_handle = &g_ble_attr_write_handle,
                 .access_cb = gatt_svr_chr_access_func,
                 .flags = BLE_GATT_CHR_F_WRITE,
             }, {
                 /* Characteristic: Body sensor location */
-                .uuid = BLE_UUID16_DECLARE(WM_GATT_INDICATE_UUID),
+                // .uuid = BLE_UUID16_DECLARE(WM_GATT_INDICATE_UUID),
+                .uuid = &ble_peripheral_indicate_uuid,
                 .val_handle = &g_ble_attr_indicate_handle,
                 .access_cb = gatt_svr_chr_access_func,
                 .flags = BLE_GATT_CHR_F_INDICATE | BLE_GATT_CHR_F_READ,
@@ -126,7 +133,7 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
             },
         }
     },
-#if 1
+#if 0
     {
         /* Service: Device Information */
         .type = BLE_GATT_SVC_TYPE_PRIMARY,
@@ -152,7 +159,9 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
         0, /* No more services */
     },
 };
+#endif
 
+#if 0
 static int
 gatt_svr_chr_access_heart_rate(uint16_t conn_handle, uint16_t attr_handle,
                                struct ble_gatt_access_ctxt *ctxt, void *arg)
@@ -196,6 +205,7 @@ gatt_svr_chr_access_device_info(uint16_t conn_handle, uint16_t attr_handle,
     assert(0);
     return BLE_ATT_ERR_UNLIKELY;
 }
+#endif
 
 static void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
 {
@@ -311,7 +321,7 @@ int luat_nimble_server_send(int id, char* data, size_t data_len) {
     }
     rc = ble_gattc_indicate_custom(g_ble_conn_handle,g_ble_attr_indicate_handle, om);
     LLOGD("ble_gattc_indicate_custom ret %d", rc);
-    return 0;
+    return rc;
 }
 
 
@@ -448,9 +458,10 @@ bleprph_advertise(void)
     fields.name_len = strlen(name);
     fields.name_is_complete = 1;
 
-    fields.uuids16 = (ble_uuid16_t[]) {
-        BLE_UUID16_INIT(GATT_SVR_SVC_ALERT_UUID)
-    };
+    // fields.uuids16 = (ble_uuid16_t[]) {
+    //     ble_peripheral_srv_uuid
+    // };
+    fields.uuids16 = (const ble_uuid16_t *)&ble_peripheral_srv_uuid;
     fields.num_uuids16 = 1;
     fields.uuids16_is_complete = 1;
 
@@ -690,6 +701,12 @@ bleprph_on_sync(void)
     rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
 
     LLOGI("Device Address: " ADDR_FMT, ADDR_T(addr_val));
+    if (ble_use_custom_name == 0) {
+        char buff[32];
+        sprintf_(buff, "LOS-" ADDR_FMT, ADDR_T(addr_val));
+        LLOGD("BLE name: %s", buff);
+        rc = ble_svc_gap_device_name_set((const char*)buff);
+    }
     ble_gatts_start();
     //print_addr(addr_val);
     // LLOGI("\n");
@@ -710,18 +727,23 @@ int luat_nimble_init_peripheral(uint8_t uart_idx, char* name, int mode) {
     int rc = 0;
     nimble_port_init();
 
-    if (name == NULL || strlen(name) == 0) {
-        if (selfname[0] == 0) {
-            memcpy(selfname, "LuatOS", strlen("LuatOS") + 1);
-        }
-    }
-    else {
-        memcpy(selfname, name, strlen(name) + 1);
+    // if (name == NULL || strlen(name) == 0) {
+    //     if (selfname[0] == 0) {
+    //         memcpy(selfname, "LuatOS", strlen("LuatOS") + 1);
+    //     }
+    // }
+    // else {
+    //     memcpy(selfname, name, strlen(name) + 1);
+    // }
+    char buff[32] = {0};
+    // char mac[6] = {0};
+    /* Set the default device name. */
+    if (name != NULL && strlen(name)) {
+        rc = ble_svc_gap_device_name_set((const char*)name);
+        ble_use_custom_name = 1;
     }
 
-    /* Set the default device name. */
-    if (strlen(selfname))
-        rc = ble_svc_gap_device_name_set((const char*)selfname);
+        
 
 
     /* Initialize the NimBLE host configuration. */

+ 15 - 2
demo/nimble/Air103/main.lua

@@ -39,8 +39,21 @@ end)
 
 sys.taskInit(function()
     sys.wait(2000)
-    nimble.debug(6)
-    nimble.init("LuatOS-Wendal") -- 蓝牙名称可修改,也有默认值LOS-$mac地址
+
+    -- BLE模式, 默认是SERVER/Peripheral,即外设模式, 等待被连接的设
+    -- nimble.mode(nimble.MODE_BLE_SERVER)
+
+    -- 设置SERVER/Peripheral模式下的UUID, 支持设置3个
+    -- 地址支持 2/4/16字节, 需要二进制数据, 例如 string.fromHex("AABB") 返回的是2个字节数据,0xAABB
+    if nimble.setUUID then -- 2023-02-25之后编译的固件支持本API
+        nimble.setUUID("srv", string.fromHex("380D"))      -- 服务主UUID         ,  默认值 180D
+        nimble.setUUID("write", string.fromHex("FF31"))    -- 往本设备写数据的UUID,  默认值 FFF1
+        nimble.setUUID("indicate", string.fromHex("FF32")) -- 订阅本设备的数据的UUID,默认值 FFF2
+    end
+
+    -- nimble.debug(6)
+    -- nimble.init("LuatOS-Wendal") -- 蓝牙名称可修改,也有默认值LOS-$mac地址
+    nimble.init() -- 蓝牙名称可修改,也有默认值LOS-$mac地址
 
     if nimble.send_msg then
         while 1 do