app_main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "hog.h"
  8. #include <errno.h>
  9. #include <stddef.h>
  10. #include <string.h>
  11. #include "base/types.h"
  12. #include <bluetooth/bluetooth.h>
  13. #include <bluetooth/conn.h>
  14. #include <bluetooth/gatt.h>
  15. #include <bluetooth/hci.h>
  16. #include <bluetooth/uuid.h>
  17. #include <logging/bt_log_impl.h>
  18. #include "services/bas.h"
  19. #include "services/dis.h"
  20. static const struct bt_data ad[] = {
  21. BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
  22. BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_HIDS_VAL),
  23. BT_UUID_16_ENCODE(BT_UUID_BAS_VAL)),
  24. };
  25. static void connected(struct bt_conn *conn, uint8_t err)
  26. {
  27. char addr[BT_ADDR_LE_STR_LEN];
  28. bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
  29. if (err)
  30. {
  31. printk("Failed to connect to %s (%u)\n", addr, err);
  32. return;
  33. }
  34. printk("Connected %s\n", addr);
  35. if (bt_conn_set_security(conn, BT_SECURITY_L2))
  36. {
  37. printk("Failed to set security\n");
  38. }
  39. }
  40. static void disconnected(struct bt_conn *conn, uint8_t reason)
  41. {
  42. char addr[BT_ADDR_LE_STR_LEN];
  43. bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
  44. printk("Disconnected from %s (reason 0x%02x)\n", addr, reason);
  45. }
  46. static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err)
  47. {
  48. char addr[BT_ADDR_LE_STR_LEN];
  49. bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
  50. if (!err)
  51. {
  52. printk("Security changed: %s level %u\n", addr, level);
  53. }
  54. else
  55. {
  56. printk("Security failed: %s level %u err %d\n", addr, level, err);
  57. }
  58. }
  59. static struct bt_conn_cb conn_callbacks = {
  60. .connected = connected,
  61. .disconnected = disconnected,
  62. .security_changed = security_changed,
  63. };
  64. static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey)
  65. {
  66. char addr[BT_ADDR_LE_STR_LEN];
  67. bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
  68. printk("Passkey for %s: %06u\n", addr, passkey);
  69. }
  70. static void auth_cancel(struct bt_conn *conn)
  71. {
  72. char addr[BT_ADDR_LE_STR_LEN];
  73. bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
  74. printk("Pairing cancelled: %s\n", addr);
  75. }
  76. static struct bt_conn_auth_cb auth_cb_display = {
  77. .passkey_display = auth_passkey_display,
  78. .passkey_entry = NULL,
  79. .cancel = auth_cancel,
  80. };
  81. int is_bt_ready_work = 0;
  82. void bt_ready(int err)
  83. {
  84. if (err)
  85. {
  86. printk("Bluetooth init failed (err %d)\n", err);
  87. return;
  88. }
  89. is_bt_ready_work = 1;
  90. printk("Bluetooth initialized\n");
  91. extern struct bt_gatt_service_static _1_gatt_svc;
  92. extern struct bt_gatt_service_static _2_gap_svc;
  93. extern struct bt_gatt_service_static hog_svc;
  94. bt_gatt_service_init(5, _1_gatt_svc, _2_gap_svc, dis_svc, bas_svc, hog_svc);
  95. hog_init();
  96. bt_conn_cb_register(&conn_callbacks);
  97. bt_conn_auth_cb_register(&auth_cb_display);
  98. #if defined(CONFIG_BT_FIXED_PASSKEY)
  99. bt_passkey_set(1234);
  100. #endif
  101. err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
  102. if (err)
  103. {
  104. printk("Advertising failed to start (err %d)\n", err);
  105. return;
  106. }
  107. printk("Advertising successfully started\n");
  108. }
  109. int test_cnt = 0;
  110. void app_polling_work(void)
  111. {
  112. if (!is_bt_ready_work)
  113. {
  114. return;
  115. }
  116. if (test_cnt++ < 200000)
  117. {
  118. return;
  119. }
  120. test_cnt = 0;
  121. }