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 * 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 * Callback to be notified on reception of any data from remote devices.
40 * @param[in] address MAC address of remote device.
41 * @param[in] data Data received from remote device.
42 * @pre Callback must be registered using CALEServerSetCallback(CAPacketReceiveCallback callback).
44 typedef void (*CAPacketReceiveCallback)(const char *address, const uint8_t *data);
47 * initialize server for BLE.
48 * @param[in] handle thread pool handle object.
49 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
51 CAResult_t CALEServerInitialize(ca_thread_pool_t handle);
54 * terminate client for BLE.
56 void CALEServerTerminate();
59 * send data for unicast (interface).
60 * @param[in] address remote address.
61 * @param[in] data data for transmission.
62 * @param[in] dataLen data length.
63 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
65 CAResult_t CALEServerSendUnicastMessage(const char *address, const uint8_t *data, uint32_t dataLen);
68 * send data for multicast (interface).
69 * @param[in] data data for transmission.
70 * @param[in] dataLen data length.
71 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
73 CAResult_t CALEServerSendMulticastMessage(const uint8_t *data, uint32_t dataLen);
76 * start multicast server (start advertise).
77 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
79 CAResult_t CALEServerStartMulticastServer();
82 * stop multicast server (stop discovery).
85 CAResult_t CALEServerStopMulticastServer();
88 * set this callback for receiving data packets from peer devices.
89 * @param[in] callback callback to be notified on reception of
90 * unicast/multicast data packets.
92 void CALEServerSetCallback(CAPacketReceiveCallback callback);
95 * send data for unicast (implement).
96 * @param[in] env JNI interface pointer.
97 * @param[in] address remote address.
98 * @param[in] data data for transmission.
99 * @param[in] dataLen data length.
100 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
102 CAResult_t CALEServerSendUnicastMessageImpl(JNIEnv *env, const char *address, const uint8_t *data,
106 * send data for multicast (implement)
107 * @param[in] env JNI interface pointer
108 * @param[in] data data for transmission
109 * @param[in] dataLen data length
110 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
112 CAResult_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data, uint32_t dataLen);
115 * set context of application.
117 void CALEServerJNISetContext();
120 * initialize JNI object.
122 void CALeServerJniInit();
125 * create interface object and initialize the object.
126 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
128 CAResult_t CALEServerCreateJniInterfaceObject();
131 * start advertise in gatt server.
132 * @param[in] env JNI interface pointer.
133 * @param[in] advertiseCallback callback to be notified on reception of
134 * advertisement result.
135 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
137 CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback);
140 * stop advertise in gatt server.
141 * @param[in] env JNI interface pointer.
142 * @param[in] advertiseCallback callback to be notified on reception of
143 * advertisement result.
144 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
146 CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback);
149 * open a gatt server.
150 * @param[in] env JNI interface pointer.
151 * @return gatt server object.
153 jobject CALEServerOpenGattServer(JNIEnv *env);
156 * create gatt service
157 * @param[in] env JNI interface pointer
158 * @return gatt service object
160 jobject CALEServerCreateGattService(JNIEnv *env);
163 * add a descriptor to the characteristic.
164 * @param[in] env JNI interface pointer.
165 * @param[in] characteristic Characteristic object.
166 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
168 CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic);
171 * create gatt service.
172 * @param[in] env JNI interface pointer.
173 * @param[in] bluetoothGattServer Bluetooth Gatt Server object.
174 * @param[in] bluetoothGattService Bluetooth Gatt Service object.
175 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
177 CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
178 jobject bluetoothGattService);
182 * @param[in] env JNI interface pointer.
183 * @param[in] gattServerCallback callback to be notified on reception of
184 * state change of gatt server.
185 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
187 CAResult_t CALEServerStartGattServer(JNIEnv *env, jobject gattServerCallback);
191 * @param[in] env JNI interface pointer.
192 * @param[in] bluetoothGattServer Gatt Server object.
193 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
195 CAResult_t CALEServerGattClose(JNIEnv *env, jobject bluetoothGattServer);
198 * @param[in] env JNI interface pointer.
199 * @param[in] bluetoothDevice bluetooth device object.
200 * @param[in] data data which send.
201 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
203 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jstring data);
206 * set data in BluetoothGattCharacteristic.
207 * @param[in] env JNI interface pointer.
208 * @param[in] responseData data to set in characteristic object.
209 * @return BluetoothGattCharacteristic object.
211 jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData);
214 * send data through notifyCharacteristicChanged api of android.
215 * @param[in] env JNI interface pointer.
216 * @param[in] device bluetooth device object.
217 * @param[in] responseData data which send.
218 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
220 CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData);
223 * send a response to a write request to a remote device.
224 * @param[in] env JNI interface pointer.
225 * @param[in] device bluetooth device object.
226 * @param[in] requestId the id of request.
227 * @param[in] status the status of the request to be sent to the remote devices.
228 * @param[in] offset value offset for partial write response.
229 * @param[in] value the value of the attribute that written (optional).
230 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
232 CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
233 jint offset, jbyteArray value);
236 * connect BLE to remote device form gatt server.
237 * @param[in] env JNI interface pointer.
238 * @param[in] bluetoothDevice bluetooth device object.
239 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
241 CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice);
244 * disconnect LE for all devices.
245 * @param[in] env JNI interface pointer.
246 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
248 CAResult_t CALEServerDisconnectAllDevices(JNIEnv *env);
251 * disconnect LE to remote device form gatt server.
252 * @param[in] env JNI interface pointer.
253 * @param[in] bluetoothDevice bluetooth device object.
254 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
256 CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice);
259 * create connected device list.
261 void CALEServerCreateCachedDeviceList();
264 * check whether the device exist in the list or not.
265 * @param[in] env JNI interface pointer.
266 * @param[in] remoteAddress remote address.
267 * @return true or false.
269 bool CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress);
272 * add device object to the list (connected device list).
273 * @param[in] env JNI interface pointer.
274 * @param[in] device bluetooth device object.
275 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
277 CAResult_t CALEServerAddDeviceToList(JNIEnv *env, jobject device);
280 * remove all devices objects in the list (connected device list).
281 * @param[in] env JNI interface pointer.
282 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
284 CAResult_t CALEServerRemoveAllDevices(JNIEnv *env);
287 * remove target device in the list (connected device list).
288 * @param[in] env JNI interface pointer.
289 * @param[in] address target address to remove.
290 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
292 CAResult_t CALEServerRemoveDevice(JNIEnv *env, jstring address);
296 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
298 CAResult_t CALEServerInitMutexVaraibles();
303 void CALEServerTerminateMutexVaraibles();
306 * initialize condition.
307 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
309 CAResult_t CALEServerInitConditionVaraibles();
312 * terminate condition.
314 void CALEServerTerminateConditionVaraibles();
320 #endif /* CA_LESERVER_H_ */