|
|
#include "esp_gatts_api.h"
#include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include "string.h"
//
#include "ble_spp_server_demo.h"
#include "timer_u.h"
#define MAIN_TAG "MAIN"
static char bluetooth_tx_buffer[profile_b_buffer_size] = {0};
char bluetooth_rx_buffer[profile_b_buffer_size] = {0};
void receipt_json_set_position(); void receipt_json_get_status(); void bluetooth_rx_buffer_format_receipt(); void bluetooth_rx_buffer_send_indicate(); void string_copy_by_num(char *dest, const char *src, uint8_t count, uint8_t num);
bluetooth_processer_t s_bluetooth_processer = { .bluetooth_processer_rx_buf = bluetooth_rx_buffer, .bluetooth_processer_rx_buf_size = sizeof(bluetooth_rx_buffer), .bluetooth_baundrate_one_packet_delay_ms = kbluetooth_baundrate_one_packet_delay_ms, .port_delay_ms = port_timer_delay_ms,
.bluetooth_rx_buffer_start_receving = false, .bluetooth_rx_buffer_processing = false,
.order = "order", .index = 0, .speedLevel = 0, .position = 0.0, .direction = 0, .code = 0, .info = "noerror", .deviceState = "init", .deviceException = 0, .deviceExceptionInfo = "noexception",
.table_conn_id_m = 0, .table_gatts_if_m = 0, .table_handle_m = 0,
.cmd_flag = false, .actively_report_flag = false, };
void app_main(void) { constructor_bluetooth_processer(&s_bluetooth_processer);
bluetooth_rx_buffer_format_receipt(); ble_spp_server_demo_app_main(&s_bluetooth_processer); timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms); while (true) { if (s_bluetooth_processer.table_handle_m != 0) { bluetooth_rx_buffer_send_indicate(); } bluetooth_gatts_try_process_data(); if (s_bluetooth_processer.actively_report_flag) { receipt_json_get_status(); s_bluetooth_processer.actively_report_flag = false; } }
return; }
bool validation_param(cJSON *object, char *param) { cJSON *current_element = object->child;
while (current_element->string != NULL) { if (current_element->string == param) { return true; } current_element = current_element->next; } return false; }
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(MAIN_TAG, "%s", szJson);
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(MAIN_TAG, "%s", szJson); free(szJson); }
cJSON_Delete(pRoot); }
void bluetooth_rx_buffer_format_receipt() { sprintf(bluetooth_tx_buffer, "{ \"order\": \"receipt\", \"index\": %d, \"speedLevel\": %d, \"position\": %.2lf, \"direction\": %d }", //
s_bluetooth_processer.index, s_bluetooth_processer.speedLevel, s_bluetooth_processer.position, s_bluetooth_processer.direction); }
void bluetooth_rx_buffer_send_indicate() { char temp_buffer[20] = {0}; uint8_t temp_lenght = 0; uint8_t temp_count_total = 0; uint8_t temp_last_count_not_15 = false; uint8_t temp_count_remainder = 0; uint8_t i = 0;
temp_lenght = strlen(bluetooth_tx_buffer); temp_count_total = temp_lenght / 15;
if ((temp_lenght % 15) != 0) { temp_count_remainder = temp_lenght % 15; temp_last_count_not_15 = true; }
for (i = 0; i < temp_count_total; i++) { string_copy_by_num(temp_buffer, bluetooth_tx_buffer, i, 15); esp_ble_gatts_send_indicate(s_bluetooth_processer.table_gatts_if_m, s_bluetooth_processer.table_conn_id_m, //
s_bluetooth_processer.table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false); } if (temp_last_count_not_15 == true) { string_copy_by_num(temp_buffer, bluetooth_tx_buffer, temp_count_total, temp_count_remainder); esp_ble_gatts_send_indicate(s_bluetooth_processer.table_gatts_if_m, s_bluetooth_processer.table_conn_id_m, //
s_bluetooth_processer.table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false); } }
void string_copy_by_num(char *dest, const char *src, uint8_t count, uint8_t num) { int i = 0; memset(dest, '\0', 15); if (dest == NULL || src == NULL || num == 0) { /* code */ ESP_LOGW(MAIN_TAG, "string_copy_by_num function parameter is empty"); }
for (i = 0; i < num; i++) { dest[i] = src[(count * 15) + i]; } }
|