Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / tizen / cableclient.h
1 /* ****************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 ******************************************************************/
20
21 /**
22  * @file
23  *
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.
29  */
30
31 #ifndef TZ_BLE_CLIENT_H_
32 #define TZ_BLE_CLIENT_H_
33
34 #include <bluetooth.h>
35 #include <bluetooth_type.h>
36 #include <bluetooth_product.h>
37
38 #include "cacommon.h"
39 #include "caadapterutils.h"
40 #include "cableutil.h"
41 #include "caadapterinterface.h"
42 #include "logger.h"
43 #include "cathreadpool.h"
44 #include "caleinterface.h"
45 #include "oic_malloc.h"
46
47
48 /**
49  * This is the callback which will be called after the characteristic
50  * value changes happen.
51  *
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.
56  */
57 void CABleGattCharacteristicChangedCb(bt_gatt_attribute_h characteristic,
58                                       unsigned char *value,
59                                       int valueLen,
60                                       void *userData);
61 /**
62  * This is the callback which will be called after the characteristics changed.
63  *
64  * @param[in]  result   result of write value.
65  * @param[in]  userData user context.
66  */
67 void CABleGattCharacteristicWriteCb(int result, void *userData);
68
69 /**
70  * This is the callback which will be called when descriptor of
71  * characteristics is found.
72  *
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
76  *                             characteristic.
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.
80  */
81 void CABleGattDescriptorDiscoveredCb(int result, unsigned char format, int total,
82                                      bt_gatt_attribute_h descriptor,
83                                      bt_gatt_attribute_h characteristic, void *userData);
84
85 /**
86  * This is the callback which will be called after the characteristics are
87  * discovered by bt_gatt_discover_characteristics().
88  *
89  * @param[in]  result         The result of discovering.
90  * @param[in]  inputIndex     The index of characteristics in a service,
91  *                             starts from 0.
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.
95  *
96  * @return  0 on failure and 1 on success.
97  */
98 bool CABleGattCharacteristicsDiscoveredCb(int result, int inputIndex, int total,
99                                           bt_gatt_attribute_h characteristic, void *userData);
100
101 /**
102  * This is the callback which will be called when we get the primary
103  * services repeatedly.
104  *
105  * @param[in] service  The attribute handle of service. Unique identifier
106  *                      for service.
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.
110  *
111  * @return  0 on failure and 1 on success.
112  */
113 bool CABleGattPrimaryServiceCb(bt_gatt_attribute_h service, int index, int count,
114                                    void *userData);
115
116 /**
117  * This is the callback which will be called whenever there is change in
118  * gatt connection with server(Connected/Disconnected)
119  *
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
123  *                            made connection.
124  * @param[in]  userData      The user data passed from the request function.
125  */
126 void CABleGattConnectionStateChangedCb(int result, bool connected,
127                 const char *remoteAddress,void *userData);
128
129 /**
130  * This is the callback which will be called when the device discovery
131  * state changes.
132  *
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.
137  */
138 void CABtAdapterLeDeviceDiscoveryStateChangedCb(int result,
139         bt_adapter_le_device_discovery_state_e discoveryState,
140         bt_adapter_le_device_discovery_info_s *discoveryInfo,
141         void *userData);
142
143 /**
144  * Used to print device information(Util method).
145  * @param[in] discoveryInfo Device information structure.
146  */
147 void CAPrintDiscoveryInformation(const bt_adapter_le_device_discovery_info_s *discoveryInfo);
148
149 /**
150  * This thread will be used to initialize the Gatt Client and start device
151  * discovery.
152  *        1. Set scan parameters.
153  *        2. Setting neccessary callbacks for connection, characteristics
154  *          changed and discovery.
155  *        3. Start device discovery.
156  *
157  * @param[in] data Currently it will be NULL(no parameter).
158  */
159 void CAStartBleGattClientThread(void *data);
160
161 /**
162  * Used to initialize all required mutex variable for Gatt Client
163  * implementation.
164  *
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.
169  */
170 CAResult_t CAInitGattClientMutexVariables();
171
172 /**
173  * Used to terminate all required mutex variable for Gatt Client implementation.
174  */
175 void CATerminateGattClientMutexVariables();
176
177 /**
178  * Used to clear NonOICDeviceList.
179  */
180 void CAClearNonOICDeviceList();
181
182 /**
183  * Used to set scan parameter of starting discovery.
184  *
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.
189  */
190 CAResult_t CABleGattSetScanParameter();
191
192 /**
193  * Used to register required callbacks to BLE platform(connection,
194  * discovery, etc).
195  *
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.
200  */
201 CAResult_t CABleGattSetCallbacks();
202
203 /**
204  * Used to unset all the registerd callbacks to BLE platform.
205  */
206 void CABleGattUnSetCallbacks();
207
208 /**
209  * Used to watch all the changes happening in characteristics of the service.
210  *
211  * @param[in] service The attribute handle of the service.
212  *
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.
217  */
218 CAResult_t CABleGattWatchCharacteristicChanges(bt_gatt_attribute_h service);
219
220 /**
221  * Used to unwatch characteristics changes using
222  * bt_gatt_unwatch_characteristic_changes().
223  */
224 void CABleGattUnWatchCharacteristicChanges();
225
226 /**
227  * Used to start LE discovery for BLE  devices.
228  *
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.
233  */
234 CAResult_t CABleGattStartDeviceDiscovery();
235
236 /**
237  * Used to stop LE discovery for BLE  devices.
238  */
239 void CABleGattStopDeviceDiscovery();
240
241 /**
242  * This is the thread  which will be used for making gatt connection with
243  * remote devices.
244  * @param[in] remoteAddress MAC address of remote device to connect.
245  */
246 void CAGattConnectThread (void *remoteAddress);
247
248 /**
249  * Used to do connection with remote device.
250  *
251  * @param[in] remoteAddress Remote address inwhich we wants to connect with.
252  *
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.
257  */
258 CAResult_t CABleGattConnect(const char *remoteAddress);
259
260 /**
261  * Used to do disconnection with remote device.
262  * @param[in] remoteAddress Remote address inwhich we wants to disconnect with.
263  *
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.
268  */
269 CAResult_t CABleGattDisConnect(const char *remoteAddress);
270
271 /**
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.
276  */
277 void CADiscoverBLEServicesThread (void *remoteAddress);
278
279 /**
280  * Used to discover the services that is advertised by Gatt Server
281  * asynchronously.
282  *
283  * @param[in] remoteAddress MAC address of remote device in which we want
284  *                           to discover the services.
285  *
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.
290  */
291 CAResult_t CABleGattDiscoverServices(const char *remoteAddress);
292
293 /**
294  * This is the thread which will be used for finding characteristic of a
295  * service.
296  *
297  * @param[in]  stServiceInfo Service Information which contains the remote
298  *                            address, service handle and characteristic handle.
299  */
300 void CADiscoverCharThread(void *stServiceInfo);
301
302 /**
303  * Used to discover characteristics of service using
304  * bt_gatt_discover_characteristics() api.
305  *
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.
313  */
314 CAResult_t CABleGattDiscoverCharacteristics(bt_gatt_attribute_h service,
315                     const char *remoteAddress);
316
317 /**
318  * This is the thread which will be used for finding descriptor of
319  * characteristic.
320  *
321  * @param[in]  stServiceInfo Service Information which contains the remote
322  *                            address, service handle and characteristic handle.
323  */
324 void CADiscoverDescriptorThread(void *stServiceInfo);
325
326 /**
327  * This is thread which will be used for calling
328  * CASetCharacteristicDescriptorValue() api.
329  *
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.
337  */
338 CAResult_t CABleGattDiscoverDescriptor(bt_gatt_attribute_h service,
339                 const char *remoteAddress);
340
341 /**
342  * This is thread which will be used for calling
343  * CASetCharacteristicDescriptorValue() api.
344  *
345  * @param[in]  stServiceInfo Service Information which contains the remote
346  *                            address, service handle and characteristic handle.
347  */
348 void CASetCharacteristicDescriptorValueThread(void *stServiceInfo);
349
350 /**
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.
355  *
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.
360  */
361 CAResult_t CASetCharacteristicDescriptorValue
362             (stGattCharDescriptor_t *stGattCharDescriptorInfo);
363
364 /**
365  * Used to enqueue the message into sender queue using
366  * CAAdapterEnqueueMessage() and make signal to the thread to process.
367  *
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..
371  *
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.
376  */
377 CAResult_t CABleClientSenderQueueEnqueueMessage
378                             (const CAEndpoint_t *remoteEndpoint,
379                                                 const uint8_t *data, uint32_t dataLen);
380
381 /**
382  * This is the thread which will be used for processing sender queue.
383  */
384 void CABleClientSenderQueueProcessor();
385
386 /**
387  * Synchronous function for reading characteristic value.
388  *
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.
393  */
394 CAResult_t CALEReadDataFromLEClient();
395
396 #endif /* TZ_BLE_CLIENT_H_ */