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