/**
 * @file comms_uhf_rx.c
 *
 * @brief #TODO
 *
 * @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.
*/

#include <stdio.h>
#include "comms_uhf_main.h"
#include "comms_uhf_api_map.h"

#define UHF_CARRIER_LOCKED  (0x1 << 0) ///<Bitmask for carrier locked status
#define UHF_FRAME_LOCKED    (0x1 << 1) ///<Bitmask for frame locked status
#define UHF_RX_DETECTED     (0x1 << 2) ///<Bitmask for packet detected status
#define UHF_RX_DECODED      (0x1 << 3) ///<Bitmask for packet decoded status

/**
 * @brief This function is used to fetch the carrier lock and frame lock information from the UHF module
 *
 * @brief Check UHF RX link status
 * 
 * This function performs a link check on the UHF RX to determine if 
 * the link is up and receiving packets.
 *
 * It checks factors like:
 * - RX lock status 
 * - Presence of sync word
 * - Packet CRC errors  
 * - Received signal strength
 *
 * Based on these factors, it determines if the link is up or down.
 * 
 * @return uint8_t Link status
 * @retval 0 Link is down
 * @retval 1 Link is up
 * 
 */
uint8_t comms_uhf_rx_link_check(void)
{
    uint8_t rx_sts = 0;
#if 0
    uint16_t size = 0;

    s_comms_uhf_rx_tm  *rx_tm = (s_comms_uhf_rx_tm *)&rx_cfg_global.rx_cfg_frames;

    //SDR_UHF_GET_RX_TM_PROP(rx_tm, &size);

    if(rx_tm->rx_cfg_carrier_lock)
    {
        rx_sts |= UHF_CARRIER_LOCKED;
    }

    if(rx_tm->rx_frame_lock)
    {
        rx_sts |= UHF_FRAME_LOCKED;
    }

    if(rx_tm->rx_cfg_detected)
    {
        rx_sts |= UHF_RX_DETECTED;
    }

    if(rx_tm->rx_cfg_frames)
    {
        rx_sts |= UHF_RX_DECODED;
    }

#endif
    return rx_sts;
}
