Dozingfiretruck 9 месяцев назад
Родитель
Сommit
aef8677079
2 измененных файлов с 52 добавлено и 48 удалено
  1. 9 5
      components/bluetooth/include/luat_ble.h
  2. 43 43
      components/bluetooth/src/luat_lib_ble.c

+ 9 - 5
components/bluetooth/include/luat_ble.h

@@ -122,9 +122,10 @@ typedef enum{
     LUAT_BLE_EVENT_DISCONN,     // BLE断开连接
     LUAT_BLE_EVENT_GATT_DONE,     // BLE GATT
 
-    // WRITE
-    LUAT_BLE_EVENT_WRITE,       // BLE写数据
-    LUAT_BLE_EVENT_READ,        // BLE读数据
+    // WRITE/READ
+    LUAT_BLE_EVENT_WRITE,         // BLE从模式下,主设备写操作事件
+    LUAT_BLE_EVENT_READ,         // BLE从模式下,主设备读操作事件
+    LUAT_BLE_EVENT_READ_VALUE,      // BLE读value数据回调
 
     LUAT_BLE_EVENT_MAX,
 
@@ -233,8 +234,11 @@ typedef struct{
 
 typedef struct{
     uint16_t handle;       /**< The index of the attribute */
+    luat_ble_uuid_t uuid_service;
+    luat_ble_uuid_t uuid_characteristic;
+    luat_ble_uuid_t uuid_descriptor;
     uint8_t *value;         /**< The attribute value */
-    uint16_t len;           /**< The data length read */
+    uint16_t value_len;           /**< The data length read */
     uint16_t size;          /**< The size of attribute value to read */
 } luat_ble_read_req_t;
 
@@ -278,7 +282,7 @@ typedef struct{
     union {
         luat_ble_device_info_t luat_ble_device_info;
         luat_ble_write_req_t write_req;
-        // luat_ble_read_req_t read_req;
+        luat_ble_read_req_t read_req;
         luat_ble_adv_req_t adv_req;
         luat_ble_conn_ind_t conn_ind;
         luat_ble_disconn_ind_t disconn_ind;

+ 43 - 43
components/bluetooth/src/luat_lib_ble.c

@@ -106,35 +106,42 @@ int l_ble_callback(lua_State *L, void *ptr)
         break;
     }
     case LUAT_BLE_EVENT_READ:
-    {
-        // luat_ble_read_req_t *read_req = &(param->read_req);
-        // lua_createtable(L, 0, 5);
-        // lua_pushliteral(L, "handle");
-        // 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_settable(L, -3);
-        // lua_pushliteral(L, "uuid_characteristic");
-        // lua_pushlstring(L, (const char *)uuid_characteristic.uuid, uuid_characteristic.uuid_type);
-        // lua_settable(L, -3);
-        // if (uuid_descriptor.uuid[0] != 0 || uuid_descriptor.uuid[1] != 0){
-        //     lua_pushliteral(L, "uuid_descriptor");
-        //     lua_pushlstring(L, (const char *)uuid_descriptor.uuid, uuid_descriptor.uuid_type);
-        //     lua_settable(L, -3);
-        // }
-
-        // lua_call(L, 3, 0);
+    case LUAT_BLE_EVENT_READ_VALUE:{
+        luat_ble_read_req_t *read_req = &(param->read_req);
+        lua_createtable(L, 0, 5);
+        lua_pushliteral(L, "handle");
+        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_settable(L, -3);
+        lua_pushliteral(L, "uuid_characteristic");
+        lua_pushlstring(L, (const char *)uuid_characteristic.uuid, uuid_characteristic.uuid_type);
+        lua_settable(L, -3);
+        if (uuid_descriptor.uuid[0] != 0 || uuid_descriptor.uuid[1] != 0){
+            lua_pushliteral(L, "uuid_descriptor");
+            lua_pushlstring(L, (const char *)uuid_descriptor.uuid, uuid_descriptor.uuid_type);
+            lua_settable(L, -3);
+        }
+        if (evt == LUAT_BLE_EVENT_READ_VALUE){
+            lua_pushliteral(L, "data");
+            lua_pushlstring(L, (const char *)read_req->value, read_req->value_len);
+            lua_settable(L, -3);
+        }
+        lua_call(L, 3, 0);
+        if (read_req->value){
+            luat_heap_free(read_req->value);
+            read_req->value = NULL;
+        }
         break;
     }
     case LUAT_BLE_EVENT_SCAN_REPORT:
@@ -227,26 +234,20 @@ exit:
     return 0;
 }
 
-void luat_ble_cb(luat_ble_t *args, luat_ble_event_t ble_event, luat_ble_param_t *ble_param)
-{
+void luat_ble_cb(luat_ble_t *args, luat_ble_event_t ble_event, luat_ble_param_t *ble_param){
     // LLOGD("ble event: %d param: %p", ble_event, ble_param);
     luat_ble_param_t *luat_ble_param = NULL;
-    if (ble_param)
-    {
+    if (ble_param){
         // LLOGD("ble param: %p", ble_param);
         luat_ble_param = luat_heap_malloc(sizeof(luat_ble_param_t));
         memcpy(luat_ble_param, ble_param, sizeof(luat_ble_param_t));
-        if (ble_event == LUAT_BLE_EVENT_WRITE && ble_param->write_req.value_len)
-        {
+        if (ble_event == LUAT_BLE_EVENT_WRITE && ble_param->write_req.value_len){
             luat_ble_param->write_req.value = luat_heap_malloc(ble_param->write_req.value_len);
             memcpy(luat_ble_param->write_req.value, ble_param->write_req.value, ble_param->write_req.value_len);
-        }
-        // else if (ble_event == LUAT_BLE_EVENT_READ && ble_param->read_req.value_len)
-        // {
-        //     LLOGD("ble read read_req value: %p", ble_param->read_req.value);
-        // }
-        else if (ble_event == LUAT_BLE_EVENT_SCAN_REPORT && ble_param->adv_req.data_len)
-        {
+        }else if (ble_event == LUAT_BLE_EVENT_READ_VALUE && ble_param->read_req.value_len){
+            luat_ble_param->read_req.value = luat_heap_malloc(ble_param->read_req.value_len);
+            memcpy(luat_ble_param->read_req.value, ble_param->read_req.value, ble_param->read_req.value_len);
+        }else if (ble_event == LUAT_BLE_EVENT_SCAN_REPORT && ble_param->adv_req.data_len){
             luat_ble_param->adv_req.data = luat_heap_malloc(ble_param->adv_req.data_len);
             memcpy(luat_ble_param->adv_req.data, ble_param->adv_req.data, ble_param->adv_req.data_len);
         }
@@ -1009,9 +1010,8 @@ static const rotable_Reg_t reg_ble[] = {
     {"EVENT_CONN", ROREG_INT(LUAT_BLE_EVENT_CONN)},
     {"EVENT_DISCONN", ROREG_INT(LUAT_BLE_EVENT_DISCONN)},
     {"EVENT_WRITE", ROREG_INT(LUAT_BLE_EVENT_WRITE)},
-    {"EVENT_WRITE_REQ", ROREG_INT(LUAT_BLE_EVENT_WRITE)},
     {"EVENT_READ", ROREG_INT(LUAT_BLE_EVENT_READ)},
-    {"EVENT_READ_REQ", ROREG_INT(LUAT_BLE_EVENT_READ)},
+    {"EVENT_READ_VALUE", ROREG_INT(LUAT_BLE_EVENT_READ_VALUE)},
 
     // ADV_ADDR_MODE
     {"PUBLIC", ROREG_INT(LUAT_BLE_ADDR_MODE_PUBLIC)},