872 lines
26 KiB
C
872 lines
26 KiB
C
#include <rtthread.h>
|
||
#include <finsh.h>
|
||
#include "common.h"
|
||
#include "param_config.h"
|
||
#include "ble_pub.h"
|
||
#include "drv_oled.h"
|
||
#include "flash.h"
|
||
#include "wlan_ui_pub.h"
|
||
#include "rw_pub.h"
|
||
#include "belon_ble.h"
|
||
#include "app_temperature.h"
|
||
|
||
|
||
#if (CFG_BLE_VERSION == BLE_VERSION_5_2)
|
||
#include "app_ble.h"
|
||
#include "app_sdp.h"
|
||
#include "app_ble_init.h"
|
||
#if (BLE_APP_SEC)
|
||
#include "app_sec.h"
|
||
#endif
|
||
#include "kernel_mem.h"
|
||
#include "easyflash.h"
|
||
|
||
#define BUILD_UINT16(loByte, hiByte) \
|
||
((uint16_t)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))
|
||
|
||
#define BK_ATT_DECL_PRIMARY_SERVICE_128 {0x00,0x28,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
|
||
#define BK_ATT_DECL_CHARACTERISTIC_128 {0x03,0x28,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
|
||
#define BK_ATT_DESC_CLIENT_CHAR_CFG_128 {0x02,0x29,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
|
||
|
||
#define WRITE_REQ_CHARACTERISTIC_128 {0x01,0xFF,0,0,0x34,0x56,0,0,0,0,0x28,0x37,0,0,0,0}
|
||
#define INDICATE_CHARACTERISTIC_128 {0x02,0xFF,0,0,0x34,0x56,0,0,0,0,0x28,0x37,0,0,0,0}
|
||
#define NOTIFY_CHARACTERISTIC_128 {0x03,0xFF,0,0,0x34,0x56,0,0,0,0,0x28,0x37,0,0,0,0}
|
||
|
||
#define BLE_WIFI_FLASH_KEY_SSID "wifi_ssid"
|
||
#define BLE_WIFI_FLASH_KEY_PASS "wifi_pass"
|
||
// 全局变量
|
||
char last_ssid[33] = {0};
|
||
char last_password[65] = {0};
|
||
|
||
static const uint8_t test_svc_uuid[16] = {0xFF,0xFF,0,0,0x34,0x56,0,0,0,0,0x28,0x37,0,0,0,0};
|
||
app_dev_config_t dev_config = {0};
|
||
extern int hostapd_scan_started; //扫描wifi全局变量
|
||
#define BLE_IDLE_TIMEOUT_MS 10000 // 10秒无操作超时
|
||
static beken_timer_t ble_idle;
|
||
|
||
enum
|
||
{
|
||
TEST_IDX_SVC,
|
||
TEST_IDX_FF01_VAL_CHAR,
|
||
TEST_IDX_FF01_VAL_VALUE,
|
||
TEST_IDX_FF02_VAL_CHAR,
|
||
TEST_IDX_FF02_VAL_VALUE,
|
||
TEST_IDX_FF02_VAL_IND_CFG,
|
||
TEST_IDX_FF03_VAL_CHAR,
|
||
TEST_IDX_FF03_VAL_VALUE,
|
||
TEST_IDX_FF03_VAL_NTF_CFG,
|
||
TEST_IDX_NB,
|
||
};
|
||
uint8_t actv_idx_adv;
|
||
bk_attm_desc_t test_att_db[TEST_IDX_NB] =
|
||
{
|
||
// Service Declaration
|
||
[TEST_IDX_SVC] = {BK_ATT_DECL_PRIMARY_SERVICE_128, PROP(RD), 0},
|
||
|
||
// Level Characteristic Declaration
|
||
[TEST_IDX_FF01_VAL_CHAR] = {BK_ATT_DECL_CHARACTERISTIC_128, PROP(RD), 0},
|
||
// Level Characteristic Value
|
||
[TEST_IDX_FF01_VAL_VALUE] = {WRITE_REQ_CHARACTERISTIC_128, PROP(WR), 128|OPT(NO_OFFSET)},
|
||
|
||
[TEST_IDX_FF02_VAL_CHAR] = {BK_ATT_DECL_CHARACTERISTIC_128, PROP(RD), 0},
|
||
// Level Characteristic Value
|
||
[TEST_IDX_FF02_VAL_VALUE] = {INDICATE_CHARACTERISTIC_128, PROP(I), 128|OPT(NO_OFFSET)},
|
||
|
||
// Level Characteristic - Client Characteristic Configuration Descriptor
|
||
|
||
[TEST_IDX_FF02_VAL_IND_CFG] = {BK_ATT_DESC_CLIENT_CHAR_CFG_128, PROP(RD)|PROP(WR),OPT(NO_OFFSET)},
|
||
|
||
[TEST_IDX_FF03_VAL_CHAR] = {BK_ATT_DECL_CHARACTERISTIC_128, PROP(RD), 0},
|
||
// Level Characteristic Value
|
||
[TEST_IDX_FF03_VAL_VALUE] = {NOTIFY_CHARACTERISTIC_128, PROP(N), 128|OPT(NO_OFFSET)},
|
||
|
||
// Level Characteristic - Client Characteristic Configuration Descriptor
|
||
|
||
[TEST_IDX_FF03_VAL_NTF_CFG] = {BK_ATT_DESC_CLIENT_CHAR_CFG_128, PROP(RD)|PROP(WR), OPT(NO_OFFSET)},
|
||
};
|
||
ble_err_t bk_ble_init(void)
|
||
{
|
||
ble_err_t status = ERR_SUCCESS;
|
||
|
||
struct bk_ble_db_cfg ble_db_cfg;
|
||
|
||
ble_db_cfg.att_db = test_att_db;
|
||
ble_db_cfg.att_db_nb = TEST_IDX_NB;
|
||
ble_db_cfg.prf_task_id = 0;
|
||
ble_db_cfg.start_hdl = 0;
|
||
ble_db_cfg.svc_perm = BK_PERM_SET(SVC_UUID_LEN, UUID_16);
|
||
memcpy(&(ble_db_cfg.uuid[0]), &test_svc_uuid[0], 16);
|
||
|
||
status = bk_ble_create_db(&ble_db_cfg);
|
||
|
||
return status;
|
||
}
|
||
|
||
void cli_ble_write_callback(write_req_t *write_req)
|
||
{
|
||
bk_printf("write_cb[prf_id:%d, att_idx:%d, len:%d]\r\n", write_req->prf_id, write_req->att_idx, write_req->len);
|
||
for(int i = 0; i < write_req->len; i++)
|
||
{
|
||
bk_printf("0x%x ", write_req->value[i]);
|
||
}
|
||
bk_printf("\r\n");
|
||
}
|
||
|
||
uint8_t cli_ble_read_callback(read_req_t *read_req)
|
||
{
|
||
bk_printf("read_cb[prf_id:%d, att_idx:%d]\r\n", read_req->prf_id, read_req->att_idx);
|
||
read_req->value[0] = 0x10;
|
||
read_req->value[1] = 0x20;
|
||
read_req->value[2] = 0x30;
|
||
return 3;
|
||
}
|
||
|
||
static void ble_command_usage(void)
|
||
{
|
||
bk_printf("ble help - Help information\n");
|
||
bk_printf("ble active - Active ble to with BK7231BTxxx\n");
|
||
bk_printf("ble start_adv - Start advertising as a slave device\n");
|
||
bk_printf("ble stop_adv - Stop advertising as a slave device\n");
|
||
bk_printf("ble notify prf_id att_id value\n");
|
||
bk_printf(" - Send ntf value to master\n");
|
||
bk_printf("ble indicate prf_id att_id value\n");
|
||
bk_printf(" - Send ind value to master\n");
|
||
|
||
bk_printf("ble disc - Disconnect\n");
|
||
bk_printf("ble dut - dut test\n");
|
||
}
|
||
|
||
|
||
void sdp_event_cb(sdp_notice_t notice, void *param)
|
||
{
|
||
switch (notice) {
|
||
case SDP_CHARAC_NOTIFY_EVENT:
|
||
{
|
||
sdp_event_t *g_sdp = (sdp_event_t *)param;
|
||
bk_printf("[SDP_CHARAC_NOTIFY_EVENT]con_idx:%d,hdl:0x%x,value_length:%d\r\n",g_sdp->con_idx,g_sdp->hdl,g_sdp->value_length);
|
||
}
|
||
break;
|
||
case SDP_CHARAC_INDICATE_EVENT:
|
||
{
|
||
sdp_event_t *g_sdp = (sdp_event_t *)param;
|
||
bk_printf("[SDP_CHARAC_INDICATE_EVENT]con_idx:%d,hdl:0x%x,value_length:%d\r\n",g_sdp->con_idx,g_sdp->hdl,g_sdp->value_length);
|
||
}
|
||
break;
|
||
case SDP_CHARAC_READ:
|
||
{
|
||
sdp_event_t *g_sdp = (sdp_event_t *)param;
|
||
bk_printf("[SDP_CHARAC_READ]con_idx:%d,hdl:0x%x,value_length:%d\r\n",g_sdp->con_idx,g_sdp->hdl,g_sdp->value_length);
|
||
}
|
||
break;
|
||
case SDP_DISCOVER_SVR_DONE:
|
||
{
|
||
bk_printf("[SDP_DISCOVER_SVR_DONE]\r\n");
|
||
}
|
||
break;
|
||
case SDP_CHARAC_WRITE_DONE:
|
||
{
|
||
bk_printf("[SDP_CHARAC_WRITE_DONE]\r\n");
|
||
}
|
||
break;
|
||
default:
|
||
bk_printf("[%s]Event:%d\r\n",__func__,notice);
|
||
break;
|
||
}
|
||
}
|
||
|
||
void ble_cmd_cb(ble_cmd_t cmd, ble_cmd_param_t *param)
|
||
{
|
||
bk_printf("cmd:%d idx:%d status:%d\r\n", cmd, param->cmd_idx, param->status);
|
||
}
|
||
|
||
|
||
void scan_cb(void *ctxt, uint8_t param){//扫描wifi
|
||
int ret;
|
||
ScanResult_adv apList;
|
||
if (bk_wlan_ap_is_up() > 0 || hostapd_scan_started)
|
||
ret = wlan_ap_scan_result(&apList);
|
||
else
|
||
ret = wlan_sta_scan_result(&apList);
|
||
|
||
if (!ret) {
|
||
int ap_num = apList.ApNum;
|
||
int i;
|
||
int offset = 0;
|
||
char msg[256] = {0};
|
||
|
||
offset += snprintf(msg + offset, sizeof(msg) - offset, "AT+SSID=");
|
||
for (i = 0; i < ap_num; i++)
|
||
if(strcmp(apList.ApList[i].ssid, "") != 0){//过滤掉空SSID
|
||
offset += snprintf(msg + offset, sizeof(msg) - offset, "%s", apList.ApList[i].ssid);
|
||
offset += snprintf(msg + offset, sizeof(msg) - offset, ",");
|
||
}
|
||
bk_printf("AT+SSID=%s\r\n", msg);
|
||
bk_ble_send_ntf_value(strlen(msg), (uint8_t*)msg, 0, TEST_IDX_FF03_VAL_VALUE); //发送notify
|
||
os_free(apList.ApList);
|
||
}
|
||
}
|
||
|
||
void ble_report_state(int state)//发送状态值
|
||
{
|
||
char msg[32];
|
||
snprintf(msg, sizeof(msg), "AT+STATE=%d", state);
|
||
bk_ble_send_ntf_value(strlen(msg), (uint8_t*)msg, 0, TEST_IDX_FF03_VAL_VALUE);
|
||
}
|
||
|
||
void wifi_save_ssid_pass(const char *ssid, const char *password)//保存最后一次连接的wifi信息
|
||
{
|
||
EfErrCode result;
|
||
result = ef_set_env(BLE_WIFI_FLASH_KEY_SSID, ssid);
|
||
result = ef_set_env(BLE_WIFI_FLASH_KEY_PASS, password);
|
||
result = ef_save_env();
|
||
if(result == EF_NO_ERR)
|
||
bk_printf("wifi_save_ssid_pass success!\r\n");
|
||
else
|
||
bk_printf("wifi_save_ssid_pass failed!\r\n");
|
||
}
|
||
|
||
void wifi_status_cb(void *param)//注册WIFI回调
|
||
{
|
||
LinkStatusTypeDef *status = (LinkStatusTypeDef *)param;
|
||
//char *argv[] = {"mqtt_start"};
|
||
bk_printf("wifi_status_cb: conn_state=%d\r\n", status->conn_state);
|
||
if(status->conn_state == 12) {
|
||
bk_printf("WiFi: SSID=%s, PASSWORD=%s\r\n", last_ssid, last_password);
|
||
wifi_save_ssid_pass(last_ssid, last_password);
|
||
ble_report_state(3);//连接成功
|
||
//mqtt_start(1, argv);
|
||
}
|
||
}
|
||
|
||
void ble_connnect_wifi(uint8_t data[]){
|
||
char ssid[33] = {0};
|
||
char password[65] = {0};
|
||
char *p = strchr((char *)data + 8, ',');
|
||
rtos_reload_timer(&ble_idle);
|
||
if(p) {
|
||
size_t ssid_len = p - ((char *)data + 8);
|
||
if(ssid_len < sizeof(ssid)) {
|
||
strncpy(ssid, (char *)data + 8, ssid_len);
|
||
ssid[ssid_len] = '\0';
|
||
strncpy(password, p + 1, sizeof(password) - 1);
|
||
// 连接WiFi
|
||
strncpy(last_ssid, ssid, sizeof(last_ssid)-1);
|
||
strncpy(last_password, password, sizeof(last_password)-1);
|
||
ble_report_state(2); // 开始连接
|
||
connnect_wifi(ssid,password);
|
||
}
|
||
}
|
||
}
|
||
|
||
static void ble_idle_timeout_cb(void *parameter){
|
||
ble_report_state(0); // 无操作
|
||
}
|
||
|
||
void ble_notice_cb(ble_notice_t notice, void *param)
|
||
{
|
||
switch (notice) {
|
||
case BLE_5_STACK_OK:
|
||
bk_printf("ble stack ok");
|
||
break;
|
||
case BLE_5_WRITE_EVENT:
|
||
{
|
||
write_req_t *w_req = (write_req_t *)param;
|
||
bk_printf("write_cb:conn_idx:%d, prf_id:%d, add_id:%d, len:%d, data[0]:%02x\r\n",
|
||
w_req->conn_idx, w_req->prf_id, w_req->att_idx, w_req->len, w_req->value[0]);
|
||
bk_printf("data:%s\r\n",w_req->value);
|
||
uint8_t data[32]="";
|
||
memcpy(data, w_req->value, w_req->len);
|
||
if(strstr((char *)data,"AT+BIND")!=NULL){
|
||
//flash_read(0x60000,9,complete_name);
|
||
memset((void *)&dev_config, 0, sizeof(dev_config));
|
||
//flash_read((uint8_t *)&dev_config, sizeof(dev_config), (UINT32)0x1FD000);
|
||
//sscanf(w_req->value,"AT+BIND=%s", dev_config.bind_mac);
|
||
bk_printf("mac:%s\r\n",w_req->value);
|
||
//flash_write((uint8_t *)&dev_config, sizeof(dev_config), (UINT32)0x1FD000);
|
||
|
||
}
|
||
|
||
if(strstr((char *)data,"AT+SCAN")!=NULL){ //扫描wifi命令:AT+SCAN
|
||
bk_printf("start WiFi scan...\r\n");
|
||
rtos_reload_timer(&ble_idle);
|
||
ble_report_state(1); // 开始扫描
|
||
mhdr_scanu_reg_cb(scan_cb, 0);
|
||
bk_wlan_start_scan();
|
||
}
|
||
|
||
if(strncmp((char *)data, "AT+WIFI:", 8) == 0){ //连接WiFi命令:AT+WIFI:SSID,PASSWORD
|
||
ble_connnect_wifi(data);
|
||
}
|
||
break;
|
||
}
|
||
case BLE_5_READ_EVENT:
|
||
{
|
||
read_req_t *r_req = (read_req_t *)param;
|
||
bk_printf("read_cb:conn_idx:%d, prf_id:%d, add_id:%d\r\n",
|
||
r_req->conn_idx, r_req->prf_id, r_req->att_idx);
|
||
|
||
#if (CFG_BLE_VERSION == BLE_VERSION_5_2)
|
||
uint16_t length = 3;
|
||
r_req->value = kernel_malloc(length, KERNEL_MEM_KERNEL_MSG);
|
||
r_req->value[0] = 0x12;
|
||
r_req->value[1] = 0x34;
|
||
r_req->value[2] = 0x56;
|
||
|
||
app_gatts_rsp_t rsp;
|
||
rsp.token = r_req->token;
|
||
rsp.con_idx = r_req->conn_idx;
|
||
rsp.attr_handle = r_req->hdl;
|
||
rsp.status = GAP_ERR_NO_ERROR;
|
||
rsp.att_length = length;
|
||
rsp.value_length = length;
|
||
rsp.value = r_req->value;
|
||
|
||
app_ble_gatts_set_attr_value(rsp.attr_handle, rsp.value_length, rsp.value);
|
||
bk_ble_gatts_read_response(&rsp);
|
||
kernel_free(r_req->value);
|
||
#else
|
||
r_req->value[0] = 0x12;
|
||
r_req->value[1] = 0x34;
|
||
r_req->value[2] = 0x56;
|
||
r_req->length = 3;
|
||
#endif
|
||
|
||
break;
|
||
}
|
||
case BLE_5_REPORT_ADV:
|
||
{
|
||
recv_adv_t *r_ind = (recv_adv_t *)param;
|
||
#if 0
|
||
//adv]r_ind:actv_idx:0,evt_type:57 adv_addr:e5:e6:b6:09:28:37,rssi:188 data_len27
|
||
bk_printf("[%s]r_ind:actv_idx:%d,evt_type:%d adv_addr:%02x:%02x:%02x:%02x:%02x:%02x,rssi:%d\r\n",
|
||
((r_ind->evt_type&0x7) == 3)?"scan-rsp":((r_ind->evt_type&0x7) == 1)?"adv":"unknow",
|
||
r_ind->actv_idx,r_ind->evt_type, r_ind->adv_addr[0], r_ind->adv_addr[1], r_ind->adv_addr[2],
|
||
r_ind->adv_addr[3], r_ind->adv_addr[4], r_ind->adv_addr[5],r_ind->rssi);
|
||
|
||
bk_printf("data_len%d\r\n",r_ind->data_len);
|
||
#endif
|
||
//ble_parser_recv_adv(r_ind);
|
||
temperature_adv_callback(r_ind);
|
||
/*memset((void *)&dev_config, 0, sizeof(dev_config));
|
||
//flash_read(0x1FD000, sizeof(dev_config), (uint8_t *)&dev_config);
|
||
*Device_Mac=*(dev_config.bind_mac);*/
|
||
|
||
//uint8_t Device_Mac[6]={0x1c,0x16,0xC6,0x02,0x09,0xd6};
|
||
#if 0
|
||
if(r_ind->adv_addr[0]==Device_Mac[0]&&r_ind->adv_addr[1]==Device_Mac[1]&&r_ind->adv_addr[2]==Device_Mac[2]&&
|
||
r_ind->adv_addr[3]==Device_Mac[3]&&r_ind->adv_addr[4]==Device_Mac[4]&&r_ind->adv_addr[5]==Device_Mac[5])
|
||
{
|
||
bk_printf("[%s]r_ind:actv_idx:%d,evt_type:%d adv_addr:%02x:%02x:%02x:%02x:%02x:%02x,rssi:%d\r\n",
|
||
((r_ind->evt_type&0x7) == 3)?"scan-rsp":((r_ind->evt_type&0x7) == 1)?"adv":"unknow",
|
||
r_ind->actv_idx,r_ind->evt_type, r_ind->adv_addr[0], r_ind->adv_addr[1], r_ind->adv_addr[2],
|
||
r_ind->adv_addr[3], r_ind->adv_addr[4], r_ind->adv_addr[5],r_ind->rssi);
|
||
|
||
ble_parser_recv_adv(r_ind);
|
||
//int i;
|
||
//for(i=0;i<r_ind->data_len;i++){
|
||
// bk_printf("0x%02x,",r_ind->data[i]);
|
||
//}
|
||
|
||
bk_printf("\r\n");
|
||
|
||
memset(Temphex,0,sizeof(Temphex));
|
||
sprintf((char*)Temphex,"%02x%02x",r_ind->data[23],r_ind->data[24]);
|
||
memset(temphex,0,sizeof(temphex));
|
||
sprintf((char*)temphex,"%02x%02x",r_ind->data[25],r_ind->data[26]);
|
||
memset(bathex,0,sizeof(bathex));
|
||
sprintf((char*)bathex,"%02x",r_ind->data[27]);
|
||
bk_printf("%s\r\n", Temphex);
|
||
bk_printf("%s\r\n", temphex);
|
||
bk_printf("%s\r\n", bathex);
|
||
Temp=strtol((char*)Temphex,&endPtr, 16);
|
||
Temp2=strtol((char*)temphex,&endPtr2, 16);
|
||
bat=strtol((char*)bathex,&endPtr2, 16);
|
||
bk_printf("%d\r\n", Temp);
|
||
bk_printf("%d\r\n", Temp2);
|
||
bk_printf("%d\r\n", bat);
|
||
|
||
|
||
|
||
/*
|
||
temph=Temp/100;
|
||
templ=Temp%100/10;
|
||
temp2h=Temp2/100;
|
||
temp2l=Temp2%100/10;*/
|
||
}
|
||
#endif
|
||
break;
|
||
}
|
||
case BLE_5_REPORT_PER_ADV:
|
||
{
|
||
recv_adv_t *r_ind = (recv_adv_t *)param;
|
||
|
||
bk_printf("[%s]r_ind:actv_idx:%d,evt_type:%d rssi:%d data_len:%d data[0]:0x%x\r\n","per-adv",
|
||
r_ind->actv_idx,r_ind->evt_type, r_ind->rssi, r_ind->data_len, r_ind->data[0]);
|
||
break;
|
||
}
|
||
case BLE_5_MTU_CHANGE:
|
||
{
|
||
mtu_change_t *m_ind = (mtu_change_t *)param;
|
||
bk_printf("BLE_5_MTU_CHANGE:conn_idx:%d, mtu_size:%d\r\n", m_ind->conn_idx, m_ind->mtu_size);
|
||
break;
|
||
}
|
||
case BLE_5_PHY_IND_EVENT:
|
||
{
|
||
conn_phy_ind_t *set_phy = (conn_phy_ind_t *)param;
|
||
bk_printf("BLE_5_PHY_IND_EVENT:conn_idx:%d, tx_phy:0x%x, rx_phy:0x%x\r\n", set_phy->conn_idx, set_phy->tx_phy, set_phy->rx_phy);
|
||
break;
|
||
}
|
||
case BLE_5_CONNECT_EVENT:
|
||
{
|
||
//bk_ble_adv_stop(actv_idx_adv, ble_cmd_cb);
|
||
//bk_ble_delete_advertising(actv_idx_adv, ble_cmd_cb);
|
||
temperature_led_ble(1);
|
||
conn_ind_t *c_ind = (conn_ind_t *)param;
|
||
bk_printf("c_ind:conn_idx:%d, addr_type:%d, peer_addr:%02x:%02x:%02x:%02x:%02x:%02x\r\n",
|
||
c_ind->conn_idx, c_ind->peer_addr_type, c_ind->peer_addr[0], c_ind->peer_addr[1],
|
||
c_ind->peer_addr[2], c_ind->peer_addr[3], c_ind->peer_addr[4], c_ind->peer_addr[5]);
|
||
break;
|
||
}
|
||
case BLE_5_DISCONNECT_EVENT:
|
||
{
|
||
temperature_led_ble(0);
|
||
discon_ind_t *d_ind = (discon_ind_t *)param;
|
||
bk_printf("d_ind:conn_idx:%d,reason:%d\r\n", d_ind->conn_idx,d_ind->reason);
|
||
//断开连接之后 重新打开广播
|
||
|
||
//belon_ble_init_adv();
|
||
break;
|
||
}
|
||
case BLE_5_ATT_INFO_REQ:
|
||
{
|
||
att_info_req_t *a_ind = (att_info_req_t *)param;
|
||
bk_printf("a_ind:conn_idx:%d\r\n", a_ind->conn_idx);
|
||
a_ind->length = 128;
|
||
a_ind->status = ERR_SUCCESS;
|
||
break;
|
||
}
|
||
case BLE_5_CREATE_DB:
|
||
{
|
||
create_db_t *cd_ind = (create_db_t *)param;
|
||
rtos_init_timer(&ble_idle, BLE_IDLE_TIMEOUT_MS, ble_idle_timeout_cb, NULL);
|
||
rtos_start_timer(&ble_idle);
|
||
bk_printf("cd_ind:prf_id:%d, status:%d\r\n", cd_ind->prf_id, cd_ind->status);
|
||
break;
|
||
}
|
||
#if (BLE_CENTRAL)
|
||
case BLE_5_INIT_CONNECT_EVENT:
|
||
{
|
||
conn_ind_t *c_ind = (conn_ind_t *)param;
|
||
#if (CFG_BLE_VERSION == BLE_VERSION_5_2)
|
||
app_ble_get_peer_feature(c_ind->conn_idx);
|
||
app_ble_set_le_pkt_size(c_ind->conn_idx,LE_MAX_OCTETS);
|
||
app_ble_mtu_exchange(c_ind->conn_idx);
|
||
sdp_discover_all_service(c_ind->conn_idx);
|
||
#endif
|
||
bk_printf("BLE_5_INIT_CONNECT_EVENT:conn_idx:%d, addr_type:%d, peer_addr:%02x:%02x:%02x:%02x:%02x:%02x\r\n",
|
||
c_ind->conn_idx, c_ind->peer_addr_type, c_ind->peer_addr[0], c_ind->peer_addr[1],
|
||
c_ind->peer_addr[2], c_ind->peer_addr[3], c_ind->peer_addr[4], c_ind->peer_addr[5]);
|
||
break;
|
||
}
|
||
case BLE_5_INIT_DISCONNECT_EVENT:
|
||
{
|
||
discon_ind_t *d_ind = (discon_ind_t *)param;
|
||
bk_printf("BLE_5_INIT_DISCONNECT_EVENT:conn_idx:%d,reason:0x%x\r\n", d_ind->conn_idx,d_ind->reason);
|
||
break;
|
||
}
|
||
#endif
|
||
case BLE_5_INIT_CONN_PARAM_UPDATE_REQ_EVENT:
|
||
{
|
||
conn_param_req_t *d_ind = (conn_param_req_t *)param;
|
||
bk_printf("BLE_5_INIT_CONN_PARAM_UPDATE_REQ_EVENT:conn_idx:%d,intv_min:%d,intv_max:%d,time_out:%d\r\n",d_ind->conn_idx,
|
||
d_ind->intv_min,d_ind->intv_max,d_ind->time_out);
|
||
}break;
|
||
case BLE_5_INIT_CONN_PARAM_UPDATE_IND_EVENT:
|
||
{
|
||
conn_update_ind_t *d_ind = (conn_update_ind_t *)param;
|
||
bk_printf("BLE_5_INIT_CONN_PARAM_UPDATE_IND_EVENT:conn_idx:%d,interval:%d,time_out:%d,latency\r\n",d_ind->conn_idx,
|
||
d_ind->interval,d_ind->time_out,d_ind->latency);
|
||
}break;
|
||
case BLE_5_SDP_REGISTER_FAILED:
|
||
bk_printf("BLE_5_SDP_REGISTER_FAILED\r\n");
|
||
break;
|
||
case BLE_5_GAP_CMD_CMP_EVENT:
|
||
{
|
||
ble_cmd_cmp_evt_t *evt = (ble_cmd_cmp_evt_t *)param;
|
||
bk_printf("BLE_5_GAP_CMD_CMP_EVENT cmd:0x%x,conn_idx:%d,status:0x%x\r\n",evt->cmd,evt->conn_idx,evt->status);
|
||
break;
|
||
}
|
||
case BLE_5_TX_DONE:
|
||
{
|
||
atts_tx_t *evt = (atts_tx_t *)param;
|
||
bk_printf("BLE_5_TX_DONE conn_idx:%d,prf_id:%d,att_idx:%d,status:%d\r\n",
|
||
evt->conn_idx,evt->prf_id,evt->att_idx,evt->status);
|
||
}break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
static void ble(int argc, char **argv)
|
||
{
|
||
uint8_t adv_data[31];
|
||
uint8_t actv_idx;
|
||
if ((argc < 2) || (os_strcmp(argv[1], "help") == 0))
|
||
{
|
||
ble_command_usage();
|
||
return ;
|
||
}
|
||
if (os_strcmp(argv[1], "active") == 0) {
|
||
ble_set_notice_cb(ble_notice_cb);
|
||
ble_entry();
|
||
bk_ble_init();
|
||
}
|
||
if (os_strcmp(argv[1], "create_adv") == 0) {
|
||
actv_idx = app_ble_get_idle_actv_idx_handle(ADV_ACTV);
|
||
bk_printf("actv_idx = %d\r\n",actv_idx);
|
||
bk_ble_create_advertising(actv_idx, 7, 160, 160, ble_cmd_cb);
|
||
}
|
||
if (os_strcmp(argv[1], "set_adv_data") == 0) {
|
||
if (argc < 3){
|
||
bk_printf("\nThe number of param is wrong!\n");
|
||
return;
|
||
}
|
||
adv_data[0] = 0x0A;
|
||
adv_data[1] = 0x09;
|
||
#if (CFG_SOC_NAME == SOC_BK7238)
|
||
memcpy(&adv_data[2], "7238_BLE", 9);
|
||
#else
|
||
memcpy(&adv_data[2], "7252n_BLE", 9);
|
||
#endif
|
||
adv_data[11] = 0x03;
|
||
adv_data[12] = 0x19;
|
||
adv_data[13] = app_ble_env.dev_appearance & 0xFF;
|
||
adv_data[14] = (app_ble_env.dev_appearance >> 8) & 0xFF;
|
||
|
||
bk_ble_set_adv_data(os_strtoul(argv[2], NULL, 10), adv_data, 0xF, ble_cmd_cb);
|
||
}
|
||
if (os_strcmp(argv[1], "set_rsp_data") == 0) {
|
||
uint8_t adv_data_len;
|
||
if (argc < 3){
|
||
bk_printf("\nThe number of param is wrong!\n");
|
||
return;
|
||
}
|
||
adv_data[0] = 0x06;
|
||
adv_data[1] = 0x08;
|
||
#if (CFG_SOC_NAME == SOC_BK7238)
|
||
memcpy(&adv_data[2], "7238", 5);
|
||
#else
|
||
memcpy(&adv_data[2], "7252n", 5);
|
||
#endif
|
||
#if (BLE_HID_DEVICE)
|
||
adv_data[7] = 0x03;
|
||
adv_data[8] = 0x03;
|
||
adv_data[9] = 0x12;
|
||
adv_data[10] = 0x18;
|
||
adv_data_len = 0xB;
|
||
#else
|
||
adv_data_len = 0x7;
|
||
#endif
|
||
bk_ble_set_scan_rsp_data(os_strtoul(argv[2], NULL, 10), adv_data, adv_data_len, ble_cmd_cb);
|
||
}
|
||
if (os_strcmp(argv[1], "start_adv") == 0) {
|
||
if (argc < 3){
|
||
bk_printf("\nThe number of param is wrong!\n");
|
||
return;
|
||
}
|
||
bk_ble_start_advertising(os_strtoul(argv[2], NULL, 10), 0, ble_cmd_cb);
|
||
}
|
||
if (os_strcmp(argv[1], "stop_adv") == 0) {
|
||
if (argc < 3){
|
||
bk_printf("\nThe number of param is wrong!\n");
|
||
return;
|
||
}
|
||
bk_ble_stop_advertising(os_strtoul(argv[2], NULL, 10), ble_cmd_cb);
|
||
}
|
||
if (os_strcmp(argv[1], "delete_adv") == 0) {
|
||
if (argc < 3){
|
||
bk_printf("\nThe number of param is wrong!\n");
|
||
return;
|
||
}
|
||
bk_ble_delete_advertising(os_strtoul(argv[2], NULL, 10), ble_cmd_cb);
|
||
}
|
||
if (os_strcmp(argv[1], "create_scan") == 0) {
|
||
actv_idx = app_ble_get_idle_actv_idx_handle(SCAN_ACTV);
|
||
bk_ble_create_scaning(actv_idx, ble_cmd_cb);
|
||
}
|
||
if (os_strcmp(argv[1], "start_scan") == 0) {
|
||
if (argc < 3){
|
||
bk_printf("\nThe number of param is wrong!\n");
|
||
return;
|
||
}
|
||
bk_ble_start_scaning(os_strtoul(argv[2], NULL, 10), 100, 30, ble_cmd_cb);
|
||
}
|
||
if (os_strcmp(argv[1], "stop_scan") == 0) {
|
||
if (argc < 3){
|
||
bk_printf("\nThe number of param is wrong!\n");
|
||
return;
|
||
}
|
||
bk_ble_stop_scaning(os_strtoul(argv[2], NULL, 10), ble_cmd_cb);
|
||
}
|
||
if (os_strcmp(argv[1], "delete_scan") == 0) {
|
||
if (argc < 3){
|
||
bk_printf("\nThe number of param is wrong!\n");
|
||
return;
|
||
}
|
||
bk_ble_delete_scaning(os_strtoul(argv[2], NULL, 10), ble_cmd_cb);
|
||
}
|
||
if (os_strcmp(argv[1], "update_conn") == 0) {
|
||
if (argc < 3){
|
||
bk_printf("\nThe number of param is wrong!\n");
|
||
return;
|
||
}
|
||
bk_ble_update_param(os_strtoul(argv[2], NULL, 10), 50, 50, 0, 800);
|
||
}
|
||
if (os_strcmp(argv[1], "dis_conn") == 0) {
|
||
if (argc < 3){
|
||
bk_printf("\nThe number of param is wrong!\n");
|
||
return;
|
||
}
|
||
bk_ble_disconnect(os_strtoul(argv[2], NULL, 10));
|
||
}
|
||
if (os_strcmp(argv[1], "mtu_change") == 0) {
|
||
if (argc < 3){
|
||
bk_printf("\nThe number of param is wrong!\n");
|
||
return;
|
||
}
|
||
bk_ble_gatt_mtu_change(os_strtoul(argv[2], NULL, 10));
|
||
}
|
||
if (os_strcmp(argv[1], "init_adv") == 0) {
|
||
struct adv_param adv_info;
|
||
adv_info.channel_map = 7;
|
||
adv_info.duration = 0;
|
||
adv_info.prop = (1 << ADV_PROP_CONNECTABLE_POS) | (1 << ADV_PROP_SCANNABLE_POS);
|
||
adv_info.interval_min = 160;
|
||
adv_info.interval_max = 160;
|
||
adv_info.advData[0] = 0x09;
|
||
adv_info.advData[1] = 0x09;
|
||
memcpy(&adv_info.advData[2], "7252nBLE", 8);
|
||
adv_info.advDataLen = 10;
|
||
adv_info.respData[0] = 0x06;
|
||
adv_info.respData[1] = 0x08;
|
||
memcpy(&adv_info.respData[2], "7252n", 5);
|
||
adv_info.respDataLen = 7;
|
||
actv_idx = app_ble_get_idle_actv_idx_handle(ADV_ACTV);
|
||
bk_ble_adv_start(actv_idx, &adv_info, ble_cmd_cb);
|
||
}
|
||
if (os_strcmp(argv[1], "deinit_adv") == 0) {
|
||
if (argc < 3){
|
||
bk_printf("\nThe number of param is wrong!\n");
|
||
return;
|
||
}
|
||
bk_ble_adv_stop(os_strtoul(argv[2], NULL, 10), ble_cmd_cb);
|
||
}
|
||
if (os_strcmp(argv[1], "init_scan") == 0) {
|
||
struct scan_param scan_info;
|
||
scan_info.channel_map = 7;
|
||
scan_info.interval = 100;
|
||
scan_info.window = 30;
|
||
actv_idx = app_ble_get_idle_actv_idx_handle(SCAN_ACTV);
|
||
bk_ble_scan_start(actv_idx, &scan_info, ble_cmd_cb);
|
||
}
|
||
if (os_strcmp(argv[1], "deinit_scan") == 0) {
|
||
if (argc < 3){
|
||
bk_printf("\nThe number of param is wrong!\n");
|
||
return;
|
||
}
|
||
bk_ble_scan_stop(os_strtoul(argv[2], NULL, 10), ble_cmd_cb);
|
||
}
|
||
uint8_t conn_idx;
|
||
if (os_strcmp(argv[1], "con_create") == 0)
|
||
{
|
||
ble_set_notice_cb(ble_notice_cb);
|
||
conn_idx = app_ble_get_idle_conn_idx_handle(INIT_ACTV);
|
||
bk_printf("------------->conn_idx:%d\r\n",conn_idx);
|
||
sdp_set_notice_cb(sdp_event_cb);
|
||
bk_ble_create_init(conn_idx, ble_cmd_cb);
|
||
}
|
||
else if ((os_strcmp(argv[1], "con_start") == 0) && (argc >= 5))
|
||
{
|
||
struct bd_addr bdaddr;
|
||
unsigned char addr_type = ADDR_PUBLIC;
|
||
int addr_type_str = atoi(argv[3]);
|
||
int actv_idx_str = atoi(argv[4]);
|
||
bk_printf("idx:%d,addr_type:%d\r\n",actv_idx_str,addr_type_str);
|
||
if((addr_type_str > ADDR_RPA_OR_RAND)||(actv_idx_str >= 0xFF)){
|
||
return;
|
||
}
|
||
conn_idx = actv_idx_str;
|
||
hexstr2bin(argv[2], bdaddr.addr, GAP_BD_ADDR_LEN);
|
||
addr_type = addr_type_str;
|
||
bk_ble_init_set_connect_dev_addr(conn_idx,&bdaddr,addr_type);
|
||
bk_ble_init_start_conn(conn_idx,10000,ble_cmd_cb);
|
||
|
||
}
|
||
else if ((os_strcmp(argv[1], "con_stop") == 0) && (argc >= 3))
|
||
{
|
||
int actv_idx_str = atoi(argv[2]);
|
||
bk_printf("idx:%d\r\n",actv_idx_str);
|
||
if(actv_idx_str >= 0xFF){
|
||
return;
|
||
}
|
||
conn_idx = actv_idx_str;
|
||
bk_ble_init_stop_conn(conn_idx,ble_cmd_cb);
|
||
}
|
||
else if ((os_strcmp(argv[1], "con_dis") == 0) && (argc >= 3))
|
||
{
|
||
int actv_idx_str = atoi(argv[2]);
|
||
bk_printf("idx:%d\r\n",actv_idx_str);
|
||
if(actv_idx_str >= 0xFF){
|
||
return;
|
||
}
|
||
conn_idx = actv_idx_str;
|
||
app_ble_master_appm_disconnect(conn_idx);
|
||
}
|
||
else if (os_strcmp(argv[1], "con_read") == 0)
|
||
{
|
||
if(argc < 4){
|
||
bk_printf("param error\r\n");
|
||
return;
|
||
}
|
||
int actv_idx_str = atoi(argv[3]);
|
||
bk_printf("idx:%d\r\n",actv_idx_str);
|
||
if(actv_idx_str >= 0xFF){
|
||
return;
|
||
}
|
||
conn_idx = actv_idx_str;
|
||
int handle = atoi(argv[2]);
|
||
if(handle >=0 && handle <= 0xFFFF){
|
||
bk_ble_read_service_data_by_handle_req(conn_idx,handle);
|
||
}
|
||
else{
|
||
bk_printf("handle(%x) error\r\n",handle);
|
||
}
|
||
}else if (os_strcmp(argv[1], "con_write") == 0)
|
||
{
|
||
//cmd:ble con_write 24 0
|
||
if(argc < 4){
|
||
bk_printf("param error\r\n");
|
||
return;
|
||
}
|
||
int handle = atoi(argv[2]);
|
||
int actv_idx_str = atoi(argv[3]);
|
||
bk_printf("idx:%d\r\n",actv_idx_str);
|
||
if(actv_idx_str >= 0xFF){
|
||
return;
|
||
}
|
||
conn_idx = actv_idx_str;
|
||
unsigned char test_buf[4] = {0x01,0x02,0x22,0x32};
|
||
if(handle >=0 && handle <= 0xFFFF){
|
||
bk_ble_write_service_data_req(conn_idx,handle,4,test_buf);
|
||
}else{
|
||
bk_printf("handle(%x) error\r\n",handle);
|
||
}
|
||
}
|
||
}
|
||
|
||
static void ble_advertise_start(int argc, char **argv)
|
||
{
|
||
uint8_t adv_data[31];
|
||
uint8_t actv_idx;
|
||
uint8_t adv_data_len;
|
||
|
||
ble_set_notice_cb(ble_notice_cb);
|
||
ble_entry();
|
||
bk_ble_init();
|
||
|
||
rt_thread_mdelay(10);
|
||
|
||
actv_idx = app_ble_get_idle_actv_idx_handle(ADV_ACTV);
|
||
bk_printf("actv_idx = %d\r\n",actv_idx);
|
||
bk_ble_create_advertising(actv_idx, 7, 160, 160, ble_cmd_cb);
|
||
|
||
adv_data[0] = 0x0A;
|
||
adv_data[1] = 0x09;
|
||
memcpy(&adv_data[2], "7252n_BLE", 9);
|
||
|
||
rt_thread_mdelay(10);
|
||
bk_ble_set_adv_data(actv_idx, adv_data, 11, ble_cmd_cb);
|
||
|
||
adv_data[0] = 0x0A;
|
||
adv_data[1] = 0x08;
|
||
memcpy(&adv_data[2], "7252n_BLE", 9);
|
||
|
||
#if (BLE_HID_DEVICE)
|
||
adv_data[7] = 0x03;
|
||
adv_data[8] = 0x03;
|
||
adv_data[9] = 0x12;
|
||
adv_data[10] = 0x18;
|
||
adv_data_len = 0xB;
|
||
#else
|
||
adv_data_len = 11;
|
||
#endif
|
||
rt_thread_mdelay(10);
|
||
bk_ble_set_scan_rsp_data(actv_idx, adv_data, adv_data_len, ble_cmd_cb);
|
||
|
||
rt_thread_mdelay(10);
|
||
bk_ble_start_advertising(actv_idx, 0, ble_cmd_cb);
|
||
}
|
||
|
||
|
||
void belon_ble_active() {
|
||
ble_set_notice_cb(ble_notice_cb);
|
||
ble_entry();
|
||
bk_ble_init();
|
||
}
|
||
void belon_ble_init_scan() {
|
||
uint8_t actv_idx;
|
||
struct scan_param scan_info;
|
||
scan_info.channel_map = 7;
|
||
scan_info.interval = 100;
|
||
scan_info.window = 60; //window大小越接近interval 广播包漏掉的越少,但耗能更高
|
||
|
||
actv_idx = app_ble_get_idle_actv_idx_handle(SCAN_ACTV);
|
||
bk_printf("actv_idx_scan : (%d) \r\n",actv_idx);
|
||
bk_ble_scan_start(actv_idx, &scan_info, ble_cmd_cb);
|
||
}
|
||
|
||
void belon_ble_init_adv() {
|
||
//uint8_t actv_idx;
|
||
struct adv_param adv_info;
|
||
adv_info.channel_map = 7;
|
||
adv_info.duration = 0;
|
||
adv_info.prop = (1 << ADV_PROP_CONNECTABLE_POS) | (1 << ADV_PROP_SCANNABLE_POS);
|
||
adv_info.interval_min = 160;
|
||
adv_info.interval_max = 160;
|
||
adv_info.advData[0] = 0x09;
|
||
adv_info.advData[1] = 0x09;
|
||
memcpy(&adv_info.advData[2], "7252nBLE", 8);
|
||
adv_info.advDataLen = 10;
|
||
adv_info.respData[0] = 0x06;
|
||
adv_info.respData[1] = 0x08;
|
||
memcpy(&adv_info.respData[2], "7252n", 5);
|
||
adv_info.respDataLen = 7;
|
||
actv_idx_adv = app_ble_get_idle_actv_idx_handle(ADV_ACTV);
|
||
actv_idx_adv =1;
|
||
bk_printf("actv_idx_adv : (%d) \r\n",actv_idx_adv);
|
||
|
||
//bk_ble_create_advertising(actv_idx_adv, 7, 160, 160, ble_cmd_cb);
|
||
bk_ble_adv_start(actv_idx_adv, &adv_info, ble_cmd_cb);
|
||
}
|
||
|
||
void belon_ble_scan_start() {
|
||
bk_ble_start_scaning(0, 100, 30, ble_cmd_cb);
|
||
}
|
||
|
||
void belon_ble_scan_stop() {
|
||
bk_ble_stop_scaning(0,ble_cmd_cb);
|
||
}
|
||
/*
|
||
发起广播的参考指令
|
||
step 1:ble active
|
||
step 2:ble create_adv
|
||
step 3:ble set_adv_data 0
|
||
step 4:ble set_rsp_data 0
|
||
step 5:ble start_adv 0
|
||
*/
|
||
MSH_CMD_EXPORT(ble, ble command);
|
||
|
||
//直接发起广播参考指令:ble_advertise_start
|
||
MSH_CMD_EXPORT(ble_advertise_start, ble_advertise_start command);
|
||
#endif
|