xref: /btstack/src/ble/gatt-service/heart_rate_service_server.c (revision 287aaa024cdacad7f7e3b3698328b0dd9cdfecc0)
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 >= 2u)){
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 >= 1u)){
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 < 2u){
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 	btstack_assert(service_found != 0);
154 	UNUSED(service_found);
155 
156     // get Heart Rate Mesurement characteristic value handle and client configuration handle
157     instance->measurement_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_HEART_RATE_MEASUREMENT);
158     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);
159     // get Body Sensor Location characteristic value handle and client configuration handle
160     instance->sensor_location_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BODY_SENSOR_LOCATION);
161     // get Hear Rate Control Point characteristic value handle and client configuration handle
162     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);
163 
164     log_info("Measurement     value handle 0x%02x", instance->measurement_value_handle);
165     log_info("Client Config   value handle 0x%02x", instance->measurement_client_configuration_descriptor_handle);
166     log_info("Sensor location value handle 0x%02x", instance->sensor_location_value_handle);
167     log_info("Control Point   value handle 0x%02x", instance->control_point_value_handle);
168     // register service with ATT Server
169     heart_rate_service.start_handle   = start_handle;
170     heart_rate_service.end_handle     = end_handle;
171     heart_rate_service.read_callback  = &heart_rate_service_read_callback;
172     heart_rate_service.write_callback = &heart_rate_service_write_callback;
173 
174     att_server_register_service_handler(&heart_rate_service);
175 }
176 
177 
178 static void heart_rate_service_can_send_now(void * context){
179     heart_rate_t * instance = (heart_rate_t *) context;
180     uint8_t flags = (1 << HEART_RATE_SERVICE_VALUE_FORMAT);
181     flags |= (instance->sensor_contact << HEART_RATE_SERVICE_SENSOR_CONTACT_STATUS);
182     if (instance->energy_expended_supported){
183         flags |= (1u << HEART_RATE_SERVICE_ENERGY_EXPENDED_STATUS);
184     }
185     if (instance->rr_interval_count){
186         flags |= (1u << HEART_RATE_SERVICE_RR_INTERVAL);
187     }
188 
189     uint8_t value[100];
190     int pos = 0;
191 
192     value[pos++] = flags;
193     little_endian_store_16(value, pos, instance->measurement_bpm);
194     pos += 2;
195     if (instance->energy_expended_supported){
196         little_endian_store_16(value, pos, instance->energy_expended_kJ);
197         pos += 2;
198     }
199 
200     // TODO: get actual MTU from ATT server
201     uint16_t bytes_left = btstack_min(sizeof(value), l2cap_max_mtu() - 3u - pos);
202 
203     while ((bytes_left > 2u) && instance->rr_interval_count){
204         little_endian_store_16(value, pos, instance->rr_intervals[0]);
205         pos +=2;
206         bytes_left -= 2u;
207         instance->rr_intervals++;
208         instance->rr_interval_count--;
209     }
210 
211     att_server_notify(instance->con_handle, instance->measurement_value_handle, &value[0], pos);
212 
213     if (instance->rr_interval_count){
214         instance->measurement_callback.callback = &heart_rate_service_can_send_now;
215         instance->measurement_callback.context  = (void*) instance;
216         att_server_register_can_send_now_callback(&instance->measurement_callback, instance->con_handle);
217     }
218 }
219 
220 void heart_rate_service_add_energy_expended(uint16_t energy_expended_kJ){
221     heart_rate_t * instance = &heart_rate;
222     // limit energy expended to 0xffff
223     if (instance->energy_expended_kJ <= (0xffffu - energy_expended_kJ)){
224         instance->energy_expended_kJ += energy_expended_kJ;
225     } else {
226         instance->energy_expended_kJ = 0xffff;
227     }
228 }
229 
230 void heart_rate_service_server_update_heart_rate_values(uint16_t heart_rate_bpm,
231     heart_rate_service_sensor_contact_status_t sensor_contact, int rr_interval_count, uint16_t * rr_intervals){
232     heart_rate_t * instance = &heart_rate;
233 
234     instance->measurement_bpm = heart_rate_bpm;
235     instance->sensor_contact = sensor_contact;
236     instance->rr_interval_count = rr_interval_count;
237     instance->rr_intervals = rr_intervals;
238     instance->rr_offset = 0;
239 
240     if (instance->measurement_client_configuration_descriptor_notify){
241         instance->measurement_callback.callback = &heart_rate_service_can_send_now;
242         instance->measurement_callback.context  = (void*) instance;
243         att_server_register_can_send_now_callback(&instance->measurement_callback, instance->con_handle);
244     }
245 }