@@ -1946,6 +1946,13 @@ static int ble_secret_store_read(int obj_type, const union ble_store_key *key, u
19461946 DEBUG_printf ("ble_secret_store_read: CCCD not supported.\n" );
19471947 return -1 ;
19481948 }
1949+ case BLE_STORE_OBJ_TYPE_HASH : {
1950+ assert (ble_addr_cmp (& key -> hash .peer_addr , BLE_ADDR_ANY )); // Must have address.
1951+ assert (key -> hash .idx == 0 );
1952+ key_data = (const uint8_t * )& key -> hash .peer_addr ;
1953+ key_data_len = sizeof (ble_addr_t );
1954+ break ;
1955+ }
19491956 default :
19501957 return BLE_HS_ENOTSUP ;
19511958 }
@@ -1957,12 +1964,29 @@ static int ble_secret_store_read(int obj_type, const union ble_store_key *key, u
19571964 return BLE_HS_ENOENT ;
19581965 }
19591966
1960- if (value_data_len != sizeof (struct ble_store_value_sec )) {
1967+ size_t expected_len ;
1968+ uint8_t * dest ;
1969+ switch (obj_type ) {
1970+ case BLE_STORE_OBJ_TYPE_HASH :
1971+ expected_len = sizeof (struct ble_store_value_hash );
1972+ dest = (uint8_t * )& value -> hash ;
1973+ break ;
1974+
1975+ case BLE_STORE_OBJ_TYPE_OUR_SEC :
1976+ case BLE_STORE_OBJ_TYPE_PEER_SEC :
1977+ expected_len = sizeof (struct ble_store_value_sec );
1978+ dest = (uint8_t * )& value -> sec ;
1979+ break ;
1980+
1981+ default :
1982+ return BLE_HS_ENOTSUP ;
1983+
1984+ }
1985+ if (value_data_len != expected_len ) {
19611986 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 ));
19621987 return BLE_HS_ENOENT ;
19631988 }
1964-
1965- memcpy ((uint8_t * )& value -> sec , value_data , sizeof (struct ble_store_value_sec ));
1989+ memcpy (dest , value_data , expected_len );
19661990
19671991 DEBUG_printf ("ble_secret_store_read: found secret\n" );
19681992
@@ -1997,6 +2021,23 @@ static int ble_secret_store_write(int obj_type, const union ble_store_value *val
19972021 // Just pretend we wrote it.
19982022 return 0 ;
19992023 }
2024+ case BLE_STORE_OBJ_TYPE_HASH : {
2025+ struct ble_store_key_hash key_hash ;
2026+ const struct ble_store_value_hash * value_hash = & val -> hash ;
2027+ ble_store_key_from_value_hash (& key_hash , value_hash );
2028+
2029+ assert (ble_addr_cmp (& key_hash .peer_addr , BLE_ADDR_ANY ));
2030+
2031+ 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 ))) {
2032+ DEBUG_printf ("Failed to write hash key: type=%d\n" , obj_type );
2033+ return BLE_HS_ESTORE_CAP ;
2034+ }
2035+
2036+ DEBUG_printf ("ble_secret_store_write: wrote hash\n" );
2037+
2038+ return 0 ;
2039+
2040+ }
20002041 default :
20012042 return BLE_HS_ENOTSUP ;
20022043 }
@@ -2025,6 +2066,19 @@ static int ble_secret_store_delete(int obj_type, const union ble_store_key *key)
20252066 // Just pretend it wasn't there.
20262067 return BLE_HS_ENOENT ;
20272068 }
2069+ case BLE_STORE_OBJ_TYPE_HASH : {
2070+ assert (ble_addr_cmp (& key -> hash .peer_addr , BLE_ADDR_ANY )); // Must have address.
2071+
2072+ if (!mp_bluetooth_gap_on_set_secret (obj_type , (const uint8_t * )& key -> hash .peer_addr , sizeof (ble_addr_t ), NULL , 0 )) {
2073+ DEBUG_printf ("Failed to delete key: type=%d\n" , obj_type );
2074+ return BLE_HS_ENOENT ;
2075+ }
2076+
2077+ DEBUG_printf ("ble_secret_store_delete: deleted secret\n" );
2078+
2079+ return 0 ;
2080+
2081+ }
20282082 default :
20292083 return BLE_HS_ENOTSUP ;
20302084 }
0 commit comments