adaptationlayer/tsy/simatktsy_dll/internal/test/simatktsy_testtool/simatk/src/satfile.cpp
changeset 4 510c70acdbf6
parent 3 1972d8c2e329
child 5 8ccc39f9d787
equal deleted inserted replaced
3:1972d8c2e329 4:510c70acdbf6
     1 /*
       
     2 * Copyright (c) 2002-2006 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 the License "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:   Provides functionality to open SIM data file and
       
    15 *                to make searches and comperations to its content
       
    16 *                Each line in SIM data file represent a separate elementary file 
       
    17 *                or file's record (referenced as RefEfs).
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 
       
    23 // INCLUDE FILES
       
    24 #include "satfile.h"
       
    25 
       
    26 
       
    27 // CONSTANTS
       
    28 
       
    29 // Name and location of SIM data file
       
    30 _LIT( KSimDataFile, "\\TestFramework\\SimAtkSimData.ini" );
       
    31 
       
    32 // Special characters found from data file
       
    33 const TUint8 KCharEndOfFile         = 0;
       
    34 const TUint8 KCharEndOfLine         = '\n';
       
    35 const TUint8 KCharCarriageReturn    = '\r';
       
    36 const TUint8 KCharTabulator         = '\t';
       
    37 const TUint8 KCharSpace             = ' ';
       
    38 const TUint8 KCharComment           = '#';
       
    39 
       
    40 // State machine for locating data from SIM data file
       
    41 enum TSearchState
       
    42     {
       
    43     EType,      // Type of SIM (SATTS/GCF)
       
    44     EFile,      // File Id ( for example 4F21)
       
    45     ERecord     // Record Id. Some files may contain several records.
       
    46     };
       
    47     
       
    48 
       
    49 // DATA TYPES
       
    50 typedef TBuf8<1> TLetter;
       
    51 
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CSatFile::CSatFile
       
    55 // C++ default constructor can NOT contain any code, that
       
    56 // might leave.
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 CSatFile::CSatFile() : iSimFilePos( KErrNotFound )
       
    60     {
       
    61     }
       
    62 
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CSatFile::~CSatFile
       
    66 // Destructor
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CSatFile::~CSatFile()
       
    70     {
       
    71     if ( KErrNotFound != iSimFilePos )
       
    72         {
       
    73         iFile.Close();
       
    74         iFileServer.Close();
       
    75         }
       
    76     }
       
    77 
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CSatFile::OpenFile
       
    81 // Opens SIM data file
       
    82 // (other items were commented in a header). 
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 TInt CSatFile::OpenFile()
       
    86     {
       
    87     TInt ret( KErrNotFound );
       
    88 
       
    89     // Connect to file server and open file
       
    90     if ( KErrNone == iFileServer.Connect() )
       
    91         {
       
    92         if ( KErrNone == iFile.Open(
       
    93              iFileServer, KSimDataFile, EFileRead ) )
       
    94             {
       
    95             iSimFilePos = 0;
       
    96             ret = KErrNone;
       
    97             }
       
    98         else
       
    99             {
       
   100             // Opening of file failed.
       
   101             iFileServer.Close();
       
   102             }
       
   103         }
       
   104     
       
   105     return ret;   
       
   106     }
       
   107 
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CSatFile::FindFile
       
   111 // Search specified REfEf from from the SIM data file
       
   112 // (other items were commented in a header). 
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 TInt CSatFile::FindFile
       
   116         (
       
   117         const TPtrC aSimType,
       
   118         const TInt aFileId,
       
   119         const TInt aRecord
       
   120         )
       
   121     {
       
   122     TInt ret( KErrNone );
       
   123     
       
   124     // Check the file is opened successfully
       
   125     if ( KErrNotFound != iSimFilePos )
       
   126         {
       
   127         // Data read from file is 8-bit. Convert paramerters to string 
       
   128         // presentation for comparation purposes.
       
   129         TBuf8<3> type;
       
   130         TBuf8<4> fileId;
       
   131         TBuf8<2> record;
       
   132         
       
   133         type.Append( aSimType.Left( 
       
   134             Min( aSimType.Length(), type.MaxLength() ) ) );
       
   135         fileId.Num( aFileId, EHex );
       
   136         
       
   137         // Not all elementary files have records
       
   138         if ( aRecord)
       
   139             {
       
   140             record.Num( aRecord );
       
   141             }
       
   142         
       
   143         // Rewind to the beginning of SIM data file
       
   144         iSimFilePos = 0;
       
   145         iFile.Seek( ESeekStart, iSimFilePos );
       
   146         
       
   147         // Begin search by looking for line which starts with correct
       
   148         // SIM type keyword (SATTS/GCF)
       
   149         TSearchState lookingFor( EType );
       
   150         
       
   151         TBuf8<10> keyWord;        
       
   152         do
       
   153             {
       
   154             // Get next token from file
       
   155             if ( KErrNone == ( ret = NextToken( keyWord ) ) )
       
   156                 {
       
   157                 // Search state machine. Next action depens on what
       
   158                 // we're looking for now.
       
   159                 switch( lookingFor )
       
   160                     {
       
   161                     case EType: // Looking for line with correct SIM type
       
   162                         {
       
   163                         if ( KErrNone == type.CompareC( keyWord ) )
       
   164                             {
       
   165                             // Type matches, check file id next
       
   166                             lookingFor = EFile;
       
   167                             }
       
   168                         else
       
   169                             {
       
   170                             // Wrong type. Continue looking from next line.
       
   171                             lookingFor = EType;
       
   172                             ret = NextLine();
       
   173                             }
       
   174                         break;
       
   175                         }
       
   176                     case EFile: // Looking for line with correct file Id
       
   177                         {
       
   178                         if ( KErrNone == fileId.CompareC( keyWord ) )
       
   179                             {
       
   180                             // Correct file. If record was provided
       
   181                             // continue to look correct record id. 
       
   182                             // Otherwise correct line was found.  
       
   183                             if ( !record.Length() )
       
   184                                 {
       
   185                                 // Line found, store lines starting position
       
   186                                 iFile.Seek( ESeekCurrent, iSimFilePos );
       
   187                                 }
       
   188                             else
       
   189                                 {
       
   190                                 lookingFor = ERecord;
       
   191                                 }
       
   192                             }
       
   193                         else
       
   194                             {
       
   195                             // Wrong file. Continue looking from next line.
       
   196                             lookingFor = EType;
       
   197                             ret = NextLine();
       
   198                             }
       
   199                         break;
       
   200                         }
       
   201                     case ERecord: // Looking for line with correct record id
       
   202                         {
       
   203                         if ( KErrNone == record.CompareC( keyWord ) )
       
   204                             {
       
   205                             // Line found, store lines starting position
       
   206                             iFile.Seek( ESeekCurrent, iSimFilePos );
       
   207                             }
       
   208                         else
       
   209                             {
       
   210                             // Wrong record. Continue looking from next line.
       
   211                             lookingFor = EType;
       
   212                             ret = NextLine();
       
   213                             }
       
   214                             
       
   215                         break;
       
   216                         }
       
   217                     default:
       
   218                         {
       
   219                         // This should be impossible to reach
       
   220                         lookingFor = EType;
       
   221                         break;
       
   222                         }
       
   223                     }
       
   224                 }
       
   225             
       
   226             // Continue search until the line containing RefEf is found
       
   227             // or parsing of file fails (=end of file is reached)
       
   228             } while ( KErrNone == ret && 0 == iSimFilePos );
       
   229         }
       
   230     
       
   231     return ret;
       
   232     }
       
   233     
       
   234     
       
   235 // -----------------------------------------------------------------------------
       
   236 // CSatFile::MatchData
       
   237 // Compares provided data to the RefEf
       
   238 // (other items were commented in a header). 
       
   239 // -----------------------------------------------------------------------------
       
   240 //
       
   241 TInt CSatFile::MatchData
       
   242         (
       
   243         const TDesC8& aData,
       
   244         const TUint aOffset
       
   245         )
       
   246     {
       
   247     // Skip to offset
       
   248     TInt ret( Goto( aOffset ) );
       
   249     
       
   250     if ( KErrNone == ret )
       
   251         {
       
   252         TUint value;
       
   253         TUint dataLength( aData.Length() );
       
   254 
       
   255         // Compare data byte by byte to the RefEf
       
   256         for ( TUint i ( 0 ) ; i < dataLength && KErrNone == ret ; i++ )
       
   257             {
       
   258             if ( KErrNone == ( ret = NextValue( value ) ) )
       
   259                 {
       
   260                 if ( aData[i] != value )
       
   261                     {
       
   262                     // Data mismatch
       
   263                     ret = KErrCorrupt;
       
   264                     }
       
   265                 }
       
   266             else
       
   267                 {
       
   268                 // Getting next byte from RefEf failed.
       
   269                 ret = KErrOverflow;
       
   270                 }
       
   271             }
       
   272         }
       
   273    
       
   274     return ret;
       
   275     }
       
   276     
       
   277     
       
   278 // -----------------------------------------------------------------------------
       
   279 // CSatFile::FetchData
       
   280 // Read data from RefEf
       
   281 // (other items were commented in a header). 
       
   282 // -----------------------------------------------------------------------------
       
   283 //
       
   284 TInt CSatFile::FetchData
       
   285         (
       
   286         TDes8& aData,
       
   287         const TUint aOffset
       
   288         )
       
   289     {
       
   290     // Leap to offset
       
   291     TInt ret( Goto( aOffset ) );
       
   292     
       
   293     TUint dataMax( aData.MaxLength() );
       
   294     
       
   295     aData.Zero();
       
   296     
       
   297     if ( KErrNone == ret )
       
   298         {
       
   299         TUint value( 0 );
       
   300         do
       
   301             {
       
   302             // Append values to the output until max capacity is reached
       
   303             // or parsing of next value fails.
       
   304             if ( KErrNone ==  ( ret = NextValue( value ) ) )
       
   305                 {
       
   306                 aData.Append( value );
       
   307                 }
       
   308             } while ( KErrNone == ret && aData.Length() < dataMax );
       
   309         }
       
   310     
       
   311     return ret;
       
   312     }
       
   313 
       
   314 // -----------------------------------------------------------------------------
       
   315 // CSatFile::NextChar
       
   316 // Returns next character from a data file
       
   317 // (other items were commented in a header). 
       
   318 // -----------------------------------------------------------------------------
       
   319 //
       
   320 TInt CSatFile::NextChar
       
   321         (
       
   322         TChar& aChar
       
   323         )
       
   324     {
       
   325     TInt ret( KErrNone );
       
   326     
       
   327     aChar = KCharEndOfFile;
       
   328     
       
   329     // Data from file is read to a buffer
       
   330     TLetter letter;
       
   331     
       
   332     do
       
   333         {
       
   334         iFile.Read( letter );
       
   335         
       
   336         if ( letter.Length() )
       
   337             {
       
   338             // Letter was read successfully. Convert to char
       
   339             aChar = letter[0];
       
   340             
       
   341             // Prohibit stepping beyond end of line
       
   342             if ( KCharEndOfLine == aChar )
       
   343                 {
       
   344                 // Line end. Step back
       
   345                 TInt stepBack( -1 );
       
   346                 iFile.Seek( ESeekCurrent, stepBack );
       
   347                 
       
   348                 ret = KErrNotFound;
       
   349                 }
       
   350             }
       
   351         else
       
   352             {
       
   353             // End of File (length of read letter is zero)
       
   354             ret = KErrNotFound;
       
   355             }
       
   356         
       
   357         // Skip carriage return (\r) in end of line sequece (\r\n)
       
   358         } while ( KCharCarriageReturn == aChar && KErrNone == ret );
       
   359     
       
   360     return ret;   
       
   361     }
       
   362     
       
   363     
       
   364 // -----------------------------------------------------------------------------
       
   365 // CSatFile::NextLine
       
   366 // Skips the current line 
       
   367 // (other items were commented in a header). 
       
   368 // -----------------------------------------------------------------------------
       
   369 //
       
   370 TInt CSatFile::NextLine()
       
   371     {
       
   372     TInt ret( KErrNone );
       
   373 
       
   374     TChar ch( KCharEndOfLine );
       
   375     
       
   376     // Read characters until end of line is reached
       
   377     do
       
   378         {
       
   379         ret = NextChar( ch );
       
   380         } while ( KErrNone == ret );
       
   381         
       
   382     // Read one letter, that should should be (\n).
       
   383     TLetter letter;
       
   384     iFile.Read( letter );
       
   385 
       
   386     if ( !letter.Length() )
       
   387         {
       
   388         // This is the end of File
       
   389         ret = KErrNotFound;
       
   390         }
       
   391     else
       
   392         {
       
   393         // Reading position is in the beginning of next line
       
   394         ret = KErrNone;
       
   395         }
       
   396 
       
   397     return ret;
       
   398     }
       
   399     
       
   400     
       
   401 // -----------------------------------------------------------------------------
       
   402 // CSatFile::NextToken
       
   403 // Read next token (word or number) from a file
       
   404 // (other items were commented in a header). 
       
   405 // -----------------------------------------------------------------------------
       
   406 //
       
   407 TInt CSatFile::NextToken
       
   408         (
       
   409         TDes8& aToken
       
   410         )
       
   411     {
       
   412     TInt ret( KErrNone );
       
   413 
       
   414     TBool done( EFalse );
       
   415 
       
   416     TChar ch( KCharEndOfLine );
       
   417 
       
   418     TUint tokenMax( aToken.MaxLength() );
       
   419     
       
   420     aToken.Zero();
       
   421 
       
   422     do
       
   423         {
       
   424         // Read next char
       
   425         ret = NextChar( ch );
       
   426         
       
   427         // Next action depends on the type of character
       
   428         switch( ch )
       
   429             {
       
   430             case KCharComment: // Comment charater received
       
   431                 {                
       
   432                 aToken.Zero();
       
   433                 // Decision: No comment char allowed at all and
       
   434                 // whole line is skipped
       
   435                 if ( KErrNone != NextLine() )
       
   436                     {
       
   437                     // cound not find next line (end of file)
       
   438                     ret = KErrNotFound;
       
   439                     done = ETrue;
       
   440                     }
       
   441                 break;
       
   442                 }
       
   443             case KCharSpace: // These characters can end the reading of tokens
       
   444             case KCharTabulator:
       
   445             case KCharEndOfLine:
       
   446             case KCharEndOfFile:
       
   447                 {
       
   448                 if ( aToken.Length() )
       
   449                     {
       
   450                     // Some characters have been succesfully read to output.
       
   451                     // Token was read successfully.
       
   452                     ret = KErrNone;
       
   453                     done = ETrue;
       
   454                     }
       
   455                 else if ( KCharEndOfLine == ch )
       
   456                     {
       
   457                     // Output is still empty and end of line has been reached.
       
   458                     // Skip line
       
   459                     if ( KErrNone != NextLine() )
       
   460                         {
       
   461                         // No next line, end of file reached
       
   462                         ret = KErrNotFound;
       
   463                         done = ETrue;
       
   464                         }                    
       
   465                     }
       
   466                 else if ( KCharEndOfFile == ch )
       
   467                     {
       
   468                     // End of file was reached.
       
   469                     ret = KErrNotFound;
       
   470                     done = ETrue;
       
   471                     }
       
   472                 break;
       
   473                 }
       
   474             default:
       
   475                 {
       
   476                 // Not a special character. Append to output if valid 
       
   477                 // alphabet or digit.
       
   478                 if ( ch.IsAlphaDigit() )
       
   479                     {
       
   480                     if ( aToken.Length() < tokenMax )
       
   481                         {
       
   482                         aToken.Append( ch );
       
   483                         }
       
   484                     else
       
   485                         {
       
   486                         // Too long token parsed.
       
   487                         ret = KErrOverflow;
       
   488                         done = ETrue;
       
   489                         }
       
   490                     }
       
   491                 else
       
   492                     {
       
   493                     // Chars that aren't either digits or alphabets are ignored
       
   494                     }
       
   495                 break;
       
   496                 }
       
   497             }
       
   498         
       
   499         } while ( !done );
       
   500     
       
   501     return ret;
       
   502     }
       
   503     
       
   504     
       
   505 // -----------------------------------------------------------------------------
       
   506 // CSatFile::NextValue
       
   507 // Parses next hex integer value from RefEf
       
   508 // (other items were commented in a header). 
       
   509 // -----------------------------------------------------------------------------
       
   510 //
       
   511 TInt CSatFile::NextValue
       
   512         (
       
   513         TUint& aValue
       
   514         )
       
   515     {
       
   516     TInt ret( KErrNone );
       
   517     
       
   518     TBuf8<2> valueString; // 8-bit max
       
   519     
       
   520     // Read value and convert to integer. Make also sure that
       
   521     // RefEf has been located beforehand
       
   522     if ( KErrNone == NextToken( valueString ) && 0 < iSimFilePos  )
       
   523         {
       
   524         TLex8 lex( valueString );
       
   525         
       
   526         if ( KErrNone != lex.Val( aValue, EHex ) )
       
   527             {
       
   528             ret = KErrNotFound;
       
   529             }
       
   530         }
       
   531     else
       
   532         {
       
   533         ret = KErrNotFound;
       
   534         }
       
   535 
       
   536     return ret;
       
   537     }
       
   538     
       
   539     
       
   540 // -----------------------------------------------------------------------------
       
   541 // CSatFile::Goto
       
   542 // Forward to offset in the RefEf
       
   543 // (other items were commented in a header). 
       
   544 // -----------------------------------------------------------------------------
       
   545 //
       
   546 TInt CSatFile::Goto
       
   547         (
       
   548         const TUint aOffset
       
   549         )
       
   550     {
       
   551     TInt ret( KErrNone );
       
   552     
       
   553     // Make sure that RefEf has been seached beforehand
       
   554     if ( 0 < iSimFilePos )
       
   555         {
       
   556         // First go to the start of RefEf
       
   557         
       
   558         // Get current position
       
   559         TInt posNow( 0 );
       
   560         iFile.Seek( ESeekCurrent, posNow );
       
   561         
       
   562         // Rewind to RefEf start
       
   563         if ( posNow != iSimFilePos )
       
   564             {
       
   565             TInt posDelta( iSimFilePos - posNow );
       
   566             iFile.Seek( ESeekCurrent, posDelta );
       
   567             }
       
   568         
       
   569         // Skip offsets if provided
       
   570         if ( aOffset )
       
   571             {
       
   572             TUint i( aOffset );
       
   573             TUint value( 0 ); // Not needed
       
   574             
       
   575             while ( i && KErrNone == ret )
       
   576                 {
       
   577                 ret = NextValue( value );
       
   578                 i--;
       
   579                 }
       
   580             }
       
   581         }
       
   582     else
       
   583         {
       
   584         // File position not found
       
   585         ret = KErrNotReady;
       
   586         }
       
   587     
       
   588     return ret;    
       
   589     }
       
   590 
       
   591 
       
   592 // End of File