/* * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and * its subsidiaries and affiliates (collectly called MKSEMI). * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form, except as embedded into an MKSEMI * integrated circuit in a product or a software update for such product, * must reproduce the above copyright notice, this list of conditions and * the following disclaimer in the documentation and/or other materials * provided with the distribution. * * 3. Neither the name of MKSEMI nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * 4. This software, with or without modification, must only be used with a * MKSEMI integrated circuit. * * 5. Any software provided in binary form under this license must not be * reverse engineered, decompiled, modified and/or disassembled. * * THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "mk_trace.h" #include "mk_power.h" #include "mk_uwb.h" #include "uwb_test.h" #include "wsf_msg.h" #include "uwb_api.h" #include "lib_aoa.h" #include "lib_ranging.h" #include "crc.h" #ifdef FIRA_RANGING_EN #include "lib_fira.h" #else static uint8_t uwb_tx_buf[PHY_PAYLOAD_LEN_MAX]; #endif static struct UWB_TEST_CB_T uwb_test_cb; static void uwb_test_tx_process(struct MAC_HW_REPORT_T *tx_report); static void uwb_test_rx_process(struct MAC_HW_REPORT_T *rx_report); void test_session_init(void); int uwb_test_init(uint8_t handle_id) { /* store handler ID */ uwb_test_cb.handle_id = handle_id; /* init rx queue */ WSF_QUEUE_INIT(&uwb_test_cb.msg_queue); return 0; } int uwb_test_deinit(void) { return 0; } // This function will be called by uwbapi_session_init() void test_session_init(void) { #ifdef FIRA_RANGING_EN uwbs_test_pkt_buffer_set(fira_uwb_tx_buf); #else uwbs_test_pkt_buffer_set(uwb_tx_buf); #endif // register process handler for MAC TX done and RX done mac_register_process_handler(uwb_test_tx_process, uwb_test_rx_process); uwbs_handler_init(NULL); } static void uwb_test_tx_done_ind(struct MAC_HW_REPORT_T *tx) { struct UWB_TEST_TX_DONE_IND_T *ind; uint8_t fcs_len = (uwb_app_config.ppdu_params.fcs_type == FCS_CRC_16 ? 2 : 4); if ((ind = WsfMsgAlloc(sizeof(struct UWB_TEST_TX_DONE_IND_T) + tx->pkt_len + fcs_len)) != NULL) { ind->hdr.event = UWB_TEST_TX_DONE_MSG; ind->tx.phy_timer_count = tx->timestamp; ind->tx.status = tx->err_code; if (tx->err_code == UWB_TX_OK) { int64_t timestamp = ranging_tx_time(tx->timestamp); ind->tx.timestamp_frac = timestamp & 0x1ff; ind->tx.timestamp_int = (uint32_t)(timestamp >> 9); } if (uwb_app_config.ppdu_params.sts_pkt_cfg == SP3) { ind->tx.length = tx->pkt_len; ind->tx.data = NULL; } else { ind->tx.length = tx->pkt_len + fcs_len; ind->tx.data = &ind->buffer[0]; if (tx->pkt_data != NULL) { memcpy(ind->tx.data, tx->pkt_data, tx->pkt_len); } if (uwb_app_config.ppdu_params.fcs_type == FCS_CRC_16) { uint16_t fcs = fcs_crc_16(tx->pkt_data, tx->pkt_len); ind->tx.data[tx->pkt_len] = fcs & 0xff; ind->tx.data[tx->pkt_len + 1] = (fcs >> 8) & 0xff; } else { uint32_t crc = crc32(tx->pkt_data, tx->pkt_len); ind->tx.data[tx->pkt_len] = crc & 0xff; ind->tx.data[tx->pkt_len + 1] = (crc >> 8) & 0xff; ind->tx.data[tx->pkt_len + 2] = (crc >> 16) & 0xff; ind->tx.data[tx->pkt_len + 3] = (crc >> 24) & 0xff; } } // Send the message WsfMsgSend(uwb_test_cb.handle_id, ind); } else { LOG_WARNING(TRACE_MODULE_UWB, "memory is not enough for UWB_TEST_TX_DONE_IND_T\r\n"); } } static void uwb_test_rx_done_ind(struct MAC_HW_REPORT_T *rx) { // send an indication to application struct UWB_TEST_RX_DONE_IND_T *ind; uint8_t fcs_len = (uwb_app_config.ppdu_params.fcs_type == FCS_CRC_16 ? 2 : 4); if ((ind = WsfMsgAlloc(sizeof(struct UWB_TEST_RX_DONE_IND_T) + rx->pkt_len + fcs_len)) != NULL) { ind->hdr.event = UWB_TEST_RX_DONE_MSG; ind->rx.phy_timer_count = rx->timestamp; ind->rx.phr_bits = (uint16_t)rx->phy_header; ind->rx.status = rx->err_code; ind->rx.rssi = rx->rssi; ind->rx.snr = rx->snr; if (rx->err_code == UWB_RX_OK) { uint8_t sts_pkt_cfg = (rx->phy_header >> 28) & 0x3; if ((sts_pkt_cfg != SP0) && (sts_valid_check() == 0)) { ind->rx.status |= UWB_STS_ERR; } if (ind->rx.status == UWB_RX_OK) { int64_t timestamp = ranging_rx_time(rx); ind->rx.timestamp_frac = timestamp & 0x1ff; ind->rx.timestamp_int = (uint32_t)(timestamp >> 9); if (sts_pkt_cfg == SP3) { ind->rx.length = rx->pkt_len; ind->rx.data = NULL; } else { ind->rx.length = rx->pkt_len + fcs_len; ind->rx.data = &ind->buffer[0]; if (rx->pkt_data != NULL) { memcpy(ind->rx.data, rx->pkt_data, rx->pkt_len); } if (uwb_app_config.ppdu_params.fcs_type == FCS_CRC_16) { uint16_t fcs = fcs_crc_16(rx->pkt_data, rx->pkt_len); ind->rx.data[rx->pkt_len] = fcs & 0xff; ind->rx.data[rx->pkt_len + 1] = (fcs >> 8) & 0xff; } else { uint32_t crc = crc32(rx->pkt_data, rx->pkt_len); ind->rx.data[rx->pkt_len] = crc & 0xff; ind->rx.data[rx->pkt_len + 1] = (crc >> 8) & 0xff; ind->rx.data[rx->pkt_len + 2] = (crc >> 16) & 0xff; ind->rx.data[rx->pkt_len + 3] = (crc >> 24) & 0xff; } } } } // Send the message WsfMsgSend(uwb_test_cb.handle_id, ind); } else { LOG_WARNING(TRACE_MODULE_UWB, "memory is not enough for UWB_TEST_RX_DONE_IND_T\r\n"); } } void uwb_test_tx_process(struct MAC_HW_REPORT_T *tx_report) { uwb_test_tx_done_ind(tx_report); } void uwb_test_rx_process(struct MAC_HW_REPORT_T *rx_report) { uwb_test_rx_done_ind(rx_report); }