ROOT logo
// $Id$

/**************************************************************************
 * This file is property of and copyright by the ALICE HLT Project        * 
 * ALICE Experiment at CERN, All rights reserved.                         *
 *                                                                        *
 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
 *                  for The ALICE HLT Project.                            *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/

/** @file   AliHLTOUTHomerBuffer.cxx
    @author Matthias Richter
    @date   
    @brief  HLTOUT data wrapper for buffer in HOMER format.               */

// see header file for class documentation
// or
// refer to README to build package
// or
// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt

#include <cerrno>
#include <cassert>
#include "AliHLTOUTHomerBuffer.h"
#include "AliHLTHOMERReader.h"
#include "AliHLTHOMERLibManager.h"

/** ROOT macro for the implementation of ROOT specific class methods */
ClassImp(AliHLTOUTHomerBuffer)

AliHLTOUTHomerBuffer::AliHLTOUTHomerBuffer(const AliHLTUInt8_t* pBuffer, int size)
  :
  AliHLTOUT(),
  fpManager(new AliHLTHOMERLibManager),
  fpBuffer(pBuffer),
  fSize(size),
  fpReader(NULL)
{
  // see header file for class documentation
  // or
  // refer to README to build package
  // or
  // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
  assert(sizeof(homer_uint64)==kAliHLTComponentDataTypefIDsize);
  assert(sizeof(homer_uint32)==kAliHLTComponentDataTypefOriginSize);
  assert(fpManager);
}

AliHLTOUTHomerBuffer::~AliHLTOUTHomerBuffer()
{
  // see header file for class documentation
  if (fpManager) {
    if (fpReader) fpManager->DeleteReader(fpReader);
    delete fpManager;
    fpManager=NULL;
    fpReader=NULL;
  }
}

int AliHLTOUTHomerBuffer::GenerateIndex()
{
  // see header file for class documentation
  int iResult=0;
  if (!fpReader) {
    if (fpManager) {
      fpReader=fpManager->OpenReaderBuffer(fpBuffer, fSize);
    }
  }
  if (fpReader) {
    iResult=ScanReader(fpReader);
  } else {
    iResult=-ENODEV;
  }
  return iResult;
}

int AliHLTOUTHomerBuffer::GetDataBuffer(AliHLTUInt32_t index, const AliHLTUInt8_t* &pBuffer, 
					AliHLTUInt32_t& size)
{
  // see header file for class documentation
  int iResult=0;
  if (fpReader) {
    if ((pBuffer=static_cast<const AliHLTUInt8_t*>(fpReader->GetBlockData(index)))!=NULL) {
      size=fpReader->GetBlockDataLength(index);
    } else {
      iResult=-ENOENT;
    }
  } else {
    iResult=-ENODEV;
  }
  return iResult;
}

AliHLTOUT::AliHLTOUTByteOrder AliHLTOUTHomerBuffer::CheckBlockByteOrder(AliHLTUInt32_t index)
{
  if (fpReader) {
    return static_cast<AliHLTOUTByteOrder>(fpReader->GetBlockByteOrder(index));
  }
  return kInvalidByteOrder;
}

int AliHLTOUTHomerBuffer::CheckBlockAlignment(AliHLTUInt32_t index, AliHLTOUT::AliHLTOUTDataType type)
{
  if (fpReader) {
    return fpReader->GetBlockTypeAlignment(index, static_cast<homer_uint8>(type));
  }
  return -ENODATA;
}

int AliHLTOUTHomerBuffer::ScanReader(AliHLTMonitoringReader* pReader, AliHLTUInt32_t offset)
{
  // see header file for class documentation
  int iResult=0;
  if (pReader && (iResult=pReader->ReadNextEvent())==0) {
    AliHLTUInt32_t nofBlocks=pReader->GetBlockCnt();
    AliHLTUInt32_t tmp1=0x1;
    AliHLTUInt32_t tmp2=offset;

    // first check if the offset allows to add all data blocks without exceeding the
    // range
    while (nofBlocks>tmp1 && tmp2>0) {
      if (tmp2&0x1) {
	HLTError("index range %#x exceeded for %d data blocks", nofBlocks, offset);
	iResult=-ERANGE;
      }
      tmp2>>=1;
      tmp1<<=1;
    }

    // loop over data blocks
    HLTDebug("generating index for %d data blocks of reader with offset %#x", nofBlocks, offset);
    for (AliHLTUInt32_t i=0; i<nofBlocks && iResult>=0; i++) {
      homer_uint64 id=pReader->GetBlockDataType( i );
      homer_uint32 origin=pReader->GetBlockDataOrigin( i );
      homer_uint32 spec=pReader->GetBlockDataSpec( i );
      AliHLTComponentDataType dt;
      AliHLTComponent::SetDataType(dt, ByteSwap64(id), ByteSwap32(origin));
      AliHLTOUTBlockDescriptor desc(dt, spec, offset|i, this);
      HLTDebug("adding block %d: %s %#x", i, AliHLTComponent::DataType2Text(dt).c_str(), spec);
      iResult=AddBlockDescriptor(desc);
    }
  } else {
    if (iResult==EBADMSG) {
      HLTWarning("Format error in data block");
      iResult*=-1;
    } else if (iResult==126/*ENOKEY*/) {
      HLTWarning("Format error in data block: can not find HOMER block descriptor id");
      iResult*=-1;
    } else {
      iResult=-ENODEV;
    }
  }
  return iResult;
}

 AliHLTOUTHomerBuffer.cxx:1
 AliHLTOUTHomerBuffer.cxx:2
 AliHLTOUTHomerBuffer.cxx:3
 AliHLTOUTHomerBuffer.cxx:4
 AliHLTOUTHomerBuffer.cxx:5
 AliHLTOUTHomerBuffer.cxx:6
 AliHLTOUTHomerBuffer.cxx:7
 AliHLTOUTHomerBuffer.cxx:8
 AliHLTOUTHomerBuffer.cxx:9
 AliHLTOUTHomerBuffer.cxx:10
 AliHLTOUTHomerBuffer.cxx:11
 AliHLTOUTHomerBuffer.cxx:12
 AliHLTOUTHomerBuffer.cxx:13
 AliHLTOUTHomerBuffer.cxx:14
 AliHLTOUTHomerBuffer.cxx:15
 AliHLTOUTHomerBuffer.cxx:16
 AliHLTOUTHomerBuffer.cxx:17
 AliHLTOUTHomerBuffer.cxx:18
 AliHLTOUTHomerBuffer.cxx:19
 AliHLTOUTHomerBuffer.cxx:20
 AliHLTOUTHomerBuffer.cxx:21
 AliHLTOUTHomerBuffer.cxx:22
 AliHLTOUTHomerBuffer.cxx:23
 AliHLTOUTHomerBuffer.cxx:24
 AliHLTOUTHomerBuffer.cxx:25
 AliHLTOUTHomerBuffer.cxx:26
 AliHLTOUTHomerBuffer.cxx:27
 AliHLTOUTHomerBuffer.cxx:28
 AliHLTOUTHomerBuffer.cxx:29
 AliHLTOUTHomerBuffer.cxx:30
 AliHLTOUTHomerBuffer.cxx:31
 AliHLTOUTHomerBuffer.cxx:32
 AliHLTOUTHomerBuffer.cxx:33
 AliHLTOUTHomerBuffer.cxx:34
 AliHLTOUTHomerBuffer.cxx:35
 AliHLTOUTHomerBuffer.cxx:36
 AliHLTOUTHomerBuffer.cxx:37
 AliHLTOUTHomerBuffer.cxx:38
 AliHLTOUTHomerBuffer.cxx:39
 AliHLTOUTHomerBuffer.cxx:40
 AliHLTOUTHomerBuffer.cxx:41
 AliHLTOUTHomerBuffer.cxx:42
 AliHLTOUTHomerBuffer.cxx:43
 AliHLTOUTHomerBuffer.cxx:44
 AliHLTOUTHomerBuffer.cxx:45
 AliHLTOUTHomerBuffer.cxx:46
 AliHLTOUTHomerBuffer.cxx:47
 AliHLTOUTHomerBuffer.cxx:48
 AliHLTOUTHomerBuffer.cxx:49
 AliHLTOUTHomerBuffer.cxx:50
 AliHLTOUTHomerBuffer.cxx:51
 AliHLTOUTHomerBuffer.cxx:52
 AliHLTOUTHomerBuffer.cxx:53
 AliHLTOUTHomerBuffer.cxx:54
 AliHLTOUTHomerBuffer.cxx:55
 AliHLTOUTHomerBuffer.cxx:56
 AliHLTOUTHomerBuffer.cxx:57
 AliHLTOUTHomerBuffer.cxx:58
 AliHLTOUTHomerBuffer.cxx:59
 AliHLTOUTHomerBuffer.cxx:60
 AliHLTOUTHomerBuffer.cxx:61
 AliHLTOUTHomerBuffer.cxx:62
 AliHLTOUTHomerBuffer.cxx:63
 AliHLTOUTHomerBuffer.cxx:64
 AliHLTOUTHomerBuffer.cxx:65
 AliHLTOUTHomerBuffer.cxx:66
 AliHLTOUTHomerBuffer.cxx:67
 AliHLTOUTHomerBuffer.cxx:68
 AliHLTOUTHomerBuffer.cxx:69
 AliHLTOUTHomerBuffer.cxx:70
 AliHLTOUTHomerBuffer.cxx:71
 AliHLTOUTHomerBuffer.cxx:72
 AliHLTOUTHomerBuffer.cxx:73
 AliHLTOUTHomerBuffer.cxx:74
 AliHLTOUTHomerBuffer.cxx:75
 AliHLTOUTHomerBuffer.cxx:76
 AliHLTOUTHomerBuffer.cxx:77
 AliHLTOUTHomerBuffer.cxx:78
 AliHLTOUTHomerBuffer.cxx:79
 AliHLTOUTHomerBuffer.cxx:80
 AliHLTOUTHomerBuffer.cxx:81
 AliHLTOUTHomerBuffer.cxx:82
 AliHLTOUTHomerBuffer.cxx:83
 AliHLTOUTHomerBuffer.cxx:84
 AliHLTOUTHomerBuffer.cxx:85
 AliHLTOUTHomerBuffer.cxx:86
 AliHLTOUTHomerBuffer.cxx:87
 AliHLTOUTHomerBuffer.cxx:88
 AliHLTOUTHomerBuffer.cxx:89
 AliHLTOUTHomerBuffer.cxx:90
 AliHLTOUTHomerBuffer.cxx:91
 AliHLTOUTHomerBuffer.cxx:92
 AliHLTOUTHomerBuffer.cxx:93
 AliHLTOUTHomerBuffer.cxx:94
 AliHLTOUTHomerBuffer.cxx:95
 AliHLTOUTHomerBuffer.cxx:96
 AliHLTOUTHomerBuffer.cxx:97
 AliHLTOUTHomerBuffer.cxx:98
 AliHLTOUTHomerBuffer.cxx:99
 AliHLTOUTHomerBuffer.cxx:100
 AliHLTOUTHomerBuffer.cxx:101
 AliHLTOUTHomerBuffer.cxx:102
 AliHLTOUTHomerBuffer.cxx:103
 AliHLTOUTHomerBuffer.cxx:104
 AliHLTOUTHomerBuffer.cxx:105
 AliHLTOUTHomerBuffer.cxx:106
 AliHLTOUTHomerBuffer.cxx:107
 AliHLTOUTHomerBuffer.cxx:108
 AliHLTOUTHomerBuffer.cxx:109
 AliHLTOUTHomerBuffer.cxx:110
 AliHLTOUTHomerBuffer.cxx:111
 AliHLTOUTHomerBuffer.cxx:112
 AliHLTOUTHomerBuffer.cxx:113
 AliHLTOUTHomerBuffer.cxx:114
 AliHLTOUTHomerBuffer.cxx:115
 AliHLTOUTHomerBuffer.cxx:116
 AliHLTOUTHomerBuffer.cxx:117
 AliHLTOUTHomerBuffer.cxx:118
 AliHLTOUTHomerBuffer.cxx:119
 AliHLTOUTHomerBuffer.cxx:120
 AliHLTOUTHomerBuffer.cxx:121
 AliHLTOUTHomerBuffer.cxx:122
 AliHLTOUTHomerBuffer.cxx:123
 AliHLTOUTHomerBuffer.cxx:124
 AliHLTOUTHomerBuffer.cxx:125
 AliHLTOUTHomerBuffer.cxx:126
 AliHLTOUTHomerBuffer.cxx:127
 AliHLTOUTHomerBuffer.cxx:128
 AliHLTOUTHomerBuffer.cxx:129
 AliHLTOUTHomerBuffer.cxx:130
 AliHLTOUTHomerBuffer.cxx:131
 AliHLTOUTHomerBuffer.cxx:132
 AliHLTOUTHomerBuffer.cxx:133
 AliHLTOUTHomerBuffer.cxx:134
 AliHLTOUTHomerBuffer.cxx:135
 AliHLTOUTHomerBuffer.cxx:136
 AliHLTOUTHomerBuffer.cxx:137
 AliHLTOUTHomerBuffer.cxx:138
 AliHLTOUTHomerBuffer.cxx:139
 AliHLTOUTHomerBuffer.cxx:140
 AliHLTOUTHomerBuffer.cxx:141
 AliHLTOUTHomerBuffer.cxx:142
 AliHLTOUTHomerBuffer.cxx:143
 AliHLTOUTHomerBuffer.cxx:144
 AliHLTOUTHomerBuffer.cxx:145
 AliHLTOUTHomerBuffer.cxx:146
 AliHLTOUTHomerBuffer.cxx:147
 AliHLTOUTHomerBuffer.cxx:148
 AliHLTOUTHomerBuffer.cxx:149
 AliHLTOUTHomerBuffer.cxx:150
 AliHLTOUTHomerBuffer.cxx:151
 AliHLTOUTHomerBuffer.cxx:152
 AliHLTOUTHomerBuffer.cxx:153
 AliHLTOUTHomerBuffer.cxx:154
 AliHLTOUTHomerBuffer.cxx:155
 AliHLTOUTHomerBuffer.cxx:156
 AliHLTOUTHomerBuffer.cxx:157
 AliHLTOUTHomerBuffer.cxx:158
 AliHLTOUTHomerBuffer.cxx:159
 AliHLTOUTHomerBuffer.cxx:160
 AliHLTOUTHomerBuffer.cxx:161
 AliHLTOUTHomerBuffer.cxx:162
 AliHLTOUTHomerBuffer.cxx:163