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 3885a677ecSMatthias Ringwald /** 3985a677ecSMatthias Ringwald * Implementation of the GATT Battery Service Server 4085a677ecSMatthias Ringwald * To use with your application, add '#import <battery_service.gatt' to your .gatt file 4185a677ecSMatthias Ringwald */ 4285a677ecSMatthias Ringwald 4385a677ecSMatthias Ringwald #include "btstack_defines.h" 4485a677ecSMatthias Ringwald #include "ble/att_db.h" 4585a677ecSMatthias Ringwald #include "ble/att_server.h" 4685a677ecSMatthias Ringwald #include "btstack_util.h" 4785a677ecSMatthias Ringwald #include "bluetooth_gatt.h" 4885a677ecSMatthias Ringwald 4985a677ecSMatthias Ringwald #include "ble/gatt-service/battery_service_server.h" 5085a677ecSMatthias Ringwald 5185a677ecSMatthias Ringwald static btstack_context_callback_registration_t battery_callback; 5285a677ecSMatthias Ringwald static att_service_handler_t battery_service; 5385a677ecSMatthias Ringwald 5485a677ecSMatthias Ringwald static uint8_t battery_value; 5585a677ecSMatthias Ringwald static uint16_t battery_value_client_configuration; 5685a677ecSMatthias Ringwald static hci_con_handle_t battery_value_client_configuration_connection; 5785a677ecSMatthias Ringwald 5885a677ecSMatthias Ringwald static uint16_t battery_value_handle_value; 5985a677ecSMatthias Ringwald static uint16_t battery_value_handle_client_configuration; 6085a677ecSMatthias Ringwald 6185a677ecSMatthias Ringwald 6285a677ecSMatthias 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){ 6385a677ecSMatthias Ringwald if (attribute_handle == battery_value_handle_value){ 6485a677ecSMatthias Ringwald if (buffer) { 6585a677ecSMatthias Ringwald buffer[0] = battery_value; 6685a677ecSMatthias Ringwald } 6785a677ecSMatthias Ringwald return 1; 6885a677ecSMatthias Ringwald } 6985a677ecSMatthias Ringwald if (attribute_handle == battery_value_handle_client_configuration){ 7085a677ecSMatthias Ringwald if (buffer){ 7185a677ecSMatthias Ringwald little_endian_store_16(buffer, 0, battery_value_client_configuration); 7285a677ecSMatthias Ringwald battery_value_client_configuration_connection = con_handle; 7385a677ecSMatthias Ringwald } 7485a677ecSMatthias Ringwald return 2; 7585a677ecSMatthias Ringwald } 7685a677ecSMatthias Ringwald return 0; 7785a677ecSMatthias Ringwald } 7885a677ecSMatthias Ringwald 7985a677ecSMatthias 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){ 8085a677ecSMatthias Ringwald if (attribute_handle == battery_value_handle_client_configuration){ 8185a677ecSMatthias Ringwald battery_value_client_configuration = little_endian_read_16(buffer, 0); 8285a677ecSMatthias Ringwald } 8385a677ecSMatthias Ringwald return 0; 8485a677ecSMatthias Ringwald } 8585a677ecSMatthias Ringwald 8685a677ecSMatthias Ringwald static void battery_service_can_send_now(void * context){ 87*c61a5ea7SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 8885a677ecSMatthias Ringwald att_server_notify(con_handle, battery_value_handle_value, &battery_value, 1); 8985a677ecSMatthias Ringwald } 9085a677ecSMatthias Ringwald 9185a677ecSMatthias Ringwald void battery_service_server_init(uint8_t value){ 9285a677ecSMatthias Ringwald 9385a677ecSMatthias Ringwald battery_value = value; 9485a677ecSMatthias Ringwald 9585a677ecSMatthias Ringwald // get service handle range 9685a677ecSMatthias Ringwald uint16_t start_handle; 9785a677ecSMatthias Ringwald uint16_t end_handle; 9885a677ecSMatthias Ringwald int service_found = gatt_server_get_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE, &start_handle, &end_handle); 9985a677ecSMatthias Ringwald if (!service_found) return; 10085a677ecSMatthias Ringwald 10185a677ecSMatthias Ringwald // get characteristic value handle and client configuration handle 10285a677ecSMatthias Ringwald battery_value_handle_value = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 10385a677ecSMatthias 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); 10485a677ecSMatthias Ringwald 10585a677ecSMatthias Ringwald // register service with ATT DB 10685a677ecSMatthias Ringwald battery_service.start_handle = start_handle; 10785a677ecSMatthias Ringwald battery_service.end_handle = end_handle; 10885a677ecSMatthias Ringwald battery_service.read_callback = &battery_service_read_callback; 10985a677ecSMatthias Ringwald battery_service.write_callback = &battery_service_write_callback; 11085a677ecSMatthias Ringwald att_register_service_handler(&battery_service); 11185a677ecSMatthias Ringwald } 11285a677ecSMatthias Ringwald 11385a677ecSMatthias Ringwald void battery_service_server_set_battery_value(uint8_t value){ 11485a677ecSMatthias Ringwald battery_value = value; 11585a677ecSMatthias Ringwald if (battery_value_client_configuration){ 11685a677ecSMatthias Ringwald battery_callback.callback = &battery_service_can_send_now; 11785a677ecSMatthias Ringwald battery_callback.context = NULL; 11885a677ecSMatthias Ringwald att_server_register_can_send_now_callback(&battery_callback, battery_value_client_configuration_connection); 11985a677ecSMatthias Ringwald } 12085a677ecSMatthias Ringwald } 121