Imported Upstream version 0.9.2
[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 caleserver.h
23  * @brief 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  * @brief Callback to be notified on reception of any data from remote devices.
40  * @param  address           [IN] MAC address of remote device.
41  * @param  data              [IN] Data received from remote device.
42  * @return None
43  * @pre  Callback must be registered using CALEServerSetCallback(CAPacketReceiveCallback callback)
44  */
45 typedef void (*CAPacketReceiveCallback)(const char *address, const char *data);
46
47 /**
48  * @brief   initialize server for BLE
49  * @param   handle           [IN] thread pool handle object
50  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
51  */
52 CAResult_t CALEServerInitialize(ca_thread_pool_t handle);
53
54 /**
55  * @brief   terminate client for BLE
56  * @return  None
57  */
58 void CALEServerTerminate();
59
60 /**
61  * @brief   send data for unicast (interface)
62  * @param   address          [IN] remote address
63  * @param   data             [IN] data for transmission
64  * @param   dataLen          [IN] data length
65  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
66  */
67 CAResult_t CALEServerSendUnicastMessage(const char *address, const char *data, uint32_t dataLen);
68
69 /**
70  * @brief   send data for multicast (interface)
71  * @param   data             [IN] data for transmission
72  * @param   dataLen          [IN] data length
73  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
74  */
75 CAResult_t CALEServerSendMulticastMessage(const char *data, uint32_t dataLen);
76
77 /**
78  * @brief   start multicast server (start advertise)
79  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
80  */
81 CAResult_t CALEServerStartMulticastServer();
82
83 /**
84  * @brief   stop multicast server (stop discovery)
85  * @return  None
86  */
87 CAResult_t CALEServerStopMulticastServer();
88
89 /**
90  * @brief   set this callback for receiving data packets from peer devices.
91  * @param   callback         [IN] callback to be notified on reception of
92  *                                unicast/multicast data packets.
93  * @return  None
94  */
95 void CALEServerSetCallback(CAPacketReceiveCallback callback);
96
97 /**
98  * @brief   send data for unicast (implement)
99  * @param   env              [IN] JNI interface pointer
100  * @param   address          [IN] remote address
101  * @param   data             [IN] data for transmission
102  * @param   dataLen          [IN] data length
103  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
104  */
105 CAResult_t CALEServerSendUnicastMessageImpl(JNIEnv *env, const char *address, const char *data,
106                                             uint32_t dataLen);
107
108 /**
109  * @brief   send data for multicast (implement)
110  * @param   env              [IN] JNI interface pointer
111  * @param   data             [IN] data for transmission
112  * @param   dataLen          [IN] data length
113  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
114  */
115 CAResult_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const char *data, uint32_t dataLen);
116
117 /* Android BLE Server Functions */
118 /**
119  * @brief   set context of application
120  * @return  None
121  */
122 void CALEServerJNISetContext();
123
124 /**
125  * @brief   initialize JNI object
126  * @return  None
127  */
128 void CALeServerJniInit();
129
130 /**
131  * @brief   create interface object and initialize the object
132  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
133  */
134 CAResult_t CALEServerCreateJniInterfaceObject();
135
136 /**
137  * @brief   start advertise in gatt server
138  * @param   env                [IN] JNI interface pointer
139  * @param   advertiseCallback  [IN] callback to be notified on reception of
140  *                                advertisement result
141  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
142  */
143 CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback);
144
145 /**
146  * @brief   stop advertise in gatt server
147  * @param   env                [IN] JNI interface pointer
148  * @param   advertiseCallback  [IN] callback to be notified on reception of
149  *                                advertisement result
150  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
151  */
152 CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback);
153
154 /**
155  * @brief   open a gatt server
156  * @param   env                [IN] JNI interface pointer
157  * @return  gatt server object
158  */
159 jobject CALEServerOpenGattServer(JNIEnv *env);
160
161 /**
162  * @brief   create gatt service
163  * @param   env                [IN] JNI interface pointer
164  * @return  gatt service object
165  */
166 jobject CALEServerCreateGattService(JNIEnv *env);
167
168 /**
169  * @brief   add a descriptor to the characteristic
170  * @param   env                [IN] JNI interface pointer
171  * @param   characteristic     [IN] Characteristic object
172  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
173  */
174 CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic);
175
176 /**
177  * @brief   create gatt service
178  * @param   env                  [IN] JNI interface pointer
179  * @param   bluetoothGattServer  [IN] Bluetooth Gatt Server object
180  * @param   bluetoothGattService [IN] Bluetooth Gatt Service object
181  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
182  */
183 CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
184                                     jobject bluetoothGattService);
185
186 /**
187  * @brief   start gatt server
188  * @param   env                  [IN] JNI interface pointer
189  * @param   gattServerCallback   [IN] callback to be notified on reception of
190  *                                state change of gatt server
191  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
192  */
193 CAResult_t CALEServerStartGattServer(JNIEnv *env, jobject gattServerCallback);
194
195 /**
196  * @brief   close gatt server
197  * @param   env                  [IN] JNI interface pointer
198  * @param   bluetoothGattServer  [IN] Gatt Server object
199  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
200  */
201 CAResult_t CALEServerGattClose(JNIEnv *env, jobject bluetoothGattServer);
202 /**
203  * @brief   send data
204  * @param   env                  [IN] JNI interface pointer
205  * @param   bluetoothDevice      [IN] bluetooth device object
206  * @param   data                 [IN] data which send
207  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
208  */
209 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jstring data);
210
211 /**
212  * @brief   set data in BluetoothGattCharacteristic
213  * @param   env                  [IN] JNI interface pointer
214  * @param   responseData         [IN] data to set in characteristic object
215  * @return  BluetoothGattCharacteristic object
216  */
217 jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData);
218
219 /**
220  * @brief   send data through notifyCharacteristicChanged api of android
221  * @param   env                  [IN] JNI interface pointer
222  * @param   device               [IN] bluetooth device object
223  * @param   responseData         [IN] data which send
224  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
225  */
226 CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData);
227
228 /**
229  * @brief   send a response to a write request to a remote device
230  * @param   env                  [IN] JNI interface pointer
231  * @param   device               [IN] bluetooth device object
232  * @param   requestId            [IN] the id of request
233  * @param   status               [IN] the status of the request to be sent to the remote devices
234  * @param   offset               [IN] value offset for partial write response
235  * @param   value                [IN] the value of the attribute that written (optional)
236  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
237  */
238 CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
239                                   jint offset, jbyteArray value);
240
241 /**
242  * @brief   connect BLE to remote device form gatt server
243  * @param   env                  [IN] JNI interface pointer
244  * @param   bluetoothDevice      [IN] bluetooth device object
245  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
246  */
247 CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice);
248
249 /**
250  * @brief   disconnect LE for all devices
251  * @param   env                  [IN] JNI interface pointer
252  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
253  */
254 CAResult_t CALEServerDisconnectAllDevices(JNIEnv *env);
255
256 /**
257  * @brief   disconnect LE to remote device form gatt server
258  * @param   env                  [IN] JNI interface pointer
259  * @param   bluetoothDevice      [IN] bluetooth device object
260  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
261  */
262 CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice);
263
264 /* BLE Server Utils */
265 /**
266  * @brief   create connected device list
267  * @return  None
268  */
269 void CALEServerCreateCachedDeviceList();
270
271 /**
272  * @brief   check whether the device exist in the list or not
273  * @param   env                   [IN] JNI interface pointer
274  * @param   remoteAddress         [IN] remote address
275  * @return  true or false
276  */
277 bool CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress);
278
279 /**
280  * @brief   add device object to the list (connected device list)
281  * @param   env                   [IN] JNI interface pointer
282  * @param   device                [IN] bluetooth device object
283  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
284  */
285 CAResult_t CALEServerAddDeviceToList(JNIEnv *env, jobject device);
286
287 /**
288  * @brief   remove all devices objects in the list (connected device list)
289  * @param   env                   [IN] JNI interface pointer
290  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
291  */
292 CAResult_t CALEServerRemoveAllDevices(JNIEnv *env);
293
294 /**
295  * @brief   remove target device in the list (connected device list)
296  * @param   env                   [IN] JNI interface pointer
297  * @param   address               [IN] target address to remove
298  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
299  */
300 CAResult_t CALEServerRemoveDevice(JNIEnv *env, jstring address);
301
302 /**
303  * @brief   Reordering for the list (connected device list)
304  * @param   index                 [IN] index of device list that want to reordering
305  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
306  */
307 CAResult_t CALEServerReorderinglist(uint32_t index);
308
309 /**
310  * @brief   initialize mutex
311  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
312  */
313 CAResult_t CALEServerInitMutexVaraibles();
314
315 /**
316  * @brief   terminate mutex
317  * @return  None
318  */
319 void CALEServerTerminateMutexVaraibles();
320
321 /**
322  * @brief   initialize condition
323  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
324  */
325 CAResult_t CALEServerInitConditionVaraibles();
326
327 /**
328  * @brief   terminate condition
329  * @return  None
330  */
331 void CALEServerTerminateConditionVaraibles();
332
333 #ifdef __cplusplus
334 } /* extern "C" */
335 #endif
336
337 #endif /* CA_LESERVER_H_ */