My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2008 Vlad Skarzhevskyy
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @version $Id$
*/
#define CPP__FILE "common.c"

#include "common.h"
#include <stdio.h>

const char* cRuntimeException = "java/lang/RuntimeException";
const char* cIOException = "java/io/IOException";
const char* cInterruptedIOException = "java/io/InterruptedIOException";
const char* cBluetoothStateException = "javax/bluetooth/BluetoothStateException";
const char* cBluetoothConnectionException = "javax/bluetooth/BluetoothConnectionException";
const char* cServiceRegistrationException = "javax/bluetooth/ServiceRegistrationException";

// --- Debug

bool nativeDebugCallbackEnabled = false;
static jclass nativeDebugListenerClass;
static jmethodID nativeDebugMethod = NULL;

void enableNativeDebug(JNIEnv *env, jobject loggerClass, jboolean on) {
if (on) {
if (nativeDebugCallbackEnabled) {
return;
}
nativeDebugListenerClass = (jclass)(*env)->NewGlobalRef(env, loggerClass);
if (nativeDebugListenerClass != NULL) {
nativeDebugMethod = (*env)->GetStaticMethodID(env, nativeDebugListenerClass, "nativeDebugCallback", "(Ljava/lang/String;ILjava/lang/String;)V");
if (nativeDebugMethod != NULL) {
nativeDebugCallbackEnabled = true;
debug("nativeDebugCallback ON");
}
}
} else {
nativeDebugCallbackEnabled = false;
}
}

void callDebugListener(JNIEnv *env, const char* fileName, int lineN, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
{
if ((env != NULL) && (nativeDebugCallbackEnabled)) {
char msg[1064];
vsnprintf(msg, 1064, fmt, ap);
(*env)->CallStaticVoidMethod(env, nativeDebugListenerClass, nativeDebugMethod, (*env)->NewStringUTF(env, fileName), lineN, (*env)->NewStringUTF(env, msg));
}
}
va_end(ap);
}

void ndebug(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
if (nativeDebugCallbackEnabled) {
fprintf(stdout, "NATIVE:");
vfprintf(stdout, fmt, ap);
fprintf(stdout, "\n");
fflush(stdout);
}
va_end(ap);
}

// --- Error handling

void vthrowException(JNIEnv *env, const char *name, const char *fmt, va_list ap) {
char msg[1064];
if (env == NULL) {
return;
}
vsnprintf(msg, 1064, fmt, ap);
if ((*env)->ExceptionCheck(env)) {
ndebug("ERROR: can't throw second exception %s(%s)", name, msg);
return;
}
debug("will throw exception %s(%s)", name, msg);
jclass cls = (*env)->FindClass(env, name);
/* if cls is NULL, an exception has already been thrown */
if (cls != NULL) {
(*env)->ThrowNew(env, cls, msg);
/* free the local ref */
(*env)->DeleteLocalRef(env, cls);
} else {
debug("Can't find Exception %s", name);
(*env)->FatalError(env, name);
}

}

void throwException(JNIEnv *env, const char *name, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vthrowException(env, name, fmt, ap);
va_end(ap);
}

void throwRuntimeException(JNIEnv *env, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vthrowException(env, cRuntimeException, fmt, ap);
va_end(ap);
}

void throwIOException(JNIEnv *env, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vthrowException(env, cIOException, fmt, ap);
va_end(ap);
}

void throwInterruptedIOException(JNIEnv *env, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vthrowException(env, cInterruptedIOException, fmt, ap);
va_end(ap);
}

void throwServiceRegistrationException(JNIEnv *env, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vthrowException(env, cServiceRegistrationException, fmt, ap);
va_end(ap);
}

void throwBluetoothStateException(JNIEnv *env, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vthrowException(env, cBluetoothStateException, fmt, ap);
va_end(ap);
}

void throwBluetoothConnectionException(JNIEnv *env, int error, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);

char msg[1064];
if (env == NULL) {
va_end(ap);
return;
}
vsnprintf(msg, 1064, fmt, ap);

if ((*env)->ExceptionCheck(env)) {
debug("ERROR: can't throw second exception %s(%s)", cBluetoothConnectionException, msg);
va_end(ap);
return;
}
debug("will throw exception %s(%s)", cBluetoothConnectionException, msg);
jclass cls = (*env)->FindClass(env, cBluetoothConnectionException);
/* if cls is NULL, an exception has already been thrown */
if (cls != NULL) {
jmethodID methodID = (*env)->GetMethodID(env, cls, "<init>", "(ILjava/lang/String;)V");
if (methodID == NULL) {
(*env)->FatalError(env, "Fail to get constructor for Exception");
} else {
jstring excMessage = (*env)->NewStringUTF(env, msg);
jthrowable obj = (jthrowable)(*env)->NewObject(env, cls, methodID, error, excMessage);
if (obj != NULL) {
(*env)->Throw(env, obj);
} else {
(*env)->FatalError(env, "Fail to create new Exception");
}
}
/* free the local ref */
(*env)->DeleteLocalRef(env, cls);
} else {
(*env)->FatalError(env, cBluetoothConnectionException);
}

va_end(ap);
}

// --- Interaction with java classes

bool isCurrentThreadInterrupted(JNIEnv *env, jobject peer) {
jclass peerClass = (*env)->GetObjectClass(env, peer);
if (peerClass == NULL) {
throwRuntimeException(env, "Fail to get Object Class");
return true;
}
jmethodID aMethod = getGetMethodID(env, peerClass, "isCurrentThreadInterruptedCallback", "()Z");
if (aMethod == NULL) {
throwRuntimeException(env, "Fail to get MethodID isCurrentThreadInterruptedCallback");
return true;
}
if ((*env)->CallBooleanMethod(env, peer, aMethod)) {
throwInterruptedIOException(env, "thread interrupted");
return true;
}
return (*env)->ExceptionCheck(env);
}

bool threadSleep(JNIEnv *env, jlong millis) {
jclass clazz = (*env)->FindClass(env, "java/lang/Thread");
if (clazz == NULL) {
throwRuntimeException(env, "Fail to get Thread class");
return false;
}
jmethodID methodID = (*env)->GetStaticMethodID(env, clazz, "sleep", "(J)V");
if (methodID == NULL) {
throwRuntimeException(env, "Fail to get MethodID Thread.sleep");
return false;
}
(*env)->CallStaticVoidMethod(env, clazz, methodID, millis);
if ((*env)->ExceptionCheck(env)) {
return false;
}
return true;
}

jmethodID getGetMethodID(JNIEnv * env, jclass clazz, const char *name, const char *sig) {
if (clazz == NULL) {
throwRuntimeException(env, "Fail to get MethodID %s for NULL class", name);
return NULL;
}
jmethodID methodID = (*env)->GetMethodID(env, clazz, name, sig);
if (methodID == NULL) {
throwRuntimeException(env, "Fail to get MethodID %s", name);
return NULL;
}
return methodID;
}

void DeviceInquiryCallback_Init(struct DeviceInquiryCallback* callback) {
callback->inquiryRunnable = NULL;
callback->deviceDiscoveredCallbackMethod = NULL;
callback->startedNotify = NULL;
callback->startedNotifyNotifyMethod = NULL;
}

bool DeviceInquiryCallback_builDeviceInquiryCallbacks(JNIEnv * env, struct DeviceInquiryCallback* callback, jobject inquiryRunnable, jobject startedNotify) {
jclass inquiryRunnableClass = (*env)->GetObjectClass(env, inquiryRunnable);

if (inquiryRunnableClass == NULL) {
throwRuntimeException(env, "Fail to get Object Class");
return false;
}

jmethodID deviceDiscoveredCallbackMethod = (*env)->GetMethodID(env, inquiryRunnableClass, "deviceDiscoveredCallback", "(Ljavax/bluetooth/DiscoveryListener;JILjava/lang/String;Z)V");
if (deviceDiscoveredCallbackMethod == NULL) {
throwRuntimeException(env, "Fail to get MethodID deviceDiscoveredCallback");
return false;
}

jclass notifyClass = (*env)->GetObjectClass(env, startedNotify);
if (notifyClass == NULL) {
throwRuntimeException(env, "Fail to get Object Class");
return false;
}
jmethodID notifyMethod = (*env)->GetMethodID(env, notifyClass, "deviceInquiryStartedCallback", "()V");
if (notifyMethod == NULL) {
throwRuntimeException(env, "Fail to get MethodID deviceInquiryStartedCallback");
return false;
}

callback->inquiryRunnable = inquiryRunnable;
callback->deviceDiscoveredCallbackMethod = deviceDiscoveredCallbackMethod;
callback->startedNotify = startedNotify;
callback->startedNotifyNotifyMethod = notifyMethod;

return true;
}

bool DeviceInquiryCallback_callDeviceInquiryStartedCallback(JNIEnv * env, struct DeviceInquiryCallback* callback) {
if ((callback->startedNotify == NULL) || (callback->startedNotifyNotifyMethod == NULL)) {
throwRuntimeException(env, "DeviceInquiryCallback not initialized");
return false;
}
(*env)->CallVoidMethod(env, callback->startedNotify, callback->startedNotifyNotifyMethod);
if ((*env)->ExceptionCheck(env)) {
return false;
} else {
return true;
}
}

bool DeviceInquiryCallback_callDeviceDiscovered(JNIEnv * env, struct DeviceInquiryCallback* callback, jobject listener, jlong deviceAddr, jint deviceClass, jstring name, jboolean paired) {
if ((callback->inquiryRunnable == NULL) || (callback->deviceDiscoveredCallbackMethod == NULL)) {
throwRuntimeException(env, "DeviceInquiryCallback not initialized");
return false;
}
(*env)->CallVoidMethod(env, callback->inquiryRunnable, callback->deviceDiscoveredCallbackMethod, listener, deviceAddr, deviceClass, name, paired);
if ((*env)->ExceptionCheck(env)) {
return false;
} else {
return true;
}
}

Change log

r2952 by skarzhevskyy on Mar 21, 2009   Diff
debug Exception
Go to: 
Project members, sign in to write a code review

Older revisions

r2951 by skarzhevskyy on Mar 21, 2009   Diff
debug Exception
r2808 by skarzhevskyy on Feb 26, 2009   Diff
Corrections to additianl builds
r2558 by skarzhevskyy on Dec 11, 2008   Diff
Proper DeviceInquiryCallback
All revisions of this file

File info

Size: 10787 bytes, 307 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting