Imported Upstream version 0.9.1
[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   send data
197  * @param   env                  [IN] JNI interface pointer
198  * @param   bluetoothDevice      [IN] bluetooth device object
199  * @param   data                 [IN] data which send
200  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
201  */
202 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jstring data);
203
204 /**
205  * @brief   set data in BluetoothGattCharacteristic
206  * @param   env                  [IN] JNI interface pointer
207  * @param   responseData         [IN] data to set in characteristic object
208  * @return  BluetoothGattCharacteristic object
209  */
210 jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData);
211
212 /**
213  * @brief   send data through notifyCharacteristicChanged api of android
214  * @param   env                  [IN] JNI interface pointer
215  * @param   device               [IN] bluetooth device object
216  * @param   responseData         [IN] data which send
217  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
218  */
219 CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData);
220
221 /**
222  * @brief   send a response to a write request to a remote device
223  * @param   env                  [IN] JNI interface pointer
224  * @param   device               [IN] bluetooth device object
225  * @param   requestId            [IN] the id of request
226  * @param   status               [IN] the status of the request to be sent to the remote devices
227  * @param   offset               [IN] value offset for partial write response
228  * @param   value                [IN] the value of the attribute that written (optional)
229  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
230  */
231 CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
232                                   jint offset, jbyteArray value);
233
234 /**
235  * @brief   connect BLE to remote device form gatt server
236  * @param   env                  [IN] JNI interface pointer
237  * @param   bluetoothDevice      [IN] bluetooth device object
238  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
239  */
240 CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice);
241
242 /**
243  * @brief   disconnect LE for all devices
244  * @param   env                  [IN] JNI interface pointer
245  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
246  */
247 CAResult_t CALEServerDisconnectAllDevices(JNIEnv *env);
248
249 /**
250  * @brief   disconnect LE to remote device form gatt server
251  * @param   env                  [IN] JNI interface pointer
252  * @param   bluetoothDevice      [IN] bluetooth device object
253  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
254  */
255 CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice);
256
257 /* BLE Server Utils */
258 /**
259  * @brief   create connected device list
260  * @return  None
261  */
262 void CALEServerCreateCachedDeviceList();
263
264 /**
265  * @brief   check whether the device exist in the list or not
266  * @param   env                   [IN] JNI interface pointer
267  * @param   remoteAddress         [IN] remote address
268  * @return  true or false
269  */
270 bool CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress);
271
272 /**
273  * @brief   add device object to the list (connected device list)
274  * @param   env                   [IN] JNI interface pointer
275  * @param   device                [IN] bluetooth device object
276  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
277  */
278 CAResult_t CALEServerAddDeviceToList(JNIEnv *env, jobject device);
279
280 /**
281  * @brief   remove all devices objects in the list (connected device list)
282  * @param   env                   [IN] JNI interface pointer
283  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
284  */
285 CAResult_t CALEServerRemoveAllDevices(JNIEnv *env);
286
287 /**
288  * @brief   remove target device in the list (connected device list)
289  * @param   env                   [IN] JNI interface pointer
290  * @param   address               [IN] target address to remove
291  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
292  */
293 CAResult_t CALEServerRemoveDevice(JNIEnv *env, jstring address);
294
295 /**
296  * @brief   Reordering for the list (connected device list)
297  * @param   index                 [IN] index of device list that want to reordering
298  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
299  */
300 CAResult_t CALEServerReorderinglist(uint32_t index);
301
302 /**
303  * @brief   initialize mutex
304  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
305  */
306 CAResult_t CALEServerInitMutexVaraibles();
307
308 /**
309  * @brief   terminate mutex
310  * @return  None
311  */
312 void CALEServerTerminateMutexVaraibles();
313
314 /**
315  * @brief   initialize condition
316  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
317  */
318 CAResult_t CALEServerInitConditionVaraibles();
319
320 /**
321  * @brief   terminate condition
322  * @return  None
323  */
324 void CALEServerTerminateConditionVaraibles();
325
326 #ifdef __cplusplus
327 } /* extern "C" */
328 #endif
329
330 #endif /* CA_LESERVER_H_ */