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