1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
24 * This file contains the functionalities of GATT Client. Functionalities
25 * like LE device discovery, connecting to the LE device with OIC service,
26 * registering to the service and there characteristics, registering to the
27 * change in the charateristics, setting the value of the characteristcs
28 * for the request and response will be done here.
31 #ifndef TZ_BLE_CLIENT_H_
32 #define TZ_BLE_CLIENT_H_
34 #include <bluetooth.h>
35 #include <bluetooth_type.h>
36 #include <bluetooth_product.h>
39 #include "caadapterutils.h"
40 #include "cableutil.h"
41 #include "caadapterinterface.h"
43 #include "cathreadpool.h"
44 #include "caleinterface.h"
45 #include "oic_malloc.h"
49 * This is the callback which will be called after the characteristic
50 * value changes happen.
52 * @param[in] characteristic The attribute handle of characteristic.
53 * @param[in] value Value of the characteristics of a service.
54 * @param[in] valueLen length of data.
55 * @param[in] userData The user data passed from the request function.
57 void CABleGattCharacteristicChangedCb(bt_gatt_attribute_h characteristic,
62 * This is the callback which will be called after the characteristics changed.
64 * @param[in] result result of write value.
65 * @param[in] userData user context.
67 void CABleGattCharacteristicWriteCb(int result, void *userData);
70 * This is the callback which will be called when descriptor of
71 * characteristics is found.
73 * @param[in] result The result of discovering.
74 * @param[in] format format of descriptor.
75 * @param[in] total The total number of descriptor in a
77 * @param[in] descriptor The attribute handle of descriptor.
78 * @param[in] characteristic The attribute handle of characteristic.
79 * @param[in] userData The user data passed from the request function.
81 void CABleGattDescriptorDiscoveredCb(int result, unsigned char format, int total,
82 bt_gatt_attribute_h descriptor,
83 bt_gatt_attribute_h characteristic, void *userData);
86 * This is the callback which will be called after the characteristics are
87 * discovered by bt_gatt_discover_characteristics().
89 * @param[in] result The result of discovering.
90 * @param[in] inputIndex The index of characteristics in a service,
92 * @param[in] total The total number of characteristics in a service.
93 * @param[in] characteristic The attribute handle of characteristic.
94 * @param[in] userData The user data passed from the request function.
96 * @return 0 on failure and 1 on success.
98 bool CABleGattCharacteristicsDiscoveredCb(int result, int inputIndex, int total,
99 bt_gatt_attribute_h characteristic, void *userData);
102 * This is the callback which will be called when we get the primary
103 * services repeatedly.
105 * @param[in] service The attribute handle of service. Unique identifier
107 * @param[in] index The current index of the service.
108 * @param[in] count Total number of services available in remote device.
109 * @param[in] userData user data.
111 * @return 0 on failure and 1 on success.
113 bool CABleGattPrimaryServiceCb(bt_gatt_attribute_h service, int index, int count,
117 * This is the callback which will be called whenever there is change in
118 * gatt connection with server(Connected/Disconnected)
120 * @param[in] result The result of discovering.
121 * @param[in] connected State of connection.
122 * @param[in] remoteAddress Mac address of the remote device in which we
124 * @param[in] userData The user data passed from the request function.
126 void CABleGattConnectionStateChangedCb(int result, bool connected,
127 const char *remoteAddress,void *userData);
130 * This is the callback which will be called when the device discovery
133 * @param[in] result The result of discovering.
134 * @param[in] discoveryState State of the discovery(FOUND/STARTED/ FINISHED).
135 * @param[in] discoveryInfo Remote Device information.
136 * @param[in] userData The user data passed from the request function.
138 void CABtAdapterLeDeviceDiscoveryStateChangedCb(int result,
139 bt_adapter_le_device_discovery_state_e discoveryState,
140 bt_adapter_le_device_discovery_info_s *discoveryInfo,
144 * Used to print device information(Util method).
145 * @param[in] discoveryInfo Device information structure.
147 void CAPrintDiscoveryInformation(const bt_adapter_le_device_discovery_info_s *discoveryInfo);
150 * This thread will be used to initialize the Gatt Client and start device
152 * 1. Set scan parameters.
153 * 2. Setting neccessary callbacks for connection, characteristics
154 * changed and discovery.
155 * 3. Start device discovery.
157 * @param[in] data Currently it will be NULL(no parameter).
159 void CAStartBleGattClientThread(void *data);
162 * Used to initialize all required mutex variable for Gatt Client
165 * @return ::CA_STATUS_OK or Appropriate error code.
166 * @retval ::CA_STATUS_OK Successful.
167 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
168 * @retval ::CA_STATUS_FAILED Operation failed.
170 CAResult_t CAInitGattClientMutexVariables();
173 * Used to terminate all required mutex variable for Gatt Client implementation.
175 void CATerminateGattClientMutexVariables();
178 * Used to clear NonOICDeviceList.
180 void CAClearNonOICDeviceList();
183 * Used to set scan parameter of starting discovery.
185 * @return ::CA_STATUS_OK or Appropriate error code.
186 * @retval ::CA_STATUS_OK Successful.
187 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
188 * @retval ::CA_STATUS_FAILED Operation failed.
190 CAResult_t CABleGattSetScanParameter();
193 * Used to register required callbacks to BLE platform(connection,
196 * @return ::CA_STATUS_OK or Appropriate error code.
197 * @retval ::CA_STATUS_OK Successful.
198 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
199 * @retval ::CA_STATUS_FAILED Operation failed.
201 CAResult_t CABleGattSetCallbacks();
204 * Used to unset all the registerd callbacks to BLE platform.
206 void CABleGattUnSetCallbacks();
209 * Used to watch all the changes happening in characteristics of the service.
211 * @param[in] service The attribute handle of the service.
213 * @return ::CA_STATUS_OK or Appropriate error code.
214 * @retval ::CA_STATUS_OK Successful.
215 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
216 * @retval ::CA_STATUS_FAILED Operation failed.
218 CAResult_t CABleGattWatchCharacteristicChanges(bt_gatt_attribute_h service);
221 * Used to unwatch characteristics changes using
222 * bt_gatt_unwatch_characteristic_changes().
224 void CABleGattUnWatchCharacteristicChanges();
227 * Used to start LE discovery for BLE devices.
229 * @return ::CA_STATUS_OK or Appropriate error code.
230 * @retval ::CA_STATUS_OK Successful.
231 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
232 * @retval ::CA_STATUS_FAILED Operation failed.
234 CAResult_t CABleGattStartDeviceDiscovery();
237 * Used to stop LE discovery for BLE devices.
239 void CABleGattStopDeviceDiscovery();
242 * This is the thread which will be used for making gatt connection with
244 * @param[in] remoteAddress MAC address of remote device to connect.
246 void CAGattConnectThread (void *remoteAddress);
249 * Used to do connection with remote device.
251 * @param[in] remoteAddress Remote address inwhich we wants to connect with.
253 * @return ::CA_STATUS_OK or Appropriate error code.
254 * @retval ::CA_STATUS_OK Successful.
255 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
256 * @retval ::CA_STATUS_FAILED Operation failed.
258 CAResult_t CABleGattConnect(const char *remoteAddress);
261 * Used to do disconnection with remote device.
262 * @param[in] remoteAddress Remote address inwhich we wants to disconnect with.
264 * @return ::CA_STATUS_OK or Appropriate error code.
265 * @retval ::CA_STATUS_OK Successful.
266 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
267 * @retval ::CA_STATUS_FAILED Operation failed.
269 CAResult_t CABleGattDisConnect(const char *remoteAddress);
272 * This is thread which will be spawned for discovering ble services. Once
273 * called discover api, then it will be terminated.
274 * @param[in] remoteAddress Mac address of the remote device in which we
275 * want to search services.
277 void CADiscoverBLEServicesThread (void *remoteAddress);
280 * Used to discover the services that is advertised by Gatt Server
283 * @param[in] remoteAddress MAC address of remote device in which we want
284 * to discover the services.
286 * @return ::CA_STATUS_OK or Appropriate error code.
287 * @retval ::CA_STATUS_OK Successful.
288 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
289 * @retval ::CA_STATUS_FAILED Operation failed.
291 CAResult_t CABleGattDiscoverServices(const char *remoteAddress);
294 * This is the thread which will be used for finding characteristic of a
297 * @param[in] stServiceInfo Service Information which contains the remote
298 * address, service handle and characteristic handle.
300 void CADiscoverCharThread(void *stServiceInfo);
303 * Used to discover characteristics of service using
304 * bt_gatt_discover_characteristics() api.
306 * @param[in] service The attribute handle for service.
307 * @param[in] remoteAddress Remote address inwhich we wants to discover
308 * characteristics of given service handle.
309 * @return ::CA_STATUS_OK or Appropriate error code.
310 * @retval ::CA_STATUS_OK Successful.
311 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
312 * @retval ::CA_STATUS_FAILED Operation failed.
314 CAResult_t CABleGattDiscoverCharacteristics(bt_gatt_attribute_h service,
315 const char *remoteAddress);
318 * This is the thread which will be used for finding descriptor of
321 * @param[in] stServiceInfo Service Information which contains the remote
322 * address, service handle and characteristic handle.
324 void CADiscoverDescriptorThread(void *stServiceInfo);
327 * This is thread which will be used for calling
328 * CASetCharacteristicDescriptorValue() api.
330 * @param[in] service The attribute handle for characteristics.
331 * @param[in] remoteAddress Remote address inwhich we wants to discover
332 * descriptor of given char handle.
333 * @return ::CA_STATUS_OK or Appropriate error code.
334 * @retval ::CA_STATUS_OK Successful.
335 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
336 * @retval ::CA_STATUS_FAILED Operation failed.
338 CAResult_t CABleGattDiscoverDescriptor(bt_gatt_attribute_h service,
339 const char *remoteAddress);
342 * This is thread which will be used for calling
343 * CASetCharacteristicDescriptorValue() api.
345 * @param[in] stServiceInfo Service Information which contains the remote
346 * address, service handle and characteristic handle.
348 void CASetCharacteristicDescriptorValueThread(void *stServiceInfo);
351 * Used to set characteristic descriptor value using
352 * bt_gatt_set_characteristic_desc_value_request() api.
353 * @param[in] stGattCharDescriptorInfo Structure which contains char
354 * handle and descriptor handle.
356 * @return ::CA_STATUS_OK or Appropriate error code.
357 * @retval ::CA_STATUS_OK Successful.
358 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
359 * @retval ::CA_STATUS_FAILED Operation failed.
361 CAResult_t CASetCharacteristicDescriptorValue
362 (stGattCharDescriptor_t *stGattCharDescriptorInfo);
365 * Used to enqueue the message into sender queue using
366 * CAAdapterEnqueueMessage() and make signal to the thread to process.
368 * @param[in] remoteEndpoint Remote device information.
369 * @param[in] data Data to be sent to remote device.
370 * @param[in] dataLen Length of data..
372 * @return ::CA_STATUS_OK or Appropriate error code.
373 * @retval ::CA_STATUS_OK Successful.
374 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
375 * @retval ::CA_STATUS_FAILED Operation failed.
377 CAResult_t CABleClientSenderQueueEnqueueMessage
378 (const CAEndpoint_t *remoteEndpoint,
379 const uint8_t *data, uint32_t dataLen);
382 * This is the thread which will be used for processing sender queue.
384 void CABleClientSenderQueueProcessor();
387 * Synchronous function for reading characteristic value.
389 * @return ::CA_STATUS_OK or Appropriate error code.
390 * @retval ::CA_STATUS_OK Successful.
391 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
392 * @retval ::CA_STATUS_FAILED Operation failed.
394 CAResult_t CALEReadDataFromLEClient();
396 #endif /* TZ_BLE_CLIENT_H_ */