From 2b552b2306fa072c814a621715edcba6eda58f3e Mon Sep 17 00:00:00 2001 From: "matthias.ringwald@gmail.com" Date: Sat, 3 May 2014 22:18:29 +0000 Subject: [PATCH] define daemon commands for gap le scan and le connect, define gap events --- example/libusb/ble_client.c | 4 +-- include/btstack/hci_cmds.h | 49 ++++++++++++++++++++++++++++-- src/hci.c | 10 +++--- src/hci.h | 13 ++++++++ src/hci_cmds.c | 57 ++++++++++++++++++++++++++++++++++- test/gatt_client/le_central.c | 2 +- 6 files changed, 123 insertions(+), 12 deletions(-) diff --git a/example/libusb/ble_client.c b/example/libusb/ble_client.c index 79c2db129..49c4d6998 100644 --- a/example/libusb/ble_client.c +++ b/example/libusb/ble_client.c @@ -773,7 +773,7 @@ static void handle_hci_event(uint8_t packet_type, uint8_t *packet, uint16_t size case HCI_EVENT_DISCONNECTION_COMPLETE: printf("test client - DISCONNECTED\n"); break; - case GATT_ADVERTISEMENT: + case GAP_LE_ADVERTISING_REPORT: if (tc_state != TC_W4_SCAN_RESULT) return; printf("test client - SCAN ACTIVE\n"); @@ -783,10 +783,10 @@ static void handle_hci_event(uint8_t packet_type, uint8_t *packet, uint16_t size ad_event.address_type = packet[pos++]; memcpy(ad_event.address, &packet[pos], 6); pos += 6; + ad_event.rssi = packet[pos++]; ad_event.length = packet[pos++]; ad_event.data = &packet[pos]; pos += ad_event.length; - ad_event.rssi = packet[pos]; dump_ad_event(&ad_event); test_device_addr_type = ad_event.address_type; diff --git a/include/btstack/hci_cmds.h b/include/btstack/hci_cmds.h index 2d93dfa80..35f22194f 100644 --- a/include/btstack/hci_cmds.h +++ b/include/btstack/hci_cmds.h @@ -35,6 +35,10 @@ * Created by Matthias Ringwald on 7/23/09. */ +/** + * @format + */ + #ifndef __HCI_CMDS_H #define __HCI_CMDS_H @@ -86,7 +90,15 @@ extern "C" { #define HCI_EVENT_INQUIRY_RESULT 0x02 #define HCI_EVENT_CONNECTION_COMPLETE 0x03 #define HCI_EVENT_CONNECTION_REQUEST 0x04 + +/** + * @format 121 + * @param status + * @param connection_handle + * @param reason + */ #define HCI_EVENT_DISCONNECTION_COMPLETE 0x05 + #define HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT 0x06 #define HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 0x07 #define HCI_EVENT_ENCRYPTION_CHANGE 0x08 @@ -121,6 +133,19 @@ extern "C" { #define HCI_EVENT_LE_META 0x3E #define HCI_EVENT_VENDOR_SPECIFIC 0xFF +/** + * @format 11211B2221 + * @param subevent_code + * @param status + * @param connection_handle + * @param role + * @param peer_address_type + * @param peer_address + * @param conn_interval + * @param conn_latency + * @param supervision_timeout + * @param master_clock_accuracy + */ #define HCI_SUBEVENT_LE_CONNECTION_COMPLETE 0x01 #define HCI_SUBEVENT_LE_ADVERTISING_REPORT 0x02 #define HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE 0x03 @@ -134,6 +159,11 @@ extern "C" { // BTSTACK DAEMON EVENTS // events from BTstack for application/client lib + +/** + * @format 1 + * @param state + */ #define BTSTACK_EVENT_STATE 0x60 // data: event(8), len(8), nr hci connections @@ -220,8 +250,15 @@ extern "C" { // not provided by daemon, only used for internal testing #define SDP_QUERY_SERVICE_RECORD_HANDLE 0x94 -// data: event(8), gatt subevent(8), address_type(8), address(6x8), rssi(8), len(8), data(len*8) -#define GATT_ADVERTISEMENT 0xA0 +/** + * @format 1B1LV + * @param address_type + * @param address + * @param rssi + * @param data_length + * @param data + */ +#define GAP_LE_ADVERTISING_REPORT 0xA0 #define GATT_CONNECTION_COMPLETE 0xA1 #define GATT_SERVICE_QUERY_RESULT 0xA2 @@ -521,6 +558,14 @@ extern const hci_cmd_t rfcomm_unregister_service; // request persisten rfcomm channel for service name: serive name (char*) extern const hci_cmd_t rfcomm_persistent_channel_for_service; +extern const hci_cmd_t gap_disconnect_cmd; +extern const hci_cmd_t gap_le_scan_start; +extern const hci_cmd_t gap_le_scan_stop; +extern const hci_cmd_t gap_le_connect_cmd; +extern const hci_cmd_t gatt_client_start_cmd; +extern const hci_cmd_t gatt_client_stop_cmd; +extern const hci_cmd_t gatt_discover_primary_services_cmd; + #if defined __cplusplus } #endif diff --git a/src/hci.c b/src/hci.c index a84b258b2..6a905b5a2 100644 --- a/src/hci.c +++ b/src/hci.c @@ -526,21 +526,19 @@ static void le_handle_advertisement_report(uint8_t *packet, int size){ for (i=0; ipacket_handler(HCI_EVENT_PACKET, event, sizeof(event)); } } diff --git a/src/hci.h b/src/hci.h index fb03f4e45..45247397e 100644 --- a/src/hci.h +++ b/src/hci.h @@ -185,6 +185,19 @@ extern "C" { #define RFCOMM_REGISTER_SERVICE_WITH_CREDITS 0x48 #define RFCOMM_GRANT_CREDITS 0x49 +// GAP Classic 0x50 +#define GAP_DISCONNECT 0x50 + +// GAP LE 0x60 +#define GAP_LE_SCAN_START 0x60 +#define GAP_LE_SCAN_STOP 0x61 +#define GAP_LE_CONNECT 0x62 + +// GATT (Client) 0x70 +#define GATT_START 0x70 +#define GATT_STOP 0x71 +#define GATT_DISCOVER_ALL_PRIMARY_SERVICES 0x72 + // #define IS_COMMAND(packet, command) (READ_BT_16(packet,0) == command.opcode) diff --git a/src/hci_cmds.c b/src/hci_cmds.c index 4e16e2f91..bc35b5579 100644 --- a/src/hci_cmds.c +++ b/src/hci_cmds.c @@ -606,6 +606,9 @@ OPCODE(OGF_BTSTACK, BTSTACK_GET_STATE), "" // no params -> }; +/** + * @param power_mode + */ const hci_cmd_t btstack_set_power_mode = { OPCODE(OGF_BTSTACK, BTSTACK_SET_POWER_MODE), "1" // mode: 0 = off, 1 = on @@ -723,10 +726,62 @@ const hci_cmd_t rfcomm_accept_connection = { const hci_cmd_t rfcomm_decline_connection = { OPCODE(OGF_BTSTACK, RFCOMM_DECLINE_CONNECTION), "21" }; -// request persisten rfcomm channel number for named service +// request persistent rfcomm channel number for named service const hci_cmd_t rfcomm_persistent_channel_for_service = { OPCODE(OGF_BTSTACK, RFCOMM_PERSISTENT_CHANNEL), "N" }; +/** + * @param handle + */ +const hci_cmd_t gap_disconnect_cmd = { + OPCODE(OGF_BTSTACK, GAP_DISCONNECT), "H" +}; + +/** + */ +const hci_cmd_t gap_le_scan_start = { + OPCODE(OGF_BTSTACK, GAP_LE_SCAN_START), "" +}; + +/** + */ +const hci_cmd_t gap_le_scan_stop = { + OPCODE(OGF_BTSTACK, GAP_LE_SCAN_STOP), "" +}; + +/** + * @param peer_address_type + * @param peer_address + */ +const hci_cmd_t gap_le_connect_cmd = { + OPCODE(OGF_BTSTACK, GAP_LE_CONNECT), "1B" +}; + +/** + * @param handle + */ +const hci_cmd_t gatt_client_start_cmd = { + OPCODE(OGF_BTSTACK, GAP_LE_CONNECT), "H" +}; + +/** + * @param handle + */ +const hci_cmd_t gatt_client_stop_cmd = { + OPCODE(OGF_BTSTACK, GAP_LE_CONNECT), "H" +}; + +/** + * @param handle + */ +const hci_cmd_t gatt_discover_primary_services_cmd = { + OPCODE(OGF_BTSTACK, GAP_LE_CONNECT), "H" +}; + + +// GATT Client 0x70 +#define GATT_DISCOVER_ALL_PRIMARY_SERVICES 0x70 + // register rfcomm service: @param channel(8), mtu (16), initial credits (8) extern const hci_cmd_t rfcomm_register_service_with_initial_credits; diff --git a/test/gatt_client/le_central.c b/test/gatt_client/le_central.c index dfbc85419..f4ec18023 100644 --- a/test/gatt_client/le_central.c +++ b/test/gatt_client/le_central.c @@ -50,7 +50,7 @@ static void verify_advertisement(ad_event_t * e){ static void handle_ble_client_event(le_event_t * event){ switch(event->type){ - case GATT_ADVERTISEMENT: + case GAP_LE_ADVERTISING_REPORT: advertisement_received = 1; verify_advertisement((ad_event_t *) event); break;