| | |
| | | #include "sysinit/sysinit.h" |
| | | #include "nimble_syscfg.h" |
| | | #include "host/ble_hs.h" |
| | | #include "store/config/ble_store_config.h" |
| | | #include "ble_store_config_priv.h" |
| | | #include "os/panchip_mempool.h" |
| | | #if MYNEWT_VAL_BLE_STORE_CONFIG_PERSIST |
| | | #include "kv_store_api.h" |
| | | #endif |
| | | |
| | | #if SMP_MULTI_ADDR_MODE_EN |
| | | #include "host/ble_hs_id.h" |
| | | #endif |
| | | |
| | | #define BLE_STORE_LOG_EN 0 |
| | | |
| | | #ifdef IP_101x |
| | | |
| | | struct ble_store_value_sec *ble_store_config_our_secs; |
| | |
| | | ble_store_config_cccds[MYNEWT_VAL(BLE_STORE_MAX_CCCDS)]; |
| | | int ble_store_config_num_cccds; |
| | | #endif |
| | | |
| | | #if SMP_MULTI_ADDR_MODE_EN |
| | | struct ble_store_value_sec *ble_store_config_get_our_secs(uint8_t id) |
| | | { |
| | | return &ble_store_config_our_secs[id]; |
| | | } |
| | | |
| | | struct ble_store_value_sec *ble_store_config_get_peer_secs(uint8_t id) |
| | | { |
| | | return &ble_store_config_peer_secs[id]; |
| | | } |
| | | |
| | | struct ble_store_value_cccd *ble_store_config_get_cccds(uint8_t id) |
| | | { |
| | | return &ble_store_config_cccds[id]; |
| | | } |
| | | |
| | | int ble_store_config_get_our_secs_num(void) |
| | | { |
| | | return ble_store_config_num_our_secs; |
| | | } |
| | | |
| | | int ble_store_config_get_peer_secs_num(void) |
| | | { |
| | | return ble_store_config_num_peer_secs; |
| | | } |
| | | |
| | | int ble_store_config_get_cccds_num(void) |
| | | { |
| | | return ble_store_config_num_cccds; |
| | | } |
| | | #endif |
| | | |
| | | /***************************************************************************** |
| | | * $sec * |
| | | *****************************************************************************/ |
| | | #if BLE_STORE_LOG_EN |
| | | void |
| | | ble_hs_log_flat_buf_priv(const void *data, int len) |
| | | { |
| | |
| | | printf( "%02x", u8ptr[i]); |
| | | } |
| | | } |
| | | #endif |
| | | |
| | | static void |
| | | ble_store_config_print_value_sec(const struct ble_store_value_sec *sec) |
| | | { |
| | | #if BLE_STORE_LOG_EN |
| | | if (sec->ltk_present) { |
| | | printf( "ediv=%u rand=%llu authenticated=%d ltk= ", |
| | | sec->ediv, sec->rand_num, sec->authenticated); |
| | |
| | | } |
| | | |
| | | printf( "\n"); |
| | | #endif |
| | | } |
| | | |
| | | static void |
| | |
| | | cur = value_secs + i; |
| | | |
| | | if (ble_addr_cmp(&key_sec->peer_addr, BLE_ADDR_ANY)) { |
| | | #if SMP_MULTI_ADDR_MODE_EN |
| | | ble_addr_t own_addr; |
| | | if(key_sec->is_own_addr_valid == 0xAA){ |
| | | own_addr = key_sec->own_addr; |
| | | } |
| | | else{ |
| | | own_addr.type = ble_hs_id_get_used_addr_type(); |
| | | int rc = ble_hs_id_copy_addr(own_addr.type, own_addr.val, NULL); |
| | | if(rc != 0){ |
| | | return -1; |
| | | } |
| | | } |
| | | |
| | | if (ble_addr_cmp(&cur->peer_addr, &key_sec->peer_addr) || |
| | | ble_addr_cmp(&cur->own_addr, &own_addr)) { |
| | | continue; |
| | | } |
| | | #else |
| | | if (ble_addr_cmp(&cur->peer_addr, &key_sec->peer_addr)) { |
| | | continue; |
| | | } |
| | | #endif |
| | | } |
| | | |
| | | if (key_sec->ediv_rand_present) { |
| | |
| | | cccd = ble_store_config_cccds + i; |
| | | |
| | | if (ble_addr_cmp(&key->peer_addr, BLE_ADDR_ANY)) { |
| | | #if SMP_MULTI_ADDR_MODE_EN |
| | | ble_addr_t own_addr; |
| | | if(key->is_own_addr_valid == 0xAA){ |
| | | own_addr = key->own_addr; |
| | | } |
| | | else{ |
| | | own_addr.type = ble_hs_id_get_used_addr_type(); |
| | | int rc = ble_hs_id_copy_addr(own_addr.type, own_addr.val, NULL); |
| | | if(rc != 0){ |
| | | return -1; |
| | | } |
| | | } |
| | | |
| | | if (ble_addr_cmp(&cccd->peer_addr, &key->peer_addr) || |
| | | ble_addr_cmp(&cccd->own_addr, &own_addr)) { |
| | | continue; |
| | | } |
| | | #else |
| | | if (ble_addr_cmp(&cccd->peer_addr, &key->peer_addr)) { |
| | | continue; |
| | | } |
| | | #endif |
| | | } |
| | | |
| | | if (key->chr_val_handle != 0) { |
| | |
| | | idx = ble_store_config_find_cccd(&key_cccd); |
| | | if (idx == -1) { |
| | | if (ble_store_config_num_cccds >= MYNEWT_VAL(BLE_STORE_MAX_CCCDS)) { |
| | | printf("error persisting cccd; too many entries (%d)\n", |
| | | ble_store_config_num_cccds); |
| | | #if BLE_STORE_LOG_EN |
| | | printf("error persisting cccd; too many entries (%d)\n", ble_store_config_num_cccds); |
| | | #endif |
| | | return BLE_HS_ESTORE_CAP; |
| | | } |
| | | |
| | |
| | | ble_store_config_init(void) |
| | | { |
| | | /* Ensure this function only gets called by sysinit. */ |
| | | #ifdef IP_101x |
| | | #ifdef IP_101x |
| | | ble_store_config_our_secs = panchip_mem_get((MYNEWT_VAL(BLE_STORE_MAX_BONDS)) * sizeof(struct ble_store_value_sec), 8); |
| | | ble_store_config_peer_secs = panchip_mem_get((MYNEWT_VAL(BLE_STORE_MAX_BONDS)) * sizeof(struct ble_store_value_sec), 8); |
| | | ble_store_config_cccds = panchip_mem_get((MYNEWT_VAL(BLE_STORE_MAX_CCCDS)) * sizeof(struct ble_store_value_cccd), 4); |
| | | #endif |
| | | #endif |
| | | |
| | | SYSINIT_ASSERT_ACTIVE(); |
| | | |
| | | host_kvstore_init(); |
| | | #if MYNEWT_VAL_BLE_STORE_CONFIG_PERSIST |
| | | /* kv initialization */ |
| | | extern void kv_init(void); |
| | | kv_init(); |
| | | #endif |
| | | |
| | | ble_hs_cfg.store_read_cb = ble_store_config_read; |
| | | ble_hs_cfg.store_write_cb = ble_store_config_write; |