/** THIS IS AN AUTOMATICALLY GENERATED FILE.  DO NOT MODIFY
 * BY HAND!!
 *
 * Generated by LCM
 **/

#include <string.h>
#include "auvlcm_os_conduit_t.h"

static int __auvlcm_os_conduit_t_hash_computed;
static int64_t __auvlcm_os_conduit_t_hash;
 
int64_t __auvlcm_os_conduit_t_hash_recursive(const __lcm_hash_ptr *p)
{
    const __lcm_hash_ptr *fp;
    for (fp = p; fp != NULL; fp = fp->parent)
        if (fp->v == __auvlcm_os_conduit_t_get_hash)
            return 0;
 
    const __lcm_hash_ptr cp = { p, (void*)__auvlcm_os_conduit_t_get_hash };
    (void) cp;
 
    int64_t hash = 0x4178f69b99aa1d40LL
         + __int64_t_hash_recursive(&cp)
         + __auvlcm_os_conduit_raw_t_hash_recursive(&cp)
         + __auvlcm_os_conduit_raw_t_hash_recursive(&cp)
         + __auvlcm_os_conduit_raw_t_hash_recursive(&cp)
         + __auvlcm_os_conduit_raw_t_hash_recursive(&cp)
        ;
 
    return (hash<<1) + ((hash>>63)&1);
}
 
int64_t __auvlcm_os_conduit_t_get_hash(void)
{
    if (!__auvlcm_os_conduit_t_hash_computed) {
        __auvlcm_os_conduit_t_hash = __auvlcm_os_conduit_t_hash_recursive(NULL);
        __auvlcm_os_conduit_t_hash_computed = 1;
    }
 
    return __auvlcm_os_conduit_t_hash;
}
 
int __auvlcm_os_conduit_t_encode_array(void *buf, int offset, int maxlen, const auvlcm_os_conduit_t *p, int elements)
{
    int pos = 0, thislen, element;
 
    for (element = 0; element < elements; element++) {
 
        thislen = __int64_t_encode_array(buf, offset + pos, maxlen - pos, &(p[element].utime), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
        thislen = __auvlcm_os_conduit_raw_t_encode_array(buf, offset + pos, maxlen - pos, &(p[element].dvl), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
        thislen = __auvlcm_os_conduit_raw_t_encode_array(buf, offset + pos, maxlen - pos, &(p[element].altimeter), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
        thislen = __auvlcm_os_conduit_raw_t_encode_array(buf, offset + pos, maxlen - pos, &(p[element].gps), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
        thislen = __auvlcm_os_conduit_raw_t_encode_array(buf, offset + pos, maxlen - pos, &(p[element].compass), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
    }
    return pos;
}
 
int auvlcm_os_conduit_t_encode(void *buf, int offset, int maxlen, const auvlcm_os_conduit_t *p)
{
    int pos = 0, thislen;
    int64_t hash = __auvlcm_os_conduit_t_get_hash();
 
    thislen = __int64_t_encode_array(buf, offset + pos, maxlen - pos, &hash, 1);
    if (thislen < 0) return thislen; else pos += thislen;
 
    thislen = __auvlcm_os_conduit_t_encode_array(buf, offset + pos, maxlen - pos, p, 1);
    if (thislen < 0) return thislen; else pos += thislen;
 
    return pos;
}
 
int __auvlcm_os_conduit_t_encoded_array_size(const auvlcm_os_conduit_t *p, int elements)
{
    int size = 0, element;
    for (element = 0; element < elements; element++) {
 
        size += __int64_t_encoded_array_size(&(p[element].utime), 1);
 
        size += __auvlcm_os_conduit_raw_t_encoded_array_size(&(p[element].dvl), 1);
 
        size += __auvlcm_os_conduit_raw_t_encoded_array_size(&(p[element].altimeter), 1);
 
        size += __auvlcm_os_conduit_raw_t_encoded_array_size(&(p[element].gps), 1);
 
        size += __auvlcm_os_conduit_raw_t_encoded_array_size(&(p[element].compass), 1);
 
    }
    return size;
}
 
int auvlcm_os_conduit_t_encoded_size(const auvlcm_os_conduit_t *p)
{
    return 8 + __auvlcm_os_conduit_t_encoded_array_size(p, 1);
}
 
int __auvlcm_os_conduit_t_decode_array(const void *buf, int offset, int maxlen, auvlcm_os_conduit_t *p, int elements)
{
    int pos = 0, thislen, element;
 
    for (element = 0; element < elements; element++) {
 
        thislen = __int64_t_decode_array(buf, offset + pos, maxlen - pos, &(p[element].utime), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
        thislen = __auvlcm_os_conduit_raw_t_decode_array(buf, offset + pos, maxlen - pos, &(p[element].dvl), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
        thislen = __auvlcm_os_conduit_raw_t_decode_array(buf, offset + pos, maxlen - pos, &(p[element].altimeter), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
        thislen = __auvlcm_os_conduit_raw_t_decode_array(buf, offset + pos, maxlen - pos, &(p[element].gps), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
        thislen = __auvlcm_os_conduit_raw_t_decode_array(buf, offset + pos, maxlen - pos, &(p[element].compass), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
    }
    return pos;
}
 
int __auvlcm_os_conduit_t_decode_array_cleanup(auvlcm_os_conduit_t *p, int elements)
{
    int element;
    for (element = 0; element < elements; element++) {
 
        __int64_t_decode_array_cleanup(&(p[element].utime), 1);
 
        __auvlcm_os_conduit_raw_t_decode_array_cleanup(&(p[element].dvl), 1);
 
        __auvlcm_os_conduit_raw_t_decode_array_cleanup(&(p[element].altimeter), 1);
 
        __auvlcm_os_conduit_raw_t_decode_array_cleanup(&(p[element].gps), 1);
 
        __auvlcm_os_conduit_raw_t_decode_array_cleanup(&(p[element].compass), 1);
 
    }
    return 0;
}
 
int auvlcm_os_conduit_t_decode(const void *buf, int offset, int maxlen, auvlcm_os_conduit_t *p)
{
    int pos = 0, thislen;
    int64_t hash = __auvlcm_os_conduit_t_get_hash();
 
    int64_t this_hash;
    thislen = __int64_t_decode_array(buf, offset + pos, maxlen - pos, &this_hash, 1);
    if (thislen < 0) return thislen; else pos += thislen;
    if (this_hash != hash) return -1;
 
    thislen = __auvlcm_os_conduit_t_decode_array(buf, offset + pos, maxlen - pos, p, 1);
    if (thislen < 0) return thislen; else pos += thislen;
 
    return pos;
}
 
int auvlcm_os_conduit_t_decode_cleanup(auvlcm_os_conduit_t *p)
{
    return __auvlcm_os_conduit_t_decode_array_cleanup(p, 1);
}
 
int __auvlcm_os_conduit_t_clone_array(const auvlcm_os_conduit_t *p, auvlcm_os_conduit_t *q, int elements)
{
    int element;
    for (element = 0; element < elements; element++) {
 
        __int64_t_clone_array(&(p[element].utime), &(q[element].utime), 1);
 
        __auvlcm_os_conduit_raw_t_clone_array(&(p[element].dvl), &(q[element].dvl), 1);
 
        __auvlcm_os_conduit_raw_t_clone_array(&(p[element].altimeter), &(q[element].altimeter), 1);
 
        __auvlcm_os_conduit_raw_t_clone_array(&(p[element].gps), &(q[element].gps), 1);
 
        __auvlcm_os_conduit_raw_t_clone_array(&(p[element].compass), &(q[element].compass), 1);
 
    }
    return 0;
}
 
auvlcm_os_conduit_t *auvlcm_os_conduit_t_copy(const auvlcm_os_conduit_t *p)
{
    auvlcm_os_conduit_t *q = (auvlcm_os_conduit_t*) malloc(sizeof(auvlcm_os_conduit_t));
    __auvlcm_os_conduit_t_clone_array(p, q, 1);
    return q;
}
 
void auvlcm_os_conduit_t_destroy(auvlcm_os_conduit_t *p)
{
    __auvlcm_os_conduit_t_decode_array_cleanup(p, 1);
    free(p);
}
 
int auvlcm_os_conduit_t_publish(lcm_t *lc, const char *channel, const auvlcm_os_conduit_t *p)
{
      int max_data_size = auvlcm_os_conduit_t_encoded_size (p);
      uint8_t *buf = (uint8_t*) malloc (max_data_size);
      if (!buf) return -1;
      int data_size = auvlcm_os_conduit_t_encode (buf, 0, max_data_size, p);
      if (data_size < 0) {
          free (buf);
          return data_size;
      }
      int status = lcm_publish (lc, channel, buf, data_size);
      free (buf);
      return status;
}

struct _auvlcm_os_conduit_t_subscription_t {
    auvlcm_os_conduit_t_handler_t user_handler;
    void *userdata;
    lcm_subscription_t *lc_h;
};
static
void auvlcm_os_conduit_t_handler_stub (const lcm_recv_buf_t *rbuf, 
                            const char *channel, void *userdata)
{
    int status;
    auvlcm_os_conduit_t p;
    memset(&p, 0, sizeof(auvlcm_os_conduit_t));
    status = auvlcm_os_conduit_t_decode (rbuf->data, 0, rbuf->data_size, &p);
    if (status < 0) {
        fprintf (stderr, "error %d decoding auvlcm_os_conduit_t!!!\n", status);
        return;
    }

    auvlcm_os_conduit_t_subscription_t *h = (auvlcm_os_conduit_t_subscription_t*) userdata;
    h->user_handler (rbuf, channel, &p, h->userdata);

    auvlcm_os_conduit_t_decode_cleanup (&p);
}

auvlcm_os_conduit_t_subscription_t* auvlcm_os_conduit_t_subscribe (lcm_t *lcm, 
                    const char *channel, 
                    auvlcm_os_conduit_t_handler_t f, void *userdata)
{
    auvlcm_os_conduit_t_subscription_t *n = (auvlcm_os_conduit_t_subscription_t*)
                       malloc(sizeof(auvlcm_os_conduit_t_subscription_t));
    n->user_handler = f;
    n->userdata = userdata;
    n->lc_h = lcm_subscribe (lcm, channel, 
                                 auvlcm_os_conduit_t_handler_stub, n);
    if (n->lc_h == NULL) {
        fprintf (stderr,"couldn't reg auvlcm_os_conduit_t LCM handler!\n");
        free (n);
        return NULL;
    }
    return n;
}

int auvlcm_os_conduit_t_unsubscribe(lcm_t *lcm, auvlcm_os_conduit_t_subscription_t* hid)
{
    int status = lcm_unsubscribe (lcm, hid->lc_h);
    if (0 != status) {
        fprintf(stderr, 
           "couldn't unsubscribe auvlcm_os_conduit_t_handler %p!\n", hid);
        return -1;
    }
    free (hid);
    return 0;
}

