|
1 /* |
|
2 * Copyright (c) 2002 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: SyncML Obex client |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "NSmlBTServiceSearcher.h" |
|
20 |
|
21 //============================================================ |
|
22 // CNSmlBTServiceSearcher definition |
|
23 //============================================================ |
|
24 |
|
25 //------------------------------------------------------------ |
|
26 // CNSmlBTServiceSearcher::CNSmlBTServiceSearcher() |
|
27 //------------------------------------------------------------ |
|
28 CNSmlBTServiceSearcher::CNSmlBTServiceSearcher( const TBTConnInfo& aBTConnInfo ) |
|
29 : iIsDeviceSelectorConnected(EFalse), |
|
30 iBTConnInfo( aBTConnInfo ) |
|
31 { |
|
32 iResponse().SetDeviceAddress( aBTConnInfo.iDevAddr ); |
|
33 } |
|
34 //------------------------------------------------------------ |
|
35 // CNSmlBTServiceSearcher::~CNSmlBTServiceSearcher() |
|
36 //------------------------------------------------------------ |
|
37 CNSmlBTServiceSearcher::~CNSmlBTServiceSearcher() |
|
38 { |
|
39 if( iIsDeviceSelectorConnected ) |
|
40 { |
|
41 iDeviceSelector.CancelNotifier( KDeviceSelectionNotifierUid ); |
|
42 iDeviceSelector.Close(); |
|
43 } |
|
44 Finished(); |
|
45 delete iSdpSearchPattern; |
|
46 iSdpSearchPattern = NULL; |
|
47 |
|
48 delete iAgent; |
|
49 iAgent = NULL; |
|
50 } |
|
51 //------------------------------------------------------------ |
|
52 // CNSmlBTServiceSearcher::SelectDeviceByDiscoveryL( TRequestStatus& aObserverRequestStatus ) |
|
53 //------------------------------------------------------------ |
|
54 void CNSmlBTServiceSearcher::SelectDeviceByDiscoveryL( TRequestStatus& aObserverRequestStatus ) |
|
55 { |
|
56 if( !iIsDeviceSelectorConnected ) |
|
57 { |
|
58 User::LeaveIfError( iDeviceSelector.Connect() ); |
|
59 iIsDeviceSelectorConnected = ETrue; |
|
60 } |
|
61 |
|
62 // Request a device selection |
|
63 TBTDeviceClass deviceClass(0 ,0 ,0); |
|
64 |
|
65 TBTDeviceSelectionParams parameters; |
|
66 parameters.SetDeviceClass(deviceClass); |
|
67 |
|
68 TBTDeviceSelectionParamsPckg pckg(parameters); |
|
69 |
|
70 pckg().SetUUID( iBTConnInfo.iServiceClass ); |
|
71 |
|
72 iDeviceSelector.StartNotifierAndGetResponse( |
|
73 aObserverRequestStatus, |
|
74 KDeviceSelectionNotifierUid, |
|
75 /*selectionFilter*/pckg, |
|
76 iResponse); |
|
77 |
|
78 |
|
79 } |
|
80 //------------------------------------------------------------ |
|
81 // CNSmlBTServiceSearcher::FindServiceL( TRequestStatus& aObserverRequestStatus ) |
|
82 //------------------------------------------------------------ |
|
83 void CNSmlBTServiceSearcher::FindServiceL( TRequestStatus& aObserverRequestStatus ) |
|
84 { |
|
85 if( !iResponse().IsValidBDAddr() ) |
|
86 { |
|
87 User::Leave( KErrNotFound ); |
|
88 } |
|
89 |
|
90 iHasFoundService = EFalse; |
|
91 |
|
92 // delete any existing agent and search pattern |
|
93 delete iSdpSearchPattern; |
|
94 iSdpSearchPattern = NULL; |
|
95 |
|
96 delete iAgent; |
|
97 iAgent = NULL; |
|
98 |
|
99 iAgent = CSdpAgent::NewL( *this, BTDevAddr() ); |
|
100 |
|
101 iSdpSearchPattern = CSdpSearchPattern::NewL(); |
|
102 iSdpSearchPattern->AddL( iBTConnInfo.iServiceClass ); |
|
103 |
|
104 iAgent->SetRecordFilterL( *iSdpSearchPattern ); |
|
105 |
|
106 iStatusObserver = &aObserverRequestStatus; |
|
107 |
|
108 iAgent->NextRecordRequestL(); |
|
109 } |
|
110 //------------------------------------------------------------ |
|
111 // CNSmlBTServiceSearcher::NextRecordRequestComplete( TInt aError, TSdpServRecordHandle aHandle, TInt aTotalRecordsCount ) |
|
112 //------------------------------------------------------------ |
|
113 void CNSmlBTServiceSearcher::NextRecordRequestComplete( TInt aError, TSdpServRecordHandle aHandle, TInt aTotalRecordsCount ) |
|
114 { |
|
115 TRAPD( error, NextRecordRequestCompleteL( aError, aHandle, aTotalRecordsCount) ); |
|
116 if ( error != KErrNone ) |
|
117 { |
|
118 Panic( EBTServiceSearcherNextRecordRequestComplete ); |
|
119 } |
|
120 } |
|
121 //------------------------------------------------------------ |
|
122 // CNSmlBTServiceSearcher::NextRecordRequestCompleteL( TInt aError, TSdpServRecordHandle aHandle, TInt aTotalRecordsCount ) |
|
123 //------------------------------------------------------------ |
|
124 void CNSmlBTServiceSearcher::NextRecordRequestCompleteL( TInt aError, TSdpServRecordHandle aHandle, TInt aTotalRecordsCount ) |
|
125 { |
|
126 if ( aError == KErrEof ) |
|
127 { |
|
128 Finished(); |
|
129 return; |
|
130 } |
|
131 |
|
132 if ( aError != KErrNone ) |
|
133 { |
|
134 Finished( aError ); |
|
135 return; |
|
136 } |
|
137 |
|
138 if ( aTotalRecordsCount == 0 ) |
|
139 { |
|
140 Finished( KErrNotFound ); |
|
141 return; |
|
142 } |
|
143 |
|
144 // Request its attributes |
|
145 iAgent->AttributeRequestL( aHandle, KSdpAttrIdProtocolDescriptorList ); |
|
146 } |
|
147 //------------------------------------------------------------ |
|
148 // CNSmlBTServiceSearcher::AttributeRequestResult() |
|
149 //------------------------------------------------------------ |
|
150 void CNSmlBTServiceSearcher::AttributeRequestResult( |
|
151 TSdpServRecordHandle aHandle, |
|
152 TSdpAttributeID aAttrID, |
|
153 CSdpAttrValue* aAttrValue) |
|
154 { |
|
155 TRAPD( error, AttributeRequestResultL( aHandle, aAttrID, aAttrValue ) ); |
|
156 |
|
157 //deleted because ownership has been transferred |
|
158 delete aAttrValue; |
|
159 aAttrValue = NULL; |
|
160 |
|
161 if( error != KErrNone ) |
|
162 { |
|
163 Panic(EBTServiceSearcherAttributeRequestResult); |
|
164 } |
|
165 } |
|
166 //------------------------------------------------------------ |
|
167 // CNSmlBTServiceSearcher::AttributeRequestResultL() |
|
168 //------------------------------------------------------------ |
|
169 void CNSmlBTServiceSearcher::AttributeRequestResultL( |
|
170 TSdpServRecordHandle /*aHandle*/, |
|
171 TSdpAttributeID aAttrID, |
|
172 CSdpAttrValue* aAttrValue) |
|
173 { |
|
174 |
|
175 ASSERT( aAttrID == KSdpAttrIdProtocolDescriptorList ); |
|
176 |
|
177 TSdpAttributeParser parser( ProtocolList(), *this ); |
|
178 |
|
179 // Validate the attribute value, and extract the RFCOMM channel |
|
180 aAttrValue->AcceptVisitorL( parser ); |
|
181 |
|
182 if ( parser.HasFinished() ) |
|
183 { |
|
184 // Found a suitable record so change state |
|
185 iHasFoundService = ETrue; |
|
186 } |
|
187 } |
|
188 //------------------------------------------------------------ |
|
189 // CNSmlBTServiceSearcher::AttributeRequestComplete() |
|
190 //------------------------------------------------------------ |
|
191 void CNSmlBTServiceSearcher::AttributeRequestComplete(TSdpServRecordHandle aHandle, TInt aError) |
|
192 { |
|
193 TRAPD( error, AttributeRequestCompleteL( aHandle, aError ) ); |
|
194 if( error != KErrNone ) |
|
195 { |
|
196 Panic( EBTServiceSearcherAttributeRequestComplete ); |
|
197 } |
|
198 } |
|
199 //------------------------------------------------------------ |
|
200 // CNSmlBTServiceSearcher::AttributeRequestCompleteL() |
|
201 //------------------------------------------------------------ |
|
202 void CNSmlBTServiceSearcher::AttributeRequestCompleteL(TSdpServRecordHandle /*aHandle*/, TInt aError) |
|
203 { |
|
204 if ( aError != KErrNone ) |
|
205 { |
|
206 } |
|
207 else if( !HasFinishedSearching() ) |
|
208 { |
|
209 // have not found a suitable record so request another |
|
210 iAgent->NextRecordRequestL(); |
|
211 } |
|
212 else |
|
213 { |
|
214 Finished(); |
|
215 } |
|
216 } |
|
217 //------------------------------------------------------------ |
|
218 // CNSmlBTServiceSearcher::Finished() |
|
219 //------------------------------------------------------------ |
|
220 void CNSmlBTServiceSearcher::Finished(TInt aError /* default = KErrNone */) |
|
221 { |
|
222 if( aError == KErrNone && !HasFoundService() ) |
|
223 { |
|
224 aError = KErrNotFound; |
|
225 } |
|
226 if ( iStatusObserver ) |
|
227 { |
|
228 if ( *iStatusObserver == KRequestPending ) |
|
229 { |
|
230 User::RequestComplete( iStatusObserver, aError ); |
|
231 } |
|
232 } |
|
233 } |
|
234 //------------------------------------------------------------ |
|
235 // CNSmlBTServiceSearcher::HasFinishedSearching() |
|
236 //------------------------------------------------------------ |
|
237 TBool CNSmlBTServiceSearcher::HasFinishedSearching() const |
|
238 { |
|
239 return EFalse; |
|
240 } |
|
241 //------------------------------------------------------------ |
|
242 // CNSmlBTServiceSearcher::BTDevAddr() |
|
243 //------------------------------------------------------------ |
|
244 const TBTDevAddr& CNSmlBTServiceSearcher::BTDevAddr() |
|
245 { |
|
246 return iResponse().BDAddr(); |
|
247 } |
|
248 //------------------------------------------------------------ |
|
249 // CNSmlBTServiceSearcher::ResponseParams() |
|
250 //------------------------------------------------------------ |
|
251 const TBTDeviceResponseParams& CNSmlBTServiceSearcher::ResponseParams() |
|
252 { |
|
253 return iResponse(); |
|
254 } |
|
255 //------------------------------------------------------------ |
|
256 // CNSmlBTServiceSearcher::HasFoundService() |
|
257 //------------------------------------------------------------ |
|
258 TBool CNSmlBTServiceSearcher::HasFoundService() const |
|
259 { |
|
260 return iHasFoundService; |
|
261 } |