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 ******************************************************************/
23 * @brief This file contains the APIs for BT LE communications.
25 #ifndef CA_LESERVER_H_
26 #define CA_LESERVER_H_
29 #include "cathreadpool.h"
30 #include "uarraylist.h"
39 * @brief Callback to be notified on reception of any data from remote devices.
40 * @param address [IN] MAC address of remote device.
41 * @param data [IN] Data received from remote device.
43 * @pre Callback must be registered using CALEServerSetCallback(CAPacketReceiveCallback callback)
45 typedef void (*CAPacketReceiveCallback)(const char *address, const char *data);
48 * @brief initialize server for BLE
49 * @param handle [IN] thread pool handle object
50 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
52 CAResult_t CALEServerInitialize(ca_thread_pool_t handle);
55 * @brief terminate client for BLE
58 void CALEServerTerminate();
61 * @brief send data for unicast (interface)
62 * @param address [IN] remote address
63 * @param data [IN] data for transmission
64 * @param dataLen [IN] data length
65 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
67 CAResult_t CALEServerSendUnicastMessage(const char *address, const char *data, uint32_t dataLen);
70 * @brief send data for multicast (interface)
71 * @param data [IN] data for transmission
72 * @param dataLen [IN] data length
73 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
75 CAResult_t CALEServerSendMulticastMessage(const char *data, uint32_t dataLen);
78 * @brief start multicast server (start advertise)
79 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
81 CAResult_t CALEServerStartMulticastServer();
84 * @brief stop multicast server (stop discovery)
87 CAResult_t CALEServerStopMulticastServer();
90 * @brief set this callback for receiving data packets from peer devices.
91 * @param callback [IN] callback to be notified on reception of
92 * unicast/multicast data packets.
95 void CALEServerSetCallback(CAPacketReceiveCallback callback);
98 * @brief send data for unicast (implement)
99 * @param env [IN] JNI interface pointer
100 * @param address [IN] remote address
101 * @param data [IN] data for transmission
102 * @param dataLen [IN] data length
103 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
105 CAResult_t CALEServerSendUnicastMessageImpl(JNIEnv *env, const char *address, const char *data,
109 * @brief send data for multicast (implement)
110 * @param env [IN] JNI interface pointer
111 * @param data [IN] data for transmission
112 * @param dataLen [IN] data length
113 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
115 CAResult_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const char *data, uint32_t dataLen);
117 /* Android BLE Server Functions */
119 * @brief set context of application
122 void CALEServerJNISetContext();
125 * @brief initialize JNI object
128 void CALeServerJniInit();
131 * @brief create interface object and initialize the object
132 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
134 CAResult_t CALEServerCreateJniInterfaceObject();
137 * @brief start advertise in gatt server
138 * @param env [IN] JNI interface pointer
139 * @param advertiseCallback [IN] callback to be notified on reception of
140 * advertisement result
141 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
143 CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback);
146 * @brief stop advertise in gatt server
147 * @param env [IN] JNI interface pointer
148 * @param advertiseCallback [IN] callback to be notified on reception of
149 * advertisement result
150 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
152 CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback);
155 * @brief open a gatt server
156 * @param env [IN] JNI interface pointer
157 * @return gatt server object
159 jobject CALEServerOpenGattServer(JNIEnv *env);
162 * @brief create gatt service
163 * @param env [IN] JNI interface pointer
164 * @return gatt service object
166 jobject CALEServerCreateGattService(JNIEnv *env);
169 * @brief add a descriptor to the characteristic
170 * @param env [IN] JNI interface pointer
171 * @param characteristic [IN] Characteristic object
172 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
174 CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic);
177 * @brief create gatt service
178 * @param env [IN] JNI interface pointer
179 * @param bluetoothGattServer [IN] Bluetooth Gatt Server object
180 * @param bluetoothGattService [IN] Bluetooth Gatt Service object
181 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
183 CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
184 jobject bluetoothGattService);
187 * @brief start gatt server
188 * @param env [IN] JNI interface pointer
189 * @param gattServerCallback [IN] callback to be notified on reception of
190 * state change of gatt server
191 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
193 CAResult_t CALEServerStartGattServer(JNIEnv *env, jobject gattServerCallback);
197 * @param env [IN] JNI interface pointer
198 * @param bluetoothDevice [IN] bluetooth device object
199 * @param data [IN] data which send
200 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
202 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jstring data);
205 * @brief set data in BluetoothGattCharacteristic
206 * @param env [IN] JNI interface pointer
207 * @param responseData [IN] data to set in characteristic object
208 * @return BluetoothGattCharacteristic object
210 jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData);
213 * @brief send data through notifyCharacteristicChanged api of android
214 * @param env [IN] JNI interface pointer
215 * @param device [IN] bluetooth device object
216 * @param responseData [IN] data which send
217 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
219 CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData);
222 * @brief send a response to a write request to a remote device
223 * @param env [IN] JNI interface pointer
224 * @param device [IN] bluetooth device object
225 * @param requestId [IN] the id of request
226 * @param status [IN] the status of the request to be sent to the remote devices
227 * @param offset [IN] value offset for partial write response
228 * @param value [IN] the value of the attribute that written (optional)
229 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
231 CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
232 jint offset, jbyteArray value);
235 * @brief connect BLE to remote device form gatt server
236 * @param env [IN] JNI interface pointer
237 * @param bluetoothDevice [IN] bluetooth device object
238 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
240 CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice);
243 * @brief disconnect LE for all devices
244 * @param env [IN] JNI interface pointer
245 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
247 CAResult_t CALEServerDisconnectAllDevices(JNIEnv *env);
250 * @brief disconnect LE to remote device form gatt server
251 * @param env [IN] JNI interface pointer
252 * @param bluetoothDevice [IN] bluetooth device object
253 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
255 CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice);
257 /* BLE Server Utils */
259 * @brief create connected device list
262 void CALEServerCreateCachedDeviceList();
265 * @brief check whether the device exist in the list or not
266 * @param env [IN] JNI interface pointer
267 * @param remoteAddress [IN] remote address
268 * @return true or false
270 bool CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress);
273 * @brief add device object to the list (connected device list)
274 * @param env [IN] JNI interface pointer
275 * @param device [IN] bluetooth device object
276 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
278 CAResult_t CALEServerAddDeviceToList(JNIEnv *env, jobject device);
281 * @brief remove all devices objects in the list (connected device list)
282 * @param env [IN] JNI interface pointer
283 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
285 CAResult_t CALEServerRemoveAllDevices(JNIEnv *env);
288 * @brief remove target device in the list (connected device list)
289 * @param env [IN] JNI interface pointer
290 * @param address [IN] target address to remove
291 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
293 CAResult_t CALEServerRemoveDevice(JNIEnv *env, jstring address);
296 * @brief Reordering for the list (connected device list)
297 * @param index [IN] index of device list that want to reordering
298 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
300 CAResult_t CALEServerReorderinglist(uint32_t index);
303 * @brief initialize mutex
304 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
306 CAResult_t CALEServerInitMutexVaraibles();
309 * @brief terminate mutex
312 void CALEServerTerminateMutexVaraibles();
315 * @brief initialize condition
316 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
318 CAResult_t CALEServerInitConditionVaraibles();
321 * @brief terminate condition
324 void CALEServerTerminateConditionVaraibles();
330 #endif /* CA_LESERVER_H_ */