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

Merge branch 'master' of https://gitee.com/openLuat/LuatOS

??? 9 месяцев назад
Родитель
Сommit
189faea306

+ 4 - 12
components/bluetooth/src/luat_lib_ble.c

@@ -109,23 +109,15 @@ int l_ble_callback(lua_State *L, void *ptr)
         lua_pushinteger(L, read_req->handle);
         lua_settable(L, -3);
 
-        luat_ble_uuid_t uuid_service = {0};
-        luat_ble_uuid_t uuid_characteristic = {0};
-        luat_ble_uuid_t uuid_descriptor = {0};
-        luat_ble_handle2uuid(read_req->handle, &uuid_service, &uuid_characteristic, &uuid_descriptor);
-        // LLOGD("service:0x%02X %d characteristic:0x%02X %d descriptor:0x%02X %d",
-        //     uuid_service.uuid[0]<<8|uuid_service.uuid[1],uuid_service.uuid_type,
-        //     uuid_characteristic.uuid[0]<<8|uuid_characteristic.uuid[1],uuid_characteristic.uuid_type,
-        //     uuid_descriptor.uuid[0]<<8|uuid_descriptor.uuid[1],uuid_descriptor.uuid_type);
         lua_pushliteral(L, "uuid_service");
-        lua_pushlstring(L, (const char *)uuid_service.uuid, uuid_service.uuid_type);
+        lua_pushlstring(L, (const char *)read_req->uuid_service.uuid, read_req->uuid_service.uuid_type);
         lua_settable(L, -3);
         lua_pushliteral(L, "uuid_characteristic");
-        lua_pushlstring(L, (const char *)uuid_characteristic.uuid, uuid_characteristic.uuid_type);
+        lua_pushlstring(L, (const char *)read_req->uuid_characteristic.uuid, read_req->uuid_characteristic.uuid_type);
         lua_settable(L, -3);
-        if (uuid_descriptor.uuid[0] != 0 || uuid_descriptor.uuid[1] != 0){
+        if (read_req->uuid_descriptor.uuid[0] != 0 || read_req->uuid_descriptor.uuid[1] != 0){
             lua_pushliteral(L, "uuid_descriptor");
-            lua_pushlstring(L, (const char *)uuid_descriptor.uuid, uuid_descriptor.uuid_type);
+            lua_pushlstring(L, (const char *)read_req->uuid_descriptor.uuid, read_req->uuid_descriptor.uuid_type);
             lua_settable(L, -3);
         }
         if (evt == LUAT_BLE_EVENT_READ_VALUE){

+ 40 - 16
luat/demo/bluetooth/ble/main.lua

@@ -8,30 +8,32 @@ sys = require("sys")
 
 log.info("main", "project name is ", PROJECT, "version is ", VERSION)
 
--- characteristic handle
-local characteristic1,characteristic2,characteristic3,characteristic4
-
 local att_db = {--Service
         string.fromHex("FA00"),             --Service UUID
         -- Characteristic
         { -- Characteristic 1
-            string.fromHex("EA01"),         -- Characteristic UUID Value
+            0xEA01,                         -- Characteristic UUID Value
             ble.NOTIFY|ble.READ|ble.WRITE,  -- Properties
+            string.fromHex("1234")          -- Value
+
         },
         { -- Characteristic 2
-            string.fromHex("EA02"),
+            0xEA02,
             ble.WRITE,
         },
         { -- Characteristic 3
-            string.fromHex("EA03"),
+            0xEA03,
             ble.READ,
+            string.fromHex("5678")
         },
         { -- Characteristic 4
-            string.fromHex("EA04"),
-            ble.READ|ble.WRITE,
+            0xEA04,
+            ble.NOTIFY|ble.READ|ble.WRITE,
         },
     }
 
+local scan_count = 0
+
 local function ble_callback(ble_device, ble_event, ble_param)
     if ble_event == ble.EVENT_CONN then
         log.info("ble", "connect 成功")
@@ -40,10 +42,28 @@ local function ble_callback(ble_device, ble_event, ble_param)
         -- 1秒后重新开始广播
         sys.timerStart(function() ble_device:adv_start() end, 1000)
     elseif ble_event == ble.EVENT_WRITE then
-        log.info("ble", "write", ble_param.conn_idx,ble_param.service_id,ble_param.handle,ble_param.data:toHex())
-    elseif ble_event == ble.EVENT_READ then
-        log.info("ble", "read", ble_param.conn_idx,ble_param.service_id,ble_param.handle)
-        ble_device:read_response(ble_param,string.fromHex("1234"))
+        log.info("ble", "write", ble_param.handle,ble_param.uuid_service:toHex(),ble_param.uuid_characteristic:toHex())
+        log.info("ble", "data", ble_param.data:toHex())
+        -- ble_device:write_notify(ble_param,string.fromHex("123456"))
+    elseif ble_event == ble.EVENT_READ_VALUE then
+        log.info("ble", "read", ble_param.handle,ble_param.uuid_service:toHex(),ble_param.uuid_characteristic:toHex(),ble_param.data:toHex(),ble_param.data)
+    elseif ble_event == ble.EVENT_SCAN_REPORT then
+        print("ble scan report",ble_param.addr_type,ble_param.rssi,ble_param.adv_addr:toHex(),ble_param.data:toHex(),ble_param.data)
+        scan_count = scan_count + 1
+        if scan_count > 20 then
+            ble_device:scan_stop()
+        end
+        if ble_param.addr_type == 0 and ble_param.data:find("LuatOS") then
+            ble_device:scan_stop()
+            ble_device:connect(ble_param.adv_addr,ble_param.addr_type)
+        end
+    elseif ble_event == 14 then
+
+        local characteristic = {uuid_service = string.fromHex("FA00"), uuid_characteristic = string.fromHex("EA02")}
+        ble_device:write_value(characteristic,string.fromHex("1234"))
+
+        local characteristic = {uuid_service = string.fromHex("FA00"), uuid_characteristic = string.fromHex("EA03")}
+        ble_device:read_value(characteristic)
     end
 end
 
@@ -54,9 +74,9 @@ sys.taskInit(function()
     log.info("初始化BLE功能")
     ble_device = bluetooth_device:ble(ble_callback)
 
+    -- slaver
     log.info('开始创建GATT')
-    characteristic1,characteristic2,characteristic3,characteristic4 = ble_device:gatt_create(att_db)
-    log.info("创建的GATT为",characteristic1,characteristic2,characteristic3,characteristic4)
+    ble_device:gatt_create(att_db)
 
     log.info("开始设置广播内容")
     ble_device:adv_create({
@@ -69,13 +89,17 @@ sys.taskInit(function()
             {ble.COMPLETE_LOCAL_NAME, "LuatOS"},
             {ble.SERVICE_DATA, string.fromHex("FE01")},
             {ble.MANUFACTURER_SPECIFIC_DATA, string.fromHex("05F0")},
-        }
+        },
     })
-
     log.info("开始广播")
     ble_device:adv_start()
     -- ble_device:adv_stop()
 
+    -- master
+    -- ble_device:scan_create({})
+    -- ble_device:scan_start()
+    -- -- ble_device:scan_stop()
+
     while 1 do
         sys.wait(1000)
     end

BIN
module/Air780EHM/core/LuatOS-SoC_V2007_Air780EHM.soc


BIN
module/Air780EPM/core/LuatOS-SoC_V2007_Air780EPM.soc