xref: /btstack/src/ble/gatt-service/battery_service_server.c (revision 67b65e8f108eb716cb65c6192e677b0f719b3e70)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "battery_service_server.c"
39ab2c6ae4SMatthias Ringwald 
4085a677ecSMatthias Ringwald /**
4185a677ecSMatthias Ringwald  * Implementation of the GATT Battery Service Server
42*67b65e8fSChristian Erhardt  * 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"
502e5374e1SMilanka Ringwald #include "btstack_debug.h"
512e5374e1SMilanka Ringwald 
5285a677ecSMatthias Ringwald 
5385a677ecSMatthias Ringwald #include "ble/gatt-service/battery_service_server.h"
5485a677ecSMatthias Ringwald 
5585a677ecSMatthias Ringwald static btstack_context_callback_registration_t  battery_callback;
5685a677ecSMatthias Ringwald static att_service_handler_t       battery_service;
5785a677ecSMatthias Ringwald 
5885a677ecSMatthias Ringwald static uint8_t 	battery_value;
5985a677ecSMatthias Ringwald static uint16_t battery_value_client_configuration;
6085a677ecSMatthias Ringwald static hci_con_handle_t battery_value_client_configuration_connection;
6185a677ecSMatthias Ringwald 
62fc8e1652SMilanka Ringwald static uint16_t battery_value_handle;
63fc8e1652SMilanka Ringwald static uint16_t battery_value_client_configuration_handle;
6485a677ecSMatthias Ringwald 
6585a677ecSMatthias Ringwald 
battery_service_read_callback(hci_con_handle_t con_handle,uint16_t attribute_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)6685a677ecSMatthias 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){
67ad5fb1efSMatthias Ringwald 	UNUSED(con_handle);
689ec2630cSMatthias Ringwald 
69fc8e1652SMilanka Ringwald 	if (attribute_handle == battery_value_handle){
7041abe6e6SMatthias Ringwald 		return att_read_callback_handle_byte(battery_value, offset, buffer, buffer_size);
7185a677ecSMatthias Ringwald 	}
72fc8e1652SMilanka Ringwald 	if (attribute_handle == battery_value_client_configuration_handle){
7341abe6e6SMatthias Ringwald 		return att_read_callback_handle_little_endian_16(battery_value_client_configuration, offset, buffer, buffer_size);
7485a677ecSMatthias Ringwald 	}
7585a677ecSMatthias Ringwald 	return 0;
7685a677ecSMatthias Ringwald }
7785a677ecSMatthias Ringwald 
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)7885a677ecSMatthias 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){
799ec2630cSMatthias Ringwald 	UNUSED(offset);
809ec2630cSMatthias Ringwald 	UNUSED(buffer_size);
819ec2630cSMatthias Ringwald 
82689e7fb3SMatthias Ringwald     if (transaction_mode != ATT_TRANSACTION_MODE_NONE){
83689e7fb3SMatthias Ringwald         return 0;
84689e7fb3SMatthias Ringwald     }
85689e7fb3SMatthias Ringwald 
86fc8e1652SMilanka Ringwald     if (attribute_handle == battery_value_client_configuration_handle){
8785a677ecSMatthias Ringwald 		battery_value_client_configuration = little_endian_read_16(buffer, 0);
881034f066SMatthias Ringwald 		battery_value_client_configuration_connection = con_handle;
8985a677ecSMatthias Ringwald 	}
9085a677ecSMatthias Ringwald 	return 0;
9185a677ecSMatthias Ringwald }
9285a677ecSMatthias Ringwald 
battery_service_can_send_now(void * context)9385a677ecSMatthias Ringwald static void battery_service_can_send_now(void * context){
94c61a5ea7SMatthias Ringwald 	hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
95fc8e1652SMilanka Ringwald 	att_server_notify(con_handle, battery_value_handle, &battery_value, 1);
9685a677ecSMatthias Ringwald }
9785a677ecSMatthias Ringwald 
battery_service_server_init(uint8_t value)9885a677ecSMatthias Ringwald void battery_service_server_init(uint8_t value){
9985a677ecSMatthias Ringwald 
10085a677ecSMatthias Ringwald 	battery_value = value;
10185a677ecSMatthias Ringwald 
10285a677ecSMatthias Ringwald 	// get service handle range
103fecc923bSMatthias Ringwald 	uint16_t start_handle = 0;
104843359edSMatthias Ringwald 	uint16_t end_handle   = 0xffff;
105c436b760SMilanka Ringwald 	int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE, &start_handle, &end_handle);
106a2489f29SMatthias Ringwald 	btstack_assert(service_found != 0);
107a2489f29SMatthias Ringwald 	UNUSED(service_found);
10885a677ecSMatthias Ringwald 
10985a677ecSMatthias Ringwald 	// get characteristic value handle and client configuration handle
110fc8e1652SMilanka Ringwald 	battery_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
111fc8e1652SMilanka Ringwald 	battery_value_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
11285a677ecSMatthias Ringwald 
1133d71c7a4SMatthias Ringwald 	// register service with ATT Server
11485a677ecSMatthias Ringwald 	battery_service.start_handle   = start_handle;
11585a677ecSMatthias Ringwald 	battery_service.end_handle     = end_handle;
11685a677ecSMatthias Ringwald 	battery_service.read_callback  = &battery_service_read_callback;
11785a677ecSMatthias Ringwald 	battery_service.write_callback = &battery_service_write_callback;
1183d71c7a4SMatthias Ringwald 	att_server_register_service_handler(&battery_service);
11985a677ecSMatthias Ringwald }
12085a677ecSMatthias Ringwald 
battery_service_server_set_battery_value(uint8_t value)12185a677ecSMatthias Ringwald void battery_service_server_set_battery_value(uint8_t value){
12285a677ecSMatthias Ringwald 	battery_value = value;
1239305033eSMatthias Ringwald 	if (battery_value_client_configuration != 0){
12485a677ecSMatthias Ringwald 		battery_callback.callback = &battery_service_can_send_now;
1253bdbb0eaSMatthias Ringwald 		battery_callback.context  = (void*) (uintptr_t) battery_value_client_configuration_connection;
12685a677ecSMatthias Ringwald 		att_server_register_can_send_now_callback(&battery_callback, battery_value_client_configuration_connection);
12785a677ecSMatthias Ringwald 	}
12885a677ecSMatthias Ringwald }
129