185a677ecSMatthias Ringwald /* 285a677ecSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 385a677ecSMatthias Ringwald * 485a677ecSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 585a677ecSMatthias Ringwald * modification, are permitted provided that the following conditions 685a677ecSMatthias Ringwald * are met: 785a677ecSMatthias Ringwald * 885a677ecSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 985a677ecSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 1085a677ecSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 1185a677ecSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 1285a677ecSMatthias Ringwald * documentation and/or other materials provided with the distribution. 1385a677ecSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 1485a677ecSMatthias Ringwald * contributors may be used to endorse or promote products derived 1585a677ecSMatthias Ringwald * from this software without specific prior written permission. 1685a677ecSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 1785a677ecSMatthias Ringwald * personal benefit and not for any commercial purpose or for 1885a677ecSMatthias Ringwald * monetary gain. 1985a677ecSMatthias Ringwald * 2085a677ecSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 2185a677ecSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2285a677ecSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2385a677ecSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 2485a677ecSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2585a677ecSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2685a677ecSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 2785a677ecSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2885a677ecSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2985a677ecSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 3085a677ecSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3185a677ecSMatthias Ringwald * SUCH DAMAGE. 3285a677ecSMatthias Ringwald * 3385a677ecSMatthias Ringwald * Please inquire about commercial licensing options at 3485a677ecSMatthias Ringwald * [email protected] 3585a677ecSMatthias Ringwald * 3685a677ecSMatthias Ringwald */ 3785a677ecSMatthias Ringwald 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "battery_service_server.c" 39ab2c6ae4SMatthias Ringwald 4085a677ecSMatthias Ringwald /** 4185a677ecSMatthias Ringwald * Implementation of the GATT Battery Service Server 4285a677ecSMatthias Ringwald * To use with your application, add '#import <battery_service.gatt' to your .gatt file 4385a677ecSMatthias Ringwald */ 4485a677ecSMatthias Ringwald 4585a677ecSMatthias Ringwald #include "btstack_defines.h" 4685a677ecSMatthias Ringwald #include "ble/att_db.h" 4785a677ecSMatthias Ringwald #include "ble/att_server.h" 4885a677ecSMatthias Ringwald #include "btstack_util.h" 4985a677ecSMatthias Ringwald #include "bluetooth_gatt.h" 5085a677ecSMatthias Ringwald 5185a677ecSMatthias Ringwald #include "ble/gatt-service/battery_service_server.h" 5285a677ecSMatthias Ringwald 5385a677ecSMatthias Ringwald static btstack_context_callback_registration_t battery_callback; 5485a677ecSMatthias Ringwald static att_service_handler_t battery_service; 5585a677ecSMatthias Ringwald 5685a677ecSMatthias Ringwald static uint8_t battery_value; 5785a677ecSMatthias Ringwald static uint16_t battery_value_client_configuration; 5885a677ecSMatthias Ringwald static hci_con_handle_t battery_value_client_configuration_connection; 5985a677ecSMatthias Ringwald 6085a677ecSMatthias Ringwald static uint16_t battery_value_handle_value; 6185a677ecSMatthias Ringwald static uint16_t battery_value_handle_client_configuration; 6285a677ecSMatthias Ringwald 6385a677ecSMatthias Ringwald 6485a677ecSMatthias Ringwald static uint16_t battery_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ 65*ad5fb1efSMatthias Ringwald UNUSED(con_handle); 669ec2630cSMatthias Ringwald UNUSED(offset); 679ec2630cSMatthias Ringwald UNUSED(buffer_size); 689ec2630cSMatthias Ringwald 6985a677ecSMatthias Ringwald if (attribute_handle == battery_value_handle_value){ 7085a677ecSMatthias Ringwald if (buffer) { 7185a677ecSMatthias Ringwald buffer[0] = battery_value; 7285a677ecSMatthias Ringwald } 7385a677ecSMatthias Ringwald return 1; 7485a677ecSMatthias Ringwald } 7585a677ecSMatthias Ringwald if (attribute_handle == battery_value_handle_client_configuration){ 7685a677ecSMatthias Ringwald if (buffer){ 7785a677ecSMatthias Ringwald little_endian_store_16(buffer, 0, battery_value_client_configuration); 7885a677ecSMatthias Ringwald } 7985a677ecSMatthias Ringwald return 2; 8085a677ecSMatthias Ringwald } 8185a677ecSMatthias Ringwald return 0; 8285a677ecSMatthias Ringwald } 8385a677ecSMatthias Ringwald 8485a677ecSMatthias Ringwald static int battery_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){ 859ec2630cSMatthias Ringwald UNUSED(transaction_mode); 869ec2630cSMatthias Ringwald UNUSED(offset); 879ec2630cSMatthias Ringwald UNUSED(buffer_size); 889ec2630cSMatthias Ringwald 8985a677ecSMatthias Ringwald if (attribute_handle == battery_value_handle_client_configuration){ 9085a677ecSMatthias Ringwald battery_value_client_configuration = little_endian_read_16(buffer, 0); 911034f066SMatthias Ringwald battery_value_client_configuration_connection = con_handle; 9285a677ecSMatthias Ringwald } 9385a677ecSMatthias Ringwald return 0; 9485a677ecSMatthias Ringwald } 9585a677ecSMatthias Ringwald 9685a677ecSMatthias Ringwald static void battery_service_can_send_now(void * context){ 97c61a5ea7SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 9885a677ecSMatthias Ringwald att_server_notify(con_handle, battery_value_handle_value, &battery_value, 1); 9985a677ecSMatthias Ringwald } 10085a677ecSMatthias Ringwald 10185a677ecSMatthias Ringwald void battery_service_server_init(uint8_t value){ 10285a677ecSMatthias Ringwald 10385a677ecSMatthias Ringwald battery_value = value; 10485a677ecSMatthias Ringwald 10585a677ecSMatthias Ringwald // get service handle range 106fecc923bSMatthias Ringwald uint16_t start_handle = 0; 107fecc923bSMatthias Ringwald uint16_t end_handle = 0xfff; 10885a677ecSMatthias Ringwald int service_found = gatt_server_get_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE, &start_handle, &end_handle); 10985a677ecSMatthias Ringwald if (!service_found) return; 11085a677ecSMatthias Ringwald 11185a677ecSMatthias Ringwald // get characteristic value handle and client configuration handle 11285a677ecSMatthias Ringwald battery_value_handle_value = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 11385a677ecSMatthias Ringwald battery_value_handle_client_configuration = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 11485a677ecSMatthias Ringwald 1153d71c7a4SMatthias Ringwald // register service with ATT Server 11685a677ecSMatthias Ringwald battery_service.start_handle = start_handle; 11785a677ecSMatthias Ringwald battery_service.end_handle = end_handle; 11885a677ecSMatthias Ringwald battery_service.read_callback = &battery_service_read_callback; 11985a677ecSMatthias Ringwald battery_service.write_callback = &battery_service_write_callback; 1203d71c7a4SMatthias Ringwald att_server_register_service_handler(&battery_service); 12185a677ecSMatthias Ringwald } 12285a677ecSMatthias Ringwald 12385a677ecSMatthias Ringwald void battery_service_server_set_battery_value(uint8_t value){ 12485a677ecSMatthias Ringwald battery_value = value; 12585a677ecSMatthias Ringwald if (battery_value_client_configuration){ 12685a677ecSMatthias Ringwald battery_callback.callback = &battery_service_can_send_now; 1273bdbb0eaSMatthias Ringwald battery_callback.context = (void*) (uintptr_t) battery_value_client_configuration_connection; 12885a677ecSMatthias Ringwald att_server_register_can_send_now_callback(&battery_callback, battery_value_client_configuration_connection); 12985a677ecSMatthias Ringwald } 13085a677ecSMatthias Ringwald } 131