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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2006-2008 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @version $Id$
*/

#include "common.h"
#include "commonObjects.h"

#ifndef CPP_FILE
#define CPP_FILE "common.cpp"
#endif

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

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";

jint blueCoveVersion() {
return BLUECOVE_VERSION * 100 + BLUECOVE_BUILD;
}

// --- Debug

BOOL isDebugOn() {
return nativeDebugCallbackEnabled;
}

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

void stdOutPrint(const char* fileName, int lineN, const char* msg) {
fprintf(stdout, "NATIVE: %s\n\t%s(%i)\n", msg, fileName, lineN);
fflush(stdout);
}

void log_info(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
fprintf(stdout, "BlueCove-INFO:");
vfprintf(stdout, fmt, ap);
fprintf(stdout, "\n");
fflush(stdout);
va_end(ap);
}

void DebugMessage::printf(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
{
if (nativeDebugCallbackEnabled) {
_vsnprintf_s(msg, DEBUG_MESSAGE_MAX, fmt, ap);
}
}
va_end(ap);
}

void DebugMessage::callDebugListener(JNIEnv *env, const char* fileName, int lineN) {
if ((env != NULL) && (nativeDebugCallbackEnabled)) {
if (ExceptionCheckCompatible(env)) {
stdOutPrint(fileName, lineN, msg);
} else {
env->CallStaticVoidMethod(nativeDebugListenerClass, nativeDebugMethod, env->NewStringUTF(fileName), lineN, env->NewStringUTF(msg));
}
}
}

void DebugMessage::callDebugStdOut(const char* fileName, int lineN) {
if (nativeDebugCallbackEnabled) {
stdOutPrint(fileName, lineN, msg);
}
}

void callDebugListener(JNIEnv *env, const char* fileName, int lineN, ...) {
va_list ap;
va_start(ap, lineN);
{
if ((env != NULL) && (nativeDebugCallbackEnabled)) {
char msg[DEBUG_MESSAGE_MAX+1];
char *fmt = va_arg(ap, char*);
_vsnprintf_s(msg, DEBUG_MESSAGE_MAX, fmt, ap);
if (ExceptionCheckCompatible(env)) {
stdOutPrint(fileName, lineN, msg);
} else {
env->CallStaticVoidMethod(nativeDebugListenerClass, nativeDebugMethod, env->NewStringUTF(fileName), lineN, env->NewStringUTF(msg));
}
}
}
va_end(ap);
}

void callDebugStdOut(const char* fileName, int lineN, ...) {
va_list ap;
va_start(ap, lineN);
{
if (nativeDebugCallbackEnabled) {
char msg[DEBUG_MESSAGE_MAX+1];
char *fmt = va_arg(ap, char*);
_vsnprintf_s(msg, DEBUG_MESSAGE_MAX, fmt, ap);

stdOutPrint(fileName, lineN, msg);
}
}
va_end(ap);
}

const char* bool2str(BOOL b) {
if (b == false) {
return "FALSE";
} else {
return "TRUE";
}
}

// --- Error handling

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

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

va_end(ap);
}

#ifdef WIN32
WCHAR *getWinErrorMessage(DWORD last_error) {
static WCHAR errmsg[DEBUG_MESSAGE_MAX+1];
if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
0,
last_error,
0,
errmsg,
511,
NULL))
{
swprintf_s(errmsg, DEBUG_MESSAGE_MAX, L"No error message for code %lu", last_error);
return errmsg;
}
size_t last = wcslen(errmsg) - 1;
while ((errmsg[last] == '\n') || (errmsg[last] == '\r')) {
errmsg[last] = 0;
last --;
}
return errmsg;
}

void throwExceptionWinErrorMessage(JNIEnv *env, const char *name, const char *msg, DWORD last_error) {
char errmsg[DEBUG_MESSAGE_MAX+1];
sprintf_s(errmsg, DEBUG_MESSAGE_MAX, "%s; [%lu] %S", msg, last_error, getWinErrorMessage(last_error));
throwException(env, name, errmsg);
}

void throwIOExceptionWinErrorMessage(JNIEnv *env, const char *msg, DWORD last_error) {
throwExceptionWinErrorMessage(env, cIOException, msg, last_error);
}

void throwBluetoothStateExceptionWinErrorMessage(JNIEnv *env, const char *msg, DWORD last_error) {
throwExceptionWinErrorMessage(env, cBluetoothStateException, msg, last_error);
}

void throwIOExceptionWinGetLastError(JNIEnv *env, const char *msg) {
throwIOExceptionWinErrorMessage(env, msg, GetLastError());
}

const char* waitResultsString(DWORD rc) {
switch (rc) {
case WAIT_FAILED: return "WAIT_FAILED";
case WAIT_TIMEOUT: return "WAIT_TIMEOUT";
case WAIT_OBJECT_0: return "WAIT_OBJECT_0";
case WAIT_OBJECT_0 + 1: return "WAIT_OBJECT_1";
case WAIT_OBJECT_0 + 2: return "WAIT_OBJECT_2";
case WAIT_ABANDONED_0: return "WAIT_ABANDONED_0";
case WAIT_ABANDONED_0 + 1: return "WAIT_ABANDONED_1";
case WAIT_ABANDONED_0 + 2: return "WAIT_ABANDONED_2";
default : return "Unknown";
}
}

#endif

BOOL ExceptionCheckCompatible(JNIEnv *env) {
if (env->GetVersion() > JNI_VERSION_1_1) {
return env->ExceptionCheck();
} else {
return (env->ExceptionOccurred() != NULL);
}
}

BOOL isCurrentThreadInterrupted(JNIEnv *env, jobject peer, const char* message) {
jclass peerClass = env->GetObjectClass(peer);
if (peerClass == NULL) {
throwRuntimeException(env, "Fail to get Object Class");
return TRUE;
}
jmethodID aMethod = env->GetMethodID(peerClass, "isCurrentThreadInterruptedCallback", "()Z");
if (aMethod == NULL) {
throwRuntimeException(env, "Fail to get MethodID isCurrentThreadInterruptedCallback");
return TRUE;
}
if (env->CallBooleanMethod(peer, aMethod)) {
throwInterruptedIOException(env, "thread interrupted %s", message);
return TRUE;
}
return ExceptionCheckCompatible(env);
}

jint detectBluetoothStack(JNIEnv *env) {
jint rc = 0;
#ifdef WIN32
#ifndef VC6
if (isMicrosoftBluetoothStackPresent(env)) {
rc += BLUECOVE_STACK_DETECT_MICROSOFT;
}
#else
if (isMicrosoftBluetoothStackPresentVC6(env)) {
rc += BLUECOVE_STACK_DETECT_MICROSOFT;
}
#endif
if (isWIDCOMMBluetoothStackPresent(env)) {
rc += BLUECOVE_STACK_DETECT_WIDCOMM;
}
#ifndef _WIN32_WCE
if (isBlueSoleilBluetoothStackPresent(env)) {
rc += BLUECOVE_STACK_DETECT_BLUESOLEIL;
}
if (isToshibaBluetoothStackPresent(env)) {
rc += BLUECOVE_STACK_DETECT_TOSHIBA;
}
#endif
#endif
return rc;
}

#ifdef WIN32
void convertUUIDBytesToGUID(jbyte *bytes, GUID *uuid) {
uuid->Data1 = bytes[0]<<24&0xff000000|bytes[1]<<16&0x00ff0000|bytes[2]<<8&0x0000ff00|bytes[3]&0x000000ff;
uuid->Data2 = bytes[4]<<8&0xff00|bytes[5]&0x00ff;
uuid->Data3 = bytes[6]<<8&0xff00|bytes[7]&0x00ff;

for(int i = 0; i < 8; i++) {
uuid->Data4[i] = bytes[i + 8];
}
}

void convertGUIDToUUIDBytes(GUID *uuid, jbyte *bytes) {
bytes[0] = (jbyte)((uuid->Data1>>24) & 0x00ff);
bytes[1] = (jbyte)((uuid->Data1>>16) & 0x00ff);
bytes[2] = (jbyte)((uuid->Data1>>8) & 0x00ff);
bytes[3] = (jbyte)((uuid->Data1) & 0x00ff);
bytes[4] = (jbyte)((uuid->Data2>>8) & 0x00ff);
bytes[5] = (jbyte)((uuid->Data2 & 0x00ff));
bytes[6] = (jbyte)((uuid->Data3>>8) & 0x00ff);
bytes[7] = (jbyte)((uuid->Data3 & 0x00ff));
for(int i = 0; i < 8; i++) {
bytes[i + 8] = uuid->Data4[i];
}
}
#endif

#define MAJOR_COMPUTER 0x0100
#define MAJOR_PHONE 0x0200
#define COMPUTER_MINOR_HANDHELD 0x10
#define PHONE_MINOR_SMARTPHONE 0x0c

jint getDeviceClassByOS(JNIEnv *env) {
#ifndef _WIN32_WCE
return MAJOR_COMPUTER;
#else
OSVERSIONINFO osvi;
TCHAR szPlatform[MAX_PATH];

BOOL rb;

osvi.dwOSVersionInfoSize = sizeof(osvi);
rb = GetVersionEx(&osvi);
if (rb == FALSE) {
return MAJOR_COMPUTER;
}
switch (osvi.dwPlatformId) {
// A Windows CE platform.
case VER_PLATFORM_WIN32_CE:
// Get platform string.
rb = SystemParametersInfo(SPI_GETPLATFORMTYPE, MAX_PATH, szPlatform, 0);
if (rb == FALSE) // SystemParametersInfo failed.
{
return MAJOR_COMPUTER & COMPUTER_MINOR_HANDHELD;
}
debug(("PLATFORMTYPE %S", szPlatform));
if (0 == lstrcmpi(szPlatform, TEXT("Smartphone"))) {
return MAJOR_PHONE | PHONE_MINOR_SMARTPHONE;
}
if (0 == lstrcmpi(szPlatform, TEXT("PocketPC"))) {
return MAJOR_COMPUTER | COMPUTER_MINOR_HANDHELD;
}
}
return MAJOR_COMPUTER;
#endif
}

#ifndef WIN32
void InitializeCriticalSection(CRITICAL_SECTION *criticalRegion) {
MPCreateCriticalRegion(criticalRegion);
}
void DeleteCriticalSection(CRITICAL_SECTION *criticalRegion) {
MPDeleteCriticalRegion(*criticalRegion);
}
void EnterCriticalSection(CRITICAL_SECTION *criticalRegion) {
MPEnterCriticalRegion(*criticalRegion, kDurationForever);
}
void LeaveCriticalSection(CRITICAL_SECTION *criticalRegion) {
MPExitCriticalRegion(*criticalRegion);
}
#endif

ReceiveBuffer::ReceiveBuffer() {
safe = RECEIVE_BUFFER_SAFE;
if (safe) InitializeCriticalSection(&lock);
this->size = RECEIVE_BUFFER_MAX;
reset();
}

ReceiveBuffer::ReceiveBuffer(int size) {
safe = RECEIVE_BUFFER_SAFE;
if (safe) InitializeCriticalSection(&lock);
this->size = size;
if (this->size > RECEIVE_BUFFER_MAX) {
this->size = RECEIVE_BUFFER_MAX;
}
reset();
}

ReceiveBuffer::~ReceiveBuffer() {
if (safe) DeleteCriticalSection(&lock);
magic1b = 0;
magic2b = 0;
magic1e = 0;
magic2e = 0;
}

void ReceiveBuffer::reset() {
rcv_idx = 0;
read_idx = 0;
overflown = FALSE;
full = FALSE;
magic1b = MAGIC_1;
magic2b = MAGIC_2;
magic1e = MAGIC_1;
magic2e = MAGIC_2;
}

BOOL ReceiveBuffer::isCorrupted() {
return ((magic1b != MAGIC_1) || (magic2b != MAGIC_2) || (magic1e != MAGIC_1) || (magic2e != MAGIC_2));
}

BOOL ReceiveBuffer::isOverflown() {
return overflown && (available() == 0);
}

void ReceiveBuffer::setOverflown() {
overflown = TRUE;
}

int ReceiveBuffer::write_buffer(void *p_data, int len) {
if (overflown) {
return 0;
}
int accept;
int _read_idx = read_idx;

if ((_read_idx == rcv_idx) && full) {
accept = 0;
} else if (_read_idx <= rcv_idx) {
accept = size - rcv_idx + _read_idx;
} else {
accept = _read_idx - rcv_idx;
}

if (accept > len) {
accept = len;
} else if (accept < len) {
overflown = TRUE;
}

if (accept != 0) {
if (rcv_idx + accept <= size) {
memcpy((buffer + rcv_idx), p_data, accept);
int new_rcv_idx = rcv_idx + accept;
if (new_rcv_idx >= size) {
new_rcv_idx = 0;
}
rcv_idx = new_rcv_idx;
} else {
// Read first part till the end of the buffer.
int accept_fill_end_size = size - rcv_idx;
memcpy((buffer + rcv_idx), p_data, accept_fill_end_size);
// Read second part at the beginning of the buffer.
int accept_fill_begin_size = accept - accept_fill_end_size;
memcpy(buffer, ((jbyte*)p_data + accept_fill_end_size), accept_fill_begin_size);
rcv_idx = accept_fill_begin_size;
}

if (read_idx == rcv_idx) {
full = TRUE;
}
}
return accept;
}

int ReceiveBuffer::write(void *p_data, int len) {
if (overflown) {
return 0;
}
if (safe) EnterCriticalSection(&lock);
int accept = write_buffer(p_data, len);
if (safe) LeaveCriticalSection(&lock);
return accept;
}

int ReceiveBuffer::write_with_len(void *p_data, int len) {
if (overflown) {
return 0;
}
if (safe) EnterCriticalSection(&lock);
int accept = write_buffer(&len, sizeof(int));
accept += write_buffer(p_data, len);
if (safe) LeaveCriticalSection(&lock);
return accept;
}


void ReceiveBuffer::incReadIdx(int count) {
int next_read_idx = read_idx + count;
if (next_read_idx >= size) {
next_read_idx -= size;
}
read_idx = next_read_idx;
full = FALSE;
}

int ReceiveBuffer::readByte() {
if (available() == 0) {
return -1;
}
if (safe) EnterCriticalSection(&lock);
jint result = (unsigned char)buffer[read_idx];
incReadIdx(1);
if (safe) LeaveCriticalSection(&lock);
return result;
}

int ReceiveBuffer::sizeof_len() {
return sizeof(int);
}

int ReceiveBuffer::read_len(int* len) {
return read(len, sizeof(int));
}

int ReceiveBuffer::read(void *p_data, int len) {
int count = available();
if (count == 0) {
return 0;
}
if (safe) EnterCriticalSection(&lock);
if (count > len) {
count = len;
}
if (read_idx + count < size) {
if (p_data != NULL) {
memcpy(p_data, (buffer + read_idx), count);
}
} else {
// Read first part from the end of the buffer.
int accept_fill_end_size = size - read_idx;
if (p_data != NULL) {
memcpy(p_data, (buffer + read_idx), accept_fill_end_size);
}
// Read second part from the beginning of the buffer.
int accept_fill_begin_size = count - accept_fill_end_size;
if (p_data != NULL) {
memcpy((jbyte*)p_data + accept_fill_end_size, buffer, accept_fill_begin_size);
}
}
incReadIdx(count);
if (safe) LeaveCriticalSection(&lock);
return count;
}

int ReceiveBuffer::skip(int n) {
return read(NULL, n);
}

int ReceiveBuffer::available() {
if (safe) EnterCriticalSection(&lock);
int rc;
int _rcv_idx = rcv_idx;
int _read_idx = read_idx;
if ((_read_idx == _rcv_idx) && full) {
rc = size;
} else if (_read_idx <= _rcv_idx) {
rc = (_rcv_idx - _read_idx);
} else {
rc = (_rcv_idx + (size - _read_idx));
}
if (safe) LeaveCriticalSection(&lock);
return rc;
}

// --------- ObjectPool -------------

PoolableObject::PoolableObject() {
magic1 = MAGIC_1;
magic2 = MAGIC_2;
internalHandle = -1;
usedCount = 0;
readyToFree = FALSE;
}

PoolableObject::~PoolableObject() {
magic1 = 0;
magic2 = 0;
#ifdef SAFE_OBJECT_DESTRUCTION
// Do not allow to free the object until thre are no functions in wait using it. e.g. read and write
while (usedCount > 0) {
Sleep(50);
}
#endif
}

void PoolableObject::tInc() {
#ifdef SAFE_OBJECT_DESTRUCTION
InterlockedIncrement(&usedCount);
#endif
}

void PoolableObject::tDec() {
#ifdef SAFE_OBJECT_DESTRUCTION
InterlockedDecrement(&usedCount);
#endif
}

BOOL PoolableObject::isValidObject() {
return ((magic1 == MAGIC_1) && (magic2 == MAGIC_2));
}

BOOL PoolableObject::isExternalHandle(jlong handle) {
return FALSE;
}

ObjectPool::ObjectPool(int size, int handleOffset, BOOL delayDelete) {
InitializeCriticalSection(&lock);
this->size = size;
this->handleOffset = handleOffset;
this->delayDelete = delayDelete;
this->handleReturned = 0;
this->handleBatch = 0;
handleMove = 0;
objs = new PoolableObject* [size];
for(int i = 0; i < size; i ++) {
objs[i] = NULL;
}
}

ObjectPool::~ObjectPool() {
EnterCriticalSection(&lock);
PoolableObject** __objs = objs;
objs = NULL;
for(int i = 0; i < size; i ++) {
if (__objs[i] != NULL) {
PoolableObject* o = __objs[i];
__objs[i] = NULL;
delete o;
}
}
delete __objs;
LeaveCriticalSection(&lock);
DeleteCriticalSection(&lock);
}

jlong ObjectPool::realIndex(jlong internalHandle) {
return (internalHandle - handleOffset) % size;
}

jlong ObjectPool::realIndex(PoolableObject* obj) {
return realIndex(obj->internalHandle);
}

BOOL ObjectPool::addObject(PoolableObject* obj) {
//ndebug(("new Object %p", obj));
EnterCriticalSection(&lock);
if (objs == NULL) {
LeaveCriticalSection(&lock);
return FALSE;
}
int freeIndex = -1;
for(int k = 0; k < size; k++) {
int i = k + handleMove;
if (i >= size) {
i -= size;
}

if (delayDelete && (objs[i] != NULL)) {
if (objs[i]->readyToFree) {
delete objs[i];
objs[i] = NULL;
}
}

if (objs[i] == NULL) {
freeIndex = i;
objs[freeIndex] = obj;
long newHandle = handleOffset + freeIndex + handleBatch * size;
while (newHandle <= handleReturned) {
newHandle += size;
handleBatch ++;
}
// Start all over from start
if (newHandle >= INT_MAX) {
newHandle = handleOffset + freeIndex;
handleBatch = 0;
}
handleReturned = (int)newHandle;
obj->internalHandle = (int)newHandle;

handleMove ++;
if (handleMove >= size) {
handleMove = 0;
}

LeaveCriticalSection(&lock);
return TRUE;
}
}
LeaveCriticalSection(&lock);
return FALSE;
}

BOOL ObjectPool::hasObject(PoolableObject* obj) {
for(int i = 0; (objs != NULL) && (i < size); i++) {
if ((void*)objs[i] == (void*)obj) {
return TRUE;
}
}
return FALSE;
}

BOOL ObjectPool::addObject(PoolableObject* obj, char poolableObjectType) {
obj->poolableObjectType = poolableObjectType;
return addObject(obj);
}

PoolableObject* ObjectPool::getObject(JNIEnv *env, jlong handle) {
if ((handle <= 0) || (objs == NULL)) {
throwIOException(env, "[EAO] Invalid handle %i", handle);
return NULL;
}
jlong idx = realIndex(handle);
if ((idx < 0) || (idx >= size)) {
throwIOException(env, "[EAO] Obsolete handle %i", handle);
return NULL;
}
PoolableObject* o = objs[idx];
if (o == NULL) {
throwIOException(env, "[EAO] Destroyed handle %i", handle);
return NULL;
}
if (o->readyToFree) {
throwIOException(env, "[EAO] Delay delete object access %i", handle);
return NULL;
}
if ((o->magic1 != MAGIC_1) || (o->magic2 != MAGIC_2)) {
throwIOException(env, "[EAO] Corrupted object %i", handle);
return NULL;
}
if ((o->internalHandle != handle) || (!o->isValidObject())) {
throwIOException(env, "[EAO] Corrupted handle %i", handle);
return NULL;
}
return o;
}

PoolableObject* ObjectPool::getObject(JNIEnv *env, jlong handle, char poolableObjectType) {
PoolableObject* o = getObject(env, handle);
if ((o != NULL) && (o->poolableObjectType != poolableObjectType)) {
throwIOException(env, "[EAO] Invalid handle type %i", handle);
return NULL;
}
return o;
}

PoolableObject* ObjectPool::getObjectByExternalHandle(jlong handle) {
for(int i = 0; (objs != NULL) && (i < size); i ++) {
if (objs[i] != NULL) {
PoolableObject* o = objs[i];
if (o->isExternalHandle(handle)) {
return o;
}
}
}
return NULL;
}

void ObjectPool::removeObject(PoolableObject* obj) {
jlong idx = realIndex(obj);
if ((idx >= 0) && (idx < size) && (objs != NULL)) {
objs[idx] = NULL;
}
}

DeviceInquiryCallback::DeviceInquiryCallback() {
this->inquiryRunnable = NULL;
this->deviceDiscoveredCallbackMethod = NULL;
this->startedNotify = NULL;
this->startedNotifyNotifyMethod = NULL;
}

BOOL DeviceInquiryCallback::builDeviceInquiryCallbacks(JNIEnv * env, jobject inquiryRunnable, jobject startedNotify) {
jclass inquiryRunnableClass = env->GetObjectClass(inquiryRunnable);

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

jmethodID deviceDiscoveredCallbackMethod = env->GetMethodID(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(startedNotify);
if (notifyClass == NULL) {
throwRuntimeException(env, "Fail to get Object Class");
return FALSE;
}
jmethodID notifyMethod = env->GetMethodID(notifyClass, "deviceInquiryStartedCallback", "()V");
if (notifyMethod == NULL) {
throwRuntimeException(env, "Fail to get MethodID deviceInquiryStartedCallback");
return FALSE;
}

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

return TRUE;
}

BOOL DeviceInquiryCallback::callDeviceInquiryStartedCallback(JNIEnv * env) {
if ((this->startedNotify == NULL) || (this->startedNotifyNotifyMethod == NULL)) {
throwRuntimeException(env, "DeviceInquiryCallback not initialized");
return FALSE;
}
env->CallVoidMethod(this->startedNotify, this->startedNotifyNotifyMethod);
if (ExceptionCheckCompatible(env)) {
return FALSE;
} else {
return TRUE;
}
}

BOOL DeviceInquiryCallback::callDeviceDiscovered(JNIEnv * env, jobject listener, jlong deviceAddr, jint deviceClass, jstring name, jboolean paired) {
if ((this->inquiryRunnable == NULL) || (this->deviceDiscoveredCallbackMethod == NULL)) {
throwRuntimeException(env, "DeviceInquiryCallback not initialized");
return FALSE;
}
env->CallVoidMethod(this->inquiryRunnable, this->deviceDiscoveredCallbackMethod, listener, deviceAddr, deviceClass, name, paired);
if (ExceptionCheckCompatible(env)) {
return FALSE;
} else {
return TRUE;
}
}

// --------------------

RetrieveDevicesCallback::RetrieveDevicesCallback() {
this->listener = NULL;
this->deviceFoundCallbackMethod = NULL;
}

BOOL RetrieveDevicesCallback::builCallback(JNIEnv * env, jobject peer, jobject listener) {
jclass listenerClass = env->GetObjectClass(listener);

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

jmethodID callbackMethod = env->GetMethodID(listenerClass, "deviceFoundCallback", "(JILjava/lang/String;Z)V");
if (callbackMethod == NULL) {
throwRuntimeException(env, "Fail to get MethodID deviceFoundCallback");
return FALSE;
}
this->listener = listener;
this->deviceFoundCallbackMethod = callbackMethod;
return TRUE;
}

BOOL RetrieveDevicesCallback::callDeviceFoundCallback(JNIEnv * env, jlong deviceAddr, jint deviceClass, jstring name, jboolean paired) {
if ((this->listener == NULL) || (this->deviceFoundCallbackMethod == NULL)) {
throwRuntimeException(env, "deviceFoundCallback not initialized");
return FALSE;
}
env->CallVoidMethod(this->listener, this->deviceFoundCallbackMethod, deviceAddr, deviceClass, name, paired);
if (ExceptionCheckCompatible(env)) {
return FALSE;
} else {
return TRUE;
}
}

Change log

r3064 by skarzhevskyy on Nov 28, 2010   Diff
issues #120; Apply changes suggested by
Sven Köhler for mingw64 toolchain
Go to: 
Project members, sign in to write a code review

Older revisions

r2821 by skarzhevskyy on Mar 1, 2009   Diff
review calls for
isCurrentThreadInterrupted
r2559 by skarzhevskyy on Dec 11, 2008   Diff
proper java code for InquiryRunnable
and SearchServicesRunnable
r2450 by skarzhevskyy on Nov 14, 2008   Diff
Implementation of
DiscoveryAgent.retrieveDevices on MS
stack
New interface functions for other
stacks.
All revisions of this file

File info

Size: 29092 bytes, 946 lines

File properties

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