|
|
@@ -102,39 +102,44 @@ 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:
|
|
|
- {
|
|
|
+ }case LUAT_BLE_EVENT_SCAN_REPORT:{
|
|
|
luat_ble_adv_req_t *adv_req = &(param->adv_req);
|
|
|
lua_createtable(L, 0, 4);
|
|
|
|
|
|
@@ -155,8 +160,7 @@ int l_ble_callback(lua_State *L, void *ptr)
|
|
|
|
|
|
lua_call(L, 3, 0);
|
|
|
break;
|
|
|
- }
|
|
|
- case LUAT_BLE_EVENT_GATT_DONE:{
|
|
|
+ }case LUAT_BLE_EVENT_GATT_DONE:{
|
|
|
luat_ble_gatt_service_t **gatt_services = param->gatt_done_ind.gatt_service;
|
|
|
uint8_t gatt_service_num = param->gatt_done_ind.gatt_service_num;
|
|
|
lua_createtable(L, gatt_service_num, 0);
|
|
|
@@ -183,9 +187,7 @@ int l_ble_callback(lua_State *L, void *ptr)
|
|
|
}
|
|
|
lua_call(L, 3, 0);
|
|
|
break;
|
|
|
- }
|
|
|
- case LUAT_BLE_EVENT_CONN:
|
|
|
- {
|
|
|
+ }case LUAT_BLE_EVENT_CONN:{
|
|
|
luat_ble_conn_ind_t *conn = &(param->conn_ind);
|
|
|
lua_newtable(L);
|
|
|
memcpy(tmpbuff, conn->peer_addr, 6);
|
|
|
@@ -196,9 +198,7 @@ int l_ble_callback(lua_State *L, void *ptr)
|
|
|
lua_setfield(L, -2, "addr_type");
|
|
|
lua_call(L, 3, 0);
|
|
|
break;
|
|
|
- }
|
|
|
- case LUAT_BLE_EVENT_DISCONN:
|
|
|
- {
|
|
|
+ }case LUAT_BLE_EVENT_DISCONN:{
|
|
|
luat_ble_disconn_ind_t *disconn = &(param->disconn_ind);
|
|
|
lua_newtable(L);
|
|
|
lua_pushinteger(L, disconn->reason);
|
|
|
@@ -231,26 +231,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);
|
|
|
}
|
|
|
@@ -606,7 +600,6 @@ static int l_ble_advertising_stop(lua_State *L){
|
|
|
ble_device:write_notify({
|
|
|
uuid_service = "FA00", -- 服务的UUID, 可以是16位、32位或128位
|
|
|
uuid_characteristic = "EA01", -- 特征的UUID值, 可以是16位、32位或128位
|
|
|
- uuid_descriptor = "2902" -- 可选, 描述符的UUID值, 可以是16位、32位或128位
|
|
|
}, "Hello BLE") -- 要写入的值
|
|
|
*/
|
|
|
static int l_ble_write_notify(lua_State *L){
|
|
|
@@ -627,7 +620,7 @@ static int l_ble_write_notify(lua_State *L){
|
|
|
service_uuid = luaL_checklstring(L, -1, &tmp);
|
|
|
service.uuid_type = tmp;
|
|
|
memcpy(service.uuid, service_uuid, service.uuid_type);
|
|
|
- LLOGD("uuid_service: %02X %02X", service.uuid[0], service.uuid[1]);
|
|
|
+ // LLOGD("uuid_service: %02X %02X", service.uuid[0], service.uuid[1]);
|
|
|
}
|
|
|
else{
|
|
|
LLOGW("缺失 uuid_service 参数");
|
|
|
@@ -640,7 +633,7 @@ static int l_ble_write_notify(lua_State *L){
|
|
|
characteristic_uuid = luaL_checklstring(L, -1, &tmp);
|
|
|
characteristic.uuid_type = tmp;
|
|
|
memcpy(characteristic.uuid, characteristic_uuid, characteristic.uuid_type);
|
|
|
- LLOGD("uuid_characteristic: %02X %02X", characteristic.uuid[0], characteristic.uuid[1]);
|
|
|
+ // LLOGD("uuid_characteristic: %02X %02X", characteristic.uuid[0], characteristic.uuid[1]);
|
|
|
}
|
|
|
else{
|
|
|
LLOGW("缺失 uuid_characteristic 参数");
|
|
|
@@ -653,7 +646,7 @@ static int l_ble_write_notify(lua_State *L){
|
|
|
descriptor_uuid = luaL_checklstring(L, -1, &tmp);
|
|
|
descriptor.uuid_type = tmp;
|
|
|
memcpy(descriptor.uuid, descriptor_uuid, descriptor.uuid_type);
|
|
|
- LLOGD("uuid_descriptor: %02X %02X", descriptor.uuid[0], descriptor.uuid[1]);
|
|
|
+ // LLOGD("uuid_descriptor: %02X %02X", descriptor.uuid[0], descriptor.uuid[1]);
|
|
|
ret = luat_ble_write_notify_value(&service, &characteristic, &descriptor, (uint8_t *)value, len);
|
|
|
}else{
|
|
|
ret = luat_ble_write_notify_value(&service, &characteristic, NULL, (uint8_t *)value, len);
|
|
|
@@ -680,7 +673,6 @@ end_error:
|
|
|
ble_device:write_indicate({
|
|
|
uuid_service = "FA00", -- 服务的UUID, 可以是16位、32位或128位
|
|
|
uuid_characteristic = "EA01", -- 特征的UUID值, 可以是16位、32位或128位
|
|
|
- uuid_descriptor = "2902" -- 可选, 描述符的UUID值, 可以是16位、32位或128位
|
|
|
}, "Hello BLE") -- 要写入的值
|
|
|
*/
|
|
|
static int l_ble_write_indicate(lua_State *L){
|
|
|
@@ -754,7 +746,6 @@ end_error:
|
|
|
ble_device:write_value({
|
|
|
uuid_service = "FA00", -- 服务的UUID, 可以是16位、32位或128位
|
|
|
uuid_characteristic = "EA01", -- 特征的UUID值, 可以是16位、32位或128位
|
|
|
- uuid_descriptor = "2902" -- 可选, 描述符的UUID值, 可以是16位、32位或128位
|
|
|
}, "Hello BLE") -- 要写入的值
|
|
|
*/
|
|
|
static int l_ble_write_value(lua_State *L){
|
|
|
@@ -775,7 +766,7 @@ static int l_ble_write_value(lua_State *L){
|
|
|
service_uuid = luaL_checklstring(L, -1, &tmp);
|
|
|
service.uuid_type = tmp;
|
|
|
memcpy(service.uuid, service_uuid, service.uuid_type);
|
|
|
- LLOGD("uuid_service: %02X %02X", service.uuid[0], service.uuid[1]);
|
|
|
+ // LLOGD("uuid_service: %02X %02X", service.uuid[0], service.uuid[1]);
|
|
|
}
|
|
|
else{
|
|
|
LLOGW("缺失 uuid_service 参数");
|
|
|
@@ -788,7 +779,7 @@ static int l_ble_write_value(lua_State *L){
|
|
|
characteristic_uuid = luaL_checklstring(L, -1, &tmp);
|
|
|
characteristic.uuid_type = tmp;
|
|
|
memcpy(characteristic.uuid, characteristic_uuid, characteristic.uuid_type);
|
|
|
- LLOGD("uuid_characteristic: %02X %02X", characteristic.uuid[0], characteristic.uuid[1]);
|
|
|
+ // LLOGD("uuid_characteristic: %02X %02X", characteristic.uuid[0], characteristic.uuid[1]);
|
|
|
}
|
|
|
else{
|
|
|
LLOGW("缺失 uuid_characteristic 参数");
|
|
|
@@ -801,7 +792,7 @@ static int l_ble_write_value(lua_State *L){
|
|
|
descriptor_uuid = luaL_checklstring(L, -1, &tmp);
|
|
|
descriptor.uuid_type = tmp;
|
|
|
memcpy(descriptor.uuid, descriptor_uuid, descriptor.uuid_type);
|
|
|
- LLOGD("uuid_descriptor: %02X %02X", descriptor.uuid[0], descriptor.uuid[1]);
|
|
|
+ // LLOGD("uuid_descriptor: %02X %02X", descriptor.uuid[0], descriptor.uuid[1]);
|
|
|
ret = luat_ble_write_value(&service, &characteristic, &descriptor, (uint8_t *)value, len);
|
|
|
|
|
|
}else{
|
|
|
@@ -818,6 +809,18 @@ end_error:
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+读取特征值
|
|
|
+@api ble.read_value(opts)
|
|
|
+@table 特征值的描述信息
|
|
|
+@return boolean 是否成功
|
|
|
+@usage
|
|
|
+-- 读取特征值,通过回调中的 EVENT_READ_VALUE 事件返回读取的value值
|
|
|
+ble_device:read_value({
|
|
|
+ uuid_service = "FA00", -- 服务的UUID, 可以是16位、32位或128位
|
|
|
+ uuid_characteristic = "EA01", -- 特征的UUID值, 可以是16位、32位或128位
|
|
|
+})
|
|
|
+*/
|
|
|
static int l_ble_read_value(lua_State *L){
|
|
|
uint16_t ret = 0;
|
|
|
const char *service_uuid = NULL;
|
|
|
@@ -836,7 +839,7 @@ static int l_ble_read_value(lua_State *L){
|
|
|
service_uuid = luaL_checklstring(L, -1, &tmp);
|
|
|
service.uuid_type = tmp;
|
|
|
memcpy(service.uuid, service_uuid, service.uuid_type);
|
|
|
- LLOGD("uuid_service: %02X %02X", service.uuid[0], service.uuid[1]);
|
|
|
+ // LLOGD("uuid_service: %02X %02X", service.uuid[0], service.uuid[1]);
|
|
|
}
|
|
|
else{
|
|
|
LLOGW("缺失 uuid_service 参数");
|
|
|
@@ -849,7 +852,7 @@ static int l_ble_read_value(lua_State *L){
|
|
|
characteristic_uuid = luaL_checklstring(L, -1, &tmp);
|
|
|
characteristic.uuid_type = tmp;
|
|
|
memcpy(characteristic.uuid, characteristic_uuid, characteristic.uuid_type);
|
|
|
- LLOGD("uuid_characteristic: %02X %02X", characteristic.uuid[0], characteristic.uuid[1]);
|
|
|
+ // LLOGD("uuid_characteristic: %02X %02X", characteristic.uuid[0], characteristic.uuid[1]);
|
|
|
}
|
|
|
else{
|
|
|
LLOGW("缺失 uuid_characteristic 参数");
|
|
|
@@ -862,7 +865,7 @@ static int l_ble_read_value(lua_State *L){
|
|
|
descriptor_uuid = luaL_checklstring(L, -1, &tmp);
|
|
|
descriptor.uuid_type = tmp;
|
|
|
memcpy(descriptor.uuid, descriptor_uuid, descriptor.uuid_type);
|
|
|
- LLOGD("uuid_descriptor: %02X %02X", descriptor.uuid[0], descriptor.uuid[1]);
|
|
|
+ // LLOGD("uuid_descriptor: %02X %02X", descriptor.uuid[0], descriptor.uuid[1]);
|
|
|
ret = luat_ble_read_value(&service, &characteristic, &descriptor, &value, &len);
|
|
|
|
|
|
}else{
|
|
|
@@ -949,17 +952,35 @@ static int l_ble_scanning_stop(lua_State *L){
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+BLE连接
|
|
|
+@api ble.connect()
|
|
|
+@string mac 地址
|
|
|
+@int 地址类型 ble.PUBLIC ble.RANDOM
|
|
|
+@return boolean 是否成功
|
|
|
+@usage
|
|
|
+-- BLE连接
|
|
|
+ble_device:connect(string.fromHex("C8478C4E027D"),0)
|
|
|
+*/
|
|
|
static int l_ble_connect(lua_State *L){
|
|
|
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]);
|
|
|
+ // 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(NULL, adv_addr, adv_addr_type) ? 0 : 1);
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+BLE断开连接
|
|
|
+@api ble.disconnect()
|
|
|
+@return boolean 是否成功
|
|
|
+@usage
|
|
|
+-- BLE断开连接
|
|
|
+ble_device:disconnect()
|
|
|
+*/
|
|
|
static int l_ble_disconnect(lua_State *L){
|
|
|
lua_pushboolean(L, luat_ble_disconnect(NULL) ? 0 : 1);
|
|
|
return 1;
|
|
|
@@ -1013,26 +1034,36 @@ 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
|
|
|
+ // @const PUBLIC 控制器的公共地址
|
|
|
{"PUBLIC", ROREG_INT(LUAT_BLE_ADDR_MODE_PUBLIC)},
|
|
|
+ // @const RANDOM 生成的静态地址
|
|
|
{"RANDOM", ROREG_INT(LUAT_BLE_ADDR_MODE_RANDOM)},
|
|
|
{"RPA", ROREG_INT(LUAT_BLE_ADDR_MODE_RPA)},
|
|
|
{"NRPA", ROREG_INT(LUAT_BLE_ADDR_MODE_NRPA)},
|
|
|
+
|
|
|
// ADV_CHNL
|
|
|
+ //@const CHNL_37 37通道
|
|
|
{"CHNL_37", ROREG_INT(LUAT_BLE_ADV_CHNL_37)},
|
|
|
+ //@const CHNL_38 38通道
|
|
|
{"CHNL_38", ROREG_INT(LUAT_BLE_ADV_CHNL_38)},
|
|
|
+ //@const CHNL_39 39通道
|
|
|
{"CHNL_39", ROREG_INT(LUAT_BLE_ADV_CHNL_39)},
|
|
|
+ //@const CHNLS_ALL 所有通道(37 38 39)
|
|
|
{"CHNLS_ALL", ROREG_INT(LUAT_BLE_ADV_CHNLS_ALL)},
|
|
|
+
|
|
|
// Permission
|
|
|
+ //@const READ 读权限
|
|
|
{"READ", ROREG_INT(LUAT_BLE_GATT_PERM_READ)},
|
|
|
+ //@const READ 写权限
|
|
|
{"WRITE", ROREG_INT(LUAT_BLE_GATT_PERM_WRITE)},
|
|
|
{"IND", ROREG_INT(LUAT_BLE_GATT_PERM_IND)},
|
|
|
{"NOTIFY", ROREG_INT(LUAT_BLE_GATT_PERM_NOTIFY)},
|
|
|
{"WRITE_CMD", ROREG_INT(LUAT_BLE_GATT_PERM_WRITE_CMD)},
|
|
|
+
|
|
|
// FLAGS
|
|
|
{"FLAGS", ROREG_INT(LUAT_ADV_TYPE_FLAGS)},
|
|
|
{"COMPLETE_LOCAL_NAME", ROREG_INT(LUAT_ADV_TYPE_COMPLETE_LOCAL_NAME)},
|