/*
 * LEIGH:plc
 * Copyright 2022 Alex Leigh
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _SACN_H
#define _SACN_H

typedef union
{
    struct
    {
        struct
        {                            /* ACN Root Layer: 38 bytes */
            uint16_t preamble_size;  /* Preamble Size */
            uint16_t postamble_size; /* Post-amble Size */
            uint8_t acn_pid[12];     /* ACN Packet Identifier */
            uint16_t flength;        /* Flags (high 4 bits) & Length (low 12 bits) */
            uint32_t vector;         /* Layer Vector */
            uint8_t cid[16];         /* Component Identifier (UUID) */
        } __attribute__((packed)) root;

        struct
        {                            /* Framing Layer: 77 bytes */
            uint16_t flength;        /* Flags (high 4 bits) & Length (low 12 bits) */
            uint32_t vector;         /* Layer Vector */
            char source_name[64];    /* User Assigned Name of Source (UTF-8) null terminated */
            uint8_t priority;        /* Packet Priority (0-200, default 100) */
            uint16_t reserved;       /* Reserved (should be always 0) */
            uint8_t seq_number;      /* Sequence Number (detect duplicates or out of order packets) */
            uint8_t options;         /* Options Flags (bit 7: preview data, bit 6: stream terminated) */
            uint16_t universe;       /* DMX Universe Number */
        } __attribute__((packed)) frame;

        struct
        {                          /* Device Management Protocol (DMP) Layer: 523 bytes */
            uint16_t flength;      /* Flags (high 4 bits) / Length (low 12 bits) */
            uint8_t vector;        /* Layer Vector */
            uint8_t type;          /* Address Type & Data Type */
            uint16_t first_addr;   /* First Property Address */
            uint16_t addr_inc;     /* Address Increment */
            uint16_t prop_val_cnt; /* Property Value Count (1 + number of slots) */
            uint8_t prop_val[513]; /* Property Values (DMX start code + slots data) */
        } __attribute__((packed)) dmp;
    } __attribute__((packed));

    uint8_t raw[638]; /* raw buffer view: 638 bytes */
} sacnTe131_packet;

void sacnFinit();
void sacnFtask();
void sacnF_watchdog_task();

#endif