Commit eab9ac81 authored by 薛小虎's avatar 薛小虎

feat: 添加蓝牙握手

parent e5a7083d
...@@ -6,5 +6,6 @@ idf_component_register(SRCS "gatts_table_creat_demo.c" ...@@ -6,5 +6,6 @@ idf_component_register(SRCS "gatts_table_creat_demo.c"
"Protocol_Lib.c" "Protocol_Lib.c"
"Protocol_User.c" "Protocol_User.c"
"MCU_Core_Protocol.c" "MCU_Core_Protocol.c"
"app_Ble_User.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
EMBED_TXTFILES ${project_dir}/server_certs/ca_cert.pem) EMBED_TXTFILES ${project_dir}/server_certs/ca_cert.pem)
#include "app_Ble_User.h"
#include "gatts_table_creat_demo.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#define TAG "APP_BLE_USER"
Ble_User_Control_Struct Ble_User_Ctrl;
void Ble_Connect_Event_Pro(void )
{
Ble_User_Ctrl.Ble_Sts = Ble_Conn;
}
void Ble_DisConnect_Event_Pro(void )
{
Ble_User_Ctrl.Ble_Sts = Ble_Idle;
}
uint8_t tp_Ble_Crc(uint8_t *data, uint32_t len )
{
uint8_t val = 0;
for(uint32_t i = 0; i<len; i++)
{
val ^= data[i];
}
return val;
}
typedef union
{
struct
{
uint8_t FrameHeader;
uint8_t Instruct;
uint8_t Lenth;
uint8_t Param[252];
}Msg;
uint8_t Data[255];
}app_Ble_User_Data_Rec_Union;
app_Ble_User_Data_Rec_Union BleRec;
app_Ble_User_Data_Rec_Union BleTrans;
typedef struct
{
uint32_t RecLen;
uint32_t TotalLen;
uint8_t Sts;
uint8_t Receive;
}app_Ble_Rec_Data_Ctrl_Struct;
app_Ble_Rec_Data_Ctrl_Struct RecCtrl;
uint8_t ble_key[32] = "1BJ7J8S";
uint8_t ble_uerid[32] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 0x30, 0x31,0x32,0x33,0x34,0x35,0x36,0x00};
uint8_t ble_pwd[32] = {'A','D','E','B','O','N','2','0','2','3','Z', 'H','I','D','O','U'};
uint8_t flavor[4] = {0, 0, 0, 0x33};
void Clear_Rx_Buffer(void )
{
for(int i = 0; i< sizeof(BleRec.Data); i++)
{
BleRec.Data[i] = 0;
}
}
uint8_t Ble_Get_User_Info(void )
{
uint8_t i = 0;
for(i = 0; i<32;i++)
{
ble_uerid[i] = i;
ble_pwd[i] = i+64;
}
return 1;
}
void Get_Ble_Raw_Data(uint8_t *data, uint32_t len)
{
uint8_t err = 0;
uint8_t crc = 0;
if(len > 1024)
{
return;
}
if(data[0] == 0x24)
{
if(RecCtrl.Sts == BleRec_Idle)
{
printf("receive data:");
for(uint16_t i = 0; i< len; i++)
{
printf(" %x ", data[i]);
}
printf("\r\n");
printf("\r\n");
RecCtrl.RecLen = 0;
memcpy(( uint8_t * )&BleRec.Data[RecCtrl.RecLen], data, len);
RecCtrl.TotalLen = BleRec.Msg.Lenth +1 ;
RecCtrl.RecLen += len;
if(RecCtrl.RecLen >= RecCtrl.TotalLen)
{
crc = tp_Ble_Crc(BleRec.Data, RecCtrl.TotalLen - 2);
printf("receive data:");
for(uint16_t i = 0; i< RecCtrl.TotalLen; i++)
{
printf(" %x ", BleRec.Data[i]);
}
printf("\r\n");
printf("rec crc = %d\r\n", crc);
if((BleRec.Data[RecCtrl.TotalLen - 1] != 0x0A) || (crc != BleRec.Data[RecCtrl.TotalLen - 2]))
{
Clear_Rx_Buffer();
RecCtrl.Sts = Ble_Idle;
}
else
{
RecCtrl.Sts = BleRec_Suc;
}
}
else
{
RecCtrl.Sts = BleRec_Start;
printf("BleRec_Start \r\n");
}
}
}
else
{
if(RecCtrl.Sts == BleRec_Start)
{
if((RecCtrl.RecLen+len) <= RecCtrl.TotalLen)
{
printf("receive data:");
for(uint16_t i = 0; i< len; i++)
{
printf(" %x ", data[i]);
}
printf("\r\n");
printf("\r\n");
memcpy(( uint8_t * )&BleRec.Data[RecCtrl.RecLen], data, len);
RecCtrl.RecLen += len;
if(RecCtrl.RecLen >= RecCtrl.TotalLen)
{
crc = tp_Ble_Crc(BleRec.Data, RecCtrl.TotalLen - 2);
printf("receive data:");
for(uint16_t i = 0; i< RecCtrl.TotalLen; i++)
{
printf(" %x ", BleRec.Data[i]);
}
printf("\r\n");
printf("rec crc = %d\r\n", crc);
if((BleRec.Data[RecCtrl.TotalLen - 1] != 0x0A) || (crc != BleRec.Data[RecCtrl.TotalLen - 2]))
{
Clear_Rx_Buffer();
RecCtrl.Sts = Ble_Idle;
}
else
{
RecCtrl.Sts = BleRec_Suc;
}
}
}
else
{
//len err
Clear_Rx_Buffer();
RecCtrl.Sts = Ble_Idle;
}
}
}
uint8_t total_frame = 0;
uint8_t trans_len = 0;
if(RecCtrl.Sts == BleRec_Suc)
{
if(BleRec.Msg.Instruct == 0x30)
{
//Authr suc
BleTrans.Msg.FrameHeader = 0x24;
BleTrans.Msg.Instruct = 0x58;
BleTrans.Msg.Lenth = 104;
if((BleTrans.Msg.Lenth+1) % 20 == 0)
{
total_frame = (BleTrans.Msg.Lenth+1) / 20;
}
else
{
total_frame = (BleTrans.Msg.Lenth+1) / 20 + 1;
}
if(Ble_Get_User_Info())
{
memcpy(( uint8_t * )&BleTrans.Msg.Param[0], ble_key, 32);
memcpy(( uint8_t * )&BleTrans.Msg.Param[32], ble_uerid, 32);
memcpy(( uint8_t * )&BleTrans.Msg.Param[64], ble_pwd, 32);
memcpy(( uint8_t * )&BleTrans.Msg.Param[96], flavor, 4);
}
BleTrans.Data[BleTrans.Msg.Lenth - 1] = tp_Ble_Crc(BleTrans.Data, BleTrans.Msg.Lenth - 1);
BleTrans.Data[BleTrans.Msg.Lenth] = 0x0A;
for(uint8_t i = 0; i < total_frame; i++)
{
if(((BleTrans.Msg.Lenth+1) - 20*i) >= 20)
{
trans_len = 20;
}
else
{
trans_len = (BleTrans.Msg.Lenth+1) - 20*i;
}
printf("trans_len = %d\r\n", trans_len);
printf("trans data:");
for(uint16_t j = 0; j< trans_len; j++)
{
printf(" %x ", BleTrans.Data[j + i*20]);
}
printf("\r\n");
printf("\r\n");
bsp_Ble_Gatts_Send_Indicate( trans_len, &BleTrans.Data[i*20]);
// vTaskDelay(1);
}
}
else
{
}
RecCtrl.Sts = BleRec_Idle;
}
}
void test_test(void )
{
uint8_t total_frame = 0;
uint8_t trans_len = 0;
vTaskDelay(5000);
if(RecCtrl.Sts == BleRec_Idle)
{
}
else
{
return;
}
BleTrans.Msg.FrameHeader = 0x24;
BleTrans.Msg.Instruct = 0x14;
BleTrans.Msg.Lenth = 9;
if((BleTrans.Msg.Lenth+1) % 20 == 0)
{
total_frame = (BleTrans.Msg.Lenth+1) / 20;
}
else
{
total_frame = (BleTrans.Msg.Lenth+1) / 20 + 1;
}
BleTrans.Msg.Param[0] = 1;
BleTrans.Msg.Param[1] = 80;
BleTrans.Msg.Param[2] = 0;
BleTrans.Msg.Param[3] = 3;
BleTrans.Msg.Param[4] = 0;
// if(Ble_Get_User_Info())
// {
// memcpy(( uint8_t * )&BleTrans.Msg.Param[0], ble_key, 32);
// memcpy(( uint8_t * )&BleTrans.Msg.Param[32], ble_uerid, 32);
// memcpy(( uint8_t * )&BleTrans.Msg.Param[64], ble_pwd, 32);
// memcpy(( uint8_t * )&BleTrans.Msg.Param[96], flavor, 4);
// }
BleTrans.Data[BleTrans.Msg.Lenth - 1] = tp_Ble_Crc(BleTrans.Data, BleTrans.Msg.Lenth - 1);
BleTrans.Data[BleTrans.Msg.Lenth] = 0x0A;
for(uint8_t i = 0; i < total_frame; i++)
{
if(((BleTrans.Msg.Lenth+1) - 20*i) >= 20)
{
trans_len = 20;
}
else
{
trans_len = (BleTrans.Msg.Lenth+1) - 20*i;
}
printf("trans_len = %d\r\n", trans_len);
printf("trans data:");
for(uint16_t j = 0; j< trans_len; j++)
{
printf(" %x ", BleTrans.Data[j + i*20]);
}
printf("\r\n");
printf("\r\n");
bsp_Ble_Gatts_Send_Indicate( trans_len, &BleTrans.Data[i*20]);
}
}
#ifndef _APP_BLE_USER_H_
#define _APP_BLE_USER_H_
#include <stdlib.h>
typedef enum
{
Ble_Idle = 0,
Ble_Conn,
Ble_Pair,
Ble_Authr,
}Ble_Sts_Enum;
typedef enum
{
BleRec_Idle = 0,
BleRec_Start,
BleRec_Suc,
}Ble_Rec_Sts;
typedef struct
{
uint8_t Ble_Sts;
}Ble_User_Control_Struct;
extern Ble_User_Control_Struct Ble_User_Ctrl;
void Get_Ble_Raw_Data(uint8_t *data, uint32_t len);
void test_test(void );
#endif
\ No newline at end of file
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
#include "Protocol_CRC16.h" #include "Protocol_CRC16.h"
#include "Protocol_Lib.h" #include "Protocol_Lib.h"
#include "Protocol_User.h" #include "Protocol_User.h"
#include "app_Ble_User.h"
typedef unsigned char uint8_t; typedef unsigned char uint8_t;
...@@ -84,8 +85,21 @@ static void tx_task(void *arg) ...@@ -84,8 +85,21 @@ static void tx_task(void *arg)
// uint8_t bbb[2] = {0x55,0x99}; // uint8_t bbb[2] = {0x55,0x99};
// Protocol_Send(0x11,bbb,2); // Protocol_Send(0x11,bbb,2);
//sendData(TX_TASK_TAG, (const char*)Uart_Data.data); //sendData(TX_TASK_TAG, (const char*)Uart_Data.data);
Protocol_Send_Service(); // Protocol_Send_Service();
uint8_t temp[20];
for(int i=0; i<20; i++)
{
temp[i] = i+0x55;
}
test_test();
vTaskDelay(10 / portTICK_PERIOD_MS); vTaskDelay(10 / portTICK_PERIOD_MS);
// bsp_Ble_Gatts_Send_Indicate( 20, temp);
} }
} }
...@@ -144,9 +158,10 @@ static prepare_type_env_t prepare_write_env; ...@@ -144,9 +158,10 @@ static prepare_type_env_t prepare_write_env;
#ifdef CONFIG_SET_RAW_ADV_DATA #ifdef CONFIG_SET_RAW_ADV_DATA
static uint8_t raw_adv_data[] = { static uint8_t raw_adv_data[] = {
0x02, 0x01, 0x02, 0x02, 0x01, 0x02,
0x03, 0x03, 0x69, 0x67,
0x09, 0xff, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x0d, 0x09, 'Q', 'I', 'A', 'N', 'J', 'I', 'A', 'N', 'V', 'i', 'v', 'a', 0x0d, 0x09, 'Q', 'I', 'A', 'N', 'J', 'I', 'A', 'N', 'V', 'i', 'v', 'a',
0x09, 0xff, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x03, 0x03, 0x69, 0x67,
// 0x08, 0x16, 0x60, 0xb3, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08, 0x16, 0x60, 0xb3, 0x00, 0x00, 0x00, 0x00, 0x00,
}; };
static uint8_t raw_scan_rsp_data[] = { static uint8_t raw_scan_rsp_data[] = {
...@@ -304,7 +319,7 @@ static const esp_gatts_attr_db_t gatt_db2[HRS_IDX_NB2] = ...@@ -304,7 +319,7 @@ static const esp_gatts_attr_db_t gatt_db2[HRS_IDX_NB2] =
/* Characteristic Value */ /* Characteristic Value */
[IDX_CHAR_VAL_A2] = [IDX_CHAR_VAL_A2] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_128, (uint8_t *)&char_1_uuid, ESP_GATT_PERM_WRITE , {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_128, (uint8_t *)&char_1_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE ,
GATTS_DEMO_CHAR_VAL_LEN_MAX, 0, (uint8_t *)((void *)0)}}, GATTS_DEMO_CHAR_VAL_LEN_MAX, 0, (uint8_t *)((void *)0)}},
/* Client Characteristic Configuration Descriptor */ /* Client Characteristic Configuration Descriptor */
...@@ -315,6 +330,20 @@ static const esp_gatts_attr_db_t gatt_db2[HRS_IDX_NB2] = ...@@ -315,6 +330,20 @@ static const esp_gatts_attr_db_t gatt_db2[HRS_IDX_NB2] =
void bsp_Ble_Gatts_Send_Indicate( uint8_t len, uint8_t *data)
{
// if(Ble_User_Ctrl.Ble_Sts != Ble_Idle)
{
// if(en)
{
esp_ble_gatts_send_indicate(heart_rate_profile_tab[0].gatts_if,
heart_rate_profile_tab[0].conn_id,
46,
len, data, false);
}
}
}
typedef struct typedef struct
{ {
uint8_t en; uint8_t en;
...@@ -567,8 +596,20 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_ ...@@ -567,8 +596,20 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
// the data length of gattc write must be less than GATTS_DEMO_CHAR_VAL_LEN_MAX. // the data length of gattc write must be less than GATTS_DEMO_CHAR_VAL_LEN_MAX.
ESP_LOGI(GATTS_TABLE_TAG, "GATT_WRITE_EVT, handle = %d, value len = %d, value :", param->write.handle, param->write.len); ESP_LOGI(GATTS_TABLE_TAG, "GATT_WRITE_EVT, handle = %d, value len = %d, value :", param->write.handle, param->write.len);
esp_log_buffer_hex(GATTS_TABLE_TAG, param->write.value, param->write.len); esp_log_buffer_hex(GATTS_TABLE_TAG, param->write.value, param->write.len);
if (heart_rate_handle_table[IDX_CHAR_CFG_A] == param->write.handle && param->write.len == 2){
// printf("descr_value = %d\r\n", heart_rate_handle_table[IDX_CHAR_CFG_A2]);
// printf("handle = %d\r\n", param->write.handle);
if (heart_rate_handle_table2[IDX_CHAR_VAL_A2] == param->write.handle)
{
Get_Ble_Raw_Data(param->write.value, param->write.len);
}
if (heart_rate_handle_table2[IDX_CHAR_CFG_A2] == param->write.handle && param->write.len == 2){
uint16_t descr_value = param->write.value[1]<<8 | param->write.value[0]; uint16_t descr_value = param->write.value[1]<<8 | param->write.value[0];
if (descr_value == 0x0001){ if (descr_value == 0x0001){
ESP_LOGI(GATTS_TABLE_TAG, "notify enable"); ESP_LOGI(GATTS_TABLE_TAG, "notify enable");
uint8_t notify_data[15]; uint8_t notify_data[15];
...@@ -577,7 +618,7 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_ ...@@ -577,7 +618,7 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
notify_data[i] = i % 0xff; notify_data[i] = i % 0xff;
} }
//the size of notify_data[] need less than MTU size //the size of notify_data[] need less than MTU size
esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, heart_rate_handle_table[IDX_CHAR_VAL_A], esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, heart_rate_handle_table2[IDX_CHAR_CFG_A2],
sizeof(notify_data), notify_data, false); sizeof(notify_data), notify_data, false);
}else if (descr_value == 0x0002){ }else if (descr_value == 0x0002){
ESP_LOGI(GATTS_TABLE_TAG, "indicate enable"); ESP_LOGI(GATTS_TABLE_TAG, "indicate enable");
......
...@@ -12,10 +12,27 @@ ...@@ -12,10 +12,27 @@
enum enum
{ {
IDX_SVC, IDX_SVC,
// IDX_CHAR_2A29,
// IDX_CHAR_VAL_2A29,
// IDX_CHAR_CFG_2A29,
// IDX_CHAR_2A24,
// IDX_CHAR_VAL_2A24,
// IDX_CHAR_CFG_2A24,
IDX_CHAR_A, IDX_CHAR_A,
IDX_CHAR_VAL_A, IDX_CHAR_VAL_A,
IDX_CHAR_CFG_A, IDX_CHAR_CFG_A,
// IDX_CHAR_2A27,
// IDX_CHAR_VAL_2A27,
// IDX_CHAR_CFG_2A27,
// IDX_CHAR_2A26,
// IDX_CHAR_VAL_2A26,
// IDX_CHAR_CFG_2A26,
HRS_IDX_NB, HRS_IDX_NB,
}; };
...@@ -33,3 +50,10 @@ enum ...@@ -33,3 +50,10 @@ enum
HRS_IDX_NB2, HRS_IDX_NB2,
}; };
int sendData(const char* logName, const char* data);
void bsp_Ble_Gatts_Send_Indicate(uint8_t len, uint8_t *data);
#endif
\ No newline at end of file
...@@ -1526,7 +1526,7 @@ CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_MAX_CERTS=200 ...@@ -1526,7 +1526,7 @@ CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_MAX_CERTS=200
# end of Certificate Bundle # end of Certificate Bundle
# CONFIG_MBEDTLS_ECP_RESTARTABLE is not set # CONFIG_MBEDTLS_ECP_RESTARTABLE is not set
# CONFIG_MBEDTLS_CMAC_C is not set CONFIG_MBEDTLS_CMAC_C=y
CONFIG_MBEDTLS_HARDWARE_AES=y CONFIG_MBEDTLS_HARDWARE_AES=y
# CONFIG_MBEDTLS_GCM_SUPPORT_NON_AES_CIPHER is not set # CONFIG_MBEDTLS_GCM_SUPPORT_NON_AES_CIPHER is not set
CONFIG_MBEDTLS_HARDWARE_MPI=y CONFIG_MBEDTLS_HARDWARE_MPI=y
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment