// $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/src/tiptilt_ctrl/bcu/Interface/Sharknir/Bcu/SharknirBcuCcdDiagnosticRecord.cpp 961 2018-10-30 11:15:19Z depa $
// #include "basda/service/bcu/worker/ArgosBcuCcdDiagnosticRecord.h"
#include "tiptilt_ctrl/bcu/Interface/Sharknir/Bcu/SharknirBcuCcdDiagnosticRecord.h"

#include <boost/foreach.hpp>
#include <cstdio>
#include <cassert>
#include <cstring>


namespace Sharknir { namespace Bcu {

const uint16_t BaseCcdDiagnosticRecord::width= 264;
const uint16_t BaseCcdDiagnosticRecord::height= 264;

static inline void retrievePixels(SeqFloat& destination,
        const Basda::Microgate::ByteBufferPtr& b)
{
    assert(sizeof(SharknirBcuCcdDiagnosticRecord) == b->size());
    const SharknirBcuCcdDiagnosticRecord* const record=
            reinterpret_cast<SharknirBcuCcdDiagnosticRecord*>(&b->front());

    size_t pixelIndex= 0;
    destination.resize(SharknirBcuCcdDiagnosticRecord::N_PIXELS);
    BOOST_FOREACH(SharknirBcuCcdDiagnosticRecord::Pixel pixel, record->pixels)
    {

        destination[pixelIndex++]= static_cast<float>(pixel);
    }

    assert(SharknirBcuCcdDiagnosticRecord::N_PIXELS == pixelIndex);

}
SeqFloat getPixelsAsFloats(const Basda::Microgate::ByteBufferPtr& b)
{
    SeqFloat seq(SharknirBcuCcdDiagnosticRecord::N_PIXELS);
    retrievePixels(seq, b);
    return seq;
}

std::string
getRawPixelFloatsAsString(const Basda::Microgate::ByteBufferPtr& b)
{
    BOOST_STATIC_ASSERT(4 == sizeof(float));
    SeqFloat floatPixels;
    retrievePixels(floatPixels, b);

    return std::string(reinterpret_cast<const char*>(&floatPixels[0]),
            SharknirBcuCcdDiagnosticRecord::N_PIXELS * sizeof(float));
}

bool isCcdDiagnosticRecordBroken(
        const BuiltInAdcCcdDiagnosticRecord* const rec)
{
    BOOST_STATIC_ASSERT(sizeof(rec->header) == sizeof(rec->footer));
    return 0 != memcmp(&rec->header, &rec->footer,
            sizeof(DiagnosticRecordHeader));
}

bool isCcdDiagnosticRecordBroken(
        const OptAdcCcdDiagnosticRecord* const r)
{
    assert(! hasSanityCheck(*r));
    return false;
}

bool hasSanityCheck(BuiltInAdcCcdDiagnosticRecord)
{
    return true;
}

bool hasSanityCheck(OptAdcCcdDiagnosticRecord)
{
    return false;
}

}
}
