app_main.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "hts.h"
  8. #include "services/bas.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. #include "hts.h"
  25. static const struct bt_data ad[] = {
  26. BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
  27. BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_HTS_VAL),
  28. BT_UUID_16_ENCODE(BT_UUID_DIS_VAL), BT_UUID_16_ENCODE(BT_UUID_BAS_VAL)),
  29. };
  30. static void connected(struct bt_conn *conn, uint8_t err)
  31. {
  32. if (err)
  33. {
  34. printk("Connection failed (err 0x%02x)\n", err);
  35. }
  36. else
  37. {
  38. printk("Connected\n");
  39. }
  40. }
  41. static void disconnected(struct bt_conn *conn, uint8_t reason)
  42. {
  43. printk("Disconnected (reason 0x%02x)\n", reason);
  44. }
  45. static struct bt_conn_cb conn_callbacks = {
  46. .connected = connected,
  47. .disconnected = disconnected,
  48. };
  49. static void auth_cancel(struct bt_conn *conn)
  50. {
  51. char addr[BT_ADDR_LE_STR_LEN];
  52. bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
  53. printk("Pairing cancelled: %s\n", addr);
  54. }
  55. static struct bt_conn_auth_cb auth_cb_display = {
  56. .cancel = auth_cancel,
  57. };
  58. static void bas_notify(void)
  59. {
  60. uint8_t battery_level = bt_bas_get_battery_level();
  61. battery_level--;
  62. if (!battery_level)
  63. {
  64. battery_level = 100U;
  65. }
  66. bt_bas_set_battery_level(battery_level);
  67. }
  68. static void idle_timeout(struct k_timer *work)
  69. {
  70. /* Heartrate measurements simulation */
  71. hts_indicate();
  72. /* Battery level simulation */
  73. bas_notify();
  74. }
  75. void bt_ready(int err)
  76. {
  77. if (err)
  78. {
  79. printk("Bluetooth init failed (err %d)\n", err);
  80. return;
  81. }
  82. printk("Bluetooth initialized\n");
  83. hts_init();
  84. extern struct bt_gatt_service_static _1_gatt_svc;
  85. extern struct bt_gatt_service_static _2_gap_svc;
  86. bt_gatt_service_init(5, _1_gatt_svc, _2_gap_svc, bas_svc, dis_svc, hts_svc);
  87. bt_conn_cb_register(&conn_callbacks);
  88. bt_conn_auth_cb_register(&auth_cb_display);
  89. #if defined(CONFIG_BT_FIXED_PASSKEY)
  90. bt_passkey_set(1234);
  91. #endif
  92. k_timer_init(&idle_work, idle_timeout, NULL);
  93. k_timer_start(&idle_work, K_SECONDS(1), K_SECONDS(1));
  94. err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
  95. if (err)
  96. {
  97. printk("Advertising failed to start (err %d)\n", err);
  98. return;
  99. }
  100. printk("Advertising successfully started\n");
  101. }
  102. void app_polling_work(void)
  103. {
  104. }