1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 18 #ifndef ANDROID_INCLUDE_BT_GATT_SERVER_H 19 #define ANDROID_INCLUDE_BT_GATT_SERVER_H 20 21 #include <stdint.h> 22 #include <vector> 23 24 #include "bt_gatt_types.h" 25 26 __BEGIN_DECLS 27 28 /** GATT value type used in response to remote read requests */ 29 typedef struct 30 { 31 uint8_t value[BTGATT_MAX_ATTR_LEN]; 32 uint16_t handle; 33 uint16_t offset; 34 uint16_t len; 35 uint8_t auth_req; 36 } btgatt_value_t; 37 38 /** GATT remote read request response type */ 39 typedef union 40 { 41 btgatt_value_t attr_value; 42 uint16_t handle; 43 } btgatt_response_t; 44 45 /** BT-GATT Server callback structure. */ 46 47 /** Callback invoked in response to register_server */ 48 typedef void (*register_server_callback)(int status, int server_if, 49 bt_uuid_t *app_uuid); 50 51 /** Callback indicating that a remote device has connected or been disconnected */ 52 typedef void (*connection_callback)(int conn_id, int server_if, int connected, 53 bt_bdaddr_t *bda); 54 55 /** Callback invoked in response to create_service */ 56 typedef void (*service_added_callback)(int status, int server_if, 57 std::vector<btgatt_db_element_t> service); 58 59 /** Callback invoked in response to stop_service */ 60 typedef void (*service_stopped_callback)(int status, int server_if, 61 int srvc_handle); 62 63 /** Callback triggered when a service has been deleted */ 64 typedef void (*service_deleted_callback)(int status, int server_if, 65 int srvc_handle); 66 67 /** 68 * Callback invoked when a remote device has requested to read a characteristic 69 * or descriptor. The application must respond by calling send_response 70 */ 71 typedef void (*request_read_callback)(int conn_id, int trans_id, bt_bdaddr_t *bda, 72 int attr_handle, int offset, bool is_long); 73 74 /** 75 * Callback invoked when a remote device has requested to write to a 76 * characteristic or descriptor. 77 */ 78 typedef void (*request_write_callback)(int conn_id, int trans_id, bt_bdaddr_t *bda, 79 int attr_handle, int offset, bool need_rsp, 80 bool is_prep, std::vector<uint8_t> value); 81 82 /** Callback invoked when a previously prepared write is to be executed */ 83 typedef void (*request_exec_write_callback)(int conn_id, int trans_id, 84 bt_bdaddr_t *bda, int exec_write); 85 86 /** 87 * Callback triggered in response to send_response if the remote device 88 * sends a confirmation. 89 */ 90 typedef void (*response_confirmation_callback)(int status, int handle); 91 92 /** 93 * Callback confirming that a notification or indication has been sent 94 * to a remote device. 95 */ 96 typedef void (*indication_sent_callback)(int conn_id, int status); 97 98 /** 99 * Callback notifying an application that a remote device connection is currently congested 100 * and cannot receive any more data. An application should avoid sending more data until 101 * a further callback is received indicating the congestion status has been cleared. 102 */ 103 typedef void (*congestion_callback)(int conn_id, bool congested); 104 105 /** Callback invoked when the MTU for a given connection changes */ 106 typedef void (*mtu_changed_callback)(int conn_id, int mtu); 107 108 /** Callback invoked when the PHY for a given connection changes */ 109 typedef void (*phy_updated_callback)(int conn_id, uint8_t tx_phy, 110 uint8_t rx_phy, uint8_t status); 111 112 /** Callback invoked when the connection parameters for a given connection changes */ 113 typedef void (*conn_updated_callback)(int conn_id, uint16_t interval, 114 uint16_t latency, uint16_t timeout, 115 uint8_t status); 116 typedef struct { 117 register_server_callback register_server_cb; 118 connection_callback connection_cb; 119 service_added_callback service_added_cb; 120 service_stopped_callback service_stopped_cb; 121 service_deleted_callback service_deleted_cb; 122 request_read_callback request_read_characteristic_cb; 123 request_read_callback request_read_descriptor_cb; 124 request_write_callback request_write_characteristic_cb; 125 request_write_callback request_write_descriptor_cb; 126 request_exec_write_callback request_exec_write_cb; 127 response_confirmation_callback response_confirmation_cb; 128 indication_sent_callback indication_sent_cb; 129 congestion_callback congestion_cb; 130 mtu_changed_callback mtu_changed_cb; 131 phy_updated_callback phy_updated_cb; 132 conn_updated_callback conn_updated_cb; 133 } btgatt_server_callbacks_t; 134 135 /** Represents the standard BT-GATT server interface. */ 136 typedef struct { 137 /** Registers a GATT server application with the stack */ 138 bt_status_t (*register_server)( bt_uuid_t *uuid ); 139 140 /** Unregister a server application from the stack */ 141 bt_status_t (*unregister_server)(int server_if ); 142 143 /** Create a connection to a remote peripheral */ 144 bt_status_t (*connect)(int server_if, const bt_bdaddr_t *bd_addr, 145 bool is_direct, int transport); 146 147 /** Disconnect an established connection or cancel a pending one */ 148 bt_status_t (*disconnect)(int server_if, const bt_bdaddr_t *bd_addr, 149 int conn_id ); 150 151 /** Create a new service */ 152 bt_status_t (*add_service)(int server_if, std::vector<btgatt_db_element_t> service); 153 154 /** Stops a local service */ 155 bt_status_t (*stop_service)(int server_if, int service_handle); 156 157 /** Delete a local service */ 158 bt_status_t (*delete_service)(int server_if, int service_handle); 159 160 /** Send value indication to a remote device */ 161 bt_status_t (*send_indication)(int server_if, int attribute_handle, 162 int conn_id, int confirm, 163 std::vector<uint8_t> value); 164 165 /** Send a response to a read/write operation */ 166 bt_status_t (*send_response)(int conn_id, int trans_id, 167 int status, btgatt_response_t *response); 168 169 bt_status_t (*set_preferred_phy)(int conn_id, uint8_t tx_phy, 170 uint8_t rx_phy, uint16_t phy_options); 171 172 bt_status_t (*read_phy)( 173 int conn_id, 174 base::Callback<void(uint8_t tx_phy, uint8_t rx_phy, uint8_t status)> 175 cb); 176 177 } btgatt_server_interface_t; 178 179 __END_DECLS 180 181 #endif /* ANDROID_INCLUDE_BT_GATT_CLIENT_H */ 182