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 BLUEKITCHEN
24 * GMBH 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__ "tx_power_service_server.c"
39
40
41 /**
42 * Implementation of the GATT TX Power Service Server
43 * To use with your application, add `#import <tx_power_service.gatt>` to your .gatt file
44 */
45
46 #ifdef ENABLE_TESTING_SUPPORT
47 #include <stdio.h>
48 #endif
49
50 #include "btstack_defines.h"
51 #include "ble/att_db.h"
52 #include "ble/att_server.h"
53 #include "btstack_util.h"
54 #include "bluetooth_gatt.h"
55 #include "btstack_debug.h"
56
57
58 #include "ble/gatt-service/tx_power_service_server.h"
59
60 static btstack_context_callback_registration_t tx_power_level_callback;
61 static att_service_handler_t tx_power_service_handler;
62
63 static int8_t tx_power_level_value;
64 static uint16_t tx_power_level_value_handle;
65
66 static uint16_t tx_power_level_client_configuration;
67 static uint16_t tx_power_level_client_configuration_handle;
68 static hci_con_handle_t tx_power_level_client_configuration_con_handle;
69
tx_power_service_read_callback(hci_con_handle_t con_handle,uint16_t attribute_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)70 static uint16_t tx_power_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
71 UNUSED(con_handle);
72
73 if (attribute_handle == tx_power_level_value_handle){
74 return att_read_callback_handle_byte((uint8_t)tx_power_level_value, offset, buffer, buffer_size);
75 }
76 if (attribute_handle == tx_power_level_client_configuration_handle){
77 return att_read_callback_handle_little_endian_16(tx_power_level_client_configuration, offset, buffer, buffer_size);
78 }
79 return 0;
80 }
81
82
tx_power_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)83 static int tx_power_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){
84 UNUSED(offset);
85 UNUSED(buffer_size);
86 UNUSED(con_handle);
87
88 if (transaction_mode != ATT_TRANSACTION_MODE_NONE){
89 return 0;
90 }
91
92 if (attribute_handle == tx_power_level_client_configuration_handle){
93 tx_power_level_client_configuration = little_endian_read_16(buffer, 0);
94 tx_power_level_client_configuration_con_handle = con_handle;
95 }
96 return 0;
97 }
98
99
tx_power_service_server_init(int8_t tx_power_level_dBm)100 void tx_power_service_server_init(int8_t tx_power_level_dBm){
101 tx_power_level_value = tx_power_level_dBm;
102
103 // get service handle range
104 uint16_t start_handle = 0;
105 uint16_t end_handle = 0xffff;
106 bool service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_TX_POWER, &start_handle, &end_handle);
107 btstack_assert(service_found);
108 UNUSED(service_found);
109
110 // get characteristic value handle and client configuration handle
111 tx_power_level_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_TX_POWER_LEVEL);
112 tx_power_level_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_TX_POWER_LEVEL);
113
114 #ifdef ENABLE_TESTING_SUPPORT
115 printf("TXP 0x%02x - 0x%02x \n", start_handle, end_handle);
116 printf(" tx_power_level 0x%02x \n", tx_power_level_value_handle);
117 printf(" tx_power_level CCC 0x%02x \n", tx_power_level_client_configuration_handle);
118 #endif
119 log_info("TX Power Service Server 0x%02x-0x%02x", start_handle, end_handle);
120
121 // register service with ATT Server
122 tx_power_service_handler.start_handle = start_handle;
123 tx_power_service_handler.end_handle = end_handle;
124 tx_power_service_handler.read_callback = &tx_power_service_read_callback;
125 tx_power_service_handler.write_callback = &tx_power_service_write_callback;
126 att_server_register_service_handler(&tx_power_service_handler);
127 }
128
tx_power_service_can_send_now(void * context)129 static void tx_power_service_can_send_now(void * context){
130 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
131 uint8_t value = (uint8_t)tx_power_level_value;
132 att_server_notify(con_handle, tx_power_level_value_handle, &value, 1);
133 }
134
tx_power_service_server_set_level(int8_t tx_power_level_dBm)135 void tx_power_service_server_set_level(int8_t tx_power_level_dBm){
136 tx_power_level_value = tx_power_level_dBm;
137 if (tx_power_level_client_configuration != 0){
138 tx_power_level_callback.callback = &tx_power_service_can_send_now;
139 tx_power_level_callback.context = (void*) (uintptr_t) tx_power_level_client_configuration_con_handle;
140 att_server_register_can_send_now_callback(&tx_power_level_callback, tx_power_level_client_configuration_con_handle);
141 }
142 }
143