circuitpython/ports/nrf/common-hal/_bleio/bonding.c

311 lines
12 KiB
C
Raw Normal View History

2020-01-03 10:24:07 -05:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Dan Halbert for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "ble.h"
#include "ble_drv.h"
#include "shared-bindings/_bleio/__init__.h"
#include "shared-bindings/_bleio/Adapter.h"
#include "shared-bindings/nvm/ByteArray.h"
#include "supervisor/shared/tick.h"
2020-01-03 10:24:07 -05:00
#include "nrf_soc.h"
2020-01-05 23:33:42 -05:00
2020-01-03 10:24:07 -05:00
#include "bonding.h"
// Internal flash area reserved for bonding storage.
#define BONDING_PAGES_START_ADDR CIRCUITPY_BLE_CONFIG_START_ADDR
#define BONDING_PAGES_END_ADDR (CIRCUITPY_BLE_CONFIG_START_ADDR + CIRCUITPY_BLE_CONFIG_SIZE)
2020-01-10 23:55:45 -05:00
// First and last four bytes are magic bytes for id and version. Data is in between.
2020-01-03 10:24:07 -05:00
// 'BD01'
2020-01-10 23:55:45 -05:00
const uint32_t BONDING_FLAG = ('1' | '0' << 8 | 'D' << 16 | 'B' << 24);
2020-01-03 10:24:07 -05:00
2020-01-10 23:55:45 -05:00
#define BONDING_DATA_START_ADDR (BONDING_PAGES_START_ADDR + sizeof(BONDING_FLAG))
#define BONDING_DATA_END_ADDR (BONDING_PAGES_END_ADDR - sizeof(BONDING_FLAG))
2020-01-03 10:24:07 -05:00
2020-01-05 23:33:42 -05:00
#define BONDING_START_FLAG_ADDR BONDING_PAGES_START_ADDR
2020-01-03 10:24:07 -05:00
#define BONDING_END_FLAG_ADDR BONDING_DATA_END_ADDR
2020-01-05 23:33:42 -05:00
// Save both system and user service info.
#define SYS_ATTR_FLAGS (BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS | BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS)
2020-01-03 10:24:07 -05:00
2020-01-10 23:55:45 -05:00
#if BONDING_DEBUG
void bonding_print_block(bonding_block_t *block) {
printf("at 0x%08lx: is_central: %1d, type: 0x%x, ediv: 0x%04x, data_length: %d\n",
2021-03-15 09:57:36 -04:00
(uint32_t)block, block->is_central, block->type, block->ediv, block->data_length);
2020-01-10 23:55:45 -05:00
}
void bonding_print_keys(bonding_keys_t *keys) {
2021-03-15 09:57:36 -04:00
for (size_t i = 0; i < sizeof(bonding_keys_t); i++) {
printf("%x", ((uint8_t *)keys)[i]);
2020-01-10 23:55:45 -05:00
}
printf("\n");
}
#endif
STATIC size_t compute_block_size(uint16_t data_length) {
2020-01-05 23:33:42 -05:00
// Round data size up to the nearest 32-bit address.
2020-01-10 23:55:45 -05:00
return sizeof(bonding_block_t) + ((data_length + 3) & ~0x3);
2020-01-03 10:24:07 -05:00
}
2020-01-10 23:55:45 -05:00
void bonding_erase_storage(void) {
2020-01-03 10:24:07 -05:00
// Erase all pages in the bonding area.
2021-03-15 09:57:36 -04:00
for (uint32_t page_address = BONDING_PAGES_START_ADDR;
page_address < BONDING_PAGES_END_ADDR;
page_address += FLASH_PAGE_SIZE) {
// Argument is page number, not address.
sd_flash_page_erase_sync(page_address / FLASH_PAGE_SIZE);
2020-01-03 10:24:07 -05:00
}
// Write marker words at the beginning and the end of the bonding area.
2020-01-10 23:55:45 -05:00
uint32_t flag = BONDING_FLAG;
2021-03-15 09:57:36 -04:00
sd_flash_write_sync((uint32_t *)BONDING_START_FLAG_ADDR, &flag, 1);
sd_flash_write_sync((uint32_t *)BONDING_END_FLAG_ADDR, &flag, 1);
2020-01-03 10:24:07 -05:00
}
// Given NULL to start or block address, return the address of the next valid block.
// The last block returned is the unused block at the end.
// Return NULL if we have run off the end of the bonding space.
2020-01-14 17:23:16 -05:00
2020-01-05 23:33:42 -05:00
STATIC bonding_block_t *next_block(bonding_block_t *block) {
2020-01-03 10:24:07 -05:00
while (1) {
// Advance to next block.
if (block == NULL) {
2021-03-15 09:57:36 -04:00
return (bonding_block_t *)BONDING_DATA_START_ADDR;
2020-01-03 10:24:07 -05:00
} else if (block->type == BLOCK_UNUSED) {
// Already at last block (the unused block).
return NULL;
}
// Advance to next block.
2021-03-15 09:57:36 -04:00
block = (bonding_block_t *)((uint8_t *)block + compute_block_size(block->data_length));
2020-01-03 10:24:07 -05:00
2021-03-15 09:57:36 -04:00
if (block >= (bonding_block_t *)BONDING_DATA_END_ADDR) {
2020-01-03 10:24:07 -05:00
// Went past end of bonding space.
return NULL;
}
2020-01-05 23:33:42 -05:00
if (block->type != BLOCK_INVALID) {
2020-01-03 10:24:07 -05:00
// Found an empty or a valid block.
return block;
}
// Invalid block (was erased); try again.
}
}
// Find the block with given is_central, type and ediv value.
2020-01-03 10:24:07 -05:00
// If type == BLOCK_UNUSED, ediv is ignored and the the sole unused block at the end is returned.
// If not found, return NULL.
STATIC bonding_block_t *find_existing_block(bool is_central, bonding_block_type_t type, uint16_t ediv) {
2020-01-03 10:24:07 -05:00
bonding_block_t *block = NULL;
while (1) {
2020-01-05 23:33:42 -05:00
block = next_block(block);
2020-01-03 10:24:07 -05:00
if (block == NULL) {
return NULL;
}
2020-01-10 23:55:45 -05:00
// If types match, and block is unused, just return it.
// Otherwise check that is_central and ediv match.
if (type == block->type) {
if (type == BLOCK_UNUSED ||
(is_central == block->is_central && ediv == block->ediv)) {
2021-03-15 09:57:36 -04:00
return block;
2020-01-10 23:55:45 -05:00
}
2020-01-03 10:24:07 -05:00
}
}
}
// Get an empty block large enough to store data_length data.
2021-03-15 09:57:36 -04:00
STATIC bonding_block_t *find_unused_block(uint16_t data_length) {
bonding_block_t *unused_block = find_existing_block(true, BLOCK_UNUSED, EDIV_INVALID);
// If no more room, erase all existing blocks and start over.
if (!unused_block ||
2021-03-15 09:57:36 -04:00
(uint8_t *)unused_block + compute_block_size(data_length) >= (uint8_t *)BONDING_DATA_END_ADDR) {
bonding_erase_storage();
2021-03-15 09:57:36 -04:00
unused_block = (bonding_block_t *)BONDING_DATA_START_ADDR;
}
return unused_block;
}
2020-01-03 10:24:07 -05:00
// Set the header word to all 0's, to mark the block as invalid.
// We don't change data_length, so we can still skip over this block.
2020-01-05 23:33:42 -05:00
STATIC void invalidate_block(bonding_block_t *block) {
uint32_t zero = 0;
2021-03-15 09:57:36 -04:00
sd_flash_write_sync((uint32_t *)block, &zero, 1);
2020-01-03 10:24:07 -05:00
}
2020-01-05 23:33:42 -05:00
// Write bonding block header.
STATIC void write_block_header(bonding_block_t *dest_block, bonding_block_t *source_block_header) {
2021-03-15 09:57:36 -04:00
sd_flash_write_sync((uint32_t *)dest_block, (uint32_t *)source_block_header, sizeof(bonding_block_t) / 4);
2020-01-05 23:33:42 -05:00
}
2020-01-03 10:24:07 -05:00
2020-01-05 23:33:42 -05:00
// Write variable-length data at end of bonding block.
STATIC void write_block_data(bonding_block_t *dest_block, uint8_t *data, uint16_t data_length) {
2020-01-03 10:24:07 -05:00
// Minimize the number of writes. Datasheet says no more than two writes per word before erasing again.
2020-01-05 23:33:42 -05:00
// Start writing after the current header.
2021-03-15 09:57:36 -04:00
uint32_t *flash_word_p = (uint32_t *)((uint8_t *)dest_block + sizeof(bonding_block_t));
2020-01-03 10:24:07 -05:00
while (1) {
uint32_t word = 0xffffffff;
memcpy(&word, data, data_length >= 4 ? 4 : data_length);
sd_flash_write_sync(flash_word_p, &word, 1);
2020-01-03 10:24:07 -05:00
if (data_length <= 4) {
break;
}
data_length -= 4;
2020-01-10 23:55:45 -05:00
data += 4;
2020-01-05 23:33:42 -05:00
// Increment by word size.
flash_word_p++;
}
}
STATIC void write_sys_attr_block(bleio_connection_internal_t *connection) {
2020-01-05 23:33:42 -05:00
uint16_t length = 0;
// First find out how big a buffer we need, then fetch the data.
2021-03-15 09:57:36 -04:00
if (sd_ble_gatts_sys_attr_get(connection->conn_handle, NULL, &length, SYS_ATTR_FLAGS) != NRF_SUCCESS) {
return;
2020-01-03 10:24:07 -05:00
}
2020-01-05 23:33:42 -05:00
uint8_t sys_attr[length];
2021-03-15 09:57:36 -04:00
if (sd_ble_gatts_sys_attr_get(connection->conn_handle, sys_attr, &length, SYS_ATTR_FLAGS) != NRF_SUCCESS) {
return;
2020-01-05 23:33:42 -05:00
}
// Is there an existing sys_attr block that matches the current sys_attr data?
bonding_block_t *existing_block =
find_existing_block(connection->is_central, BLOCK_SYS_ATTR, connection->ediv);
if (existing_block) {
if (length == existing_block->data_length &&
memcmp(sys_attr, existing_block->data, length) == 0) {
// Identical block found. No need to store again.
return;
}
// Data doesn't match. Invalidate block and store a new one.
invalidate_block(existing_block);
}
bonding_block_t block_header = {
.is_central = connection->is_central,
.type = BLOCK_SYS_ATTR,
.ediv = connection->ediv,
.conn_handle = connection->conn_handle,
.data_length = length,
};
bonding_block_t *new_block = find_unused_block(length);
write_block_header(new_block, &block_header);
write_block_data(new_block, sys_attr, length);
return;
2020-01-03 10:24:07 -05:00
}
STATIC void write_keys_block(bleio_connection_internal_t *connection) {
uint16_t const ediv = connection->is_central
? connection->bonding_keys.peer_enc.master_id.ediv
: connection->bonding_keys.own_enc.master_id.ediv;
// Is there an existing keys block that matches?
bonding_block_t *existing_block = find_existing_block(connection->is_central, BLOCK_KEYS, ediv);
if (existing_block) {
if (existing_block->data_length == sizeof(bonding_keys_t) &&
memcmp(existing_block->data, &connection->bonding_keys, sizeof(bonding_keys_t)) == 0) {
// Identical block found. No need to store again.
return;
}
// Data doesn't match. Invalidate block and store a new one.
invalidate_block(existing_block);
2020-01-05 23:33:42 -05:00
}
2020-01-03 10:24:07 -05:00
bonding_block_t block_header = {
.is_central = connection->is_central,
.type = BLOCK_KEYS,
.ediv = ediv,
.conn_handle = connection->conn_handle,
.data_length = sizeof(bonding_keys_t),
};
bonding_block_t *new_block = find_unused_block(sizeof(bonding_keys_t));
write_block_header(new_block, &block_header);
2021-03-15 09:57:36 -04:00
write_block_data(new_block, (uint8_t *)&connection->bonding_keys, sizeof(bonding_keys_t));
2020-01-05 23:33:42 -05:00
}
void bonding_clear_keys(bonding_keys_t *bonding_keys) {
2021-03-15 09:57:36 -04:00
memset((uint8_t *)bonding_keys, 0, sizeof(bonding_keys_t));
2020-01-05 23:33:42 -05:00
}
void bonding_reset(void) {
2021-03-15 09:57:36 -04:00
if (BONDING_FLAG != *((uint32_t *)BONDING_START_FLAG_ADDR) ||
BONDING_FLAG != *((uint32_t *)BONDING_END_FLAG_ADDR)) {
2020-01-10 23:55:45 -05:00
bonding_erase_storage();
2020-01-05 23:33:42 -05:00
}
}
// Write bonding blocks to flash. Requests have been queued during evt handlers.
2020-01-05 23:33:42 -05:00
void bonding_background(void) {
// A paired connection will request that its keys and CCCD values be stored.
// The CCCD store whenever a CCCD value is written.
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
2020-01-13 18:15:32 -05:00
bleio_connection_internal_t *connection = &bleio_connections[i];
// Wait at least one second before saving CCCD, to consolidate
// writes that involve multiple CCCDs. For instance, for HID,
// three CCCD's are set in short succession by the HID client.
2020-01-30 09:52:06 -05:00
if (connection->do_bond_cccds) {
uint64_t current_ticks_ms = supervisor_ticks_ms64();
if (current_ticks_ms - connection->do_bond_cccds_request_time >= 1000) {
write_sys_attr_block(connection);
connection->do_bond_cccds = false;
}
}
2020-01-05 23:33:42 -05:00
if (connection->do_bond_keys) {
write_keys_block(connection);
connection->do_bond_keys = false;
}
2020-01-05 23:33:42 -05:00
}
}
bool bonding_load_cccd_info(bool is_central, uint16_t conn_handle, uint16_t ediv) {
bonding_block_t *block = find_existing_block(is_central, BLOCK_SYS_ATTR, ediv);
2020-01-05 23:33:42 -05:00
if (block == NULL) {
2020-01-03 10:24:07 -05:00
return false;
}
2020-01-05 23:33:42 -05:00
return NRF_SUCCESS ==
2021-03-15 09:57:36 -04:00
sd_ble_gatts_sys_attr_set(conn_handle, block->data, block->data_length, SYS_ATTR_FLAGS);
2020-01-03 10:24:07 -05:00
}
bool bonding_load_keys(bool is_central, uint16_t ediv, bonding_keys_t *bonding_keys) {
bonding_block_t *block = find_existing_block(is_central, BLOCK_KEYS, ediv);
2020-01-05 23:33:42 -05:00
if (block == NULL) {
return false;
}
if (sizeof(bonding_keys_t) != block->data_length) {
// bonding_keys_t is a fixed length, so lengths should match.
2020-01-03 10:24:07 -05:00
return false;
}
2020-01-05 23:33:42 -05:00
memcpy(bonding_keys, block->data, block->data_length);
return true;
2020-01-03 10:24:07 -05:00
}