diff --git a/main/gatts_demo.c b/main/gatts_demo.c index 3a5d389..f5c8a80 100644 --- a/main/gatts_demo.c +++ b/main/gatts_demo.c @@ -244,6 +244,10 @@ uint32_t total = 0; #define set_position "setPosition" #define get_status "getStatus" +esp_gatts_cb_event_t constructor_event; +esp_gatt_if_t constructor_gatts_if; +esp_ble_gatts_cb_param_t *constructor_param; + /*********************************************************************************************************************** * **********************************************user_function_statement********************************************** * ***********************************************************************************************************************/ @@ -254,6 +258,8 @@ void port_timer_delay_ms(uint64_t delay); void start_receive_data_to_buffer(uint16_t length, uint8_t *value); bool parse_rxbuffer_and_validation_data(cJSON **json_tmp); bool parse_json_to_struct(cJSON *ch); +void receipt_json_set_position(); +void gatts_profile_a_constructor(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param); bluetooth_processer_t s_bluetooth_processer = { .bluetooth_processer_rx_buf = bluetooth_rx_buffer, @@ -419,6 +425,7 @@ void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) { + gatts_profile_a_constructor(event, gatts_if, param); switch (event) { case ESP_GATTS_REG_EVT: @@ -580,8 +587,8 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i break; } case ESP_GATTS_DISCONNECT_EVT: - // ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x%x", param->disconnect.reason); - // esp_ble_gap_start_advertising(&adv_params); + ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x%x", param->disconnect.reason); + esp_ble_gap_start_advertising(&adv_params); break; case ESP_GATTS_CONF_EVT: // ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONF_EVT, status %d attr_handle %d", param->conf.status, param->conf.handle); @@ -716,6 +723,9 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i } break; case ESP_GATTS_DISCONNECT_EVT: + ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x%x", param->disconnect.reason); + esp_ble_gap_start_advertising(&adv_params); + break; case ESP_GATTS_OPEN_EVT: case ESP_GATTS_CANCEL_OPEN_EVT: case ESP_GATTS_CLOSE_EVT: @@ -953,10 +963,12 @@ void bluetooth_gatts_try_process_data() { ESP_LOGI(GATTS_TAG, set_position); // motor_cmd_set_position(s_bluetooth_processer.speed_level, s_bluetooth_processer.position, s_bluetooth_processer.direction); + receipt_json_set_position(); } if (strcmp(s_bluetooth_processer.order, get_status) == 0) { ESP_LOGI(GATTS_TAG, get_status); + receipt_json_get_status(); } // if (strcmp(s_bluetooth_processer.order, "deviceStatusReport") == 0) // { @@ -1078,4 +1090,73 @@ bool parse_json_to_struct(cJSON *ch) } return s_bluetooth_processer.cmd_flag; +} + +void receipt_json_set_position() +{ + cJSON *pRoot = cJSON_CreateObject(); //创建一个对象 + if (!pRoot) + { + return; + } + + cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点 + cJSON_AddNumberToObject(pRoot, "code", s_bluetooth_processer.code); + cJSON_AddStringToObject(pRoot, "info", "success"); + cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index); + char *szJson = cJSON_Print(pRoot); + + if (szJson != NULL) + { + ESP_LOGI(GATTS_TAG, "%s", szJson); + + // esp_gatt_rsp_t rsp; + // memset(&rsp, 0, sizeof(esp_gatt_rsp_t)); + // rsp.attr_value.handle = constructor_param->read.handle; + // rsp.attr_value.len = 4; + // rsp.attr_value.value[0] = 0x11; + // rsp.attr_value.value[1] = 0x22; + // rsp.attr_value.value[2] = 0x33; + // rsp.attr_value.value[3] = 0x44; + + // esp_ble_gatts_send_response(constructor_gatts_if, constructor_param->read.conn_id, + // constructor_param->read.trans_id, ESP_GATT_OK, &rsp); + + free(szJson); + } + + cJSON_Delete(pRoot); +} + +void receipt_json_get_status() +{ + cJSON *pRoot = cJSON_CreateObject(); //创建一个对象 + if (!pRoot) + { + return; + } + + cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点 + cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index); + cJSON_AddStringToObject(pRoot, "deviceState", s_bluetooth_processer.deviceState); + cJSON_AddNumberToObject(pRoot, "deviceException", s_bluetooth_processer.deviceException); + cJSON_AddStringToObject(pRoot, "deviceExceptionInfo", s_bluetooth_processer.deviceExceptionInfo); + cJSON_AddNumberToObject(pRoot, "position", s_bluetooth_processer.position); + + char *szJson = cJSON_Print(pRoot); + + if (szJson != NULL) + { + ESP_LOGI(GATTS_TAG, "%s", szJson); + free(szJson); + } + + cJSON_Delete(pRoot); +} + +void gatts_profile_a_constructor(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) +{ + constructor_event = event; + constructor_gatts_if = gatts_if; + constructor_param = param; } \ No newline at end of file