| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- /*
- * Copyright (C) 2014 BlueKitchen GmbH
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the copyright holders nor the names of
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * 4. Any redistribution, use, or modification is done solely for
- * personal benefit and not for any commercial purpose or for
- * monetary gain.
- *
- * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
- * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
- * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * Please inquire about commercial licensing options at
- * contact@bluekitchen-gmbh.com
- *
- */
- #define BTSTACK_FILE__ "spp_and_gatt_counter.c"
- // *****************************************************************************
- /* EXAMPLE_START(spp_and_le_counter): Dual mode example
- *
- * @text The SPP and LE Counter example combines the Bluetooth Classic SPP Counter
- * and the Bluetooth LE Counter into a single application.
- *
- * @text In this Section, we only point out the differences to the individual examples
- * and how the stack is configured.
- *
- * @text Note: To test, please run the example, and then:
- * - for SPP pair from a remote device, and open the Virtual Serial Port,
- * - for LE use some GATT Explorer, e.g. LightBlue, BLExplr, to enable notifications.
- */
- // *****************************************************************************
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <inttypes.h>
-
- #include "btstack.h"
- #include "spp_and_gatt_counter.h"
- #define RFCOMM_SERVER_CHANNEL 1
- #define HEARTBEAT_PERIOD_MS 1000
- static uint16_t rfcomm_channel_id;
- static uint8_t spp_service_buffer[150];
- static int le_notification_enabled;
- static hci_con_handle_t att_con_handle;
- // THE Couner
- static btstack_timer_source_t heartbeat;
- static int counter = 0;
- static char counter_string[30];
- static int counter_string_len;
- static btstack_packet_callback_registration_t hci_event_callback_registration;
- #ifdef ENABLE_GATT_OVER_CLASSIC
- static uint8_t gatt_service_buffer[70];
- #endif
- /*
- * @section Advertisements
- *
- * @text The Flags attribute in the Advertisement Data indicates if a device is in dual-mode or not.
- * Flag 0x06 indicates LE General Discoverable, BR/EDR not supported although we're actually using BR/EDR.
- * In the past, there have been problems with Anrdoid devices when the flag was not set.
- * Setting it should prevent the remote implementation to try to use GATT over LE/EDR, which is not
- * implemented by BTstack. So, setting the flag seems like the safer choice (while it's technically incorrect).
- */
- /* LISTING_START(advertisements): Advertisement data: Flag 0x06 indicates LE-only device */
- const uint8_t adv_data[] = {
- // Flags general discoverable, BR/EDR not supported
- 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
- // Name
- 0x0b, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'L', 'E', ' ', 'C', 'o', 'u', 'n', 't', 'e', 'r',
- // Incomplete List of 16-bit Service Class UUIDs -- FF10 - only valid for testing!
- 0x03, BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 0x10, 0xff,
- };
- /* LISTING_END */
- uint8_t adv_data_len = sizeof(adv_data);
- /*
- * @section Packet Handler
- *
- * @text The packet handler of the combined example is just the combination of the individual packet handlers.
- */
- static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
- UNUSED(channel);
- bd_addr_t event_addr;
- uint8_t rfcomm_channel_nr;
- uint16_t mtu;
- int i;
- switch (packet_type) {
- case HCI_EVENT_PACKET:
- switch (hci_event_packet_get_type(packet)) {
- case HCI_EVENT_PIN_CODE_REQUEST:
- // inform about pin code request
- printf("Pin code request - using '0000'\n");
- hci_event_pin_code_request_get_bd_addr(packet, event_addr);
- gap_pin_code_response(event_addr, "0000");
- break;
- case HCI_EVENT_USER_CONFIRMATION_REQUEST:
- // inform about user confirmation request
- printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8));
- printf("SSP User Confirmation Auto accept\n");
- break;
- case HCI_EVENT_DISCONNECTION_COMPLETE:
- le_notification_enabled = 0;
- break;
- case ATT_EVENT_CAN_SEND_NOW:
- att_server_notify(att_con_handle, ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t*) counter_string, counter_string_len);
- break;
- case RFCOMM_EVENT_INCOMING_CONNECTION:
- // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
- rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
- rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
- rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
- printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
- rfcomm_accept_connection(rfcomm_channel_id);
- break;
-
- case RFCOMM_EVENT_CHANNEL_OPENED:
- // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
- if (rfcomm_event_channel_opened_get_status(packet)) {
- printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet));
- } else {
- rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
- mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
- printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
- }
- break;
- case RFCOMM_EVENT_CAN_SEND_NOW:
- rfcomm_send(rfcomm_channel_id, (uint8_t*) counter_string, counter_string_len);
- break;
- case RFCOMM_EVENT_CHANNEL_CLOSED:
- printf("RFCOMM channel closed\n");
- rfcomm_channel_id = 0;
- break;
-
- default:
- break;
- }
- break;
-
- case RFCOMM_DATA_PACKET:
- printf("RCV: '");
- for (i=0;i<size;i++){
- putchar(packet[i]);
- }
- printf("'\n");
- break;
- default:
- break;
- }
- }
- // ATT Client Read Callback for Dynamic Data
- // - if buffer == NULL, don't copy data, just return size of value
- // - if buffer != NULL, copy data and return number bytes copied
- // @param offset defines start of attribute value
- static uint16_t att_read_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
- UNUSED(con_handle);
- if (att_handle == ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE){
- return att_read_callback_handle_blob((const uint8_t *)counter_string, counter_string_len, offset, buffer, buffer_size);
- }
- return 0;
- }
- // write requests
- static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
- // ignore cancel sent for new connections
- if (transaction_mode == ATT_TRANSACTION_MODE_CANCEL) return 0;
- // find characteristic for handle
- switch (att_handle){
- case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
- le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
- att_con_handle = con_handle;
- return 0;
- case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE:
- printf("Write on test characteristic: ");
- printf_hexdump(buffer, buffer_size);
- return 0;
- default:
- printf("WRITE Callback, handle %04x, mode %u, offset %u, data: ", con_handle, transaction_mode, offset);
- printf_hexdump(buffer, buffer_size);
- return 0;
- }
- }
- static void beat(void){
- counter++;
- counter_string_len = sprintf(counter_string, "BTstack counter %04u", counter);
- puts(counter_string);
- }
- /*
- * @section Heartbeat Handler
- *
- * @text Similar to the packet handler, the heartbeat handler is the combination of the individual ones.
- * After updating the counter, it requests an ATT_EVENT_CAN_SEND_NOW and/or RFCOMM_EVENT_CAN_SEND_NOW
- */
- /* LISTING_START(heartbeat): Combined Heartbeat handler */
- static void heartbeat_handler(struct btstack_timer_source *ts){
- if (rfcomm_channel_id || le_notification_enabled) {
- beat();
- }
- if (rfcomm_channel_id){
- rfcomm_request_can_send_now_event(rfcomm_channel_id);
- }
- if (le_notification_enabled) {
- att_server_request_can_send_now_event(att_con_handle);
- }
- btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
- btstack_run_loop_add_timer(ts);
- }
- /* LISTING_END */
- /*
- * @section Main Application Setup
- *
- * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups.
- */
- /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */
- int btstack_main(void);
- int btstack_main(void)
- {
- l2cap_init();
- rfcomm_init();
- rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff);
- // init SDP, create record for SPP and register with SDP
- sdp_init();
- memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
- spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Counter");
- sdp_register_service(spp_service_buffer);
- printf("SDP service record size: %u\n", de_get_len(spp_service_buffer));
- #ifdef ENABLE_GATT_OVER_CLASSIC
- // init SDP, create record for GATT and register with SDP
- memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer));
- gatt_create_sdp_record(gatt_service_buffer, 0x10001, ATT_SERVICE_GATT_SERVICE_START_HANDLE, ATT_SERVICE_GATT_SERVICE_END_HANDLE);
- sdp_register_service(gatt_service_buffer);
- printf("SDP service record size: %u\n", de_get_len(gatt_service_buffer));
- #endif
- gap_set_local_name("SPP and LE Counter 00:00:00:00:00:00");
- gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
- gap_discoverable_control(1);
- // setup le device db
- le_device_db_init();
- // setup SM: Display only
- sm_init();
- // setup ATT server
- att_server_init(profile_data, att_read_callback, att_write_callback);
- // register for HCI events
- hci_event_callback_registration.callback = &packet_handler;
- hci_add_event_handler(&hci_event_callback_registration);
- // register for ATT events
- att_server_register_packet_handler(packet_handler);
- // setup advertisements
- uint16_t adv_int_min = 0x0030;
- uint16_t adv_int_max = 0x0030;
- uint8_t adv_type = 0;
- bd_addr_t null_addr;
- memset(null_addr, 0, 6);
- gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
- gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
- gap_advertisements_enable(1);
- // set one-shot timer
- heartbeat.process = &heartbeat_handler;
- btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
- btstack_run_loop_add_timer(&heartbeat);
- // beat once
- beat();
- // turn on!
- hci_power_control(HCI_POWER_ON);
-
- return 0;
- }
- /* LISTING_END */
- /* EXAMPLE_END */
|