/*
 * 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 xbitems.cpp
 * @brief Various XbPacket items
 * @author Daniel Pisi
 */

#include "xbitems.h"

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

QString XbDeliveryStatus::deliveryStatusString()
{
    switch (mDeliveryStatus)
    {
    case 0x00: return "Success";
    case 0x01: return "MAC ACK Failure";
    case 0x02: return "CCA Failure";
    case 0x15: return "Invalid destination endpoint";
    case 0x21: return "Network ACK Failure";
    case 0x22: return "Not Joined to Network";
    case 0x23: return "Self-addressed";
    case 0x24: return "Address Not Found";
    case 0x25: return "Route Not Found";
    case 0x26: return "Broadcast source failed to hear a neighbor relay the message";
    case 0x2B: return "Invalid binding table index";
    case 0x2C: return "Resource error lack of free buffers, timers, etc.";
    case 0x32: return "Resource error lack of free buffers, timers, etc.";
    case 0x74: return "Data payload too large";
    case 0x75: return "Indirect message unrequested";
    default:   return "Unknown error";
    }
}
// -----------------------------------------------------------------------------

XbCommand::XbCommand()
{
    mCmdParam = NULL;
}
// -----------------------------------------------------------------------------

QByteArray XbCommand::command()
{
    return QByteArray((const char*)mCommand, 2);
}
// -----------------------------------------------------------------------------

void XbCommand::setCommand(QByteArray command)
{
    if (command.size() == 2)
    {
        mCommand[0] = command.at(0);
        mCommand[1] = command.at(1);
    }
    else
        mCommand[0] = mCommand[1] = 0;

    if (mCmdParam != NULL)
    {
        delete mCmdParam;
        mCmdParam = NULL;
    }
}
// -----------------------------------------------------------------------------

void XbCommand::setCommand(QByteArray command, quint8 param)
{
    setCommand(command);

    mCmdParam = new XbUint8Block(param);
}
// -----------------------------------------------------------------------------

void XbCommand::setCommand(QByteArray command, XbStreamItem *param)
{
    setCommand(command);

    mCmdParam = param;
}
// -----------------------------------------------------------------------------

void XbCommand::updateParamType()
{
    if (mCmdParam != NULL)
    {
        delete mCmdParam;
        mCmdParam = NULL;
    }

    if (mCommand[0] == 'I' && mCommand[1] == 'S')
    {
        mCmdParam = new XbIoSamplesBlock;
    }
}
// -----------------------------------------------------------------------------

XbIoSamplesBlock::XbIoSamplesBlock()
{
    mItems.append(new XbUint8Item(mSamplesCnt));
    mItems.append(new XbUint16Item(mDigitalMask));
    mItems.append(new XbUint8Item(mAnalogMask));
    mItems.append(new XbDigitalSampleItem(mDigitalSample, mDigitalMask));
    mItems.append(new XbAnalogSampleItem(mAnalogSamples, mAnalogMask));
}
// -----------------------------------------------------------------------------

XbUint8Block::XbUint8Block(quint8 value)
{
    mValue = value;
}
// -----------------------------------------------------------------------------

XbUint16Block::XbUint16Block(quint16 value)
{
    mValue = value;
}
// -----------------------------------------------------------------------------
