xref: /btstack/src/ble/gatt-service/heart_rate_service_server.c (revision 82c9843e67930118ee91effaf3bfc78a59bb5b9a)
1 /*
2  * Copyright (C) 2014 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__ "heart_rate_service_server.c"
39 
40 
41 #include "bluetooth.h"
42 #include "btstack_defines.h"
43 #include "ble/att_db.h"
44 #include "ble/att_server.h"
45 #include "btstack_util.h"
46 #include "bluetooth_gatt.h"
47 #include "btstack_debug.h"
48 #include "l2cap.h"
49 
50 #include "ble/gatt-service/heart_rate_service_server.h"
51 
52 #define HEART_RATE_RESET_ENERGY_EXPENDED 0x01
53 #define HEART_RATE_CONTROL_POINT_NOT_SUPPORTED 0x80
54 
55 typedef enum {
56 	HEART_RATE_SERVICE_VALUE_FORMAT = 0,
57 	HEART_RATE_SERVICE_SENSOR_CONTACT_STATUS,
58 	HEART_RATE_SERVICE_ENERGY_EXPENDED_STATUS = 3,
59 	HEART_RATE_SERVICE_RR_INTERVAL
60 } heart_rate_service_flag_bit_t;
61 
62 typedef struct {
63 	hci_con_handle_t con_handle;
64 
65 	// characteristic: Heart Rate Mesurement
66 	uint16_t measurement_value_handle;
67 	uint16_t measurement_bpm;
68 	uint8_t  energy_expended_supported;
69 	uint16_t energy_expended_kJ; // kilo Joules
70 	int rr_interval_count;
71 	int rr_offset;
72 	uint16_t * rr_intervals;
73 	heart_rate_service_sensor_contact_status_t sensor_contact;
74 
75 	// characteristic descriptor: Client Characteristic Configuration
76 	uint16_t measurement_client_configuration_descriptor_handle;
77 	uint16_t measurement_client_configuration_descriptor_notify;
78 	btstack_context_callback_registration_t measurement_callback;
79 
80 	// characteristic: Body Sensor Location
81 	uint16_t sensor_location_value_handle;
82 	heart_rate_service_body_sensor_location_t sensor_location;
83 
84 	// characteristic: Heart Rate Control Point
85 	uint16_t control_point_value_handle;
86 } heart_rate_t;
87 
88 static att_service_handler_t heart_rate_service;
89 static heart_rate_t heart_rate;
90 
91 static uint16_t heart_rate_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
92 	UNUSED(con_handle);
93 	UNUSED(attribute_handle);
94 	UNUSED(offset);
95 	UNUSED(buffer_size);
96 
97 	if (attribute_handle == heart_rate.measurement_client_configuration_descriptor_handle){
98 		if (buffer && buffer_size >= 2){
99 			little_endian_store_16(buffer, 0, heart_rate.measurement_client_configuration_descriptor_notify);
100 		}
101 		return 2;
102 	}
103 
104 	if (attribute_handle == heart_rate.sensor_location_value_handle){
105 		if (buffer && buffer_size >= 1){
106 			buffer[0] = heart_rate.sensor_location;
107 		}
108 		return 1;
109 	}
110 	printf("heart_rate_service_read_callback, not handeled read on handle 0x%02x\n", attribute_handle);
111 	return 0;
112 }
113 
114 static int heart_rate_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){
115 	UNUSED(transaction_mode);
116 	UNUSED(offset);
117 	UNUSED(buffer_size);
118 
119 	if (attribute_handle == heart_rate.measurement_client_configuration_descriptor_handle){
120 		if (buffer_size < 2){
121 			return ATT_ERROR_INVALID_OFFSET;
122 		}
123 		heart_rate.measurement_client_configuration_descriptor_notify = little_endian_read_16(buffer, 0);
124 		heart_rate.con_handle = con_handle;
125 		if (heart_rate.measurement_client_configuration_descriptor_notify){
126 			printf("notify enabled\n");
127 		} else {
128 			printf("notify disabled\n");
129 		}
130 		return 0;
131 	}
132 
133 	if (attribute_handle == heart_rate.control_point_value_handle){
134 		uint16_t cmd = little_endian_read_16(buffer, 0);
135 		switch (cmd){
136 			case HEART_RATE_RESET_ENERGY_EXPENDED:
137 				heart_rate.energy_expended_kJ = 0;
138 				heart_rate.con_handle = con_handle;
139 				break;
140 			default:
141 				return HEART_RATE_CONTROL_POINT_NOT_SUPPORTED;
142 		}
143 		return 0;
144 	}
145 
146 	printf("heart_rate_service_write_callback, not handeled write on handle 0x%02x\n", attribute_handle);
147 	return 0;
148 }
149 
150 
151 void heart_rate_service_server_init(heart_rate_service_body_sensor_location_t location, int energy_expended_supported){
152 	heart_rate_t * instance = &heart_rate;
153 
154 	instance->sensor_location = location;
155 	instance->energy_expended_supported = energy_expended_supported;
156 
157 	// get service handle range
158 	uint16_t start_handle = 0;
159 	uint16_t end_handle   = 0xffff;
160 	int service_found = gatt_server_get_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HEART_RATE, &start_handle, &end_handle);
161 	if (!service_found) return;
162 
163 	// get Heart Rate Mesurement characteristic value handle and client configuration handle
164 	instance->measurement_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_HEART_RATE_MEASUREMENT);
165 	instance->measurement_client_configuration_descriptor_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_HEART_RATE_MEASUREMENT);
166 	// get Body Sensor Location characteristic value handle and client configuration handle
167 	instance->sensor_location_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BODY_SENSOR_LOCATION);
168 	// get Hear Rate Control Point characteristic value handle and client configuration handle
169 	instance->control_point_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_HEART_RATE_CONTROL_POINT);
170 
171 	printf("Measurement     value handle 0x%02x\n", instance->measurement_value_handle);
172 	printf("Client Config   value handle 0x%02x\n", instance->measurement_client_configuration_descriptor_handle);
173 	printf("Sensor location value handle 0x%02x\n", instance->sensor_location_value_handle);
174 	printf("Control Point   value handle 0x%02x\n", instance->control_point_value_handle);
175 	// register service with ATT Server
176 	heart_rate_service.start_handle   = start_handle;
177 	heart_rate_service.end_handle     = end_handle;
178 	heart_rate_service.read_callback  = &heart_rate_service_read_callback;
179 	heart_rate_service.write_callback = &heart_rate_service_write_callback;
180 
181 	att_server_register_service_handler(&heart_rate_service);
182 }
183 
184 
185 static void heart_rate_service_can_send_now(void * context){
186 	heart_rate_t * instance = (heart_rate_t *) context;
187 	uint8_t flags = (1 << HEART_RATE_SERVICE_VALUE_FORMAT);
188 	flags |= (instance->sensor_contact << HEART_RATE_SERVICE_SENSOR_CONTACT_STATUS);
189 	if (instance->energy_expended_supported){
190 		flags |= (1 << HEART_RATE_SERVICE_ENERGY_EXPENDED_STATUS);
191 	}
192 	if (instance->rr_interval_count){
193 		flags |= (1 << HEART_RATE_SERVICE_RR_INTERVAL);
194 	}
195 	printf("heart_rate_service_can_send_now: flags 0%2x\n", flags);
196 
197 	uint8_t value[100];
198 	int pos = 0;
199 
200 	value[pos++] = flags;
201 	little_endian_store_16(value, pos, instance->measurement_bpm);
202 	pos += 2;
203 	if (instance->energy_expended_supported){
204 		little_endian_store_16(value, pos, instance->energy_expended_kJ);
205 		pos += 2;
206 	}
207 
208 	// TODO: get actual MTU from ATT server
209 	uint16_t bytes_left = btstack_min(sizeof(value), l2cap_max_mtu() - 3 - pos);
210 
211 	while (bytes_left > 2 && instance->rr_interval_count){
212 		little_endian_store_16(value, pos, instance->rr_intervals[0]);
213 		pos +=2;
214 		bytes_left -= 2;
215 		instance->rr_intervals++;
216 		instance->rr_interval_count--;
217 	}
218 
219 	printf_hexdump(value, pos);
220 	att_server_notify(instance->con_handle, instance->measurement_value_handle, &value[0], pos);
221 
222 	if (instance->rr_interval_count){
223 		instance->measurement_callback.callback = &heart_rate_service_can_send_now;
224 		instance->measurement_callback.context  = (void*) instance;
225 		att_server_register_can_send_now_callback(&instance->measurement_callback, instance->con_handle);
226 	}
227 }
228 
229 void heart_rate_service_add_energy_expended(uint16_t energy_expended_kJ){
230 	heart_rate_t * instance = &heart_rate;
231 	// limit energy expended to 0xffff
232 	if (instance->energy_expended_kJ <= 0xffff - energy_expended_kJ){
233 		instance->energy_expended_kJ += energy_expended_kJ;
234 	} else {
235 		instance->energy_expended_kJ = 0xffff;
236 	}
237 }
238 
239 void heart_rate_service_server_update_heart_rate_values(uint16_t heart_rate_bpm,
240 	heart_rate_service_sensor_contact_status_t sensor_contact, int rr_interval_count, uint16_t * rr_intervals){
241 	heart_rate_t * instance = &heart_rate;
242 
243 	printf("update_heart_rate_values, notify %u con_handle %04x\n", instance->measurement_client_configuration_descriptor_notify, instance->con_handle);
244 
245 	instance->measurement_bpm = heart_rate_bpm;
246 	instance->sensor_contact = sensor_contact;
247 	instance->rr_interval_count = rr_interval_count;
248 	instance->rr_intervals = rr_intervals;
249 	instance->rr_offset = 0;
250 
251 	if (instance->measurement_client_configuration_descriptor_notify){
252 		instance->measurement_callback.callback = &heart_rate_service_can_send_now;
253 		instance->measurement_callback.context  = (void*) instance;
254 		att_server_register_can_send_now_callback(&instance->measurement_callback, instance->con_handle);
255 	}
256 }