|
S60 3rd Edition SDK for Symbian OS Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : ChatBtServiceSearcher.cpp 00004 * Part of : Chat 00005 * Interface : 00006 * Description : 00007 * Version : 00008 * 00009 * Copyright (c) 2005-2006 Nokia Corporation. 00010 * This material, including documentation and any related 00011 * computer programs, is protected by copyright controlled by 00012 * Nokia Corporation. 00013 * ============================================================================== 00014 */ 00015 00016 // INCLUDE FILES 00017 #include <StringLoader.h> 00018 #include <ChatEx.rsg> 00019 #include "ChatBtServiceSearcher.h" 00020 #include "ChatBtServiceSearcher.pan" 00021 00022 // ============================ MEMBER FUNCTIONS ============================== 00023 00024 // ---------------------------------------------------------------------------- 00025 // CChatBtServiceSearcher::CChatBtServiceSearcher() 00026 // Constructor. 00027 // ---------------------------------------------------------------------------- 00028 // 00029 CChatBtServiceSearcher::CChatBtServiceSearcher( MLog& aLog ) 00030 : iIsDeviceSelectorConnected( EFalse ), 00031 iLog( aLog ) 00032 { 00033 } 00034 00035 // ---------------------------------------------------------------------------- 00036 // CChatBtServiceSearcher::~CChatBtServiceSearcher() 00037 // Destructor. 00038 // ---------------------------------------------------------------------------- 00039 // 00040 CChatBtServiceSearcher::~CChatBtServiceSearcher() 00041 { 00042 if ( iIsDeviceSelectorConnected ) 00043 { 00044 iDeviceSelector.CancelNotifier( KDeviceSelectionNotifierUid ); 00045 iDeviceSelector.Close(); 00046 } 00047 delete iSdpSearchPattern; 00048 iSdpSearchPattern = NULL; 00049 00050 delete iAgent; 00051 iAgent = NULL; 00052 } 00053 00054 // ---------------------------------------------------------------------------- 00055 // CChatBtServiceSearcher::SelectDeviceByDiscoveryL() 00056 // Select a device. 00057 // ---------------------------------------------------------------------------- 00058 // 00059 void CChatBtServiceSearcher::SelectDeviceByDiscoveryL( 00060 TRequestStatus& aObserverRequestStatus ) 00061 { 00062 if ( ! iIsDeviceSelectorConnected ) 00063 { 00064 User::LeaveIfError( iDeviceSelector.Connect() ); 00065 iIsDeviceSelectorConnected = ETrue; 00066 } 00067 00068 00069 iSelectionFilter().SetUUID( ServiceClass() ); 00070 00071 iDeviceSelector.StartNotifierAndGetResponse( 00072 aObserverRequestStatus, 00073 KDeviceSelectionNotifierUid, 00074 iSelectionFilter, 00075 iResponse ); 00076 } 00077 00078 // ---------------------------------------------------------------------------- 00079 // CChatBtServiceSearcher::FindServiceL() 00080 // Find a service on the specified device. 00081 // ---------------------------------------------------------------------------- 00082 // 00083 void CChatBtServiceSearcher::FindServiceL( TRequestStatus& aObserverRequestStatus ) 00084 { 00085 if ( !iResponse().IsValidBDAddr() ) 00086 { 00087 User::Leave( KErrNotFound ); 00088 } 00089 iHasFoundService = EFalse; 00090 00091 // delete any existing agent and search pattern 00092 delete iSdpSearchPattern; 00093 iSdpSearchPattern = NULL; 00094 00095 delete iAgent; 00096 iAgent = NULL; 00097 00098 iAgent = CSdpAgent::NewL( *this, BTDevAddr() ); 00099 00100 iSdpSearchPattern = CSdpSearchPattern::NewL(); 00101 00102 iSdpSearchPattern->AddL( ServiceClass() ); 00103 // return code is the position in the list that the UUID is inserted at 00104 // and is intentionally ignored 00105 00106 iAgent->SetRecordFilterL( *iSdpSearchPattern ); 00107 00108 iStatusObserver = &aObserverRequestStatus; 00109 00110 iAgent->NextRecordRequestL(); 00111 } 00112 00113 // ---------------------------------------------------------------------------- 00114 // CChatBtServiceSearcher::NextRecordRequestComplete() 00115 // Process the result of the next record request. 00116 // ---------------------------------------------------------------------------- 00117 // 00118 void CChatBtServiceSearcher::NextRecordRequestComplete( 00119 TInt aError, 00120 TSdpServRecordHandle aHandle, 00121 TInt aTotalRecordsCount ) 00122 { 00123 TRAPD( error, 00124 NextRecordRequestCompleteL( aError, aHandle, aTotalRecordsCount ) ); 00125 00126 if ( error != KErrNone ) 00127 { 00128 Panic( EChatBtServiceSearcherNextRecordRequestComplete ); 00129 } 00130 } 00131 00132 // ---------------------------------------------------------------------------- 00133 // CChatBtServiceSearcher::NextRecordRequestCompleteL() 00134 // Process the result of the next record request. 00135 // ---------------------------------------------------------------------------- 00136 // 00137 void CChatBtServiceSearcher::NextRecordRequestCompleteL( 00138 TInt aError, 00139 TSdpServRecordHandle aHandle, 00140 TInt aTotalRecordsCount ) 00141 { 00142 if ( aError == KErrEof ) 00143 { 00144 Finished(); 00145 return; 00146 } 00147 00148 if ( aError != KErrNone ) 00149 { 00150 iLog.LogL( KErrNRRCErr, aError ); 00151 Finished( aError ); 00152 return; 00153 } 00154 00155 if ( aTotalRecordsCount == 0 ) 00156 { 00157 HBufC* errNRRCNoRecords = StringLoader 00158 ::LoadLC ( R_CHAT_ERR_NRRC_NO_RECORDS ); 00159 iLog.LogL( *errNRRCNoRecords ); 00160 CleanupStack::PopAndDestroy ( errNRRCNoRecords ); 00161 Finished( KErrNotFound ); 00162 return; 00163 } 00164 00165 // Request its attributes 00166 iAgent->AttributeRequestL( aHandle, KSdpAttrIdProtocolDescriptorList ); 00167 } 00168 00169 // ---------------------------------------------------------------------------- 00170 // CChatBtServiceSearcher::AttributeRequestResult() 00171 // Process the next attribute requested. 00172 // ---------------------------------------------------------------------------- 00173 // 00174 void CChatBtServiceSearcher::AttributeRequestResult( 00175 TSdpServRecordHandle aHandle, 00176 TSdpAttributeID aAttrID, 00177 CSdpAttrValue* aAttrValue ) 00178 { 00179 TRAPD( error, AttributeRequestResultL( aHandle, aAttrID, aAttrValue ) ); 00180 if ( error != KErrNone ) 00181 { 00182 Panic( EChatBtServiceSearcherAttributeRequestResult ); 00183 } 00184 // Delete obsolete local atribute pointer. 00185 delete aAttrValue; 00186 aAttrValue = NULL; 00187 } 00188 00189 // ---------------------------------------------------------------------------- 00190 // CChatBtServiceSearcher::AttributeRequestResultL() 00191 // Process the next attribute requested. 00192 // ---------------------------------------------------------------------------- 00193 // 00194 void CChatBtServiceSearcher::AttributeRequestResultL( 00195 TSdpServRecordHandle /*aHandle*/, 00196 TSdpAttributeID aAttrID, 00197 CSdpAttrValue* aAttrValue ) 00198 { 00199 __ASSERT_ALWAYS( aAttrID == KSdpAttrIdProtocolDescriptorList, 00200 User::Leave( KErrNotFound ) ); 00201 TChatSdpAttributeParser parser( ProtocolList(), *this ); 00202 00203 // Validate the attribute value, and extract the RFCOMM channel 00204 aAttrValue->AcceptVisitorL( parser ); 00205 00206 if ( parser.HasFinished() ) 00207 { 00208 // Found a suitable record so change state 00209 iHasFoundService = ETrue; 00210 } 00211 } 00212 00213 // ----------------------------------------------------------------------------- 00214 // CChatBtServiceSearcher::AttributeRequestComplete() 00215 // Process the attribute request completion. 00216 // ----------------------------------------------------------------------------- 00217 // 00218 void CChatBtServiceSearcher::AttributeRequestComplete( TSdpServRecordHandle aHandle, 00219 TInt aError ) 00220 { 00221 TRAPD( error, AttributeRequestCompleteL( aHandle, aError ) ); 00222 if ( error != KErrNone ) 00223 { 00224 Panic( EChatBtServiceSearcherAttributeRequestComplete ); 00225 } 00226 } 00227 00228 // ---------------------------------------------------------------------------- 00229 // CChatBtServiceSearcher::AttributeRequestCompleteL() 00230 // Process the attribute request completion. 00231 // ---------------------------------------------------------------------------- 00232 // 00233 void CChatBtServiceSearcher::AttributeRequestCompleteL( 00234 TSdpServRecordHandle /*aHandle*/, 00235 TInt aError ) 00236 { 00237 if ( aError != KErrNone ) 00238 { 00239 HBufC* errCantGetAttribute = StringLoader 00240 ::LoadLC ( R_CHAT_ERR_CANT_GET_ATTRIBUTE ); 00241 iLog.LogL( *errCantGetAttribute, aError ); 00242 CleanupStack::PopAndDestroy ( errCantGetAttribute ); 00243 } 00244 else if ( !HasFinishedSearching() ) 00245 { 00246 // have not found a suitable record so request another 00247 iAgent->NextRecordRequestL(); 00248 } 00249 else 00250 { 00251 HBufC* errAttrReqCom = StringLoader 00252 ::LoadLC ( R_CHAT_ERR_ATTR_REQ_COM ); 00253 iLog.LogL( *errAttrReqCom ); 00254 CleanupStack::PopAndDestroy ( errAttrReqCom ); 00255 Finished(); 00256 } 00257 } 00258 00259 // ---------------------------------------------------------------------------- 00260 // CChatBtServiceSearcher::Finished() 00261 // The search has finished and notify the observer 00262 // that the process is complete. 00263 // ---------------------------------------------------------------------------- 00264 // 00265 void CChatBtServiceSearcher::Finished( TInt aError /* default = KErrNone */ ) 00266 { 00267 if ( aError == KErrNone && !HasFoundService() ) 00268 { 00269 aError = KErrNotFound; 00270 } 00271 User::RequestComplete( iStatusObserver, aError ); 00272 } 00273 00274 // ---------------------------------------------------------------------------- 00275 // CChatBtServiceSearcher::HasFinishedSearching() 00276 // Is the instance still wanting to search. 00277 // ---------------------------------------------------------------------------- 00278 // 00279 TBool CChatBtServiceSearcher::HasFinishedSearching() const 00280 { 00281 return EFalse; 00282 } 00283 00284 // ---------------------------------------------------------------------------- 00285 // CChatBtServiceSearcher::BTDevAddr() 00286 // Returns the bluetooth device address. 00287 // ---------------------------------------------------------------------------- 00288 // 00289 const TBTDevAddr& CChatBtServiceSearcher::BTDevAddr() 00290 { 00291 return iResponse().BDAddr(); 00292 } 00293 00294 // ---------------------------------------------------------------------------- 00295 // CChatBtServiceSearcher::ResponseParams() 00296 // Returns information about the device selected by the user. 00297 // ---------------------------------------------------------------------------- 00298 // 00299 const TBTDeviceResponseParams& CChatBtServiceSearcher::ResponseParams() 00300 { 00301 return iResponse(); 00302 } 00303 00304 // ---------------------------------------------------------------------------- 00305 // CChatBtServiceSearcher::HasFoundService() 00306 // True if a service has been found. 00307 // ---------------------------------------------------------------------------- 00308 // 00309 TBool CChatBtServiceSearcher::HasFoundService() const 00310 { 00311 return iHasFoundService; 00312 } 00313 00314 // End of File
|
© Nokia 2006 |