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.
133 CAResult_t CALEStartAdvertise();
136 * start advertise in gatt server.
137 * @param[in] env JNI interface pointer.
138 * @param[in] advertiseCallback callback to be notified on reception of
139 * advertisement result.
140 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
142 CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback);
145 * stop advertise in gatt server.
146 * @param[in] env JNI interface pointer.
147 * @param[in] advertiseCallback callback to be notified on reception of
148 * advertisement result.
149 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
151 CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback);
154 * open a gatt server.
155 * @param[in] env JNI interface pointer.
156 * @return gatt server object.
158 jobject CALEServerOpenGattServer(JNIEnv *env);
161 * create gatt service
162 * @param[in] env JNI interface pointer
163 * @return gatt service object
165 jobject CALEServerCreateGattService(JNIEnv *env);
168 * add a descriptor to the characteristic.
169 * @param[in] env JNI interface pointer.
170 * @param[in] characteristic Characteristic object.
171 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
173 CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic);
176 * create gatt service.
177 * @param[in] env JNI interface pointer.
178 * @param[in] bluetoothGattServer Bluetooth Gatt Server object.
179 * @param[in] bluetoothGattService Bluetooth Gatt Service object.
180 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
182 CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
183 jobject bluetoothGattService);
187 * @param[in] env JNI interface pointer.
188 * @param[in] gattServerCallback callback to be notified on reception of
189 * state change of gatt server.
190 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
192 CAResult_t CALEServerStartGattServer(JNIEnv *env, jobject gattServerCallback);
196 * @param[in] env JNI interface pointer.
197 * @param[in] bluetoothGattServer Gatt Server object.
198 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
200 CAResult_t CALEServerGattClose(JNIEnv *env, jobject bluetoothGattServer);
203 * @param[in] env JNI interface pointer.
204 * @param[in] bluetoothDevice bluetooth device object.
205 * @param[in] data data which send.
206 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
208 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jstring data);
211 * set data in BluetoothGattCharacteristic.
212 * @param[in] env JNI interface pointer.
213 * @param[in] responseData data to set in characteristic object.
214 * @return BluetoothGattCharacteristic object.
216 jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData);
219 * send data through notifyCharacteristicChanged api of android.
220 * @param[in] env JNI interface pointer.
221 * @param[in] device bluetooth device object.
222 * @param[in] responseData data which send.
223 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
225 CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData);
228 * send a response to a write request to a remote device.
229 * @param[in] env JNI interface pointer.
230 * @param[in] device bluetooth device object.
231 * @param[in] requestId the id of request.
232 * @param[in] status the status of the request to be sent to the remote devices.
233 * @param[in] offset value offset for partial write response.
234 * @param[in] value the value of the attribute that written (optional).
235 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
237 CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
238 jint offset, jbyteArray value);
241 * connect BLE to remote device form gatt server.
242 * @param[in] env JNI interface pointer.
243 * @param[in] bluetoothDevice bluetooth device object.
244 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
246 CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice);
249 * disconnect LE for all devices.
250 * @param[in] env JNI interface pointer.
251 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
253 CAResult_t CALEServerDisconnectAllDevices(JNIEnv *env);
256 * disconnect LE to remote device form gatt server.
257 * @param[in] env JNI interface pointer.
258 * @param[in] bluetoothDevice bluetooth device object.
259 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
261 CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice);
264 * create connected device list.
266 void CALEServerCreateCachedDeviceList();
269 * check whether the device exist in the list or not.
270 * @param[in] env JNI interface pointer.
271 * @param[in] remoteAddress remote address.
272 * @return true or false.
274 bool CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress);
277 * add device object to the list (connected device list).
278 * @param[in] env JNI interface pointer.
279 * @param[in] device bluetooth device object.
280 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
282 CAResult_t CALEServerAddDeviceToList(JNIEnv *env, jobject device);
285 * remove all devices objects in the list (connected device list).
286 * @param[in] env JNI interface pointer.
287 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
289 CAResult_t CALEServerRemoveAllDevices(JNIEnv *env);
292 * remove target device in the list (connected device list).
293 * @param[in] env JNI interface pointer.
294 * @param[in] address target address to remove.
295 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
297 CAResult_t CALEServerRemoveDevice(JNIEnv *env, jstring address);
301 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
303 CAResult_t CALEServerInitMutexVaraibles();
308 void CALEServerTerminateMutexVaraibles();
311 * initialize condition.
312 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
314 CAResult_t CALEServerInitConditionVaraibles();
317 * terminate condition.
319 void CALEServerTerminateConditionVaraibles();
325 #endif /* CA_LESERVER_H_ */