From 423c667c13e0fb5d595b3640a5d1a44fbdf8d998 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Fri, 1 Apr 2016 11:41:58 +0200 Subject: [PATCH] bnep: add bnep_cid to all events, generate bnep event getters, BNEP_EVENT_OPEN_CHANNEL_COMPLETE -> BNEP_EVENT_CHANNEL_OPENED --- doc/manual/docs/protocols.md | 2 +- example/panu_demo.c | 29 ++-- src/btstack_defines.h | 14 +- src/btstack_event.h | 194 +++++++++++++++++++++++++++ src/classic/bnep.c | 46 ++++--- test/pts/bnep_test.c | 2 +- tool/btstack_event_generator.py | 2 +- tool/migration_to_v1.0/migration.sed | 1 + 8 files changed, 246 insertions(+), 44 deletions(-) diff --git a/doc/manual/docs/protocols.md b/doc/manual/docs/protocols.md index 69e907279..48eb0613a 100644 --- a/doc/manual/docs/protocols.md +++ b/doc/manual/docs/protocols.md @@ -695,7 +695,7 @@ To connect to a remote BNEP service, you need to know its UUID. The set of available UUIDs can be queried by a SDP query for the PAN profile. Please see section on [PAN profile](profiles/#sec:panProfiles) for details. With the remote UUID, you can create a connection using the *bnep_connect* -function. You’ll receive a *BNEP_EVENT_OPEN_CHANNEL_COMPLETE* on success or +function. You’ll receive a *BNEP_EVENT_CHANNEL_OPENED* on success or failure. After the connection was opened successfully, you can send and receive diff --git a/example/panu_demo.c b/example/panu_demo.c index 18500b8da..4a655581d 100644 --- a/example/panu_demo.c +++ b/example/panu_demo.c @@ -133,7 +133,7 @@ static btstack_packet_callback_registration_t hci_event_callback_registration; /* LISTING_START(PanuSetup): Panu setup */ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); -static void handle_sdp_client_query_result(uint8_t packet_type, uint8_t *packet, uint16_t size); +static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); static void panu_setup(void){ @@ -268,13 +268,13 @@ static int tap_alloc(char *dev, bd_addr_t bd_addr) */ /* LISTING_START(processTapData): Process incoming network packets */ -static int process_tap_dev_data(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) +static void process_tap_dev_data(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) { ssize_t len; len = read(ds->fd, network_buffer, sizeof(network_buffer)); if (len <= 0){ fprintf(stderr, "TAP: Error while reading: %s\n", strerror(errno)); - return 0; + return; } network_buffer_len = len; @@ -285,7 +285,7 @@ static int process_tap_dev_data(btstack_data_source_t *ds, btstack_data_source_c // park the current network packet btstack_run_loop_remove_data_source(&tap_dev_ds); } - return 0; + return; } /* LISTING_END */ @@ -316,7 +316,7 @@ static char * get_string_from_data_element(uint8_t * element){ * @text The SDP parsers retrieves the BNEP PAN UUID as explained in * Section [on SDP BNEP Query example](#sec:sdpbnepqueryExample}. */ -static void handle_sdp_client_query_result(uint8_t packet_type, uint8_t *packet, uint16_t size) { +static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { des_iterator_t des_list_it; des_iterator_t prot_it; @@ -444,7 +444,7 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack case BTSTACK_EVENT_STATE: if (packet[2] == HCI_STATE_WORKING) { printf("Start SDP BNEP query.\n"); - sdp_client_query_uuid16(remote, SDP_BNEPProtocol); + sdp_client_query_uuid16(&handle_sdp_client_query_result, remote, SDP_BNEPProtocol); } break; @@ -464,7 +464,7 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack /* LISTING_RESUME */ - /* @text BNEP_EVENT_OPEN_CHANNEL_COMPLETE is received after a BNEP connection was established or + /* @text BNEP_EVENT_CHANNEL_OPENED is received after a BNEP connection was established or * or when the connection fails. The status field returns the error code. * * The TAP network interface is then configured. A data source is set up and registered with the @@ -473,17 +473,16 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack * The event contains both the source and destination UUIDs, as well as the MTU for this connection and * the BNEP Channel ID, which is used for sending Ethernet packets over BNEP. */ - case BNEP_EVENT_OPEN_CHANNEL_COMPLETE: + case BNEP_EVENT_CHANNEL_OPENED: if (packet[2]) { printf("BNEP channel open failed, status %02x\n", packet[2]); } else { - // data: event(8), len(8), status (8), bnep source uuid (16), bnep destination uuid (16), remote_address (48) - uuid_source = little_endian_read_16(packet, 3); - uuid_dest = little_endian_read_16(packet, 5); - mtu = little_endian_read_16(packet, 7); - bnep_cid = channel; + bnep_cid = little_endian_read_16(packet, 3); + uuid_source = little_endian_read_16(packet, 5); + uuid_dest = little_endian_read_16(packet, 7); + mtu = little_endian_read_16(packet, 9); //bt_flip_addr(event_addr, &packet[9]); - memcpy(&event_addr, &packet[9], sizeof(bd_addr_t)); + memcpy(&event_addr, &packet[11], sizeof(bd_addr_t)); printf("BNEP connection open succeeded to %s source UUID 0x%04x dest UUID: 0x%04x, max frame size %u\n", bd_addr_to_str(event_addr), uuid_source, uuid_dest, mtu); /* Create the tap interface */ gap_local_bd_addr(local_addr); @@ -494,7 +493,7 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack printf("BNEP device \"%s\" allocated.\n", tap_dev_name); /* Create and register a new runloop data source */ btstack_run_loop_set_data_source_fd(&tap_dev_ds, tap_fd); - btstack_run_loop_set_data_source_handler(&tap_dev_ds, process_tap_dev_data); + btstack_run_loop_set_data_source_handler(&tap_dev_ds, &process_tap_dev_data); btstack_run_loop_add_data_source(&tap_dev_ds); } } diff --git a/src/btstack_defines.h b/src/btstack_defines.h index 1a0467f1a..1b72a4bbb 100644 --- a/src/btstack_defines.h +++ b/src/btstack_defines.h @@ -646,17 +646,19 @@ typedef uint8_t sm_key_t[16]; #define BNEP_EVENT_SERVICE_REGISTERED 0xC0 /** - * @format 1222B + * @format 12222B * @param status + * @param bnep_cid * @param source_uuid * @param destination_uuid * @param mtu * @param remote_address */ - #define BNEP_EVENT_OPEN_CHANNEL_COMPLETE 0xC1 + #define BNEP_EVENT_CHANNEL_OPENED 0xC1 /** - * @format 22B + * @format 222B + * @param bnep_cid * @param source_uuid * @param destination_uuid * @param remote_address @@ -664,7 +666,8 @@ typedef uint8_t sm_key_t[16]; #define BNEP_EVENT_CHANNEL_CLOSED 0xC2 /** - * @format 22B1 + * @format 222B1 + * @param bnep_cid * @param source_uuid * @param destination_uuid * @param remote_address @@ -673,7 +676,8 @@ typedef uint8_t sm_key_t[16]; #define BNEP_EVENT_CHANNEL_TIMEOUT 0xC3 /** - * @format 22B + * @format 222B + * @param bnep_cid * @param source_uuid * @param destination_uuid * @param remote_address diff --git a/src/btstack_event.h b/src/btstack_event.h index 5c37da4bd..88ee486ca 100644 --- a/src/btstack_event.h +++ b/src/btstack_event.h @@ -1703,6 +1703,200 @@ static inline uint16_t gatt_event_mtu_get_MTU(const uint8_t * event){ } #endif +/** + * @brief Get field status from event bnep_event_service_registered + * @param event packet + * @return status + * @note: btstack_type 1 + */ +static inline uint8_t bnep_event_service_registered_get_status(const uint8_t * event){ + return event[2]; +} +/** + * @brief Get field service_uuid from event bnep_event_service_registered + * @param event packet + * @return service_uuid + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_service_registered_get_service_uuid(const uint8_t * event){ + return little_endian_read_16(event, 3); +} + +/** + * @brief Get field status from event bnep_event_channel_opened + * @param event packet + * @return status + * @note: btstack_type 1 + */ +static inline uint8_t bnep_event_channel_opened_get_status(const uint8_t * event){ + return event[2]; +} +/** + * @brief Get field bnep_cid from event bnep_event_channel_opened + * @param event packet + * @return bnep_cid + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_channel_opened_get_bnep_cid(const uint8_t * event){ + return little_endian_read_16(event, 3); +} +/** + * @brief Get field source_uuid from event bnep_event_channel_opened + * @param event packet + * @return source_uuid + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_channel_opened_get_source_uuid(const uint8_t * event){ + return little_endian_read_16(event, 5); +} +/** + * @brief Get field destination_uuid from event bnep_event_channel_opened + * @param event packet + * @return destination_uuid + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_channel_opened_get_destination_uuid(const uint8_t * event){ + return little_endian_read_16(event, 7); +} +/** + * @brief Get field mtu from event bnep_event_channel_opened + * @param event packet + * @return mtu + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_channel_opened_get_mtu(const uint8_t * event){ + return little_endian_read_16(event, 9); +} +/** + * @brief Get field remote_address from event bnep_event_channel_opened + * @param event packet + * @param Pointer to storage for remote_address + * @note: btstack_type B + */ +static inline void bnep_event_channel_opened_get_remote_address(const uint8_t * event, bd_addr_t remote_address){ + reverse_bd_addr(&event[11], remote_address); +} + +/** + * @brief Get field bnep_cid from event bnep_event_channel_closed + * @param event packet + * @return bnep_cid + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_channel_closed_get_bnep_cid(const uint8_t * event){ + return little_endian_read_16(event, 2); +} +/** + * @brief Get field source_uuid from event bnep_event_channel_closed + * @param event packet + * @return source_uuid + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_channel_closed_get_source_uuid(const uint8_t * event){ + return little_endian_read_16(event, 4); +} +/** + * @brief Get field destination_uuid from event bnep_event_channel_closed + * @param event packet + * @return destination_uuid + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_channel_closed_get_destination_uuid(const uint8_t * event){ + return little_endian_read_16(event, 6); +} +/** + * @brief Get field remote_address from event bnep_event_channel_closed + * @param event packet + * @param Pointer to storage for remote_address + * @note: btstack_type B + */ +static inline void bnep_event_channel_closed_get_remote_address(const uint8_t * event, bd_addr_t remote_address){ + reverse_bd_addr(&event[8], remote_address); +} + +/** + * @brief Get field bnep_cid from event bnep_event_channel_timeout + * @param event packet + * @return bnep_cid + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_channel_timeout_get_bnep_cid(const uint8_t * event){ + return little_endian_read_16(event, 2); +} +/** + * @brief Get field source_uuid from event bnep_event_channel_timeout + * @param event packet + * @return source_uuid + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_channel_timeout_get_source_uuid(const uint8_t * event){ + return little_endian_read_16(event, 4); +} +/** + * @brief Get field destination_uuid from event bnep_event_channel_timeout + * @param event packet + * @return destination_uuid + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_channel_timeout_get_destination_uuid(const uint8_t * event){ + return little_endian_read_16(event, 6); +} +/** + * @brief Get field remote_address from event bnep_event_channel_timeout + * @param event packet + * @param Pointer to storage for remote_address + * @note: btstack_type B + */ +static inline void bnep_event_channel_timeout_get_remote_address(const uint8_t * event, bd_addr_t remote_address){ + reverse_bd_addr(&event[8], remote_address); +} +/** + * @brief Get field channel_state from event bnep_event_channel_timeout + * @param event packet + * @return channel_state + * @note: btstack_type 1 + */ +static inline uint8_t bnep_event_channel_timeout_get_channel_state(const uint8_t * event){ + return event[14]; +} + +/** + * @brief Get field bnep_cid from event bnep_event_can_send_now + * @param event packet + * @return bnep_cid + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_can_send_now_get_bnep_cid(const uint8_t * event){ + return little_endian_read_16(event, 2); +} +/** + * @brief Get field source_uuid from event bnep_event_can_send_now + * @param event packet + * @return source_uuid + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_can_send_now_get_source_uuid(const uint8_t * event){ + return little_endian_read_16(event, 4); +} +/** + * @brief Get field destination_uuid from event bnep_event_can_send_now + * @param event packet + * @return destination_uuid + * @note: btstack_type 2 + */ +static inline uint16_t bnep_event_can_send_now_get_destination_uuid(const uint8_t * event){ + return little_endian_read_16(event, 6); +} +/** + * @brief Get field remote_address from event bnep_event_can_send_now + * @param event packet + * @param Pointer to storage for remote_address + * @note: btstack_type B + */ +static inline void bnep_event_can_send_now_get_remote_address(const uint8_t * event, bd_addr_t remote_address){ + reverse_bd_addr(&event[8], remote_address); +} + #ifdef ENABLE_BLE /** * @brief Get field handle from event sm_event_just_works_request diff --git a/src/classic/bnep.c b/src/classic/bnep.c index 5ad169cee..8bee860d1 100644 --- a/src/classic/bnep.c +++ b/src/classic/bnep.c @@ -77,53 +77,57 @@ inline static void bnep_channel_state_add(bnep_channel_t *channel, BNEP_CHANNEL_ static void bnep_emit_open_channel_complete(bnep_channel_t *channel, uint8_t status) { - log_info("BNEP_EVENT_OPEN_CHANNEL_COMPLETE status 0x%02x bd_addr: %s", status, bd_addr_to_str(channel->remote_addr)); - uint8_t event[3 + sizeof(bd_addr_t) + 3 * sizeof(uint16_t)]; - event[0] = BNEP_EVENT_OPEN_CHANNEL_COMPLETE; + log_info("BNEP_EVENT_CHANNEL_OPENED status 0x%02x bd_addr: %s", status, bd_addr_to_str(channel->remote_addr)); + uint8_t event[3 + sizeof(bd_addr_t) + 4 * sizeof(uint16_t)]; + event[0] = BNEP_EVENT_CHANNEL_OPENED; event[1] = sizeof(event) - 2; event[2] = status; - little_endian_store_16(event, 3, channel->uuid_source); - little_endian_store_16(event, 5, channel->uuid_dest); - little_endian_store_16(event, 7, channel->max_frame_size); - bd_addr_copy(&event[9], channel->remote_addr); + little_endian_store_16(event, 3, channel->l2cap_cid); + little_endian_store_16(event, 5, channel->uuid_source); + little_endian_store_16(event, 7, channel->uuid_dest); + little_endian_store_16(event, 9, channel->max_frame_size); + bd_addr_copy(&event[11], channel->remote_addr); hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); - (*app_packet_handler)(HCI_EVENT_PACKET, channel->l2cap_cid, (uint8_t *) event, sizeof(event)); + (*app_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event)); } static void bnep_emit_channel_timeout(bnep_channel_t *channel) { log_info("BNEP_EVENT_CHANNEL_TIMEOUT bd_addr: %s", bd_addr_to_str(channel->remote_addr)); - uint8_t event[2 + sizeof(bd_addr_t) + 2 * sizeof(uint16_t) + sizeof(uint8_t)]; + uint8_t event[2 + sizeof(bd_addr_t) + 3 * sizeof(uint16_t) + sizeof(uint8_t)]; event[0] = BNEP_EVENT_CHANNEL_TIMEOUT; event[1] = sizeof(event) - 2; - little_endian_store_16(event, 2, channel->uuid_source); - little_endian_store_16(event, 4, channel->uuid_dest); - bd_addr_copy(&event[6], channel->remote_addr); - event[12] = channel->state; + little_endian_store_16(event, 2, channel->l2cap_cid); + little_endian_store_16(event, 4, channel->uuid_source); + little_endian_store_16(event, 6, channel->uuid_dest); + bd_addr_copy(&event[8], channel->remote_addr); + event[14] = channel->state; hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); - (*app_packet_handler)(HCI_EVENT_PACKET, channel->l2cap_cid, (uint8_t *) event, sizeof(event)); + (*app_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event)); } static void bnep_emit_channel_closed(bnep_channel_t *channel) { log_info("BNEP_EVENT_CHANNEL_CLOSED bd_addr: %s", bd_addr_to_str(channel->remote_addr)); - uint8_t event[2 + sizeof(bd_addr_t) + 2 * sizeof(uint16_t)]; + uint8_t event[2 + sizeof(bd_addr_t) + 3 * sizeof(uint16_t)]; event[0] = BNEP_EVENT_CHANNEL_CLOSED; event[1] = sizeof(event) - 2; - little_endian_store_16(event, 2, channel->uuid_source); - little_endian_store_16(event, 4, channel->uuid_dest); - bd_addr_copy(&event[6], channel->remote_addr); + little_endian_store_16(event, 2, channel->l2cap_cid); + little_endian_store_16(event, 4, channel->uuid_source); + little_endian_store_16(event, 6, channel->uuid_dest); + bd_addr_copy(&event[8], channel->remote_addr); hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); - (*app_packet_handler)(HCI_EVENT_PACKET, channel->l2cap_cid, (uint8_t *) event, sizeof(event)); + (*app_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event)); } static void bnep_emit_ready_to_send(bnep_channel_t *channel) { - uint8_t event[2]; + uint8_t event[4]; event[0] = BNEP_EVENT_CAN_SEND_NOW; event[1] = sizeof(event) - 2; + little_endian_store_16(event, 2, channel->l2cap_cid); hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); - (*app_packet_handler)(HCI_EVENT_PACKET, channel->l2cap_cid, (uint8_t *) event, sizeof(event)); + (*app_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event)); } /* Send BNEP connection request */ diff --git a/test/pts/bnep_test.c b/test/pts/bnep_test.c index fac953c88..470168637 100644 --- a/test/pts/bnep_test.c +++ b/test/pts/bnep_test.c @@ -702,7 +702,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe printf("SSP User Confirmation Auto accept\n"); break; - case BNEP_EVENT_OPEN_CHANNEL_COMPLETE: + case BNEP_EVENT_CHANNEL_OPENED: if (packet[2]) { printf("BNEP channel open failed, status %02x\n", packet[2]); } else { diff --git a/tool/btstack_event_generator.py b/tool/btstack_event_generator.py index d065bcec0..936b4478a 100755 --- a/tool/btstack_event_generator.py +++ b/tool/btstack_event_generator.py @@ -237,7 +237,7 @@ def create_events(events): for event_type, event_name, format, args in events: parts = event_name.split("_") event_group = parts[0] - if not event_group in [ 'BTSTACK', 'GAP', 'HCI', 'HSP', 'HFP', 'SDP', 'ANCS', 'SM', 'L2CAP', 'RFCOMM', 'GATT']: + if not event_group in [ 'BTSTACK', 'GAP', 'HCI', 'HSP', 'HFP', 'SDP', 'ANCS', 'SM', 'L2CAP', 'RFCOMM', 'GATT', 'BNEP']: print("// %s " % event_name) continue print(event_name) diff --git a/tool/migration_to_v1.0/migration.sed b/tool/migration_to_v1.0/migration.sed index d24b85266..b4af473d4 100644 --- a/tool/migration_to_v1.0/migration.sed +++ b/tool/migration_to_v1.0/migration.sed @@ -6,6 +6,7 @@ s/ANCS_CLIENT_DISCONNECTED/ANCS_EVENT_CLIENT_DISCONNECTED/g s/ANCS_CLIENT_NOTIFICATION/ANCS_EVENT_CLIENT_NOTIFICATION/g s/ATT_HANDLE_VALUE_INDICATION_COMPLETE/ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE/g s/ATT_MTU_EXCHANGE_COMPLETE/ATT_EVENT_MTU_EXCHANGE_COMPLETE/g +s/BNEP_EVENT_OPEN_CHANNEL_COMPLETE/BNEP_EVENT_CHANNEL_OPENED/g s/BTSTACK_EVENT_REMOTE_NAME_CACHED/DAEMON_EVENT_REMOTE_NAME_CACHED/g s/COMMAND_COMPLETE_EVENT/HCI_EVENT_IS_COMMAND_COMPLETE/g s/COMMAND_STATUS_EVENT/HCI_EVENT_IS_COMMAND_STATUS/g