Replace glib threadpool usage with a 'dumb' thread implementation.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / 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  *
24  * This file contains the APIs for BT LE communications.
25  */
26
27 #ifndef __CA_LESERVER_H_
28 #define __CA_LESERVER_H_
29
30 #include "cacommon.h"
31 #include "cathreadpool.h"
32 #include "uarraylist.h"
33 #include "jni.h"
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #endif
39
40 /**
41  * @brief Callback to be notified on reception of any data from remote devices.
42  * @param  address          [IN] MAC address of remote device.
43  * @param  data             [IN] Data received from remote device.
44  * @return None
45  * @pre  Callback must be registered using CALEServerSetCallback(CAPacketReceiveCallback callback)
46  */
47 typedef void (*CAPacketReceiveCallback)(const char *address,
48         const char *data);
49
50 /**
51  * @brief   initialize server for BLE
52  * @param   handle           [IN] thread pool handle object
53  * @return  None
54  */
55 void CALEServerInitialize(ca_thread_pool_t handle);
56
57 /**
58  * @brief   terminate client for BLE
59  * @return  None
60  */
61 void CALEServerTerminate();
62
63 /**
64  * @brief   send data for unicast (interface)
65  * @param   address          [IN] remote address
66  * @param   data             [IN] data for transmission
67  * @param   dataLen          [IN] data length
68  * @return  #CA_STATUS_OK or Appropriate error code
69  * @retval  #CA_STATUS_OK  Successful
70  * @retval  #CA_STATUS_FAILED Operation failed
71  */
72 CAResult_t CALEServerSendUnicastMessage(const char *address, const char *data,
73         uint32_t dataLen);
74
75 /**
76  * @brief   send data for multicast (interface)
77  * @param   data             [IN] data for transmission
78  * @param   dataLen          [IN] data length
79  * @return  #CA_STATUS_OK or Appropriate error code
80  * @retval  #CA_STATUS_OK  Successful
81  * @retval  #CA_STATUS_FAILED Operation failed
82  */
83 CAResult_t CALEServerSendMulticastMessage(const char *data, uint32_t dataLen);
84
85 /**
86  * @brief   start unicast server
87  * @param   address          [IN] remote address
88  * @return  #CA_STATUS_OK or Appropriate error code
89  * @retval  #CA_STATUS_OK  Successful
90  * @retval  #CA_STATUS_FAILED Operation failed
91  */
92 CAResult_t CALEServerStartUnicastServer(const char *address);
93
94 /**
95  * @brief   start multicast server (start advertise)
96  * @return  #CA_STATUS_OK or Appropriate error code
97  * @retval  #CA_STATUS_OK  Successful
98  * @retval  #CA_STATUS_FAILED Operation failed
99  */
100 CAResult_t CALEServerStartMulticastServer();
101
102 /**
103  * @brief   stop unicast server
104  * @return  None
105  */
106 CAResult_t CALEServerStopUnicastServer();
107
108 /**
109  * @brief   stop multicast server (stop discovery)
110  * @return  None
111  */
112 CAResult_t CALEServerStopMulticastServer();
113
114 /**
115  * @brief   set this callback for receiving data packets from peer devices.
116  * @param   callback         [IN] callback to be notified on reception of
117  *                                unicast/multicast data packets.
118  * @return  None
119  */
120 void CALEServerSetCallback(CAPacketReceiveCallback callback);
121
122 /**
123  * @brief  Get local adapter network information.
124  * @param  info              [OUT] local connectivity information
125  * @param  size              [OUT] connectivity count
126  * @return  None
127  */
128 void CALEServerGetInterfaceInfo(CALocalConnectivity_t **info,
129         uint32_t *size);
130
131 /**
132  * @brief   get local address
133  * @param   address          [OUT] local address
134  * @return  None
135  */
136 void CALEServerGetLocalAddress(char *address);
137
138 /**
139  * @brief   send data for unicast (implement)
140  * @param   env              [IN] JNI interface pointer
141  * @param   address          [IN] remote address
142  * @param   data             [IN] data for transmission
143  * @param   dataLen          [IN] data length
144  * @return  #CA_STATUS_OK or Appropriate error code
145  * @retval  #CA_STATUS_OK  Successful
146  * @retval  #CA_STATUS_FAILED Operation failed
147  */
148 CAResult_t CALEServerSendUnicastMessageImpl(JNIEnv *env, const char *address,
149         const char *data, uint32_t dataLen);
150
151 /**
152  * @brief   send data for multicast (implement)
153  * @param   env              [IN] JNI interface pointer
154  * @param   address          [IN] remote address
155  * @return  #CA_STATUS_OK or Appropriate error code
156  * @retval  #CA_STATUS_OK  Successful
157  * @retval  #CA_STATUS_FAILED Operation failed
158  */
159 CAResult_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const char *data,
160         uint32_t dataLen);
161
162 /* Android BLE Server Functions */
163 /**
164  * @brief   set context of application
165  * @param   env              [IN] JNI interface pointer
166  * @param   context          [IN] context of application
167  * @return  None
168  */
169 void CALEServerJNISetContext(JNIEnv *env, jobject context);
170
171 /**
172  * @brief   initialize JNI object
173  * @param   env              [IN] JNI interface pointer
174  * @param   jvm              [IN] java virtual machine pointer
175  * @return  None
176  */
177 void CALeServerJniInit(JNIEnv *env, JavaVM* jvm);
178
179 /**
180  * @brief   create interface object and initialize the object
181  * @return  None
182  */
183 void CALeServerCreateJniInterfaceObject();
184
185 /**
186  * @brief   start advertise in gatt server
187  * @param   env                [IN] JNI interface pointer
188  * @param   advertiseCallback  [IN] callback to be notified on reception of
189  *                                advertisement result
190  * @return  None
191  */
192 void CANativeLEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback);
193
194 /**
195  * @brief   stop advertise in gatt server
196  * @param   env                [IN] JNI interface pointer
197  * @param   advertiseCallback  [IN] callback to be notified on reception of
198  *                                advertisement result
199  * @return  None
200  */
201 void CANativeLEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback);
202
203 /**
204  * @brief   open a gatt server
205  * @param   env                [IN] JNI interface pointer
206  * @return  gatt server object
207  */
208 jobject CANativeLEServerOpenGattServer(JNIEnv *env);
209
210 /**
211  * @brief   create gatt service
212  * @param   env                [IN] JNI interface pointer
213  * @return  gatt service object
214  */
215 jobject CANativeLEServerCreateGattService(JNIEnv *env);
216
217 /**
218  * @brief   create gatt service
219  * @param   env                [IN] JNI interface pointer
220  * @return  #CA_STATUS_OK or Appropriate error code
221  * @retval  #CA_STATUS_OK  Successful
222  * @retval  #CA_ADAPTER_NOT_ENABLED Adapter is not enabled
223  * @retval  #CA_STATUS_FAILED Operation failed
224  */
225 CAResult_t CANativeLEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
226         jobject bluetoothGattService);
227
228 /**
229  * @brief   start gatt server
230  * @param   env                  [IN] JNI interface pointer
231  * @param   gattServerCallback   [IN] callback to be notified on reception of
232  *                                state change of gatt server
233  * @return  #CA_STATUS_OK or Appropriate error code
234  * @retval  #CA_STATUS_OK  Successful
235  * @retval  #CA_ADAPTER_NOT_ENABLED Adapter is not enabled
236  * @retval  #CA_STATUS_FAILED Operation failed
237  */
238 CAResult_t CALEStartGattServer(JNIEnv *env, jobject gattServerCallback);
239
240 /**
241  * @brief   send data
242  * @param   env                  [IN] JNI interface pointer
243  * @param   bluetoothDevice      [IN] bluetooth device object
244  * @param   data                 [IN] data which send
245  * @return  #CA_STATUS_OK or Appropriate error code
246  * @retval  #CA_STATUS_OK  Successful
247  * @retval  #CA_SEND_FAILED  Send request failed
248  * @retval  #CA_ADAPTER_NOT_ENABLED Adapter is not enabled
249  * @retval  #CA_STATUS_FAILED Operation failed
250  */
251 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jstring data);
252
253 /**
254  * @brief   set data in BluetoothGattCharacteristic
255  * @param   env                  [IN] JNI interface pointer
256  * @param   bluetoothDevice      [IN] bluetooth device object
257  * @return  BluetoothGattCharacteristic object
258  */
259 jobject CANativeLEServerSetResponseData(JNIEnv *env, jbyteArray responseData);
260
261 /**
262  * @brief   send data through notifyCharacteristicChanged api of android
263  * @param   env                  [IN] JNI interface pointer
264  * @param   bluetoothDevice      [IN] bluetooth device object
265  * @param   data                 [IN] data which send
266  * @return  #CA_STATUS_OK or Appropriate error code
267  * @retval  #CA_STATUS_OK  Successful
268  * @retval  #CA_SEND_FAILED  Send request failed
269  * @retval  #CA_STATUS_FAILED Operation failed
270  */
271 CAResult_t CANativeLEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData);
272
273 /**
274  * @brief   send a response to a write request to a remote device
275  * @param   env                  [IN] JNI interface pointer
276  * @param   device               [IN] bluetooth device object
277  * @param   requestId            [IN] the id of request
278  * @param   status               [IN] the status of the request to be sent to the remote devices
279  * @param   offset               [IN] value offset for partial write response
280  * @param   value                [IN] the value of the attribute that written (optional)
281  * @return  #CA_STATUS_OK or Appropriate error code
282  * @retval  #CA_STATUS_OK  Successful
283  * @retval  #CA_SEND_FAILED  Send request failed
284  * @retval  #CA_ADAPTER_NOT_ENABLED Adapter is not enabled
285  * @retval  #CA_STATUS_FAILED Operation failed
286  */
287 CAResult_t CANativeLEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
288         jint offset, jbyteArray value);
289
290 /**
291  * @brief   connect BLE to remote device form gatt server
292  * @param   env                  [IN] JNI interface pointer
293  * @param   bluetoothDevice      [IN] bluetooth device object
294  * @return  #CA_STATUS_OK or Appropriate error code
295  * @retval  #CA_STATUS_OK  Successful
296  * @retval  #CA_ADAPTER_NOT_ENABLED Adapter is not enabled
297  * @retval  #CA_STATUS_FAILED Operation failed
298  */
299 CAResult_t CANativeLEServerConnect(JNIEnv *env, jobject bluetoothDevice);
300
301 /**
302  * @brief   disconnect BLE to remote device form gatt server
303  * @param   env                  [IN] JNI interface pointer
304  * @param   bluetoothDevice      [IN] bluetooth device object
305  * @return  None
306  */
307 void CANativeLEServerDisconnect(JNIEnv *env, jobject bluetoothDevice);
308
309 /* BLE Server Utils */
310 /**
311  * @brief   create connected device list
312  * @return  None
313  */
314 void CALEServerCreateCachedDeviceList();
315
316 /**
317  * @brief   check whether the device exist in the list or not
318  * @param   env                   [IN] JNI interface pointer
319  * @param   remoteAddress         [IN] remote address
320  * @return  TRUE or FALSE
321  */
322 jboolean CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress);
323
324 /**
325  * @brief   add device object to the list (connected device list)
326  * @param   env                   [IN] JNI interface pointer
327  * @param   device                [IN] bluetooth device object
328  * @return  None
329  */
330 void CALEServerAddDeviceToList(JNIEnv *env, jobject device);
331
332 /**
333  * @brief   remove all devices objects in the list (connected device list)
334  * @param   env                   [IN] JNI interface pointer
335  * @return  None
336  */
337 void CALEServerRemoveAllDevices(JNIEnv *env);
338
339 /**
340  * @brief   remove target device in the list (connected device list)
341  * @param   env                   [IN] JNI interface pointer
342  * @param   gatt                  [IN] Gatt profile object
343  * @return  None
344  */
345 void CALEServerRemoveDevice(JNIEnv *env, jstring address);
346
347 /**
348  * @brief   Reordering for the list (connected device list)
349  * @param   index                 [IN] index of device list that want to reordering
350  * @return  None
351  */
352 void CALEServerReorderinglist(uint32_t index);
353
354 #ifdef __cplusplus
355 } /* extern "C" */
356 #endif
357
358 #endif /* __CA_LESERVER_H_ */
359