Replace glib threadpool usage with a 'dumb' thread implementation.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_edr_adapter / android / caedrutils.c
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 #include <jni.h>
22 #include <stdio.h>
23 #include <android/log.h>
24 #include "caedrutils.h"
25 #include "logger.h"
26 #include "oic_malloc.h"
27 #include "cathreadpool.h"
28 #include "uarraylist.h"
29
30 #define ERROR_CODE (-1)
31 #define TAG PCF("CA_EDR_UTILS")
32
33 static const char *METHODID_OBJECTNONPARAM = "()Landroid/bluetooth/BluetoothAdapter;";
34 static const char *METHODID_STRINGNONPARAM = "()Ljava/lang/String;";
35 static const char *CLASSPATH_BT_ADPATER = "android/bluetooth/BluetoothAdapter";
36
37 /**
38  * BT common
39  */
40
41 // get address from bluetooth socket
42 jstring CAEDRNativeGetAddressFromDeviceSocket(JNIEnv *env, jobject bluetoothSocketObj)
43 {
44     if(!bluetoothSocketObj)
45     {
46         return NULL;
47     }
48
49     jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
50     if(!jni_cid_BTSocket)
51     {
52         OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: jni_cid_BTSocket is null");
53         return NULL;
54     }
55
56     jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(env,
57             jni_cid_BTSocket, "getRemoteDevice",
58             "()Landroid/bluetooth/BluetoothDevice;");
59     if(!jni_mid_getRemoteDevice)
60     {
61         OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: jni_mid_getRemoteDevice is null");
62         return NULL;
63     }
64
65     jobject jni_obj_remoteBTDevice = (*env)->CallObjectMethod(env,
66             bluetoothSocketObj, jni_mid_getRemoteDevice);
67     if(!jni_obj_remoteBTDevice)
68     {
69         OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: jni_obj_remoteBTDevice is null");
70         return NULL;
71     }
72
73     jclass jni_cid_BTDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
74     if(!jni_cid_BTDevice)
75     {
76         OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: jni_cid_BTDevice is null");
77         return NULL;
78     }
79     jmethodID j_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTDevice,
80             "getAddress", "()Ljava/lang/String;");
81     if(!j_mid_getAddress)
82     {
83         OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: j_mid_getAddress is null");
84         return NULL;
85     }
86
87     jstring j_str_address = (*env)->CallObjectMethod(env, jni_obj_remoteBTDevice, j_mid_getAddress);
88     if(!j_str_address)
89     {
90         OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: j_str_address is null");
91         return NULL;
92     }
93
94     return j_str_address;
95 }
96
97 jstring CAEDRNativeGetLocalDeviceAddress(JNIEnv* env)
98 {
99     jclass jni_cid_BTAdapter = (*env)->FindClass(env,  CLASSPATH_BT_ADPATER);
100     if(!jni_cid_BTAdapter)
101     {
102         OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_cid_BTAdapter is null");
103         return NULL;
104     }
105
106     jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env,
107             jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
108     if(!jni_mid_getDefaultAdapter)
109     {
110         OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_mid_getDefaultAdapter is null");
111         return NULL;
112     }
113
114     jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTAdapter,
115             "getAddress", METHODID_STRINGNONPARAM);
116     if(!jni_mid_getAddress)
117     {
118         OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_mid_getAddress is null");
119         return NULL;
120     }
121
122     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env,
123             jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
124     if(!jni_obj_BTAdapter)
125     {
126         OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_obj_BTAdapter is null");
127         return NULL;
128     }
129
130     jstring jni_str_address = (jstring)(*env)->CallObjectMethod(env,
131             jni_obj_BTAdapter, jni_mid_getAddress);
132     if(!jni_str_address)
133     {
134         OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_str_address is null");
135         return NULL;
136     }
137
138     return jni_str_address;
139 }
140
141 jobjectArray CAEDRNativeGetBondedDevices(JNIEnv *env)
142 {
143     jclass jni_cid_BTAdapter = (*env)->FindClass(env,  CLASSPATH_BT_ADPATER);
144     if(!jni_cid_BTAdapter)
145     {
146         OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_cid_BTAdapter is null");
147         return NULL;
148     }
149
150     jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env,
151             jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
152
153     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env,
154             jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
155     if(!jni_obj_BTAdapter)
156     {
157         OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: bluetooth adapter is null");
158         return NULL;
159     }
160
161     // Get a list of currently paired devices
162     jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
163             "getBondedDevices", "()Ljava/util/Set;");
164     if(!jni_mid_getBondedDevices)
165     {
166         OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_mid_getBondedDevicesr is null");
167         return NULL;
168     }
169
170     jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env,
171             jni_obj_BTAdapter, jni_mid_getBondedDevices);
172     if(!jni_obj_setPairedDevices)
173     {
174         OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_obj_setPairedDevices is null");
175         return NULL;
176     }
177
178     // Convert the set to an object array
179     // object[] array = Set<BluetoothDevice>.toArray();
180     jclass jni_cid_Set = (*env)->FindClass(env,  "java/util/Set");
181     jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray",
182             "()[Ljava/lang/Object;");
183
184     if(!jni_mid_toArray)
185     {
186         OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_mid_toArray is null");
187         return NULL;
188     }
189
190     jobjectArray jni_arrayPairedDevices = (jobjectArray)((*env)->CallObjectMethod(env,
191             jni_obj_setPairedDevices, jni_mid_toArray));
192     if(!jni_arrayPairedDevices)
193     {
194         OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_arrayPairedDevices is null");
195         return NULL;
196     }
197
198     return jni_arrayPairedDevices;
199 }
200
201 jint CAEDRNativeGetBTStateOnInfo(JNIEnv *env)
202 {
203     jclass jni_cid_BTAdapter = (*env)->FindClass(env,  CLASSPATH_BT_ADPATER);
204     if(!jni_cid_BTAdapter)
205     {
206         OIC_LOG(ERROR, TAG, "[EDR][Native] getBTStateOnInfo: jni_cid_BTAdapter is null");
207         return ERROR_CODE;
208     }
209
210     jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
211     if (jni_fid_stateon == 0)
212     {
213         OIC_LOG(DEBUG, TAG, "[EDR][Native] get_field_state is 0");
214         return ERROR_CODE;
215     }
216     jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
217
218     OIC_LOG_V(DEBUG, TAG, "[EDR][Native] bluetooth STATE_ON state integer value : %d", jni_int_val);
219
220     return jni_int_val;
221 }
222
223 jboolean CAEDRNativeIsEnableBTAdapter(JNIEnv *env)
224 {
225     jclass jni_cid_BTAdapter = (*env)->FindClass(env,  CLASSPATH_BT_ADPATER);
226     if(!jni_cid_BTAdapter)
227     {
228         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_BTAdapter: jni_cid_BTAdapter is null");
229         return FALSE;
230     }
231
232     jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env,
233             jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
234     if(!jni_mid_getDefaultAdapter)
235     {
236         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_getDefaultAdapter is null");
237         return FALSE;
238     }
239
240     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env,
241             jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
242     if(!jni_obj_BTAdapter)
243     {
244         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_obj_BTAdapter is null");
245         return FALSE;
246     }
247
248     // isEnable()
249     jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled",
250             "()Z");
251     if(!jni_mid_isEnable)
252     {
253         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_isEnable is null");
254         return FALSE;
255     }
256
257     jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
258     OIC_LOG_V(DEBUG, TAG, "[EDR][Native] adapter state is %d", jni_isEnable);
259
260     return jni_isEnable;
261 }
262
263 jstring CAEDRNativeGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
264 {
265     jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
266     if(!jni_cid_device_list)
267     {
268         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_device_list is null");
269         return NULL;
270     }
271
272     jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_device_list, "getAddress",
273             "()Ljava/lang/String;");
274     if(!jni_mid_getAddress)
275     {
276         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_getAddress is null");
277         return NULL;
278     }
279
280     jstring jni_address = (jstring)(*env)->CallObjectMethod(env,
281             bluetoothDevice, jni_mid_getAddress);
282     if(!jni_address)
283     {
284         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_address is null");
285         return NULL;
286     }
287     return jni_address;
288 }
289