|
|
@@ -19,9 +19,12 @@ uint32_t reserved; // 保留字段, 目前都是0
|
|
|
#define LUAT_LOG_TAG "drv.ble"
|
|
|
#include "luat_log.h"
|
|
|
|
|
|
+luat_ble_cb_t g_drv_ble_cb;
|
|
|
+
|
|
|
int luat_ble_init(void* args, luat_ble_cb_t luat_ble_cb) {
|
|
|
- LLOGD("执行luat_ble_init");
|
|
|
- uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
|
|
|
+ LLOGD("执行luat_ble_init %p", luat_ble_cb);
|
|
|
+ g_drv_ble_cb = luat_ble_cb;
|
|
|
+ uint64_t seq = luat_airlink_get_next_cmd_id();
|
|
|
airlink_queue_item_t item = {
|
|
|
.len = 8 + sizeof(luat_airlink_cmd_t) + 8
|
|
|
};
|
|
|
@@ -29,16 +32,9 @@ int luat_ble_init(void* args, luat_ble_cb_t luat_ble_cb) {
|
|
|
if (cmd == NULL) {
|
|
|
return -101;
|
|
|
}
|
|
|
- memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
|
|
|
- uint8_t data[8] = {0};
|
|
|
- // 前2个字节是蓝牙cmd id
|
|
|
- uint16_t id = LUAT_DRV_BT_CMD_BLE_INIT;
|
|
|
- memcpy(data, &id, 2);
|
|
|
- // 然后2个字节的主机协议版本号, 当前全是0
|
|
|
- // 剩余4个字节做预留
|
|
|
-
|
|
|
- // 全部拷贝过去
|
|
|
- memcpy(cmd->data + 8, data, 8);
|
|
|
+ luat_drv_ble_msg_t msg = { .id = seq};
|
|
|
+ msg.cmd_id = LUAT_DRV_BT_CMD_BLE_INIT;
|
|
|
+ memcpy(cmd->data, &msg, sizeof(luat_drv_ble_msg_t));
|
|
|
|
|
|
item.cmd = cmd;
|
|
|
luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
|
|
|
@@ -47,7 +43,7 @@ int luat_ble_init(void* args, luat_ble_cb_t luat_ble_cb) {
|
|
|
|
|
|
int luat_ble_deinit(void* args) {
|
|
|
LLOGD("执行luat_ble_deinit");
|
|
|
- uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
|
|
|
+ uint64_t seq = luat_airlink_get_next_cmd_id();
|
|
|
airlink_queue_item_t item = {
|
|
|
.len = 8 + sizeof(luat_airlink_cmd_t) + 8
|
|
|
};
|
|
|
@@ -55,16 +51,9 @@ int luat_ble_deinit(void* args) {
|
|
|
if (cmd == NULL) {
|
|
|
return -101;
|
|
|
}
|
|
|
- memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
|
|
|
- uint8_t data[8] = {0};
|
|
|
- // 前2个字节是蓝牙cmd id
|
|
|
- uint16_t id = LUAT_DRV_BT_CMD_BLE_DEINIT;
|
|
|
- memcpy(data, &id, 2);
|
|
|
- // 然后2个字节的主机协议版本号, 当前全是0
|
|
|
- // 剩余4个字节做预留
|
|
|
-
|
|
|
- // 全部拷贝过去
|
|
|
- memcpy(cmd->data + 8, data, 8);
|
|
|
+ luat_drv_ble_msg_t msg = { .id = seq};
|
|
|
+ msg.cmd_id = LUAT_DRV_BT_CMD_BLE_DEINIT;
|
|
|
+ memcpy(cmd->data, &msg, sizeof(luat_drv_ble_msg_t));
|
|
|
|
|
|
item.cmd = cmd;
|
|
|
luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
|
|
|
@@ -74,28 +63,22 @@ int luat_ble_deinit(void* args) {
|
|
|
|
|
|
int luat_ble_set_name(void* args, char* name, uint8_t len) {
|
|
|
LLOGD("执行luat_ble_set_name");
|
|
|
- uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
|
|
|
+ uint64_t seq = luat_airlink_get_next_cmd_id();
|
|
|
airlink_queue_item_t item = {
|
|
|
- .len = 8 + sizeof(luat_airlink_cmd_t) + 8 + len + sizeof(uint16_t)
|
|
|
+ .len = sizeof(luat_airlink_cmd_t) + sizeof(luat_drv_ble_msg_t) + len + 1
|
|
|
};
|
|
|
luat_airlink_cmd_t* cmd = luat_airlink_cmd_new(0x500, item.len - sizeof(luat_airlink_cmd_t));
|
|
|
if (cmd == NULL) {
|
|
|
return -101;
|
|
|
}
|
|
|
- memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
|
|
|
- uint8_t data[8] = {0};
|
|
|
- // 前2个字节是蓝牙cmd id
|
|
|
- uint16_t id = LUAT_DRV_BT_CMD_BLE_SET_NAME;
|
|
|
- memcpy(data, &id, 2);
|
|
|
- // 然后2个字节的主机协议版本号, 当前全是0
|
|
|
- // 剩余4个字节做预留
|
|
|
-
|
|
|
- // 全部拷贝过去
|
|
|
- memcpy(cmd->data + 8, data, 8);
|
|
|
+ luat_drv_ble_msg_t msg = { .id = seq};
|
|
|
+ msg.cmd_id = LUAT_DRV_BT_CMD_BLE_SET_NAME;
|
|
|
+ memcpy(cmd->data, &msg, sizeof(luat_drv_ble_msg_t));
|
|
|
+
|
|
|
// 然后是名字长度, 1字节
|
|
|
- memcpy(cmd->data + 8 + 8, &len, 1);
|
|
|
+ memcpy(cmd->data + sizeof(luat_drv_ble_msg_t), &len, 1);
|
|
|
// 然后是名字
|
|
|
- memcpy(cmd->data + 8 + 8 + 1, name, len);
|
|
|
+ memcpy(cmd->data + sizeof(luat_drv_ble_msg_t) + 1, name, len);
|
|
|
|
|
|
item.cmd = cmd;
|
|
|
luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
|
|
|
@@ -104,14 +87,14 @@ int luat_ble_set_name(void* args, char* name, uint8_t len) {
|
|
|
|
|
|
|
|
|
int luat_ble_set_max_mtu(void* args, uint16_t max_mtu) {
|
|
|
- LLOGE("not support yet");
|
|
|
+ LLOGE("set max mtu %d not support yet", max_mtu);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
// advertise
|
|
|
int luat_ble_create_advertising(void* args, luat_ble_adv_cfg_t* adv_cfg) {
|
|
|
LLOGD("执行luat_ble_create_advertising");
|
|
|
- uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
|
|
|
+ uint64_t seq = luat_airlink_get_next_cmd_id();
|
|
|
airlink_queue_item_t item = {
|
|
|
.len = 8 + sizeof(luat_airlink_cmd_t) + 8 + sizeof(uint16_t) + sizeof(luat_ble_adv_cfg_t)
|
|
|
};
|
|
|
@@ -119,16 +102,10 @@ int luat_ble_create_advertising(void* args, luat_ble_adv_cfg_t* adv_cfg) {
|
|
|
if (cmd == NULL) {
|
|
|
return -101;
|
|
|
}
|
|
|
- memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
|
|
|
- uint8_t data[8] = {0};
|
|
|
- // 前2个字节是蓝牙cmd id
|
|
|
- uint16_t id = LUAT_DRV_BT_CMD_BLE_ADV_CREATE;
|
|
|
- memcpy(data, &id, 2);
|
|
|
- // 然后2个字节的主机协议版本号, 当前全是0
|
|
|
- // 剩余4个字节做预留
|
|
|
-
|
|
|
- // 全部拷贝过去
|
|
|
- memcpy(cmd->data + 8, data, 8);
|
|
|
+ luat_drv_ble_msg_t msg = { .id = seq};
|
|
|
+ msg.cmd_id = LUAT_DRV_BT_CMD_BLE_ADV_CREATE;
|
|
|
+ memcpy(cmd->data, &msg, sizeof(luat_drv_ble_msg_t));
|
|
|
+
|
|
|
// 然后是结构体大小
|
|
|
uint16_t sizeof_adv = sizeof(luat_ble_adv_cfg_t);
|
|
|
memcpy(cmd->data + 8 + 8, &sizeof_adv, 2);
|
|
|
@@ -143,7 +120,7 @@ int luat_ble_create_advertising(void* args, luat_ble_adv_cfg_t* adv_cfg) {
|
|
|
|
|
|
int luat_ble_set_adv_data(void* args, uint8_t* adv_buff, uint8_t adv_len) {
|
|
|
LLOGD("执行luat_ble_set_adv_data %p %d", adv_buff, adv_len);
|
|
|
- uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
|
|
|
+ uint64_t seq = luat_airlink_get_next_cmd_id();
|
|
|
airlink_queue_item_t item = {
|
|
|
.len = 8 + sizeof(luat_airlink_cmd_t) + 8 + sizeof(uint16_t) + adv_len
|
|
|
};
|
|
|
@@ -151,16 +128,9 @@ int luat_ble_set_adv_data(void* args, uint8_t* adv_buff, uint8_t adv_len) {
|
|
|
if (cmd == NULL) {
|
|
|
return -101;
|
|
|
}
|
|
|
- memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
|
|
|
- uint8_t data[8] = {0};
|
|
|
- // 前2个字节是蓝牙cmd id
|
|
|
- uint16_t id = LUAT_DRV_BT_CMD_BLE_ADV_SET_DATA;
|
|
|
- memcpy(data, &id, 2);
|
|
|
- // 然后2个字节的主机协议版本号, 当前全是0
|
|
|
- // 剩余4个字节做预留
|
|
|
-
|
|
|
- // 全部拷贝过去
|
|
|
- memcpy(cmd->data + 8, data, 8);
|
|
|
+ luat_drv_ble_msg_t msg = { .id = seq};
|
|
|
+ msg.cmd_id = LUAT_DRV_BT_CMD_BLE_ADV_SET_DATA;
|
|
|
+ memcpy(cmd->data, &msg, sizeof(luat_drv_ble_msg_t));
|
|
|
// 然后是结构体大小
|
|
|
uint16_t datalen = adv_len;
|
|
|
memcpy(cmd->data + 8 + 8, &datalen, 2);
|
|
|
@@ -175,7 +145,7 @@ int luat_ble_set_adv_data(void* args, uint8_t* adv_buff, uint8_t adv_len) {
|
|
|
|
|
|
int luat_ble_set_scan_rsp_data(void* args, uint8_t* rsp_data, uint8_t rsp_len) {
|
|
|
LLOGD("执行luat_ble_set_scan_rsp_data %p %d", rsp_data, rsp_len);
|
|
|
- uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
|
|
|
+ uint64_t seq = luat_airlink_get_next_cmd_id();
|
|
|
airlink_queue_item_t item = {
|
|
|
.len = 8 + sizeof(luat_airlink_cmd_t) + 8 + sizeof(uint16_t) + rsp_len
|
|
|
};
|
|
|
@@ -183,16 +153,10 @@ int luat_ble_set_scan_rsp_data(void* args, uint8_t* rsp_data, uint8_t rsp_len) {
|
|
|
if (cmd == NULL) {
|
|
|
return -101;
|
|
|
}
|
|
|
- memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
|
|
|
- uint8_t data[8] = {0};
|
|
|
- // 前2个字节是蓝牙cmd id
|
|
|
- uint16_t id = LUAT_DRV_BT_CMD_BLE_ADV_SET_SCAN_RSP_DATA;
|
|
|
- memcpy(data, &id, 2);
|
|
|
- // 然后2个字节的主机协议版本号, 当前全是0
|
|
|
- // 剩余4个字节做预留
|
|
|
-
|
|
|
- // 全部拷贝过去
|
|
|
- memcpy(cmd->data + 8, data, 8);
|
|
|
+ luat_drv_ble_msg_t msg = { .id = seq};
|
|
|
+ msg.cmd_id = LUAT_DRV_BT_CMD_BLE_ADV_SET_SCAN_RSP_DATA;
|
|
|
+ memcpy(cmd->data, &msg, sizeof(luat_drv_ble_msg_t));
|
|
|
+
|
|
|
// 然后是结构体大小
|
|
|
uint16_t datalen = rsp_len;
|
|
|
memcpy(cmd->data + 8 + 8, &datalen, 2);
|
|
|
@@ -207,7 +171,7 @@ int luat_ble_set_scan_rsp_data(void* args, uint8_t* rsp_data, uint8_t rsp_len) {
|
|
|
|
|
|
int luat_ble_start_advertising(void* args) {
|
|
|
LLOGD("执行luat_ble_start_advertising");
|
|
|
- uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
|
|
|
+ uint64_t seq = luat_airlink_get_next_cmd_id();
|
|
|
airlink_queue_item_t item = {
|
|
|
.len = 8 + sizeof(luat_airlink_cmd_t) + 8
|
|
|
};
|
|
|
@@ -215,16 +179,9 @@ int luat_ble_start_advertising(void* args) {
|
|
|
if (cmd == NULL) {
|
|
|
return -101;
|
|
|
}
|
|
|
- memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
|
|
|
- uint8_t data[8] = {0};
|
|
|
- // 前2个字节是蓝牙cmd id
|
|
|
- uint16_t id = LUAT_DRV_BT_CMD_BLE_ADV_START;
|
|
|
- memcpy(data, &id, 2);
|
|
|
- // 然后2个字节的主机协议版本号, 当前全是0
|
|
|
- // 剩余4个字节做预留
|
|
|
-
|
|
|
- // 全部拷贝过去
|
|
|
- memcpy(cmd->data + 8, data, 8);
|
|
|
+ luat_drv_ble_msg_t msg = { .id = seq};
|
|
|
+ msg.cmd_id = LUAT_DRV_BT_CMD_BLE_ADV_START;
|
|
|
+ memcpy(cmd->data, &msg, sizeof(luat_drv_ble_msg_t));
|
|
|
|
|
|
item.cmd = cmd;
|
|
|
luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
|
|
|
@@ -234,7 +191,7 @@ int luat_ble_start_advertising(void* args) {
|
|
|
|
|
|
int luat_ble_stop_advertising(void* args) {
|
|
|
LLOGD("执行luat_ble_start_advertising");
|
|
|
- uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
|
|
|
+ uint64_t seq = luat_airlink_get_next_cmd_id();
|
|
|
airlink_queue_item_t item = {
|
|
|
.len = 8 + sizeof(luat_airlink_cmd_t) + 8
|
|
|
};
|
|
|
@@ -242,16 +199,9 @@ int luat_ble_stop_advertising(void* args) {
|
|
|
if (cmd == NULL) {
|
|
|
return -101;
|
|
|
}
|
|
|
- memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
|
|
|
- uint8_t data[8] = {0};
|
|
|
- // 前2个字节是蓝牙cmd id
|
|
|
- uint16_t id = LUAT_DRV_BT_CMD_BLE_ADV_STOP;
|
|
|
- memcpy(data, &id, 2);
|
|
|
- // 然后2个字节的主机协议版本号, 当前全是0
|
|
|
- // 剩余4个字节做预留
|
|
|
-
|
|
|
- // 全部拷贝过去
|
|
|
- memcpy(cmd->data + 8, data, 8);
|
|
|
+ luat_drv_ble_msg_t msg = { .id = seq};
|
|
|
+ msg.cmd_id = LUAT_DRV_BT_CMD_BLE_ADV_STOP;
|
|
|
+ memcpy(cmd->data, &msg, sizeof(luat_drv_ble_msg_t));
|
|
|
|
|
|
item.cmd = cmd;
|
|
|
luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
|
|
|
@@ -260,8 +210,8 @@ int luat_ble_stop_advertising(void* args) {
|
|
|
|
|
|
|
|
|
int luat_ble_delete_advertising(void* args) {
|
|
|
- LLOGD("执行luat_ble_start_advertising");
|
|
|
- uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
|
|
|
+ LLOGD("执行luat_ble_start_advertising");
|
|
|
+ uint64_t seq = luat_airlink_get_next_cmd_id();
|
|
|
airlink_queue_item_t item = {
|
|
|
.len = 8 + sizeof(luat_airlink_cmd_t) + 8
|
|
|
};
|
|
|
@@ -269,16 +219,9 @@ int luat_ble_delete_advertising(void* args) {
|
|
|
if (cmd == NULL) {
|
|
|
return -101;
|
|
|
}
|
|
|
- memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
|
|
|
- uint8_t data[8] = {0};
|
|
|
- // 前2个字节是蓝牙cmd id
|
|
|
- uint16_t id = LUAT_DRV_BT_CMD_BLE_ADV_START;
|
|
|
- memcpy(data, &id, 2);
|
|
|
- // 然后2个字节的主机协议版本号, 当前全是0
|
|
|
- // 剩余4个字节做预留
|
|
|
-
|
|
|
- // 全部拷贝过去
|
|
|
- memcpy(cmd->data + 8, data, 8);
|
|
|
+ luat_drv_ble_msg_t msg = { .id = seq};
|
|
|
+ msg.cmd_id = LUAT_DRV_BT_CMD_BLE_ADV_DELETE;
|
|
|
+ memcpy(cmd->data, &msg, sizeof(luat_drv_ble_msg_t));
|
|
|
|
|
|
item.cmd = cmd;
|
|
|
luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
|
|
|
@@ -290,46 +233,43 @@ int luat_ble_delete_advertising(void* args) {
|
|
|
int luat_ble_create_gatt(void* args, luat_ble_gatt_service_t* gatt) {
|
|
|
LLOGD("执行luat_ble_create_gatt");
|
|
|
uint16_t tmp = 0;
|
|
|
- uint64_t luat_airlink_next_cmd_id = luat_airlink_get_next_cmd_id();
|
|
|
+ uint64_t seq = luat_airlink_get_next_cmd_id();
|
|
|
airlink_queue_item_t item = {
|
|
|
- .len = 8 + sizeof(luat_airlink_cmd_t)
|
|
|
- + 8 + sizeof(luat_ble_gatt_service_t)
|
|
|
+ .len = sizeof(luat_airlink_cmd_t)
|
|
|
+ + sizeof(luat_drv_ble_msg_t) + sizeof(luat_ble_gatt_service_t)
|
|
|
+ gatt->characteristics_num * sizeof(luat_ble_gatt_chara_t)
|
|
|
- + 8
|
|
|
+ + 16
|
|
|
};
|
|
|
luat_airlink_cmd_t* cmd = luat_airlink_cmd_new(0x500, item.len - sizeof(luat_airlink_cmd_t));
|
|
|
if (cmd == NULL) {
|
|
|
return -101;
|
|
|
}
|
|
|
- memcpy(cmd->data, &luat_airlink_next_cmd_id, 8);
|
|
|
- uint8_t data[8] = {0};
|
|
|
- // 前2个字节是蓝牙cmd id
|
|
|
- uint16_t id = LUAT_DRV_BT_CMD_BLE_GATT_CREATE;
|
|
|
- memcpy(data, &id, 2);
|
|
|
- // 然后2个字节的主机协议版本号, 当前全是0
|
|
|
- // 剩余4个字节做预留
|
|
|
-
|
|
|
- // 全部拷贝过去
|
|
|
- memcpy(cmd->data + 8, data, 8);
|
|
|
+
|
|
|
+ luat_drv_ble_msg_t msg = { .id = seq};
|
|
|
+ msg.cmd_id = LUAT_DRV_BT_CMD_BLE_GATT_CREATE;
|
|
|
+ memcpy(cmd->data, &msg, sizeof(luat_drv_ble_msg_t));
|
|
|
|
|
|
// 数据部分
|
|
|
// 首先是luat_ble_gatt_service_t结构的大小
|
|
|
tmp = sizeof(luat_ble_gatt_service_t);
|
|
|
- memcpy(cmd->data + 8 + 8, &tmp, 2);
|
|
|
+ memcpy(cmd->data + sizeof(luat_drv_ble_msg_t), &tmp, 2);
|
|
|
// 然后是luat_ble_gatt_chara_t的大小
|
|
|
tmp = sizeof(luat_ble_gatt_chara_t);
|
|
|
- memcpy(cmd->data + 8 + 8 + 2, &tmp, 2);
|
|
|
+ memcpy(cmd->data + sizeof(luat_drv_ble_msg_t) + 2, &tmp, 2);
|
|
|
// 然后是服务id的数量
|
|
|
tmp = gatt->characteristics_num;
|
|
|
- memcpy(cmd->data + 8 + 8 + 2 + 2, &tmp, 2);
|
|
|
+ memcpy(cmd->data + sizeof(luat_drv_ble_msg_t) + 2 + 2, &tmp, 2);
|
|
|
|
|
|
// 头部拷贝完成, 拷贝数据
|
|
|
- memcpy(cmd->data + 8 + 8 + 2 + 2 + 2, gatt, sizeof(luat_ble_gatt_service_t));
|
|
|
+ memcpy(cmd->data + sizeof(luat_drv_ble_msg_t) + 8, gatt, sizeof(luat_ble_gatt_service_t));
|
|
|
// 然后是服务id
|
|
|
- memcpy(cmd->data + 8 + 8 + 2 + 2 + 2 + sizeof(luat_ble_gatt_service_t),
|
|
|
+ memcpy(cmd->data + sizeof(luat_drv_ble_msg_t) + 8 + sizeof(luat_ble_gatt_service_t),
|
|
|
gatt->characteristics, gatt->characteristics_num * sizeof(luat_ble_gatt_chara_t));
|
|
|
|
|
|
item.cmd = cmd;
|
|
|
+
|
|
|
+ LLOGD("gatt 数据长度 %d %d %d", item.len, cmd->len, cmd->len - sizeof(luat_drv_ble_msg_t));
|
|
|
+ luat_airlink_print_buff("bt req HEX", cmd->data, cmd->len);
|
|
|
luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
|
|
|
return 0;
|
|
|
}
|