From 62ca45d7552b475c79bca8e4e34577043093f0d6 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Tue, 28 Feb 2017 12:08:10 +0100 Subject: [PATCH] h4_ehcill/h5: provide HCI_EVENT_TRANSPORT_SLEEP_MODE event that indicates to app if it can put device into deep sleep --- src/btstack_defines.h | 7 +++++++ src/btstack_event.h | 10 ++++++++++ src/hci_transport_h4.c | 17 +++++++++++++++++ src/hci_transport_h5.c | 16 ++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/src/btstack_defines.h b/src/btstack_defines.h index f8a7abfd0..46b6e3381 100644 --- a/src/btstack_defines.h +++ b/src/btstack_defines.h @@ -365,6 +365,13 @@ typedef uint8_t sm_key_t[16]; // additional HCI events +/** + * @brief Indicates HCI transport enters/exits Sleep mode + * @format 1 + * @param active + */ +#define HCI_EVENT_TRANSPORT_SLEEP_MODE 0x66 + /** * @brief Outgoing packet */ diff --git a/src/btstack_event.h b/src/btstack_event.h index ba2bf4986..3ad622f3a 100644 --- a/src/btstack_event.h +++ b/src/btstack_event.h @@ -943,6 +943,16 @@ static inline uint8_t btstack_event_discoverable_enabled_get_discoverable(const return event[2]; } +/** + * @brief Get field active from event HCI_EVENT_TRANSPORT_SLEEP_MODE + * @param event packet + * @return active + * @note: btstack_type 1 + */ +static inline uint8_t hci_event_transport_sleep_mode_get_active(const uint8_t * event){ + return event[2]; +} + /** * @brief Get field handle from event HCI_EVENT_SCO_CAN_SEND_NOW * @param event packet diff --git a/src/hci_transport_h4.c b/src/hci_transport_h4.c index e1f2deb26..5f1f4d1a1 100644 --- a/src/hci_transport_h4.c +++ b/src/hci_transport_h4.c @@ -333,6 +333,19 @@ static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ #ifdef ENABLE_EHCILL +static void hci_transport_h4_ehcill_emit_sleep_state(int sleep_active){ + static int last_state = 0; + if (sleep_active == last_state) return; + last_state = sleep_active; + + log_info("hci_transport_h4_ehcill_emit_sleep_state: %u", sleep_active); + uint8_t event[3]; + event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE; + event[1] = sizeof(event) - 2; + event[2] = sleep_active; + packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); +} + static void hci_transport_h4_ehcill_open(void){ hci_transport_h4_ehcill_reset_statemachine(); @@ -412,6 +425,7 @@ static void hci_transport_h4_ehcill_trigger_wakeup(void){ break; } // UART needed again + hci_transport_h4_ehcill_emit_sleep_state(0); if (btstack_uart_sleep_mode){ btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); } @@ -461,6 +475,7 @@ static void hci_transport_h4_ehcill_handle_command(uint8_t action){ switch(action){ case EHCILL_WAKE_UP_IND: ehcill_state = EHCILL_STATE_AWAKE; + hci_transport_h4_ehcill_emit_sleep_state(0); #ifdef ENABLE_LOG_EHCILL log_info("eHCILL: Received WAKE_UP_IND RX"); #endif @@ -524,9 +539,11 @@ static void hci_transport_h4_ehcill_handle_ehcill_command_sent(void){ if (btstack_uart_sleep_mode != BTSTACK_UART_SLEEP_OFF){ btstack_uart->set_sleep(btstack_uart_sleep_mode); } + hci_transport_h4_ehcill_emit_sleep_state(1); } // already packet ready? then start wakeup if (hci_transport_h4_ehcill_outgoing_packet_ready()){ + hci_transport_h4_ehcill_emit_sleep_state(0); if (btstack_uart_sleep_mode){ btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF); } diff --git a/src/hci_transport_h5.c b/src/hci_transport_h5.c index 1082807d5..23358345a 100644 --- a/src/hci_transport_h5.c +++ b/src/hci_transport_h5.c @@ -479,6 +479,19 @@ static void hci_transport_h5_queue_packet(uint8_t packet_type, uint8_t *packet, hci_packet_size = size; } +static void hci_transport_h5_emit_sleep_state(int sleep_active){ + static int last_state = 0; + if (sleep_active == last_state) return; + last_state = sleep_active; + + log_info("hci_transport_h5_emit_sleep_state: %u", sleep_active); + uint8_t event[3]; + event[0] = HCI_EVENT_TRANSPORT_SLEEP_MODE; + event[1] = sizeof(event) - 2; + event[2] = sleep_active; + packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); +} + static void hci_transport_h5_process_frame(uint16_t frame_size){ if (frame_size < 4) return; @@ -619,6 +632,7 @@ static void hci_transport_h5_process_frame(uint16_t frame_size){ if (btstack_uart_sleep_mode){ log_info("link: received sleep message. Enabling UART Sleep."); btstack_uart->set_sleep(btstack_uart_sleep_mode); + hci_transport_h5_emit_sleep_state(1); } else { log_info("link: received sleep message. UART Sleep not supported"); } @@ -710,6 +724,7 @@ static void hci_transport_h5_block_sent(void){ } else { log_info("link: sent sleep message. UART Sleep not supported"); } + hci_transport_h5_emit_sleep_state(1); } hci_transport_link_run(); @@ -798,6 +813,7 @@ static int hci_transport_h5_send_packet(uint8_t packet_type, uint8_t *packet, in // send wakeup first if (link_peer_asleep){ + hci_transport_h5_emit_sleep_state(0); if (btstack_uart_sleep_mode){ log_info("h5: disable UART sleep"); btstack_uart->set_sleep(BTSTACK_UART_SLEEP_OFF);