/*
 * SmartHome server: Plug-in xbee
 *
 * Copyright (C) 2010 Daniel Pisi <dan.pisi@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 */

/**
 * @file xbstreamitem.h
 * @brief Digi XBee2 modules' API items streaming support
 * @author Daniel Pisi
 */

#ifndef XBSTREAMITEM_H
#define XBSTREAMITEM_H

#include <QtCore/QCoreApplication>
#include <QDataStream>

// -----------------------------------------------------------------------------

class XbCommand;
class XbIoSamples;
class XbIoSamplesBlock;

// -----------------------------------------------------------------------------

/**
 * \class XbStreamItem
 * \brief Item streaming support base class
 */
class XbStreamItem
{
public:
    virtual void wr(QDataStream &stream) {};
    virtual void rd(QDataStream &stream) {};
    virtual ~XbStreamItem();
};
// -----------------------------------------------------------------------------

/**
 * \class XbStreamItemList
 * \brief Streaming of item lists
 */
class XbStreamItemList : public XbStreamItem
{
public:
    virtual void wr(QDataStream &stream);
    virtual void rd(QDataStream &stream);
    virtual ~XbStreamItemList();

protected:
    QList<XbStreamItem*> mItems;
};
// -----------------------------------------------------------------------------

/**
 * \class XbUint8Item
 * \brief Streaming of 8-bit unsigned values
 */
class XbUint8Item : public XbStreamItem
{
public:
    XbUint8Item(quint8 &data);

    virtual void wr(QDataStream &stream);
    virtual void rd(QDataStream &stream);

private:
    quint8 &mData;
};
// -----------------------------------------------------------------------------

/**
 * \class XbUint16Item
 * \brief Streaming of 16-bit unsigned values
 */
class XbUint16Item : public XbStreamItem
{
public:
    XbUint16Item(quint16 &data);

    virtual void wr(QDataStream &stream);
    virtual void rd(QDataStream &stream);

private:
    quint16 &mData;
};
// -----------------------------------------------------------------------------

/**
 * \class XbUint64Item
 * \brief Streaming of 64-bit unsigned values
 */
class XbUint64Item : public XbStreamItem
{
public:
    XbUint64Item(quint64 &data);

    virtual void wr(QDataStream &stream);
    virtual void rd(QDataStream &stream);

private:
    quint64 &mData;
};
// -----------------------------------------------------------------------------

/**
 * \class XbByteArrayItem
 * \brief Streaming of byte arrays
 */
class XbByteArrayItem : public XbStreamItem
{
public:
    XbByteArrayItem(QByteArray &data);

    virtual void wr(QDataStream &stream);
    virtual void rd(QDataStream &stream);

private:
    QByteArray &mData;
};
// -----------------------------------------------------------------------------

/**
 * \class XbAtCommandItem
 * \brief Streaming of AT commands
 */
class XbAtCommandItem : public XbStreamItem
{
public:
    XbAtCommandItem(XbCommand &xbCommand);

    virtual void wr(QDataStream &stream);
    virtual void rd(QDataStream &stream);

private:
    XbCommand &mXbCommand;
};
// -----------------------------------------------------------------------------

/**
 * \class XbAddressItem
 * \brief Streaming of MAC and network addresses
 */
class XbAddressItem : public XbStreamItem
{
public:
    XbAddressItem(quint64 &data64, quint16 &data16);

    virtual void wr(QDataStream &stream);
    virtual void rd(QDataStream &stream);

private:
    quint64 &mData64;
    quint16 &mData16;
};
// -----------------------------------------------------------------------------

/**
 * \class XbDigitalSampleItem
 * \brief Streaming of digital I/O samples
 */
class XbDigitalSampleItem : public XbStreamItem
{
public:
    XbDigitalSampleItem(quint16 &sample, quint16 &mask);

    virtual void wr(QDataStream &stream);
    virtual void rd(QDataStream &stream);

private:
    quint16 &mSample;
    quint16 &mMask;
};
// -----------------------------------------------------------------------------

/**
 * \class XbAnalogSampleItem
 * \brief Streaming of analog I/O samples
 */
class XbAnalogSampleItem : public XbStreamItem
{
public:
    XbAnalogSampleItem(QList<quint16> &samples, quint8 &mask);

    virtual void wr(QDataStream &stream);
    virtual void rd(QDataStream &stream);

private:
    QList<quint16> &mSamples;
    quint8         &mMask;
};

// -----------------------------------------------------------------------------

/**
 * \class XbIoSamplesItem
 * \brief Encapsulates digital and analog I/O samples streaming
 */
class XbIoSamplesItem : public XbStreamItemList
{
public:
    XbIoSamplesItem(XbIoSamplesBlock &ioSamples);
};
// -----------------------------------------------------------------------------

/**
 * \class XbVariantItem
 * \brief Allows to stream arbitrary data type
 */
class XbVariantItem : public XbStreamItem
{
public:
    XbVariantItem(XbStreamItem **streamItem);

    virtual void wr(QDataStream &stream);
    virtual void rd(QDataStream &stream);

private:
    XbStreamItem **mStreamItem;
};
// -----------------------------------------------------------------------------

#endif // XBSTREAMITEM_H
