xref: /btstack/src/ble/gatt-service/nordic_spp_service_server.c (revision 29502c9b6278f5ef85634162beef9b6a43c78688)
1 /*
2  * Copyright (C) 2018 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "nordic_spp_service_server.c"
39 
40 /**
41  * Implementation of the Nordic SPP-like profile
42  *
43  * To use with your application, add `#import <nordic_spp_service.gatt>` to your .gatt file
44  * and call all functions below. All strings and blobs need to stay valid after calling the functions.
45  */
46 
47 #include <stdint.h>
48 #include <string.h>
49 
50 #include "ble/att_db.h"
51 #include "ble/att_server.h"
52 #include "btstack_debug.h"
53 #include "btstack_event.h"
54 
55 #include "ble/gatt-service/nordic_spp_service_server.h"
56 
57 static att_service_handler_t  nordic_spp_service;
58 static btstack_packet_handler_t client_packet_handler;
59 static btstack_packet_callback_registration_t nordic_spp_service_hci_event_callback_registration;
60 
61 static uint16_t nordic_spp_rx_value_handle;
62 static uint16_t nordic_spp_tx_value_handle;
63 static uint16_t nordic_spp_tx_client_configuration_handle;
64 static uint16_t nordic_spp_tx_client_configuration_value;
65 static hci_con_handle_t nordic_spp_con_handle;
66 
67 static void nordic_spp_service_emit_state(hci_con_handle_t con_handle, bool enabled){
68     uint8_t event[5];
69     uint8_t pos = 0;
70     event[pos++] = HCI_EVENT_GATTSERVICE_META;
71     event[pos++] = sizeof(event) - 2;
72     event[pos++] = enabled ? GATTSERVICE_SUBEVENT_SPP_SERVICE_CONNECTED : GATTSERVICE_SUBEVENT_SPP_SERVICE_DISCONNECTED;
73     little_endian_store_16(event,pos, (uint16_t) con_handle);
74     pos += 2;
75     (*client_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
76 }
77 
78 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){
79 	UNUSED(con_handle);
80 	UNUSED(offset);
81 	UNUSED(buffer_size);
82 
83 	if (attribute_handle == nordic_spp_tx_client_configuration_handle){
84 		if (buffer != NULL){
85 			little_endian_store_16(buffer, 0, nordic_spp_tx_client_configuration_value);
86 		}
87 		return 2;
88 	}
89 	return 0;
90 }
91 
92 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){
93 	UNUSED(offset);
94 	UNUSED(buffer_size);
95 
96     if (transaction_mode != ATT_TRANSACTION_MODE_NONE){
97         return 0;
98     }
99 
100     if (attribute_handle == nordic_spp_rx_value_handle){
101         (*client_packet_handler)(RFCOMM_DATA_PACKET, (uint16_t) con_handle, buffer, buffer_size);
102 	}
103 
104 	if (attribute_handle == nordic_spp_tx_client_configuration_handle){
105 		nordic_spp_tx_client_configuration_value = little_endian_read_16(buffer, 0);
106 		bool enabled = (nordic_spp_tx_client_configuration_value != 0);
107         nordic_spp_con_handle = con_handle;
108         nordic_spp_service_emit_state(con_handle, enabled);
109 	}
110 
111 	return 0;
112 }
113 
114 static void nordic_spp_service_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
115     UNUSED(channel);
116     UNUSED(size);
117 
118     if (packet_type != HCI_EVENT_PACKET) return;
119 
120     hci_con_handle_t con_handle;
121 
122     switch (hci_event_packet_get_type(packet)){
123         case HCI_EVENT_DISCONNECTION_COMPLETE:
124             con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
125             if (con_handle == nordic_spp_con_handle){
126                 nordic_spp_con_handle = HCI_CON_HANDLE_INVALID;
127                 if (nordic_spp_tx_client_configuration_value > 0){
128                     nordic_spp_tx_client_configuration_value = 0;
129                     nordic_spp_service_emit_state(con_handle, false);
130                 }
131             }
132             break;
133         default:
134             break;
135     }
136 }
137 
138 /**
139  * @brief Init Nordic SPP Service Server with ATT DB
140  * @param callback for tx data from peer
141  */
142 void nordic_spp_service_server_init(btstack_packet_handler_t packet_handler){
143 
144     static const uint8_t nordic_spp_profile_uuid128[] = { 0x6E, 0x40, 0x00, 0x01, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
145     static const uint8_t nordic_spp_rx_uuid128[] 	  = { 0x6E, 0x40, 0x00, 0x02, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
146     static const uint8_t nordic_spp_tx_uuid128[] 	  = { 0x6E, 0x40, 0x00, 0x03, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
147 
148     client_packet_handler = packet_handler;
149 
150 	// get service handle range
151 	uint16_t start_handle = 0;
152 	uint16_t end_handle   = 0xffff;
153 	int service_found = gatt_server_get_handle_range_for_service_with_uuid128(nordic_spp_profile_uuid128, &start_handle, &end_handle);
154 	btstack_assert(service_found != 0);
155 	UNUSED(service_found);
156 
157 	// get characteristic value handle and client configuration handle
158 	nordic_spp_rx_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, nordic_spp_rx_uuid128);
159 	nordic_spp_tx_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, nordic_spp_tx_uuid128);
160 	nordic_spp_tx_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, nordic_spp_tx_uuid128);
161 
162 	log_info("nordic_spp_rx_value_handle 					0x%02x", nordic_spp_rx_value_handle);
163 	log_info("nordic_spp_tx_value_handle 					0x%02x", nordic_spp_tx_value_handle);
164 	log_info("nordic_spp_tx_client_configuration_handle 	0x%02x", nordic_spp_tx_client_configuration_handle);
165 
166 	// register service with ATT Server
167 	nordic_spp_service.start_handle   = start_handle;
168 	nordic_spp_service.end_handle     = end_handle;
169 	nordic_spp_service.read_callback  = &nordic_spp_service_read_callback;
170 	nordic_spp_service.write_callback = &nordic_spp_service_write_callback;
171 	att_server_register_service_handler(&nordic_spp_service);
172 
173     // register HCI packet handler
174     nordic_spp_service_hci_event_callback_registration.callback = &nordic_spp_service_hci_event_handler;
175     hci_add_event_handler(&nordic_spp_service_hci_event_callback_registration);
176 
177     // reset connection
178     nordic_spp_con_handle = HCI_CON_HANDLE_INVALID;
179 }
180 
181 /**
182  * @brief Queue send request. When called, one packet can be send via nordic_spp_service_send below
183  * @param request
184  * @param con_handle
185  */
186 void nordic_spp_service_server_request_can_send_now(btstack_context_callback_registration_t * request, hci_con_handle_t con_handle){
187 	att_server_request_to_send_notification(request, con_handle);
188 }
189 
190 /**
191  * @brief Send data
192  * @param con_handle
193  * @param data
194  * @param size
195  */
196 int nordic_spp_service_server_send(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size){
197 	return att_server_notify(con_handle, nordic_spp_tx_value_handle, data, size);
198 }
199 
200