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 #ifndef ANDROID_INCLUDE_BT_GATT_SERVER_H
18 #define ANDROID_INCLUDE_BT_GATT_SERVER_H
19 
20 #include <raw_address.h>
21 #include <stdint.h>
22 
23 #include "bt_gatt_types.h"
24 
25 __BEGIN_DECLS
26 
27 /** GATT value type used in response to remote read requests */
28 typedef struct {
29   uint8_t value[GATT_MAX_ATTR_LEN];
30   uint16_t handle;
31   uint16_t offset;
32   uint16_t len;
33   uint8_t auth_req;
34 } btgatt_value_t;
35 
36 /** GATT remote read request response type */
37 typedef union {
38   btgatt_value_t attr_value;
39   uint16_t handle;
40 } btgatt_response_t;
41 
42 /** BT-GATT Server callback structure. */
43 
44 /** Callback invoked in response to register_server */
45 typedef void (*register_server_callback)(int status, int server_if,
46                                          const bluetooth::Uuid& app_uuid);
47 
48 /** Callback indicating that a remote device has connected or been disconnected
49  */
50 typedef void (*connection_callback)(int conn_id, int server_if, int connected,
51                                     const RawAddress& bda);
52 
53 /** Callback invoked in response to create_service */
54 typedef void (*service_added_callback)(int status, int server_if,
55                                        const btgatt_db_element_t* service,
56                                        size_t service_count);
57 
58 /** Callback invoked in response to stop_service */
59 typedef void (*service_stopped_callback)(int status, int server_if,
60                                          int srvc_handle);
61 
62 /** Callback triggered when a service has been deleted */
63 typedef void (*service_deleted_callback)(int status, int server_if,
64                                          int srvc_handle);
65 
66 /**
67  * Callback invoked when a remote device has requested to read a characteristic
68  * or descriptor. The application must respond by calling send_response
69  */
70 typedef void (*request_read_callback)(int conn_id, int trans_id,
71                                       const RawAddress& bda, int attr_handle,
72                                       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,
79                                        const RawAddress& bda, int attr_handle,
80                                        int offset, bool need_rsp, bool is_prep,
81                                        const uint8_t* value, size_t length);
82 
83 /** Callback invoked when a previously prepared write is to be executed */
84 typedef void (*request_exec_write_callback)(int conn_id, int trans_id,
85                                             const RawAddress& bda,
86                                             int exec_write);
87 
88 /**
89  * Callback triggered in response to send_response if the remote device
90  * sends a confirmation.
91  */
92 typedef void (*response_confirmation_callback)(int status, int handle);
93 
94 /**
95  * Callback confirming that a notification or indication has been sent
96  * to a remote device.
97  */
98 typedef void (*indication_sent_callback)(int conn_id, int status);
99 
100 /**
101  * Callback notifying an application that a remote device connection is
102  * currently congested and cannot receive any more data. An application should
103  * avoid sending more data until a further callback is received indicating the
104  * congestion status has been cleared.
105  */
106 typedef void (*congestion_callback)(int conn_id, bool congested);
107 
108 /** Callback invoked when the MTU for a given connection changes */
109 typedef void (*mtu_changed_callback)(int conn_id, int mtu);
110 
111 /** Callback invoked when the PHY for a given connection changes */
112 typedef void (*phy_updated_callback)(int conn_id, uint8_t tx_phy,
113                                      uint8_t rx_phy, uint8_t status);
114 
115 /** Callback invoked when the connection parameters for a given connection
116  * changes */
117 typedef void (*conn_updated_callback)(int conn_id, uint16_t interval,
118                                       uint16_t latency, uint16_t timeout,
119                                       uint8_t status);
120 
121 /** Callback invoked when the subrate change event for a given connection
122  * is received */
123 typedef void (*subrate_change_callback)(int conn_id, uint16_t subrate_factor,
124                                         uint16_t latency, uint16_t cont_num,
125                                         uint16_t timeout, uint8_t status);
126 typedef struct {
127   register_server_callback register_server_cb;
128   connection_callback connection_cb;
129   service_added_callback service_added_cb;
130   service_stopped_callback service_stopped_cb;
131   service_deleted_callback service_deleted_cb;
132   request_read_callback request_read_characteristic_cb;
133   request_read_callback request_read_descriptor_cb;
134   request_write_callback request_write_characteristic_cb;
135   request_write_callback request_write_descriptor_cb;
136   request_exec_write_callback request_exec_write_cb;
137   response_confirmation_callback response_confirmation_cb;
138   indication_sent_callback indication_sent_cb;
139   congestion_callback congestion_cb;
140   mtu_changed_callback mtu_changed_cb;
141   phy_updated_callback phy_updated_cb;
142   conn_updated_callback conn_updated_cb;
143   subrate_change_callback subrate_chg_cb;
144 } btgatt_server_callbacks_t;
145 
146 /** Represents the standard BT-GATT server interface. */
147 typedef struct {
148   /** Registers a GATT server application with the stack */
149   bt_status_t (*register_server)(const bluetooth::Uuid& uuid,
150                                  bool eatt_support);
151 
152   /** Unregister a server application from the stack */
153   bt_status_t (*unregister_server)(int server_if);
154 
155   /** Create a connection to a remote peripheral */
156   bt_status_t (*connect)(int server_if, const RawAddress& bd_addr,
157                          uint8_t addr_type, bool is_direct, int transport);
158 
159   /** Disconnect an established connection or cancel a pending one */
160   bt_status_t (*disconnect)(int server_if, const RawAddress& bd_addr,
161                             int conn_id);
162 
163   /** Create a new service */
164   bt_status_t (*add_service)(int server_if, const btgatt_db_element_t* service,
165                              size_t service_count);
166 
167   /** Stops a local service */
168   bt_status_t (*stop_service)(int server_if, int service_handle);
169 
170   /** Delete a local service */
171   bt_status_t (*delete_service)(int server_if, int service_handle);
172 
173   /** Send value indication to a remote device */
174   bt_status_t (*send_indication)(int server_if, int attribute_handle,
175                                  int conn_id, int confirm, const uint8_t* value,
176                                  size_t length);
177 
178   /** Send a response to a read/write operation */
179   bt_status_t (*send_response)(int conn_id, int trans_id, int status,
180                                const btgatt_response_t& response);
181 
182   bt_status_t (*set_preferred_phy)(const RawAddress& bd_addr, uint8_t tx_phy,
183                                    uint8_t rx_phy, uint16_t phy_options);
184 
185   bt_status_t (*read_phy)(
186       const RawAddress& bd_addr,
187       base::Callback<void(uint8_t tx_phy, uint8_t rx_phy, uint8_t status)> cb);
188 
189 } btgatt_server_interface_t;
190 
191 __END_DECLS
192 
193 #endif /* ANDROID_INCLUDE_BT_GATT_CLIENT_H */
194