|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "bluetoothnamelookup.h" |
|
20 #include "fs_methodcall.h" |
|
21 #include "javajniutils.h" |
|
22 #include "logger.h" |
|
23 |
|
24 namespace java |
|
25 { |
|
26 namespace bluetooth |
|
27 { |
|
28 |
|
29 _LIT(KBTLinkManagerTxt, "BTLinkManager"); |
|
30 |
|
31 OS_EXPORT BluetoothNameLookup* BluetoothNameLookup::NewL() |
|
32 { |
|
33 JELOG2(EJavaBluetooth); |
|
34 BluetoothNameLookup* btNameLookup = new BluetoothNameLookup(); |
|
35 CleanupStack::PushL(btNameLookup); |
|
36 btNameLookup->constructL(); |
|
37 CleanupStack::Pop(btNameLookup); |
|
38 return btNameLookup; |
|
39 } |
|
40 |
|
41 void BluetoothNameLookup::constructL() |
|
42 { |
|
43 JELOG2(EJavaBluetooth); |
|
44 User::LeaveIfError(mSocketServ.Connect()); |
|
45 } |
|
46 |
|
47 OS_EXPORT BluetoothNameLookup::~BluetoothNameLookup() |
|
48 { |
|
49 JELOG2(EJavaBluetooth); |
|
50 mHostResolver.Close(); |
|
51 mSocketServ.Close(); |
|
52 delete mLookupMonitor; |
|
53 } |
|
54 |
|
55 OS_EXPORT std::wstring * BluetoothNameLookup::doDeviceNameLookupL( |
|
56 TInt64 aDevAddr) |
|
57 { |
|
58 JELOG2(EJavaBluetooth); |
|
59 mLookupName = NULL; |
|
60 |
|
61 if (0 == mLookupMonitor) |
|
62 { |
|
63 mLookupMonitor = new CActiveSchedulerWait(); |
|
64 } |
|
65 LOG1(EJavaBluetooth, EInfo, |
|
66 "+ BluetoothNameLookup::doDeviceNameLookupL DeviceAddress:%x", |
|
67 aDevAddr); |
|
68 |
|
69 TProtocolDesc pdesc; |
|
70 User::LeaveIfError(mSocketServ.FindProtocol(KBTLinkManagerTxt(), pdesc)); |
|
71 |
|
72 // Initialize host resolver |
|
73 mHostResolver.Close(); |
|
74 User::LeaveIfError(mHostResolver.Open(mSocketServ, pdesc.iAddrFamily, |
|
75 pdesc.iProtocol)); |
|
76 |
|
77 TBTDevAddr deviceAddr(aDevAddr); |
|
78 |
|
79 mSockAddr.SetBTAddr(deviceAddr); |
|
80 mSockAddr.SetAction(KHostResName | KHostResIgnoreCache); |
|
81 mHostResolver.GetByAddress(mSockAddr, mNameEntry, iStatus); |
|
82 |
|
83 SetActive(); |
|
84 mLookupMonitor->Start(); |
|
85 if (KErrNone != mLookupStatus && KErrCancel != mLookupStatus |
|
86 && KErrCompletion != mLookupStatus) |
|
87 { |
|
88 //if there is any error, then we send null. need to throw IOException |
|
89 //at the java side if null is returned |
|
90 ELOG1(EJavaBluetooth, |
|
91 "- BluetoothNameLookup::doDeviceNameLookupL Error %d", |
|
92 mLookupStatus); |
|
93 User::Leave(mLookupStatus); |
|
94 } |
|
95 |
|
96 return mLookupName; |
|
97 } |
|
98 |
|
99 void BluetoothNameLookup::RunL() |
|
100 { |
|
101 JELOG2(EJavaBluetooth); |
|
102 std::wstring *deviceName = NULL; |
|
103 if (KErrNone == iStatus.Int() && NULL != mNameEntry().iName.Ptr()) |
|
104 { |
|
105 deviceName = new std::wstring((wchar_t*) mNameEntry().iName.Ptr()); |
|
106 deviceName->resize(mNameEntry().iName.Length()); |
|
107 } |
|
108 |
|
109 nameLookupCompleted(iStatus.Int(), deviceName); |
|
110 //We do not set this to active again as we just need to lookup once |
|
111 } |
|
112 |
|
113 /** |
|
114 * Notify the monitor, which waits for the name lookup to complete |
|
115 */ |
|
116 void BluetoothNameLookup::nameLookupCompleted(int aStatus, |
|
117 std::wstring* deviceName) |
|
118 { |
|
119 JELOG2(EJavaBluetooth); |
|
120 mLookupStatus = aStatus; |
|
121 mLookupName = deviceName; |
|
122 mLookupMonitor->AsyncStop(); |
|
123 } |
|
124 |
|
125 void BluetoothNameLookup::DoCancel() |
|
126 { |
|
127 } |
|
128 |
|
129 } //end namespace java |
|
130 } //end namespace bluetooth |