Skip to content

extmod/nimble: Support database hash storing #7877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions extmod/nimble/modbluetooth_nimble.c
Original file line number Diff line number Diff line change
Expand Up @@ -1862,6 +1862,13 @@ STATIC int ble_store_ram_read(int obj_type, const union ble_store_key *key, unio
DEBUG_printf("ble_store_ram_read: CCCD not supported.\n");
return -1;
}
case BLE_STORE_OBJ_TYPE_HASH: {
assert(ble_addr_cmp(&key->hash.peer_addr, BLE_ADDR_ANY)); // Must have address.
assert(key->hash.idx == 0);
key_data = (const uint8_t *)&key->hash.peer_addr;
key_data_len = sizeof(ble_addr_t);
break;
}
default:
return BLE_HS_ENOTSUP;
}
Expand All @@ -1873,12 +1880,23 @@ STATIC int ble_store_ram_read(int obj_type, const union ble_store_key *key, unio
return BLE_HS_ENOENT;
}

if (value_data_len != sizeof(struct ble_store_value_sec)) {
DEBUG_printf("ble_store_ram_read: Invalid key data: actual=" UINT_FMT " expected=" UINT_FMT "\n", value_data_len, sizeof(struct ble_store_value_sec));
size_t expected_len;
uint8_t *dest;
if (obj_type == BLE_STORE_OBJ_TYPE_HASH){
expected_len = sizeof(struct ble_store_value_hash);
dest = (uint8_t *)&value->hash;
}
else {
expected_len = sizeof(struct ble_store_value_sec);
dest = (uint8_t *)&value->sec;
}

if (value_data_len != expected_len) {
DEBUG_printf("ble_secret_store_read: Invalid key data: actual=" UINT_FMT " expected=" UINT_FMT "\n", value_data_len, sizeof(struct ble_store_value_sec));
return BLE_HS_ENOENT;
}

memcpy((uint8_t *)&value->sec, value_data, sizeof(struct ble_store_value_sec));
memcpy(dest, value_data, expected_len);

DEBUG_printf("ble_store_ram_read: found secret\n");

Expand Down Expand Up @@ -1918,6 +1936,23 @@ STATIC int ble_store_ram_write(int obj_type, const union ble_store_value *val) {
// Just pretend we wrote it.
return 0;
}
case BLE_STORE_OBJ_TYPE_HASH: {
struct ble_store_key_hash key_hash;
const struct ble_store_value_hash *value_hash = &val->hash;
ble_store_key_from_value_hash(&key_hash, value_hash);

assert(ble_addr_cmp(&key_hash.peer_addr, BLE_ADDR_ANY));

if (!mp_bluetooth_gap_on_set_secret(obj_type, (const uint8_t *)&key_hash.peer_addr, sizeof(ble_addr_t), (const uint8_t *)value_hash, sizeof(struct ble_store_value_hash))) {
DEBUG_printf("Failed to write hash key: type=%d\n", obj_type);
return BLE_HS_ESTORE_CAP;
}

DEBUG_printf("ble_secret_store_write: wrote hash\n");

return 0;

}
default:
return BLE_HS_ENOTSUP;
}
Expand Down Expand Up @@ -1948,6 +1983,19 @@ STATIC int ble_store_ram_delete(int obj_type, const union ble_store_key *key) {
// Just pretend it wasn't there.
return BLE_HS_ENOENT;
}
case BLE_STORE_OBJ_TYPE_HASH: {
assert(ble_addr_cmp(&key->hash.peer_addr, BLE_ADDR_ANY)); // Must have address.

if (!mp_bluetooth_gap_on_set_secret(obj_type, (const uint8_t *)&key->hash.peer_addr, sizeof(ble_addr_t), NULL, 0)) {
DEBUG_printf("Failed to delete key: type=%d\n", obj_type);
return BLE_HS_ENOENT;
}

DEBUG_printf("ble_secret_store_delete: deleted secret\n");

return 0;

}
default:
return BLE_HS_ENOTSUP;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/mynewt-nimble
Submodule mynewt-nimble updated 349 files