Skip to content

Commit a050c1b

Browse files
author
Marceau Fillon
committed
Implement support for database hash write/read
1 parent d42cba0 commit a050c1b

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

extmod/nimble/modbluetooth_nimble.c

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
#define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY NIMBLE"
5858
#endif
5959

60-
#define DEBUG_printf(...) // printf("nimble: " __VA_ARGS__)
60+
#define DEBUG_printf(...) printf("nimble: " __VA_ARGS__)
6161

6262
#define ERRNO_BLUETOOTH_NOT_ACTIVE MP_ENODEV
6363

@@ -1862,6 +1862,13 @@ STATIC int ble_store_ram_read(int obj_type, const union ble_store_key *key, unio
18621862
DEBUG_printf("ble_store_ram_read: CCCD not supported.\n");
18631863
return -1;
18641864
}
1865+
case BLE_STORE_OBJ_TYPE_HASH: {
1866+
assert(ble_addr_cmp(&key->hash.peer_addr, BLE_ADDR_ANY)); // Must have address.
1867+
assert(key->hash.idx == 0);
1868+
key_data = (const uint8_t *)&key->hash.peer_addr;
1869+
key_data_len = sizeof(ble_addr_t);
1870+
break;
1871+
}
18651872
default:
18661873
return BLE_HS_ENOTSUP;
18671874
}
@@ -1873,12 +1880,23 @@ STATIC int ble_store_ram_read(int obj_type, const union ble_store_key *key, unio
18731880
return BLE_HS_ENOENT;
18741881
}
18751882

1876-
if (value_data_len != sizeof(struct ble_store_value_sec)) {
1877-
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));
1883+
size_t expected_len;
1884+
uint8_t *dest;
1885+
if (obj_type == BLE_STORE_OBJ_TYPE_HASH){
1886+
expected_len = sizeof(struct ble_store_value_hash);
1887+
dest = (uint8_t *)&value->hash;
1888+
}
1889+
else {
1890+
expected_len = sizeof(struct ble_store_value_sec);
1891+
dest = (uint8_t *)&value->sec;
1892+
}
1893+
1894+
if (value_data_len != expected_len) {
1895+
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));
18781896
return BLE_HS_ENOENT;
18791897
}
18801898

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

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

@@ -1918,6 +1936,23 @@ STATIC int ble_store_ram_write(int obj_type, const union ble_store_value *val) {
19181936
// Just pretend we wrote it.
19191937
return 0;
19201938
}
1939+
case BLE_STORE_OBJ_TYPE_HASH: {
1940+
struct ble_store_key_hash key_hash;
1941+
const struct ble_store_value_hash *value_hash = &val->hash;
1942+
ble_store_key_from_value_hash(&key_hash, value_hash);
1943+
1944+
assert(ble_addr_cmp(&key_hash.peer_addr, BLE_ADDR_ANY));
1945+
1946+
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))) {
1947+
DEBUG_printf("Failed to write hash key: type=%d\n", obj_type);
1948+
return BLE_HS_ESTORE_CAP;
1949+
}
1950+
1951+
DEBUG_printf("ble_secret_store_write: wrote hash\n");
1952+
1953+
return 0;
1954+
1955+
}
19211956
default:
19221957
return BLE_HS_ENOTSUP;
19231958
}
@@ -1948,6 +1983,19 @@ STATIC int ble_store_ram_delete(int obj_type, const union ble_store_key *key) {
19481983
// Just pretend it wasn't there.
19491984
return BLE_HS_ENOENT;
19501985
}
1986+
case BLE_STORE_OBJ_TYPE_HASH: {
1987+
assert(ble_addr_cmp(&key->hash.peer_addr, BLE_ADDR_ANY)); // Must have address.
1988+
1989+
if (!mp_bluetooth_gap_on_set_secret(obj_type, (const uint8_t *)&key->hash.peer_addr, sizeof(ble_addr_t), NULL, 0)) {
1990+
DEBUG_printf("Failed to delete key: type=%d\n", obj_type);
1991+
return BLE_HS_ENOENT;
1992+
}
1993+
1994+
DEBUG_printf("ble_secret_store_delete: deleted secret\n");
1995+
1996+
return 0;
1997+
1998+
}
19511999
default:
19522000
return BLE_HS_ENOTSUP;
19532001
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy