Update RD with latest master
[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  * @param[in]   handle           thread pool handle object.
49  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
50  */
51 CAResult_t CALEServerInitialize(ca_thread_pool_t handle);
52
53 /**
54  * terminate client for BLE.
55  */
56 void CALEServerTerminate();
57
58 /**
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).
64  */
65 CAResult_t CALEServerSendUnicastMessage(const char *address, const uint8_t *data, uint32_t dataLen);
66
67 /**
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).
72  */
73 CAResult_t CALEServerSendMulticastMessage(const uint8_t *data, uint32_t dataLen);
74
75 /**
76  * start multicast server (start advertise).
77  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
78  */
79 CAResult_t CALEServerStartMulticastServer();
80
81 /**
82  * stop multicast server (stop discovery).
83  * @return  None
84  */
85 CAResult_t CALEServerStopMulticastServer();
86
87 /**
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.
91  */
92 void CALEServerSetCallback(CAPacketReceiveCallback callback);
93
94 /**
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).
101  */
102 CAResult_t CALEServerSendUnicastMessageImpl(JNIEnv *env, const char *address, const uint8_t *data,
103                                             uint32_t dataLen);
104
105 /**
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).
111  */
112 CAResult_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data, uint32_t dataLen);
113
114 /**
115  * set context of application.
116  */
117 void CALEServerJNISetContext();
118
119 /**
120  * initialize JNI object.
121  */
122 void CALeServerJniInit();
123
124 /**
125  * create interface object and initialize the object.
126  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
127  */
128 CAResult_t CALEServerCreateJniInterfaceObject();
129
130 /**
131  * start advertise in gatt server.
132  */
133 CAResult_t CALEStartAdvertise();
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 CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback);
143
144 /**
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).
150  */
151 CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback);
152
153 /**
154  * open a gatt server.
155  * @param[in]   env                JNI interface pointer.
156  * @return  gatt server object.
157  */
158 jobject CALEServerOpenGattServer(JNIEnv *env);
159
160 /**
161  * create gatt service
162  * @param[in]   env                JNI interface pointer
163  * @return  gatt service object
164  */
165 jobject CALEServerCreateGattService(JNIEnv *env);
166
167 /**
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).
172  */
173 CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic);
174
175 /**
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).
181  */
182 CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
183                                     jobject bluetoothGattService);
184
185 /**
186  * start gatt server.
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).
191  */
192 CAResult_t CALEServerStartGattServer(JNIEnv *env, jobject gattServerCallback);
193
194 /**
195  * close gatt server.
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).
199  */
200 CAResult_t CALEServerGattClose(JNIEnv *env, jobject bluetoothGattServer);
201 /**
202  * send data.
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).
207  */
208 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jstring data);
209
210 /**
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.
215  */
216 jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData);
217
218 /**
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).
224  */
225 CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData);
226
227 /**
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).
236  */
237 CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
238                                   jint offset, jbyteArray value);
239
240 /**
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).
245  */
246 CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice);
247
248 /**
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).
252  */
253 CAResult_t CALEServerDisconnectAllDevices(JNIEnv *env);
254
255 /**
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).
260  */
261 CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice);
262
263 /**
264  * create connected device list.
265  */
266 void CALEServerCreateCachedDeviceList();
267
268 /**
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.
273  */
274 bool CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress);
275
276 /**
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).
281  */
282 CAResult_t CALEServerAddDeviceToList(JNIEnv *env, jobject device);
283
284 /**
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).
288  */
289 CAResult_t CALEServerRemoveAllDevices(JNIEnv *env);
290
291 /**
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).
296  */
297 CAResult_t CALEServerRemoveDevice(JNIEnv *env, jstring address);
298
299 /**
300  * initialize mutex.
301  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
302  */
303 CAResult_t CALEServerInitMutexVaraibles();
304
305 /**
306  * terminate mutex.
307  */
308 void CALEServerTerminateMutexVaraibles();
309
310 /**
311  * initialize condition.
312  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
313  */
314 CAResult_t CALEServerInitConditionVaraibles();
315
316 /**
317  * terminate condition.
318  */
319 void CALEServerTerminateConditionVaraibles();
320
321 #ifdef __cplusplus
322 } /* extern "C" */
323 #endif
324
325 #endif /* CA_LESERVER_H_ */