obex/obexprotocol/obex/src/TObexServerStateObexConnecting.cpp
changeset 57 f6055a57ae18
parent 0 d0791faffa3f
equal deleted inserted replaced
54:4dc88a4ac6f4 57:f6055a57ae18
       
     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 <obex.h>
       
    17 #include "obexserverstatemachine.h"
       
    18 
       
    19 /**
       
    20 @file
       
    21 @internalComponent
       
    22 
       
    23 OBEX Connecting State
       
    24 This state is entered during an OBEX connection attempt. The authentication state machine is
       
    25 invoked via ParseConnectPacket and PrepareConnect packet.
       
    26 A successful CONNECT will move the machine to Ready.
       
    27 A CONNECT requiring a user password will move the machine to WaitForUserPassword
       
    28 Other CONNECT results may stay in this state or move the machine to TransportConnected
       
    29 A DISCONNECT will be processed
       
    30 An ABORT will cause a Protocol Error
       
    31 Any other OBEX operation will be answered with an OBEX error code
       
    32 */
       
    33 
       
    34 TObexServerStateObexConnecting::TObexServerStateObexConnecting()
       
    35 	{
       
    36 #ifdef __FLOG_ACTIVE
       
    37 	_LIT8(KName, "ObexConnecting");
       
    38 	iName = KName;
       
    39 #endif
       
    40 	}
       
    41 
       
    42 void TObexServerStateObexConnecting::Entry(CObexServerStateMachine& aContext)
       
    43 	{
       
    44 	// Reset the Connect State Machine
       
    45 	aContext.Owner().SetConnectState(CObex::EConnTransport);
       
    46 	// Chain on to Connect to process packet
       
    47 	Connect(aContext, aContext.LastReceivedPacket());
       
    48 	}
       
    49 
       
    50 void TObexServerStateObexConnecting::Connect(CObexServerStateMachine& aContext, CObexPacket& aPacket)
       
    51 	{
       
    52 	// Process connect packet
       
    53 	// This will return KErrNone if the packet is OK
       
    54 	//                  a positive number if an OBEX error should be returned to the Client
       
    55 	//				    a negative number if a Symbian error should be raised (as a Protocol Error)
       
    56 	TInt parseConnectResult = aContext.Owner().ParseConnectPacket(aPacket);
       
    57 	if (parseConnectResult == KErrNone)
       
    58 		{
       
    59 		FLOG(_L("OnPacketReceive ParseConnectPacket succesfull\r\n"));
       
    60 		aContext.Transport().SendPacket().Init(CObex::EOpConnect); 
       
    61 		// PrepareConnectPacket will check the present state and request
       
    62 		// Auth, or check Auth as necessary and will return:
       
    63 		//		An error & connect state set to EWaitForUserInput if a User Password is required  
       
    64 		//		No error and connect state set to EConnObex if the connection was successful
       
    65 		//		Connect state set to EConnTransport if there is an error packet to send back to the client
       
    66 		//		Other connect states will return a packet to send and stay in the current state
       
    67 		TInt err = aContext.Owner().PrepareConnectPacket(aContext.Transport().SendPacket());
       
    68 		if ((err == KErrNone)	|| (aContext.Owner().GetConnectState() == CObex::EConnTransport)
       
    69 								|| (aContext.Owner().GetConnectState() == CObex::EWaitForUserInput))
       
    70 			{
       
    71 			FLOG(_L("OnPacketReceive PrepareConnectPacket SUCCESS\r\n"));
       
    72 			// If not waiting for user input, send the resultant packet
       
    73 			if ( aContext.Owner().GetConnectState() != CObex::EWaitForUserInput )
       
    74 				{
       
    75 				aContext.Transport().SendPacket().SetFinal();
       
    76 				aContext.Transport().Send();
       
    77 				}
       
    78 			// Move this (operation) state machine according to result of authentication state machine
       
    79 			switch (aContext.Owner().GetConnectState())
       
    80 				{
       
    81 				case CObex::EConnTransport:
       
    82 					aContext.ChangeState(CObexServerStateMachine::ETransportConnected);
       
    83 					break;
       
    84 				case CObex::EConnObex:
       
    85 					aContext.ChangeState(CObexServerStateMachine::EReady);
       
    86 					break;
       
    87 				case CObex::EWaitForUserInput:
       
    88 					aContext.ChangeState(CObexServerStateMachine::EWaitForUserPassword);
       
    89 					break;
       
    90 				}
       
    91 			}
       
    92 		else 
       
    93 			{
       
    94 			FTRACE( if (err)
       
    95 						{
       
    96 						FPrint(_L("OnPacketReceive PrepareConnectPacket FAILED"));
       
    97 						}
       
    98 					else
       
    99 						{
       
   100 						FPrint(_L("OnPacketReceive PrepareConnectPacket OK but state is %d"), aContext.Owner().GetConnectState());
       
   101 						}
       
   102 					);
       
   103 			// Raising a Protocol Error will cause a Reset event resulting in a move to Disconnected.
       
   104 			// So any code after this call will potentially be executed in a different state.
       
   105 			aContext.Owner().Error(err);
       
   106 			}
       
   107 		}
       
   108 	else if (parseConnectResult > 0) // so it's an OBEX error code
       
   109 		{
       
   110 		FLOG(_L("OnPacketReceive ParseConnectPacket FAILED (OBEX error)\r\n"));
       
   111 		
       
   112 		aContext.Transport().SendPacket().Init(parseConnectResult); 
       
   113 		TInt rsp = aContext.Owner().PrepareErroredConnectPacket(aContext.Transport().SendPacket());
       
   114 		if (rsp == KErrNone)	
       
   115    			{
       
   116 			aContext.Transport().SendPacket().SetFinal();
       
   117 			aContext.Transport().Send();
       
   118 			aContext.ChangeState(CObexServerStateMachine::ETransportConnected);
       
   119 			}			
       
   120 		else
       
   121 			{
       
   122 			aContext.Owner().Error(rsp);	
       
   123 			}
       
   124 		}
       
   125 	else // parseConnectResult < 0 so it's a Symbian error code
       
   126 		{
       
   127 		FLOG(_L("OnPacketReceive ParseConnectPacket FAILED (Symbian error)\r\n"));
       
   128 		// Raising a Protocol Error will cause a Reset event resulting in a move to Disconnected.
       
   129 		// So any code after this call will potentially be executed in a different state.
       
   130 		aContext.Owner().Error(KErrAbort);
       
   131 		}
       
   132 	}
       
   133 
       
   134 void TObexServerStateObexConnecting::Disconnect(CObexServerStateMachine& aContext, CObexPacket& aPacket)
       
   135 	{
       
   136 	// Process disconnect
       
   137 	PerformDisconnect(aContext, aPacket);
       
   138 	}
       
   139 
       
   140 void TObexServerStateObexConnecting::Put(CObexServerStateMachine& aContext, CObexPacket& /*aPacket*/)
       
   141 	{
       
   142 	// Send ERespBadRequest and return to ETransportConnected
       
   143 	aContext.Transport().Send(ERespBadRequest);
       
   144 	aContext.ChangeState(CObexServerStateMachine::ETransportConnected);
       
   145 	}
       
   146 
       
   147 void TObexServerStateObexConnecting::Get(CObexServerStateMachine& aContext, CObexPacket& /*aPacket*/)
       
   148 	{
       
   149 	// Send ERespConflict and return to ETransportConnected
       
   150 	aContext.Transport().Send(ERespConflict);
       
   151 	aContext.ChangeState(CObexServerStateMachine::ETransportConnected);
       
   152 	}
       
   153 
       
   154 void TObexServerStateObexConnecting::SetPath(CObexServerStateMachine& aContext, CObexPacket& /*aPacket*/)
       
   155 	{
       
   156 	// Send ERespConflict and return to ETransportConnected
       
   157 	aContext.Transport().Send(ERespConflict);
       
   158 	aContext.ChangeState(CObexServerStateMachine::ETransportConnected);
       
   159 	}
       
   160 
       
   161 void TObexServerStateObexConnecting::Abort(CObexServerStateMachine& aContext)
       
   162 	{
       
   163 	// Send ERespSuccess
       
   164 	// Any other response would, according to the spec,
       
   165 	// require the Obex client to bring down the transport.
       
   166 	// Our attempt is to be resilient if an 'Abort'
       
   167 	// is sent erroneously whilst we are in this state.
       
   168 	aContext.Transport().Send(ERespSuccess);
       
   169 	aContext.ChangeState(CObexServerStateMachine::ETransportConnected);
       
   170 	}
       
   171 	
       
   172 void TObexServerStateObexConnecting::OverrideRequestHandling(CObexServerStateMachine& aContext, TObexResponse aResponse)
       
   173 	{
       
   174 	// Send the server applications response and return to Transport Connected
       
   175 	aContext.Transport().Send(aResponse);
       
   176 	aContext.ChangeState(CObexServerStateMachine::ETransportConnected);
       
   177 	}
       
   178