/* * Copyright (C) 2014 BlueKitchen GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holders nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * 4. Any redistribution, use, or modification is done solely for * personal benefit and not for any commercial purpose or for * monetary gain. * * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Please inquire about commercial licensing options at * contact@bluekitchen-gmbh.com * */ #define __BTSTACK_FILE__ "heart_rate_service_server.c" /** * Implementation of the GATT Battery Service Server * To use with your application, add '#import sensor_location = location; // get service handle range uint16_t start_handle = 0; uint16_t end_handle = 0xffff; int service_found = gatt_server_get_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HEART_RATE, &start_handle, &end_handle); if (!service_found) return; // get Heart Rate Mesurement characteristic value handle and client configuration handle instance->measurement_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_HEART_RATE_MEASUREMENT); 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); // get Body Sensor Location characteristic value handle and client configuration handle instance->sensor_location_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_SENSOR_LOCATION); // get Hear Rate Control Point characteristic value handle and client configuration handle 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); // register service with ATT Server heart_rate_service.start_handle = start_handle; heart_rate_service.end_handle = end_handle; heart_rate_service.read_callback = &heart_rate_service_read_callback; heart_rate_service.write_callback = &heart_rate_service_write_callback; att_server_register_service_handler(&heart_rate_service); } static void heart_rate_service_can_send_now(void * context){ heart_rate_t * instance = (heart_rate_t *) context; instance->flags |= (1 << HEART_RATE_SERVICE_VALUE_FORMAT); instance->flags |= (instance->sensor_contact << HEART_RATE_SERVICE_SENSOR_CONTACT_STATUS); if (instance->energy_expended_kJ){ instance->flags |= (1 << HEART_RATE_SERVICE_ENERGY_EXPENDED_STATUS); } if (instance->rr_interval_count){ instance->flags |= (1 << HEART_RATE_SERVICE_RR_INTERVAL); } uint8_t value[100]; int pos = 0; value[pos++] = instance->flags; little_endian_store_16(value, pos, instance->measurement_bpm); pos += 2; if (instance->energy_expended_kJ){ little_endian_store_16(value, pos, instance->energy_expended_kJ); pos += 2; } // TODO: get actual MTU from ATT server uint16_t bytes_left = btstack_min(sizeof(value), l2cap_max_mtu() - 3 - pos); while (bytes_left > 2 && instance->rr_interval_count){ little_endian_store_16(value, pos, instance->rr_intervals[0]); pos +=2; bytes_left -= 2; instance->rr_intervals++; instance->rr_interval_count--; } att_server_notify(instance->con_handle, instance->measurement_value_handle, &value[0], pos); if (instance->rr_interval_count){ instance->measurement_callback.callback = &heart_rate_service_can_send_now; instance->measurement_callback.context = (void*) instance; att_server_register_can_send_now_callback(&instance->measurement_callback, instance->con_handle); } } void heart_rate_service_add_energy_expended(uint16_t energy_expended_kJ){ heart_rate_t * instance = &heart_rate; // limit energy expended to 0xffff if ( instance->energy_expended_kJ <= 0xffff - energy_expended_kJ){ instance->energy_expended_kJ += energy_expended_kJ; } else { instance->energy_expended_kJ = 0xffff; } if (instance->measurement_client_configuration_descriptor_notify){ instance->measurement_callback.callback = &heart_rate_service_can_send_now; instance->measurement_callback.context = (void*) instance; att_server_register_can_send_now_callback(&instance->measurement_callback, instance->con_handle); } } void heart_rate_service_server_update_heart_rate_values(uint16_t bits_per_minute, heart_rate_service_sensor_contact_status_t sensor_contact, int rr_interval_count, uint16_t * rr_intervals){ heart_rate_t * instance = &heart_rate; instance->measurement_bpm = bits_per_minute; instance->sensor_contact = sensor_contact; instance->rr_interval_count = rr_interval_count; instance->rr_intervals = rr_intervals; instance->rr_offset = 0; if (instance->measurement_client_configuration_descriptor_notify){ instance->measurement_callback.callback = &heart_rate_service_can_send_now; instance->measurement_callback.context = (void*) instance; att_server_register_can_send_now_callback(&instance->measurement_callback, instance->con_handle); } }