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
24const btgatt_interface_t *gatt_interface;
25static bt_bdaddr_t remote_bd_addr;
26static int gatt_client_interface;
27static int gatt_server_interface;
28static int gatt_service_handle;
29static int gatt_included_service_handle;
30static int gatt_characteristic_handle;
31static int gatt_descriptor_handle;
32static int gatt_connection_id;
33static int gatt_status;
34
35bool 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
40int gatt_get_connection_id() {
41  return gatt_connection_id;
42}
43
44int gatt_get_client_interface() {
45  return gatt_client_interface;
46}
47
48int gatt_get_server_interface() {
49  return gatt_server_interface;
50}
51
52int gatt_get_service_handle() {
53  return gatt_service_handle;
54}
55
56int gatt_get_included_service_handle() {
57  return gatt_included_service_handle;
58}
59
60int gatt_get_characteristic_handle() {
61  return gatt_characteristic_handle;
62}
63
64int gatt_get_descriptor_handle() {
65  return gatt_descriptor_handle;
66}
67
68int gatt_get_status() {
69  return gatt_status;
70}
71
72// GATT client callbacks
73void 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
79void btgattc_scan_result_cb(bt_bdaddr_t* bda, int rssi, uint8_t* adv_data) {
80  CALLBACK_RET();
81}
82
83void btgattc_open_cb(int conn_id, int status, int clientIf, bt_bdaddr_t* bda) {
84  CALLBACK_RET();
85}
86
87void btgattc_close_cb(int conn_id, int status, int clientIf, bt_bdaddr_t* bda) {
88  CALLBACK_RET();
89}
90
91void btgattc_search_complete_cb(int conn_id, int status) {
92  CALLBACK_RET();
93}
94
95void btgattc_search_result_cb(int conn_id, btgatt_srvc_id_t *srvc_id) {
96  CALLBACK_RET();
97}
98
99void 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
103void 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
107void 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
111void 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
115void btgattc_notify_cb(int conn_id, btgatt_notify_params_t *p_data) {
116  CALLBACK_RET();
117}
118
119void btgattc_read_characteristic_cb(int conn_id, int status, btgatt_read_params_t *p_data) {
120  CALLBACK_RET();
121}
122
123void btgattc_write_characteristic_cb(int conn_id, int status, btgatt_write_params_t *p_data) {
124  CALLBACK_RET();
125}
126
127void btgattc_execute_write_cb(int conn_id, int status) {
128  CALLBACK_RET();
129}
130
131void btgattc_read_descriptor_cb(int conn_id, int status, btgatt_read_params_t *p_data) {
132  CALLBACK_RET();
133}
134
135void btgattc_write_descriptor_cb(int conn_id, int status, btgatt_write_params_t *p_data) {
136  CALLBACK_RET();
137}
138
139void btgattc_remote_rssi_cb(int client_if,bt_bdaddr_t* bda, int rssi, int status) {
140  CALLBACK_RET();
141}
142
143void 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
150void 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
156void 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
164void 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
171void 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
179void 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
187void 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
195void 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
202void 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
209void 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
216void 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
220void 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
224void btgatts_request_exec_write_cb(int conn_id, int trans_id, bt_bdaddr_t *bda, int exec_write) {
225  CALLBACK_RET();
226}
227
228void btgatts_response_confirmation_cb(int status, int handle) {
229  CALLBACK_RET();
230}