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.
132 CAResult_t CALEStartAdvertise();
135 * start advertise in gatt server.
136 * @param[in] env JNI interface pointer.
137 * @param[in] advertiseCallback callback to be notified on reception of
138 * advertisement result.
139 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
141 CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback);
144 * stop advertise in gatt server.
145 * @param[in] env JNI interface pointer.
146 * @param[in] advertiseCallback callback to be notified on reception of
147 * advertisement result.
148 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
150 CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback);
153 * open a gatt server.
154 * @param[in] env JNI interface pointer.
155 * @return gatt server object.
157 jobject CALEServerOpenGattServer(JNIEnv *env);
160 * create gatt service
161 * @param[in] env JNI interface pointer
162 * @return gatt service object
164 jobject CALEServerCreateGattService(JNIEnv *env);
167 * add a descriptor to the characteristic.
168 * @param[in] env JNI interface pointer.
169 * @param[in] characteristic Characteristic object.
170 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
172 CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic);
175 * create gatt service.
176 * @param[in] env JNI interface pointer.
177 * @param[in] bluetoothGattServer Bluetooth Gatt Server object.
178 * @param[in] bluetoothGattService Bluetooth Gatt Service object.
179 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
181 CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
182 jobject bluetoothGattService);
186 * @param[in] env JNI interface pointer.
187 * @param[in] gattServerCallback callback to be notified on reception of
188 * state change of gatt server.
189 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
191 CAResult_t CALEServerStartGattServer(JNIEnv *env, jobject gattServerCallback);
195 * @param[in] env JNI interface pointer.
196 * @param[in] bluetoothGattServer Gatt Server object.
197 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
199 CAResult_t CALEServerGattClose(JNIEnv *env, jobject bluetoothGattServer);
202 * @param[in] env JNI interface pointer.
203 * @param[in] bluetoothDevice bluetooth device object.
204 * @param[in] data data which send.
205 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
207 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jstring data);
210 * set data in BluetoothGattCharacteristic.
211 * @param[in] env JNI interface pointer.
212 * @param[in] responseData data to set in characteristic object.
213 * @return BluetoothGattCharacteristic object.
215 jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData);
218 * send data through notifyCharacteristicChanged api of android.
219 * @param[in] env JNI interface pointer.
220 * @param[in] device bluetooth device object.
221 * @param[in] responseData data which send.
222 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
224 CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData);
227 * send a response to a write request to a remote device.
228 * @param[in] env JNI interface pointer.
229 * @param[in] device bluetooth device object.
230 * @param[in] requestId the id of request.
231 * @param[in] status the status of the request to be sent to the remote devices.
232 * @param[in] offset value offset for partial write response.
233 * @param[in] value the value of the attribute that written (optional).
234 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
236 CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
237 jint offset, jbyteArray value);
240 * connect BLE to remote device form gatt server.
241 * @param[in] env JNI interface pointer.
242 * @param[in] bluetoothDevice bluetooth device object.
243 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
245 CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice);
248 * disconnect LE for all devices.
249 * @param[in] env JNI interface pointer.
250 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
252 CAResult_t CALEServerDisconnectAllDevices(JNIEnv *env);
255 * disconnect LE to remote device form gatt server.
256 * @param[in] env JNI interface pointer.
257 * @param[in] bluetoothDevice bluetooth device object.
258 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
260 CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice);
263 * create connected device list.
265 void CALEServerCreateCachedDeviceList();
268 * check whether the device exist in the list or not.
269 * @param[in] env JNI interface pointer.
270 * @param[in] remoteAddress remote address.
271 * @return true or false.
273 bool CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress);
276 * add device object to the list (connected device list).
277 * @param[in] env JNI interface pointer.
278 * @param[in] device bluetooth device object.
279 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
281 CAResult_t CALEServerAddDeviceToList(JNIEnv *env, jobject device);
284 * remove all devices objects in the list (connected device list).
285 * @param[in] env JNI interface pointer.
286 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
288 CAResult_t CALEServerRemoveAllDevices(JNIEnv *env);
291 * remove target device in the list (connected device list).
292 * @param[in] env JNI interface pointer.
293 * @param[in] address target address to remove.
294 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
296 CAResult_t CALEServerRemoveDevice(JNIEnv *env, jstring address);
300 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
302 CAResult_t CALEServerInitMutexVaraibles();
307 void CALEServerTerminateMutexVaraibles();
310 * initialize condition.
311 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
313 CAResult_t CALEServerInitConditionVaraibles();
316 * terminate condition.
318 void CALEServerTerminateConditionVaraibles();
324 #endif /* CA_LESERVER_H_ */