|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <errno.h> |
| 8 | +#include <zephyr/bluetooth/conn.h> |
| 9 | + |
| 10 | +#include <bluetooth/services/fast_pair/fast_pair.h> |
| 11 | + |
| 12 | +#include <zephyr/logging/log.h> |
| 13 | +LOG_MODULE_DECLARE(fast_pair, CONFIG_BT_FAST_PAIR_LOG_LEVEL); |
| 14 | + |
| 15 | +#include "fp_activation.h" |
| 16 | +#include "fp_storage_ak_bond.h" |
| 17 | + |
| 18 | +struct bond_find_ctx { |
| 19 | + bool bond_found; |
| 20 | + const bt_addr_le_t *bond_addr; |
| 21 | +}; |
| 22 | + |
| 23 | +static bool is_enabled; |
| 24 | + |
| 25 | +static void identity_resolved(struct bt_conn *conn, const bt_addr_le_t *rpa, |
| 26 | + const bt_addr_le_t *identity) |
| 27 | +{ |
| 28 | + if (!bt_fast_pair_is_ready()) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + LOG_DBG("Identity resolved"); |
| 33 | + |
| 34 | + fp_storage_ak_bond_conn_addr_update(conn, identity); |
| 35 | +} |
| 36 | + |
| 37 | +static void disconnected(struct bt_conn *conn, uint8_t reason) |
| 38 | +{ |
| 39 | + if (!bt_fast_pair_is_ready()) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + fp_storage_ak_bond_conn_finalize(conn); |
| 44 | +} |
| 45 | + |
| 46 | +BT_CONN_CB_DEFINE(conn_callbacks) = { |
| 47 | + .disconnected = disconnected, |
| 48 | + .identity_resolved = identity_resolved, |
| 49 | +}; |
| 50 | + |
| 51 | +static void bond_deleted(uint8_t id, const bt_addr_le_t *peer) |
| 52 | +{ |
| 53 | + if (!bt_fast_pair_is_ready()) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + fp_storage_ak_bond_delete(peer); |
| 58 | +} |
| 59 | + |
| 60 | +static struct bt_conn_auth_info_cb conn_auth_info_callbacks = { |
| 61 | + .bond_deleted = bond_deleted, |
| 62 | +}; |
| 63 | + |
| 64 | +static void bond_address_cmp(const struct bt_bond_info *info, void *user_data) |
| 65 | +{ |
| 66 | + struct bond_find_ctx *ctx = user_data; |
| 67 | + |
| 68 | + if (bt_addr_le_eq(ctx->bond_addr, &info->addr)) { |
| 69 | + __ASSERT_NO_MSG(!ctx->bond_found); |
| 70 | + ctx->bond_found = true; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +static int bond_identity_find(uint8_t *identity, const bt_addr_le_t *addr) |
| 75 | +{ |
| 76 | + struct bond_find_ctx ctx = { |
| 77 | + .bond_found = false, |
| 78 | + .bond_addr = addr, |
| 79 | + }; |
| 80 | + |
| 81 | + for (uint8_t id = 0; id < CONFIG_BT_ID_MAX; id++) { |
| 82 | + bt_foreach_bond(id, bond_address_cmp, &ctx); |
| 83 | + if (ctx.bond_found) { |
| 84 | + if (identity) { |
| 85 | + *identity = id; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return ctx.bond_found ? 0 : -ESRCH; |
| 91 | +} |
| 92 | + |
| 93 | +static int bond_remove(const bt_addr_le_t *addr) |
| 94 | +{ |
| 95 | + uint8_t identity; |
| 96 | + int err; |
| 97 | + |
| 98 | + err = bond_identity_find(&identity, addr); |
| 99 | + if (err) { |
| 100 | + return err; |
| 101 | + } |
| 102 | + |
| 103 | + return bt_unpair(identity, addr); |
| 104 | +} |
| 105 | + |
| 106 | +static bool is_addr_bonded(const bt_addr_le_t *addr) |
| 107 | +{ |
| 108 | + return !bond_identity_find(NULL, addr); |
| 109 | +} |
| 110 | + |
| 111 | +static const struct fp_storage_ak_bond_bt_request_cb bt_request_cb = { |
| 112 | + .bond_remove = bond_remove, |
| 113 | + .is_addr_bonded = is_addr_bonded, |
| 114 | +}; |
| 115 | + |
| 116 | +static int bt_request_cb_register(void) |
| 117 | +{ |
| 118 | + fp_storage_ak_bond_bt_request_cb_register(&bt_request_cb); |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | +static int bond_manager_init(void) |
| 123 | +{ |
| 124 | + if (is_enabled) { |
| 125 | + LOG_WRN("fp_bond_manager module already initialized"); |
| 126 | + return 0; |
| 127 | + } |
| 128 | + |
| 129 | + int err; |
| 130 | + |
| 131 | + err = bt_conn_auth_info_cb_register(&conn_auth_info_callbacks); |
| 132 | + if (err) { |
| 133 | + return err; |
| 134 | + } |
| 135 | + |
| 136 | + is_enabled = true; |
| 137 | + |
| 138 | + return 0; |
| 139 | +} |
| 140 | + |
| 141 | +static int bond_manager_uninit(void) |
| 142 | +{ |
| 143 | + if (!is_enabled) { |
| 144 | + LOG_WRN("fp_bond_manager module already uninitialized"); |
| 145 | + return 0; |
| 146 | + } |
| 147 | + |
| 148 | + int err; |
| 149 | + |
| 150 | + is_enabled = false; |
| 151 | + |
| 152 | + err = bt_conn_auth_info_cb_unregister(&conn_auth_info_callbacks); |
| 153 | + if (err) { |
| 154 | + return err; |
| 155 | + } |
| 156 | + |
| 157 | + return 0; |
| 158 | +} |
| 159 | + |
| 160 | +/* Register fp_storage_ak_bond_bt_request_cb callback structure to be able to remove Bluetooth |
| 161 | + * bonds during Fast Pair storage factory reset even if the Fast Pair is not enabled. |
| 162 | + */ |
| 163 | +SYS_INIT(bt_request_cb_register, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); |
| 164 | + |
| 165 | +FP_ACTIVATION_MODULE_REGISTER(fp_bond_manager, FP_ACTIVATION_INIT_PRIORITY_DEFAULT, |
| 166 | + bond_manager_init, bond_manager_uninit); |
0 commit comments