hrs.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /** @file
  2. * @brief HRS Service sample
  3. */
  4. /*
  5. * Copyright (c) 2016 Intel Corporation
  6. *
  7. * SPDX-License-Identifier: Apache-2.0
  8. */
  9. #include <zephyr/types.h>
  10. #include <stddef.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <zephyr.h>
  14. #include <init.h>
  15. #include <acts_bluetooth/bluetooth.h>
  16. #include <acts_bluetooth/hci.h>
  17. #include <acts_bluetooth/conn.h>
  18. #include <acts_bluetooth/uuid.h>
  19. #include <acts_bluetooth/gatt.h>
  20. #define LOG_LEVEL CONFIG_BT_HRS_LOG_LEVEL
  21. #include <logging/log.h>
  22. LOG_MODULE_REGISTER(hrs);
  23. static uint8_t hrs_blsc;
  24. static void hrmc_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
  25. {
  26. ARG_UNUSED(attr);
  27. bool notif_enabled = (value == BT_GATT_CCC_NOTIFY);
  28. LOG_INF("HRS notifications %s", notif_enabled ? "enabled" : "disabled");
  29. }
  30. static ssize_t read_blsc(struct bt_conn *conn, const struct bt_gatt_attr *attr,
  31. void *buf, uint16_t len, uint16_t offset)
  32. {
  33. return bt_gatt_attr_read(conn, attr, buf, len, offset, &hrs_blsc,
  34. sizeof(hrs_blsc));
  35. }
  36. /* Heart Rate Service Declaration */
  37. BT_GATT_SERVICE_DEFINE(hrs_svc,
  38. BT_GATT_PRIMARY_SERVICE(BT_UUID_HRS),
  39. BT_GATT_CHARACTERISTIC(BT_UUID_HRS_MEASUREMENT, BT_GATT_CHRC_NOTIFY,
  40. BT_GATT_PERM_NONE, NULL, NULL, NULL),
  41. BT_GATT_CCC(hrmc_ccc_cfg_changed,
  42. BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
  43. BT_GATT_CHARACTERISTIC(BT_UUID_HRS_BODY_SENSOR, BT_GATT_CHRC_READ,
  44. BT_GATT_PERM_READ, read_blsc, NULL, NULL),
  45. BT_GATT_CHARACTERISTIC(BT_UUID_HRS_CONTROL_POINT, BT_GATT_CHRC_WRITE,
  46. BT_GATT_PERM_NONE, NULL, NULL, NULL),
  47. );
  48. int hrs_init(void)
  49. {
  50. hrs_blsc = 0x01;
  51. return 0;
  52. }
  53. int bt_hrs_notify(uint16_t heartrate)
  54. {
  55. int rc;
  56. static uint8_t hrm[2];
  57. hrm[0] = 0x06; /* uint8, sensor contact */
  58. hrm[1] = heartrate;
  59. rc = bt_gatt_notify(NULL, &hrs_svc.attrs[1], &hrm, sizeof(hrm));
  60. return rc == -ENOTCONN ? 0 : rc;
  61. }