diff --git a/include/btstack/run_loop.h b/include/btstack/run_loop.h index 09d466ccc..077e8db03 100644 --- a/include/btstack/run_loop.h +++ b/include/btstack/run_loop.h @@ -75,37 +75,44 @@ typedef struct timer { } timer_source_t; -// set timer based on current time +// Set timer based on current time in milliseconds. void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms); -// set timeout callback +// Set callback that will be executed when timer expires. void run_loop_set_timer_handler(timer_source_t *ts, void (*process)(timer_source_t *_ts)); -// add/remove timer_source +// Add/Remove timer source. void run_loop_add_timer(timer_source_t *timer); int run_loop_remove_timer(timer_source_t *timer); -// init must be called before any other run_loop call +// Init must be called before any other run_loop call. +// Use RUN_LOOP_EMBEDDED for embedded devices. void run_loop_init(RUN_LOOP_TYPE type); -// set data-source callback +// Set data source callback. void run_loop_set_data_source_handler(data_source_t *ds, int (*process)(data_source_t *_ds)); -// add/remove data_source +// Add/Remove data source. void run_loop_add_data_source(data_source_t *dataSource); int run_loop_remove_data_source(data_source_t *dataSource); -// execute configured run_loop +// Execute configured run loop. This function does not return. void run_loop_execute(void); // hack to fix HCI timer handling #ifdef HAVE_TICK -uint32_t embedded_get_ticks(void); +// Sets how many miliseconds has one tick. uint32_t embedded_ticks_for_ms(uint32_t time_in_ms); +// Queries the current time in ticks. +uint32_t embedded_get_ticks(void); #endif #ifdef EMBEDDED +// Sets an internal flag that is checked in the critical section +// just before entering sleep mode. Has to be called by the interupt +// handler of a data source to signal the run loop that a new data +// is available. void embedded_trigger(void); #endif #if defined __cplusplus diff --git a/src/hci.h b/src/hci.h index e6020df28..d613b4967 100644 --- a/src/hci.h +++ b/src/hci.h @@ -306,24 +306,14 @@ typedef struct { uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...); uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, const hci_cmd_t *cmd, va_list argptr); -// set up HCI -void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db); -void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); -void hci_close(void); - -// power and inquriy scan control -int hci_power_control(HCI_POWER_MODE mode); -void hci_discoverable_control(uint8_t enable); void hci_connectable_control(uint8_t enable); +void hci_close(void); /** * run the hci control loop once */ void hci_run(void); -// create and send hci command packets based on a template and a list of parameters -int hci_send_cmd(const hci_cmd_t *cmd, ...); - // send complete CMD packet int hci_send_cmd_packet(uint8_t *packet, int size); @@ -337,7 +327,6 @@ hci_connection_t * connection_for_handle(hci_con_handle_t con_handle); uint8_t hci_number_outgoing_packets(hci_con_handle_t handle); uint8_t hci_number_free_acl_slots(void); int hci_authentication_active_for_handle(hci_con_handle_t handle); -void hci_drop_link_key_for_bd_addr(bd_addr_t *addr); uint16_t hci_max_acl_data_packet_length(void); uint16_t hci_usable_acl_packet_types(void); uint8_t* hci_get_outgoing_acl_packet_buffer(void); @@ -354,6 +343,30 @@ void hci_emit_system_bluetooth_enabled(uint8_t enabled); void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name); void hci_emit_discoverable_enabled(uint8_t enabled); + +/** Embedded API **/ + +// Set up HCI. +void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db); + +// Registers a packet handler. Used if L2CAP is not used (rarely). +void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); + +// Requests the change of BTstack power mode. +int hci_power_control(HCI_POWER_MODE mode); + +// Allows to control if device is dicoverable. OFF by default. +void hci_discoverable_control(uint8_t enable); + +// Creates and sends hci command packets based on a template and +// a list of parameters. Will return error if outgoing data buffer +// is occupied. +int hci_send_cmd(const hci_cmd_t *cmd, ...); + +// Deletes link key for remote device with baseband address. +void hci_drop_link_key_for_bd_addr(bd_addr_t *addr); + + #if defined __cplusplus } #endif diff --git a/src/l2cap.h b/src/l2cap.h index a743b5c08..10224232e 100644 --- a/src/l2cap.h +++ b/src/l2cap.h @@ -82,38 +82,7 @@ extern "C" { // L2CAP Reject Result Codes #define L2CAP_REJ_CMD_UNKNOWN 0x0000 -void l2cap_init(void); -void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)); -void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu); -void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason); -uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid); -uint16_t l2cap_max_mtu(void); - -void l2cap_block_new_credits(uint8_t blocked); -int l2cap_can_send_packet_now(uint16_t local_cid); // non-blocking UART write - -// get outgoing buffer and prepare data -uint8_t *l2cap_get_outgoing_buffer(void); - -int l2cap_send_prepared(uint16_t local_cid, uint16_t len); -int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len); - -int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len); -int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len); - -void l2cap_close_connection(void *connection); - -void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu); -void l2cap_unregister_service_internal(void *connection, uint16_t psm); - -void l2cap_accept_connection_internal(uint16_t local_cid); -void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason); - -// Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol -void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id); - - -// private structs + // private structs typedef enum { L2CAP_STATE_CLOSED = 1, // no baseband L2CAP_STATE_WILL_SEND_CREATE_CONNECTION, @@ -205,7 +174,58 @@ typedef struct l2cap_signaling_response { uint16_t data; // infoType for INFORMATION REQUEST, result for CONNECTION request and command unknown } l2cap_signaling_response_t; - + +void l2cap_block_new_credits(uint8_t blocked); +int l2cap_can_send_packet_now(uint16_t local_cid); // non-blocking UART write + +// get outgoing buffer and prepare data +uint8_t *l2cap_get_outgoing_buffer(void); + +int l2cap_send_prepared(uint16_t local_cid, uint16_t len); + +int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len); + +// Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol +void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id); + +uint16_t l2cap_max_mtu(void); + +int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len); + +void l2cap_close_connection(void *connection); + + +/** Embedded API **/ + +// Set up L2CAP and register L2CAP with HCI layer. +void l2cap_init(void); + +// Registers a packet handler that handles HCI and general BTstack events. +void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)); + +// Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. +void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu); + +// Disconencts L2CAP channel with given identifier. +void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason); + +// Queries the maximal transfer unit (MTU) for L2CAP channel with given identifier. +uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid); + +// Sends L2CAP data packet to the channel with given identifier. +int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len); + +// Registers L2CAP service with given PSM and MTU, and assigns a packet handler. On embedded systems, use NULL for connection parameter. +void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu); + +// Unregisters L2CAP service with given PSM. On embedded systems, use NULL for connection parameter. +void l2cap_unregister_service_internal(void *connection, uint16_t psm); + +// Accepts/Deny incoming L2CAP connection. +void l2cap_accept_connection_internal(uint16_t local_cid); +void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason); + + #if defined __cplusplus } #endif diff --git a/src/rfcomm.h b/src/rfcomm.h index 207053f8c..193f208d8 100644 --- a/src/rfcomm.h +++ b/src/rfcomm.h @@ -47,26 +47,6 @@ extern "C" { #endif -void rfcomm_init(void); - -// register packet handler -void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); -void rfcomm_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, - uint16_t channel, uint8_t *packet, uint16_t size)); - -// BTstack Internal RFCOMM API -void rfcomm_create_channel_internal(void * connection, bd_addr_t *addr, uint8_t channel); -void rfcomm_create_channel_with_initial_credits_internal(void * connection, bd_addr_t *addr, uint8_t server_channel, uint8_t initial_credits); -void rfcomm_disconnect_internal(uint16_t rfcomm_cid); -void rfcomm_register_service_internal(void * connection, uint8_t channel, uint16_t max_frame_size); -void rfcomm_register_service_with_initial_credits_internal(void * connection, uint8_t channel, uint16_t max_frame_size, uint8_t initial_credits); -void rfcomm_unregister_service_internal(uint8_t service_channel); -void rfcomm_accept_connection_internal(uint16_t rfcomm_cid); -void rfcomm_decline_connection_internal(uint16_t rfcomm_cid); -void rfcomm_grant_credits(uint16_t rfcomm_cid, uint8_t credits); -int rfcomm_send_internal(uint16_t rfcomm_cid, uint8_t *data, uint16_t len); -void rfcomm_close_connection(void *connection); - #define UNLIMITED_INCOMING_CREDITS 0xff // private structs @@ -272,6 +252,49 @@ typedef struct { } rfcomm_channel_t; +void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); +void rfcomm_close_connection(void *connection); + +/** Embedded API **/ + +// Set up RFCOMM +void rfcomm_init(void); + +// register packet handler +void rfcomm_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, + uint16_t channel, uint8_t *packet, uint16_t size)); + +// Creates RFCOMM channel to a given server channel on a remote device with baseband address. A new baseband connection will be initiated if necessary. +// This channel will automatically provides enough credits to the remote side +void rfcomm_create_channel_internal(void * connection, bd_addr_t *addr, uint8_t channel); + +// Creates RFCOMM channel to a given server channel on a remote device with baseband address. new baseband connection will be initiated if necessary. +// This channel will use explicit credit management. During channel establishment, an initial amount of credits are provided. +void rfcomm_create_channel_with_initial_credits_internal(void * connection, bd_addr_t *addr, uint8_t server_channel, uint8_t initial_credits); + +// Disconencts RFCOMM channel with given identifier. +void rfcomm_disconnect_internal(uint16_t rfcomm_cid); + +// Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler. On embedded systems, use NULL for connection parameter. +// This channel will automatically provides enough credits to the remote side +void rfcomm_register_service_internal(void * connection, uint8_t channel, uint16_t max_frame_size); + +// Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler. On embedded systems, use NULL for connection parameter. +// This channel will use explicit credit management. During channel establishment, an initial amount of credits are provided. +void rfcomm_register_service_with_initial_credits_internal(void * connection, uint8_t channel, uint16_t max_frame_size, uint8_t initial_credits); + +// Unregister RFCOMM service +void rfcomm_unregister_service_internal(uint8_t service_channel); + +// Accepts/Deny incoming RFCOMM connection. +void rfcomm_accept_connection_internal(uint16_t rfcomm_cid); +void rfcomm_decline_connection_internal(uint16_t rfcomm_cid); + +// Grant more incoming credits to the remote side for the given channel ID +void rfcomm_grant_credits(uint16_t rfcomm_cid, uint8_t credits); + +// Sends RFCOMM data packet to the channel with given identifier. +int rfcomm_send_internal(uint16_t rfcomm_cid, uint8_t *data, uint16_t len); #if defined __cplusplus } diff --git a/src/sdp.h b/src/sdp.h index c2433c892..daac77387 100644 --- a/src/sdp.h +++ b/src/sdp.h @@ -65,17 +65,16 @@ typedef struct { } service_record_item_t; -void sdp_init(void); - void sdp_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)); -#ifdef EMBEDDED -// register service record internally - the normal version creates a copy of the record -// pre: AttributeIDs are in ascending order => ServiceRecordHandle is first attribute if present -// @returns ServiceRecordHandle or 0 if registration failed -uint32_t sdp_register_service_internal(void *connection, service_record_item_t * record_item); -#else + +// +int sdp_handle_service_search_request(uint8_t * packet, uint16_t remote_mtu); +int sdp_handle_service_attribute_request(uint8_t * packet, uint16_t remote_mtu); +int sdp_handle_service_search_attribute_request(uint8_t * packet, uint16_t remote_mtu); + +#ifndef EMBEDDED // register service record internally - this special version doesn't copy the record, it cannot be freeed // pre: AttributeIDs are in ascending order // pre: ServiceRecordHandle is first attribute and valid @@ -84,13 +83,20 @@ uint32_t sdp_register_service_internal(void *connection, service_record_item_t * uint32_t sdp_register_service_internal(void *connection, uint8_t * service_record); #endif -// unregister service record internally -void sdp_unregister_service_internal(void *connection, uint32_t service_record_handle); - // void sdp_unregister_services_for_connection(void *connection); -// -int sdp_handle_service_search_request(uint8_t * packet, uint16_t remote_mtu); -int sdp_handle_service_attribute_request(uint8_t * packet, uint16_t remote_mtu); -int sdp_handle_service_search_attribute_request(uint8_t * packet, uint16_t remote_mtu); +/** Embedded API **/ + +void sdp_init(void); + +#ifdef EMBEDDED +// register service record internally - the normal version creates a copy of the record +// pre: AttributeIDs are in ascending order => ServiceRecordHandle is first attribute if present +// @returns ServiceRecordHandle or 0 if registration failed +uint32_t sdp_register_service_internal(void *connection, service_record_item_t * record_item); +#endif + +// unregister service record internally +void sdp_unregister_service_internal(void *connection, uint32_t service_record_handle); +