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 APIs for BT LE communications.
27 #ifndef __CA_LESERVER_H_
28 #define __CA_LESERVER_H_
31 #include "uthreadpool.h"
32 #include "uarraylist.h"
41 * @brief Callback to be notified on reception of any data from remote devices.
42 * @param address [IN] MAC address of remote device.
43 * @param data [IN] Data received from remote device.
45 * @pre Callback must be registered using CALEServerSetCallback(CAPacketReceiveCallback callback)
47 typedef void (*CAPacketReceiveCallback)(const char *address,
51 * @brief initialize server for BLE
52 * @param handle [IN] thread pool handle object
55 void CALEServerInitialize(u_thread_pool_t handle);
58 * @brief terminate client for BLE
61 void CALEServerTerminate();
64 * @brief send data for unicast (interface)
65 * @param address [IN] remote address
66 * @param data [IN] data for transmission
67 * @param dataLen [IN] data length
68 * @return #CA_STATUS_OK or Appropriate error code
69 * @retval #CA_STATUS_OK Successful
70 * @retval #CA_STATUS_FAILED Operation failed
72 CAResult_t CALEServerSendUnicastMessage(const char *address, const char *data,
76 * @brief send data for multicast (interface)
77 * @param data [IN] data for transmission
78 * @param dataLen [IN] data length
79 * @return #CA_STATUS_OK or Appropriate error code
80 * @retval #CA_STATUS_OK Successful
81 * @retval #CA_STATUS_FAILED Operation failed
83 CAResult_t CALEServerSendMulticastMessage(const char *data, uint32_t dataLen);
86 * @brief start unicast server
87 * @param address [IN] remote address
88 * @return #CA_STATUS_OK or Appropriate error code
89 * @retval #CA_STATUS_OK Successful
90 * @retval #CA_STATUS_FAILED Operation failed
92 CAResult_t CALEServerStartUnicastServer(const char *address);
95 * @brief start multicast server (start advertise)
96 * @return #CA_STATUS_OK or Appropriate error code
97 * @retval #CA_STATUS_OK Successful
98 * @retval #CA_STATUS_FAILED Operation failed
100 CAResult_t CALEServerStartMulticastServer();
103 * @brief stop unicast server
106 CAResult_t CALEServerStopUnicastServer();
109 * @brief stop multicast server (stop discovery)
112 CAResult_t CALEServerStopMulticastServer();
115 * @brief set this callback for receiving data packets from peer devices.
116 * @param callback [IN] callback to be notified on reception of
117 * unicast/multicast data packets.
120 void CALEServerSetCallback(CAPacketReceiveCallback callback);
123 * @brief Get local adapter network information.
124 * @param info [OUT] local connectivity information
125 * @param size [OUT] connectivity count
128 void CALEServerGetInterfaceInfo(CALocalConnectivity_t **info,
132 * @brief get local address
133 * @param address [OUT] local address
136 void CALEServerGetLocalAddress(char *address);
139 * @brief send data for unicast (implement)
140 * @param env [IN] JNI interface pointer
141 * @param address [IN] remote address
142 * @param data [IN] data for transmission
143 * @param dataLen [IN] data length
144 * @return #CA_STATUS_OK or Appropriate error code
145 * @retval #CA_STATUS_OK Successful
146 * @retval #CA_STATUS_FAILED Operation failed
148 CAResult_t CALEServerSendUnicastMessageImpl(JNIEnv *env, const char *address,
149 const char *data, uint32_t dataLen);
152 * @brief send data for multicast (implement)
153 * @param env [IN] JNI interface pointer
154 * @param address [IN] remote address
155 * @return #CA_STATUS_OK or Appropriate error code
156 * @retval #CA_STATUS_OK Successful
157 * @retval #CA_STATUS_FAILED Operation failed
159 CAResult_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const char *data,
162 /* Android BLE Server Functions */
164 * @brief set context of application
165 * @param env [IN] JNI interface pointer
166 * @param context [IN] context of application
169 void CALEServerJNISetContext(JNIEnv *env, jobject context);
172 * @brief initialize JNI object
173 * @param env [IN] JNI interface pointer
174 * @param jvm [IN] java virtual machine pointer
177 void CALeServerJniInit(JNIEnv *env, JavaVM* jvm);
180 * @brief create interface object and initialize the object
183 void CALeServerCreateJniInterfaceObject();
186 * @brief start advertise in gatt server
187 * @param env [IN] JNI interface pointer
188 * @param advertiseCallback [IN] callback to be notified on reception of
189 * advertisement result
192 void CANativeLEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback);
195 * @brief stop advertise in gatt server
196 * @param env [IN] JNI interface pointer
197 * @param advertiseCallback [IN] callback to be notified on reception of
198 * advertisement result
201 void CANativeLEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback);
204 * @brief open a gatt server
205 * @param env [IN] JNI interface pointer
206 * @return gatt server object
208 jobject CANativeLEServerOpenGattServer(JNIEnv *env);
211 * @brief create gatt service
212 * @param env [IN] JNI interface pointer
213 * @return gatt service object
215 jobject CANativeLEServerCreateGattService(JNIEnv *env);
218 * @brief create gatt service
219 * @param env [IN] JNI interface pointer
220 * @return #CA_STATUS_OK or Appropriate error code
221 * @retval #CA_STATUS_OK Successful
222 * @retval #CA_ADAPTER_NOT_ENABLED Adapter is not enabled
223 * @retval #CA_STATUS_FAILED Operation failed
225 CAResult_t CANativeLEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
226 jobject bluetoothGattService);
229 * @brief start gatt server
230 * @param env [IN] JNI interface pointer
231 * @param gattServerCallback [IN] callback to be notified on reception of
232 * state change of gatt server
233 * @return #CA_STATUS_OK or Appropriate error code
234 * @retval #CA_STATUS_OK Successful
235 * @retval #CA_ADAPTER_NOT_ENABLED Adapter is not enabled
236 * @retval #CA_STATUS_FAILED Operation failed
238 CAResult_t CALEStartGattServer(JNIEnv *env, jobject gattServerCallback);
242 * @param env [IN] JNI interface pointer
243 * @param bluetoothDevice [IN] bluetooth device object
244 * @param data [IN] data which send
245 * @return #CA_STATUS_OK or Appropriate error code
246 * @retval #CA_STATUS_OK Successful
247 * @retval #CA_SEND_FAILED Send request failed
248 * @retval #CA_ADAPTER_NOT_ENABLED Adapter is not enabled
249 * @retval #CA_STATUS_FAILED Operation failed
251 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jstring data);
254 * @brief set data in BluetoothGattCharacteristic
255 * @param env [IN] JNI interface pointer
256 * @param bluetoothDevice [IN] bluetooth device object
257 * @return BluetoothGattCharacteristic object
259 jobject CANativeLEServerSetResponseData(JNIEnv *env, jbyteArray responseData);
262 * @brief send data through notifyCharacteristicChanged api of android
263 * @param env [IN] JNI interface pointer
264 * @param bluetoothDevice [IN] bluetooth device object
265 * @param data [IN] data which send
266 * @return #CA_STATUS_OK or Appropriate error code
267 * @retval #CA_STATUS_OK Successful
268 * @retval #CA_SEND_FAILED Send request failed
269 * @retval #CA_STATUS_FAILED Operation failed
271 CAResult_t CANativeLEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData);
274 * @brief send a response to a write request to a remote device
275 * @param env [IN] JNI interface pointer
276 * @param device [IN] bluetooth device object
277 * @param requestId [IN] the id of request
278 * @param status [IN] the status of the request to be sent to the remote devices
279 * @param offset [IN] value offset for partial write response
280 * @param value [IN] the value of the attribute that written (optional)
281 * @return #CA_STATUS_OK or Appropriate error code
282 * @retval #CA_STATUS_OK Successful
283 * @retval #CA_SEND_FAILED Send request failed
284 * @retval #CA_ADAPTER_NOT_ENABLED Adapter is not enabled
285 * @retval #CA_STATUS_FAILED Operation failed
287 CAResult_t CANativeLEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
288 jint offset, jbyteArray value);
291 * @brief connect BLE to remote device form gatt server
292 * @param env [IN] JNI interface pointer
293 * @param bluetoothDevice [IN] bluetooth device object
294 * @return #CA_STATUS_OK or Appropriate error code
295 * @retval #CA_STATUS_OK Successful
296 * @retval #CA_ADAPTER_NOT_ENABLED Adapter is not enabled
297 * @retval #CA_STATUS_FAILED Operation failed
299 CAResult_t CANativeLEServerConnect(JNIEnv *env, jobject bluetoothDevice);
302 * @brief disconnect BLE to remote device form gatt server
303 * @param env [IN] JNI interface pointer
304 * @param bluetoothDevice [IN] bluetooth device object
307 void CANativeLEServerDisconnect(JNIEnv *env, jobject bluetoothDevice);
309 /* BLE Server Utils */
311 * @brief create connected device list
314 void CALEServerCreateCachedDeviceList();
317 * @brief check whether the device exist in the list or not
318 * @param env [IN] JNI interface pointer
319 * @param remoteAddress [IN] remote address
320 * @return TRUE or FALSE
322 jboolean CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress);
325 * @brief add device object to the list (connected device list)
326 * @param env [IN] JNI interface pointer
327 * @param device [IN] bluetooth device object
330 void CALEServerAddDeviceToList(JNIEnv *env, jobject device);
333 * @brief remove all devices objects in the list (connected device list)
334 * @param env [IN] JNI interface pointer
337 void CALEServerRemoveAllDevices(JNIEnv *env);
340 * @brief remove target device in the list (connected device list)
341 * @param env [IN] JNI interface pointer
342 * @param gatt [IN] Gatt profile object
345 void CALEServerRemoveDevice(JNIEnv *env, jstring address);
348 * @brief Reordering for the list (connected device list)
349 * @param index [IN] index of device list that want to reordering
352 void CALEServerReorderinglist(uint32_t index);
358 #endif /* __CA_LESERVER_H_ */