xref: /btstack/src/ble/gatt-service/heart_rate_service_server.c (revision dbe92de6a93924b91532f8d818af55adf6b370d8)
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     return 0;
111 }
112 
113 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){
114     UNUSED(transaction_mode);
115     UNUSED(offset);
116     UNUSED(buffer_size);
117 
118     if (attribute_handle == heart_rate.measurement_client_configuration_descriptor_handle){
119         if (buffer_size < 2){
120             return ATT_ERROR_INVALID_OFFSET;
121         }
122         heart_rate.measurement_client_configuration_descriptor_notify = little_endian_read_16(buffer, 0);
123         heart_rate.con_handle = con_handle;
124         return 0;
125     }
126 
127     if (attribute_handle == heart_rate.control_point_value_handle){
128         uint16_t cmd = little_endian_read_16(buffer, 0);
129         switch (cmd){
130             case HEART_RATE_RESET_ENERGY_EXPENDED:
131                 heart_rate.energy_expended_kJ = 0;
132                 heart_rate.con_handle = con_handle;
133                 break;
134             default:
135                 return HEART_RATE_CONTROL_POINT_NOT_SUPPORTED;
136         }
137         return 0;
138     }
139     return 0;
140 }
141 
142 
143 void heart_rate_service_server_init(heart_rate_service_body_sensor_location_t location, int energy_expended_supported){
144     heart_rate_t * instance = &heart_rate;
145 
146     instance->sensor_location = location;
147     instance->energy_expended_supported = energy_expended_supported;
148 
149     // get service handle range
150     uint16_t start_handle = 0;
151     uint16_t end_handle   = 0xffff;
152     int service_found = gatt_server_get_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HEART_RATE, &start_handle, &end_handle);
153     if (!service_found) return;
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 |= (1 << HEART_RATE_SERVICE_ENERGY_EXPENDED_STATUS);
183     }
184     if (instance->rr_interval_count){
185         flags |= (1 << 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     // TODO: get actual MTU from ATT server
200     uint16_t bytes_left = btstack_min(sizeof(value), l2cap_max_mtu() - 3 - pos);
201 
202     while ((bytes_left > 2) && instance->rr_interval_count){
203         little_endian_store_16(value, pos, instance->rr_intervals[0]);
204         pos +=2;
205         bytes_left -= 2;
206         instance->rr_intervals++;
207         instance->rr_interval_count--;
208     }
209 
210     att_server_notify(instance->con_handle, instance->measurement_value_handle, &value[0], pos);
211 
212     if (instance->rr_interval_count){
213         instance->measurement_callback.callback = &heart_rate_service_can_send_now;
214         instance->measurement_callback.context  = (void*) instance;
215         att_server_register_can_send_now_callback(&instance->measurement_callback, instance->con_handle);
216     }
217 }
218 
219 void heart_rate_service_add_energy_expended(uint16_t energy_expended_kJ){
220     heart_rate_t * instance = &heart_rate;
221     // limit energy expended to 0xffff
222     if (instance->energy_expended_kJ <= (0xffff - energy_expended_kJ)){
223         instance->energy_expended_kJ += energy_expended_kJ;
224     } else {
225         instance->energy_expended_kJ = 0xffff;
226     }
227 }
228 
229 void heart_rate_service_server_update_heart_rate_values(uint16_t heart_rate_bpm,
230     heart_rate_service_sensor_contact_status_t sensor_contact, int rr_interval_count, uint16_t * rr_intervals){
231     heart_rate_t * instance = &heart_rate;
232 
233     instance->measurement_bpm = heart_rate_bpm;
234     instance->sensor_contact = sensor_contact;
235     instance->rr_interval_count = rr_interval_count;
236     instance->rr_intervals = rr_intervals;
237     instance->rr_offset = 0;
238 
239     if (instance->measurement_client_configuration_descriptor_notify){
240         instance->measurement_callback.callback = &heart_rate_service_can_send_now;
241         instance->measurement_callback.context  = (void*) instance;
242         att_server_register_can_send_now_callback(&instance->measurement_callback, instance->con_handle);
243     }
244 }