1*85a677ecSMatthias Ringwald /* 2*85a677ecSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3*85a677ecSMatthias Ringwald * 4*85a677ecSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*85a677ecSMatthias Ringwald * modification, are permitted provided that the following conditions 6*85a677ecSMatthias Ringwald * are met: 7*85a677ecSMatthias Ringwald * 8*85a677ecSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*85a677ecSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*85a677ecSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*85a677ecSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*85a677ecSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*85a677ecSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*85a677ecSMatthias Ringwald * contributors may be used to endorse or promote products derived 15*85a677ecSMatthias Ringwald * from this software without specific prior written permission. 16*85a677ecSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*85a677ecSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*85a677ecSMatthias Ringwald * monetary gain. 19*85a677ecSMatthias Ringwald * 20*85a677ecSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*85a677ecSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*85a677ecSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*85a677ecSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*85a677ecSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*85a677ecSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*85a677ecSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*85a677ecSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*85a677ecSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*85a677ecSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*85a677ecSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*85a677ecSMatthias Ringwald * SUCH DAMAGE. 32*85a677ecSMatthias Ringwald * 33*85a677ecSMatthias Ringwald * Please inquire about commercial licensing options at 34*85a677ecSMatthias Ringwald * [email protected] 35*85a677ecSMatthias Ringwald * 36*85a677ecSMatthias Ringwald */ 37*85a677ecSMatthias Ringwald 38*85a677ecSMatthias Ringwald /** 39*85a677ecSMatthias Ringwald * Implementation of the GATT Battery Service Server 40*85a677ecSMatthias Ringwald * To use with your application, add '#import <battery_service.gatt' to your .gatt file 41*85a677ecSMatthias Ringwald */ 42*85a677ecSMatthias Ringwald 43*85a677ecSMatthias Ringwald #include "btstack_defines.h" 44*85a677ecSMatthias Ringwald #include "ble/att_db.h" 45*85a677ecSMatthias Ringwald #include "ble/att_server.h" 46*85a677ecSMatthias Ringwald #include "btstack_util.h" 47*85a677ecSMatthias Ringwald #include "bluetooth_gatt.h" 48*85a677ecSMatthias Ringwald 49*85a677ecSMatthias Ringwald #include "ble/gatt-service/battery_service_server.h" 50*85a677ecSMatthias Ringwald 51*85a677ecSMatthias Ringwald static btstack_context_callback_registration_t battery_callback; 52*85a677ecSMatthias Ringwald static att_service_handler_t battery_service; 53*85a677ecSMatthias Ringwald 54*85a677ecSMatthias Ringwald static uint8_t battery_value; 55*85a677ecSMatthias Ringwald static uint16_t battery_value_client_configuration; 56*85a677ecSMatthias Ringwald static hci_con_handle_t battery_value_client_configuration_connection; 57*85a677ecSMatthias Ringwald 58*85a677ecSMatthias Ringwald static uint16_t battery_value_handle_value; 59*85a677ecSMatthias Ringwald static uint16_t battery_value_handle_client_configuration; 60*85a677ecSMatthias Ringwald 61*85a677ecSMatthias Ringwald 62*85a677ecSMatthias 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){ 63*85a677ecSMatthias Ringwald if (attribute_handle == battery_value_handle_value){ 64*85a677ecSMatthias Ringwald if (buffer) { 65*85a677ecSMatthias Ringwald buffer[0] = battery_value; 66*85a677ecSMatthias Ringwald } 67*85a677ecSMatthias Ringwald return 1; 68*85a677ecSMatthias Ringwald } 69*85a677ecSMatthias Ringwald if (attribute_handle == battery_value_handle_client_configuration){ 70*85a677ecSMatthias Ringwald if (buffer){ 71*85a677ecSMatthias Ringwald little_endian_store_16(buffer, 0, battery_value_client_configuration); 72*85a677ecSMatthias Ringwald battery_value_client_configuration_connection = con_handle; 73*85a677ecSMatthias Ringwald } 74*85a677ecSMatthias Ringwald return 2; 75*85a677ecSMatthias Ringwald } 76*85a677ecSMatthias Ringwald return 0; 77*85a677ecSMatthias Ringwald } 78*85a677ecSMatthias Ringwald 79*85a677ecSMatthias 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){ 80*85a677ecSMatthias Ringwald if (attribute_handle == battery_value_handle_client_configuration){ 81*85a677ecSMatthias Ringwald battery_value_client_configuration = little_endian_read_16(buffer, 0); 82*85a677ecSMatthias Ringwald } 83*85a677ecSMatthias Ringwald return 0; 84*85a677ecSMatthias Ringwald } 85*85a677ecSMatthias Ringwald 86*85a677ecSMatthias Ringwald static void battery_service_can_send_now(void * context){ 87*85a677ecSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) context; 88*85a677ecSMatthias Ringwald att_server_notify(con_handle, battery_value_handle_value, &battery_value, 1); 89*85a677ecSMatthias Ringwald } 90*85a677ecSMatthias Ringwald 91*85a677ecSMatthias Ringwald void battery_service_server_init(uint8_t value){ 92*85a677ecSMatthias Ringwald 93*85a677ecSMatthias Ringwald battery_value = value; 94*85a677ecSMatthias Ringwald 95*85a677ecSMatthias Ringwald // get service handle range 96*85a677ecSMatthias Ringwald uint16_t start_handle; 97*85a677ecSMatthias Ringwald uint16_t end_handle; 98*85a677ecSMatthias Ringwald int service_found = gatt_server_get_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE, &start_handle, &end_handle); 99*85a677ecSMatthias Ringwald if (!service_found) return; 100*85a677ecSMatthias Ringwald 101*85a677ecSMatthias Ringwald // get characteristic value handle and client configuration handle 102*85a677ecSMatthias Ringwald battery_value_handle_value = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 103*85a677ecSMatthias 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); 104*85a677ecSMatthias Ringwald 105*85a677ecSMatthias Ringwald // register service with ATT DB 106*85a677ecSMatthias Ringwald battery_service.start_handle = start_handle; 107*85a677ecSMatthias Ringwald battery_service.end_handle = end_handle; 108*85a677ecSMatthias Ringwald battery_service.read_callback = &battery_service_read_callback; 109*85a677ecSMatthias Ringwald battery_service.write_callback = &battery_service_write_callback; 110*85a677ecSMatthias Ringwald att_register_service_handler(&battery_service); 111*85a677ecSMatthias Ringwald } 112*85a677ecSMatthias Ringwald 113*85a677ecSMatthias Ringwald void battery_service_server_set_battery_value(uint8_t value){ 114*85a677ecSMatthias Ringwald battery_value = value; 115*85a677ecSMatthias Ringwald if (battery_value_client_configuration){ 116*85a677ecSMatthias Ringwald battery_callback.callback = &battery_service_can_send_now; 117*85a677ecSMatthias Ringwald battery_callback.context = NULL; 118*85a677ecSMatthias Ringwald att_server_register_can_send_now_callback(&battery_callback, battery_value_client_configuration_connection); 119*85a677ecSMatthias Ringwald } 120*85a677ecSMatthias Ringwald } 121