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