userlibandfileserver/fileserver/shostmassstorage/server/protocol/tblocktransfer.cpp
changeset 0 a41df078684a
child 4 56f325a607ea
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2008-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 the License "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  @internalTechnology
       
    19 */
       
    20 
       
    21 #include <e32def.h>
       
    22 #include <e32def_private.h>
       
    23 #include <e32std.h>
       
    24 #include <e32std_private.h>
       
    25 #include <e32base.h>
       
    26 #include <e32base_private.h>
       
    27 #include "msctypes.h"
       
    28 
       
    29 #include "mblocktransferprotocol.h"
       
    30 #include "tblocktransfer.h"
       
    31 #include "debug.h"
       
    32 #include "msdebug.h"
       
    33 
       
    34 
       
    35 /**
       
    36 Manage a read from a block device.
       
    37 
       
    38 @param aProtocol A reference to object providing protocol read method
       
    39 @param aPosition The position to start reading from
       
    40 @param aLength The length of data to read
       
    41 @param aBuf Buffer to copy the data to
       
    42 */
       
    43 void TBlockTransfer::ReadL(MBlockTransferProtocol& aProtocol,
       
    44                            TPos aPosition,
       
    45                            TInt aLength,
       
    46                            TDes8& aBuf)
       
    47     {
       
    48     __MSFNLOG
       
    49 	__HOSTPRINT1(_L("blocklen = 0%lx"), iBlockLength);
       
    50 
       
    51 	TInt copylen = 0;
       
    52     TInt headlen = 0;
       
    53 
       
    54 	aBuf.SetLength(0);
       
    55     TPos headOffset = GetHeadBlockOffset(aPosition);
       
    56 	/**** READ10 HEAD ****/
       
    57 	if (headOffset)
       
    58         {
       
    59 		TPos headpos = aPosition - headOffset;
       
    60 		RBuf8  headbuf;
       
    61         CleanupClosePushL(headbuf);
       
    62 		headbuf.Create(iBlockLength);
       
    63 
       
    64 		headlen = ((headOffset + aLength - 1) / iBlockLength) == 0 ? aLength : (iBlockLength - headOffset);
       
    65 
       
    66 		__HOSTPRINT2(_L("\tRead head pos = 0%lx length = 0%lx"),
       
    67                      headpos, iBlockLength);
       
    68 		aProtocol.BlockReadL(headpos, headbuf, iBlockLength);
       
    69 		aBuf.Append(headbuf.Ptr() + headOffset, headlen);
       
    70 		CleanupStack::PopAndDestroy(&headbuf);
       
    71         }
       
    72 
       
    73 	/**** READ10 BODY ****/
       
    74     TInt blocksInMain = (aLength - headlen)/iBlockLength;
       
    75 	if (blocksInMain)
       
    76         {
       
    77 		copylen = blocksInMain * iBlockLength;
       
    78 		__HOSTPRINT2(_L("\tRead main pos = 0%lx length = 0x%x"),
       
    79                      aPosition+headlen, copylen);
       
    80 		aProtocol.BlockReadL(aPosition+headlen, (TDes8 &)aBuf, copylen);
       
    81         }
       
    82 
       
    83 	copylen += headlen;
       
    84 
       
    85 	/**** READ10 TAIL ****/
       
    86     TInt tailLen = aLength - copylen;
       
    87 	if ((tailLen) != 0)
       
    88         {
       
    89 		RBuf8 tailbuf;
       
    90         CleanupClosePushL(tailbuf);
       
    91 		tailbuf.Create(iBlockLength);
       
    92 
       
    93 		__HOSTPRINT2(_L("\tRead tail pos = 0%lx length = 0%lx"),
       
    94                      aPosition+copylen, iBlockLength);
       
    95 		aProtocol.BlockReadL(aPosition+copylen, tailbuf, iBlockLength);
       
    96 		aBuf.Append(tailbuf.Ptr(), tailLen);
       
    97 
       
    98 		CleanupStack::PopAndDestroy(&tailbuf);
       
    99         }
       
   100     }
       
   101 
       
   102 
       
   103 /**
       
   104 Manage a write to a block device
       
   105 
       
   106 @param aProtocol A reference to object providing protocol read method
       
   107 @param aPosition The position to start reading from
       
   108 @param aLength The length of data to read
       
   109 @param aBuf Buffer containing the data to write
       
   110 */
       
   111 void TBlockTransfer::WriteL(MBlockTransferProtocol& aProtocol,
       
   112                             TPos aPosition,
       
   113                             TInt aLength,
       
   114                             TDesC8& aBuf)
       
   115     {
       
   116     __MSFNLOG
       
   117 	TInt copylen = 0;
       
   118     TInt headlen = 0;
       
   119 
       
   120     TPos headOffset = GetHeadBlockOffset(aPosition);
       
   121 	/**** WRITE10 HEAD ****/
       
   122 	if (headOffset)
       
   123         {
       
   124 		TPos headpos = aPosition - headOffset;
       
   125 
       
   126 		RBuf8 headbuf;
       
   127         CleanupClosePushL(headbuf);
       
   128 		headbuf.Create(iBlockLength);
       
   129 
       
   130         RBuf8 buf;
       
   131         CleanupClosePushL(buf);
       
   132 		buf.Create(iBlockLength);
       
   133 
       
   134 		headlen = ((headOffset + aLength - 1) / iBlockLength) == 0 ? aLength : (iBlockLength - headOffset);
       
   135 
       
   136 		__HOSTPRINT2(_L("\tWrite-Read head pos = 0%lx length = 0%lx"),
       
   137                      headpos, iBlockLength);
       
   138 
       
   139 		aProtocol.BlockReadL(headpos, headbuf, iBlockLength);
       
   140 		/* get head */
       
   141 		__HOSTPRINT2(_L("\tcopying read data pos = 0%lx offset = 0%lx"),
       
   142                      headpos, headOffset);
       
   143 		buf.Append(headbuf.Ptr(), headOffset);
       
   144 
       
   145 		/* get body */
       
   146 		buf.Append(aBuf.Ptr(), headlen);
       
   147 
       
   148 		/* Is it a short write and tail exist? */
       
   149         TInt headEndOffset = headOffset + headlen;
       
   150 		if (headEndOffset != iBlockLength)
       
   151             {
       
   152             TInt len = iBlockLength - headEndOffset;
       
   153 
       
   154 			__HOSTPRINT2(_L("\t(short write) copying read data pos = 0%lx length = %08x"),
       
   155                          (headpos + headEndOffset), len);
       
   156 			buf.Append(headbuf.Ptr() + headEndOffset, len);
       
   157             }
       
   158 
       
   159 		__HOSTPRINT2(_L("\tWrite head pos = 0%lx length = %08x"),
       
   160                      headpos, headlen);
       
   161 
       
   162 		aProtocol.BlockWriteL(headpos, (TDes8 &)buf, 0, iBlockLength);
       
   163 
       
   164 		CleanupStack::PopAndDestroy(&buf);
       
   165 		CleanupStack::PopAndDestroy(&headbuf);
       
   166         }
       
   167 
       
   168 	/**** WRITE10 BODY ****/
       
   169     TPos blocksInMain = (aLength - headlen)/iBlockLength;
       
   170 	if (blocksInMain)
       
   171         {
       
   172 		copylen = blocksInMain * iBlockLength;
       
   173 
       
   174         const TUint64 pos = aPosition + headlen;
       
   175 		__HOSTPRINT2(_L("\tWrite main pos = 0%lx length = %08x"),
       
   176                      pos, copylen);
       
   177 
       
   178 		/* form the body */
       
   179 		aProtocol.BlockWriteL(pos, (TDes8 &)aBuf, headlen, copylen);
       
   180         }
       
   181 
       
   182 	copylen += headlen;
       
   183 
       
   184 	/**** WRITE10 TAIL ****/
       
   185     TInt tailLen = aLength - copylen;;
       
   186 	if (tailLen != 0)
       
   187         {
       
   188         RBuf8 buf;
       
   189         CleanupClosePushL(buf);
       
   190         buf.Create(iBlockLength);
       
   191 
       
   192 		RBuf8 tailbuf;
       
   193         CleanupClosePushL(tailbuf);
       
   194 		tailbuf.Create(iBlockLength);
       
   195 
       
   196         const TUint64 pos = aPosition + copylen;
       
   197 
       
   198 		__HOSTPRINT2(_L("\tWrite-Read tail pos = 0%lx length = %08x"),
       
   199                      pos, iBlockLength);
       
   200 
       
   201 		aProtocol.BlockReadL(pos, tailbuf, iBlockLength);
       
   202 		/* get head */
       
   203 		buf.Append(aBuf.Ptr() + copylen, tailLen);
       
   204 		/* get tail */
       
   205 		buf.Append(tailbuf.Ptr() + tailLen, iBlockLength - tailLen);
       
   206 
       
   207 		aProtocol.BlockWriteL(pos, (TDes8 &)buf, 0, iBlockLength);
       
   208 
       
   209 		CleanupStack::PopAndDestroy(&tailbuf);
       
   210 		CleanupStack::PopAndDestroy(&buf);
       
   211         }
       
   212     }
       
   213 
       
   214 
       
   215 void TBlockTransfer::SetCapacity(TUint32 aBlockLength, TLba aLastLba)
       
   216     {
       
   217     __MSFNLOG
       
   218     iBlockLength = static_cast<TInt64>(aBlockLength);
       
   219     iLastLba = aLastLba;
       
   220     }
       
   221 
       
   222 
       
   223 TPos TBlockTransfer::GetHeadBlockOffset(TPos aPos)
       
   224     {
       
   225     __MSFNLOG
       
   226 	return (aPos % iBlockLength);
       
   227     }
       
   228 
       
   229 TLba TBlockTransfer::GetHeadBlockLbaL(TPos aPos)
       
   230     {
       
   231     __MSFNLOG
       
   232     TLba lba = I64LOW(aPos / iBlockLength);
       
   233     if (lba > iLastLba)
       
   234         User::Leave(KErrArgument);
       
   235     return lba;
       
   236     }
       
   237 
       
   238 TPos TBlockTransfer::GetTailBlockOffset(TPos aPos, TInt aLen)
       
   239     {
       
   240     __MSFNLOG
       
   241     return ((aPos + aLen) % iBlockLength);
       
   242     }
       
   243 
       
   244 TLba TBlockTransfer::GetTailBlockLbaL(TPos aPos, TInt aLen)
       
   245     {
       
   246     __MSFNLOG
       
   247     TLba lba = I64LOW((aPos + aLen) / iBlockLength);
       
   248     if (lba > iLastLba)
       
   249         User::Leave(KErrArgument);
       
   250     return lba;
       
   251     }
       
   252 
       
   253 
       
   254