app_main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* main.c - Application main entry point */
  2. /*
  3. * Copyright (c) 2015-2016 Intel Corporation
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #include "services/bas.h"
  8. #include "services/hrs.h"
  9. #include "services/dis.h"
  10. #include <errno.h>
  11. #include <stddef.h>
  12. #include <string.h>
  13. #include "base/byteorder.h"
  14. #include "base/types.h"
  15. #include <bluetooth/bluetooth.h>
  16. #include <bluetooth/conn.h>
  17. #include <bluetooth/gatt.h>
  18. #include <bluetooth/hci.h>
  19. #include <bluetooth/uuid.h>
  20. #include <logging/bt_log_impl.h>
  21. #include "common/timer.h"
  22. /* Idle timer */
  23. struct k_timer idle_work;
  24. static const struct bt_data ad[] = {
  25. BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
  26. BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_HRS_VAL),
  27. BT_UUID_16_ENCODE(BT_UUID_BAS_VAL), BT_UUID_16_ENCODE(BT_UUID_DIS_VAL))};
  28. static void connected(struct bt_conn *conn, uint8_t err)
  29. {
  30. if (err)
  31. {
  32. printk("Connection failed (err 0x%02x)\n", err);
  33. }
  34. else
  35. {
  36. printk("Connected\n");
  37. }
  38. }
  39. static void disconnected(struct bt_conn *conn, uint8_t reason)
  40. {
  41. printk("Disconnected (reason 0x%02x)\n", reason);
  42. }
  43. static struct bt_conn_cb conn_callbacks = {
  44. .connected = connected,
  45. .disconnected = disconnected,
  46. };
  47. static void auth_cancel(struct bt_conn *conn)
  48. {
  49. char addr[BT_ADDR_LE_STR_LEN];
  50. bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
  51. printk("Pairing cancelled: %s\n", addr);
  52. }
  53. static struct bt_conn_auth_cb auth_cb_display = {
  54. .cancel = auth_cancel,
  55. };
  56. static void bas_notify(void)
  57. {
  58. uint8_t battery_level = bt_bas_get_battery_level();
  59. battery_level--;
  60. if (!battery_level)
  61. {
  62. battery_level = 100U;
  63. }
  64. bt_bas_set_battery_level(battery_level);
  65. }
  66. static void hrs_notify(void)
  67. {
  68. static uint8_t heartrate = 90U;
  69. /* Heartrate measurements simulation */
  70. heartrate++;
  71. if (heartrate == 160U)
  72. {
  73. heartrate = 90U;
  74. }
  75. bt_hrs_notify(heartrate);
  76. }
  77. static void idle_timeout(struct k_timer *work)
  78. {
  79. /* Heartrate measurements simulation */
  80. hrs_notify();
  81. /* Battery level simulation */
  82. bas_notify();
  83. }
  84. void bt_ready(int err)
  85. {
  86. if (err)
  87. {
  88. printk("Bluetooth init failed (err %d)\n", err);
  89. return;
  90. }
  91. printk("Bluetooth initialized\n");
  92. extern struct bt_gatt_service_static _1_gatt_svc;
  93. extern struct bt_gatt_service_static _2_gap_svc;
  94. bt_gatt_service_init(5, _1_gatt_svc, _2_gap_svc, dis_svc, bas_svc, hrs_svc);
  95. bt_conn_cb_register(&conn_callbacks);
  96. bt_conn_auth_cb_register(&auth_cb_display);
  97. #if defined(CONFIG_BT_FIXED_PASSKEY)
  98. bt_passkey_set(1234);
  99. #endif
  100. k_timer_init(&idle_work, idle_timeout, NULL);
  101. k_timer_start(&idle_work, K_SECONDS(1), K_SECONDS(1));
  102. err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
  103. if (err)
  104. {
  105. printk("Advertising failed to start (err %d)\n", err);
  106. return;
  107. }
  108. printk("Advertising successfully started\n");
  109. }
  110. void app_polling_work(void)
  111. {
  112. }