/**
 * @file csp_comms.h
 *
 * @brief This file contains function prototypes and types that is used
 *        for sending and receiving data through can interface
 *
 * @copyright Copyright 2023 Antaris, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef CSP_COMMS_H_
#define CSP_COMMS_H_

#include <stdint.h>
#include <errno.h>

#define UHF_UART_MTU 245

#define UHF_DEFAULT_TIMEOUT 1000
#define UHF_START_BYTE_0     0x22           /** First start byte  */
#define UHF_START_BYTE_1     0x69           /** Second start byte  */
#define UHF_HEADER_SIZE      6
#define UHF_MAX_PAYLOAD      251
#define UHF_ADC_NUM_CHANNELS 10

/**
 *   * @brief Socket option for comms
 *     */
typedef enum
{
    S1_SBAND_HWIL_GS1,
    S2_DEFAULT_SBAND_GS2,
    S3_UHF_HWIL_GS3
}e_comms_uhf_param_opt_sts;

typedef enum {
    bootloader_msg_ping        = 0x00,
    bootloader_msg_ack         = 0x01,
    bootloader_msg_write_page  = 0x02,
    bootloader_msg_erase       = 0x0c,
    bootloader_msg_nack        = 0x0f,
    cmn_msg_ack                = 0x10,
    cmn_msg_ascii              = 0x11,
    radio_msg_reboot           = 0x12,
    radio_msg_get_time         = 0x13,
    radio_msg_set_time         = 0x14,
    radio_msg_ranging          = 0x15,
    radio_msg_ranging_ack      = 0x16,
    radio_msg_get_telem        = 0x17,
    radio_msg_telem            = 0x18,
    radio_msg_get_callsign     = 0x19,
    radio_msg_set_callsign     = 0x1a,
    radio_msg_callsign         = 0x1b,
    cmn_msg_nack               = 0xff
}e_comms_uhf_msg_no;

typedef struct __attribute__((packed))
{
    uint8_t sync1;
    uint8_t sync2;
    uint8_t length;
    uint16_t hwid;
    uint16_t seqnum;
    uint8_t system;
    uint8_t command;
}s_comms_uhf_header;

typedef struct __attribute__((packed))
{
    s_comms_uhf_header header;
    uint8_t data[UHF_MAX_PAYLOAD - sizeof(s_comms_uhf_header)];
}s_comms_uhf_data;

typedef union command_buffer
{
    uint8_t msg[UHF_MAX_PAYLOAD];
    s_comms_uhf_data cmd;
}u_comms_uhf_command_buffer_t;

typedef struct
{
    uint8_t reserved;
    uint32_t uptime;
    uint32_t uart0_rx_count;
    uint32_t uart1_rx_count;
    uint8_t  rx_mode;
    uint8_t  tx_mode;
    int16_t  adc[UHF_ADC_NUM_CHANNELS];
    int8_t   last_rssi;
    uint8_t  last_lqi;
    int8_t   last_freqest;
    uint32_t packets_sent;
    uint32_t cs_count;
    uint32_t packets_good;
    uint32_t packets_rejected_checksum;
    uint32_t packets_rejected_reserved;
    uint32_t packets_rejected_other;
    uint32_t reserved0;
    uint32_t reserved1;
    uint32_t custom0;
    uint32_t custom1;

}s_comms_uhf_tm_prop;

#endif /* CSP_COMMS_H_ */

