/* AUTOMATICALY GENERATED by org.flib.net.CPickle.java */

#include <netinet/in.h>
#include <string.h>

#include <fcpickle_impl.h>
#include <sdodownloadrequestmsg.h>

void SDODownloadRequestMsg_init(SDODownloadRequestMsg_t *o)
{
    o->data = NULL;
    o->data_arraylen = 0;
}

/// This value varies only if dynamic length arrays or String type are used.
/// @param o If class does not contain variable length field types (String) or superclass,/// then o can be NULL.
/// @return size of buffer needed to pickle object only data
int SDODownloadRequestMsg_getObjectBufferSize(SDODownloadRequestMsg_t *o)
{
    int size = 0;
    // get superclass size
    if(!o) return -1;
    size += SDOMsgBase_getObjectBufferSize(&(o->super));
    if(!o) return -1;
    size += sizeof(fcpickle_array_size_t);
    size += o->data_arraylen * sizeof(int8_t);
    size += 0;
    return size;
}

/// This value varies only if dynamic length arrays or String type are used
/// @return size of buffer needed to pickle object data and service data
int SDODownloadRequestMsg_getPacketBufferSize(SDODownloadRequestMsg_t *o)
{
    int32_t len = 0;
    fcpickle_class_def_t class_def;
    class_def.className = "ocera.rtcan.SDODownloadRequestMsg";
    len += FCPICKLE_PACKET_HEAD_SIZE;
    len += CPickleClassDef_getObjectBufferSize(&class_def);
    len += SDODownloadRequestMsg_getObjectBufferSize(o);
    return len;
}

int SDODownloadRequestMsg_pickleObject(SDODownloadRequestMsg_t *o, uint8_t *buffer, int buff_len)
{
    int32_t len = 0;
    // pickle superclass
    {
        int i;
        i = SDOMsgBase_pickleObject(&(o->super), buffer+len, buff_len-len);
        if(i < 0) return i;
        len += i;
    }

    // pickle field 'data'
    {
        int i;
        fcpickle_array_size_t arraylen;
        if(!o) return -1;
        arraylen = o->data_arraylen;
        if(arraylen > 1024) arraylen = 1024; // limit max packet size
        if(len + sizeof(arraylen) > (size_t)buff_len) return -2;
        *(fcpickle_array_size_t*)(buffer + len) = h2ns(arraylen); len += sizeof(fcpickle_array_size_t);
        for(i=0; i<arraylen; i++) {
            if(!o) return -1;
            if(len + sizeof(int8_t) > (size_t)buff_len) return -3;
            *(int8_t*)(buffer + len) = (o->data[i]); len += sizeof(int8_t);
        }
    }
    return len;
}

/// align structure to array and set network endianing
/// @param buffer sufficient size of buffer is  xx_getBufferSize()
/// @param buff_len actual size of buffer
/// @return number of filled bytes or negative value if error
int SDODownloadRequestMsg_toNet(SDODownloadRequestMsg_t *o, uint8_t *buffer, int buff_len)
{
    int32_t len = 0, i;
    CPickleHead_t head;
    CPickleClassDef_t class_def;
    // set class definition
    class_def.className = "ocera.rtcan.SDODownloadRequestMsg";
    // check buffer size
    len = SDODownloadRequestMsg_getPacketBufferSize(o);
    if(len > (size_t)buff_len) return -1;
    // set packet head
    memcpy(&(head.headMagic), "CPICKLE_", 8);
    head.termZero = '\0';
    head.packetSize = len;
    len = 0;
    // pickle all to packet
    i = CPickleHead_pickleObject(&head, buffer+len, buff_len-len);
    if(i < 0) return i;
    len += i;
    i = CPickleClassDef_pickleObject(&class_def, buffer+len, buff_len-len);
    if(i < 0) return i;
    len += i;
    i = SDODownloadRequestMsg_pickleObject(o, buffer+len, buff_len-len);
    if(i < 0) return i;
    len += i;
    return len;
}

/// load object data from buffer and set host endianing
/// @param buffer data to read from
/// NOTE!! char* (String in Java) fields are backuped by buffer (no memory is allocated during loading)
/// @return number of read bytes or negative value if error
int SDODownloadRequestMsg_unpickleObject(SDODownloadRequestMsg_t *o, uint8_t *buffer, int buff_len)
{
    int32_t len = 0;
    // unpickle superclass
    {
        int i;
        i = SDOMsgBase_unpickleObject(&(o->super), buffer+len, buff_len-len);
        if(i < 0) return i;
        len += i;
    }

    // unpickle field 'data'
    {
        fcpickle_array_size_t arraylen;
        arraylen = n2hs(*(fcpickle_array_size_t*)(buffer + len));
        len += sizeof(fcpickle_array_size_t);
        o->data_arraylen = arraylen;
        // unpickled varriable size arrays are backuped by packet buffer (library should alocate dinamic memory in other case)
        o->data = (int8_t*)(buffer + len); len += arraylen * sizeof(int8_t);
    }
    return len;
}
/// load object and service data from buffer and set host endianing
/// @param buffer data to read from
/// NOTE!! char* (String in Java) fields are backuped by buffer (no memory is allocated during loading)
/// @return number of read bytes or negative value if error
int SDODownloadRequestMsg_fromNet(SDODownloadRequestMsg_t *o, uint8_t *buffer, int buff_len)
{
    int32_t len = 0, i;
    CPickleHead_t head;
    CPickleClassDef_t class_def;
    i = CPickleHead_unpickleObject(&head, buffer+len, buff_len-len);
    if(i < 0) return i;
    len += i;
    i = CPickleClassDef_unpickleObject(&class_def, buffer+len, buff_len-len);
    if(i < 0) return i;
    len += i;
    i = SDODownloadRequestMsg_unpickleObject(o, buffer+len, buff_len-len);
    if(i < 0) return i;
    len += i;
    return len;
}

