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 provides APIs for BLE modules.
27 #ifndef CA_LE_INTERFACE_H_
28 #define CA_LE_INTERFACE_H_
33 #include "cathreadpool.h"
34 #include "uarraylist.h"
42 * Provide info about different mode of data transfer.
44 * This enum is used to differentiate between unicast and multicast
49 LE_MULTICAST, /**< When this enum is selected, data will be updated to all OIC servers. */
50 LE_UNICAST /**< When this enum is selected, data will be updated to desired OIC Server. */
54 * Stores the information of the Data to be sent from the queues.
56 * This structure will be pushed to the sender/receiver queue for
61 /// Remote endpoint contains the information of remote device.
62 CAEndpoint_t *remoteEndpoint;
64 /// Data to be transmitted over LE transport.
67 /// Length of the data being transmitted.
70 /// Sender information list
71 u_arraylist_t * senderInfo;
75 * This will be used to notify device status changes to the LE adapter layer.
76 * @param[in] adapter_state State of the adapter.
78 typedef void (*CALEDeviceStateChangedCallback)(CAAdapterState_t adapter_state);
81 * Notify the adapter layer that a packet was received from the GATT
84 * @param[in] remoteAddress Remote endpoint Address.
85 * @param[in] data Data received.
86 * @param[in] dataLength Length of the data received.
87 * @param[in] sentLength Length of the data sent.
89 * @return ::CA_STATUS_OK or Appropriate error code.
90 * @retval ::CA_STATUS_OK Successful.
91 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
92 * @retval ::CA_STATUS_FAILED Operation failed.
94 typedef CAResult_t (*CABLEDataReceivedCallback)(const char *remoteAddress,
97 uint32_t *sentLength);
100 * Initialize the LE adapter layer. This will be invoked from the CA
103 * @param[in] threadPool Thread pool handle
104 * @return ::CA_STATUS_OK or Appropriate error code
105 * @retval ::CA_STATUS_OK Successful
106 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
107 * @retval ::CA_STATUS_FAILED Operation failed
109 CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool);
112 * Start the LE adapter layer.
114 * This function will be invoked from the CA layer when the LE
115 * "network" is selected via @c CASelectNetwork(). It gives an
116 * opportunity for LE adapter implementations to perform operations
117 * before starting a GATT client or server. Most LE adapter
118 * implementations will simply implement this function as no-op.
120 * @return ::CA_STATUS_OK or Appropriate error code
122 CAResult_t CAStartLEAdapter();
125 * Stop the LE adapter layer.
127 * This function will be invoked from the CA layer when the LE
128 * "network" is unselected via @c CAUnselectNetwork(). It gives an
129 * opportunity for LE adapter implementations to perform operations
130 * after stopping a GATT client or server. Most LE adapter
131 * implementations will simply implement this function as no-op.
133 * @return ::CA_STATUS_OK or Appropriate error code
135 CAResult_t CAStopLEAdapter();
138 * Used to get the current state of the LE adapter.
140 * @return ::CA_STATUS_OK or Appropriate error code
141 * @retval ::CA_STATUS_OK Successful
142 * @retval ::CA_ADAPTER_NOT_ENABLED adapter not enabled
143 * @retval ::CA_STATUS_FAILED Operation failed
145 CAResult_t CAGetLEAdapterState();
148 * Initialize the network monitor layer of the LE adapter. Mutex
149 * variables required to operate in this layer and other parameters
150 * can be initialized in this function.
152 * @return ::CA_STATUS_OK or Appropriate error code
153 * @retval ::CA_STATUS_OK Successful
154 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
155 * @retval ::CA_STATUS_FAILED Operation failed
157 CAResult_t CAInitializeLENetworkMonitor();
160 * Terminate the network monitor layer of the LE adapter. The
161 * variables initialized in @c CAInitializeLENetworkMonitor() must be
162 * cleared in this function.
164 void CATerminateLENetworkMonitor();
167 * Set the callback for the device state changes in the adapter.
169 * @param[in] callback Callback to notify the Device state change to
172 * @return ::CA_STATUS_OK or Appropriate error code
173 * @retval ::CA_STATUS_OK Successful
174 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
175 * @retval ::CA_STATUS_FAILED Operation failed
177 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback);
180 * Provides the MAC address of the local Bluetooth adapter.
182 * @param[out] local_address Pointer to the location where bd address
183 * needs to be stored.
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 CAGetLEAddress(char **local_address);
193 * Start Gatt Server thread for service creation and advertise BLE
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 CAStartLEGattServer();
204 * Stop BLE Gatt Service.
206 * @return ::CA_STATUS_OK or Appropriate error code
207 * @retval ::CA_STATUS_OK Successful
208 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
209 * @retval ::CA_STATUS_FAILED Operation failed
211 CAResult_t CAStopLEGattServer();
214 * initialize gatt server
216 * @return ::CA_STATUS_OK or Appropriate error code
217 * @retval ::CA_STATUS_OK Successful
218 * @retval ::CA_STATUS_FAILED Operation failed
220 CAResult_t CAInitializeLEGattServer();
223 * Stop Gatt Server thread and remove service registration, stop
226 void CATerminateLEGattServer();
229 * Used to store upper layer callback locally which will be used to
230 * send the data to application.
232 * @param[in] callback Callback function to pass the data to CA layer.
234 void CASetLEReqRespServerCallback(CABLEDataReceivedCallback callback);
237 * Update characteristics(Read/Write) value that we want to send to
240 * @param[in] address BD address of Gatt client
241 * @param[in] value Data that we want to send to client(unicast)
242 * @param[in] valueLen Length of the data.
244 * @return ::CA_STATUS_OK or Appropriate error code
245 * @retval ::CA_STATUS_OK Successful
246 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
247 * @retval ::CA_STATUS_FAILED Operation failed
249 CAResult_t CAUpdateCharacteristicsToGattClient(const char *address,
250 const uint8_t *value,
254 * Update characteristics(Read/Write) value that we want to multicast
257 * @param[in] value Data that we want to send to clients(multicast)
258 * @param[in] valueLen Length of the data.
260 * @return ::CA_STATUS_OK or Appropriate error code
261 * @retval ::CA_STATUS_OK Successful
262 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
263 * @retval ::CA_STATUS_FAILED Operation failed
265 CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *value,
269 * Start @c CAStartBleGattClientThread for initializing Gatt Client.
271 * @return ::CA_STATUS_OK or Appropriate error code
272 * @retval ::CA_STATUS_OK Successful
273 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
274 * @retval ::CA_STATUS_FAILED Operation failed
276 CAResult_t CAStartLEGattClient();
279 * Stop Gatt client gracefully. In turn it will call the
280 * @c CATerminateBLEGattClient function.
282 * @return ::CA_STATUS_OK or Appropriate error code
283 * @retval ::CA_STATUS_OK Successful
284 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
285 * @retval ::CA_STATUS_FAILED Operation failed
287 void CAStopLEGattClient();
292 * @return ::CA_STATUS_OK or Appropriate error code
293 * @retval ::CA_STATUS_OK Successful
294 * @retval ::CA_STATUS_FAILED Operation failed
296 CAResult_t CAInitializeLEGattClient();
299 * Unset all the callbacks and stop service discovery
301 void CATerminateLEGattClient();
304 * Read the data from characteristics and invoke notify callback.
306 void CACheckLEData();
309 * Set the value of characteristic and update the value to
310 * GATTServer (unicast).
312 * @param[in] remoteAddress The address of the remote device
313 * @param[in] data The value of characteristic (byte array)
314 * @param[in] dataLen The length of value
315 * @param[in] type Type of the transfer(::CALETransferType_t)
316 * @param[in] position The unique index of each ble server. Used
317 * for multicast feature.
319 * @return ::CA_STATUS_OK or Appropriate error code
320 * @retval ::CA_STATUS_OK Successful
321 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
322 * @retval ::CA_STATUS_FAILED Operation failed
324 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
327 CALETransferType_t type,
331 * Set the value of characteristic and update the value to all
332 * registered GATTServer (multicast).
334 * @param[in] data The value of characteristic (byte array)
335 * @param[in] dataLen The length of value
337 * @return ::CA_STATUS_OK or Appropriate error code
338 * @retval ::CA_STATUS_OK Successful
339 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
340 * @retval ::CA_STATUS_FAILED Operation failed
342 CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data,
346 * Store upper layer callback locally which will be used to send the
347 * data to application.
349 * @param[in] callback Callback function to pass the data to CA layer.
351 void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback);
354 * Set the server thread pool handle which is required for spawning
357 * @param[in] handle Thread pool handle which is given by above layer
358 * for using thread creation task.
360 * @return ::CA_STATUS_OK or Appropriate error code
361 * @retval ::CA_STATUS_OK Successful
362 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
363 * @retval ::CA_STATUS_FAILED Operation failed
365 void CASetLEServerThreadPoolHandle(ca_thread_pool_t handle);
368 * Set the client thread pool handle which is required for spawning new
371 * @param[in] handle Thread pool handle which is given by above layer
372 * for using thread creation task.
374 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle);
377 * Unset the callback of adapter connection state change.
379 * @return ::CA_STATUS_OK or Appropriate error code.
380 * @retval ::CA_STATUS_OK Successful.
381 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
382 * @retval ::CA_STATUS_FAILED Operation failed.
384 CAResult_t CAUnSetLEAdapterStateChangedCb();
387 * This will be used to notify errors in BLE adapter.
389 * @param[in] remoteAddress Remote endpoint Address
390 * @param[in] data Data received
391 * @param[in] dataLength Length of the data received
392 * @param[in] result error code as per CAResult_t
394 typedef void (*CABLEErrorHandleCallback)(const char *remoteAddress,
399 * Set the client error handler callback.
401 * @param[in] callback Callback function to update error to the
404 void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback);
408 * Set the server error handler callback.
410 * @param[in] callback Callback function to update error to the
413 void CASetBLEServerErrorHandleCallback(CABLEErrorHandleCallback callback);
419 #endif /* CA_LE_INTERFACE_H_ */