chen
2024-11-01 631a90c1116fa33382a88a747c89bf761bc0fa9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * 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();
                        }
                    }
                }
            }
        }
    }
}