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 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
50 CAResult_t CALEServerInitialize();
53 * terminate client for BLE.
55 void CALEServerTerminate();
58 * send data for unicast (interface).
59 * @param[in] address remote address.
60 * @param[in] data data for transmission.
61 * @param[in] dataLen data length.
62 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
64 CAResult_t CALEServerSendUnicastMessage(const char *address, const uint8_t *data, uint32_t dataLen);
67 * send data for multicast (interface).
68 * @param[in] data data for transmission.
69 * @param[in] dataLen data length.
70 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
72 CAResult_t CALEServerSendMulticastMessage(const uint8_t *data, uint32_t dataLen);
75 * start multicast server (start advertise).
76 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
78 CAResult_t CALEServerStartMulticastServer();
81 * stop multicast server (stop discovery).
84 CAResult_t CALEServerStopMulticastServer();
87 * set this callback for receiving data packets from peer devices.
88 * @param[in] callback callback to be notified on reception of
89 * unicast/multicast data packets.
91 void CALEServerSetCallback(CAPacketReceiveCallback callback);
94 * send data for unicast (implement).
95 * @param[in] env JNI interface pointer.
96 * @param[in] address remote address.
97 * @param[in] data data for transmission.
98 * @param[in] dataLen data length.
99 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
101 CAResult_t CALEServerSendUnicastMessageImpl(JNIEnv *env, const char *address, const uint8_t *data,
105 * send data for multicast (implement)
106 * @param[in] env JNI interface pointer
107 * @param[in] data data for transmission
108 * @param[in] dataLen data length
109 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
111 CAResult_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data, uint32_t dataLen);
114 * set context of application.
116 void CALEServerJNISetContext();
119 * initialize JNI object.
121 void CALeServerJniInit();
124 * create interface object and initialize the object.
125 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
127 CAResult_t CALEServerCreateJniInterfaceObject();
130 * start advertise in gatt server.
131 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
133 CAResult_t CALEServerStartAdvertise();
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 CALEServerStartAdvertiseImpl(JNIEnv *env, jobject advertiseCallback);
145 * stop advertise in gatt server.
146 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
148 CAResult_t CALEServerStopAdvertise();
151 * stop advertise in gatt server.
152 * @param[in] env JNI interface pointer.
153 * @param[in] advertiseCallback callback to be notified on reception of
154 * advertisement result.
155 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
157 CAResult_t CALEServerStopAdvertiseImpl(JNIEnv *env, jobject advertiseCallback);
160 * open a gatt server.
161 * @param[in] env JNI interface pointer.
162 * @return gatt server object.
164 jobject CALEServerOpenGattServer(JNIEnv *env);
167 * create gatt service
168 * @param[in] env JNI interface pointer
169 * @return gatt service object
171 jobject CALEServerCreateGattService(JNIEnv *env);
174 * add a descriptor to the characteristic.
175 * @param[in] env JNI interface pointer.
176 * @param[in] characteristic Characteristic object.
177 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
179 CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic);
182 * create gatt service.
183 * @param[in] env JNI interface pointer.
184 * @param[in] bluetoothGattServer Bluetooth Gatt Server object.
185 * @param[in] bluetoothGattService Bluetooth Gatt Service object.
186 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
188 CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
189 jobject bluetoothGattService);
193 * @param[in] env JNI interface pointer.
194 * @param[in] gattServerCallback callback to be notified on reception of
195 * state change of gatt server.
196 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
198 CAResult_t CALEServerStartGattServer(JNIEnv *env, jobject gattServerCallback);
202 * @param[in] env JNI interface pointer.
203 * @param[in] bluetoothGattServer Gatt Server object.
204 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
206 CAResult_t CALEServerGattClose(JNIEnv *env, jobject bluetoothGattServer);
209 * @param[in] env JNI interface pointer.
210 * @param[in] bluetoothDevice bluetooth device object.
211 * @param[in] data data which send.
212 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
214 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jstring data);
217 * set data in BluetoothGattCharacteristic.
218 * @param[in] env JNI interface pointer.
219 * @param[in] responseData data to set in characteristic object.
220 * @return BluetoothGattCharacteristic object.
222 jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData);
225 * send data through notifyCharacteristicChanged api of android.
226 * @param[in] env JNI interface pointer.
227 * @param[in] device bluetooth device object.
228 * @param[in] responseData data which send.
229 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
231 CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData);
234 * send a response to a write request to a remote device.
235 * @param[in] env JNI interface pointer.
236 * @param[in] device bluetooth device object.
237 * @param[in] requestId the id of request.
238 * @param[in] status the status of the request to be sent to the remote devices.
239 * @param[in] offset value offset for partial write response.
240 * @param[in] value the value of the attribute that written (optional).
241 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
243 CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
244 jint offset, jbyteArray value);
247 * connect BLE to remote device form gatt server.
248 * @param[in] env JNI interface pointer.
249 * @param[in] bluetoothDevice bluetooth device object.
250 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
252 CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice);
255 * disconnect LE for all devices.
256 * @param[in] env JNI interface pointer.
257 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
259 CAResult_t CALEServerDisconnectAllDevices(JNIEnv *env);
262 * disconnect LE to remote device form gatt server.
263 * @param[in] env JNI interface pointer.
264 * @param[in] bluetoothDevice bluetooth device object.
265 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
267 CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice);
270 * create connected device list.
272 void CALEServerCreateCachedDeviceList();
275 * check whether the device exist in the list or not.
276 * @param[in] env JNI interface pointer.
277 * @param[in] remoteAddress remote address.
278 * @return true or false.
280 bool CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress);
283 * add device object to the list (connected device list).
284 * @param[in] env JNI interface pointer.
285 * @param[in] device bluetooth device object.
286 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
288 CAResult_t CALEServerAddDeviceToList(JNIEnv *env, jobject device);
291 * remove all devices objects in the list (connected device list).
292 * @param[in] env JNI interface pointer.
293 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
295 CAResult_t CALEServerRemoveAllDevices(JNIEnv *env);
298 * remove target device in the list (connected device list).
299 * @param[in] env JNI interface pointer.
300 * @param[in] address target address to remove.
301 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
303 CAResult_t CALEServerRemoveDevice(JNIEnv *env, jstring address);
307 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
309 CAResult_t CALEServerInitMutexVaraibles();
314 void CALEServerTerminateMutexVaraibles();
317 * initialize condition.
318 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
320 CAResult_t CALEServerInitConditionVaraibles();
323 * terminate condition.
325 void CALEServerTerminateConditionVaraibles();
328 * check connection status.
329 * @param[in] address the address of the remote device.
330 * @return true or false.
332 bool CALEServerIsConnected(const char* address);
336 * @param[in] address the address of the remote device.
337 * @return mtu size negotiated from remote device.
339 uint16_t CALEServerGetMtuSize(const char* address);
344 #endif /* CA_LESERVER_H_ */