<commit_before>#include "xbeepacket.h"
#include <QDebug>

namespace QtXBee {

/**
 * @brief XBeePacket's constructor
 * @param parent
 */
XBeePacket::XBeePacket(QObject *parent) :
    QObject(parent),
    m_startDelimiter(0x7E),
    m_length(0),
    m_frameType(UndefinedId),
    m_frameId(0),
    m_checksum(0)
{
}

/**
 * @brief Sets the frame's start delimiter.
 * By default the start delemiter is set to 0x7E
 * @sa XBeePacket::startDelimiter()
 * @param sd
 */
void XBeePacket::setStartDelimiter(unsigned sd) {
    m_startDelimiter = sd;
}

/**
 * @brief Sets the frame's length
 * @param l
 * @sa XBeePacket::length()
 */
void XBeePacket::setLength(unsigned l) {
    m_length = l;
}

/**
 * @brief Sets the frame's type
 * @param type
 * @sa XBeePacket::frameType()
 */
void XBeePacket::setFrameType(ApiId type) {
    m_frameType = type;
}

/**
 * @brief Sets the frame's id
 * @param id
 * @sa XBeePacket::frameType()
 */
void XBeePacket::setFrameId(unsigned id) {
    m_frameId = id;
}

/**
 * @brief Sets the checksum
 * @param cs
 * @sa XBeePacket::checksum()
 */
void XBeePacket::setChecksum(unsigned cs) {
    m_checksum = cs;
}

/**
 * @brief Returns the frame's start delimiter
 * @returns the frame's start delimiter
 * @sa XBeePacket::setStartDelimiter()
 */
unsigned XBeePacket::startDelimiter() const {
    return m_startDelimiter;
}

/**
 * @brief Returns the frame-specific data length (Number of bytes between the length and the checksum)
 * @returns the packet's length
 * @sa XBeePacket::setLength()
 */
quint16 XBeePacket::length() const {
    return m_length;
}

/**
 * @brief Returns the frame's type
 * @returns the frame's type
 * @sa XBeePacket::setFrameType()
 * @sa XBeePacket::APIFrameType
 */
XBeePacket::ApiId XBeePacket::frameType() const {
    return m_frameType;
}

/**
 * @brief Returns the frame's id
 * @returns the frame's id
 * @sa XBeePacket::setFrameId()
 */
unsigned XBeePacket::frameId() const {
    return m_frameId;
}

/**
 * @brief Returns the packet's checksum
 * @returns the packet's checksum
 * @sa XBeePacket::setChecksum()
 */
unsigned XBeePacket::checksum() const {
    return m_checksum;
}

/**
 * @brief Computes the checksum of the given QByteArray, and set it.
 * @param array
 * @sa XBeePacket::setChecksum()
 * @sa XBeePacket::checksum()
 */
void XBeePacket::createChecksum(QByteArray array) {
    int len = array.size();
    unsigned int sum = 0x00;
    unsigned int ff = 0xFF;
    unsigned int fe = 0xFE;
    for(int i=0;i<len;i++)
    {
        unsigned int a = array.at(i);
        if (a == (unsigned int)4294967295){
            a = ff;
        } else if (a == (unsigned int)4294967294){
            a = fe;
        }

        sum += a;
    }
    setChecksum((ff - sum) & 0x000000FF);
}

/**
 * @brief Returns the frame's packet (raw data)
 * @returns the frame's packet (raw data)
 */
QByteArray XBeePacket::packet() const {
    return m_packet;
}

bool XBeePacket::setPacket(const QByteArray &packet) {
    ApiId apiId = UndefinedId;
    quint8 apiSpecificOffset = 4; // 5th byte
    QByteArray specificData;

    clear();
    m_packet = packet;

    if(packet.size() < 5) {
        qDebug() << Q_FUNC_INFO << "bad packet !";
        return false;
    }

    setStartDelimiter(packet.at(0));
    setLength((unsigned char)packet.at(2) + ((unsigned char)packet.at(1)<<8));
    apiId = (ApiId)(packet.at(3)&0xff);
//    qDebug() << "apiid" << apiId << ",frame type" << m_frameType;
//    if(apiId != m_frameType) {
//        qDebug() << Q_FUNC_INFO << "Bad API frame ID !" << qPrintable(QString("(expected 0x%1, got 0x%2)").arg(m_frameType,0,16).arg(apiId,0,16));
//        return false;
//    }
    setFrameType(apiId);

    if(packet.size() > 5) {
        for(int i=apiSpecificOffset; i< packet.size(); i++) {
            specificData.append(packet.at(i));
        }
        parseApiSpecificData(specificData);
    }
    setChecksum(packet.at(packet.size()-1));

    assemblePacket();
    return true;
}

bool XBeePacket::parseApiSpecificData(const QByteArray &data) {
    Q_UNUSED(data)
    return false;
}

/**
 * @brief Assembles the packet to be able to send it.
 *
 * Overload this function to create your own packet.
 */
void XBeePacket::assemblePacket() {

}

void XBeePacket::clear() {
    m_packet.clear();
    m_startDelimiter = 0x7E;
    m_length = 0;
    m_frameType = UndefinedId;<commit_msg>Fixed size of API specific data in XBeePacket class<commit_after>#include "xbeepacket.h"
#include <QDebug>

namespace QtXBee {

/**
 * @brief XBeePacket's constructor
 * @param parent
 */
XBeePacket::XBeePacket(QObject *parent) :
    QObject(parent),
    m_startDelimiter(0x7E),
    m_length(0),
    m_frameType(UndefinedId),
    m_frameId(0),
    m_checksum(0)
{
}

/**
 * @brief Sets the frame's start delimiter.
 * By default the start delemiter is set to 0x7E
 * @sa XBeePacket::startDelimiter()
 * @param sd
 */
void XBeePacket::setStartDelimiter(unsigned sd) {
    m_startDelimiter = sd;
}

/**
 * @brief Sets the frame's length
 * @param l
 * @sa XBeePacket::length()
 */
void XBeePacket::setLength(unsigned l) {
    m_length = l;
}

/**
 * @brief Sets the frame's type
 * @param type
 * @sa XBeePacket::frameType()
 */
void XBeePacket::setFrameType(ApiId type) {
    m_frameType = type;
}

/**
 * @brief Sets the frame's id
 * @param id
 * @sa XBeePacket::frameType()
 */
void XBeePacket::setFrameId(unsigned id) {
    m_frameId = id;
}

/**
 * @brief Sets the checksum
 * @param cs
 * @sa XBeePacket::checksum()
 */
void XBeePacket::setChecksum(unsigned cs) {
    m_checksum = cs;
}

/**
 * @brief Returns the frame's start delimiter
 * @returns the frame's start delimiter
 * @sa XBeePacket::setStartDelimiter()
 */
unsigned XBeePacket::startDelimiter() const {
    return m_startDelimiter;
}

/**
 * @brief Returns the frame-specific data length (Number of bytes between the length and the checksum)
 * @returns the packet's length
 * @sa XBeePacket::setLength()
 */
quint16 XBeePacket::length() const {
    return m_length;
}

/**
 * @brief Returns the frame's type
 * @returns the frame's type
 * @sa XBeePacket::setFrameType()
 * @sa XBeePacket::APIFrameType
 */
XBeePacket::ApiId XBeePacket::frameType() const {
    return m_frameType;
}

/**
 * @brief Returns the frame's id
 * @returns the frame's id
 * @sa XBeePacket::setFrameId()
 */
unsigned XBeePacket::frameId() const {
    return m_frameId;
}

/**
 * @brief Returns the packet's checksum
 * @returns the packet's checksum
 * @sa XBeePacket::setChecksum()
 */
unsigned XBeePacket::checksum() const {
    return m_checksum;
}

/**
 * @brief Computes the checksum of the given QByteArray, and set it.
 * @param array
 * @sa XBeePacket::setChecksum()
 * @sa XBeePacket::checksum()
 */
void XBeePacket::createChecksum(QByteArray array) {
    int len = array.size();
    unsigned int sum = 0x00;
    unsigned int ff = 0xFF;
    unsigned int fe = 0xFE;
    for(int i=0;i<len;i++)
    {
        unsigned int a = array.at(i);
        if (a == (unsigned int)4294967295){
            a = ff;
        } else if (a == (unsigned int)4294967294){
            a = fe;
        }

        sum += a;
    }
    setChecksum((ff - sum) & 0x000000FF);
}

/**
 * @brief Returns the frame's packet (raw data)
 * @returns the frame's packet (raw data)
 */
QByteArray XBeePacket::packet() const {
    return m_packet;
}

bool XBeePacket::setPacket(const QByteArray &packet) {
    ApiId apiId = UndefinedId;
    quint8 apiSpecificOffset = 4; // 5th byte
    QByteArray specificData;

    clear();
    m_packet = packet;

    if(packet.size() < 5) {
        qDebug() << Q_FUNC_INFO << "bad packet !";
        return false;
    }

    setStartDelimiter(packet.at(0));
    setLength((unsigned char)packet.at(2) + ((unsigned char)packet.at(1)<<8));
    apiId = (ApiId)(packet.at(3)&0xff);
//    qDebug() << "apiid" << apiId << ",frame type" << m_frameType;
//    if(apiId != m_frameType) {
//        qDebug() << Q_FUNC_INFO << "Bad API frame ID !" << qPrintable(QString("(expected 0x%1, got 0x%2)").arg(m_frameType,0,16).arg(apiId,0,16));
//        return false;
//    }
    setFrameType(apiId);

    if(packet.size() > 5) {
        for(int i=apiSpecificOffset; i< packet.size()-1; i++) {
            specificData.append(packet.at(i));
        }
        parseApiSpecificData(specificData);
    }
    setChecksum(packet.at(packet.size()-1));

    assemblePacket();
    return true;
}

bool XBeePacket::parseApiSpecificData(const QByteArray &data) {
    Q_UNUSED(data)
    return false;
}

/**
 * @brief Assembles the packet to be able to send it.
 *
 * Overload this function to create your own packet.
 */
void XBeePacket::assemblePacket() {

}

void XBeePacket::clear() {
    m_packet.clear();
    m_startDelimiter = 0x7E;
    m_length = 0;
    m_frameType = UndefinedId;
{'fraction_non_alphanumeric': 0.14349112426035504, 'fraction_numerical': 0.01536185707783341, 'mean_word_length': 3.3466864490603365, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2937802', 'n_tokens_mistral': 3207, 'n_tokens_neox': 3113, 'n_words': 749}