replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / android / caleserver.h
1 /* ****************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 /**
22  * @file
23  * This file contains the APIs for BT LE communications.
24  */
25 #ifndef CA_LESERVER_H_
26 #define CA_LESERVER_H_
27
28 #include "cacommon.h"
29 #include "cathreadpool.h"
30 #include "uarraylist.h"
31 #include "jni.h"
32
33 #ifdef __cplusplus
34 extern "C"
35 {
36 #endif
37
38 /**
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).
43  */
44 typedef void (*CAPacketReceiveCallback)(const char *address, const uint8_t *data);
45
46 /**
47  * initialize server for BLE.
48  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
49  */
50 CAResult_t CALEServerInitialize();
51
52 /**
53  * terminate client for BLE.
54  */
55 void CALEServerTerminate();
56
57 /**
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).
63  */
64 CAResult_t CALEServerSendUnicastMessage(const char *address, const uint8_t *data, uint32_t dataLen);
65
66 /**
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).
71  */
72 CAResult_t CALEServerSendMulticastMessage(const uint8_t *data, uint32_t dataLen);
73
74 /**
75  * start multicast server (start advertise).
76  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
77  */
78 CAResult_t CALEServerStartMulticastServer();
79
80 /**
81  * stop multicast server (stop discovery).
82  * @return  None
83  */
84 CAResult_t CALEServerStopMulticastServer();
85
86 /**
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.
90  */
91 void CALEServerSetCallback(CAPacketReceiveCallback callback);
92
93 /**
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).
100  */
101 CAResult_t CALEServerSendUnicastMessageImpl(JNIEnv *env, const char *address, const uint8_t *data,
102                                             uint32_t dataLen);
103
104 /**
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).
110  */
111 CAResult_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data, uint32_t dataLen);
112
113 /**
114  * set context of application.
115  */
116 void CALEServerJNISetContext();
117
118 /**
119  * initialize JNI object.
120  */
121 void CALeServerJniInit();
122
123 /**
124  * create interface object and initialize the object.
125  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
126  */
127 CAResult_t CALEServerCreateJniInterfaceObject();
128
129 /**
130  * start advertise in gatt server.
131  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
132  */
133 CAResult_t CALEServerStartAdvertise();
134
135 /**
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).
141  */
142 CAResult_t CALEServerStartAdvertiseImpl(JNIEnv *env, jobject advertiseCallback);
143
144 /**
145  * stop advertise in gatt server.
146  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
147  */
148 CAResult_t CALEServerStopAdvertise();
149
150 /**
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).
156  */
157 CAResult_t CALEServerStopAdvertiseImpl(JNIEnv *env, jobject advertiseCallback);
158
159 /**
160  * open a gatt server.
161  * @param[in]   env                JNI interface pointer.
162  * @return  gatt server object.
163  */
164 jobject CALEServerOpenGattServer(JNIEnv *env);
165
166 /**
167  * create gatt service
168  * @param[in]   env                JNI interface pointer
169  * @return  gatt service object
170  */
171 jobject CALEServerCreateGattService(JNIEnv *env);
172
173 /**
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).
178  */
179 CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic);
180
181 /**
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).
187  */
188 CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
189                                     jobject bluetoothGattService);
190
191 /**
192  * start gatt server.
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).
197  */
198 CAResult_t CALEServerStartGattServer(JNIEnv *env, jobject gattServerCallback);
199
200 /**
201  * close gatt server.
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).
205  */
206 CAResult_t CALEServerGattClose(JNIEnv *env, jobject bluetoothGattServer);
207 /**
208  * send data.
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).
213  */
214 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jstring data);
215
216 /**
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.
221  */
222 jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData);
223
224 /**
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).
230  */
231 CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData);
232
233 /**
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).
242  */
243 CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
244                                   jint offset, jbyteArray value);
245
246 /**
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).
251  */
252 CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice);
253
254 /**
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).
258  */
259 CAResult_t CALEServerDisconnectAllDevices(JNIEnv *env);
260
261 /**
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).
266  */
267 CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice);
268
269 /**
270  * create connected device list.
271  */
272 void CALEServerCreateCachedDeviceList();
273
274 /**
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.
279  */
280 bool CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress);
281
282 /**
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).
287  */
288 CAResult_t CALEServerAddDeviceToList(JNIEnv *env, jobject device);
289
290 /**
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).
294  */
295 CAResult_t CALEServerRemoveAllDevices(JNIEnv *env);
296
297 /**
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).
302  */
303 CAResult_t CALEServerRemoveDevice(JNIEnv *env, jstring address);
304
305 /**
306  * initialize mutex.
307  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
308  */
309 CAResult_t CALEServerInitMutexVaraibles();
310
311 /**
312  * terminate mutex.
313  */
314 void CALEServerTerminateMutexVaraibles();
315
316 /**
317  * initialize condition.
318  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
319  */
320 CAResult_t CALEServerInitConditionVaraibles();
321
322 /**
323  * terminate condition.
324  */
325 void CALEServerTerminateConditionVaraibles();
326
327 /**
328  * check connection status.
329  * @param[in] address      the address of the remote device.
330  * @return  true or false.
331  */
332 bool CALEServerIsConnected(const char* address);
333
334 /**
335  * get MTU size.
336  * @param[in] address      the address of the remote device.
337  * @return  mtu size negotiated from remote device.
338  */
339 uint16_t CALEServerGetMtuSize(const char* address);
340 #ifdef __cplusplus
341 } /* extern "C" */
342 #endif
343
344 #endif /* CA_LESERVER_H_ */