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
/**
* 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 "WIDCOMMStack.h"

#ifdef _BTWLIB

#ifdef VC6
#define CPP_FILE "WIDCOMMStackRFCOMM.cpp"
#endif

static int openConnections = 0;

WIDCOMMStackRfCommPort::WIDCOMMStackRfCommPort() {

server = NULL;

readyForReuse();

hDataReceivedEvent = CreateEvent(
NULL, // no security attributes
FALSE, // auto-reset event
FALSE, // initial state is NOT signaled
NULL); // object not named

hDataTransmitEvent = CreateEvent(
NULL, // no security attributes
FALSE, // auto-reset event
FALSE, // initial state is NOT signaled
NULL); // object not named

openConnections ++;
}

void WIDCOMMStackRfCommPort::readyForReuse() {
resetReceiveBuffer();
isConnected = FALSE;
isConnectionError = FALSE;
isConnectionErrorType = 0;
other_event_code = 0;
isClosing = FALSE;
readyToFree = FALSE;
service_name[0] = '\0';
}

void WIDCOMMStackRfCommPort::resetReceiveBuffer() {
receiveBuffer.reset();
}

WIDCOMMStackRfCommPort::~WIDCOMMStackRfCommPort() {
if (isConnected) {
isClosing = TRUE;
SetEvent(hConnectionEvent);
Close();
}
isConnected = FALSE;

CloseHandle(hDataTransmitEvent);
CloseHandle(hDataReceivedEvent);
openConnections --;
}

void WIDCOMMStackRfCommPort::OnEventReceived (UINT32 event_code) {
if (stack == NULL) {
return;
}
if ((magic1 != MAGIC_1) || (magic2 != MAGIC_2) || isClosing || (!isValidStackObject(this))) {
ndebug(("e.rf(%i) OnEventReceived for invlaid object, event_code 0x%x", internalHandle, event_code));
return;
}
if (PORT_EV_CONNECTED & event_code) {
if (isConnectionError) {
ndebug(("rf(%i) OnEventReceived connected event 0x%x on broken connection", internalHandle, event_code));
return;
}
ndebug(("rf(%i) OnEventReceived connected event 0x%x", internalHandle, event_code));
BD_ADDR connected_bd_addr;
if (IsConnected(&connected_bd_addr)) {
isConnected = TRUE;
SetEvent(hConnectionEvent);
} else {
ndebug(("rf(%i) OnEventReceived not yet connected", internalHandle));
}
} else if (PORT_EV_CONNECT_ERR & event_code) {
ndebug(("e.rf(%i) OnEventReceived Connect error 0x%x", internalHandle, event_code));
isConnectionErrorType = 1;
isConnectionError = TRUE;
isConnected = FALSE;
SetEvent(hConnectionEvent);
} else if (PORT_EV_OVERRUN & event_code) {
ndebug(("e.rf(%i) OnEventReceived Overrun, 0x%x", internalHandle, event_code));
isConnectionErrorType = 2;
isConnectionError = TRUE;
receiveBuffer.setOverflown();
SetEvent(hConnectionEvent);
} else if (PORT_EV_TXCHAR & event_code) {
// Any character transmitted
SetEvent(hDataTransmitEvent);
} else if (PORT_EV_TXEMPTY & event_code) {
// Transmit Queue Empty
// TODO use in flush();
SetEvent(hDataTransmitEvent);
} else if ((!isConnected) && (!isConnectionError) && (PORT_EV_RXFLAG & event_code)) {
ndebug(("e.rf(%i) OnEventReceived not yet connected, 0x%x", internalHandle, event_code));
isConnectionErrorType = 3;
isConnectionError = TRUE;
SetEvent(hConnectionEvent);
} else {
ndebug(("rf(%i) OnEventReceived event_code 0x%x", internalHandle, event_code));
other_event_code = event_code;
SetEvent(hConnectionEvent);
}
}

void WIDCOMMStackRfCommPort::OnDataReceived(void *p_data, UINT16 len) {
if (stack == NULL) {
return;
}
if ((magic1 != MAGIC_1) || (magic2 != MAGIC_2) || isClosing || (!isValidStackObject(this))) {
ndebug(("e.rf(%i) OnDataReceived for invlaid object", internalHandle));
return;
}
if (isConnected && !isClosing) {
receiveBuffer.write(p_data, len);
SetEvent(hDataReceivedEvent);
}
}

#define open_client_return open_client_finally(rf); return

void open_client_finally(WIDCOMMStackRfCommPort* rf) {
if ((rf != NULL) && (stack != NULL)) {
// Just in case Close
rf->Close();
stack->deleteConnection(rf);
}
if (stack != NULL) {
LeaveCriticalSection(&stack->csCommIf);
}
}

JNIEXPORT jlong JNICALL Java_com_intel_bluetooth_BluetoothStackWIDCOMM_connectionRfOpenClientConnectionImpl
(JNIEnv *env, jobject peer, jlong address, jint channel, jboolean authenticate, jboolean encrypt, jint timeout) {
BD_ADDR bda;
LongToBcAddr(address, bda);

WIDCOMMStackRfCommPort* rf = NULL;
EnterCriticalSection(&stack->csCommIf);
//vc6 __try {
if (stack != NULL) {
rf = stack->createCommPort();
}
if (rf == NULL) {
throwBluetoothConnectionException(env, BT_CONNECTION_ERROR_NO_RESOURCES, "No free connections Objects in Pool");
open_client_return 0;
}
debug(("rf(%i) RfCommPort client handle created", rf->internalHandle));
if ((rf->hConnectionEvent == NULL) || (rf->hDataReceivedEvent == NULL)) {
throwRuntimeException(env, "fails to CreateEvent");
open_client_return 0;
}
debug(("rf(%i) RfCommPort channel %i", rf->internalHandle, channel));
CRfCommIf* rfCommIf = &(stack->rfCommIfClient);

//debug(("AssignScnValue");
// What GUID do we need in call to CRfCommIf.AssignScnValue() if we don't have any?
//memcpy(&(rf->service_guid), &test_client_service_guid, sizeof(GUID));
if (!rfCommIf->AssignScnValue(&(rf->service_guid), (UINT8)channel)) {
throwBluetoothConnectionException(env, BT_CONNECTION_ERROR_UNKNOWN_PSM, "failed to assign SCN %i", (UINT8)channel);
open_client_return 0;
}
//debug(("SetSecurityLevel");
UINT8 sec_level = BTM_SEC_NONE;
if (authenticate) {
sec_level = BTM_SEC_OUT_AUTHENTICATE;
}
if (encrypt) {
sec_level = sec_level | BTM_SEC_OUT_ENCRYPT;
}

BT_CHAR *p_service_name;

#ifdef _WIN32_WCE
p_service_name = (BT_CHAR*)L"bluecovesrv";
#else // _WIN32_WCE
p_service_name = "bluecovesrv";
#endif // #else // _WIN32_WCE

if (!rfCommIf->SetSecurityLevel(p_service_name, sec_level, FALSE)) {
throwBluetoothConnectionException(env, BT_CONNECTION_ERROR_SECURITY_BLOCK, "Error setting security level");
open_client_return 0;
}
Edebug(("OpenClient"));
CRfCommPort::PORT_RETURN_CODE rc = rf->OpenClient((UINT8)channel, bda);
if (rc != CRfCommPort::SUCCESS) {
if (rc == CRfCommPort::PEER_TIMEOUT) {
throwBluetoothConnectionException(env, BT_CONNECTION_ERROR_TIMEOUT, "Failed to OpenClient");
} else {
throwBluetoothConnectionException(env, BT_CONNECTION_ERROR_FAILED_NOINFO, "Failed to OpenClient [%i]", rc);
}
open_client_return 0;
}

Edebug(("waitFor Connection signal"));
DWORD waitStart = GetTickCount();
while ((stack != NULL) && !rf->isClosing && !rf->isConnected && !rf->isConnectionError) {
DWORD rc = WaitForSingleObject(rf->hConnectionEvent, 500);
if (rc == WAIT_FAILED) {
throwRuntimeException(env, "WaitForSingleObject");
open_client_return 0;
}
if (isCurrentThreadInterrupted(env, peer, "connect")) {
open_client_return 0;
}
if ((timeout > 0) && ((GetTickCount() - waitStart) > (DWORD)timeout)) {
throwBluetoothConnectionException(env, BT_CONNECTION_ERROR_TIMEOUT, "Connection timeout");
open_client_return 0;
}
}
if ((stack == NULL) || rf->isClosing || rf->isConnectionError) {
if ((stack != NULL) && (rf->isConnectionError)) {
debug(("RfCommPort isConnectionError %i", rf->isConnectionErrorType));
}
throwBluetoothConnectionException(env, BT_CONNECTION_ERROR_FAILED_NOINFO, "Failed to connect");
open_client_return 0;
}
debug(("rf(%i) rfClientConnected", rf->internalHandle));
//rf->SetFlowEnabled(TRUE);

jlong handle = rf->internalHandle;
rf = NULL;
open_client_return handle;
/* vc6 } __finally {
if ((rf != NULL) && (stack != NULL)) {
// Just in case Close
rf->Close();
stack->deleteCommPort(rf);
}
LeaveCriticalSection(&stack->csCommIf);
}*/
}

void WIDCOMMStackRfCommPort::close(JNIEnv *env, BOOL allowExceptions) {
isClosing = TRUE;
server = NULL;
SetEvent(hConnectionEvent);
debug(("rf(%i) closing RfCommPort, Connected[%s]", internalHandle, bool2str(isConnected)));
Purge(PORT_PURGE_TXCLEAR);
Purge(PORT_PURGE_RXCLEAR);
CRfCommPort::PORT_RETURN_CODE rc = Close();
if (rc != CRfCommPort::SUCCESS && rc != CRfCommPort::NOT_OPENED) {
if (allowExceptions) {
throwIOException(env, "Failed to Close");
}
} else {
debug(("rf(%i) closed RfCommPort", internalHandle));
}
}

JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackWIDCOMM_closeRfCommPortImpl
(JNIEnv *env, jobject peer, jlong handle) {
WIDCOMMStackRfCommPort* rf = validRfCommHandle(env, handle);
if (rf == NULL) {
return;
}
//debug(("CloseClientConnection"));
rf->close(env, true);
// Some worker thread is still trying to access this object, delete later
if (stack != NULL) {
stack->deleteConnection(rf);
}
//debug(("connection handles %i", openConnections));
}

JNIEXPORT jlong JNICALL Java_com_intel_bluetooth_BluetoothStackWIDCOMM_getConnectionRfRemoteAddress
(JNIEnv *env, jobject peer, jlong handle) {
WIDCOMMStackRfCommPort* rf = validRfCommHandle(env, handle);
if (rf == NULL) {
return 0;
}
if (!rf->isConnected || rf->isClosing) {
throwIOException(env, cCONNECTION_IS_CLOSED);
return 0;
}
BD_ADDR connected_bd_addr;
if (rf->IsConnected(&connected_bd_addr)) {
return BcAddrToLong(connected_bd_addr);
} else {
throwIOException(env, "Connection down");
return 0;
}
}

JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackWIDCOMM_connectionRfRead__J
(JNIEnv *env, jobject peer, jlong handle) {
WIDCOMMStackRfCommPort* rf = validRfCommHandle(env, handle);
if (rf == NULL) {
return -1;
}
debug(("rf(%i) read()", rf->internalHandle));
if (rf->isClosing) {
return -1;
}
if (rf->receiveBuffer.isOverflown()) {
throwIOException(env, "Receive buffer overflown");
return 0;
}
HANDLE hEvents[2];
hEvents[0] = rf->hConnectionEvent;
hEvents[1] = rf->hDataReceivedEvent;
BOOL debugOnce = TRUE;
while ((stack != NULL) && rf->isConnected && (!rf->isClosing) && (rf->receiveBuffer.available() == 0)) {
if (debugOnce) {
debug(("read() waits for data"));
debugOnce = FALSE;
}
DWORD rc = WaitForMultipleObjects(2, hEvents, FALSE, 500);
if (rc == WAIT_FAILED) {
throwRuntimeException(env, "WaitForMultipleObjects");
return 0;
}
if (isCurrentThreadInterrupted(env, peer, "read")) {
return 0;
}
/*
if (rf->other_event_code != 0) {
debug(("connectionEvent %i", rf->other_event_code);
rf->other_event_code = 0;
}
debug2("isClosing [%s], isConnected[%s]", bool2str(rf->isClosing), bool2str(rf->isConnected));
*/
}
if ((stack == NULL) || (rf->isClosing) || (rf->receiveBuffer.available() == 0)) {
// See InputStream.read();
return -1;
}
return rf->receiveBuffer.readByte();
}

JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackWIDCOMM_connectionRfRead__J_3BII
(JNIEnv *env, jobject peer, jlong handle, jbyteArray b, jint off, jint len) {
WIDCOMMStackRfCommPort* rf = validRfCommHandle(env, handle);
if (rf == NULL) {
return -1;
}
debug(("rf(%i) read(byte[%i])", rf->internalHandle, len));
if (rf->isClosing) {
debug(("read([]) isClosing"));
return -1;
}
if (rf->receiveBuffer.isOverflown()) {
throwIOException(env, "Receive buffer overflown");
return 0;
}

jbyte *bytes = env->GetByteArrayElements(b, 0);

HANDLE hEvents[2];
hEvents[0] = rf->hConnectionEvent;
hEvents[1] = rf->hDataReceivedEvent;

int done = 0;

BOOL debugOnce = TRUE;
while ((stack != NULL) && rf->isConnected && (!rf->isClosing) && (done < len)) {
while ((stack != NULL) && rf->isConnected && (!rf->isClosing) && (rf->receiveBuffer.available() == 0)) {
if (debugOnce) {
debug(("read[] waits for data"));
debugOnce = FALSE;
}
DWORD rc = WaitForMultipleObjects(2, hEvents, FALSE, 500);
if (rc == WAIT_FAILED) {
env->ReleaseByteArrayElements(b, bytes, 0);
throwRuntimeException(env, "WaitForMultipleObjects");
return 0;
}
if (rc != WAIT_TIMEOUT) {
debug(("read waits returns %s", waitResultsString(rc)));
}
if (isCurrentThreadInterrupted(env, peer, "read")) {
return 0;
}
}
if (stack == NULL) {
env->ReleaseByteArrayElements(b, bytes, 0);
return -1;
}
int count = rf->receiveBuffer.available();
if (count > 0) {
if (count > len - done) {
count = len - done;
}
done += rf->receiveBuffer.read(bytes + off + done, count);
}
if (done != 0) {
// Don't do readFully!
break;
}
debug(("rf(%i) read([]) received %i", rf->internalHandle, count));
}

if (!rf->isConnected) {
debug(("rf(%i) read([]) not connected", rf->internalHandle));
}
// Read from not Connected
int count = rf->receiveBuffer.available();
if (count > 0) {
if (count > len - done) {
count = len - done;
}
done += rf->receiveBuffer.read(bytes + off + done, count);
debug(("read[] available %i", done));
}

if ((stack == NULL) || (rf->isClosing) || (!rf->isConnected && done == 0)) {
if (done == 0) {
debug(("read([]) no data"));
}
// See InputStream.read();
debug(("read([]) return EOF"));
done = -1;
} else {
debug(("read([]) return %i", done));
}
env->ReleaseByteArrayElements(b, bytes, 0);
return done;
}

JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackWIDCOMM_connectionRfReadAvailable
(JNIEnv *env, jobject peer, jlong handle) {
WIDCOMMStackRfCommPort* rf = validRfCommHandle(env, handle);
if (rf == NULL || rf->isClosing) {
return 0;
}
if (rf->receiveBuffer.isOverflown()) {
throwIOException(env, "Receive buffer overflown");
}
return rf->receiveBuffer.available();
}

JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackWIDCOMM_connectionRfWriteImpl
(JNIEnv *env, jobject peer, jlong handle, jbyteArray b, jint off, jint len) {
WIDCOMMStackRfCommPort* rf = validRfCommHandle(env, handle);
if (rf == NULL) {
return;
}
debug(("rf(%i) write(byte[%i])", rf->internalHandle, len));
if (len > 0x10000) {
throwRuntimeException(env, "64K Array max");
return;
}
if (!rf->isConnected || rf->isClosing) {
throwIOException(env, cCONNECTION_IS_CLOSED);
return;
}

jbyte *bytes = env->GetByteArrayElements(b, 0);

jint done = 0;

while ((rf->isConnected) && (!rf->isClosing)) {
UINT8 signal;
rf->GetModemStatus(&signal);
if (signal & PORT_CTSRTS_ON) {
break;
}
if (isCurrentThreadInterrupted(env, peer, "write")) {
return;
}
Sleep(200);
}
HANDLE hEvents[2];
hEvents[0] = rf->hConnectionEvent;
hEvents[1] = rf->hDataTransmitEvent;

BOOL waitForTransmit = false;

while ((done < len) && rf->isConnected && (!rf->isClosing)) {
if (waitForTransmit) {
// second write since not all data was written by first call
debug(("rf(%i) write(byte[%i]) continue from [%i]", rf->internalHandle, len, done));
DWORD rc = WaitForMultipleObjects(2, hEvents, FALSE, 500);
if (rc == WAIT_FAILED) {
env->ReleaseByteArrayElements(b, bytes, 0);
throwRuntimeException(env, "WaitForMultipleObjects");
return;
}
if (rc != WAIT_TIMEOUT) {
debug(("write waits returns %s", waitResultsString(rc)));
}
}
UINT16 written = 0;
UINT16 write_len = (UINT16)(len - done);
CRfCommPort::PORT_RETURN_CODE rc = rf->Write((void*)(bytes + off + done), write_len, &written);
if ((rc != CRfCommPort::SUCCESS) || (written > write_len)) {
throwIOException(env, "Failed to write");
break;
}
done += written;
if (isCurrentThreadInterrupted(env, peer, "write")) {
break;
}
waitForTransmit = true;
}

env->ReleaseByteArrayElements(b, bytes, 0);
}


WIDCOMMStackRfCommPortServer::WIDCOMMStackRfCommPortServer() {
}

WIDCOMMStackRfCommPortServer::~WIDCOMMStackRfCommPortServer() {
}

#define open_rf_server_return open_rf_server_finally(env, rf); return

void open_rf_server_finally(JNIEnv *env, WIDCOMMStackRfCommPortServer* rf) {
if ((rf != NULL) && (stack != NULL)) {
rf->close(env, false);
stack->deleteConnection(rf);
}

if (stack != NULL) {
LeaveCriticalSection(&stack->csCommIf);
}
}

JNIEXPORT jlong JNICALL Java_com_intel_bluetooth_BluetoothStackWIDCOMM_rfServerOpenImpl
(JNIEnv *env, jobject peer, jbyteArray uuidValue, jbyteArray uuidValue2, jboolean obexSrv, jstring name, jboolean authenticate, jboolean encrypt) {
if (stack == NULL) {
throwIOException(env, cSTACK_CLOSED);
return 0;
}
EnterCriticalSection(&stack->csCommIf);
WIDCOMMStackRfCommPortServer* rf = NULL;
//VC6 __try {

rf = (WIDCOMMStackRfCommPortServer*)stack->createCommServer();
if (rf == NULL) {
throwIOException(env, "No free connections Objects in Pool");
open_rf_server_return 0;
}
const char *cname = env->GetStringUTFChars(name, 0);
#ifdef _WIN32_WCE
swprintf_s((wchar_t*)rf->service_name, BT_MAX_SERVICE_NAME_LEN, L"%s", cname);
#else // _WIN32_WCE
sprintf_s(rf->service_name, BT_MAX_SERVICE_NAME_LEN, "%s", cname);
#endif // #else // _WIN32_WCE
env->ReleaseStringUTFChars(name, cname);

jbyte *bytes = env->GetByteArrayElements(uuidValue, 0);
// build UUID
convertUUIDBytesToGUID(bytes, &(rf->service_guid));
env->ReleaseByteArrayElements(uuidValue, bytes, 0);

CRfCommIf* rfCommIf = &(rf->rfCommIf);

BOOL assignScnRC;
#ifdef _WIN32_WCE
assignScnRC = rfCommIf->AssignScnValue(&(rf->service_guid), (UINT8)0);
#else // _WIN32_WCE
assignScnRC = rfCommIf->AssignScnValue(&(rf->service_guid), 0, rf->service_name);
#endif // #else // _WIN32_WCE

if (!assignScnRC) {
throwIOException(env, "failed to assign SCN");
open_rf_server_return 0;
}
rf->scn = rfCommIf->GetScn();

rf->service_guids_len = 1;
if (uuidValue2 != NULL) {
rf->service_guids_len = 2;
}
rf->service_guids = new GUID[rf->service_guids_len];
if (rf->service_guids == NULL) {
throwIOException(env, cOUT_OF_MEMORY);
open_rf_server_return 0;
}
memcpy(&(rf->service_guids[0]), &(rf->service_guid), sizeof(GUID));
if (uuidValue2 != NULL) {
jbyte *bytes2 = env->GetByteArrayElements(uuidValue2, 0);
convertUUIDBytesToGUID(bytes2, &(rf->service_guids[1]));
env->ReleaseByteArrayElements(uuidValue2, bytes2, 0);
}
/*
Can't use AddRFCommProtocolDescriptor
An L2Cap UUID (100) with a value of 3 is added because RFCOMM protocol is over the L2CAP protocol.
build the proto_elem_list
*/
rf->proto_num_elem = 2;
if (obexSrv) {
rf->proto_num_elem++;
}

rf->proto_elem_list = new tSDP_PROTOCOL_ELEM[rf->proto_num_elem];
if (rf->proto_elem_list == NULL) {
throwIOException(env, cOUT_OF_MEMORY);
open_rf_server_return 0;
}
rf->proto_elem_list[0].protocol_uuid = 0x0100; // L2CAP
rf->proto_elem_list[0].num_params = 0;

rf->proto_elem_list[1].protocol_uuid = 0x0003; // RFCOMM
rf->proto_elem_list[1].num_params = 1;
rf->proto_elem_list[1].params[0] = rf->scn;

if (obexSrv) {
rf->proto_elem_list[2].protocol_uuid = 0x0008; // OBEX
rf->proto_elem_list[2].num_params = 0;
}

UINT8 sec_level = BTM_SEC_NONE;
if (authenticate) {
sec_level = BTM_SEC_IN_AUTHENTICATE;
}

if (encrypt) {
sec_level = sec_level | BTM_SEC_IN_ENCRYPT;
}

if (!rfCommIf->SetSecurityLevel(rf->service_name, sec_level, TRUE)) {
throwIOException(env, "Error setting security level");
open_rf_server_return 0;
}

jlong handle = rf->internalHandle;
debug(("rfs(%i) ServerOpen", rf->internalHandle));
rf = NULL;
open_rf_server_return handle;
/*VC6
} __finally {
if ((rf != NULL) && (stack != NULL)) {
rf->closeRfCommPort(env);
stack->deleteCommPort(rf);
}
LeaveCriticalSection(&stack->csCommIf);
}
*/
}

JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackWIDCOMM_rfServerCloseImpl
(JNIEnv *env, jobject peer, jlong handle) {
WIDCOMMStackRfCommPortServer* srv = validRfCommServerHandle(env, handle);
if (srv == NULL) {
return;
}
debug(("rfs(%i) rfCloseServer", srv->internalHandle));
srv->close(env, true);
// Some worker thread is still trying to access this object, delete later
if (stack != NULL) {
stack->deleteConnection(srv);
}
//debug(("connection handles %i", openConnections));
}

#define accept_rf_server_return accept_rf_server_finally(env, rf); return

void accept_rf_server_finally(JNIEnv *env, WIDCOMMStackRfCommPort* rf) {
if ((stack != NULL) && (rf != NULL)) {
if (rf->server != NULL) {
rf->server->closeClient(env, rf);
}
if (stack != NULL) {
stack->deleteConnection(rf);
}
}
}

JNIEXPORT jlong JNICALL Java_com_intel_bluetooth_BluetoothStackWIDCOMM_rfServerAcceptAndOpenRfServerConnection
(JNIEnv *env, jobject peer, jlong handle) {
debug(("rfs(%i) acceptAndOpen", handle));
WIDCOMMStackRfCommPortServer* srv = validRfCommServerHandle(env, handle);
if (srv == NULL) {
return 0;
}
if (srv->sdpService == NULL) {
throwIOException(env, cCONNECTION_IS_CLOSED);
return 0;
}
if (!srv->finalizeSDPRecord(env)) {
return 0;
}

#ifdef BWT_SINCE_SDK_6_0_1
srv->sdpService->CommitRecord();
#endif

EnterCriticalSection(&stack->csCommIf);
if (stack == NULL) {
throwInterruptedIOException(env, cSTACK_CLOSED);
}

WIDCOMMStackRfCommPort* rf = stack->createCommPort();
if (rf == NULL) {
throwIOException(env, "No free connections Objects in Pool");
return 0;
}
srv->addClient(rf);

CRfCommPort::PORT_RETURN_CODE rc = rf->OpenServer(srv->scn);
if (stack != NULL) {
LeaveCriticalSection(&stack->csCommIf);
}

if (rc != CRfCommPort::SUCCESS) {
throwIOException(env, "Failed to OpenServer");
accept_rf_server_return 0;
}

HANDLE hEvents[2];
hEvents[0] = rf->hConnectionEvent;
hEvents[1] = srv->hConnectionEvent;

debug(("rfs(%i) rf(%i) RFCOMM server waits for connection", srv->internalHandle, rf->internalHandle));
while ((stack != NULL) && (!srv->isClosing) && (!rf->isConnected) && (!rf->isConnectionError) && (srv->sdpService != NULL)) {
DWORD rc = WaitForMultipleObjects(2, hEvents, FALSE, 500);
if (rc == WAIT_FAILED) {
throwRuntimeException(env, "WaitForMultipleObjects");
accept_rf_server_return 0;
}
if (isCurrentThreadInterrupted(env, peer, "accept")) {
accept_rf_server_return 0;
}
}

if ((stack == NULL) || srv->isClosing || rf->isConnectionError || (srv->sdpService == NULL)) {
if (stack == NULL) {
throwInterruptedIOException(env, cSTACK_CLOSED);
} else if (srv->isClosing || (srv->sdpService == NULL)) {
throwInterruptedIOException(env, cCONNECTION_CLOSED);
} else if (rf->isConnectionError) {
throwIOException(env, "Connection error");
} else {
throwIOException(env, "Failed to connect");
}
accept_rf_server_return 0;
}
debug(("rfs(%i) rf(%i) RFCOMM server connection made", srv->internalHandle, rf->internalHandle));
return rf->internalHandle;
}

JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackWIDCOMM_rfServerSCN
(JNIEnv *env, jobject, jlong handle) {
WIDCOMMStackRfCommPortServer* srv = validRfCommServerHandle(env, handle);
if (srv == NULL) {
return 0;
}
return srv->scn;
}

JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackWIDCOMM_connectionRfCloseServerConnection
(JNIEnv *env, jobject, jlong handle) {
WIDCOMMStackRfCommPort* rf = validRfCommHandle(env, handle);
if (rf == NULL) {
return;
}
debug(("rf(%i) CloseServerConnection", rf->internalHandle));
WIDCOMMStackServerConnectionBase* srv = rf->server;
if (srv != NULL) {
srv->closeClient(env, rf);
} else {
rf->close(env, true);
}
if (stack != NULL) {
stack->deleteConnection(rf);
}
}

#endif // _BTWLIB

Change log

r2821 by skarzhevskyy on Mar 1, 2009   Diff
review calls for
isCurrentThreadInterrupted
Go to: 
Project members, sign in to write a code review

Older revisions

r2484 by skarzhevskyy on Dec 3, 2008   Diff
respect interrupt() on MS STACK
r2416 by skarzhevskyy on Oct 9, 2008   Diff
Change license to Apache License,
Version 2.0
r2408 by skarzhevskyy on Oct 9, 2008   Diff
organize product to modules
All revisions of this file

File info

Size: 24405 bytes, 792 lines

File properties

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