bluetoothengine/btsap/src/BTSapSession.cpp
changeset 0 f63038272f30
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2004 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 *	  SAP Server session definition.
       
    16 *
       
    17 */
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <BTManClient.h>
       
    21 #include "BTSapSession.h"
       
    22 #include "BTSapServer.h"
       
    23 #include "debug.h"
       
    24 
       
    25 // ================= MEMBER FUNCTIONS =======================
       
    26 
       
    27 // C++ default constructor can NOT contain any code that
       
    28 // might leave.
       
    29 
       
    30 // constructor - must pass client to CSession
       
    31 CBTSapSession::CBTSapSession()
       
    32 	{
       
    33 	}
       
    34 
       
    35 // destructor
       
    36 CBTSapSession::~CBTSapSession()
       
    37 	{
       
    38 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSession::~CBTSapSession")));
       
    39     TRAPD(err, Server().SessionClosedL());
       
    40     if(err != KErrNone)
       
    41         {
       
    42         BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS,
       
    43                         BTSapPrintTrace(_L("[BTSap]  CBTSapSession::~CBTSapSession: Error when closing the session! %d"), err));
       
    44         }
       
    45 	}
       
    46 
       
    47 // Two-phased constructor.
       
    48 CBTSapSession* CBTSapSession::NewL()
       
    49 	{
       
    50     return new (ELeave) CBTSapSession();
       
    51 	}
       
    52 
       
    53 void CBTSapSession::CreateL()
       
    54 	{
       
    55 	Server().SessionOpened();
       
    56 	}
       
    57 
       
    58 // ---------------------------------------------------------
       
    59 // Server
       
    60 // Return a reference to CBTSapServer
       
    61 // ---------------------------------------------------------
       
    62 CBTSapServer& CBTSapSession::Server()
       
    63 	{
       
    64 	return *STATIC_CAST(CBTSapServer*, CONST_CAST(CServer2*, CSession2::Server()));
       
    65 	}
       
    66 
       
    67 // ---------------------------------------------------------
       
    68 
       
    69 // ---------------------------------------------------------
       
    70 // ServiceL
       
    71 // Calls DispatchMessage under trap harness
       
    72 //
       
    73 // ---------------------------------------------------------
       
    74 void CBTSapSession::ServiceL(const RMessage2& aMessage)
       
    75 	{
       
    76 	TRAPD(err, DispatchMessageL(aMessage));
       
    77 
       
    78     if(err == KErrPermissionDenied)
       
    79         {
       
    80         // Fail if the client doesn't have the required capabilities
       
    81         aMessage.Complete(err);
       
    82         }
       
    83     else if (err != KErrNone)
       
    84 		{
       
    85         // A bad descriptor error implies a badly programmed client, so panic it;
       
    86 		PanicClient(aMessage, err);
       
    87 		}
       
    88 	}
       
    89 
       
    90 // ---------------------------------------------------------
       
    91 // DispatchMessageL
       
    92 // service a client request; test the opcode and then do
       
    93 // appropriate servicing
       
    94 //
       
    95 // ---------------------------------------------------------
       
    96 void CBTSapSession::DispatchMessageL(const RMessage2 &aMessage)
       
    97 	{
       
    98 	TInt retVal = KErrNone;
       
    99 
       
   100     // Leave with KErrPermissionDenied if the client doesn't have Local Services capability
       
   101     aMessage.HasCapabilityL(ECapabilityLocalServices);
       
   102 
       
   103 	switch (aMessage.Function())
       
   104 		{
       
   105 		case EBTSapManageService:
       
   106 			{
       
   107 			retVal = Server().ManageService(aMessage.Int0());
       
   108 			aMessage.Complete(retVal);
       
   109 
       
   110 			BTSAP_TRACE_OPT(KBTSAP_TRACE_INFO, BTSapPrintTrace(_L("[BTSap]  CBTSapSession: ManageServiceL: %d"), retVal));
       
   111 			break;
       
   112 			}
       
   113 
       
   114 		case EBTSapAcceptSapConnection:
       
   115 			{
       
   116 			retVal = Server().AcceptSapConnection();
       
   117 
       
   118 			BTSAP_TRACE_OPT(KBTSAP_TRACE_INFO, BTSapPrintTrace(_L("[BTSap]  CBTSapSession: AcceptSapConnection: %d"), retVal));
       
   119 			aMessage.Complete(retVal);
       
   120 			break;
       
   121 			}
       
   122 
       
   123 		case EBTSapRejectSapConnection:
       
   124 			{
       
   125 			TBTSapRejectReason reason = STATIC_CAST(TBTSapRejectReason, aMessage.Int0());
       
   126 			retVal = Server().RejectSapConnection(reason);
       
   127 			aMessage.Complete(retVal);
       
   128 
       
   129 			BTSAP_TRACE_OPT(KBTSAP_TRACE_INFO, BTSapPrintTrace(_L("[BTSap]  CBTSapSession: RejectSapConnection: %d"), retVal));
       
   130 			break;
       
   131 			}
       
   132 
       
   133 		case EBTSapDisconnectSapConnection:
       
   134 			{
       
   135 			TBTSapDisconnectType type =	 STATIC_CAST(TBTSapDisconnectType, aMessage.Int0());
       
   136 			retVal = Server().DisconnectSapConnection(type);
       
   137 			aMessage.Complete(retVal);
       
   138 
       
   139 			BTSAP_TRACE_OPT(KBTSAP_TRACE_INFO, BTSapPrintTrace(_L("[BTSap]  CBTSapSession: DisconnectSapConnection: %d"), retVal));
       
   140 			break;
       
   141 			}
       
   142 
       
   143 		case EBTSapIsConnected:
       
   144 			{
       
   145 			retVal = Server().IsSapConnected();
       
   146 			aMessage.Complete(retVal);
       
   147 
       
   148 			BTSAP_TRACE_OPT(KBTSAP_TRACE_INFO, BTSapPrintTrace(_L("[BTSap]  CBTSapSession: IsSapConnected: %d"), retVal));
       
   149 			break;
       
   150 			}
       
   151 
       
   152 		case EBTSapGetRemoteBTAddress:
       
   153 			{
       
   154 			TBTDevAddr addr;
       
   155 			retVal = Server().GetRemoteBTAddress(addr);
       
   156 
       
   157 			TBTDevAddrPckgBuf pckg(addr);
       
   158 			aMessage.WriteL(0, pckg);
       
   159 			aMessage.Complete(retVal);
       
   160 
       
   161 			TBuf<12> buf; 
       
   162 			addr.GetReadable(buf);
       
   163 			BTSAP_TRACE_OPT(KBTSAP_TRACE_INFO, BTSapPrintTrace(_L("[BTSap]  CBTSapSession: GetRemoteBTAddress: %d, addr: %S"), retVal, &buf));
       
   164 			break;
       
   165 			}
       
   166 
       
   167 		default:
       
   168 			{
       
   169 			BTSAP_TRACE_OPT(KBTSAP_TRACE_ERROR, BTSapPrintTrace(_L("[BTSap]  CBTSapSession: DispatchMessage: Bad request #")));
       
   170 			User::Leave(KErrNotSupported);
       
   171 			break;
       
   172 			}
       
   173 		}
       
   174 	}
       
   175 
       
   176 // End of File