4 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Anupam Roy <anupam.r@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include <hardware/bluetooth.h>
23 #include <hardware/bt_gatt.h>
30 #include "bt-hal-log.h"
31 #include "bt-hal-msg.h"
32 #include "bt-hal-event-receiver.h"
33 #include "bt-hal-adapter-le.h"
35 #include "bt-hal-gatt-client.h"
37 /************************************************************************************
39 ************************************************************************************/
40 extern const btgatt_callbacks_t *bt_gatt_callbacks;
41 #define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
43 ERR("%s: BTGATT not initialized", __FUNCTION__);\
44 return BT_STATUS_NOT_READY;\
46 DBG("%s", __FUNCTION__);\
52 } hal_registered_client_t;
55 int le_scan_type = BT_GATTC_LE_SCAN_TYPE_PASSIVE;
58 static handle_stack_msg event_cb = NULL;
60 /* To send stack event to hal-av handler */
61 void _bt_hal_register_gatt_client_handler_cb(handle_stack_msg cb)
66 void _bt_hal_unregister_gatt_client_handler_cb(void)
72 int _bt_hal_gatt_client_get_le_scan_type(void)
78 static gboolean __bt_hal_register_client_cb(gpointer user_data)
80 struct hal_ev_gatt_client_registered ev;
81 hal_registered_client_t *client_info = user_data;
83 /* Prepare to send AV connecting event */
84 memset(&ev, 0, sizeof(ev));
85 ev.status = BT_STATUS_SUCCESS;
86 ev.client_if = client_info->client_if;
87 memcpy(ev.app_uuid, client_info->uuid.uu, sizeof(ev.app_uuid));
90 ERR("GATT Callback not registered");
92 DBG("GATT client registered, client_if: [%d]", client_info->client_if);
93 event_cb(HAL_EV_GATT_CLIENT_REGISTERED, (void *)&ev, sizeof(ev));
100 /** Registers a GATT client application with the stack */
101 bt_status_t register_client(bt_uuid_t *uuid)
103 hal_registered_client_t *client_info = NULL;
105 client_info = g_malloc0(sizeof(hal_registered_client_t));
107 * TODO: Actual client_if will be used here later when GATT register_client
108 * is implemented compeltely. For now return 1 as client_if.
110 client_info->client_if = 1;
111 memcpy(client_info->uuid.uu, uuid->uu, sizeof(bt_uuid_t));
114 * As we need to provide async callback to user from HAL, simply schedule a
115 * callback method which will carry actual result
117 g_idle_add(__bt_hal_register_client_cb, (gpointer)client_info);
119 /* If available, then return success, else return error */
120 return BT_STATUS_SUCCESS;
123 /* TODO: APIs will be implemented in subsequent patches whenever required */
124 /** Unregister a client application from the stack */
125 bt_status_t unregister_client(int client_if)
128 return BT_STATUS_UNSUPPORTED;
131 /** Start or stop LE device scanning */
132 bt_status_t scan(int client_if, bool start)
139 ret = _bt_hal_adapter_le_start_scan();
141 ret = _bt_hal_adapter_le_stop_scan();
146 /** Create a connection to a remote LE or dual-mode device */
147 bt_status_t connect(int client_if, const bt_bdaddr_t *bd_addr,
151 return BT_STATUS_UNSUPPORTED;
154 /** Disconnect a remote device or cancel a pending connection */
155 bt_status_t disconnect(int client_if, const bt_bdaddr_t *bd_addr,
159 return BT_STATUS_UNSUPPORTED;
162 /** Clear the attribute cache for a given device */
163 bt_status_t refresh(int client_if, const bt_bdaddr_t *bd_addr)
166 return BT_STATUS_UNSUPPORTED;
170 * Enumerate all GATT services on a connected device.
171 * Optionally, the results can be filtered for a given UUID.
173 bt_status_t search_service(int conn_id, bt_uuid_t *filter_uuid)
176 return BT_STATUS_UNSUPPORTED;
180 * Enumerate included services for a given service.
181 * Set start_incl_srvc_id to NULL to get the first included service.
183 bt_status_t get_included_service(int conn_id, btgatt_srvc_id_t *srvc_id,
184 btgatt_srvc_id_t *start_incl_srvc_id)
187 return BT_STATUS_UNSUPPORTED;
191 * Enumerate characteristics for a given service.
192 * Set start_char_id to NULL to get the first characteristic.
194 bt_status_t get_characteristic(int conn_id,
195 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *start_char_id)
198 return BT_STATUS_UNSUPPORTED;
202 * Enumerate descriptors for a given characteristic.
203 * Set start_descr_id to NULL to get the first descriptor.
205 bt_status_t get_descriptor(int conn_id,
206 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
207 btgatt_gatt_id_t *start_descr_id)
210 return BT_STATUS_UNSUPPORTED;
213 /** Read a characteristic on a remote device */
214 bt_status_t read_characteristic(int conn_id,
215 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
219 return BT_STATUS_UNSUPPORTED;
222 /** Write a remote characteristic */
223 bt_status_t write_characteristic(int conn_id,
224 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
225 int write_type, int len, int auth_req,
229 return BT_STATUS_UNSUPPORTED;
232 /** Read the descriptor for a given characteristic */
233 bt_status_t read_descriptor(int conn_id,
234 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
235 btgatt_gatt_id_t *descr_id, int auth_req)
238 return BT_STATUS_UNSUPPORTED;
240 /** Write a remote descriptor for a given characteristic */
241 bt_status_t write_descriptor(int conn_id,
242 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
243 btgatt_gatt_id_t *descr_id, int write_type, int len,
244 int auth_req, char* p_value)
247 return BT_STATUS_UNSUPPORTED;
250 /** Execute a prepared write operation */
251 bt_status_t execute_write(int conn_id, int execute)
254 return BT_STATUS_UNSUPPORTED;
258 * Register to receive notifications or indications for a given
261 bt_status_t register_for_notification(int client_if,
262 const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
263 btgatt_gatt_id_t *char_id)
266 return BT_STATUS_UNSUPPORTED;
269 /** Deregister a previous request for notifications/indications */
270 bt_status_t deregister_for_notification(int client_if,
271 const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
272 btgatt_gatt_id_t *char_id)
275 return BT_STATUS_UNSUPPORTED;
278 /** Request RSSI for a given remote device */
279 bt_status_t read_remote_rssi(int client_if, const bt_bdaddr_t *bd_addr)
282 return BT_STATUS_UNSUPPORTED;
285 /** OTA firmware download */
286 bt_status_t ota_fw_update(int client_if, int conn_id, const bt_bdaddr_t *bd_addr, char* path)
289 return BT_STATUS_UNSUPPORTED;
292 /** Determine the type of the remote device (LE, BR/EDR, Dual-mode) */
293 int get_device_type(const bt_bdaddr_t *bd_addr)
296 return BT_STATUS_UNSUPPORTED;
299 /** Request a connection parameter update */
300 bt_status_t conn_parameter_update(bt_bdaddr_t *bd, int min_int, int max_int, int latency, int timeout)
303 return BT_STATUS_UNSUPPORTED;
306 /** Test mode interface */
307 bt_status_t test_command(int command, btgatt_test_params_t* params)
310 return BT_STATUS_UNSUPPORTED;
313 /** MTU Exchange request from client */
314 bt_status_t configure_mtu(int conn_id, int mtu)
317 return BT_STATUS_UNSUPPORTED;
320 /** Setup scan filter params */
321 bt_status_t scan_filter_param_setup(int client_if, int action, int filt_index, int feat_seln,
322 int list_logic_type, int filt_logic_type, int rssi_high_thres,
323 int rssi_low_thres, int dely_mode, int found_timeout,
324 int lost_timeout, int found_timeout_cnt)
327 return BT_STATUS_UNSUPPORTED;
331 /** Configure a scan filter condition */
332 bt_status_t scan_filter_add_remove(int client_if, int action, int filt_type,
333 int filt_index, int company_id,
334 int company_id_mask, const bt_uuid_t *p_uuid,
335 const bt_uuid_t *p_uuid_mask, const bt_bdaddr_t *bd_addr,
336 char addr_type, int data_len, char* p_data, int mask_len,
340 return BT_STATUS_UNSUPPORTED;
343 /** Clear all scan filter conditions for specific filter index*/
344 bt_status_t scan_filter_clear(int client_if, int filt_index)
347 return BT_STATUS_UNSUPPORTED;
350 /** Enable / disable scan filter feature*/
351 bt_status_t scan_filter_enable(int client_if, bool enable)
354 return BT_STATUS_UNSUPPORTED;
357 /** Sets the LE scan interval and window in units of N*0.625 msec */
359 bt_status_t set_scan_parameters(int scan_type, int scan_interval, int scan_window)
365 le_scan_type = scan_type;
366 ret = _bt_hal_adapter_le_set_scan_parameters(
367 scan_type, scan_interval, scan_window);
371 bt_status_t set_scan_parameters(int scan_interval, int scan_window)
374 return BT_STATUS_UNSUPPORTED;
378 /* Configure the batchscan storage */
379 bt_status_t batchscan_cfg_storage(int client_if, int batch_scan_full_max,
380 int batch_scan_trunc_max, int batch_scan_notify_threshold)
383 return BT_STATUS_UNSUPPORTED;
386 /* Enable batchscan */
387 bt_status_t batchscan_enb_batch_scan(int client_if, int scan_mode,
388 int scan_interval, int scan_window, int addr_type, int discard_rule)
391 return BT_STATUS_UNSUPPORTED;
394 /* Disable batchscan */
395 bt_status_t batchscan_dis_batch_scan(int client_if)
398 return BT_STATUS_UNSUPPORTED;
401 /* Read out batchscan reports */
402 bt_status_t batchscan_read_reports(int client_if, int scan_mode)
405 return BT_STATUS_UNSUPPORTED;
408 const btgatt_client_interface_t btgatt_client_interface = {
416 get_included_service,
420 write_characteristic,
424 register_for_notification,
425 deregister_for_notification,
429 conn_parameter_update,
432 scan_filter_param_setup,
433 scan_filter_add_remove,
437 batchscan_cfg_storage,
438 batchscan_enb_batch_scan,
439 batchscan_dis_batch_scan,
440 batchscan_read_reports