peripheral_gatt_write.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2022 Nordic Semiconductor ASA
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "services/bas.h"
  7. #include <errno.h>
  8. #include <stddef.h>
  9. #include <string.h>
  10. #include "base/byteorder.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. extern int mtu_exchange(struct bt_conn *conn);
  19. extern int write_cmd(struct bt_conn *conn);
  20. extern struct bt_conn *conn_connected;
  21. extern uint32_t last_write_rate;
  22. extern struct bt_conn_cb conn_callbacks;
  23. static const struct bt_data ad[] = {
  24. BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
  25. };
  26. __unused
  27. static void mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx)
  28. {
  29. printk("Updated MTU: TX: %d RX: %d bytes\n", tx, rx);
  30. }
  31. #if defined(CONFIG_BT_SMP)
  32. static void auth_cancel(struct bt_conn *conn)
  33. {
  34. char addr[BT_ADDR_LE_STR_LEN];
  35. bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
  36. printk("Pairing cancelled: %s\n", addr);
  37. }
  38. static struct bt_conn_auth_cb auth_callbacks = {
  39. .cancel = auth_cancel,
  40. };
  41. #endif /* CONFIG_BT_SMP */
  42. // static struct bt_gatt_cb gatt_callbacks = {
  43. // .att_mtu_updated = mtu_updated
  44. // };
  45. static uint32_t count;
  46. void bt_ready(int err)
  47. {
  48. if (err)
  49. {
  50. printk("Bluetooth init failed (err %d)\n", err);
  51. return;
  52. }
  53. printk("Bluetooth initialized\n");
  54. extern struct bt_gatt_service_static _1_gatt_svc;
  55. extern struct bt_gatt_service_static _2_gap_svc;
  56. bt_gatt_service_init(2, _1_gatt_svc, _2_gap_svc);
  57. // bt_gatt_cb_register(&gatt_callbacks);
  58. bt_conn_cb_register(&conn_callbacks);
  59. conn_connected = NULL;
  60. last_write_rate = 0U;
  61. count = 1;
  62. #if defined(CONFIG_BT_SMP)
  63. (void)bt_conn_auth_cb_register(&auth_callbacks);
  64. #endif /* CONFIG_BT_SMP */
  65. #if defined(CONFIG_BT_FIXED_PASSKEY)
  66. bt_passkey_set(1234);
  67. #endif
  68. err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
  69. if (err)
  70. {
  71. printk("Advertising failed to start (err %d)\n", err);
  72. return;
  73. }
  74. printk("Advertising successfully started\n");
  75. }
  76. void app_polling_work(void)
  77. {
  78. struct bt_conn *conn = conn_connected;
  79. if (conn)
  80. {
  81. if (count)
  82. {
  83. count--;
  84. (void)write_cmd(conn);
  85. }
  86. }
  87. }