mirror of
https://github.com/RfidResearchGroup/ChameleonUltra.git
synced 2026-05-12 11:22:59 -07:00
Security BLE implemented
This commit is contained in:
@@ -113,7 +113,56 @@ SRC_FILES += \
|
||||
$(SDK_ROOT)/components/softdevice/common/nrf_sdh.c \
|
||||
$(SDK_ROOT)/components/softdevice/common/nrf_sdh_ble.c \
|
||||
$(SDK_ROOT)/components/softdevice/common/nrf_sdh_soc.c \
|
||||
|
||||
$(SDK_ROOT)/components/ble/peer_manager/auth_status_tracker.c \
|
||||
$(SDK_ROOT)/components/ble/peer_manager/gatt_cache_manager.c \
|
||||
$(SDK_ROOT)/components/ble/peer_manager/gatts_cache_manager.c \
|
||||
$(SDK_ROOT)/components/ble/peer_manager/id_manager.c \
|
||||
$(SDK_ROOT)/components/ble/peer_manager/nrf_ble_lesc.c \
|
||||
$(SDK_ROOT)/components/ble/peer_manager/peer_data_storage.c \
|
||||
$(SDK_ROOT)/components/ble/peer_manager/peer_database.c \
|
||||
$(SDK_ROOT)/components/ble/peer_manager/peer_id.c \
|
||||
$(SDK_ROOT)/components/ble/peer_manager/peer_manager.c \
|
||||
$(SDK_ROOT)/components/ble/peer_manager/peer_manager_handler.c \
|
||||
$(SDK_ROOT)/components/ble/peer_manager/pm_buffer.c \
|
||||
$(SDK_ROOT)/components/ble/peer_manager/security_dispatcher.c \
|
||||
$(SDK_ROOT)/components/ble/peer_manager/security_manager.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/nrf_hw/nrf_hw_backend_init.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/nrf_hw/nrf_hw_backend_rng.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/nrf_hw/nrf_hw_backend_rng_mbedtls.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_aead.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_aes.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_aes_shared.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_ecc.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_ecdh.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_ecdsa.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_eddsa.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_error.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_hash.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_hkdf.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_hmac.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_init.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_rng.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/nrf_crypto_shared.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_aes.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_aes_aead.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_chacha_poly_aead.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_ecc.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_ecdh.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_ecdsa.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_eddsa.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_hash.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_hmac.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_init.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_mutex.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_rng.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_shared.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/oberon/oberon_backend_chacha_poly_aead.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/oberon/oberon_backend_ecc.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/oberon/oberon_backend_ecdh.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/oberon/oberon_backend_ecdsa.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/oberon/oberon_backend_eddsa.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/oberon/oberon_backend_hash.c \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/oberon/oberon_backend_hmac.c \
|
||||
|
||||
# Include folders common to all targets
|
||||
INC_FOLDERS += \
|
||||
@@ -147,6 +196,7 @@ INC_FOLDERS += \
|
||||
$(SDK_ROOT)/components/libraries/bootloader/ble_dfu \
|
||||
$(SDK_ROOT)/components/nfc/ndef/connection_handover/common \
|
||||
$(SDK_ROOT)/components/libraries/fifo \
|
||||
$(SDK_ROOT)/components/libraries/stack_info \
|
||||
$(SDK_ROOT)/components/boards \
|
||||
$(SDK_ROOT)/components/nfc/ndef/generic/record \
|
||||
$(SDK_ROOT)/components/nfc/t4t_parser/cc_file \
|
||||
@@ -257,10 +307,22 @@ INC_FOLDERS += \
|
||||
$(SDK_ROOT)/components/nfc/ndef/conn_hand_parser/ac_rec_parser \
|
||||
$(SDK_ROOT)/components/libraries/stack_guard \
|
||||
$(SDK_ROOT)/components/libraries/log/src \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310 \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cc310_bl \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/mbedtls \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/oberon \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/micro_ecc \
|
||||
$(SDK_ROOT)/external/nrf_cc310/include \
|
||||
$(SDK_ROOT)/external/nrf_cc310_bl/include \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/optiga \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/nrf_sw \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/nrf_hw \
|
||||
$(SDK_ROOT)/components/libraries/crypto/backend/cifra \
|
||||
|
||||
# Libraries common to all targets
|
||||
LIB_FILES += \
|
||||
|
||||
$(SDK_ROOT)/external/nrf_cc310/lib/cortex-m4/hard-float/libnrf_cc310_0.9.13.a \
|
||||
$(SDK_ROOT)/external/nrf_oberon/lib/cortex-m4/hard-float/liboberon_3.0.8.a \
|
||||
|
||||
ifeq (${CURRENT_DEVICE_TYPE}, ${CHAMELEON_ULTRA})
|
||||
# Append reader module source code to compile list.
|
||||
@@ -323,7 +385,6 @@ OPT = -O3 -g3
|
||||
CFLAGS += $(OPT)
|
||||
CFLAGS += -DAPP_TIMER_V2
|
||||
CFLAGS += -DAPP_TIMER_V2_RTC1_ENABLED
|
||||
|
||||
# CFLAGS += -DCONFIG_GPIO_AS_PINRESET
|
||||
CFLAGS += -DFLOAT_ABI_HARD
|
||||
CFLAGS += -DNRF52840_XXAA
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "nrfx_nfct.h"
|
||||
#include "nrfx_power.h"
|
||||
#include "nrf_drv_lpcomp.h"
|
||||
#include "nrf_ble_lesc.h"
|
||||
|
||||
#define NRF_LOG_MODULE_NAME app_main
|
||||
#include "nrf_log.h"
|
||||
@@ -107,7 +108,7 @@ static void power_management_init(void) {
|
||||
|
||||
/**@brief Function for initializing power management.
|
||||
*/
|
||||
static void rng_drv_and_srand_init(void) {
|
||||
void rng_drv_and_srand_init(void) {
|
||||
ret_code_t err_code;
|
||||
uint8_t available;
|
||||
uint32_t rand_int;
|
||||
@@ -416,7 +417,7 @@ static void check_wakeup_src(void) {
|
||||
|
||||
if (m_reset_source & NRF_POWER_RESETREAS_OFF_MASK) {
|
||||
NRF_LOG_INFO("WakeUp from button");
|
||||
advertising_start(); // Turn on Bluetooth radio
|
||||
advertising_start(false); // Turn on Bluetooth radio
|
||||
|
||||
// Button wake-up boot animation
|
||||
uint8_t animation_config = settings_get_animation_config();
|
||||
@@ -474,7 +475,7 @@ static void check_wakeup_src(void) {
|
||||
// light_up_by_slot();
|
||||
|
||||
// Start Bluetooth radio with USB plugged in, no deep hibernation required
|
||||
advertising_start();
|
||||
advertising_start(false);
|
||||
} else {
|
||||
NRF_LOG_INFO("First power system");
|
||||
|
||||
@@ -499,7 +500,7 @@ static void check_wakeup_src(void) {
|
||||
if (nrfx_power_usbstatus_get() != NRFX_POWER_USB_STATE_DISCONNECTED) {
|
||||
NRF_LOG_INFO("USB Power found.");
|
||||
// usb plugged in can broadcast BLE at will
|
||||
advertising_start();
|
||||
advertising_start(true);
|
||||
} else {
|
||||
sleep_timer_start(SLEEP_DELAY_MS_FIRST_POWER); // Wait a while and go straight to hibernation, do nothing
|
||||
}
|
||||
@@ -752,28 +753,40 @@ static void blink_usb_led_status(void) {
|
||||
}
|
||||
}
|
||||
|
||||
static void lesc_event_process(void) {
|
||||
ret_code_t err_code;
|
||||
err_code = nrf_ble_lesc_request_handler();
|
||||
APP_ERROR_CHECK(err_code);
|
||||
}
|
||||
|
||||
static void ble_passkey_init(void) {
|
||||
set_ble_connect_key(settings_get_ble_connect_key());
|
||||
}
|
||||
|
||||
/**@brief Application main function.
|
||||
*/
|
||||
int main(void) {
|
||||
hw_connect_init(); // Remember to initialize the pins first
|
||||
init_leds(); // LED initialization
|
||||
|
||||
init_leds(); // LED initialization
|
||||
log_init(); // Log initialization
|
||||
gpio_te_init(); // Initialize GPIO matrix library
|
||||
app_timers_init(); // Initialize soft timer
|
||||
fds_util_init(); // Initialize fds tool package
|
||||
power_management_init(); // Power management initialization
|
||||
usb_cdc_init(); // USB cdc emulation initialization
|
||||
ble_slave_init(); // Bluetooth protocol stack initialization
|
||||
|
||||
rng_drv_and_srand_init(); // Random number generator initialization
|
||||
bsp_timer_init(); // Initialize timeout timer
|
||||
bsp_timer_start(); // Start BSP TIMER and prepare it for processing business logic
|
||||
button_init(); // Button initialization for handling business logic
|
||||
sleep_timer_init(); // Soft timer initialization for hibernation
|
||||
rng_drv_and_srand_init(); // Random number generator initialization
|
||||
power_management_init(); // Power management initialization
|
||||
usb_cdc_init(); // USB cdc emulation initialization
|
||||
ble_slave_init(); // Bluetooth protocol stack initialization
|
||||
fds_util_init(); // Initialize fds tool package
|
||||
tag_emulation_init(); // Analog card initialization
|
||||
rgb_marquee_init(); // Light effect initialization
|
||||
|
||||
settings_load_config(); // Load settings from flash
|
||||
ble_passkey_init(); // after settings loaded, we can init ble connect key.
|
||||
|
||||
// cmd callback register
|
||||
on_data_frame_complete(on_data_frame_received);
|
||||
@@ -788,6 +801,8 @@ int main(void) {
|
||||
// Enter main loop.
|
||||
NRF_LOG_INFO("Chameleon working");
|
||||
while (1) {
|
||||
// process lesc event
|
||||
lesc_event_process();
|
||||
// Button event process
|
||||
button_press_process();
|
||||
// Led blink at usb status
|
||||
|
||||
@@ -10,8 +10,12 @@
|
||||
#include "nrf_sdh_soc.h"
|
||||
#include "nrf_sdh_ble.h"
|
||||
#include "nrf_ble_gatt.h"
|
||||
#include "nrf_ble_lesc.h"
|
||||
#include "nrf_drv_saadc.h"
|
||||
|
||||
#include "peer_manager.h"
|
||||
#include "peer_manager_handler.h"
|
||||
|
||||
#include "app_timer.h"
|
||||
#include "app_util_platform.h"
|
||||
|
||||
@@ -19,6 +23,7 @@
|
||||
#include "ble_main.h"
|
||||
#include "dataframe.h"
|
||||
#include "hw_connect.h"
|
||||
#include "settings.h"
|
||||
|
||||
#define NRF_LOG_MODULE_NAME ble_main
|
||||
#include "nrf_log.h"
|
||||
@@ -27,6 +32,23 @@
|
||||
NRF_LOG_MODULE_REGISTER();
|
||||
|
||||
|
||||
#define LESC_DEBUG_MODE 0 /**< Set to 1 to use the LESC debug keys. The debug mode allows you to use a sniffer to inspect traffic. */
|
||||
#define LESC_MITM_NC 1 /**< Use MITM (Numeric Comparison). */
|
||||
|
||||
#define SEC_PARAMS_BOND 1 /**< Perform bonding. */
|
||||
#if LESC_MITM_NC
|
||||
#define SEC_PARAMS_MITM 1 /**< Man In The Middle protection required. */
|
||||
#define SEC_PARAMS_IO_CAPABILITIES BLE_GAP_IO_CAPS_DISPLAY_ONLY
|
||||
#else
|
||||
#define SEC_PARAMS_MITM 0 /**< Man In The Middle protection required. */
|
||||
#define SEC_PARAMS_IO_CAPABILITIES BLE_GAP_IO_CAPS_DISPLAY_ONLY
|
||||
#endif
|
||||
#define SEC_PARAMS_LESC 1 /**< LE Secure Connections pairing required. */
|
||||
#define SEC_PARAMS_KEYPRESS 0 /**< Keypress notifications not required. */
|
||||
#define SEC_PARAMS_OOB 0 /**< Out Of Band data not available. */
|
||||
#define SEC_PARAMS_MIN_KEY_SIZE 7 /**< Minimum encryption key size in octets. */
|
||||
#define SEC_PARAMS_MAX_KEY_SIZE 16 /**< Maximum encryption key size in octets. */
|
||||
|
||||
#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
|
||||
#define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */
|
||||
|
||||
@@ -76,8 +98,21 @@ static ble_uuid_t m_adv_uuids[] =
|
||||
};
|
||||
volatile bool g_is_ble_connected = false;
|
||||
volatile bool g_is_low_battery_shutdown = false;
|
||||
static ble_opt_t m_static_pin_option;
|
||||
|
||||
|
||||
/**@brief Function for the ble connect key setup.
|
||||
*
|
||||
* @details This function will set up the ble connect passkey.
|
||||
*/
|
||||
void set_ble_connect_key(uint8_t* key) {
|
||||
static uint8_t passkey[BLE_CONNECT_KEY_LEN_MAX];
|
||||
memcpy(passkey, key, BLE_CONNECT_KEY_LEN_MAX);
|
||||
m_static_pin_option.gap_opt.passkey.p_passkey = passkey;
|
||||
// NRF_LOG_RAW_HEXDUMP_INFO(passkey, 6);
|
||||
APP_ERROR_CHECK(sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &m_static_pin_option));
|
||||
}
|
||||
|
||||
/**@brief Function for the GAP initialization.
|
||||
*
|
||||
* @details This function will set up all the necessary GAP (Generic Access Profile) parameters of
|
||||
@@ -287,9 +322,9 @@ static void services_init(void) {
|
||||
bas_init_obj.p_report_ref = NULL;
|
||||
bas_init_obj.initial_batt_level = 100;
|
||||
|
||||
bas_init_obj.bl_rd_sec = SEC_OPEN;
|
||||
bas_init_obj.bl_cccd_wr_sec = SEC_OPEN;
|
||||
bas_init_obj.bl_report_rd_sec = SEC_OPEN;
|
||||
bas_init_obj.bl_rd_sec = SEC_MITM;
|
||||
bas_init_obj.bl_cccd_wr_sec = SEC_MITM;
|
||||
bas_init_obj.bl_report_rd_sec = SEC_MITM;
|
||||
|
||||
err_code = ble_bas_init(&m_bas, &bas_init_obj);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
@@ -403,10 +438,18 @@ static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context) {
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
|
||||
// Pairing not supported
|
||||
err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
// Pairing not supported? No, is supported now, hahahaha...
|
||||
// err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
|
||||
// APP_ERROR_CHECK(err_code);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_PASSKEY_DISPLAY: {
|
||||
char passkey[BLE_GAP_PASSKEY_LEN + 1];
|
||||
memcpy(passkey, p_ble_evt->evt.gap_evt.params.passkey_display.passkey, BLE_GAP_PASSKEY_LEN);
|
||||
passkey[BLE_GAP_PASSKEY_LEN] = 0x00;
|
||||
NRF_LOG_INFO("=== PASSKEY: %s =====", nrf_log_push(passkey));
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
|
||||
// No system attributes have been stored.
|
||||
@@ -511,15 +554,136 @@ static void advertising_init(void) {
|
||||
ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for starting advertising.
|
||||
/**@brief Clear bond information from persistent storage.
|
||||
*/
|
||||
void advertising_start(void) {
|
||||
uint32_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
|
||||
void delete_bonds_all(void)
|
||||
{
|
||||
ret_code_t err_code;
|
||||
|
||||
NRF_LOG_INFO("Erase bonds!");
|
||||
|
||||
err_code = pm_peers_delete();
|
||||
APP_ERROR_CHECK(err_code);
|
||||
}
|
||||
|
||||
/**@brief Function for setting filtered whitelist.
|
||||
*
|
||||
* @param[in] skip Filter passed to @ref pm_peer_id_list.
|
||||
*/
|
||||
static void whitelist_set(pm_peer_id_list_skip_t skip)
|
||||
{
|
||||
pm_peer_id_t peer_ids[BLE_GAP_WHITELIST_ADDR_MAX_COUNT];
|
||||
uint32_t peer_id_count = BLE_GAP_WHITELIST_ADDR_MAX_COUNT;
|
||||
|
||||
ret_code_t err_code = pm_peer_id_list(peer_ids, &peer_id_count, PM_PEER_ID_INVALID, skip);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
|
||||
NRF_LOG_INFO("Whitelist peer cnt %d, MAX_PEERS_WLIST %d", peer_id_count, BLE_GAP_WHITELIST_ADDR_MAX_COUNT);
|
||||
|
||||
err_code = pm_whitelist_set(peer_ids, peer_id_count);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
}
|
||||
|
||||
/**@brief Function for starting advertising.
|
||||
*/
|
||||
void advertising_start(bool erase_bonds)
|
||||
{
|
||||
if (erase_bonds == true)
|
||||
{
|
||||
delete_bonds_all();
|
||||
// Advertising is started by PM_EVT_PEERS_DELETE_SUCCEEDED event.
|
||||
}
|
||||
else
|
||||
{
|
||||
whitelist_set(PM_PEER_ID_LIST_SKIP_NO_ID_ADDR);
|
||||
|
||||
ret_code_t ret = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
|
||||
APP_ERROR_CHECK(ret);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for stop advertising.
|
||||
*/
|
||||
void advertising_stop(void)
|
||||
{
|
||||
sd_ble_gap_adv_stop(m_advertising.adv_handle);
|
||||
}
|
||||
|
||||
/**@brief Function for handling Peer Manager events.
|
||||
*
|
||||
* @param[in] p_evt Peer Manager event.
|
||||
*/
|
||||
static void pm_evt_handler(pm_evt_t const * p_evt)
|
||||
{
|
||||
pm_handler_on_pm_evt(p_evt);
|
||||
pm_handler_disconnect_on_sec_failure(p_evt);
|
||||
pm_handler_flash_clean(p_evt);
|
||||
|
||||
switch (p_evt->evt_id)
|
||||
{
|
||||
case PM_EVT_CONN_SEC_SUCCEEDED:
|
||||
// p_evt->peer_id;
|
||||
break;
|
||||
|
||||
case PM_EVT_PEERS_DELETE_SUCCEEDED:
|
||||
advertising_start(false);
|
||||
break;
|
||||
|
||||
case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED:
|
||||
if ( p_evt->params.peer_data_update_succeeded.flash_changed
|
||||
&& (p_evt->params.peer_data_update_succeeded.data_id == PM_PEER_DATA_ID_BONDING))
|
||||
{
|
||||
NRF_LOG_INFO("New Bond, add the peer to the whitelist if possible");
|
||||
// Note: You should check on what kind of white list policy your application should use.
|
||||
|
||||
whitelist_set(PM_PEER_ID_LIST_SKIP_NO_ID_ADDR);
|
||||
}
|
||||
break;
|
||||
case PM_EVT_CONN_SEC_CONFIG_REQ:
|
||||
{
|
||||
pm_conn_sec_config_t cfg;
|
||||
cfg.allow_repairing = true;
|
||||
pm_conn_sec_config_reply(p_evt->conn_handle, &cfg);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**@brief Function for the Peer Manager initialization.
|
||||
*/
|
||||
static void peer_manager_init(void)
|
||||
{
|
||||
ble_gap_sec_params_t sec_param;
|
||||
ret_code_t err_code;
|
||||
|
||||
err_code = pm_init();
|
||||
APP_ERROR_CHECK(err_code);
|
||||
|
||||
memset(&sec_param, 0, sizeof(ble_gap_sec_params_t));
|
||||
|
||||
// Security parameters to be used for all security procedures.
|
||||
sec_param.bond = SEC_PARAMS_BOND;
|
||||
sec_param.mitm = SEC_PARAMS_MITM;
|
||||
sec_param.lesc = SEC_PARAMS_LESC;
|
||||
sec_param.keypress = SEC_PARAMS_KEYPRESS;
|
||||
sec_param.io_caps = SEC_PARAMS_IO_CAPABILITIES;
|
||||
sec_param.oob = SEC_PARAMS_OOB;
|
||||
sec_param.min_key_size = SEC_PARAMS_MIN_KEY_SIZE;
|
||||
sec_param.max_key_size = SEC_PARAMS_MAX_KEY_SIZE;
|
||||
sec_param.kdist_own.enc = 1;
|
||||
sec_param.kdist_own.id = 1;
|
||||
sec_param.kdist_peer.enc = 1;
|
||||
sec_param.kdist_peer.id = 1;
|
||||
|
||||
err_code = pm_sec_params_set(&sec_param);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
|
||||
err_code = pm_register(pm_evt_handler);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
}
|
||||
|
||||
/**@brief Function for handling the ADC interrupt.
|
||||
*
|
||||
@@ -618,4 +782,5 @@ void ble_slave_init(void) {
|
||||
services_init(); // Initialization of service characteristics
|
||||
advertising_init(); // Broadcast parameter initialization
|
||||
conn_params_init(); // Connection parameter initialization
|
||||
peer_manager_init(); // Peer manager Initialization
|
||||
}
|
||||
|
||||
@@ -10,8 +10,11 @@ extern uint16_t batt_lvl_in_milli_volts;
|
||||
extern uint8_t percentage_batt_lvl;
|
||||
|
||||
void ble_slave_init(void);
|
||||
void advertising_start(void);
|
||||
void advertising_start(bool erase_bonds);
|
||||
void advertising_stop(void);
|
||||
void delete_bonds_all(void);
|
||||
void nus_data_response(uint8_t *p_data, uint16_t length);
|
||||
bool is_nus_working(void);
|
||||
void set_ble_connect_key(uint8_t* key);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -205,7 +205,7 @@
|
||||
// <e> PEER_MANAGER_ENABLED - peer_manager - Peer Manager
|
||||
//==========================================================
|
||||
#ifndef PEER_MANAGER_ENABLED
|
||||
#define PEER_MANAGER_ENABLED 0
|
||||
#define PEER_MANAGER_ENABLED 1
|
||||
#endif
|
||||
// <o> PM_MAX_REGISTRANTS - Number of event handlers that can be registered.
|
||||
#ifndef PM_MAX_REGISTRANTS
|
||||
@@ -253,7 +253,7 @@
|
||||
// <i> If set to true, you need to call nrf_ble_lesc_request_handler() in the main loop to respond to LESC-related BLE events. If LESC support is not required, set this to false to save code space.
|
||||
|
||||
#ifndef PM_LESC_ENABLED
|
||||
#define PM_LESC_ENABLED 0
|
||||
#define PM_LESC_ENABLED 1
|
||||
#endif
|
||||
|
||||
// <e> PM_RA_PROTECTION_ENABLED - Enable/disable protection against repeated pairing attempts in Peer Manager.
|
||||
@@ -294,6 +294,26 @@
|
||||
|
||||
// </e>
|
||||
|
||||
// <h> nrf_ble_lesc - Le Secure Connection
|
||||
|
||||
//==========================================================
|
||||
// <q> NRF_BLE_LESC_ENABLED - Enable LESC Module
|
||||
|
||||
|
||||
#ifndef NRF_BLE_LESC_ENABLED
|
||||
#define NRF_BLE_LESC_ENABLED 1
|
||||
#endif
|
||||
|
||||
// <q> NRF_BLE_LESC_GENERATE_NEW_KEYS - Generate new LESC keys after every pairing attempt. Keys are generated on the auth status event
|
||||
|
||||
|
||||
#ifndef NRF_BLE_LESC_GENERATE_NEW_KEYS
|
||||
#define NRF_BLE_LESC_GENERATE_NEW_KEYS 1
|
||||
#endif
|
||||
|
||||
// </h>
|
||||
//==========================================================
|
||||
|
||||
// </h>
|
||||
//==========================================================
|
||||
|
||||
@@ -736,7 +756,7 @@
|
||||
// <i> The CC310 hardware-accelerated cryptography backend (only available on nRF52840).
|
||||
//==========================================================
|
||||
#ifndef NRF_CRYPTO_BACKEND_CC310_ENABLED
|
||||
#define NRF_CRYPTO_BACKEND_CC310_ENABLED 0
|
||||
#define NRF_CRYPTO_BACKEND_CC310_ENABLED 1
|
||||
#endif
|
||||
// <q> NRF_CRYPTO_BACKEND_CC310_AES_CBC_ENABLED - Enable the AES CBC mode using CC310.
|
||||
|
||||
@@ -1356,6 +1376,30 @@
|
||||
|
||||
// </e>
|
||||
|
||||
// <h> nrf_crypto_rng - RNG Configuration
|
||||
|
||||
//==========================================================
|
||||
// <q> NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS_ENABLED - Use static memory buffers for context and temporary init buffer.
|
||||
|
||||
|
||||
// <i> Always recommended when using the nRF HW RNG as the context and temporary buffers are small. Consider disabling if using the CC310 RNG in a RAM constrained application. In this case, memory must be provided to nrf_crypto_rng_init, or it can be allocated internally provided that NRF_CRYPTO_ALLOCATOR does not allocate memory on the stack.
|
||||
|
||||
#ifndef NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS_ENABLED
|
||||
#define NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS_ENABLED 1
|
||||
#endif
|
||||
|
||||
// <q> NRF_CRYPTO_RNG_AUTO_INIT_ENABLED - Initialize the RNG module automatically when nrf_crypto is initialized.
|
||||
|
||||
|
||||
// <i> Automatic initialization is only supported with static or internally allocated context and temporary memory.
|
||||
|
||||
#ifndef NRF_CRYPTO_RNG_AUTO_INIT_ENABLED
|
||||
#define NRF_CRYPTO_RNG_AUTO_INIT_ENABLED 1
|
||||
#endif
|
||||
|
||||
// </h>
|
||||
//==========================================================
|
||||
|
||||
// </h>
|
||||
//==========================================================
|
||||
|
||||
|
||||
@@ -171,10 +171,26 @@ int fds_delete_sync(uint16_t id, uint16_t key) {
|
||||
return delete_count;
|
||||
}
|
||||
|
||||
static bool is_peer_manager_record(uint16_t id_or_key) {
|
||||
if (id_or_key > 0xBFFF) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*FDS event callback
|
||||
*/
|
||||
static void fds_evt_handler(fds_evt_t const *p_evt) {
|
||||
// Skip peermanager event
|
||||
if (is_peer_manager_record(p_evt->write.record_key)
|
||||
|| is_peer_manager_record(p_evt->write.file_id)
|
||||
|| is_peer_manager_record(p_evt->del.record_key)
|
||||
|| is_peer_manager_record(p_evt->del.file_id)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// To process fds event
|
||||
switch (p_evt->id) {
|
||||
case FDS_EVT_INIT: {
|
||||
|
||||
@@ -283,8 +283,10 @@ uint32_t ble_nus_init(ble_nus_t * p_nus, ble_nus_init_t const * p_nus_init)
|
||||
add_char_params.char_props.write = 1;
|
||||
add_char_params.char_props.write_wo_resp = 1;
|
||||
|
||||
add_char_params.read_access = SEC_OPEN;
|
||||
add_char_params.write_access = SEC_OPEN;
|
||||
// add_char_params.read_access = SEC_OPEN;
|
||||
// add_char_params.write_access = SEC_OPEN;
|
||||
add_char_params.read_access = SEC_MITM;
|
||||
add_char_params.write_access = SEC_MITM;
|
||||
|
||||
err_code = characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->rx_handles);
|
||||
if (err_code != NRF_SUCCESS)
|
||||
@@ -302,9 +304,12 @@ uint32_t ble_nus_init(ble_nus_t * p_nus, ble_nus_init_t const * p_nus_init)
|
||||
add_char_params.is_var_len = true;
|
||||
add_char_params.char_props.notify = 1;
|
||||
|
||||
add_char_params.read_access = SEC_OPEN;
|
||||
add_char_params.write_access = SEC_OPEN;
|
||||
add_char_params.cccd_write_access = SEC_OPEN;
|
||||
// add_char_params.read_access = SEC_OPEN;
|
||||
// add_char_params.write_access = SEC_OPEN;
|
||||
// add_char_params.cccd_write_access = SEC_OPEN;
|
||||
add_char_params.read_access = SEC_MITM;
|
||||
add_char_params.write_access = SEC_MITM;
|
||||
add_char_params.cccd_write_access = SEC_MITM;
|
||||
|
||||
return characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->tx_handles);
|
||||
/**@snippet [Adding proprietary characteristic to the SoftDevice] */
|
||||
|
||||
Reference in New Issue
Block a user