123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- #include "bsp_btn_ble.h"
- #include "nrf_sdh_ble.h"
- #include "bsp.h"
- #define BTN_ID_WAKEUP 0
- #define BTN_ID_SLEEP 0
- #define BTN_ID_DISCONNECT 0
- #define BTN_ID_WAKEUP_BOND_DELETE 1
- #define BTN_ID_WHITELIST_OFF 1
- #define BTN_ACTION_SLEEP BSP_BUTTON_ACTION_RELEASE
- #define BTN_ACTION_DISCONNECT BSP_BUTTON_ACTION_LONG_PUSH
- #define BTN_ACTION_WHITELIST_OFF BSP_BUTTON_ACTION_LONG_PUSH
- #define RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code) \
- do \
- { \
- if (((err_code) != NRF_SUCCESS) && ((err_code) != NRF_ERROR_INVALID_PARAM)) \
- { \
- return err_code; \
- } \
- } \
- while (0)
- #define RETURN_ON_ERROR_NOT_NOT_SUPPORTED(err_code) \
- do \
- { \
- if (((err_code) != NRF_SUCCESS) && ((err_code) != NRF_ERROR_NOT_SUPPORTED)) \
- { \
- return err_code; \
- } \
- } \
- while (0)
- #define CALL_HANDLER_ON_ERROR(err_code) \
- do \
- { \
- if (((err_code) != NRF_SUCCESS) && (m_error_handler != NULL)) \
- { \
- m_error_handler(err_code); \
- } \
- } \
- while (0)
- static bsp_btn_ble_error_handler_t m_error_handler = NULL;
- static uint32_t m_num_connections = 0;
- static uint32_t connection_buttons_configure()
- {
- uint32_t err_code;
- err_code = bsp_event_to_button_action_assign(BTN_ID_SLEEP,
- BTN_ACTION_SLEEP,
- BSP_EVENT_DEFAULT);
- RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);
- err_code = bsp_event_to_button_action_assign(BTN_ID_WHITELIST_OFF,
- BTN_ACTION_WHITELIST_OFF,
- BSP_EVENT_WHITELIST_OFF);
- RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);
- err_code = bsp_event_to_button_action_assign(BTN_ID_DISCONNECT,
- BTN_ACTION_DISCONNECT,
- BSP_EVENT_DISCONNECT);
- RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);
- return NRF_SUCCESS;
- }
- static uint32_t advertising_buttons_configure()
- {
- uint32_t err_code;
- err_code = bsp_event_to_button_action_assign(BTN_ID_DISCONNECT,
- BTN_ACTION_DISCONNECT,
- BSP_EVENT_DEFAULT);
- RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);
- err_code = bsp_event_to_button_action_assign(BTN_ID_WHITELIST_OFF,
- BTN_ACTION_WHITELIST_OFF,
- BSP_EVENT_WHITELIST_OFF);
- RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);
- err_code = bsp_event_to_button_action_assign(BTN_ID_SLEEP,
- BTN_ACTION_SLEEP,
- BSP_EVENT_SLEEP);
- RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);
- return NRF_SUCCESS;
- }
- static void startup_event_extract(bsp_event_t * p_startup_event)
- {
-
- if (bsp_button_is_pressed(BTN_ID_WAKEUP_BOND_DELETE))
- {
- *p_startup_event = BSP_EVENT_CLEAR_BONDING_DATA;
- }
- else if (bsp_button_is_pressed(BTN_ID_WAKEUP))
- {
- *p_startup_event = BSP_EVENT_WAKEUP;
- }
- else
- {
- *p_startup_event = BSP_EVENT_NOTHING;
- }
- }
- uint32_t bsp_btn_ble_sleep_mode_prepare(void)
- {
- uint32_t err_code;
- err_code = bsp_wakeup_button_enable(BTN_ID_WAKEUP);
- RETURN_ON_ERROR_NOT_NOT_SUPPORTED(err_code);
- err_code = bsp_wakeup_button_enable(BTN_ID_WAKEUP_BOND_DELETE);
- RETURN_ON_ERROR_NOT_NOT_SUPPORTED(err_code);
- return NRF_SUCCESS;
- }
- static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
- {
- uint32_t err_code;
- switch (p_ble_evt->header.evt_id)
- {
- case BLE_GAP_EVT_CONNECTED:
- if (m_num_connections == 0)
- {
- err_code = connection_buttons_configure();
- CALL_HANDLER_ON_ERROR(err_code);
- }
- m_num_connections++;
- break;
- case BLE_GAP_EVT_DISCONNECTED:
- m_num_connections--;
- if (m_num_connections == 0)
- {
- err_code = advertising_buttons_configure();
- CALL_HANDLER_ON_ERROR(err_code);
- }
- break;
- default:
- break;
- }
- }
- NRF_SDH_BLE_OBSERVER(m_ble_observer, BSP_BTN_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
- uint32_t bsp_btn_ble_init(bsp_btn_ble_error_handler_t error_handler, bsp_event_t * p_startup_bsp_evt)
- {
- uint32_t err_code = NRF_SUCCESS;
- m_error_handler = error_handler;
- if (p_startup_bsp_evt != NULL)
- {
- startup_event_extract(p_startup_bsp_evt);
- }
- if (m_num_connections == 0)
- {
- err_code = advertising_buttons_configure();
- }
- return err_code;
- }
|