mcu_service.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. *
  3. * SPDX-License-Identifier: Apache-2.0
  4. *
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2022-01-29 supperthomas first version
  8. *
  9. */
  10. #include "mcu_service.h"
  11. #include "rtthread.h"
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include "ble_qiot_service.h"
  15. #include "ble_qiot_import.h"
  16. #include "host/ble_hs.h"
  17. #include "host/ble_uuid.h"
  18. #include "ble_qiot_export.h"
  19. //IOT_BLE_UUID_BASE + IOT_BLE_UUID_SERVICE
  20. #define SERVICE_BASE \
  21. 0xe2, 0xa4, 0x1b, 0x54, 0x93, 0xe4, 0x6a, 0xb5, 0x20, 0x4e, 0xd0, 0x65, 0xE0, 0xFF, 0x00, 0x00
  22. //
  23. //IOT_BLE_UUID_BASE + IOT_BLE_UUID_DEVICE_INFO
  24. #define SDEVICE_INFO_BASE \
  25. 0xe2, 0xa4, 0x1b, 0x54, 0x93, 0xe4, 0x6a, 0xb5, 0x20, 0x4e, 0xd0, 0x65, 0xE1, 0xFF, 0x00, 0x00
  26. //IOT_BLE_UUID_BASE + IOT_BLE_UUID_DATA
  27. #define SDEVICE_DATA_BASE \
  28. 0xe2, 0xa4, 0x1b, 0x54, 0x93, 0xe4, 0x6a, 0xb5, 0x20, 0x4e, 0xd0, 0x65, 0xE2, 0xFF, 0x00, 0x00
  29. //IOT_BLE_UUID_BASE + IOT_BLE_UUID_EVENT
  30. #define SDEVICE_DATA_EVENT \
  31. 0xe2, 0xa4, 0x1b, 0x54, 0x93, 0xe4, 0x6a, 0xb5, 0x20, 0x4e, 0xd0, 0x65, 0xE3, 0xFF, 0x00, 0x00
  32. static int
  33. gatt_svr_chr_access_device_info(uint16_t conn_handle, uint16_t attr_handle,
  34. struct ble_gatt_access_ctxt *ctxt, void *arg)
  35. {
  36. /* Sensor location, set to "Chest" */
  37. return 0;
  38. }
  39. static int
  40. gatt_svr_chr_access_device_data(uint16_t conn_handle, uint16_t attr_handle,
  41. struct ble_gatt_access_ctxt *ctxt, void *arg)
  42. {
  43. /* Sensor location, set to "Chest" */
  44. return 0;
  45. }
  46. static int
  47. gatt_svr_chr_access_event_cb(uint16_t conn_handle, uint16_t attr_handle,
  48. struct ble_gatt_access_ctxt *ctxt, void *arg)
  49. {
  50. /* Sensor location, set to "Chest" */
  51. return 0;
  52. }
  53. static uint16_t device_info_handle;
  54. static uint16_t device_data_handle;
  55. static uint16_t device_event_handle;
  56. static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
  57. {
  58. /* Service: Heart-rate */
  59. .type = BLE_GATT_SVC_TYPE_PRIMARY,
  60. .uuid = BLE_UUID128_DECLARE(SERVICE_BASE),
  61. .characteristics = (struct ble_gatt_chr_def[]) { {
  62. /* Characteristic: Heart-rate measurement */
  63. .uuid = BLE_UUID128_DECLARE(SDEVICE_INFO_BASE),
  64. .access_cb = gatt_svr_chr_access_device_info,
  65. .val_handle = &device_info_handle,
  66. .flags = BLE_GATT_CHR_F_WRITE,
  67. }, {
  68. /* Characteristic: Body sensor location */
  69. .uuid = BLE_UUID128_DECLARE(SDEVICE_DATA_BASE),
  70. .access_cb = gatt_svr_chr_access_device_data,
  71. .flags = BLE_GATT_CHR_F_WRITE,
  72. }, {
  73. /* Characteristic: Body sensor location */
  74. .uuid = BLE_UUID128_DECLARE(SDEVICE_DATA_EVENT),
  75. .access_cb = gatt_svr_chr_access_event_cb,
  76. .val_handle = &device_event_handle,
  77. .flags = BLE_GATT_CHR_F_NOTIFY,
  78. }, {
  79. 0, /* No more characteristics in this service */
  80. }, }
  81. },
  82. {
  83. 0, /* No more services */
  84. },
  85. };
  86. // add services and characteristic
  87. void ble_services_add(const qiot_service_init_s *p_service)
  88. {
  89. int rc;
  90. static int init_flag = 0;
  91. if (init_flag)
  92. return ;
  93. init_flag = 1;
  94. // /* Initialize the NimBLE host configuration */
  95. // ble_hs_cfg.sync_cb = blehr_on_sync;
  96. // ble_npl_callout_init(&blehr_tx_timer, nimble_port_get_dflt_eventq(),
  97. // blehr_tx_hrate, NULL);
  98. // rc = ble_gatts_count_cfg(gatt_svr_svcs);
  99. // if (rc != 0) {
  100. // return;
  101. // }
  102. // rc = ble_gatts_add_svcs(gatt_svr_svcs);
  103. // if (rc != 0) {
  104. // return;
  105. // }
  106. // /* Set the default device name */
  107. // rc = ble_svc_gap_device_name_set(device_name);
  108. // RT_ASSERT(rc == 0);
  109. // /* startup bluetooth host stack*/
  110. // ble_hs_thread_startup();
  111. }
  112. MSH_CMD_EXPORT_ALIAS(ble_services_add, ble_services_add, "bluetoooth SERVICE senson sample");