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