|
1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <e32base.h> |
|
17 #include <e32std.h> |
|
18 #include "exchange.h" |
|
19 #include "btexchange.h" |
|
20 |
|
21 |
|
22 //Dll entry |
|
23 EXPORT_C TInt _E32Dll() |
|
24 { |
|
25 return KErrNone; |
|
26 } |
|
27 |
|
28 //Construction |
|
29 /** |
|
30 @internalComponent |
|
31 Factory function that constructs a CBluetoothExchanger object. |
|
32 @param aUUID |
|
33 The Universally Unique IDentifier of the service that is provided |
|
34 and should be searched for. |
|
35 @return The constructed object |
|
36 */ |
|
37 EXPORT_C CBluetoothExchanger* CBluetoothExchanger::NewL(const TUUID &aUUID) |
|
38 { |
|
39 CBluetoothExchanger* self = CBluetoothExchanger::NewLC(aUUID); |
|
40 CleanupStack::Pop(self); |
|
41 return self; |
|
42 } |
|
43 |
|
44 /** |
|
45 @internalComponent |
|
46 Factory function that constructs a CBluetoothExchanger object. |
|
47 @param aUUID |
|
48 The Universally Unique IDentifier of the service that is provided |
|
49 and should be searched for. |
|
50 @return The constructed object |
|
51 */ |
|
52 EXPORT_C CBluetoothExchanger* CBluetoothExchanger::NewLC(const TUUID &aUUID) |
|
53 { |
|
54 CBluetoothExchanger* self = new(ELeave) CBluetoothExchanger; |
|
55 CleanupStack::PushL(self); |
|
56 self->ConstructL(aUUID); |
|
57 return self; |
|
58 } |
|
59 |
|
60 /** |
|
61 @internalComponent |
|
62 Initialises the class, and registers the service with the SDP database service. |
|
63 @param aUUID |
|
64 The Universally Unique IDentifier of the service that is provided |
|
65 and should be searched for. |
|
66 @leave If SDP registration fails, the function leaves. |
|
67 */ |
|
68 void CBluetoothExchanger::ConstructL(const TUUID &aUUID) |
|
69 { |
|
70 TSdpServRecordHandle recordHandle = 0; |
|
71 User::LeaveIfError(iSdpSession.Connect()); |
|
72 User::LeaveIfError(iSdpDb.Open(iSdpSession)); |
|
73 iSdpDb.CreateServiceRecordL(aUUID, recordHandle); |
|
74 //Service record with mandatory attributes now exists - maybe add others to it |
|
75 |
|
76 iSockSession.Connect(); |
|
77 iSeeker = CBluetoothSeeker::NewL (iSockSession, aUUID); |
|
78 } |
|
79 |
|
80 //Destruction |
|
81 CBluetoothExchanger::~CBluetoothExchanger() |
|
82 { |
|
83 iSdpDb.Close(); |
|
84 iSdpSession.Close(); |
|
85 } |
|
86 |
|
87 //Overrides of pure virtuals from CExchanger |
|
88 |
|
89 //Inquiry & SDP implementation |
|
90 /** |
|
91 @internalComponent |
|
92 Uses Inquiry and SDP to retrieve a list of peer devices which support |
|
93 the UUID that was registered during construction. |
|
94 The functionality is implemented by the CBluetoothSeeker active object. |
|
95 @param aStatus |
|
96 A TRequestStatus object that is signalled when the search is complete. |
|
97 @leave Leaves if the bluetooth protocols required can't be loaded. |
|
98 */ |
|
99 EXPORT_C void CBluetoothExchanger::LookForPeersL(TRequestStatus &aStatus) |
|
100 { |
|
101 //This is handled by the seeker class that we own. |
|
102 iSeeker->BeginUpdateL(aStatus); |
|
103 } |
|
104 |
|
105 /** |
|
106 @internalComponent |
|
107 @return |
|
108 The count of devices that were found by the search. |
|
109 @pre |
|
110 A LookForPeersL search must have sucessfully completed |
|
111 */ |
|
112 EXPORT_C TInt CBluetoothExchanger::Count() |
|
113 { |
|
114 return iSeeker->Names().Count(); |
|
115 } |
|
116 |
|
117 /** |
|
118 @internalComponent |
|
119 Selects the first device that was found |
|
120 */ |
|
121 EXPORT_C void CBluetoothExchanger::First() |
|
122 { |
|
123 iDeviceIndex = 0; |
|
124 } |
|
125 |
|
126 /** |
|
127 @internalComponent |
|
128 Retrieves the selected device, and selects the next device |
|
129 that was found. |
|
130 @param aPtr |
|
131 A reference to a TNameEntry* pointer. On success, it is filled |
|
132 in with a pointer to a device that was found. |
|
133 @return KErrNotFound on failure, KErrNone on success. |
|
134 */ |
|
135 EXPORT_C TInt CBluetoothExchanger::Next(TNameEntry*& aPtr) |
|
136 { |
|
137 if(iDeviceIndex >= Count()) |
|
138 return (KErrNotFound); |
|
139 aPtr = &(iSeeker->Names()[iDeviceIndex++]); |
|
140 return KErrNone; |
|
141 } |
|
142 |
|
143 // CBluetoothSeeker |
|
144 _LIT(KBluetoothSeekerClassName, "CBluetoothSeeker"); |
|
145 |
|
146 // construction |
|
147 CBluetoothSeeker::CBluetoothSeeker(RSocketServ &aSession) : CActive(CActive::EPriorityStandard), iSession(aSession), iNames(4) |
|
148 { |
|
149 iState = EIdle; |
|
150 } |
|
151 |
|
152 CBluetoothSeeker::~CBluetoothSeeker() |
|
153 { |
|
154 iResolver.Close(); |
|
155 delete iSdpAgent; |
|
156 delete iSdpSearchPattern; |
|
157 } |
|
158 |
|
159 CBluetoothSeeker* CBluetoothSeeker::NewLC(RSocketServ &aSession, const TUUID &aUUID) |
|
160 { |
|
161 CBluetoothSeeker* self = new(ELeave) CBluetoothSeeker(aSession); |
|
162 CleanupStack::PushL(self); |
|
163 self->ConstructL(aUUID); |
|
164 return self; |
|
165 } |
|
166 |
|
167 CBluetoothSeeker* CBluetoothSeeker::NewL(RSocketServ &aSession, const TUUID &aUUID) |
|
168 { |
|
169 CBluetoothSeeker* self = CBluetoothSeeker::NewLC(aSession, aUUID); |
|
170 CleanupStack::Pop(self); |
|
171 return self; |
|
172 } |
|
173 |
|
174 void CBluetoothSeeker::ConstructL(const TUUID &aUUID) |
|
175 { |
|
176 iSdpSearchPattern = CSdpSearchPattern::NewL(); |
|
177 iSdpSearchPattern->AddL(aUUID); |
|
178 CActiveScheduler::Add(this); |
|
179 } |
|
180 |
|
181 void CBluetoothSeeker::RunL() |
|
182 { |
|
183 switch(iState) |
|
184 { |
|
185 case EInquiring: |
|
186 HandleInquiryResultL(); |
|
187 break; |
|
188 case EServiceRequest: |
|
189 SDPQueryL(); |
|
190 break; |
|
191 default: |
|
192 //RunL in bad state |
|
193 User::Panic(KBluetoothSeekerClassName, KErrGeneral); |
|
194 break; |
|
195 } |
|
196 } |
|
197 |
|
198 TInt CBluetoothSeeker::RunError(TInt aErr) |
|
199 { |
|
200 //Complete any outstanding request with an error. |
|
201 if(iUpdateStatus) |
|
202 { |
|
203 User::RequestComplete(iUpdateStatus,aErr); |
|
204 } |
|
205 return KErrNone; |
|
206 } |
|
207 |
|
208 void CBluetoothSeeker::DoCancel() |
|
209 { |
|
210 switch(iState) |
|
211 { |
|
212 case EInquiring: |
|
213 iResolver.Cancel(); |
|
214 break; |
|
215 case EServiceRequest: |
|
216 delete iSdpAgent; |
|
217 iSdpAgent = NULL; |
|
218 break; |
|
219 default: |
|
220 break; |
|
221 } |
|
222 User::RequestComplete(iUpdateStatus, KErrCancel); |
|
223 } |
|
224 |
|
225 void CBluetoothSeeker::BeginUpdateL(TRequestStatus &aStatus) |
|
226 { |
|
227 TProtocolDesc pInfo; |
|
228 _LIT(KLinkMan, "BTLinkManager"); |
|
229 TProtocolName name(KLinkMan); |
|
230 User::LeaveIfError(iSession.FindProtocol(name,pInfo)); |
|
231 if(IsActive()) |
|
232 { |
|
233 User::Leave(KErrInUse); |
|
234 } |
|
235 //First, start an inquiry |
|
236 User::LeaveIfError(iResolver.Open(iSession,pInfo.iAddrFamily,pInfo.iProtocol)); |
|
237 |
|
238 iInquiryAddress.SetIAC(KGIAC); |
|
239 iInquiryAddress.SetAction(KHostResInquiry); |
|
240 iResolver.GetByAddress(iInquiryAddress, iNameEntry, iStatus); |
|
241 SetActive(); |
|
242 iState = EInquiring; |
|
243 iUpdateStatus = &aStatus; |
|
244 *iUpdateStatus = KRequestPending; |
|
245 } |
|
246 |
|
247 CArrayFixFlat<TNameEntry>& CBluetoothSeeker::Names() |
|
248 { |
|
249 return iNames; |
|
250 } |
|
251 |
|
252 void CBluetoothSeeker::HandleInquiryResultL() |
|
253 { |
|
254 if(iStatus.Int() == KErrNone) |
|
255 { |
|
256 //We found another device, add it to the list |
|
257 iNames.AppendL(iNameEntry); |
|
258 iResolver.Next(iNameEntry, iStatus); |
|
259 SetActive(); |
|
260 } |
|
261 else if(iStatus.Int() == KErrHostResNoMoreResults) |
|
262 { |
|
263 //No more results, so now start SDP checks |
|
264 if(iNames.Count() == 0) |
|
265 { |
|
266 //No devices found by inquiry, so complete request now with a failure |
|
267 User::RequestComplete(iUpdateStatus, KErrNotFound); |
|
268 iState = EIdle; |
|
269 } |
|
270 else |
|
271 { |
|
272 //Some devices found, filter them by whether they support this service |
|
273 iDeviceIndex = 0; |
|
274 iState = EServiceRequest; |
|
275 SDPQueryL(); |
|
276 } |
|
277 } |
|
278 else |
|
279 { |
|
280 //Some other error - fail the request |
|
281 User::RequestComplete(iUpdateStatus, iStatus.Int()); |
|
282 iState = EIdle; |
|
283 } |
|
284 } |
|
285 |
|
286 void CBluetoothSeeker::SDPQueryL() |
|
287 { |
|
288 delete iSdpAgent; |
|
289 iSdpAgent = NULL; |
|
290 if(iDeviceIndex < iNames.Count()) |
|
291 { |
|
292 TBTSockAddr* addr = (TBTSockAddr *)&((iNames[iDeviceIndex])().iAddr); |
|
293 iSdpAgent = CSdpAgent::NewL(*this, addr->BTAddr()); |
|
294 iSdpAgent -> SetRecordFilterL(*iSdpSearchPattern); |
|
295 iSdpAgent -> NextRecordRequestL(); |
|
296 // We signal ourselves when the callback completes |
|
297 iStatus = KRequestPending; |
|
298 SetActive(); |
|
299 } |
|
300 else |
|
301 { |
|
302 //No more devices, set final status |
|
303 User::RequestComplete(iUpdateStatus, KErrNone); |
|
304 } |
|
305 } |
|
306 |
|
307 //virtual overrides from MSdpAgentNotifier |
|
308 void CBluetoothSeeker::NextRecordRequestComplete(TInt aError, TSdpServRecordHandle /*aHandle*/, TInt aTotalRecordsCount) |
|
309 { |
|
310 if(aError != KErrNone || aTotalRecordsCount == 0) |
|
311 { |
|
312 //This device doesnt support the service we are looking for |
|
313 //or SDP failed and we can't tell that it does. |
|
314 //Either way, remove it from our list of supported peers. |
|
315 iNames.Delete(iDeviceIndex); |
|
316 } |
|
317 else |
|
318 { |
|
319 //This device does support the service we are looking for |
|
320 //(Attribute requests would be done here if implemented) |
|
321 |
|
322 //While we are connected anyway (due to SDP), do a name request |
|
323 //- its almost free at this point, so use the synchronous version |
|
324 TInquirySockAddr &addr = TInquirySockAddr::Cast(iNames[iDeviceIndex]().iAddr); |
|
325 addr.SetAction(KHostResName); |
|
326 iResolver.GetByAddress(addr, iNames[iDeviceIndex]); |
|
327 //move on and check the next device. |
|
328 iDeviceIndex++; |
|
329 } |
|
330 //Signal ourself to process the next one |
|
331 TRequestStatus *status = &iStatus; |
|
332 User::RequestComplete(status,KErrNone); |
|
333 } |
|
334 |
|
335 void CBluetoothSeeker::AttributeRequestResult(TSdpServRecordHandle , TSdpAttributeID , CSdpAttrValue* ) |
|
336 { |
|
337 //should never be called (not currently used) |
|
338 User::Panic(KBluetoothSeekerClassName, KErrNotSupported); |
|
339 } |
|
340 void CBluetoothSeeker::AttributeRequestComplete(TSdpServRecordHandle, TInt ) |
|
341 { |
|
342 //should never be called (not currently used) |
|
343 User::Panic(KBluetoothSeekerClassName, KErrNotSupported); |
|
344 } |