ImagePrint/ImagePrintEngine/DeviceProtocols/btprotocol/src/cbtsdpadvertiser.cpp
branchRCL_3
changeset 27 159fc2f68139
parent 21 26673e532f65
child 28 d59c248c9d36
equal deleted inserted replaced
21:26673e532f65 27:159fc2f68139
     1 /*
       
     2 * Copyright (c) 2004-2007 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 <bt_sock.h>
       
    20 #include <StringLoader.h>
       
    21 
       
    22 #include "cbtsdpadvertiser.h"
       
    23 #include "clog.h"
       
    24 
       
    25 // ============================ MEMBER FUNCTIONS ==============================
       
    26 
       
    27 // ----------------------------------------------------------------------------
       
    28 // CBtSdpAdvertiser::CBtSdpAdvertiser()
       
    29 // Constructor.
       
    30 // ----------------------------------------------------------------------------
       
    31 //
       
    32 CBtSdpAdvertiser::CBtSdpAdvertiser()
       
    33 :   iRecord( 0 ),
       
    34     iIsConnected( EFalse )
       
    35     {
       
    36     // no implementation required
       
    37     }
       
    38 
       
    39 // ----------------------------------------------------------------------------
       
    40 // CBtSdpAdvertiser::~CBtSdpAdvertiser()
       
    41 // Destructor.
       
    42 // ----------------------------------------------------------------------------
       
    43 //
       
    44 CBtSdpAdvertiser::~CBtSdpAdvertiser()
       
    45     {
       
    46     // If this fails there is nothing to do.
       
    47     TRAPD( err,StopAdvertisingL() );
       
    48     if ( err != KErrNone )
       
    49         {
       
    50 			LOG1("[CBtSdpAdvertiser::~CBtSdpAdvertiser]\t StopAdvertisingL leaves with: %d", err);
       
    51         }
       
    52     iSdpDatabase.Close();
       
    53     iSdpSession.Close();
       
    54 
       
    55 	LOG("[CBtSdpAdvertiser::~CBtSdpAdvertiser]\t done");
       
    56     }
       
    57 
       
    58 // ----------------------------------------------------------------------------
       
    59 // CBtSdpAdvertiser::ConnectL()
       
    60 // Connect to the SDP database.
       
    61 // ----------------------------------------------------------------------------
       
    62 //
       
    63 void CBtSdpAdvertiser::ConnectL()
       
    64     {
       
    65     if ( !iIsConnected )
       
    66         {
       
    67         User::LeaveIfError( iSdpSession.Connect() );
       
    68         User::LeaveIfError( iSdpDatabase.Open( iSdpSession ) );
       
    69         iIsConnected = ETrue;
       
    70         }
       
    71 		LOG1("[CBtSdpAdvertiser::ConnectL]\t iIsConnected: %d", iIsConnected);
       
    72     }
       
    73 
       
    74 // ----------------------------------------------------------------------------
       
    75 // CBtSdpAdvertiser::StartAdvertisingL()
       
    76 // Start the advertising of this service.
       
    77 // ----------------------------------------------------------------------------
       
    78 //
       
    79 void CBtSdpAdvertiser::StartAdvertisingL( TInt aPort )
       
    80     {
       
    81     // could be advertising on a different port
       
    82     StopAdvertisingL();
       
    83 
       
    84     if ( !iIsConnected )
       
    85         {
       
    86         ConnectL();
       
    87         }
       
    88     iSdpDatabase.CreateServiceRecordL( ServiceClass(), iRecord );
       
    89 
       
    90 	LOG1("[CBtSdpAdvertiser::StartAdvertisingL]\t iRecord: %d", iRecord);
       
    91 
       
    92     // add a Protocol to the record
       
    93     CSdpAttrValueDES* vProtocolDescriptor = CSdpAttrValueDES::NewDESL( NULL );
       
    94     CleanupStack::PushL( vProtocolDescriptor );
       
    95 
       
    96     BuildProtocolDescriptionL( vProtocolDescriptor,aPort );
       
    97 
       
    98     iSdpDatabase.UpdateAttributeL( iRecord, KSdpAttrIdProtocolDescriptorList,
       
    99         *vProtocolDescriptor );
       
   100 
       
   101     CleanupStack::PopAndDestroy( vProtocolDescriptor );
       
   102 
       
   103     // Add a name to the record
       
   104     iSdpDatabase.UpdateAttributeL( iRecord,
       
   105                                   KSdpAttrIdBasePrimaryLanguage +
       
   106                                   KSdpAttrIdOffsetServiceName,
       
   107                                   ServiceName() );
       
   108 
       
   109     // Add a description to the record
       
   110     iSdpDatabase.UpdateAttributeL( iRecord,
       
   111                                   KSdpAttrIdBasePrimaryLanguage +
       
   112                                   KSdpAttrIdOffsetServiceDescription,
       
   113                                   ServiceDescription() );
       
   114 
       
   115 	LOG("[CBtSdpAdvertiser::StartAdvertisingL]\t end");
       
   116     }
       
   117 
       
   118 // ----------------------------------------------------------------------------
       
   119 // CBtSdpAdvertiser::UpdateAvailabilityL()
       
   120 // Update the service availability field of the service record.
       
   121 // ----------------------------------------------------------------------------
       
   122 //
       
   123 void CBtSdpAdvertiser::UpdateAvailabilityL( TBool aIsAvailable )
       
   124     {
       
   125 	LOG("[CBtSdpAdvertiser::UpdateAvailabilityL]\t ");
       
   126     TUint state;
       
   127     if ( aIsAvailable )
       
   128         {
       
   129         state = 0xFF;  //  Fully unused
       
   130         }
       
   131     else
       
   132         {
       
   133         state = 0x00;  //  Fully used -> can't connect
       
   134         }
       
   135 
       
   136     //  Update the availibility attribute field
       
   137     iSdpDatabase.UpdateAttributeL( iRecord,
       
   138         KSdpAttrIdServiceAvailability, state );
       
   139 
       
   140 
       
   141     //  Mark the record as changed - by increasing its state number (version)
       
   142     iSdpDatabase.UpdateAttributeL( iRecord,
       
   143         KSdpAttrIdServiceRecordState, ++iRecordState );
       
   144 
       
   145 	LOG("[CBtSdpAdvertiser::UpdateAvailabilityL]\t end");
       
   146     }
       
   147 
       
   148 // ----------------------------------------------------------------------------
       
   149 // CBtSdpAdvertiser::StopAdvertisingL()
       
   150 // Stop advertising this service. Remove the record from the sdp database.
       
   151 // ----------------------------------------------------------------------------
       
   152 //
       
   153 void CBtSdpAdvertiser::StopAdvertisingL()
       
   154     {
       
   155     if ( IsAdvertising() )
       
   156         {
       
   157         iSdpDatabase.DeleteRecordL( iRecord );
       
   158         iRecord = 0;
       
   159         }
       
   160     }
       
   161 
       
   162 // ----------------------------------------------------------------------------
       
   163 // CBtSdpAdvertiser::IsAdvertising()
       
   164 // Does the SDP database contain a record for this service.
       
   165 // ----------------------------------------------------------------------------
       
   166 //
       
   167 TBool CBtSdpAdvertiser::IsAdvertising()
       
   168     {
       
   169 	LOG1("[CBtSdpAdvertiser::IsAdvertising]\t %d", iRecord != 0);
       
   170     return iRecord != 0;
       
   171     }
       
   172 
       
   173 // End of File