bsal_srv_hrs.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-09-010 WaterFishJ the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <rthw.h>
  13. #include <stdio.h>
  14. #include <stdint.h>
  15. #include "bsal.h"
  16. #include "bsal_osif.h"
  17. #include "bsal_srv_hrs.h"
  18. static uint16_t hrs_hrm_handle;
  19. /* Sensor location, set to "Chest" */
  20. static uint8_t body_sens_loc = BODY_SENSOR_LOCATION_CHEST;
  21. static P_SRV_GENERAL_CB pfn_bas_cb = NULL;
  22. static void hrs_profile_callback(void *p)
  23. {
  24. bsal_callbak_data_t *p_param = (bsal_callbak_data_t *)p;
  25. bool is_app_cb = false;
  26. if (p_param->msg_type == BSAL_CALLBACK_TYPE_READ_CHAR_VALUE)
  27. {
  28. if (GATT_SVC_BODY_SENSOR_LOCATION_READ_INDEX == p_param->off_handle)
  29. {
  30. is_app_cb = true;
  31. bsal_srv_write_data(p_param->stack_ptr, p_param->start_handle, p_param->off_handle, sizeof(uint8_t), &body_sens_loc);
  32. }
  33. }
  34. else if (p_param->msg_type == BSAL_CALLBACK_TYPE_WRITE_CHAR_VALUE)
  35. {
  36. is_app_cb = true;
  37. }
  38. else if (p_param->msg_type == BSAL_CALLBACK_TYPE_INDIFICATION_NOTIFICATION)
  39. {
  40. if (GATT_SVC_HRS_MEASUREMENT_CHAR_CCCD_INDEX == p_param->off_handle)
  41. {
  42. if (p_param->length == 2)
  43. {
  44. is_app_cb = true;
  45. }
  46. }
  47. }
  48. if (is_app_cb && (pfn_bas_cb != NULL))
  49. {
  50. pfn_bas_cb(p_param);
  51. }
  52. }
  53. void bsal_le_hrs_svr_init(void *stack_ptr, void *app_callback)
  54. {
  55. struct bsal_gatt_app_srv_def ble_svc_hrs_defs[] =
  56. {
  57. {
  58. /*** Heart Rate Service. */
  59. .type = BSAL_GATT_UUID_PRIMARY_SERVICE,
  60. .uuid = BSAL_UUID16_DECLARE(GATT_UUID_HEART_RATE),
  61. .characteristics = (bsal_gatt_chr_def_t[])
  62. {
  63. {
  64. /*** Heart Rate Measurement characteristic */
  65. .uuid = BSAL_UUID16_DECLARE(GATT_UUID_HRS_MEASUREMENT),
  66. .properties = BSAL_ATT_P_NOTIFY,
  67. .val_handle = &hrs_hrm_handle,
  68. .permission = BSAL_GATT_PERM_READ_NONE,
  69. .value_length = 1,
  70. },
  71. {
  72. /*** Body Sensor Location characteristic */
  73. .uuid = BSAL_UUID16_DECLARE(GATT_UUID_CHAR_BODY_SENSOR_LOCATION),
  74. .properties = BSAL_ATT_P_READ,
  75. .val_handle = &hrs_hrm_handle,
  76. .permission = BSAL_GATT_PERM_READ_NONE,
  77. .value_length = 1,
  78. },
  79. {
  80. 0, /* No more characteristics in this service. */
  81. }
  82. },
  83. },
  84. {
  85. 0, /* No more services. */
  86. },
  87. };
  88. bsal_stack_le_srv_reg_func(stack_ptr, &ble_svc_hrs_defs, (P_SRV_GENERAL_CB *)hrs_profile_callback);
  89. pfn_bas_cb = (P_SRV_GENERAL_CB)app_callback;
  90. }
  91. void bsal_hrs_send_notify_level(void *stack_ptr, uint16_t conn_id, uint8_t *hr_measurement)
  92. {
  93. bsal_uuid_any_t uuid_srv;
  94. uuid_srv.u_type = 16;
  95. uuid_srv.u16.value = GATT_UUID_HEART_RATE;
  96. uint16_t start_handle = bsal_srv_get_start_handle(stack_ptr, uuid_srv);
  97. bsal_srv_send_notify_data(stack_ptr, conn_id, start_handle, GATT_SVC_HRS_MEASUREMENT_CHAR_INDEX, sizeof(hr_measurement), hr_measurement);
  98. }