Browse Source

update:完善ble扫描

Dozingfiretruck 7 months ago
parent
commit
f0d66bda37

+ 4 - 1
components/bluetooth/include/luat_ble.h

@@ -163,7 +163,7 @@ typedef struct{
 typedef struct{
     uint8_t actv_idx;     /**< The index of the activity */
     uint8_t evt_type;     /**< Event type (see enum \ref adv_report_info and see enum \ref adv_report_type)*/
-    uint8_t adv_addr_type;/**< Advertising address type: public/random */
+    uint8_t adv_addr_type;  /**< Advertising address type: public/random */
     int8_t rssi;         /**< RSSI value for advertising packet (in dBm, between -127 and +20 dBm) */
     uint8_t *data;        /**< Data of advertising packet */
     uint8_t data_len;     /**< Data length in advertising packet */
@@ -239,6 +239,7 @@ typedef struct {
 }luat_ble_scan_cfg_t;
 
 struct luat_ble{
+    uint8_t actv_idx;
     luat_ble_actv_state state;
     luat_ble_cb_t cb;
     int lua_cb;
@@ -287,6 +288,8 @@ int luat_ble_stop_scanning(luat_ble_t* luat_ble);
 
 int luat_ble_delete_scanning(luat_ble_t* luat_ble);
 
+int luat_ble_connect(luat_ble_t* luat_ble, uint8_t* adv_addr,uint8_t adv_addr_type);
 
+int luat_ble_disconnect(luat_ble_t* luat_ble, uint8_t conn_idx);
 
 #endif

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

@@ -7,6 +7,8 @@
 #define LUAT_BLUETOOTH_TYPE "BLUETOOTH*"
 #define LUAT_BLE_TYPE "BLE*"
 
+#define LUAT_BLUETOOTH_MAC_LEN    6
+
 typedef struct luat_bluetooth{
     luat_ble_t* luat_ble;
     luat_bt_t* luat_bt;
@@ -21,4 +23,6 @@ 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);
 
+void luat_bluetooth_mac_swap(uint8_t* mac);
+
 #endif

+ 1 - 4
components/bluetooth/src/luat_ble.c

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

+ 18 - 0
components/bluetooth/src/luat_bluetooth.c

@@ -0,0 +1,18 @@
+#include "luat_base.h"
+#include "luat_mem.h"
+#include "luat_bluetooth.h"
+
+#include "luat_log.h"
+#define LUAT_LOG_TAG "bluetooth"
+
+void luat_bluetooth_mac_swap(uint8_t* mac){
+    uint8_t len = 0;
+    uint8_t tmp_mac[LUAT_BLUETOOTH_MAC_LEN] = {0};
+    memcpy(tmp_mac, mac, LUAT_BLUETOOTH_MAC_LEN);
+    for(int i=0;i<LUAT_BLUETOOTH_MAC_LEN;i++){
+        mac[i] = tmp_mac[LUAT_BLUETOOTH_MAC_LEN-1-i];
+    }
+}
+
+
+

+ 1 - 1
components/bluetooth/src/luat_bt.c

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

+ 25 - 1
components/bluetooth/src/luat_lib_ble.c

@@ -70,6 +70,9 @@ static int luatos_ble_callback(lua_State *L, void* ptr){
             lua_pushliteral(L, "rssi"); 
             lua_pushinteger(L, adv_req->rssi);
             lua_settable(L, -3);
+            lua_pushliteral(L, "addr_type"); 
+            lua_pushinteger(L, adv_req->adv_addr_type);
+            lua_settable(L, -3);
             lua_pushliteral(L, "adv_addr"); 
             lua_pushlstring(L, (const char *)adv_req->adv_addr, 6);
             lua_settable(L, -3);
@@ -78,7 +81,6 @@ static int luatos_ble_callback(lua_State *L, void* ptr){
             lua_settable(L, -3);
     // uint8_t actv_idx;     /**< The index of the activity */
     // uint8_t evt_type;     /**< Event type (see enum \ref adv_report_info and see enum \ref adv_report_type)*/
-    // uint8_t adv_addr_type;/**< Advertising address type: public/random */
 
             lua_call(L, 3, 0);
             if (adv_req->data){
@@ -452,6 +454,25 @@ static int l_ble_scanning_stop(lua_State* L) {
     return 1;
 }
 
+static int l_ble_connect(lua_State* L) {
+    luat_ble_t* luat_ble = (luat_ble_t *)luaL_checkudata(L, 1, LUAT_BLE_TYPE);
+    size_t len;
+    uint8_t* adv_addr = luaL_checklstring(L, 2, &len);
+    uint8_t adv_addr_type = luaL_checknumber(L, 3);
+    LLOGD(" adv_addr_type:%d, adv_addr:%02x:%02x:%02x:%02x:%02x:%02x",
+        adv_addr_type, adv_addr[0], adv_addr[1], adv_addr[2],
+        adv_addr[3], adv_addr[4], adv_addr[5]);
+    lua_pushboolean(L, luat_ble_connect(luat_ble, adv_addr, adv_addr_type)?0:1);
+    return 1;
+}
+
+static int l_ble_disconnect(lua_State* L) {
+    luat_ble_t* luat_ble = (luat_ble_t *)luaL_checkudata(L, 1, LUAT_BLE_TYPE);
+    uint8_t conn_idx = luaL_checknumber(L, 2);
+    lua_pushboolean(L, luat_ble_disconnect(luat_ble, conn_idx)?0:1);
+    return 1;
+}
+
 static int _ble_struct_newindex(lua_State *L);
 
 void luat_ble_struct_init(lua_State *L) {
@@ -477,6 +498,9 @@ static const rotable_Reg_t reg_ble[] = {
     {"scan_start",                  ROREG_FUNC(l_ble_scanning_start)},
     {"scan_stop",                   ROREG_FUNC(l_ble_scanning_stop)},
 
+    {"connect",                   ROREG_FUNC(l_ble_connect)},
+    {"disconnect",                   ROREG_FUNC(l_ble_disconnect)},
+
     // BLE_EVENT
     {"EVENT_NONE",                  ROREG_INT(LUAT_BLE_EVENT_NONE)},
     {"EVENT_INIT",                  ROREG_INT(LUAT_BLE_EVENT_INIT)},