125125#define BLE_DISCON 3
126126#define BLE_CON 4
127127#define BLE_PAIR 5
128+ #define BLE_CONN_STATUS 6
128129
129130#define PWR_DEF 0
130131#define PWR_SHUTDOWN_SYS 1
163164#define BATTERY_LEVEL_MEAS_INTERVAL APP_TIMER_TICKS(1000) /**< Battery level measurement interval (ticks). */
164165#define BATTERY_MEAS_LONG_INTERVAL APP_TIMER_TICKS(5000)
165166
166- #define MIN_CONN_INTERVAL MSEC_TO_UNITS(30 , UNIT_1_25_MS) /**< Minimum acceptable connection interval (10 ms). */
167- #define MAX_CONN_INTERVAL MSEC_TO_UNITS(30 , UNIT_1_25_MS) /**< Maximum acceptable connection interval (100 ms) */
167+ #define MIN_CONN_INTERVAL MSEC_TO_UNITS(15 , UNIT_1_25_MS) /**< Minimum acceptable connection interval (10 ms). */
168+ #define MAX_CONN_INTERVAL MSEC_TO_UNITS(15 , UNIT_1_25_MS) /**< Maximum acceptable connection interval (100 ms) */
168169#define SLAVE_LATENCY 0 /**< Slave latency. */
169170#define CONN_SUP_TIMEOUT MSEC_TO_UNITS(300, UNIT_10_MS) /**< Connection supervisory timeout (4 seconds). */
170171#define FIRST_CONN_PARAMS_UPDATE_DELAY \
271272#define ST_SEND_CLOSE_BLE 0x02
272273#define ST_SEND_DISCON_BLE 0x03
273274#define ST_GET_BLE_SWITCH_STATUS 0x04
275+ #define ST_GET_BLE_CONN_STATUS 0x05
274276//
275277#define ST_CMD_POWER 0x82
276278#define ST_SEND_CLOSE_SYS_PWR 0x01
367369
368370BLE_NUS_DEF (m_nus , NRF_SDH_BLE_TOTAL_LINK_COUNT ); /**< BLE NUS service instance. */
369371BLE_BAS_DEF (m_bas );
372+ #if BLE_FIDO_ENABLED
370373BLE_FIDO_DEF (m_fido , NRF_SDH_BLE_TOTAL_LINK_COUNT );
374+ #endif
371375NRF_BLE_GATT_DEF (m_gatt ); /**< GATT module instance. */
372376NRF_BLE_QWR_DEF (m_qwr ); /**< Context for the Queued Write module.*/
373377BLE_ADVERTISING_DEF (m_advertising ); /**< Advertising module instance. */
@@ -408,7 +412,9 @@ static ble_uuid_t m_adv_uuids[] = /**< Universally unique
408412 {BLE_UUID_DEVICE_INFORMATION_SERVICE , BLE_UUID_TYPE_BLE },
409413#endif
410414 {BLE_UUID_BATTERY_SERVICE , BLE_UUID_TYPE_BLE },
415+ #if BLE_FIDO_ENABLED
411416 {BLE_UUID_FIDO_SERVICE , BLE_UUID_TYPE_BLE },
417+ #endif
412418 {BLE_UUID_NUS_SERVICE , BLE_UUID_TYPE_BLE }};
413419
414420static volatile uint8_t flag_uart_trans = 1 ;
@@ -668,7 +674,7 @@ static void pm_evt_handler(const pm_evt_t* p_evt)
668674
669675 case PM_EVT_BONDED_PEER_CONNECTED :
670676 NRF_LOG_INFO ("%s ---> PM_EVT_BONDED_PEER_CONNECTED" , __func__ );
671- request_service_changed = true;
677+ // request_service_changed = true;
672678 break ;
673679
674680 case PM_EVT_CONN_SEC_CONFIG_REQ :
@@ -943,7 +949,7 @@ static void nus_data_handler(ble_nus_evt_t* p_evt)
943949 if ( nus_data_buf [0 ] == '?' )
944950 {
945951 pad = (nus_data_len + 63 ) / 64 ;
946- if ( nus_data_len - pad > msg_len )
952+ if ( nus_data_len - pad >= msg_len )
947953 {
948954 rcv_head_flag = DATA_INIT ;
949955 nus_data_len = msg_len + (msg_len + 62 ) / 63 ;
@@ -960,8 +966,8 @@ static void nus_data_handler(ble_nus_evt_t* p_evt)
960966 rcv_head_flag = DATA_INIT ;
961967 }
962968 }
963- // spi_write_st_data(nus_data_buf, nus_data_len);
964- app_sched_event_put (nus_data_buf , nus_data_len , spi_write_st_data );
969+ spi_write_st_data (nus_data_buf , nus_data_len );
970+ // app_sched_event_put(nus_data_buf, nus_data_len, spi_write_st_data);
965971 }
966972 else if ( p_evt -> type == BLE_NUS_EVT_TX_RDY )
967973 {
@@ -982,7 +988,9 @@ static void nus_data_handler(ble_nus_evt_t* p_evt)
982988 }
983989}
984990
991+ #if BLE_FIDO_ENABLED
985992#include "fido.h"
993+ #endif
986994
987995/**@brief Function for initializing services that will be used by the application.
988996 *
@@ -994,7 +1002,6 @@ static void services_init(void)
9941002 nrf_ble_qwr_init_t qwr_init = {0 };
9951003 ble_dis_init_t dis_init ;
9961004 ble_nus_init_t nus_init ;
997- ble_fido_init_t fido_init = {0 };
9981005
9991006 // Initialize Queued Write Module.
10001007 qwr_init .error_handler = nrf_qwr_error_handler ;
@@ -1010,7 +1017,9 @@ static void services_init(void)
10101017 memset (& dis_init , 0 , sizeof (dis_init ));
10111018
10121019 ble_srv_ascii_to_utf8 (& dis_init .manufact_name_str , MANUFACTURER_NAME );
1020+ #if BLE_FIDO_ENABLED
10131021 ble_srv_ascii_to_utf8 (& dis_init .model_num_str , ble_adv_name );
1022+ #endif
10141023 ble_srv_ascii_to_utf8 (& dis_init .serial_num_str , MODEL_NUMBER );
10151024 ble_srv_ascii_to_utf8 (& dis_init .hw_rev_str , HW_REVISION );
10161025 ble_srv_ascii_to_utf8 (& dis_init .fw_rev_str , FW_REVISION );
@@ -1033,11 +1042,14 @@ static void services_init(void)
10331042 err_code = ble_nus_init (& m_nus , & nus_init );
10341043 APP_ERROR_CHECK (err_code );
10351044
1045+ #if BLE_FIDO_ENABLED
1046+ ble_fido_init_t fido_init = {0 };
1047+
10361048 // Initialize FIDO.
1037- memset (& fido_init , 0 , sizeof (fido_init ));
10381049 fido_init .data_handler = fido_data_handler ;
10391050 err_code = ble_fido_init (& m_fido , & fido_init );
1040- APP_ERROR_CHECK (err_code );
1051+ APP_ERROR_CHECK (err_code );
1052+ #endif
10411053}
10421054
10431055/**@brief Function for the Timer initialization.
@@ -1173,24 +1185,27 @@ void send_service_changed(void* p_event_data, uint16_t event_size)
11731185
11741186 err_code = sd_ble_gatts_initial_user_handle_get (& start_handle );
11751187
1176- if ( err_code != NRF_SUCCESS )
1188+ if ( err_code != NRF_SUCCESS )
11771189 {
1178- NRF_LOG_ERROR ("sd_ble_gatts_initial_user_handle_get() returned %s which should not happen." ,
1179- nrf_strerror_get (err_code ));
1190+ NRF_LOG_ERROR (
1191+ "sd_ble_gatts_initial_user_handle_get() returned %s which should not happen." , nrf_strerror_get (err_code )
1192+ );
11801193 return ;
11811194 }
11821195
11831196 NRF_LOG_INFO ("m_conn_handle: 0x%04x, start_handle: 0x%04x" , m_conn_handle , start_handle );
11841197 err_code = sd_ble_gatts_service_changed (m_conn_handle , start_handle , 0xFFFF );
1185- if ((err_code == BLE_ERROR_INVALID_CONN_HANDLE ) || (err_code == NRF_ERROR_INVALID_STATE ) || (err_code == NRF_ERROR_BUSY ))
1198+ if ( (err_code == BLE_ERROR_INVALID_CONN_HANDLE ) || (err_code == NRF_ERROR_INVALID_STATE ) ||
1199+ (err_code == NRF_ERROR_BUSY ) )
11861200 {
11871201 /* These errors can be expected when trying to send a Service Changed indication */
11881202 /* if the CCCD is not set to indicate. Thus, set the returning error code to success. */
1189- NRF_LOG_WARNING ("Client did not have the Service Changed indication set to enabled."
1190- "Error: 0x%08x" ,
1191- err_code );
1203+ NRF_LOG_WARNING (
1204+ "Client did not have the Service Changed indication set to enabled."
1205+ "Error: 0x%08x" ,
1206+ err_code
1207+ );
11921208 err_code = NRF_SUCCESS ;
1193-
11941209 }
11951210 APP_ERROR_CHECK (err_code );
11961211}
@@ -1374,7 +1389,7 @@ static void ble_evt_handler(const ble_evt_t* p_ble_evt, void* p_context)
13741389
13751390 case BLE_GATTS_EVT_SYS_ATTR_MISSING :
13761391 case BLE_GAP_EVT_CONN_SEC_UPDATE :
1377- NRF_LOG_INFO ("BLE_GAP_EVT_CONN_SEC_UPDATE" );
1392+ NRF_LOG_INFO ("BLE_GAP_EVT_CONN_SEC_UPDATE" );
13781393 break ;
13791394
13801395 default :
@@ -1579,6 +1594,9 @@ void uart_event_handle(app_uart_evt_t* p_event)
15791594 case ST_GET_BLE_SWITCH_STATUS :
15801595 ble_conn_flag = BLE_CON ;
15811596 break ;
1597+ case ST_GET_BLE_CONN_STATUS :
1598+ ble_conn_flag = BLE_CONN_STATUS ;
1599+ break ;
15821600 default :
15831601 break ;
15841602 }
@@ -1858,8 +1876,8 @@ void in_gpiote_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
18581876 }
18591877 else if ( nrf_gpio_pin_read (SLAVE_SPI_RSP_IO ) == 0 && !spi_dir_out )
18601878 {
1861- // spi_read_st_data(NULL, 0);
1862- app_sched_event_put (NULL , 0 , spi_read_st_data );
1879+ spi_read_st_data (NULL , 0 );
1880+ // app_sched_event_put(NULL, 0, spi_read_st_data);
18631881 }
18641882 break ;
18651883 default :
@@ -2142,13 +2160,20 @@ static void ble_ctl_process(void* p_event_data, uint16_t event_size)
21422160
21432161 bt_disconnect ();
21442162 }
2145- if ( BLE_CON == ble_conn_flag )
2163+ else if ( BLE_CON == ble_conn_flag )
21462164 {
21472165 ble_conn_flag = BLE_DEF ;
21482166 bak_buff [0 ] = BLE_CMD_CON_STA ;
21492167 bak_buff [1 ] = ble_status_flag + 2 ;
21502168 send_stm_data (bak_buff , 2 );
21512169 }
2170+ else if ( BLE_CONN_STATUS == ble_conn_flag )
2171+ {
2172+ ble_conn_flag = BLE_DEF ;
2173+ bak_buff [0 ] = BLE_CMD_CON_STA ;
2174+ bak_buff [1 ] = m_conn_handle == BLE_CONN_HANDLE_INVALID ? BLE_DISCON_STATUS : BLE_CON_STATUS ;
2175+ send_stm_data (bak_buff , 2 );
2176+ }
21522177
21532178 switch ( pwr_status_flag )
21542179 {
0 commit comments