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

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

static int __legacy_relay_t_hash_computed;
static int64_t __legacy_relay_t_hash;
 
int64_t __legacy_relay_t_hash_recursive(const __lcm_hash_ptr *p)
{
    const __lcm_hash_ptr *fp;
    for (fp = p; fp != NULL; fp = fp->parent)
        if (fp->v == __legacy_relay_t_get_hash)
            return 0;
 
    const __lcm_hash_ptr cp = { p, (void*)__legacy_relay_t_get_hash };
    (void) cp;
 
    int64_t hash = 0x02c22945d29cacf9LL
         + __string_hash_recursive(&cp)
         + __string_hash_recursive(&cp)
         + __boolean_hash_recursive(&cp)
        ;
 
    return (hash<<1) + ((hash>>63)&1);
}
 
int64_t __legacy_relay_t_get_hash(void)
{
    if (!__legacy_relay_t_hash_computed) {
        __legacy_relay_t_hash = __legacy_relay_t_hash_recursive(NULL);
        __legacy_relay_t_hash_computed = 1;
    }
 
    return __legacy_relay_t_hash;
}
 
int __legacy_relay_t_encode_array(void *buf, int offset, int maxlen, const legacy_relay_t *p, int elements)
{
    int pos = 0, thislen, element;
 
    for (element = 0; element < elements; element++) {
 
        thislen = __string_encode_array(buf, offset + pos, maxlen - pos, &(p[element].label), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
        thislen = __string_encode_array(buf, offset + pos, maxlen - pos, &(p[element].group), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
        thislen = __boolean_encode_array(buf, offset + pos, maxlen - pos, &(p[element].state), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
    }
    return pos;
}
 
int legacy_relay_t_encode(void *buf, int offset, int maxlen, const legacy_relay_t *p)
{
    int pos = 0, thislen;
    int64_t hash = __legacy_relay_t_get_hash();
 
    thislen = __int64_t_encode_array(buf, offset + pos, maxlen - pos, &hash, 1);
    if (thislen < 0) return thislen; else pos += thislen;
 
    thislen = __legacy_relay_t_encode_array(buf, offset + pos, maxlen - pos, p, 1);
    if (thislen < 0) return thislen; else pos += thislen;
 
    return pos;
}
 
int __legacy_relay_t_encoded_array_size(const legacy_relay_t *p, int elements)
{
    int size = 0, element;
    for (element = 0; element < elements; element++) {
 
        size += __string_encoded_array_size(&(p[element].label), 1);
 
        size += __string_encoded_array_size(&(p[element].group), 1);
 
        size += __boolean_encoded_array_size(&(p[element].state), 1);
 
    }
    return size;
}
 
int legacy_relay_t_encoded_size(const legacy_relay_t *p)
{
    return 8 + __legacy_relay_t_encoded_array_size(p, 1);
}
 
int __legacy_relay_t_decode_array(const void *buf, int offset, int maxlen, legacy_relay_t *p, int elements)
{
    int pos = 0, thislen, element;
 
    for (element = 0; element < elements; element++) {
 
        thislen = __string_decode_array(buf, offset + pos, maxlen - pos, &(p[element].label), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
        thislen = __string_decode_array(buf, offset + pos, maxlen - pos, &(p[element].group), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
        thislen = __boolean_decode_array(buf, offset + pos, maxlen - pos, &(p[element].state), 1);
        if (thislen < 0) return thislen; else pos += thislen;
 
    }
    return pos;
}
 
int __legacy_relay_t_decode_array_cleanup(legacy_relay_t *p, int elements)
{
    int element;
    for (element = 0; element < elements; element++) {
 
        __string_decode_array_cleanup(&(p[element].label), 1);
 
        __string_decode_array_cleanup(&(p[element].group), 1);
 
        __boolean_decode_array_cleanup(&(p[element].state), 1);
 
    }
    return 0;
}
 
int legacy_relay_t_decode(const void *buf, int offset, int maxlen, legacy_relay_t *p)
{
    int pos = 0, thislen;
    int64_t hash = __legacy_relay_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 = __legacy_relay_t_decode_array(buf, offset + pos, maxlen - pos, p, 1);
    if (thislen < 0) return thislen; else pos += thislen;
 
    return pos;
}
 
int legacy_relay_t_decode_cleanup(legacy_relay_t *p)
{
    return __legacy_relay_t_decode_array_cleanup(p, 1);
}
 
int __legacy_relay_t_clone_array(const legacy_relay_t *p, legacy_relay_t *q, int elements)
{
    int element;
    for (element = 0; element < elements; element++) {
 
        __string_clone_array(&(p[element].label), &(q[element].label), 1);
 
        __string_clone_array(&(p[element].group), &(q[element].group), 1);
 
        __boolean_clone_array(&(p[element].state), &(q[element].state), 1);
 
    }
    return 0;
}
 
legacy_relay_t *legacy_relay_t_copy(const legacy_relay_t *p)
{
    legacy_relay_t *q = (legacy_relay_t*) malloc(sizeof(legacy_relay_t));
    __legacy_relay_t_clone_array(p, q, 1);
    return q;
}
 
void legacy_relay_t_destroy(legacy_relay_t *p)
{
    __legacy_relay_t_decode_array_cleanup(p, 1);
    free(p);
}
 
int legacy_relay_t_publish(lcm_t *lc, const char *channel, const legacy_relay_t *p)
{
      int max_data_size = legacy_relay_t_encoded_size (p);
      uint8_t *buf = (uint8_t*) malloc (max_data_size);
      if (!buf) return -1;
      int data_size = legacy_relay_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 _legacy_relay_t_subscription_t {
    legacy_relay_t_handler_t user_handler;
    void *userdata;
    lcm_subscription_t *lc_h;
};
static
void legacy_relay_t_handler_stub (const lcm_recv_buf_t *rbuf, 
                            const char *channel, void *userdata)
{
    int status;
    legacy_relay_t p;
    memset(&p, 0, sizeof(legacy_relay_t));
    status = legacy_relay_t_decode (rbuf->data, 0, rbuf->data_size, &p);
    if (status < 0) {
        fprintf (stderr, "error %d decoding legacy_relay_t!!!\n", status);
        return;
    }

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

    legacy_relay_t_decode_cleanup (&p);
}

legacy_relay_t_subscription_t* legacy_relay_t_subscribe (lcm_t *lcm, 
                    const char *channel, 
                    legacy_relay_t_handler_t f, void *userdata)
{
    legacy_relay_t_subscription_t *n = (legacy_relay_t_subscription_t*)
                       malloc(sizeof(legacy_relay_t_subscription_t));
    n->user_handler = f;
    n->userdata = userdata;
    n->lc_h = lcm_subscribe (lcm, channel, 
                                 legacy_relay_t_handler_stub, n);
    if (n->lc_h == NULL) {
        fprintf (stderr,"couldn't reg legacy_relay_t LCM handler!\n");
        free (n);
        return NULL;
    }
    return n;
}

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

