1 /******************************************************************************
2 *
3 * Copyright (C) 2014 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include "base.h"
20 #include "btcore/include/bdaddr.h"
21 #include "support/callbacks.h"
22 #include "support/gatt.h"
23
24 const btgatt_interface_t *gatt_interface;
25 static bt_bdaddr_t remote_bd_addr;
26 static int gatt_client_interface;
27 static int gatt_server_interface;
28 static int gatt_service_handle;
29 static int gatt_included_service_handle;
30 static int gatt_characteristic_handle;
31 static int gatt_descriptor_handle;
32 static int gatt_connection_id;
33 static int gatt_status;
34
gatt_init()35 bool gatt_init() {
36 gatt_interface = bt_interface->get_profile_interface(BT_PROFILE_GATT_ID);
37 return gatt_interface->init(callbacks_get_gatt_struct()) == BT_STATUS_SUCCESS;
38 }
39
gatt_get_connection_id()40 int gatt_get_connection_id() {
41 return gatt_connection_id;
42 }
43
gatt_get_client_interface()44 int gatt_get_client_interface() {
45 return gatt_client_interface;
46 }
47
gatt_get_server_interface()48 int gatt_get_server_interface() {
49 return gatt_server_interface;
50 }
51
gatt_get_service_handle()52 int gatt_get_service_handle() {
53 return gatt_service_handle;
54 }
55
gatt_get_included_service_handle()56 int gatt_get_included_service_handle() {
57 return gatt_included_service_handle;
58 }
59
gatt_get_characteristic_handle()60 int gatt_get_characteristic_handle() {
61 return gatt_characteristic_handle;
62 }
63
gatt_get_descriptor_handle()64 int gatt_get_descriptor_handle() {
65 return gatt_descriptor_handle;
66 }
67
gatt_get_status()68 int gatt_get_status() {
69 return gatt_status;
70 }
71
72 // GATT client callbacks
btgattc_register_app_cb(int status,int clientIf,bt_uuid_t * app_uuid)73 void btgattc_register_app_cb(int status, int clientIf, bt_uuid_t *app_uuid) {
74 gatt_status = status;
75 gatt_client_interface = clientIf;
76 CALLBACK_RET();
77 }
78
btgattc_scan_result_cb(bt_bdaddr_t * bda,int rssi,uint8_t * adv_data)79 void btgattc_scan_result_cb(bt_bdaddr_t* bda, int rssi, uint8_t* adv_data) {
80 CALLBACK_RET();
81 }
82
btgattc_open_cb(int conn_id,int status,int clientIf,bt_bdaddr_t * bda)83 void btgattc_open_cb(int conn_id, int status, int clientIf, bt_bdaddr_t* bda) {
84 CALLBACK_RET();
85 }
86
btgattc_close_cb(int conn_id,int status,int clientIf,bt_bdaddr_t * bda)87 void btgattc_close_cb(int conn_id, int status, int clientIf, bt_bdaddr_t* bda) {
88 CALLBACK_RET();
89 }
90
btgattc_search_complete_cb(int conn_id,int status)91 void btgattc_search_complete_cb(int conn_id, int status) {
92 CALLBACK_RET();
93 }
94
btgattc_search_result_cb(int conn_id,btgatt_srvc_id_t * srvc_id)95 void btgattc_search_result_cb(int conn_id, btgatt_srvc_id_t *srvc_id) {
96 CALLBACK_RET();
97 }
98
btgattc_get_characteristic_cb(int conn_id,int status,btgatt_srvc_id_t * srvc_id,btgatt_gatt_id_t * char_id,int char_prop)99 void btgattc_get_characteristic_cb(int conn_id, int status, btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id, int char_prop) {
100 CALLBACK_RET();
101 }
102
btgattc_get_descriptor_cb(int conn_id,int status,btgatt_srvc_id_t * srvc_id,btgatt_gatt_id_t * char_id,btgatt_gatt_id_t * descr_id)103 void btgattc_get_descriptor_cb(int conn_id, int status, btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id) {
104 CALLBACK_RET();
105 }
106
btgattc_get_included_service_cb(int conn_id,int status,btgatt_srvc_id_t * srvc_id,btgatt_srvc_id_t * incl_srvc_id)107 void btgattc_get_included_service_cb(int conn_id, int status, btgatt_srvc_id_t *srvc_id, btgatt_srvc_id_t *incl_srvc_id) {
108 CALLBACK_RET();
109 }
110
btgattc_register_for_notification_cb(int conn_id,int registered,int status,btgatt_srvc_id_t * srvc_id,btgatt_gatt_id_t * char_id)111 void btgattc_register_for_notification_cb(int conn_id, int registered, int status, btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id) {
112 CALLBACK_RET();
113 }
114
btgattc_notify_cb(int conn_id,btgatt_notify_params_t * p_data)115 void btgattc_notify_cb(int conn_id, btgatt_notify_params_t *p_data) {
116 CALLBACK_RET();
117 }
118
btgattc_read_characteristic_cb(int conn_id,int status,btgatt_read_params_t * p_data)119 void btgattc_read_characteristic_cb(int conn_id, int status, btgatt_read_params_t *p_data) {
120 CALLBACK_RET();
121 }
122
btgattc_write_characteristic_cb(int conn_id,int status,btgatt_write_params_t * p_data)123 void btgattc_write_characteristic_cb(int conn_id, int status, btgatt_write_params_t *p_data) {
124 CALLBACK_RET();
125 }
126
btgattc_execute_write_cb(int conn_id,int status)127 void btgattc_execute_write_cb(int conn_id, int status) {
128 CALLBACK_RET();
129 }
130
btgattc_read_descriptor_cb(int conn_id,int status,btgatt_read_params_t * p_data)131 void btgattc_read_descriptor_cb(int conn_id, int status, btgatt_read_params_t *p_data) {
132 CALLBACK_RET();
133 }
134
btgattc_write_descriptor_cb(int conn_id,int status,btgatt_write_params_t * p_data)135 void btgattc_write_descriptor_cb(int conn_id, int status, btgatt_write_params_t *p_data) {
136 CALLBACK_RET();
137 }
138
btgattc_remote_rssi_cb(int client_if,bt_bdaddr_t * bda,int rssi,int status)139 void btgattc_remote_rssi_cb(int client_if,bt_bdaddr_t* bda, int rssi, int status) {
140 CALLBACK_RET();
141 }
142
btgattc_advertise_cb(int status,int client_if)143 void btgattc_advertise_cb(int status, int client_if) {
144 gatt_status = status;
145 gatt_client_interface = client_if;
146 CALLBACK_RET();
147 }
148
149 // GATT server callbacks
btgatts_register_app_cb(int status,int server_if,bt_uuid_t * uuid)150 void btgatts_register_app_cb(int status, int server_if, bt_uuid_t *uuid) {
151 gatt_status = status;
152 gatt_server_interface = server_if;
153 CALLBACK_RET();
154 }
155
btgatts_connection_cb(int conn_id,int server_if,int connected,bt_bdaddr_t * bda)156 void btgatts_connection_cb(int conn_id, int server_if, int connected, bt_bdaddr_t *bda) {
157 gatt_connection_id = conn_id;
158 for (int i = 0; i < 6; ++i) {
159 remote_bd_addr.address[i] = bda->address[i];
160 }
161 CALLBACK_RET();
162 }
163
btgatts_service_added_cb(int status,int server_if,btgatt_srvc_id_t * srvc_id,int srvc_handle)164 void btgatts_service_added_cb(int status, int server_if, btgatt_srvc_id_t *srvc_id, int srvc_handle) {
165 gatt_status = status;
166 gatt_server_interface = server_if;
167 gatt_service_handle = srvc_handle;
168 CALLBACK_RET();
169 }
170
btgatts_included_service_added_cb(int status,int server_if,int srvc_handle,int incl_srvc_handle)171 void btgatts_included_service_added_cb(int status, int server_if, int srvc_handle, int incl_srvc_handle) {
172 gatt_status = status;
173 gatt_server_interface = server_if;
174 gatt_service_handle = srvc_handle;
175 gatt_included_service_handle = incl_srvc_handle;
176 CALLBACK_RET();
177 }
178
btgatts_characteristic_added_cb(int status,int server_if,bt_uuid_t * char_id,int srvc_handle,int char_handle)179 void btgatts_characteristic_added_cb(int status, int server_if, bt_uuid_t *char_id, int srvc_handle, int char_handle) {
180 gatt_status = status;
181 gatt_server_interface = server_if;
182 gatt_service_handle = srvc_handle;
183 gatt_characteristic_handle = char_handle;
184 CALLBACK_RET();
185 }
186
btgatts_descriptor_added_cb(int status,int server_if,bt_uuid_t * descr_id,int srvc_handle,int descr_handle)187 void btgatts_descriptor_added_cb(int status, int server_if, bt_uuid_t *descr_id, int srvc_handle, int descr_handle) {
188 gatt_status = status;
189 gatt_server_interface = server_if;
190 gatt_service_handle = srvc_handle;
191 gatt_descriptor_handle = descr_handle;
192 CALLBACK_RET();
193 }
194
btgatts_service_started_cb(int status,int server_if,int srvc_handle)195 void btgatts_service_started_cb(int status, int server_if, int srvc_handle) {
196 gatt_status = status;
197 gatt_server_interface = server_if;
198 gatt_service_handle = srvc_handle;
199 CALLBACK_RET();
200 }
201
btgatts_service_stopped_cb(int status,int server_if,int srvc_handle)202 void btgatts_service_stopped_cb(int status, int server_if, int srvc_handle) {
203 gatt_status = status;
204 gatt_server_interface = server_if;
205 gatt_service_handle = srvc_handle;
206 CALLBACK_RET();
207 }
208
btgatts_service_deleted_cb(int status,int server_if,int srvc_handle)209 void btgatts_service_deleted_cb(int status, int server_if, int srvc_handle) {
210 gatt_status = status;
211 gatt_server_interface = server_if;
212 gatt_service_handle = srvc_handle;
213 CALLBACK_RET();
214 }
215
btgatts_request_read_cb(int conn_id,int trans_id,bt_bdaddr_t * bda,int attr_handle,int offset,bool is_long)216 void btgatts_request_read_cb(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, bool is_long) {
217 CALLBACK_RET();
218 }
219
btgatts_request_write_cb(int conn_id,int trans_id,bt_bdaddr_t * bda,int attr_handle,int offset,int length,bool need_rsp,bool is_prep,uint8_t * value)220 void btgatts_request_write_cb(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, int length, bool need_rsp, bool is_prep, uint8_t* value) {
221 CALLBACK_RET();
222 }
223
btgatts_request_exec_write_cb(int conn_id,int trans_id,bt_bdaddr_t * bda,int exec_write)224 void btgatts_request_exec_write_cb(int conn_id, int trans_id, bt_bdaddr_t *bda, int exec_write) {
225 CALLBACK_RET();
226 }
227
btgatts_response_confirmation_cb(int status,int handle)228 void btgatts_response_confirmation_cb(int status, int handle) {
229 CALLBACK_RET();
230 }