|
|
@ -205,7 +205,7 @@ bool bluetooth_rx_buffer_processing = false; |
|
|
|
|
|
|
|
bool flag = true; |
|
|
|
uint32_t total = 0; |
|
|
|
#define timer_interval_num 100 |
|
|
|
#define timer_interval_num 10000 |
|
|
|
#define timer_interval_s (uint32_t)1000000 |
|
|
|
#define timer_interval_ms (uint32_t)1000 |
|
|
|
#define timer_interval_us (uint32_t)1 |
|
|
@ -216,6 +216,8 @@ uint32_t total = 0; |
|
|
|
void buffer_all_init(); |
|
|
|
void bluetooth_gatts_try_process_data(); |
|
|
|
void timer_set_pause_and_counter_zero(int group, int timer); |
|
|
|
void port_timer_delay_ms(uint64_t delay); |
|
|
|
void start_receive_data_to_buffer(uint16_t length, uint8_t *value); |
|
|
|
|
|
|
|
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) |
|
|
|
{ |
|
|
@ -572,35 +574,17 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i |
|
|
|
} |
|
|
|
case ESP_GATTS_WRITE_EVT: |
|
|
|
{ |
|
|
|
if (bluetooth_rx_buffer_processing == false) |
|
|
|
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle); |
|
|
|
if (!param->write.is_prep) |
|
|
|
{ |
|
|
|
if ((bluetooth_rx_buffer_len + param->write.len) > profile_b_buffer_size) |
|
|
|
{ |
|
|
|
ESP_LOGW(GATTS_TAG, "Array out of bounds"); |
|
|
|
timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0); |
|
|
|
example_write_event_env(gatts_if, &b_prepare_write_env, param); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
timer_set_pause_and_counter_zero(TIMER_GROUP_0, TIMER_0); |
|
|
|
timer_start(TIMER_GROUP_0, TIMER_0); |
|
|
|
|
|
|
|
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle); |
|
|
|
if (!param->write.is_prep) |
|
|
|
{ |
|
|
|
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value :", param->write.len); |
|
|
|
esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len); |
|
|
|
|
|
|
|
for (int i = 0; i < param->write.len; i++) |
|
|
|
{ |
|
|
|
bluetooth_rx_buffer[bluetooth_rx_buffer_len++] = param->write.value[i]; |
|
|
|
} |
|
|
|
} |
|
|
|
example_write_event_env(gatts_if, &b_prepare_write_env, param); |
|
|
|
break; |
|
|
|
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value :", param->write.len); |
|
|
|
esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len); |
|
|
|
start_receive_data_to_buffer(param->write.len, param->write.value); |
|
|
|
// for (int i = 0; i < param->write.len; i++) |
|
|
|
// { |
|
|
|
// bluetooth_rx_buffer[bluetooth_rx_buffer_len++] = param->write.value[i]; |
|
|
|
// } |
|
|
|
} |
|
|
|
timer_set_pause_and_counter_zero(TIMER_GROUP_0, TIMER_0); |
|
|
|
ESP_LOGW(GATTS_TAG, "Processing buffer data,write data to buferr warning"); |
|
|
|
example_write_event_env(gatts_if, &b_prepare_write_env, param); |
|
|
|
break; |
|
|
|
} |
|
|
@ -725,19 +709,11 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_ |
|
|
|
/*********************************************************************************************************************** |
|
|
|
* *******************************************************timer******************************************************* * |
|
|
|
***********************************************************************************************************************/ |
|
|
|
static bool IRAM_ATTR timer_group_isr_callback(void *args) |
|
|
|
{ |
|
|
|
// timer_pause(TIMER_GROUP_0, TIMER_0); |
|
|
|
// timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, timer_interval_num * ((80 * timer_interval_s) / 80)); |
|
|
|
|
|
|
|
// flag = !flag; |
|
|
|
|
|
|
|
// printf("esp_bt_controller_init"); |
|
|
|
// ESP_LOGI("timer","------------------"); |
|
|
|
|
|
|
|
bluetooth_rx_buffer_processing = true; |
|
|
|
return pdFALSE; |
|
|
|
} |
|
|
|
// static bool IRAM_ATTR timer_group_isr_callback(void *args) |
|
|
|
// { |
|
|
|
// bluetooth_rx_buffer_processing = true; |
|
|
|
// return pdFALSE; |
|
|
|
// } |
|
|
|
|
|
|
|
static void timer_group_init(int group, int timer, bool auto_reload, int timer_interval, uint32_t timer_unit_time) |
|
|
|
{ |
|
|
@ -763,11 +739,10 @@ static void timer_group_init(int group, int timer, bool auto_reload, int timer_i |
|
|
|
|
|
|
|
timer_enable_intr(group, timer); |
|
|
|
|
|
|
|
timer_isr_callback_add(group, timer, timer_group_isr_callback, NULL, 0); |
|
|
|
// timer_isr_callback_add(group, timer, timer_group_isr_callback, NULL, 0); |
|
|
|
|
|
|
|
// timer_start(group, timer); |
|
|
|
timer_pause(group, timer); |
|
|
|
timer_set_counter_value(group, timer, 0); |
|
|
|
} |
|
|
|
|
|
|
|
/*********************************************************************************************************************** |
|
|
@ -777,7 +752,8 @@ void app_main(void) |
|
|
|
{ |
|
|
|
esp_err_t ret; |
|
|
|
|
|
|
|
timer_group_init(TIMER_GROUP_0, TIMER_0, true, timer_interval_num, timer_interval_ms); |
|
|
|
// timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_interval_num, timer_interval_ms); |
|
|
|
timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_interval_num, timer_interval_ms); |
|
|
|
|
|
|
|
// Initialize NVS. |
|
|
|
ret = nvs_flash_init(); |
|
|
@ -899,8 +875,8 @@ void bluetooth_gatts_try_process_data() |
|
|
|
//接收完毕 |
|
|
|
if (bluetooth_rx_buffer_processing) |
|
|
|
{ |
|
|
|
//关闭定时器 |
|
|
|
timer_set_pause_and_counter_zero(TIMER_GROUP_0, TIMER_0); |
|
|
|
//开启定时器 |
|
|
|
port_timer_delay_ms(100); |
|
|
|
//打印输出 |
|
|
|
ESP_LOGI(GATTS_TAG, "%s", &bluetooth_rx_buffer[0]); |
|
|
|
|
|
|
@ -908,10 +884,13 @@ void bluetooth_gatts_try_process_data() |
|
|
|
json_tmp = cJSON_Parse(&bluetooth_rx_buffer[0]); |
|
|
|
if (json_tmp == NULL) |
|
|
|
{ |
|
|
|
ESP_LOGE("ERROR","json_tmp null"); |
|
|
|
ESP_LOGE("ERROR", "json_tmp null"); |
|
|
|
cJSON_Delete(json_tmp); |
|
|
|
buffer_all_init(); |
|
|
|
bluetooth_rx_buffer_processing = false; |
|
|
|
while (1) |
|
|
|
{ |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
|
ch = json_tmp->child; |
|
|
@ -955,3 +934,34 @@ void bluetooth_gatts_try_process_data() |
|
|
|
bluetooth_rx_buffer_processing = false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void port_timer_delay_ms(uint64_t delay) |
|
|
|
{ |
|
|
|
uint64_t timer_count = 0; |
|
|
|
timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0); |
|
|
|
timer_start(TIMER_GROUP_0, TIMER_0); |
|
|
|
|
|
|
|
while (timer_count < (delay * 1000)) |
|
|
|
{ |
|
|
|
timer_get_counter_value(TIMER_GROUP_0, TIMER_0, &timer_count); |
|
|
|
} |
|
|
|
|
|
|
|
timer_pause(TIMER_GROUP_0, TIMER_0); |
|
|
|
} |
|
|
|
|
|
|
|
void start_receive_data_to_buffer(uint16_t length, uint8_t *value) |
|
|
|
{ |
|
|
|
bluetooth_rx_buffer_processing = true; |
|
|
|
timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0); |
|
|
|
//判断是否buffer越界 |
|
|
|
if ((length + bluetooth_rx_buffer_len) > profile_b_buffer_size) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
//写入到buffer |
|
|
|
for (int i = 0; i < length; i++) |
|
|
|
{ |
|
|
|
bluetooth_rx_buffer[bluetooth_rx_buffer_len++] = value[i]; |
|
|
|
} |
|
|
|
} |