xref: /btstack/src/ble/gatt-service/nordic_spp_service_server.c (revision 7cdc89a533ca236b2c2564b759993b788bae89d3)
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 MATTHIAS
24  * RINGWALD 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 <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include "btstack_defines.h"
53 #include "ble/att_db.h"
54 #include "ble/att_server.h"
55 #include "btstack_util.h"
56 #include "bluetooth_gatt.h"
57 #include "btstack_debug.h"
58 
59 #include "ble/gatt-service/nordic_spp_service_server.h"
60 
61 //
62 static const uint8_t nordic_spp_profile_uuid128[] = { 0x6E, 0x40, 0x00, 0x01, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
63 static const uint8_t nordic_spp_rx_uuid128[] 	  = { 0x6E, 0x40, 0x00, 0x02, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
64 static const uint8_t nordic_spp_tx_uuid128[] 	  = { 0x6E, 0x40, 0x00, 0x03, 0xB5, 0xA3, 0xF3, 0x93, 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E };
65 
66 static att_service_handler_t  nordic_spp_service;
67 static void (*client_callback)(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size);
68 
69 static uint16_t nordic_spp_rx_value_handle;
70 static uint16_t nordic_spp_tx_value_handle;
71 static uint16_t nordic_spp_tx_client_configuration_handle;
72 static uint16_t nordic_spp_tx_client_configuration_value;
73 
74 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){
75 	UNUSED(con_handle);
76 	UNUSED(offset);
77 	UNUSED(buffer_size);
78 
79 	if (attribute_handle == nordic_spp_tx_client_configuration_handle){
80 		if (buffer){
81 			little_endian_store_16(buffer, 0, nordic_spp_tx_client_configuration_value);
82 		}
83 		return 2;
84 	}
85 	return 0;
86 }
87 
88 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){
89 	UNUSED(transaction_mode);
90 	UNUSED(offset);
91 	UNUSED(buffer_size);
92 
93 	if (attribute_handle == nordic_spp_rx_value_handle){
94 		client_callback(con_handle, &buffer[0], buffer_size);
95 	}
96 	if (attribute_handle == nordic_spp_tx_client_configuration_handle){
97 		nordic_spp_tx_client_configuration_value = little_endian_read_16(buffer, 0);
98 		if (nordic_spp_tx_client_configuration_value){
99 			client_callback(con_handle, NULL, 0);
100 		}
101 	}
102 
103 	return 0;
104 }
105 
106 /**
107  * @brief Init Nordic SPP Service Server with ATT DB
108  * @param callback for tx data from peer
109  */
110 void nordic_spp_service_server_init(void (*callback)(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size)){
111 	client_callback = callback;
112 
113 	// get service handle range
114 	uint16_t start_handle = 0;
115 	uint16_t end_handle   = 0xffff;
116 	int service_found = gatt_server_get_get_handle_range_for_service_with_uuid128(nordic_spp_profile_uuid128, &start_handle, &end_handle);
117 	if (!service_found) return;
118 
119 	// get characteristic value handle and client configuration handle
120 	nordic_spp_rx_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, nordic_spp_rx_uuid128);
121 	nordic_spp_tx_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, nordic_spp_tx_uuid128);
122 	nordic_spp_tx_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, nordic_spp_tx_uuid128);
123 
124 	log_info("nordic_spp_rx_value_handle 					0x%02x", nordic_spp_rx_value_handle);
125 	log_info("nordic_spp_tx_value_handle 					0x%02x", nordic_spp_tx_value_handle);
126 	log_info("nordic_spp_tx_client_configuration_handle 	0x%02x", nordic_spp_tx_client_configuration_handle);
127 
128 	// register service with ATT Server
129 	nordic_spp_service.start_handle   = start_handle;
130 	nordic_spp_service.end_handle     = end_handle;
131 	nordic_spp_service.read_callback  = &nordic_spp_service_read_callback;
132 	nordic_spp_service.write_callback = &nordic_spp_service_write_callback;
133 	att_server_register_service_handler(&nordic_spp_service);
134 }
135 
136 /**
137  * @brief Queue send request. When called, one packet can be send via nordic_spp_service_send below
138  * @param request
139  * @param con_handle
140  */
141 void nordic_spp_service_server_request_can_send_now(btstack_context_callback_registration_t * request, hci_con_handle_t con_handle){
142 	att_server_request_to_send_notification(request, con_handle);
143 }
144 
145 /**
146  * @brief Send data
147  * @param con_handle
148  * @param data
149  * @param size
150  */
151 int nordic_spp_service_server_send(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size){
152 	return att_server_notify(con_handle, nordic_spp_tx_value_handle, data, size);
153 }
154 
155