//
//  Copyright © 2003, RESON Inc. All Rights Reserved.
//
//  No part of this file may be reproduced or transmitted in any form or by
//  any means, electronic or mechanical, including photocopy, recording, or
//  information storage or retrieval system, without permission in writing
//  from RESON Inc.
//
//  Filename:   MMF6046.h
//
//  Project:    6046
//
//  Author(s):  W. Arcus
//
//  Purpose:    
//
//  Notes:      
//

#if !defined(AFX_MMF6046_H__4172B1AD_F536_4BA9_BB95_8658CB345C02__INCLUDED_)
#define AFX_MMF6046_H__4172B1AD_F536_4BA9_BB95_8658CB345C02__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "..\..\Components\NetUtils\MemMap.h"

#include "CircularBuffer.h"

///////////////////////////////////////////////////////////////////////////////
// Module constants and definitions.

namespace NMMF6046                                                                                                          // Begin namespace NMMF6046.
{
    const unsigned long     ulVersion_c                 =   1UL;                                                            // Version of the MMF structure definition.
    const unsigned long     ulSize_c                    =   32UL  * 1024UL * 1024UL;                                        // Total MMF size, 32MBytes for now.
    const unsigned long     ulNumberOfCells_c           =   255UL;                                                          // Number of data cells in MMF.
    const unsigned long     ulCellSizeInBytes_c         =   128UL * 1024UL;                                                 // Size of each data cell in bytes.
    const unsigned long     ulCellIndexSize_c           =   32UL;                                                           // Cell index entry size; should == sizeof(CMMF6046::CELLINDEX).
    const unsigned long     ulPrefixSize_c              =   20UL;                                                           // MMF header prefix size.

    const unsigned long     ulReservedSpaceInBytes_c    =   ulSize_c - ( ulPrefixSize_c +                                   // Derived reserved space to maintain overall MMF size.
                                                                         ulNumberOfCells_c * ulCellIndexSize_c +
                                                                         ulNumberOfCells_c * ulCellSizeInBytes_c );

}                                                                                                                           // End namespace NMMF6046.

#pragma pack ( push, MMF6046_H_PACK, 1 )                                                                                    // Single byte alignment.

typedef struct tagRECORDHEADER
{
    unsigned long           m_ulRecordCounter;
    unsigned long           m_ulMillisecondTimestamp;
    int                     m_iSensorIndex;
    unsigned long           m_ulRecordSizeInBytes;
}
RECORDHEADER, *PRECORDHEADER;

typedef struct tagMEMMAP6046
{
    // General structure information.

    unsigned long                       m_ulVersionNumber;                                                              // Version number of this structure.

    // Data relative parameters.

    unsigned long                       m_ulNumberOfCells;                                                              // Number of cells in this MMF.
    unsigned long                       m_ulCellSizeInBytes;                                                            // Cell size in bytes.
    unsigned long                       m_ulNextWritePoint;                                                             // Pointer for next write.
    unsigned long                       m_ulNextSequenceNumber;

    // Cell occupancy index table.

    CELLINDEX                           m_sCellIndex[ NMMF6046::ulNumberOfCells_c ];                                    // Cell occupancy data.

    // Add new elements here to maintain backward compatibility; reduce reserved space accordingly.

    BYTE                                m_byReserved[ NMMF6046::ulReservedSpaceInBytes_c ];                             // Reserved space to pad header.

    // Data portion; a quantized circular buffer of cells.

    BYTE                                m_byCells  [ NMMF6046::ulNumberOfCells_c * NMMF6046::ulCellSizeInBytes_c ];     // Cell oriented circular buffer.
}
MEMMAP6046, *PMEMMAP6046;

#pragma pack ( pop, MMF6046_H_PACK )                                                                                        // Restore previous byte alignment.

//////////////////////////////////////////////////////////////////////
// CMMF6046 class definition.

class CMMF6046 : public CMemoryMappedFile<MEMMAP6046>, public CCircularBuffer<RECORDHEADER>
{
public:

    ///////////////
    // Services.

    // Construction, destruction and assignment.

                                            CMMF6046                    (   LPCTSTR                 lpszMapName     = _T( "RESON_6046_SENSORDATA_MEMMAP" ),
                                                                            bool                    bUseDiskFile    = false  );

    virtual                                ~CMMF6046                    (   void );

    bool                                    IsInitialized               (   void )                  const;

    bool                                    InitializeMMFContents       (   void );

private:

    ///////////////
    // Attributes.

    bool                                    m_bInitialized;

    // Copy constructor and assignment operators not implemented thus private.

                                            CMMF6046                    (   const CMMF6046         &rRhs );                 // Not implemented thus private.
    CMMF6046 &                              operator    =               (   const CMMF6046         &rRhs );                 // Not implemented thus private.
};

#endif // !defined(AFX_MMF6046_H__4172B1AD_F536_4BA9_BB95_8658CB345C02__INCLUDED_)

