1ebdf3c68SMilanka Ringwald /*
2ebdf3c68SMilanka Ringwald * Copyright (C) 2018 BlueKitchen GmbH
3ebdf3c68SMilanka Ringwald *
4ebdf3c68SMilanka Ringwald * Redistribution and use in source and binary forms, with or without
5ebdf3c68SMilanka Ringwald * modification, are permitted provided that the following conditions
6ebdf3c68SMilanka Ringwald * are met:
7ebdf3c68SMilanka Ringwald *
8ebdf3c68SMilanka Ringwald * 1. Redistributions of source code must retain the above copyright
9ebdf3c68SMilanka Ringwald * notice, this list of conditions and the following disclaimer.
10ebdf3c68SMilanka Ringwald * 2. Redistributions in binary form must reproduce the above copyright
11ebdf3c68SMilanka Ringwald * notice, this list of conditions and the following disclaimer in the
12ebdf3c68SMilanka Ringwald * documentation and/or other materials provided with the distribution.
13ebdf3c68SMilanka Ringwald * 3. Neither the name of the copyright holders nor the names of
14ebdf3c68SMilanka Ringwald * contributors may be used to endorse or promote products derived
15ebdf3c68SMilanka Ringwald * from this software without specific prior written permission.
16ebdf3c68SMilanka Ringwald * 4. Any redistribution, use, or modification is done solely for
17ebdf3c68SMilanka Ringwald * personal benefit and not for any commercial purpose or for
18ebdf3c68SMilanka Ringwald * monetary gain.
19ebdf3c68SMilanka Ringwald *
20ebdf3c68SMilanka Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21ebdf3c68SMilanka Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22ebdf3c68SMilanka Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25ebdf3c68SMilanka Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26ebdf3c68SMilanka Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27ebdf3c68SMilanka Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28ebdf3c68SMilanka Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29ebdf3c68SMilanka Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30ebdf3c68SMilanka Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ebdf3c68SMilanka Ringwald * SUCH DAMAGE.
32ebdf3c68SMilanka Ringwald *
33ebdf3c68SMilanka Ringwald * Please inquire about commercial licensing options at
34ebdf3c68SMilanka Ringwald * [email protected]
35ebdf3c68SMilanka Ringwald *
36ebdf3c68SMilanka Ringwald */
37ebdf3c68SMilanka Ringwald
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "nordic_spp_service_server.c"
39ebdf3c68SMilanka Ringwald
40ebdf3c68SMilanka Ringwald /**
41ebdf3c68SMilanka Ringwald * Implementation of the Nordic SPP-like profile
42ebdf3c68SMilanka Ringwald *
4367b65e8fSChristian Erhardt * To use with your application, add `#import <nordic_spp_service.gatt>` to your .gatt file
44ebdf3c68SMilanka Ringwald * and call all functions below. All strings and blobs need to stay valid after calling the functions.
45ebdf3c68SMilanka Ringwald */
46ebdf3c68SMilanka Ringwald
47ba7944beSMilanka Ringwald #include <stdint.h>
48ba7944beSMilanka Ringwald #include <string.h>
49ba7944beSMilanka Ringwald
50ebdf3c68SMilanka Ringwald #include "ble/att_db.h"
51ebdf3c68SMilanka Ringwald #include "ble/att_server.h"
52ba7944beSMilanka Ringwald #include "btstack_debug.h"
53105be2b8SMilanka Ringwald #include "btstack_event.h"
54ebdf3c68SMilanka Ringwald
55ebdf3c68SMilanka Ringwald #include "ble/gatt-service/nordic_spp_service_server.h"
56ebdf3c68SMilanka Ringwald
57ebdf3c68SMilanka Ringwald static att_service_handler_t nordic_spp_service;
58257f2b00SMatthias Ringwald static btstack_packet_handler_t client_packet_handler;
59105be2b8SMilanka Ringwald static btstack_packet_callback_registration_t nordic_spp_service_hci_event_callback_registration;
60ebdf3c68SMilanka Ringwald
61ebdf3c68SMilanka Ringwald static uint16_t nordic_spp_rx_value_handle;
62ba7944beSMilanka Ringwald static uint16_t nordic_spp_tx_value_handle;
63ba7944beSMilanka Ringwald static uint16_t nordic_spp_tx_client_configuration_handle;
64ba7944beSMilanka Ringwald static uint16_t nordic_spp_tx_client_configuration_value;
65*77a1fa17SMatthias Ringwald static hci_con_handle_t nordic_spp_con_handle;
66ebdf3c68SMilanka Ringwald
nordic_spp_service_emit_state(hci_con_handle_t con_handle,bool enabled)67257f2b00SMatthias Ringwald static void nordic_spp_service_emit_state(hci_con_handle_t con_handle, bool enabled){
68257f2b00SMatthias Ringwald uint8_t event[5];
69257f2b00SMatthias Ringwald uint8_t pos = 0;
70257f2b00SMatthias Ringwald event[pos++] = HCI_EVENT_GATTSERVICE_META;
71257f2b00SMatthias Ringwald event[pos++] = sizeof(event) - 2;
72257f2b00SMatthias Ringwald event[pos++] = enabled ? GATTSERVICE_SUBEVENT_SPP_SERVICE_CONNECTED : GATTSERVICE_SUBEVENT_SPP_SERVICE_DISCONNECTED;
73257f2b00SMatthias Ringwald little_endian_store_16(event,pos, (uint16_t) con_handle);
74257f2b00SMatthias Ringwald pos += 2;
75257f2b00SMatthias Ringwald (*client_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
76257f2b00SMatthias Ringwald }
77257f2b00SMatthias Ringwald
nordic_spp_service_read_callback(hci_con_handle_t con_handle,uint16_t attribute_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)78ebdf3c68SMilanka Ringwald static uint16_t nordic_spp_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
79ebdf3c68SMilanka Ringwald UNUSED(con_handle);
80ebdf3c68SMilanka Ringwald UNUSED(offset);
81ebdf3c68SMilanka Ringwald UNUSED(buffer_size);
82ba7944beSMilanka Ringwald
83ba7944beSMilanka Ringwald if (attribute_handle == nordic_spp_tx_client_configuration_handle){
849305033eSMatthias Ringwald if (buffer != NULL){
85ba7944beSMilanka Ringwald little_endian_store_16(buffer, 0, nordic_spp_tx_client_configuration_value);
86ebdf3c68SMilanka Ringwald }
87ebdf3c68SMilanka Ringwald return 2;
88ebdf3c68SMilanka Ringwald }
89ebdf3c68SMilanka Ringwald return 0;
90ebdf3c68SMilanka Ringwald }
91ebdf3c68SMilanka Ringwald
nordic_spp_service_write_callback(hci_con_handle_t con_handle,uint16_t attribute_handle,uint16_t transaction_mode,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)92ebdf3c68SMilanka Ringwald static int nordic_spp_service_write_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
93ebdf3c68SMilanka Ringwald UNUSED(offset);
94ebdf3c68SMilanka Ringwald UNUSED(buffer_size);
95ebdf3c68SMilanka Ringwald
96689e7fb3SMatthias Ringwald if (transaction_mode != ATT_TRANSACTION_MODE_NONE){
97689e7fb3SMatthias Ringwald return 0;
98689e7fb3SMatthias Ringwald }
99689e7fb3SMatthias Ringwald
100ba7944beSMilanka Ringwald if (attribute_handle == nordic_spp_rx_value_handle){
101257f2b00SMatthias Ringwald (*client_packet_handler)(RFCOMM_DATA_PACKET, (uint16_t) con_handle, buffer, buffer_size);
102ebdf3c68SMilanka Ringwald }
103257f2b00SMatthias Ringwald
104ba7944beSMilanka Ringwald if (attribute_handle == nordic_spp_tx_client_configuration_handle){
105ba7944beSMilanka Ringwald nordic_spp_tx_client_configuration_value = little_endian_read_16(buffer, 0);
106257f2b00SMatthias Ringwald bool enabled = (nordic_spp_tx_client_configuration_value != 0);
107*77a1fa17SMatthias Ringwald nordic_spp_con_handle = con_handle;
108257f2b00SMatthias Ringwald nordic_spp_service_emit_state(con_handle, enabled);
109ebdf3c68SMilanka Ringwald }
110ba7944beSMilanka Ringwald
111ebdf3c68SMilanka Ringwald return 0;
112ebdf3c68SMilanka Ringwald }
113ebdf3c68SMilanka Ringwald
nordic_spp_service_hci_event_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)114105be2b8SMilanka Ringwald static void nordic_spp_service_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
115105be2b8SMilanka Ringwald UNUSED(channel);
116105be2b8SMilanka Ringwald UNUSED(size);
117105be2b8SMilanka Ringwald
118105be2b8SMilanka Ringwald if (packet_type != HCI_EVENT_PACKET) return;
119105be2b8SMilanka Ringwald
120105be2b8SMilanka Ringwald hci_con_handle_t con_handle;
121105be2b8SMilanka Ringwald
122105be2b8SMilanka Ringwald switch (hci_event_packet_get_type(packet)){
123105be2b8SMilanka Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE:
124*77a1fa17SMatthias Ringwald con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
125*77a1fa17SMatthias Ringwald if (con_handle == nordic_spp_con_handle){
126*77a1fa17SMatthias Ringwald nordic_spp_con_handle = HCI_CON_HANDLE_INVALID;
127105be2b8SMilanka Ringwald if (nordic_spp_tx_client_configuration_value > 0){
128105be2b8SMilanka Ringwald nordic_spp_tx_client_configuration_value = 0;
129105be2b8SMilanka Ringwald nordic_spp_service_emit_state(con_handle, false);
130105be2b8SMilanka Ringwald }
131*77a1fa17SMatthias Ringwald }
132105be2b8SMilanka Ringwald break;
133105be2b8SMilanka Ringwald default:
134105be2b8SMilanka Ringwald break;
135105be2b8SMilanka Ringwald }
136105be2b8SMilanka Ringwald }
137105be2b8SMilanka Ringwald
138ebdf3c68SMilanka Ringwald /**
139ebdf3c68SMilanka Ringwald * @brief Init Nordic SPP Service Server with ATT DB
140ebdf3c68SMilanka Ringwald * @param callback for tx data from peer
141ebdf3c68SMilanka Ringwald */
nordic_spp_service_server_init(btstack_packet_handler_t packet_handler)142257f2b00SMatthias Ringwald void nordic_spp_service_server_init(btstack_packet_handler_t packet_handler){
1438334d3d8SMatthias Ringwald
1448334d3d8SMatthias Ringwald static const uint8_t nordic_spp_profile_uuid128[] = { 0x6E, 0x40, 0x00, 0x01, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
1458334d3d8SMatthias Ringwald static const uint8_t nordic_spp_rx_uuid128[] = { 0x6E, 0x40, 0x00, 0x02, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
1468334d3d8SMatthias Ringwald static const uint8_t nordic_spp_tx_uuid128[] = { 0x6E, 0x40, 0x00, 0x03, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
1478334d3d8SMatthias Ringwald
148257f2b00SMatthias Ringwald client_packet_handler = packet_handler;
149ebdf3c68SMilanka Ringwald
150ebdf3c68SMilanka Ringwald // get service handle range
151ebdf3c68SMilanka Ringwald uint16_t start_handle = 0;
152ba7944beSMilanka Ringwald uint16_t end_handle = 0xffff;
153c436b760SMilanka Ringwald int service_found = gatt_server_get_handle_range_for_service_with_uuid128(nordic_spp_profile_uuid128, &start_handle, &end_handle);
154a2489f29SMatthias Ringwald btstack_assert(service_found != 0);
155a2489f29SMatthias Ringwald UNUSED(service_found);
156ebdf3c68SMilanka Ringwald
157ebdf3c68SMilanka Ringwald // get characteristic value handle and client configuration handle
158ebdf3c68SMilanka Ringwald nordic_spp_rx_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, nordic_spp_rx_uuid128);
159ba7944beSMilanka Ringwald nordic_spp_tx_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, nordic_spp_tx_uuid128);
160ba7944beSMilanka Ringwald nordic_spp_tx_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, nordic_spp_tx_uuid128);
161ba7944beSMilanka Ringwald
162ba7944beSMilanka Ringwald log_info("nordic_spp_rx_value_handle 0x%02x", nordic_spp_rx_value_handle);
163ba7944beSMilanka Ringwald log_info("nordic_spp_tx_value_handle 0x%02x", nordic_spp_tx_value_handle);
164ba7944beSMilanka Ringwald log_info("nordic_spp_tx_client_configuration_handle 0x%02x", nordic_spp_tx_client_configuration_handle);
165ebdf3c68SMilanka Ringwald
166ebdf3c68SMilanka Ringwald // register service with ATT Server
167ebdf3c68SMilanka Ringwald nordic_spp_service.start_handle = start_handle;
168ebdf3c68SMilanka Ringwald nordic_spp_service.end_handle = end_handle;
169ebdf3c68SMilanka Ringwald nordic_spp_service.read_callback = &nordic_spp_service_read_callback;
170ebdf3c68SMilanka Ringwald nordic_spp_service.write_callback = &nordic_spp_service_write_callback;
171ebdf3c68SMilanka Ringwald att_server_register_service_handler(&nordic_spp_service);
172105be2b8SMilanka Ringwald
173105be2b8SMilanka Ringwald // register HCI packet handler
174105be2b8SMilanka Ringwald nordic_spp_service_hci_event_callback_registration.callback = &nordic_spp_service_hci_event_handler;
175105be2b8SMilanka Ringwald hci_add_event_handler(&nordic_spp_service_hci_event_callback_registration);
176*77a1fa17SMatthias Ringwald
177*77a1fa17SMatthias Ringwald // reset connection
178*77a1fa17SMatthias Ringwald nordic_spp_con_handle = HCI_CON_HANDLE_INVALID;
179ebdf3c68SMilanka Ringwald }
180ebdf3c68SMilanka Ringwald
181ebdf3c68SMilanka Ringwald /**
182ebdf3c68SMilanka Ringwald * @brief Queue send request. When called, one packet can be send via nordic_spp_service_send below
183ebdf3c68SMilanka Ringwald * @param request
184ebdf3c68SMilanka Ringwald * @param con_handle
185ebdf3c68SMilanka Ringwald */
nordic_spp_service_server_request_can_send_now(btstack_context_callback_registration_t * request,hci_con_handle_t con_handle)186ebdf3c68SMilanka Ringwald void nordic_spp_service_server_request_can_send_now(btstack_context_callback_registration_t * request, hci_con_handle_t con_handle){
187ebdf3c68SMilanka Ringwald att_server_request_to_send_notification(request, con_handle);
188ebdf3c68SMilanka Ringwald }
189ebdf3c68SMilanka Ringwald
190ebdf3c68SMilanka Ringwald /**
191ebdf3c68SMilanka Ringwald * @brief Send data
192ebdf3c68SMilanka Ringwald * @param con_handle
193ebdf3c68SMilanka Ringwald * @param data
194ebdf3c68SMilanka Ringwald * @param size
195ebdf3c68SMilanka Ringwald */
nordic_spp_service_server_send(hci_con_handle_t con_handle,const uint8_t * data,uint16_t size)196ebdf3c68SMilanka Ringwald int nordic_spp_service_server_send(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size){
197842f9974SMilanka Ringwald return att_server_notify(con_handle, nordic_spp_tx_value_handle, data, size);
198ebdf3c68SMilanka Ringwald }
199ebdf3c68SMilanka Ringwald
200