You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.8 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "freertos/event_groups.h"
  7. #include "esp_system.h"
  8. #include "esp_log.h"
  9. #include "nvs_flash.h"
  10. #include "esp_bt.h"
  11. #include "driver/timer.h"
  12. #include "cJSON.h"
  13. #include "cJSON_Utils.h"
  14. #include "esp_gap_ble_api.h"
  15. #include "esp_gatts_api.h"
  16. #include "esp_bt_defs.h"
  17. #include "esp_bt_main.h"
  18. #include "esp_gatt_common_api.h"
  19. #include "sdkconfig.h"
  20. #define GATTS_TAG "GATTS_DEMO"
  21. #define GATTS_SERVICE_UUID_TEST_A 0x00FF
  22. #define GATTS_CHAR_UUID_TEST_A 0xFF01
  23. #define GATTS_DESCR_UUID_TEST_A 0x3333
  24. #define GATTS_NUM_HANDLE_TEST_A 4
  25. #define GATTS_SERVICE_UUID_TEST_B 0x00EE
  26. #define GATTS_CHAR_UUID_TEST_B 0xEE01
  27. #define GATTS_DESCR_UUID_TEST_B 0x2222
  28. #define GATTS_NUM_HANDLE_TEST_B 4
  29. #define TEST_DEVICE_NAME "ESP_GATTS_DEMO"
  30. #define TEST_MANUFACTURER_DATA_LEN 17
  31. #define GATTS_DEMO_CHAR_VAL_LEN_MAX 0x40
  32. #define PREPARE_BUF_MAX_SIZE 1024
  33. #define adv_config_flag (1 << 0)
  34. #define scan_rsp_config_flag (1 << 1)
  35. #define PROFILE_NUM 2
  36. #define PROFILE_A_APP_ID 0
  37. #define PROFILE_B_APP_ID 1
  38. /***********************************************************************************************************************
  39. * ****************************************************user_define**************************************************** *
  40. ***********************************************************************************************************************/
  41. #define profile_b_buffer_size 128
  42. #define timer_group0_interval_num 10000
  43. // #define timer_group1_interval_num 5
  44. #define timer_interval_s (uint32_t)1000000
  45. #define timer_interval_ms (uint32_t)1000
  46. #define timer_interval_us (uint32_t)1
  47. #define kbluetooth_baundrate_one_packet_delay_ms 200
  48. #define cmd_length_set_position 5
  49. #define cmd_length_get_status 2
  50. // #define cmd_length_device_status_report 6
  51. #define set_position "setPosition"
  52. #define get_status "getStatus"
  53. struct gatts_profile_inst
  54. {
  55. esp_gatts_cb_t gatts_cb;
  56. uint16_t gatts_if;
  57. uint16_t app_id;
  58. uint16_t conn_id;
  59. uint16_t service_handle;
  60. esp_gatt_srvc_id_t service_id;
  61. uint16_t char_handle;
  62. esp_bt_uuid_t char_uuid;
  63. esp_gatt_perm_t perm;
  64. esp_gatt_char_prop_t property;
  65. uint16_t descr_handle;
  66. esp_bt_uuid_t descr_uuid;
  67. };
  68. typedef struct
  69. {
  70. uint8_t *prepare_buf;
  71. int prepare_len;
  72. } prepare_type_env_t;
  73. typedef struct ble_gatts_str
  74. {
  75. uint8_t send_gatts_if;
  76. uint16_t send_conn_id;
  77. uint16_t attr_handle;
  78. } ble_gatts_str_t;
  79. typedef struct bluetooth_processer
  80. {
  81. char *bluetooth_processer_rx_buf;
  82. uint8_t bluetooth_processer_rx_buf_size; //
  83. int bluetooth_baundrate_one_packet_delay_ms;
  84. void (*port_delay_ms)(uint64_t us);
  85. bool bluetooth_rx_buffer_start_receving;
  86. bool bluetooth_rx_buffer_processing;
  87. char *order; //指令名称
  88. int index; //
  89. int speed_level; //
  90. double position; //角度
  91. int direction; //旋转方向
  92. int code; //错误码
  93. char *info; //错误码信息
  94. char *deviceState; //设备状态
  95. int deviceException; //设备异常编号
  96. char *deviceExceptionInfo; //设备异常信息
  97. bool cmd_flag;
  98. bool actively_report_flag;
  99. } bluetooth_processer_t;
  100. void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param);
  101. void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param);
  102. void esp_ble_gatts_init(ble_gatts_str_t *ble_gatts_a_str, bluetooth_processer_t *bluetooth_processer);
  103. void gatts_profile_a_constructor(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
  104. void start_receive_data_to_buffer(uint16_t length, uint8_t *value);
  105. void buffer_all_init();