symbian-qemu-0.9.1-12/model-libraries/nvmemmory/nvmemmory.cpp
branchnvmemory
changeset 74 eb3d0111f868
equal deleted inserted replaced
72:b6aa150091ee 74:eb3d0111f868
       
     1 /*
       
     2 * Copyright (c) 2010 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 *
       
    16 * Simple non volatile memory device. nvmemmory.cpp
       
    17 *
       
    18 */
       
    19 
       
    20 extern "C" 
       
    21     {
       
    22 #include <stdio.h>
       
    23     }
       
    24 #include "nvmemmory.h"
       
    25 
       
    26 SyborgNVMemory::SyborgNVMemory( uint32_t a_sectorsize ):
       
    27     iNVMemSectorSizeInBytes( a_sectorsize )
       
    28     {
       
    29     for( int32_t index = 0; index < NVMEM_MAX_STREAMHANDLES; index += 1 ) 
       
    30         {
       
    31         i_filestream[index] = NULL;
       
    32         }
       
    33     }
       
    34 
       
    35 SyborgNVMemory::~SyborgNVMemory()
       
    36     {
       
    37     NVMemReset();
       
    38     }
       
    39 
       
    40 int32_t SyborgNVMemory::NVMemReset( )
       
    41     {
       
    42     /*Initialize the file array*/
       
    43     for( int32_t index = 0; index < NVMEM_MAX_STREAMHANDLES; index += 1 ) 
       
    44         {
       
    45         if( i_filestream[index] != NULL )
       
    46             {
       
    47             fclose( i_filestream[index] );
       
    48             i_filestream[index] = NULL;
       
    49             }
       
    50         }
       
    51     return 0;
       
    52     }
       
    53 
       
    54 int32_t SyborgNVMemory::NVMemCreateImage( char* a_memoryarrayname, uint32_t a_sectorcount, uint32_t a_sectorsize )
       
    55     {
       
    56     /* Create a temporary MByte buffer array for image creation purpose */
       
    57     const uint32_t tempbufsize = 1024 * 1024;
       
    58     uint8_t tempbuf[ tempbufsize ];
       
    59     uint32_t index = 0;
       
    60     FILE *filestream = NULL;
       
    61     char mode1[4] = {"rb"};
       
    62     char mode2[4] = {"wb"};
       
    63     int32_t ret = NVMEM_ERROR_CREATE;
       
    64     uint32_t temparraysectorcount = tempbufsize / a_sectorsize;
       
    65     iNVMemSectorSizeInBytes = a_sectorsize;
       
    66             
       
    67     /* Try to open the specified file. If it exists we do not create a new one */
       
    68     filestream = fopen( a_memoryarrayname, &mode1[0] );
       
    69     if( filestream == NULL )
       
    70         {
       
    71         /* Fill MBR with zeroes */
       
    72         for( index = 0; index < iNVMemSectorSizeInBytes; index += 1 )
       
    73             {
       
    74             tempbuf[index] = 0;
       
    75             }
       
    76         /* Open a temporary file handle. Create the file*/
       
    77         filestream = fopen( a_memoryarrayname, &mode2[0] );
       
    78 
       
    79         if( ret != NULL )
       
    80             {
       
    81             ret = NVMEM_OK;
       
    82             for( index = 0; (index < a_sectorcount) && (ret == NVMEM_OK); index += temparraysectorcount )
       
    83                 {
       
    84                 /* Print one array of zeroes to our temporary buffer */
       
    85                 if( fwrite( tempbuf, iNVMemSectorSizeInBytes, temparraysectorcount, filestream ) < 0 )
       
    86                     {
       
    87                     ret = NVMEM_ERROR_FWRITE;
       
    88                     }
       
    89                 }
       
    90             fclose( filestream );
       
    91             }
       
    92         else
       
    93             {
       
    94             ret = NVMEM_ERROR_FOPEN;
       
    95             }
       
    96         }
       
    97     return ret;
       
    98     }
       
    99 
       
   100 int32_t SyborgNVMemory::NVMemOpen( char* a_memoryarrayname )
       
   101     {
       
   102     char mode[4] = {"rb+"};
       
   103     int32_t handle = NVMEM_ERROR_OUT_OF_FREE_STREAMHANDLES;
       
   104     /* Search for a free handle position and assign if found */
       
   105     int32_t index = 0;
       
   106     for( ; index < NVMEM_MAX_STREAMHANDLES; index += 1 ) 
       
   107         {
       
   108         if( i_filestream[index] == NULL )
       
   109             {
       
   110             i_filestream[index] = fopen( a_memoryarrayname, &mode[0] );
       
   111             if( i_filestream[index] != NULL )
       
   112                 {
       
   113                 handle = index;
       
   114                 printf("handle created: %d\n", index);
       
   115                 }
       
   116             else
       
   117                 {
       
   118                 handle = NVMEM_ERROR_FOPEN;
       
   119                 }
       
   120             break;
       
   121             }
       
   122         }
       
   123     return handle;
       
   124     }
       
   125 
       
   126 int32_t SyborgNVMemory::NVMemClose( int32_t a_memoryarrayhandle )
       
   127     {
       
   128     int32_t result = NVMEM_ERROR_FCLOSE;
       
   129     if( fclose( i_filestream[a_memoryarrayhandle] ) == 0 )
       
   130         {
       
   131         result = NVMEM_OK;
       
   132         }
       
   133     return result;
       
   134     }
       
   135 
       
   136 int32_t SyborgNVMemory::NVMemFlush( int32_t a_memoryarrayhandle )
       
   137     {
       
   138     int32_t result = NVMEM_ERROR_FFLUSH;
       
   139     if( fflush( i_filestream[a_memoryarrayhandle] ) == 0 )
       
   140         {
       
   141         result = NVMEM_OK;
       
   142         }
       
   143     return result;
       
   144     }
       
   145 
       
   146 void SyborgNVMemory::NVMemRead( uint32_t *a_client_targetmemoryaddr, int32_t a_memoryarrayhandle, uint32_t a_memoryarraysectoroffset, uint32_t a_sectorcount )
       
   147     {
       
   148     //printf("SyborgNVMemory::NVMemRead: sectorpos:%d sectorcount:%d hostaddr: 0x%08x handle: %d\n", a_memoryarraysectoroffset, a_sectorcount, a_client_targetmemoryaddr, a_memoryarrayhandle );
       
   149     uint32_t items_read = 0;
       
   150     long streamoffset = a_memoryarraysectoroffset * iNVMemSectorSizeInBytes;
       
   151     int32_t result = fseek( i_filestream[a_memoryarrayhandle], streamoffset, SEEK_SET );
       
   152     if( result == 0 )   
       
   153         {
       
   154         items_read = fread( a_client_targetmemoryaddr, iNVMemSectorSizeInBytes, a_sectorcount, i_filestream[a_memoryarrayhandle] );
       
   155         /*Check that everything is read*/
       
   156         if( items_read != a_sectorcount )
       
   157             {
       
   158             result = NVMEM_ERROR_FREAD;
       
   159             }
       
   160         else
       
   161             {
       
   162             result = items_read;
       
   163             }
       
   164         }
       
   165     else /*error*/
       
   166         {
       
   167         result = NVMEM_ERROR_FSEEK;
       
   168         }
       
   169     i_NVMemCallBack( result );
       
   170     }
       
   171 
       
   172 void SyborgNVMemory::NVMemWrite( uint32_t *a_client_sourcememoryaddr, int32_t a_memoryarrayhandle, uint32_t a_memoryarraysectoroffset, uint32_t a_sectorcount )
       
   173     {
       
   174     //printf("SyborgNVMemory::NVMemWrite: sectorpos:%d sectorcount:%d hostaddr: 0x%08x\n", a_memoryarraysectoroffset, a_sectorcount, a_client_sourcememoryaddr );
       
   175     uint32_t items_written = 0;
       
   176     long streamoffset = a_memoryarraysectoroffset * iNVMemSectorSizeInBytes;
       
   177     int32_t result = fseek( i_filestream[a_memoryarrayhandle], streamoffset, SEEK_SET );
       
   178     if( result == 0 )   
       
   179         {
       
   180         items_written = fwrite( a_client_sourcememoryaddr, iNVMemSectorSizeInBytes, a_sectorcount, i_filestream[a_memoryarrayhandle] );
       
   181         /*Check that everything is written*/
       
   182         if( items_written != a_sectorcount )
       
   183             {
       
   184             result = NVMEM_ERROR_FWRITE;
       
   185             }
       
   186         else
       
   187             {
       
   188             result = items_written;
       
   189             }
       
   190         }
       
   191     else /*error*/
       
   192         {
       
   193         result = NVMEM_ERROR_FSEEK;
       
   194         }
       
   195     i_NVMemCallBack( result );
       
   196     }
       
   197 
       
   198 uint32_t SyborgNVMemory::NVMemGetSectorCount( int32_t a_memoryarrayhandle )
       
   199     {
       
   200     long byte_size = 0;
       
   201     uint32_t sector_count = 0;
       
   202     printf("use handle: %d\n", a_memoryarrayhandle);
       
   203     fseek( i_filestream[a_memoryarrayhandle], 0, SEEK_END );
       
   204     byte_size = ftell( i_filestream[a_memoryarrayhandle] );
       
   205     sector_count = byte_size / iNVMemSectorSizeInBytes;
       
   206     return sector_count;
       
   207     }
       
   208 
       
   209 int32_t  SyborgNVMemory::NVMemSetCallback( int (*aNVMemCallBack) (int) )
       
   210     {
       
   211     i_NVMemCallBack = aNVMemCallBack;
       
   212     return 0;
       
   213     }
       
   214 
       
   215 
       
   216 extern "C"
       
   217     {
       
   218     NVMEMORY_API SyborgNVMemory * nvmem_create( uint32_t a_sectorsize )
       
   219         {
       
   220         return new SyborgNVMemory( a_sectorsize );
       
   221         }
       
   222 
       
   223     NVMEMORY_API int32_t nvmem_reset( SyborgNVMemory* a_syborg_nvmemory ) 
       
   224         {
       
   225         return a_syborg_nvmemory->NVMemReset();
       
   226         }
       
   227 
       
   228     NVMEMORY_API int32_t nvmem_create_image( SyborgNVMemory* a_syborg_nvmemory, char* a_memoryarrayname, uint32_t a_sectorcount, uint32_t a_sectorsize )
       
   229         {
       
   230         return a_syborg_nvmemory->NVMemCreateImage( a_memoryarrayname, a_sectorcount, a_sectorsize );
       
   231         }
       
   232 
       
   233     NVMEMORY_API int32_t nvmem_open( SyborgNVMemory* a_syborg_nvmemory, char* a_memoryarrayname ) 
       
   234         {
       
   235         return a_syborg_nvmemory->NVMemOpen( a_memoryarrayname );
       
   236         }
       
   237 
       
   238     NVMEMORY_API int32_t nvmem_close( SyborgNVMemory* a_syborg_nvmemory, int32_t a_memoryarrayhandle ) 
       
   239         {
       
   240         return a_syborg_nvmemory->NVMemClose( a_memoryarrayhandle );
       
   241         }
       
   242 
       
   243     NVMEMORY_API int32_t nvmem_flush( SyborgNVMemory* a_syborg_nvmemory, int32_t a_memoryarrayhandle ) 
       
   244         {
       
   245         return a_syborg_nvmemory->NVMemFlush( a_memoryarrayhandle );
       
   246         }
       
   247 
       
   248     NVMEMORY_API void nvmem_read( SyborgNVMemory* a_syborg_nvmemory, uint32_t *a_client_targetmemoryaddr, int32_t a_memoryarrayhandle, uint32_t a_memoryarraysectoroffset, uint32_t a_sectorcount ) 
       
   249         {
       
   250         a_syborg_nvmemory->NVMemRead( a_client_targetmemoryaddr, a_memoryarrayhandle, a_memoryarraysectoroffset, a_sectorcount );
       
   251         }
       
   252 
       
   253     NVMEMORY_API void nvmem_write( SyborgNVMemory* a_syborg_nvmemory, uint32_t *a_client_sourcememoryaddr, int32_t a_memoryarrayhandle, uint32_t a_memoryarraysectoroffset, uint32_t a_sectorcount ) 
       
   254         {
       
   255         a_syborg_nvmemory->NVMemWrite( a_client_sourcememoryaddr, a_memoryarrayhandle, a_memoryarraysectoroffset, a_sectorcount );
       
   256         }
       
   257 
       
   258     NVMEMORY_API uint32_t nvmem_get_sector_count( SyborgNVMemory* a_syborg_nvmemory, int32_t a_memoryarrayhandle )
       
   259         {
       
   260         return a_syborg_nvmemory->NVMemGetSectorCount( a_memoryarrayhandle );
       
   261         }
       
   262     
       
   263     NVMEMORY_API int32_t nvmem_set_callback( SyborgNVMemory* a_syborg_nvmemory, int (*aGraphicsCallBack) (int) )
       
   264         {
       
   265         return a_syborg_nvmemory->NVMemSetCallback( aGraphicsCallBack );
       
   266         }
       
   267     }