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
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2008 Trent Gamblin
*
* 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$
*/


// Toshiba stack not supported on CE
#ifndef _WIN32_WCE


#include "ToshibaStack.h"
#include <process.h>

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

BOOL isToshibaBluetoothStackPresent(JNIEnv *env) {
HMODULE h = LoadLibrary(TOSHIBA_DLL);
if (h == NULL) {
return FALSE;
}
FreeLibrary(h);
return TRUE;
}

#ifdef BLUECOVE_TOSHIBA

#pragma comment(lib, "TosBtAPI.lib")


const UINT MSG_DISCOVERY_UPDATE = WM_USER+1;
const UINT MSG_DISCOVER_NAME_UPDATE = WM_USER+2;
const UINT MSG_SDP_CONNECT_UPDATE = WM_USER+3;
const UINT MSG_SERVICE_SEARCH_UPDATE = WM_USER+4;
const UINT MSG_SERVICE_ATTRIBUTE_UPDATE = WM_USER+5;


static TCHAR *title = L"BLCV";
static HWND hHiddenWindow;
static BTLOCALDEVINFO2 btLocalInfo;
static wchar_t addressString[14];
static bool btInited = false;
static bool btSuccess = false;
static HINSTANCE hInstance;


// discovery stuff
static bool discoveryStarted = false;
static bool discoveryCancelled = false;
static bool discoveryError = false;
static PBTDEVINFOLIST devInfo = NULL;


void tsAddrToString(wchar_t* addressString, BDADDR bd_addr) {
swprintf_s(addressString, 14, L"%02x%02x%02x%02x%02x%02x",
bd_addr[0],
bd_addr[1],
bd_addr[2],
bd_addr[3],
bd_addr[4],
bd_addr[5]);
}


jlong tsGetLongAddress(BDADDR addr)
{
return
((jlong)addr[0] << 40) |
((jlong)addr[1] << 32) |
((jlong)addr[2] << 24) |
((jlong)addr[3] << 16) |
((jlong)addr[4] << 8) |
((jlong)addr[5]);
}


void tsGetBdAddr(jlong address, PBDADDR addr)
{
addr[0] = (BYTE)(((jlong)address >> 40) & 0xFF);
addr[1] = (BYTE)(((jlong)address >> 32) & 0xFF);
addr[2] = (BYTE)(((jlong)address >> 24) & 0xFF);
addr[3] = (BYTE)(((jlong)address >> 16) & 0xFF);
addr[4] = (BYTE)(((jlong)address >> 8) & 0xFF);
addr[5] = (BYTE)(((jlong)address >> 0) & 0xFF);
}


char *getTsAPIStatusString(LONG lSts) {
switch (lSts) {
case TOSBTAPI_NO_ERROR :
return "No errors";
default:
return "Unknown Toshiba error";
}
}

void tsThrowBluetoothStateException(JNIEnv * env, LONG lSts) {
throwBluetoothStateException(env, "Toshiba error# %i", lSts);
}


LRESULT CALLBACK WndProcBt(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LONG lStatus;

switch (message)
{
case WM_CREATE:
btSuccess = btSuccess || BtExecBtMng(&lStatus);
btSuccess = btSuccess || BtOpenAPI(hWnd, "BLCV", &lStatus);
if (btSuccess) {
if (FALSE == BtGetLocalInfo2(&btLocalInfo, &lStatus)) {
btSuccess = false;
}
else {
tsAddrToString(addressString, btLocalInfo.BdAddr);
}
}
btInited = true;
return 0;
case WM_DESTROY:
btInited = false;
btSuccess = false;
BtCloseAPI(&lStatus);
PostQuitMessage(0);
return 0;
case MSG_DISCOVERY_UPDATE: {
HANDLE h = (HANDLE)lParam;
switch (wParam) {
case TOSBTAPI_NM_DISCOVERDEVICE_ERROR:
BtCancelDiscoverRemoteDevice(&lStatus);
discoveryError = true;
SetEvent(h);
break;
case TOSBTAPI_NM_DISCOVERDEVICE_END:
SetEvent(h);
break;
}
return 0;
}
case MSG_DISCOVER_NAME_UPDATE: {
HANDLE h = (HANDLE)lParam;
switch (wParam) {
case TOSBTAPI_NM_DISCOVERNAME_ERROR:
case TOSBTAPI_NM_DISCOVERNAME_END:
SetEvent(h);
break;
}
return 0;
}
case MSG_SDP_CONNECT_UPDATE: {
HANDLE h = (HANDLE)lParam;
switch (wParam) {
case TOSBTAPI_NM_CONNECTSDP_ERROR:
case TOSBTAPI_NM_CONNECTSDP_END:
SetEvent(h);
break;
}
return 0;
}
case MSG_SERVICE_SEARCH_UPDATE: {
HANDLE h = (HANDLE)lParam;
switch (wParam) {
case TOSBTAPI_NM_SS_ERROR:
case TOSBTAPI_NM_SS_END:
SetEvent(h);
break;
}
return 0;
}
case MSG_SERVICE_ATTRIBUTE_UPDATE: {
HANDLE h = (HANDLE)lParam;
switch (wParam) {
case TOSBTAPI_NM_SA_START:
case TOSBTAPI_NM_SA_RESULT:
break;
default:
SetEvent(h);
break;
}
return 0;
}
}

return DefWindowProc(hWnd, message, wParam, lParam);
}


ATOM MyRegisterClassBt(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = 0;
wcex.lpfnWndProc = (WNDPROC)WndProcBt;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = 0;
wcex.hbrBackground = 0;
wcex.lpszMenuName = (TCHAR *)NULL;
wcex.lpszClassName = title;
wcex.hIconSm = NULL;

return RegisterClassEx(&wcex);
}

static void dispatch_thread(void *ignored)
{
MSG msg;

hInstance = GetModuleHandle(NULL);

wsprintf(title, L"BLCV");
MyRegisterClassBt(hInstance);

hHiddenWindow = CreateWindow(title, title, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL,
hInstance, NULL);

if (FAILED(hHiddenWindow)) {
return;
}

while (1) {
GetMessage(&msg, hHiddenWindow, 0, 0);
DispatchMessage(&msg);
if (!btInited)
break;
}
}

JNIEXPORT jboolean JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_isNativeCodeLoaded
(JNIEnv *env, jobject peer) {
return JNI_TRUE;
}


JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_getLibraryVersion
(JNIEnv *, jobject) {
return blueCoveVersion();
}

JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_detectBluetoothStack
(JNIEnv *env, jobject) {
return detectBluetoothStack(env);
}

JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_enableNativeDebug
(JNIEnv *env, jobject, jclass loggerClass, jboolean on) {
enableNativeDebug(env, loggerClass, on);
}

JNIEXPORT jboolean JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_initializeImpl
(JNIEnv *env, jobject) {

_beginthread(dispatch_thread, 0, 0);

while (!btInited) {
Sleep(5);
}

if (FAILED(hHiddenWindow) || !btSuccess) {
btInited = false;
btSuccess = false;
return JNI_FALSE;
}

return JNI_TRUE;
}

JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_destroyImpl
(JNIEnv *, jobject) {
PostMessage(hHiddenWindow, WM_DESTROY, 0, 0);
}

// --- LocalDevice

JNIEXPORT jstring JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_getLocalDeviceBluetoothAddress
(JNIEnv * env, jobject) {
return env->NewString((jchar*)addressString, (jsize)wcslen(addressString));
}


// --- Device discovery

JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_runDeviceInquiryImpl
(JNIEnv * env, jobject peer, jobject startedNotify, jint accessCode, jobject listener) {
LONG lStatus;

if (discoveryStarted) {
throwBluetoothStateException(env, cINQUIRY_RUNNING);
return 0;
}

HANDLE hDiscoveryComplete;

hDiscoveryComplete = CreateEvent(NULL, FALSE, FALSE, NULL);
if (hDiscoveryComplete == NULL) {
return INQUIRY_ERROR;
}

discoveryStarted = true;

DeviceInquiryCallback discoveryCallback;

if (!discoveryCallback.builDeviceInquiryCallbacks(env, peer, startedNotify)) {
discoveryStarted = false;
CloseHandle(hDiscoveryComplete);
return INQUIRY_ERROR;
}

if (!discoveryCallback.callDeviceInquiryStartedCallback(env)) {
discoveryStarted = false;
CloseHandle(hDiscoveryComplete);
return INQUIRY_ERROR;
}

if (devInfo) {
BtMemFree(devInfo);
devInfo = NULL;
}

BtDiscoverRemoteDevice2(&devInfo, TOSBTAPI_DD_NORMAL, &lStatus, hHiddenWindow, MSG_DISCOVERY_UPDATE, (LPARAM)hDiscoveryComplete);

if (lStatus < 0) {
discoveryStarted = false;
return INQUIRY_ERROR;
}

int idx = 0;

while (1) {
DWORD result = WaitForSingleObject(hDiscoveryComplete, 5);
if ((result == WAIT_OBJECT_0 || result == WAIT_TIMEOUT) && devInfo) {
for (int n = devInfo->dwDevListNum; idx < n; idx++) {
PBTDEVINFO dev = &devInfo->DevInfo[idx];
jboolean paired = false; // How do I get this on Toshiba stack?
jlong addr = tsGetLongAddress(dev->BdAddr);
if (!discoveryCallback.callDeviceDiscovered(env, listener, addr, dev->ClassOfDevice, env->NewStringUTF((char *)dev->FriendlyName), paired)) {
BtCancelDiscoverRemoteDevice(&lStatus);
discoveryError = true;
SetEvent(hDiscoveryComplete);
}
}
if (result == WAIT_OBJECT_0)
break;
}
else if (result == WAIT_FAILED || result == WAIT_ABANDONED)
break;
}

discoveryStarted = false;
CloseHandle(hDiscoveryComplete);

if (discoveryCancelled) {
discoveryCancelled = false;
return INQUIRY_TERMINATED;
}
else if (discoveryError) {
discoveryError = false;
return INQUIRY_ERROR;
}
else
return INQUIRY_COMPLETED;
}

JNIEXPORT jboolean JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_deviceInquiryCancelImpl
(JNIEnv *env, jobject peer) {
if (discoveryStarted) {
LONG lStatus;
BtCancelDiscoverRemoteDevice(&lStatus);
discoveryCancelled = true;
}

return TRUE;
}

JNIEXPORT jstring JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_getRemoteDeviceFriendlyNameImpl
(JNIEnv *env, jobject, jlong address)
{
HANDLE hDiscoverName;

hDiscoverName = CreateEvent(NULL, FALSE, FALSE, NULL);
if (hDiscoverName == NULL) {
return NULL;
}

BDADDR addr;
FRIENDLYNAME name;
LONG lStatus;

tsGetBdAddr(address, addr);

BtDiscoverRemoteName(addr, name, &lStatus, hHiddenWindow, MSG_DISCOVER_NAME_UPDATE, (LPARAM)hDiscoverName);

WaitForSingleObject(hDiscoverName, INFINITE);

CloseHandle(hDiscoverName);

if (lStatus >= 0) {
return env->NewStringUTF((char *)name);
}
else {
return NULL;
}
}


// --- Service search


static LONG connectSDP(PBDADDR addr, PWORD pwCID)
{
HANDLE hSDP;
LONG status;

hSDP = CreateEvent(NULL, FALSE, FALSE, NULL);

BtConnectSDP(addr, pwCID, &status, hHiddenWindow, MSG_SDP_CONNECT_UPDATE, (LPARAM)hSDP);

WaitForSingleObject(hSDP, INFINITE);

CloseHandle(hSDP);

return status;
}


JNIEXPORT jshort JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_connectSDPImpl
(JNIEnv *env, jobject, jlong address)
{
WORD cid;
LONG status;
BDADDR addr;

tsGetBdAddr(address, addr);

if ((status = connectSDP(addr, &cid)) < 0) {
tsThrowBluetoothStateException(env, status);
}

return cid;
}

JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_disconnectSDPImpl
(JNIEnv *, jobject, jshort cid)
{
LONG status;
BtDisconnectSDP(cid, &status);
}

JNIEXPORT jlongArray JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_searchServicesImpl
(JNIEnv *env, jobject obj, jobject startedNotify, jshort cid, jobjectArray uuidSet)
{
// Convert uuidSet array to Toshiba format
int n = env->GetArrayLength(uuidSet);
PBTUUIDLIST uuidList = (PBTUUIDLIST)(new BYTE[n*18+4]);
uuidList->dwUUIDInfoNum = n;

for (unsigned int i = 0; i < uuidList->dwUUIDInfoNum; i++) {
jbyteArray barray = (jbyteArray)env->GetObjectArrayElement(uuidSet, i);
jbyte *bytes = env->GetByteArrayElements(barray, false);
for (int j = 0; j < 16; j++) {
uuidList->BtUUIDInfo[i].BtUUID[j] = (BYTE)bytes[j];
}
uuidList->BtUUIDInfo[i].wUUIDType = 128; // 128 bit uuid
}

DWORD patternSize;
PBYTE pattern;
LONG status;

if (BtMakeServiceSearchPattern2(uuidList, &patternSize, &pattern, &status) == false) {
delete[] uuidList;
tsThrowBluetoothStateException(env, status);
}

HANDLE hServiceSearch = CreateEvent(NULL, FALSE, FALSE, NULL);
if (hServiceSearch == NULL) {
delete[] uuidList;
BtMemFree(pattern);
tsThrowBluetoothStateException(env, TOSBTAPI_ERROR);
}

PBTSDPSSRESULT results;

if (BtServiceSearch2(cid, patternSize, pattern, &results, &status, hHiddenWindow, MSG_SERVICE_SEARCH_UPDATE, (LPARAM)hServiceSearch) == FALSE) {
CloseHandle(hServiceSearch);
delete[] uuidList;
BtMemFree(pattern);
tsThrowBluetoothStateException(env, status);
}

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

WaitForSingleObject(hServiceSearch, INFINITE);

CloseHandle(hServiceSearch);

if (results == NULL || status < 0) {
if (isDebugOn()) {
debug(("Search services results == NULL or status < 0\n"));
}
delete[] uuidList;
BtMemFree(pattern);
if (results)
BtMemFree(results);
tsThrowBluetoothStateException(env, status);
}

int size = results->wServiceRecordCount;
jlongArray la = env->NewLongArray(size);
env->SetLongArrayRegion(la, 0, size, (jlong *)results->ulServiceRecordHandleList);

delete[] uuidList;
BtMemFree(pattern);
BtMemFree(results);

return la;
}

JNIEXPORT jbyteArray JNICALL Java_com_intel_bluetooth_BluetoothStackToshiba_populateWorkerImpl
(JNIEnv *env, jobject, jshort cid, jlong handle, jintArray attrIDs)
{
if (attrIDs == NULL) {
return NULL;
}

int nAttr = env->GetArrayLength(attrIDs);
BYTE *attrIdList = new BYTE[nAttr*4];
jint *ids = env->GetIntArrayElements(attrIDs, false);
int offs = 0;

for (int i = 0; i < nAttr; i++) {
jint id = ids[i];
attrIdList[offs++] = (BYTE)((id >> 0) & 0xFF);
attrIdList[offs++] = (BYTE)((id >> 8) & 0xFF);
attrIdList[offs++] = (BYTE)((id >> 16) & 0xFF);
attrIdList[offs++] = (BYTE)((id >> 24) & 0xFF);
}

HANDLE plock = CreateEvent(NULL, FALSE, FALSE, NULL);
if (plock == NULL) {
return NULL;
}

PBTSDPSARESULT result;
LONG status;

BtServiceAttribute2(cid, (unsigned long)handle, offs, attrIdList, &result, &status, hHiddenWindow, MSG_SERVICE_ATTRIBUTE_UPDATE, (LPARAM)plock);

WaitForSingleObject(plock, INFINITE);

CloseHandle(plock);
delete[] attrIdList;

if (status < 0)
return NULL;

jbyteArray b = env->NewByteArray(result->dwAttributeListSize);
env->SetByteArrayRegion(b, 0, result->dwAttributeListSize, (const signed char *)result->bAttributeList);

return b;
}

#endif // BLUECOVE_TOSHIBA
#endif // !_WIN32_WCE

Change log

r2469 by tr...@gamblin.ca on Nov 30, 2008   Diff
#ifdef out Toshiba driver on CE
Go to: 
Project members, sign in to write a code review

Older revisions

r2464 by tr...@gamblin.ca on Nov 28, 2008   Diff
Implemented
RemoteDevice.getFriendlyName for
Toshiba
Implemented service discovery for
Toshiba (retrieving attributes not
...
r2462 by tr...@gamblin.ca on Nov 25, 2008   Diff
Initial Toshiba local device support
and remote discovery
r2416 by skarzhevskyy on Oct 9, 2008   Diff
Change license to Apache License,
Version 2.0
All revisions of this file

File info

Size: 15442 bytes, 603 lines

File properties

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