obex/obexprotocol/obex/src/OBEXUTIL.CPP
changeset 0 d0791faffa3f
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 1997-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 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include <e32std.h>
       
    22 #include <obextransportinfo.h>
       
    23 #include <obexirtransportinfo.h>
       
    24 #include <obexbttransportinfo.h>
       
    25 #include <obexusbtransportinfo.h>
       
    26 #include "obexcommon.h"
       
    27 #include "OBEXUTIL.H"
       
    28 #include "obexfaults.h"
       
    29 
       
    30 
       
    31 void IrOBEXUtil::Panic(TObexPanicCode aCode)
       
    32 	{
       
    33 	User::Panic(KObexPanicCategory, aCode);
       
    34 	}
       
    35 
       
    36 
       
    37 void IrOBEXUtil::Fault(TObexFaultCode aCode)
       
    38 	{
       
    39 	User::Panic(KObexFaultCategory, aCode);
       
    40 	}
       
    41 
       
    42 /**
       
    43 Converts aResp to an EPOC error number 
       
    44 */
       
    45 TInt IrOBEXUtil::EpocError(TUint8 aResp)
       
    46 	{
       
    47 	switch (aResp)
       
    48 		{
       
    49 	case ERespContinue:
       
    50 	case ERespSuccess:
       
    51 	case ERespCreated:
       
    52 	case ERespAccepted:
       
    53 		return (KErrNone);
       
    54 	case ERespBadRequest:
       
    55 	case ERespUnauthorized:
       
    56 	case ERespForbidden:
       
    57 	case ERespNotAcceptable:
       
    58 	case ERespMethodNotAllowed:
       
    59 		return (KErrArgument);
       
    60 	case ERespNotFound:
       
    61 		return (KErrNotFound);
       
    62 	case ERespTimedOut:
       
    63 		return (KErrTimedOut);
       
    64 	case ERespConflict:
       
    65 		return (KErrInUse);
       
    66 	case ERespNotImplemented:
       
    67 		return (KErrNotSupported);
       
    68 	default:
       
    69 		return (KErrIrObexRespBase - aResp);
       
    70 		}
       
    71 	}
       
    72 
       
    73 /**
       
    74 Returns a TObexTransportInfo created from the received aObexProtocolInfoPtr and aObexProtocolPolicy
       
    75 to be used in the transport layer
       
    76 */
       
    77 TObexTransportInfo* IrOBEXUtil::CreateTransportInfoL(TObexProtocolInfo& aObexProtocolInfoPtr, TObexProtocolPolicy& aObexProtocolPolicy)
       
    78 	{
       
    79 	TObexTransportInfo* transportInfo = NULL;
       
    80 
       
    81 	if ( aObexProtocolInfoPtr.iTransport == KObexIrTTPProtocol )
       
    82 		{
       
    83 		transportInfo = new(ELeave) TObexIrTransportInfo;
       
    84 		TObexIrProtocolInfo& irProtocolInfo = static_cast<TObexIrProtocolInfo&>(aObexProtocolInfoPtr);
       
    85 		static_cast<TObexIrTransportInfo*>(transportInfo)->iAddr  = irProtocolInfo.iAddr;
       
    86 		static_cast<TObexIrTransportInfo*>(transportInfo)->iClassName = irProtocolInfo.iClassName;
       
    87 		static_cast<TObexIrTransportInfo*>(transportInfo)->iAttributeName = irProtocolInfo.iAttributeName;		
       
    88 		}
       
    89 	else if ( aObexProtocolInfoPtr.iTransport == KObexIrTTPProtocolV2 )
       
    90 		{
       
    91 		transportInfo = new(ELeave) TObexIrV2TransportInfo;
       
    92 		TObexIrProtocolInfo& irProtocolInfo = static_cast<TObexIrProtocolInfo&>(aObexProtocolInfoPtr);
       
    93 		static_cast<TObexIrV2TransportInfo*>(transportInfo)->iAddr  = irProtocolInfo.iAddr;
       
    94 		static_cast<TObexIrV2TransportInfo*>(transportInfo)->iClassName = irProtocolInfo.iClassName;
       
    95 		static_cast<TObexIrV2TransportInfo*>(transportInfo)->iAttributeName = irProtocolInfo.iAttributeName;
       
    96 		static_cast<TObexIrV2TransportInfo*>(transportInfo)->iDiscoverySlots = irProtocolInfo.iDiscoverySlots;
       
    97 		static_cast<TObexIrV2TransportInfo*>(transportInfo)->iDiscoveryAttempts = irProtocolInfo.iDiscoveryAttempts;
       
    98 		}
       
    99 	else if ( aObexProtocolInfoPtr.iTransport == KObexRfcommProtocol )
       
   100 		{
       
   101 		transportInfo = new(ELeave) TObexBtTransportInfo;
       
   102 		TObexBluetoothProtocolInfo& btProtocolInfo = static_cast<TObexBluetoothProtocolInfo&>(aObexProtocolInfoPtr);
       
   103 		static_cast<TObexBtTransportInfo*>(transportInfo)->iAddr = btProtocolInfo.iAddr;
       
   104 		}
       
   105 #ifdef __WINS__
       
   106 	else if ( aObexProtocolInfoPtr.iTransport == KObexUsbProtocol || aObexProtocolInfoPtr.iTransport == KObexWin32UsbProtocol )
       
   107 #else
       
   108 	else if ( aObexProtocolInfoPtr.iTransport == KObexUsbProtocol)
       
   109 #endif
       
   110 		{
       
   111 		transportInfo = new(ELeave) TObexUsbTransportInfo();
       
   112 		TObexUsbProtocolInfo& usbTransportInfo = static_cast<TObexUsbProtocolInfo&>(aObexProtocolInfoPtr);
       
   113 		static_cast<TObexUsbTransportInfo*>(transportInfo)->iInterfaceStringDescriptor = usbTransportInfo.iInterfaceStringDescriptor;			
       
   114 		}
       
   115 	else if ( aObexProtocolInfoPtr.iTransport == KObexUsbProtocolV2 )
       
   116 		{
       
   117 		transportInfo = new(ELeave) TObexUsbV2TransportInfo();
       
   118 		TObexUsbProtocolInfoV2& usbV2TransportInfo = static_cast<TObexUsbProtocolInfoV2&>(aObexProtocolInfoPtr);
       
   119 		static_cast<TObexUsbV2TransportInfo*>(transportInfo)->iInterfaceStringDescriptor = usbV2TransportInfo.iInterfaceStringDescriptor;	
       
   120 		static_cast<TObexUsbV2TransportInfo*>(transportInfo)->iBandwidthPriority = usbV2TransportInfo.iBandwidthPriority;
       
   121 		static_cast<TObexUsbV2TransportInfo*>(transportInfo)->iDmaOnOutEndpoint = usbV2TransportInfo.iDmaOnOutEndpoint;
       
   122 		static_cast<TObexUsbV2TransportInfo*>(transportInfo)->iDmaOnInEndpoint = usbV2TransportInfo.iDmaOnInEndpoint;	
       
   123 		}
       
   124 		
       
   125 	__ASSERT_DEBUG(transportInfo, Fault(EUtilNoTransportInfo));
       
   126 	transportInfo->iTransportName = aObexProtocolInfoPtr.iTransport;
       
   127 	transportInfo->iReceiveMtu = aObexProtocolPolicy.ReceiveMtu();
       
   128 	transportInfo->iTransmitMtu = aObexProtocolPolicy.TransmitMtu();
       
   129 	transportInfo->iVersion = KObexTransportInfoVersion0;
       
   130 	return transportInfo;
       
   131 	}
       
   132 
       
   133 /**	
       
   134 Converts aErr to an obex response.
       
   135 
       
   136 @return aDefault if aErr == KErrNone, else the appriate obex response
       
   137 */	
       
   138 TObexResponse IrOBEXUtil::ObexResponse (TInt aErr, TObexResponse aDefault)
       
   139 	{ 
       
   140 	switch (aErr)
       
   141 		{
       
   142 		case KErrNone:
       
   143 			return (aDefault);
       
   144 		case KErrNotFound:
       
   145 		case KErrPathNotFound:
       
   146 		case KErrUnknown:
       
   147 		case KErrBadName:
       
   148 			return (ERespNotFound);
       
   149 		case KErrNotSupported:
       
   150 			return (ERespNotImplemented);
       
   151 		case KErrAlreadyExists:
       
   152 		case KErrInUse:
       
   153 			return (ERespConflict);
       
   154 		case KErrAccessDenied:
       
   155 			return (ERespUnauthorized);
       
   156 		case KErrLocked:
       
   157 			return (ERespForbidden);
       
   158 		case KErrTimedOut:
       
   159 			return (ERespTimedOut);
       
   160 		default:
       
   161 			{ 
       
   162 			//check if it's one of the Symbian codes which has been set aside for 
       
   163 			//mapping to an official OBEX code
       
   164 			//unfortunately these are not continuous and exist in 4 discrete blocks
       
   165 			
       
   166 			if (( (aErr <= KErrIrObexRespSuccess ) && (aErr >= KErrIrObexRespPartialContent))
       
   167 				||( (aErr <=KErrIrObexRespMultipleChoices) && (aErr >= KErrIrObexRespUseProxy))
       
   168 				||( (aErr<=KErrIrObexRespBadRequest) && (aErr>=KErrIrObexRespHTTPVerNotSupp) )
       
   169 				||( (aErr <= KErrIrObexRespDatabaseFull) && (aErr >= KErrIrObexRespDatabaseLocked)))
       
   170 				{
       
   171 				
       
   172 				
       
   173 				TInt err = aErr * -1;
       
   174 	
       
   175 				err += KErrIrObexRespBase;
       
   176 
       
   177 				
       
   178 					
       
   179 				return ( static_cast<TObexResponse>(err) );
       
   180 
       
   181 				}
       
   182 			//in all other cases the error code isn't understood
       
   183 			return (ERespInternalError);
       
   184 			}
       
   185 		}
       
   186 	}
       
   187 
       
   188 //
       
   189 // End of file