/*
|
* 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_misc.h"
|
#include "mk_power.h"
|
#include "uwb_api.h"
|
#include "uci_tl_task.h"
|
#include "libc_rom.h"
|
#include "uci_tl_comm.h"
|
|
static uci_tl_dev_t *uci_tl_dev;
|
|
extern void uci_rsp_callback_init(void (*callback)(uint8_t, uint8_t *, uint16_t));
|
extern uint8_t uci_report_dev_status(void);
|
extern void uci_rx_msg_process(const uint8_t *buf, uint32_t len, void (*callback)(uint8_t MT, uint8_t *pkt, uint16_t pkt_len));
|
|
static wsfHandlerId_t uci_tl_handle_id;
|
|
static void uci_tl_rx_event_set(void)
|
{
|
WsfSetEvent(uci_tl_handle_id, UCI_TL_RX_EVT);
|
}
|
|
static void uci_tl_tx_event_set(void)
|
{
|
WsfSetEvent(uci_tl_handle_id, UCI_TL_TX_EVT);
|
}
|
|
static void uci_tl_chk_busy_event_set(void)
|
{
|
WsfSetEvent(uci_tl_handle_id, UCI_TL_CHK_BUSY_EVT);
|
}
|
|
static void uci_tl_down_notify(void)
|
{
|
uci_tl_rx_event_set();
|
}
|
|
static void uci_tl_up_done_notify(void)
|
{
|
uci_tl_chk_busy_event_set();
|
}
|
|
static void uci_rx_process_cb(uint8_t msg_type, uint8_t *buf, uint16_t len)
|
{
|
ASSERT(((buf != NULL) || (0x00 != len)), "uci tl input param invalid\r\n");
|
|
uint16_t count = WsfQueueCount(&uci_tl_dev->tl_up_queue);
|
|
if (count < UCI_MAX_UL_ITEMS)
|
{
|
struct UCI_TL_MSG_T *p = WsfBufAlloc((uint16_t)(len + sizeof(struct UCI_TL_MSG_T)));
|
|
if (p != NULL)
|
{
|
p->msg_length = len;
|
memcpy(p->msg, buf, len);
|
WsfQueueEnq(&uci_tl_dev->tl_up_queue, p);
|
}
|
else
|
{
|
LOG_INFO(TRACE_MODULE_UCI, "No buff to up queue\r\n");
|
}
|
}
|
else
|
{
|
LOG_INFO(TRACE_MODULE_UCI, "Up queue is full %d\r\n", count);
|
}
|
|
uci_tl_tx_event_set();
|
}
|
|
int uci_tl_init(uint8_t handle_id)
|
{
|
uci_tl_handle_id = handle_id;
|
uci_tl_dev = &g_uci_tl_dev;
|
uci_tl_dev->uci_tl_down_notify = &uci_tl_down_notify;
|
uci_tl_dev->uci_tl_up_done_notify = &uci_tl_up_done_notify;
|
uci_tl_dev->tl_timer.handlerId = handle_id;
|
uci_tl_dev->tl_timer.msg.event = UCI_TL_TIMEOUT_MSG;
|
WSF_QUEUE_INIT(&uci_tl_dev->tl_down_queue);
|
WSF_QUEUE_INIT(&uci_tl_dev->tl_up_queue);
|
uci_tl_dev->uci_tl_setup();
|
|
// send notification to host that the UWBS is ready
|
uci_rsp_callback_init(uci_rx_process_cb);
|
uci_report_dev_status();
|
|
return 0;
|
}
|
|
void uci_tl_resume(void)
|
{
|
if (uci_tl_dev->uci_tl_resume)
|
{
|
uci_tl_dev->uci_tl_resume();
|
}
|
}
|
|
/*************************************************************************************************/
|
/*!
|
* \brief WSF event handler for UCI transmission layer task.
|
*
|
* \param event WSF event mask.
|
* \param msg WSF message.
|
*
|
* \return None.
|
*/
|
/*************************************************************************************************/
|
void uci_tl_handler(wsfEventMask_t event, const void *param)
|
{
|
const wsfMsgHdr_t *msg = (const wsfMsgHdr_t *)param;
|
|
if (msg != NULL)
|
{
|
switch (msg->event)
|
{
|
case UCI_TL_TIMEOUT_MSG:
|
if (uci_tl_dev->uci_tl_timer_notify != NULL)
|
{
|
uci_tl_dev->uci_tl_timer_notify();
|
}
|
break;
|
|
default:
|
break;
|
}
|
}
|
else
|
{
|
if (event & UCI_TL_RX_EVT)
|
{
|
struct UCI_TL_MSG_T *tl_down_msg;
|
|
/* Get messages from the down queue */
|
tl_down_msg = WsfQueueDeq(&uci_tl_dev->tl_down_queue);
|
if (tl_down_msg != NULL)
|
{
|
uci_rx_msg_process(tl_down_msg->msg, tl_down_msg->msg_length, uci_rx_process_cb);
|
WsfBufFree(tl_down_msg);
|
if (!WsfQueueEmpty(&g_uci_tl_dev.tl_down_queue))
|
{
|
if (uci_tl_dev->uci_tl_down_notify != NULL)
|
{
|
uci_tl_dev->uci_tl_down_notify();
|
}
|
}
|
}
|
else
|
{
|
LOG_INFO(TRACE_MODULE_UCI, "Dn queue is empty\r\n");
|
}
|
}
|
|
if (event & UCI_TL_TX_EVT)
|
{
|
if (uci_tl_dev->uci_tl_up_req != NULL)
|
{
|
uci_tl_dev->uci_tl_up_req();
|
}
|
}
|
|
if (event & UCI_TL_CHK_BUSY_EVT)
|
{
|
if (uci_tl_dev->uci_tl_up_is_active != NULL)
|
{
|
if (uci_tl_dev->uci_tl_up_is_active())
|
{
|
uci_tl_chk_busy_event_set();
|
}
|
else
|
{
|
if (uci_post_process_flag == 1)
|
{
|
sys_reset(0);
|
}
|
else if (uci_post_process_flag == 2)
|
{
|
power_enter_shelf_mode();
|
}
|
|
if (!WsfQueueEmpty(&g_uci_tl_dev.tl_up_queue))
|
{
|
if (uci_tl_dev->uci_tl_up_req != NULL)
|
{
|
uci_tl_dev->uci_tl_up_req();
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|