Merge branch 'master' into easysetup & CBOR changes
[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  * @param[in]   env                JNI interface pointer.
133  * @param[in]   advertiseCallback  callback to be notified on reception of
134  *                                 advertisement result.
135  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
136  */
137 CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback);
138
139 /**
140  * stop advertise in gatt server.
141  * @param[in]   env                JNI interface pointer.
142  * @param[in]   advertiseCallback  callback to be notified on reception of
143  *                                 advertisement result.
144  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
145  */
146 CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback);
147
148 /**
149  * open a gatt server.
150  * @param[in]   env                JNI interface pointer.
151  * @return  gatt server object.
152  */
153 jobject CALEServerOpenGattServer(JNIEnv *env);
154
155 /**
156  * create gatt service
157  * @param[in]   env                JNI interface pointer
158  * @return  gatt service object
159  */
160 jobject CALEServerCreateGattService(JNIEnv *env);
161
162 /**
163  * add a descriptor to the characteristic.
164  * @param[in]   env                JNI interface pointer.
165  * @param[in]   characteristic     Characteristic object.
166  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
167  */
168 CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic);
169
170 /**
171  * create gatt service.
172  * @param[in]   env                  JNI interface pointer.
173  * @param[in]   bluetoothGattServer  Bluetooth Gatt Server object.
174  * @param[in]   bluetoothGattService Bluetooth Gatt Service object.
175  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
176  */
177 CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
178                                     jobject bluetoothGattService);
179
180 /**
181  * start gatt server.
182  * @param[in]   env                  JNI interface pointer.
183  * @param[in]   gattServerCallback   callback to be notified on reception of
184  *                                   state change of gatt server.
185  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
186  */
187 CAResult_t CALEServerStartGattServer(JNIEnv *env, jobject gattServerCallback);
188
189 /**
190  * close gatt server.
191  * @param[in]   env                  JNI interface pointer.
192  * @param[in]   bluetoothGattServer  Gatt Server object.
193  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
194  */
195 CAResult_t CALEServerGattClose(JNIEnv *env, jobject bluetoothGattServer);
196 /**
197  * send data.
198  * @param[in]   env                  JNI interface pointer.
199  * @param[in]   bluetoothDevice      bluetooth device object.
200  * @param[in]   data                 data which send.
201  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
202  */
203 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jstring data);
204
205 /**
206  * set data in BluetoothGattCharacteristic.
207  * @param[in]   env                  JNI interface pointer.
208  * @param[in]   responseData         data to set in characteristic object.
209  * @return  BluetoothGattCharacteristic object.
210  */
211 jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData);
212
213 /**
214  * send data through notifyCharacteristicChanged api of android.
215  * @param[in]   env                  JNI interface pointer.
216  * @param[in]   device               bluetooth device object.
217  * @param[in]   responseData         data which send.
218  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
219  */
220 CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData);
221
222 /**
223  * send a response to a write request to a remote device.
224  * @param[in]   env                  JNI interface pointer.
225  * @param[in]   device               bluetooth device object.
226  * @param[in]   requestId            the id of request.
227  * @param[in]   status               the status of the request to be sent to the remote devices.
228  * @param[in]   offset               value offset for partial write response.
229  * @param[in]   value                the value of the attribute that written (optional).
230  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
231  */
232 CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
233                                   jint offset, jbyteArray value);
234
235 /**
236  * connect BLE to remote device form gatt server.
237  * @param[in]   env                  JNI interface pointer.
238  * @param[in]   bluetoothDevice      bluetooth device object.
239  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
240  */
241 CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice);
242
243 /**
244  * disconnect LE for all devices.
245  * @param[in]   env                  JNI interface pointer.
246  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
247  */
248 CAResult_t CALEServerDisconnectAllDevices(JNIEnv *env);
249
250 /**
251  * disconnect LE to remote device form gatt server.
252  * @param[in]   env                  JNI interface pointer.
253  * @param[in]   bluetoothDevice      bluetooth device object.
254  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
255  */
256 CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice);
257
258 /**
259  * create connected device list.
260  */
261 void CALEServerCreateCachedDeviceList();
262
263 /**
264  * check whether the device exist in the list or not.
265  * @param[in]   env                   JNI interface pointer.
266  * @param[in]   remoteAddress         remote address.
267  * @return  true or false.
268  */
269 bool CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress);
270
271 /**
272  * add device object to the list (connected device list).
273  * @param[in]   env                   JNI interface pointer.
274  * @param[in]   device                bluetooth device object.
275  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
276  */
277 CAResult_t CALEServerAddDeviceToList(JNIEnv *env, jobject device);
278
279 /**
280  * remove all devices objects in the list (connected device list).
281  * @param[in]   env                   JNI interface pointer.
282  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
283  */
284 CAResult_t CALEServerRemoveAllDevices(JNIEnv *env);
285
286 /**
287  * remove target device in the list (connected device list).
288  * @param[in]   env                   JNI interface pointer.
289  * @param[in]   address               target address to remove.
290  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
291  */
292 CAResult_t CALEServerRemoveDevice(JNIEnv *env, jstring address);
293
294 /**
295  * initialize mutex.
296  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
297  */
298 CAResult_t CALEServerInitMutexVaraibles();
299
300 /**
301  * terminate mutex.
302  */
303 void CALEServerTerminateMutexVaraibles();
304
305 /**
306  * initialize condition.
307  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
308  */
309 CAResult_t CALEServerInitConditionVaraibles();
310
311 /**
312  * terminate condition.
313  */
314 void CALEServerTerminateConditionVaraibles();
315
316 #ifdef __cplusplus
317 } /* extern "C" */
318 #endif
319
320 #endif /* CA_LESERVER_H_ */