• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <arch/sys_arch.h>
33 #include <lwip/sys.h>
34 #include <lwip/debug.h>
35 #include <los_task.h>
36 #include <los_sys_pri.h>
37 #include <los_tick.h>
38 #include <los_queue.h>
39 #include <los_sem.h>
40 #include <los_mux.h>
41 #include <los_spinlock.h>
42 
43 #ifdef LOSCFG_KERNEL_SMP
44 SPIN_LOCK_INIT(arch_protect_spin);
45 static u32_t lwprot_thread = LOS_ERRNO_TSK_ID_INVALID;
46 static int lwprot_count = 0;
47 #endif /* LOSCFG_KERNEL_SMP */
48 
49 #define ROUND_UP_DIV(val, div) (((val) + (div) - 1) / (div))
50 
51 /**
52  * Thread and System misc
53  */
54 
sys_thread_new(const char * name,lwip_thread_fn thread,void * arg,int stackSize,int prio)55 sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stackSize, int prio)
56 {
57     UINT32 taskID = LOS_ERRNO_TSK_ID_INVALID;
58     UINT32 ret;
59     TSK_INIT_PARAM_S task = {0};
60 
61     /* Create host Task */
62     task.pfnTaskEntry = (TSK_ENTRY_FUNC)thread;
63     task.uwStackSize = stackSize;
64     task.pcName = (char *)name;
65     task.usTaskPrio = prio;
66     task.auwArgs[0] = (UINTPTR)arg;
67     task.uwResved = LOS_TASK_STATUS_DETACHED;
68     ret = LOS_TaskCreate(&taskID, &task);
69     if (ret != LOS_OK) {
70         LWIP_DEBUGF(SYS_DEBUG, ("sys_thread_new: LOS_TaskCreate error %u\n", ret));
71         return -1;
72     }
73 
74     return taskID;
75 }
76 
sys_init(void)77 void sys_init(void)
78 {
79     /* set rand seed to make random sequence diff on every startup */
80     UINT32 seedhsb, seedlsb;
81     LOS_GetCpuCycle(&seedhsb, &seedlsb);
82     srand(seedlsb);
83 }
84 
sys_now(void)85 u32_t sys_now(void)
86 {
87     /* Lwip docs mentioned like wraparound is not a problem in this function */
88     return (u32_t)((LOS_TickCountGet() * OS_SYS_MS_PER_SECOND) / LOSCFG_BASE_CORE_TICK_PER_SECOND);
89 }
90 
91 #if (LWIP_CHKSUM_ALGORITHM == 4) /* version #4, asm based */
92 #include "in_cksum.h"
lwip_standard_chksum(const void * dataptr,int len)93 u16_t lwip_standard_chksum(const void *dataptr, int len)
94 {
95     return ~(u16_t)(in_cksum(dataptr, len));
96 }
97 #endif
98 
99 
100 /**
101  * Protector
102  */
103 
sys_arch_protect(void)104 sys_prot_t sys_arch_protect(void)
105 {
106 #ifdef LOSCFG_KERNEL_SMP
107     /* Note that we are using spinlock instead of mutex for LiteOS-SMP here:
108      * 1. spinlock is more effective for short critical region protection.
109      * 2. this function is called only in task context, not in interrupt handler.
110      *    so it's not needed to disable interrupt.
111      */
112     if (lwprot_thread != LOS_CurTaskIDGet()) {
113         /* We are locking the spinlock where it has not been locked before
114          * or is being locked by another thread */
115         LOS_SpinLock(&arch_protect_spin);
116         lwprot_thread = LOS_CurTaskIDGet();
117         lwprot_count = 1;
118     } else {
119         /* It is already locked by THIS thread */
120         lwprot_count++;
121     }
122 #else
123     LOS_TaskLock();
124 #endif /* LOSCFG_KERNEL_SMP */
125     return 0; /* return value is unused */
126 }
127 
sys_arch_unprotect(sys_prot_t pval)128 void sys_arch_unprotect(sys_prot_t pval)
129 {
130     LWIP_UNUSED_ARG(pval);
131 #ifdef LOSCFG_KERNEL_SMP
132     if (lwprot_thread == LOS_CurTaskIDGet()) {
133         lwprot_count--;
134         if (lwprot_count == 0) {
135             lwprot_thread = LOS_ERRNO_TSK_ID_INVALID;
136             LOS_SpinUnlock(&arch_protect_spin);
137         }
138     }
139 #else
140     LOS_TaskUnlock();
141 #endif /* LOSCFG_KERNEL_SMP */
142 }
143 
144 
145 /**
146  * MessageBox
147  */
148 
sys_mbox_new(sys_mbox_t * mbox,int size)149 err_t sys_mbox_new(sys_mbox_t *mbox, int size)
150 {
151     CHAR qName[] = "lwIP";
152     UINT32 ret = LOS_QueueCreate(qName, (UINT16)size, mbox, 0, sizeof(void *));
153     switch (ret) {
154         case LOS_OK:
155             return ERR_OK;
156         case LOS_ERRNO_QUEUE_CB_UNAVAILABLE:
157         case LOS_ERRNO_QUEUE_CREATE_NO_MEMORY:
158             return ERR_MEM;
159         default:
160             break;
161     }
162     LWIP_DEBUGF(SYS_DEBUG, ("%s: LOS_QueueCreate error %u\n", __FUNCTION__, ret));
163     return ERR_ARG;
164 }
165 
sys_mbox_post(sys_mbox_t * mbox,void * msg)166 void sys_mbox_post(sys_mbox_t *mbox, void *msg)
167 {
168     /* Caution: the second parameter is NOT &msg */
169     UINT32 ret = LOS_QueueWrite(*mbox, msg, sizeof(char *), LOS_WAIT_FOREVER);
170     if (ret != LOS_OK) {
171         LWIP_DEBUGF(SYS_DEBUG, ("%s: LOS_QueueWrite error %u\n", __FUNCTION__, ret));
172     }
173 }
174 
sys_mbox_trypost(sys_mbox_t * mbox,void * msg)175 err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
176 {
177     /* Caution: the second parameter is NOT &msg */
178     UINT32 ret = LOS_QueueWrite(*mbox, msg, sizeof(char *), 0);
179     switch (ret) {
180         case LOS_OK:
181             return ERR_OK;
182         case LOS_ERRNO_QUEUE_ISFULL:
183             return ERR_MEM;
184         default:
185             break;
186     }
187     LWIP_DEBUGF(SYS_DEBUG, ("%s: LOS_QueueWrite error %u\n", __FUNCTION__, ret));
188     return ERR_ARG;
189 }
190 
191 err_t sys_mbox_trypost_fromisr(sys_mbox_t *mbox, void *msg);
192 
sys_arch_mbox_fetch(sys_mbox_t * mbox,void ** msg,u32_t timeoutMs)193 u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeoutMs)
194 {
195     void *ignore = 0; /* if msg==NULL, the fetched msg should be dropped */
196     UINT64 tick = ROUND_UP_DIV((UINT64)timeoutMs * LOSCFG_BASE_CORE_TICK_PER_SECOND, OS_SYS_MS_PER_SECOND);
197     UINT32 ret = LOS_QueueRead(*mbox, msg ? msg : &ignore, sizeof(void *), tick ? (UINT32)tick : LOS_WAIT_FOREVER);
198     switch (ret) {
199         case LOS_OK:
200             return ERR_OK;
201         case LOS_ERRNO_QUEUE_ISEMPTY:
202         case LOS_ERRNO_QUEUE_TIMEOUT:
203             return SYS_ARCH_TIMEOUT;
204         default:
205             break;
206     }
207     LWIP_DEBUGF(SYS_DEBUG, ("%s: LOS_QueueRead error %u\n", __FUNCTION__, ret));
208     return SYS_ARCH_TIMEOUT; /* Errors should be treated as timeout */
209 }
210 
sys_arch_mbox_tryfetch(sys_mbox_t * mbox,void ** msg)211 u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
212 {
213     void *ignore = 0; /* if msg==NULL, the fetched msg should be dropped */
214     UINT32 ret = LOS_QueueRead(*mbox, msg ? msg : &ignore, sizeof(void *), 0);
215     switch (ret) {
216         case LOS_OK:
217             return ERR_OK;
218         case LOS_ERRNO_QUEUE_ISEMPTY:
219             return SYS_MBOX_EMPTY;
220         case LOS_ERRNO_QUEUE_TIMEOUT:
221             return SYS_ARCH_TIMEOUT;
222         default:
223             break;
224     }
225     LWIP_DEBUGF(SYS_DEBUG, ("%s: LOS_QueueRead error %u\n", __FUNCTION__, ret));
226     return SYS_MBOX_EMPTY; /* Errors should be treated as timeout */
227 }
228 
sys_mbox_free(sys_mbox_t * mbox)229 void sys_mbox_free(sys_mbox_t *mbox)
230 {
231     (void)LOS_QueueDelete(*mbox);
232 }
233 
sys_mbox_valid(sys_mbox_t * mbox)234 int sys_mbox_valid(sys_mbox_t *mbox)
235 {
236     QUEUE_INFO_S queueInfo;
237     return LOS_OK == LOS_QueueInfoGet(*mbox, &queueInfo);
238 }
239 
sys_mbox_set_invalid(sys_mbox_t * mbox)240 void sys_mbox_set_invalid(sys_mbox_t *mbox)
241 {
242     *mbox = LOSCFG_BASE_IPC_QUEUE_LIMIT;
243 }
244 
245 
246 /**
247  * Semaphore
248  */
249 
sys_sem_new(sys_sem_t * sem,u8_t count)250 err_t sys_sem_new(sys_sem_t *sem, u8_t count)
251 {
252     UINT32 ret = LOS_SemCreate(count, sem);
253     if (ret != LOS_OK) {
254         return ERR_ARG;
255     }
256 
257     return ERR_OK;
258 }
259 
sys_sem_signal(sys_sem_t * sem)260 void sys_sem_signal(sys_sem_t *sem)
261 {
262     (void)LOS_SemPost(*sem);
263 }
264 
sys_arch_sem_wait(sys_sem_t * sem,u32_t timeoutMs)265 u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeoutMs)
266 {
267     UINT64 tick = ROUND_UP_DIV((UINT64)timeoutMs * LOSCFG_BASE_CORE_TICK_PER_SECOND, OS_SYS_MS_PER_SECOND);
268     UINT32 ret = LOS_SemPend(*sem, tick ? (UINT32)tick : LOS_WAIT_FOREVER); // timeoutMs 0 means wait forever
269     switch (ret) {
270         case LOS_OK:
271             return ERR_OK;
272         case LOS_ERRNO_SEM_TIMEOUT:
273             return SYS_ARCH_TIMEOUT;
274         default:
275             break;
276     }
277     LWIP_DEBUGF(SYS_DEBUG, ("%s: LOS_SemPend error %u\n", __FUNCTION__, ret));
278     return SYS_ARCH_TIMEOUT; /* Errors should be treated as timeout */
279 }
280 
sys_sem_free(sys_sem_t * sem)281 void sys_sem_free(sys_sem_t *sem)
282 {
283     (void)LOS_SemDelete(*sem);
284 }
285 
sys_sem_valid(sys_sem_t * sem)286 int sys_sem_valid(sys_sem_t *sem)
287 {
288     return *sem != LOSCFG_BASE_IPC_SEM_LIMIT;
289 }
290 
sys_sem_set_invalid(sys_sem_t * sem)291 void sys_sem_set_invalid(sys_sem_t *sem)
292 {
293     *sem = LOSCFG_BASE_IPC_SEM_LIMIT;
294 }
295 
296 
297 /**
298  * Mutex
299  */
300 
sys_mutex_new(sys_mutex_t * mutex)301 err_t sys_mutex_new(sys_mutex_t *mutex)
302 {
303     UINT32 ret = LOS_MuxInit(mutex, NULL);
304     if (ret != LOS_OK) {
305         return ERR_ARG;
306     }
307 
308     return ERR_OK;
309 }
310 
sys_mutex_lock(sys_mutex_t * mutex)311 void sys_mutex_lock(sys_mutex_t *mutex)
312 {
313     (void)LOS_MuxLock(mutex, LOS_WAIT_FOREVER);
314 }
315 
sys_mutex_unlock(sys_mutex_t * mutex)316 void sys_mutex_unlock(sys_mutex_t *mutex)
317 {
318     (void)LOS_MuxUnlock(mutex);
319 }
320 
sys_mutex_free(sys_mutex_t * mutex)321 void sys_mutex_free(sys_mutex_t *mutex)
322 {
323     (void)LOS_MuxDestroy(mutex);
324 }
325 
sys_mutex_valid(sys_mutex_t * mutex)326 int sys_mutex_valid(sys_mutex_t *mutex)
327 {
328     return LOS_MuxIsValid(mutex);
329 }
330 
sys_mutex_set_invalid(sys_mutex_t * mutex)331 void sys_mutex_set_invalid(sys_mutex_t *mutex)
332 {
333     (void)LOS_MuxDestroy(mutex);
334 }
335