Browse Source

update:ble uuid支持转换

Dozingfiretruck 10 months ago
parent
commit
4c3bea7d9d

+ 19 - 0
components/bluetooth/luat_bluetooth.c

@@ -6,6 +6,25 @@
 #define LUAT_LOG_TAG "bluetooth"
 
 
+int luat_ble_uuid_swap(uint8_t* uuid_data, luat_ble_uuid_type uuid_type){
+    uint8_t len = 0;
+    if(uuid_type == LUAT_BLE_UUID_TYPE_16){
+        len = 2;
+    }else if(uuid_type == LUAT_BLE_UUID_TYPE_32){
+        len = 4;
+    }else if(uuid_type == LUAT_BLE_UUID_TYPE_128){
+        len = 16;
+    }else{
+        return -1;
+    }
+    for(int i=0;i<len/2;i++){
+        uint8_t tmp = uuid_data[i];
+        uuid_data[i] = uuid_data[len-1-i];
+        uuid_data[len-1-i] = tmp;
+    }
+    return 0;
+}
+
 
 
 

+ 30 - 10
components/bluetooth/luat_bluetooth.h

@@ -26,15 +26,6 @@ typedef enum{
     // BLE_ACTV_PER_SYNC_STARTED,
 } luat_ble_actv_state;
 
-#define ADV_TYPE_FLAGS                      (0x01)
-#define ADV_TYPE_LOCAL_NAME                 (0x09)
-#define ADV_TYPE_SERVICE_UUIDS_16BIT        (0x14)
-#define ADV_TYPE_SERVICE_DATA               (0x16)
-#define ADV_TYPE_MANUFACTURER_SPECIFIC      (0xFF)
-
-#define LUAT_BLE_GATT_UUID_16                       (0x01 << 0)
-#define LUAT_BLE_GATT_UUID_32                       (0x01 << 1)
-#define LUAT_BLE_GATT_UUID_128                      (0x01 << 2)
 // Permission
 #define LUAT_BLE_GATT_PERM_READ                     (0x01 << 3) // Read
 #define LUAT_BLE_GATT_PERM_WRITE                    (0x01 << 4) // Write
@@ -95,6 +86,20 @@ typedef enum{
     LUAT_ADV_TYPE_MANUFACTURER_SPECIFIC_DATA    = 0xFF,
 } luat_ble_adv_type_t;
 
+// GATT SERVICE UUIDS 0X1800 - 0X185B
+#define LUAT_BLE_GATT_SERVICE_MIN   0X1800
+#define LUAT_BLE_GATT_SERVICE_MIX   0X185B
+
+// Descriptors UUIDS 0X2900 - 0X2915
+#define LUAT_BLE_GATT_DESC_MIN      0X2900
+#define LUAT_BLE_GATT_DESC_MIX      0X2915
+
+// Declarations
+#define LUAT_BLE_PRIMARY_SERVICE    0x2800
+#define LUAT_BLE_SECONDARY_SERVICE  0x2801
+#define LUAT_BLE_INCLUDE            0x2802
+#define LUAT_BLE_CHARACTERISTIC     0x2803
+
 typedef enum{
     LUAT_BLE_EVENT_NONE,
     // BLE
@@ -120,6 +125,11 @@ typedef enum{
 
 } luat_ble_event_t;
 
+typedef enum{
+    LUAT_BLE_UUID_TYPE_16,
+    LUAT_BLE_UUID_TYPE_32,
+    LUAT_BLE_UUID_TYPE_128,
+}luat_ble_uuid_type;
 
 typedef struct luat_bluetooth luat_bluetooth_t;
 
@@ -128,6 +138,7 @@ typedef struct{
     uint8_t peer_addr_type; /**< Peer address type */
     uint8_t peer_addr[6];   /**< Peer BT address */
 } luat_ble_device_info_t;
+
 typedef struct{
     uint8_t conn_idx;       /**< The index of the connection */
     uint16_t prf_id;        /**< The id of the profile */
@@ -164,14 +175,22 @@ typedef void (*luat_ble_cb_t)(luat_bluetooth_t* luat_bluetooth, luat_ble_event_t
 
 typedef struct{
     uint8_t uuid[16];
+    luat_ble_uuid_type uuid_type;
+} luat_ble_desc_t;
+
+typedef struct{
+    uint16_t att_idx;
+    uint8_t uuid[16];
+    luat_ble_uuid_type uuid_type;
     uint16_t perm;
-    uint16_t ext_perm;
     uint16_t max_size;
+    luat_ble_desc_t* descriptors;
 } luat_ble_att_db_t;
 
 typedef struct {
     uint16_t prf_id;
     uint8_t uuid[16];
+    luat_ble_uuid_type uuid_type;
     luat_ble_att_db_t* att_db;  // attribute database
     uint8_t att_db_num;         // number of attributes database
 }luat_ble_gatt_cfg_t;
@@ -215,6 +234,7 @@ typedef struct luat_bluetooth{
     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);
 

+ 50 - 35
components/bluetooth/luat_lib_bluetooth.c

@@ -83,24 +83,24 @@ static int luatos_ble_callback(lua_State *L, void* ptr){
 }
 
 static void luat_ble_cb(luat_bluetooth_t* luat_bluetooth, 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){
-        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_event == LUAT_BLE_EVENT_READ) && ble_param->write_req.len){
-            luat_ble_param->write_req.value = luat_heap_malloc(ble_param->write_req.len);
-            memcpy(luat_ble_param->write_req.value, ble_param->write_req.value, ble_param->write_req.len);
-        }
-    }
+    LLOGD("ble event: %d", ble_event);
+    // luat_ble_param_t* luat_ble_param = NULL;
+    // if (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_event == LUAT_BLE_EVENT_READ) && ble_param->write_req.len){
+    //         luat_ble_param->write_req.value = luat_heap_malloc(ble_param->write_req.len);
+    //         memcpy(luat_ble_param->write_req.value, ble_param->write_req.value, ble_param->write_req.len);
+    //     }
+    // }
     
-    rtos_msg_t msg = {
-        .handler = luatos_ble_callback,
-        .ptr = (void*)luat_bluetooth,
-        .arg1 = (int)ble_event,
-        .arg2 = (int)luat_ble_param,
-    };
-    luat_msgbus_put(&msg, 0);
+    // rtos_msg_t msg = {
+    //     .handler = luatos_ble_callback,
+    //     .ptr = (void*)luat_bluetooth,
+    //     .arg1 = (int)ble_event,
+    //     .arg2 = (int)luat_ble_param,
+    // };
+    // luat_msgbus_put(&msg, 0);
 }
 
 static int l_bluetooth_create_ble(lua_State* L) {
@@ -138,8 +138,16 @@ static int l_ble_gatt_create(lua_State* L) {
         memset(luat_ble_gatt_cfg.uuid, 0x00, sizeof(luat_ble_gatt_cfg.uuid));
         lua_pushstring(L, "uuid");
         if (LUA_TSTRING == lua_gettable(L, m)){
-            const char* data = luaL_checklstring(L, -1, &len);
-            memcpy(luat_ble_gatt_cfg.uuid, data, len);
+            const char* uuid_data = luaL_checklstring(L, -1, &len);
+            if (len == 2){
+                luat_ble_gatt_cfg.uuid_type = LUAT_BLE_UUID_TYPE_16;
+            }else if (len == 4){
+                luat_ble_gatt_cfg.uuid_type = LUAT_BLE_UUID_TYPE_32;
+            }else if (len == 16){
+                luat_ble_gatt_cfg.uuid_type = LUAT_BLE_UUID_TYPE_128;
+            }
+            luat_ble_uuid_swap(uuid_data, luat_ble_gatt_cfg.uuid_type);
+            memcpy(luat_ble_gatt_cfg.uuid, uuid_data, len);
         }
         lua_pop(L, 1);
         
@@ -154,13 +162,19 @@ static int l_ble_gatt_create(lua_State* L) {
                 for (int j = 1; j <= table_len; j++){
                     lua_rawgeti(L, -1, j);
                     if (j == 1 && lua_type(L, -1) == LUA_TSTRING){
-                        const char* data = luaL_checklstring(L, -1, &len);
-                        memcpy(luat_ble_gatt_cfg.att_db[i-1].uuid, data, len);
+                        const char* uuid_data = luaL_checklstring(L, -1, &len);
+                        if (len == 2){
+                            luat_ble_gatt_cfg.att_db[i-1].uuid_type = LUAT_BLE_UUID_TYPE_16;
+                        }else if (len == 4){
+                            luat_ble_gatt_cfg.att_db[i-1].uuid_type = LUAT_BLE_UUID_TYPE_32;
+                        }else if (len == 16){
+                            luat_ble_gatt_cfg.att_db[i-1].uuid_type = LUAT_BLE_UUID_TYPE_128;
+                        }
+                        luat_ble_uuid_swap(uuid_data, luat_ble_gatt_cfg.att_db[i-1].uuid_type);
+                        memcpy(luat_ble_gatt_cfg.att_db[i-1].uuid, uuid_data, len);
                     }else if(j == 2 && lua_type(L, -1) == LUA_TNUMBER){
                         luat_ble_gatt_cfg.att_db[i-1].perm = (uint16_t)luaL_optnumber(L, -1, 0);
                     }else if(j == 3 && lua_type(L, -1) == LUA_TNUMBER){
-                        luat_ble_gatt_cfg.att_db[i-1].ext_perm = (uint16_t)luaL_optnumber(L, -1, 0);
-                    }else if(j == 4 && lua_type(L, -1) == LUA_TNUMBER){
                         luat_ble_gatt_cfg.att_db[i-1].max_size = (uint16_t)luaL_optnumber(L, -1, 256);
                     }else{
                         LLOGE("error att_db type");
@@ -195,16 +209,8 @@ static int l_ble_advertising_create(lua_State* L) {
     }
     luat_bluetooth_t* luat_bluetooth = (luat_bluetooth_t *)luaL_checkudata(L, 1, LUAT_BLUETOOTH_TYPE);
     size_t len = 0;
-    // const char* complete_local_name = NULL;
-    // lua_pushstring(L, "name");
-    // if (LUA_TSTRING == lua_gettable(L, 2)) {
-    //     complete_local_name = luaL_checklstring(L, -1, &len);
-    // }else{
-    //     complete_local_name = (char* )luat_heap_malloc(32);
-    //     memset(complete_local_name, 0, 32);
-    //     sprintf_(complete_local_name, "LuatOS_%s", luat_os_bsp());
-    // }
-    // lua_pop(L, 1);
+    uint8_t local_name_set_flag = 0;
+    const char complete_local_name[32] = {0};
 
     luat_ble_adv_cfg_t luat_ble_adv_cfg = {
         .addr_mode = LUAT_BLE_ADV_ADDR_MODE_PUBLIC,
@@ -255,7 +261,12 @@ static int l_ble_advertising_create(lua_State* L) {
                     adv_data[adv_index++] = (uint8_t)(len+1);
                     lua_rawgeti(L, -2, 1);
                     if (lua_type(L, -1) == LUA_TNUMBER){
-                        adv_data[adv_index++] = (uint8_t)luaL_checknumber(L, -1);
+                        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);
+                            local_name_set_flag = 1;
+                        }
                     }else{
                         LLOGE("error adv_data type");
                         goto end;
@@ -276,9 +287,13 @@ static int l_ble_advertising_create(lua_State* L) {
     }
     lua_pop(L, 1);
 
+    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));
+    }
+
     /* set adv paramters */
     luat_ble_set_adv_data(luat_bluetooth, adv_data, adv_index);
-    // luat_ble_set_name(luat_bluetooth, complete_local_name, strlen(complete_local_name));
 
     lua_pushboolean(L, 1);
     return 1;