|
S60 3rd Edition SDK for Symbian OS Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : ChatBtServiceAdverticer.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 <bt_sock.h> 00018 #include "ChatBtServiceAdvertiser.h" 00019 #include "Chat.pan" 00020 00021 // ============================ MEMBER FUNCTIONS ============================== 00022 00023 // ---------------------------------------------------------------------------- 00024 // CChatBtServiceAdvertiser::CChatBtServiceAdvertiser() 00025 // Constructor. 00026 // ---------------------------------------------------------------------------- 00027 // 00028 CChatBtServiceAdvertiser::CChatBtServiceAdvertiser() : 00029 iRecord( 0 ), 00030 iIsConnected( EFalse ) 00031 { 00032 // no implementation required 00033 } 00034 00035 // ---------------------------------------------------------------------------- 00036 // CChatBtServiceAdvertiser::~CChatBtServiceAdvertiser() 00037 // Destructor. 00038 // ---------------------------------------------------------------------------- 00039 // 00040 CChatBtServiceAdvertiser::~CChatBtServiceAdvertiser() 00041 { 00042 if ( IsAdvertising() ) 00043 { 00044 TRAPD( err,StopAdvertisingL() ); 00045 if ( err != KErrNone ) 00046 { 00047 User::Panic( KPanicBTServiceAdvertiser, err ); 00048 } 00049 } 00050 00051 iSdpDatabase.Close(); 00052 iSdpSession.Close(); 00053 } 00054 00055 // ---------------------------------------------------------------------------- 00056 // CChatBtServiceAdvertiser::ConnectL() 00057 // Connect to the SDP database. 00058 // ---------------------------------------------------------------------------- 00059 // 00060 void CChatBtServiceAdvertiser::ConnectL() 00061 { 00062 if ( !iIsConnected ) 00063 { 00064 User::LeaveIfError( iSdpSession.Connect() ); 00065 User::LeaveIfError( iSdpDatabase.Open( iSdpSession ) ); 00066 iIsConnected = ETrue; 00067 } 00068 } 00069 00070 // ---------------------------------------------------------------------------- 00071 // CChatBtServiceAdvertiser::StartAdvertisingL() 00072 // Start the advertising of this service. 00073 // ---------------------------------------------------------------------------- 00074 // 00075 void CChatBtServiceAdvertiser::StartAdvertisingL( TInt aPort ) 00076 { 00077 if ( IsAdvertising() ) 00078 { 00079 // could be advertising on a different port 00080 StopAdvertisingL(); 00081 } 00082 if ( ! iIsConnected ) 00083 { 00084 ConnectL(); 00085 } 00086 iSdpDatabase.CreateServiceRecordL( ServiceClass(), iRecord ); 00087 00088 // add a Protocol to the record 00089 CSdpAttrValueDES* vProtocolDescriptor = CSdpAttrValueDES::NewDESL( NULL ); 00090 CleanupStack::PushL( vProtocolDescriptor ); 00091 00092 BuildProtocolDescriptionL( vProtocolDescriptor,aPort ); 00093 00094 iSdpDatabase.UpdateAttributeL( iRecord, KSdpAttrIdProtocolDescriptorList, 00095 *vProtocolDescriptor ); 00096 00097 CleanupStack::PopAndDestroy( vProtocolDescriptor ); 00098 00099 // Add a name to the record 00100 iSdpDatabase.UpdateAttributeL( iRecord, 00101 KSdpAttrIdBasePrimaryLanguage + 00102 KSdpAttrIdOffsetServiceName, 00103 ServiceName() ); 00104 00105 // Add a description to the record 00106 iSdpDatabase.UpdateAttributeL( iRecord, 00107 KSdpAttrIdBasePrimaryLanguage + 00108 KSdpAttrIdOffsetServiceDescription, 00109 ServiceDescription() ); 00110 00111 } 00112 00113 // ---------------------------------------------------------------------------- 00114 // CChatBtServiceAdvertiser::UpdateAvailabilityL() 00115 // Update the service availability field of the service record. 00116 // ---------------------------------------------------------------------------- 00117 // 00118 void CChatBtServiceAdvertiser::UpdateAvailabilityL( TBool aIsAvailable ) 00119 { 00120 TUint state; 00121 if ( aIsAvailable ) 00122 { 00123 state = 0xFF; // Fully unused 00124 } 00125 else 00126 { 00127 state = 0x00; // Fully used -> can't connect 00128 } 00129 00130 // Update the availibility attribute field 00131 iSdpDatabase.UpdateAttributeL( iRecord, 00132 KSdpAttrIdServiceAvailability, 00133 state ); 00134 00135 // Mark the record as changed - by increasing its state number (version) 00136 iSdpDatabase.UpdateAttributeL( iRecord, 00137 KSdpAttrIdServiceRecordState, 00138 ++iRecordState ); 00139 } 00140 00141 // ---------------------------------------------------------------------------- 00142 // CChatBtServiceAdvertiser::StopAdvertisingL() 00143 // Stop advertising this service. Remove the record from the sdp database. 00144 // ---------------------------------------------------------------------------- 00145 // 00146 void CChatBtServiceAdvertiser::StopAdvertisingL() 00147 { 00148 if ( IsAdvertising() ) 00149 { 00150 iSdpDatabase.DeleteRecordL( iRecord ); 00151 iRecord = 0; 00152 } 00153 } 00154 00155 // ---------------------------------------------------------------------------- 00156 // CChatBtServiceAdvertiser::IsAdvertising() 00157 // Does the SDP database contain a record for this service. 00158 // ---------------------------------------------------------------------------- 00159 // 00160 TBool CChatBtServiceAdvertiser::IsAdvertising() 00161 { 00162 return iRecord != 0; 00163 } 00164 00165 // End of File
|
© Nokia 2006 |