79a805a5efbcbcd44b9e64777e7a8d5560ebfa05
[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   create gatt service
170  * @param   env                  [IN] JNI interface pointer
171  * @param   bluetoothGattServer  [IN] Bluetooth Gatt Server object
172  * @param   bluetoothGattService [IN] Bluetooth Gatt Service object
173  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
174  */
175 CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer,
176                                     jobject bluetoothGattService);
177
178 /**
179  * @brief   start gatt server
180  * @param   env                  [IN] JNI interface pointer
181  * @param   gattServerCallback   [IN] callback to be notified on reception of
182  *                                state change of gatt server
183  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
184  */
185 CAResult_t CALEServerStartGattServer(JNIEnv *env, jobject gattServerCallback);
186
187 /**
188  * @brief   close gatt server
189  * @param   env                  [IN] JNI interface pointer
190  * @param   bluetoothGattServer  [IN] Gatt Server object
191  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
192  */
193 CAResult_t CALEServerGattClose(JNIEnv *env, jobject bluetoothGattServer);
194 /**
195  * @brief   send data
196  * @param   env                  [IN] JNI interface pointer
197  * @param   bluetoothDevice      [IN] bluetooth device object
198  * @param   data                 [IN] data which send
199  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
200  */
201 CAResult_t CALEServerSend(JNIEnv *env, jobject bluetoothDevice, jstring data);
202
203 /**
204  * @brief   set data in BluetoothGattCharacteristic
205  * @param   env                  [IN] JNI interface pointer
206  * @param   responseData         [IN] data to set in characteristic object
207  * @return  BluetoothGattCharacteristic object
208  */
209 jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData);
210
211 /**
212  * @brief   send data through notifyCharacteristicChanged api of android
213  * @param   env                  [IN] JNI interface pointer
214  * @param   device               [IN] bluetooth device object
215  * @param   responseData         [IN] data which send
216  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
217  */
218 CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject responseData);
219
220 /**
221  * @brief   send a response to a write request to a remote device
222  * @param   env                  [IN] JNI interface pointer
223  * @param   device               [IN] bluetooth device object
224  * @param   requestId            [IN] the id of request
225  * @param   status               [IN] the status of the request to be sent to the remote devices
226  * @param   offset               [IN] value offset for partial write response
227  * @param   value                [IN] the value of the attribute that written (optional)
228  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
229  */
230 CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, jint status,
231                                   jint offset, jbyteArray value);
232
233 /**
234  * @brief   connect BLE to remote device form gatt server
235  * @param   env                  [IN] JNI interface pointer
236  * @param   bluetoothDevice      [IN] bluetooth device object
237  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
238  */
239 CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice);
240
241 /**
242  * @brief   disconnect LE for all devices
243  * @param   env                  [IN] JNI interface pointer
244  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
245  */
246 CAResult_t CALEServerDisconnectAllDevices(JNIEnv *env);
247
248 /**
249  * @brief   disconnect LE to remote device form gatt server
250  * @param   env                  [IN] JNI interface pointer
251  * @param   bluetoothDevice      [IN] bluetooth device object
252  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
253  */
254 CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice);
255
256 /* BLE Server Utils */
257 /**
258  * @brief   create connected device list
259  * @return  None
260  */
261 void CALEServerCreateCachedDeviceList();
262
263 /**
264  * @brief   check whether the device exist in the list or not
265  * @param   env                   [IN] JNI interface pointer
266  * @param   remoteAddress         [IN] remote address
267  * @return  true or false
268  */
269 bool CALEServerIsDeviceInList(JNIEnv *env, const char* remoteAddress);
270
271 /**
272  * @brief   add device object to the list (connected device list)
273  * @param   env                   [IN] JNI interface pointer
274  * @param   device                [IN] 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  * @brief   remove all devices objects in the list (connected device list)
281  * @param   env                   [IN] 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  * @brief   remove target device in the list (connected device list)
288  * @param   env                   [IN] JNI interface pointer
289  * @param   address               [IN] 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  * @brief   Reordering for the list (connected device list)
296  * @param   index                 [IN] index of device list that want to reordering
297  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
298  */
299 CAResult_t CALEServerReorderinglist(uint32_t index);
300
301 /**
302  * @brief   initialize mutex
303  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
304  */
305 CAResult_t CALEServerInitMutexVaraibles();
306
307 /**
308  * @brief   terminate mutex
309  * @return  None
310  */
311 void CALEServerTerminateMutexVaraibles();
312
313 /**
314  * @brief   initialize condition
315  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
316  */
317 CAResult_t CALEServerInitConditionVaraibles();
318
319 /**
320  * @brief   terminate condition
321  * @return  None
322  */
323 void CALEServerTerminateConditionVaraibles();
324
325 #ifdef __cplusplus
326 } /* extern "C" */
327 #endif
328
329 #endif /* CA_LESERVER_H_ */