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.
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 * @brief This is the callback which will be called after the characteristic value changes happen.
51 * @param characteristic [IN] The attribute handle of characteristic
52 * @param value [IN] Value of the characteristics of a service.
53 * @param valueLen [IN] length of data.
54 * @param userData [IN] The user data passed from the request function
57 void CABleGattCharacteristicChangedCb(bt_gatt_attribute_h characteristic,
58 unsigned char *value, int valueLen, void *userData);
60 * @brief This is the callback which will be called after the characteristics changed.
62 * @param result [IN] result of write value
63 * @param userData [IN] user context
67 void CABleGattCharacteristicWriteCb(int result, void *userData);
70 * @brief This is the callback which will be called when descriptor of characteristics is found.
72 * @param result [IN] The result of discovering
73 * @param format [IN] format of descriptor.
74 * @param total [IN] The total number of descriptor in a characteristic
75 * @param descriptor [IN] The attribute handle of descriptor
76 * @param characteristic [IN] The attribute handle of characteristic
77 * @param userData [IN] The user data passed from the request function
80 void CABleGattDescriptorDiscoveredCb(int result, unsigned char format, int total,
81 bt_gatt_attribute_h descriptor,
82 bt_gatt_attribute_h characteristic, void *userData);
85 * @brief This is the callback which will be called after the characteristics are discovered by
86 * bt_gatt_discover_characteristics()
88 * @param result [IN] The result of discovering
89 * @param inputIndex [IN] The index of characteristics in a service, starts from 0
90 * @param total [IN] The total number of characteristics in a service
91 * @param characteristic [IN] The attribute handle of characteristic
92 * @param userData [IN] The user data passed from the request function
94 * @return 0 on failure and 1 on success.
96 bool CABleGattCharacteristicsDiscoveredCb(int result, int inputIndex, int total,
97 bt_gatt_attribute_h characteristic, void *userData);
100 * @brief This is the callback which will be called when bond created with remote device.
102 * @param result [IN] The result of bond creation.
103 * @param device_info [IN] remote device information
104 * @param userData [IN] The user data passed from the request function
107 void CABtGattBondCreatedCb(int result, bt_device_info_s *device_info, void *user_data);
110 * @brief This is the callback which will be called when we get the primary services repeatedly.
112 * @param service [IN] The attribute handle of service. Unique identifier for service.
113 * @param index [IN] The current index of the service
114 * @param count [IN] Total number of services available in remote device
115 * @param userData [IN] user data
117 * @return 0 on failure and 1 on success.
119 bool CABleGattPrimaryServiceCb(bt_gatt_attribute_h service, int index, int count,
123 * @brief This is the callback which will be called whenever there is change in gatt connection
124 * with server(Connected/Disconnected)
126 * @param result [IN] The result of discovering
127 * @param connected [IN] State of connection
128 * @param remoteAddress [IN] Mac address of the remote device in which we made connection.
129 * @param userData [IN] The user data passed from the request function
133 void CABleGattConnectionStateChangedCb(int result, bool connected,
134 const char *remoteAddress,void *userData);
137 * @brief This is the callback which will be called when the device discovery state changes.
139 * @param result [IN] The result of discovering
140 * @param discoveryState [IN] State of the discovery(FOUND/STARTED/ FINISHED)
141 * @param discoveryInfo [IN] Remote Device information.
142 * @param userData [IN] The user data passed from the request function
146 void CABtAdapterLeDeviceDiscoveryStateChangedCb(int result,
147 bt_adapter_le_device_discovery_state_e discoveryState,
148 bt_adapter_le_device_discovery_info_s *discoveryInfo,
152 * @brief Used to print device information(Util method)
153 * @param discoveryInfo [IN] Device information structure.
156 void CAPrintDiscoveryInformation(const bt_adapter_le_device_discovery_info_s *discoveryInfo);
159 * @brief This thread will be used to initialize the Gatt Client and start device discovery.
160 * 1. Set scan parameters
161 * 2. Setting neccessary callbacks for connection, characteristics changed and discovery
163 * 3. Start device discovery
165 * @param data [IN] Currently it will be NULL(no parameter)
170 void CAStartBleGattClientThread(void *data);
173 * @brief Used to initialize all required mutex variable for Gatt Client implementation.
175 * @return #CA_STATUS_OK or Appropriate error code
176 * @retval #CA_STATUS_OK Successful
177 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
178 * @retval #CA_STATUS_FAILED Operation failed
180 CAResult_t CAInitGattClientMutexVariables();
183 * @brief Used to terminate all required mutex variable for Gatt Client implementation.
186 void CATerminateGattClientMutexVariables();
189 * @brief Used to clear NonOICDeviceList
192 void CAClearNonOICDeviceList();
195 * @brief Used to set scan parameter of starting discovery.
197 * @return #CA_STATUS_OK or Appropriate error code
198 * @retval #CA_STATUS_OK Successful
199 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
200 * @retval #CA_STATUS_FAILED Operation failed
202 CAResult_t CABleGattSetScanParameter();
205 * @brief Used to register required callbacks to BLE platform(connection, discovery, bond and etc).
207 * @return #CA_STATUS_OK or Appropriate error code
208 * @retval #CA_STATUS_OK Successful
209 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
210 * @retval #CA_STATUS_FAILED Operation failed
212 CAResult_t CABleGattSetCallbacks();
215 * @brief Used to unset all the registerd callbacks to BLE platform
218 void CABleGattUnSetCallbacks();
221 * @brief Used to watch all the changes happening in characteristics of the service.
223 * @param service [IN] The attribute handle of the service.
225 * @return #CA_STATUS_OK or Appropriate error code
226 * @retval #CA_STATUS_OK Successful
227 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
228 * @retval #CA_STATUS_FAILED Operation failed
230 CAResult_t CABleGattWatchCharacteristicChanges(bt_gatt_attribute_h service);
233 * @brief Used to unwatch characteristics changes using bt_gatt_unwatch_characteristic_changes
236 void CABleGattUnWatchCharacteristicChanges();
239 * @brief Used to start LE discovery for BLE devices
241 * @return #CA_STATUS_OK or Appropriate error code
242 * @retval #CA_STATUS_OK Successful
243 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
244 * @retval #CA_STATUS_FAILED Operation failed
246 CAResult_t CABleGattStartDeviceDiscovery();
249 * @brief Used to stop LE discovery for BLE devices
252 void CABleGattStopDeviceDiscovery();
255 * @brief This is the thread which will be used for making gatt connection with remote devices
256 * @param remoteAddress [IN] MAC address of remote device to connect
259 void CAGattConnectThread (void *remoteAddress);
262 * @brief Used to do connection with remote device
264 * @param remoteAddress [IN] Remote address inwhich we wants to connect with
266 * @return #CA_STATUS_OK or Appropriate error code
267 * @retval #CA_STATUS_OK Successful
268 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
269 * @retval #CA_STATUS_FAILED Operation failed
271 CAResult_t CABleGattConnect(const char *remoteAddress);
274 * @brief Used to do disconnection with remote device
275 * @param remoteAddress [IN] Remote address inwhich we wants to disconnect with
277 * @return #CA_STATUS_OK or Appropriate error code
278 * @retval #CA_STATUS_OK Successful
279 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
280 * @retval #CA_STATUS_FAILED Operation failed
282 CAResult_t CABleGattDisConnect(const char *remoteAddress);
285 * @brief This is thread which will be spawned for discovering ble services. Once called discover
286 * api, then it will be terminated.
287 * @param remoteAddress [IN] Mac address of the remote device in which we want to search services.
290 void CADiscoverBLEServicesThread (void *remoteAddress);
293 * @brief Used to discover the services that is advertised by Gatt Server asynchrounously.
295 * @param remoteAddress [IN] MAC address of remote device in which we want to discover the services.
297 * @return #CA_STATUS_OK or Appropriate error code
298 * @retval #CA_STATUS_OK Successful
299 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
300 * @retval #CA_STATUS_FAILED Operation failed
302 CAResult_t CABleGattDiscoverServices(const char *remoteAddress);
305 * @brief This is the thread which will be used for finding characteristic of a service.
307 * @param stServiceInfo [IN] Service Information which contains the remote address, service
308 * handle and characteristic handle.
311 void CADiscoverCharThread(void *stServiceInfo);
314 * @brief Used to discover characteristics of service using bt_gatt_discover_characteristics api.
316 * @param service [IN] The attribute handle for service.
317 * @param remoteAddress [IN] Remote address inwhich we wants to discover characteristics of
318 * given service handle.
319 * @return #CA_STATUS_OK or Appropriate error code
320 * @retval #CA_STATUS_OK Successful
321 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
322 * @retval #CA_STATUS_FAILED Operation failed
324 CAResult_t CABleGattDiscoverCharacteristics(bt_gatt_attribute_h service,
325 const char *remoteAddress);
328 * @brief This is the thread which will be used for finding descriptor of characteristic.
330 * @param stServiceInfo [IN] Service Information which contains the remote address, service
331 * handle and characteristic handle.
334 void CADiscoverDescriptorThread(void *stServiceInfo);
337 * @brief This is thread which will be used for calling CASetCharacteristicDescriptorValue api.
339 * @param service [IN] The attribute handle for characteristics.
340 * @param remoteAddress [IN] Remote address inwhich we wants to discover descriptor of given
342 * @return #CA_STATUS_OK or Appropriate error code
343 * @retval #CA_STATUS_OK Successful
344 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
345 * @retval #CA_STATUS_FAILED Operation failed
347 CAResult_t CABleGattDiscoverDescriptor(bt_gatt_attribute_h service,
348 const char *remoteAddress);
351 * @brief This is thread which will be used for calling CASetCharacteristicDescriptorValue api.
353 * @param stServiceInfo [IN] Service Information which contains the remote address, service
354 * handle and characteristic handle.
357 void CASetCharacteristicDescriptorValueThread(void *stServiceInfo);
360 * @brief Used to set characteristic descriptor value using
361 * bt_gatt_set_characteristic_desc_value_request api.
362 * @param stGattCharDescriptorInfo [IN] Structure which contains char handle and descriptor handle.
364 * @return #CA_STATUS_OK or Appropriate error code
365 * @retval #CA_STATUS_OK Successful
366 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
367 * @retval #CA_STATUS_FAILED Operation failed
369 CAResult_t CASetCharacteristicDescriptorValue
370 (stGattCharDescriptor_t *stGattCharDescriptorInfo);
373 * @brief This is the thread which will be used for creating bond with remote device.
375 * @param stServiceInfo [IN] Service Information which contains the remote address and service
376 * handle, characteristic handle.
379 void CAGATTCreateBondThread(void *stServiceInfo);
382 * @brief Used to make LE bond with remote device(pairng the device) using bt_device_create_bond
384 * @param remoteAddress [IN] MAC address of remote device with which we want to bond
386 * @return #CA_STATUS_OK or Appropriate error code
387 * @retval #CA_STATUS_OK Successful
388 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
389 * @retval #CA_STATUS_FAILED Operation failed
391 CAResult_t CABleGATTCreateBond(const char *remoteAddress);
394 * @brief Used to enqueue the message into sender queue using CAAdapterEnqueueMessage and make
395 * signal to the thread to process.
397 * @param remoteEndpoint [IN] Remote device information
398 * @param data [IN] Data to be sent to remote device
399 * @param dataLen [IN] Length of data.
401 * @return #CA_STATUS_OK or Appropriate error code
402 * @retval #CA_STATUS_OK Successful
403 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
404 * @retval #CA_STATUS_FAILED Operation failed
406 CAResult_t CABleClientSenderQueueEnqueueMessage
407 (const CARemoteEndpoint_t *remoteEndpoint,
408 const void *data, uint32_t dataLen);
411 * @brief This is the thread which will be used for processing sender queue.
415 void CABleClientSenderQueueProcessor();
418 * @brief Synchronous function for reading characteristic value.
420 * @return #CA_STATUS_OK or Appropriate error code
421 * @retval #CA_STATUS_OK Successful
422 * @retval #CA_STATUS_INVALID_PARAM Invalid input argumets
423 * @retval #CA_STATUS_FAILED Operation failed
425 CAResult_t CALEReadDataFromLEClient();
427 #endif //#ifndef _BLE_CLIENT_