• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2022 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_queue.h>
37 #include <los_sem.h>
38 #include <los_mux.h>
39 #include <los_config.h>
40 
41 #ifndef LOSCFG_KERNEL_SMP
42 #define LOSCFG_KERNEL_SMP 0
43 #endif
44 
45 #if (LOSCFG_KERNEL_SMP)
46 SPIN_LOCK_INIT(arch_protect_spin);
47 static u32_t lwprot_thread = LOS_ERRNO_TSK_ID_INVALID;
48 static int lwprot_count = 0;
49 #endif /* LOSCFG_KERNEL_SMP */
50 
51 #define ROUND_UP_DIV(val, div) (((val) + (div) - 1) / (div))
52 #define LWIP_LOG_BUF_SIZE 64
53 
54 /**
55  * Thread and System misc
56  */
sys_thread_new(const char * name,lwip_thread_fn thread,void * arg,int stackSize,int prio)57 sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stackSize, int prio)
58 {
59     UINT32 taskID = LOS_ERRNO_TSK_ID_INVALID;
60     UINT32 ret;
61     TSK_INIT_PARAM_S task = {0};
62 
63     if (name == NULL || (strlen(name) == 0)) {
64         LWIP_DEBUGF(SYS_DEBUG, ("sys_thread_new: name is null"));
65         return -1;
66     }
67 
68     /* Create host Task */
69     task.pfnTaskEntry = (TSK_ENTRY_FUNC)thread;
70     task.uwStackSize = stackSize;
71     task.pcName = (char *)name;
72     task.usTaskPrio = prio;
73     task.uwArg = (UINTPTR)arg;
74     task.uwResved = LOS_TASK_STATUS_DETACHED;
75     ret = LOS_TaskCreate(&taskID, &task);
76     if (ret != LOS_OK) {
77         LWIP_DEBUGF(SYS_DEBUG, ("sys_thread_new: LOS_TaskCreate error %u\n", ret));
78         return -1;
79     }
80 
81     return taskID;
82 }
83 
sys_init(void)84 void sys_init(void)
85 {
86     /* set rand seed to make random sequence diff on every startup */
87     UINT32 seedlsb = (UINT32)LOS_SysCycleGet();
88     srand(seedlsb);
89 }
90 
sys_now(void)91 u32_t sys_now(void)
92 {
93     /* Lwip docs mentioned like wraparound is not a problem in this function */
94     return (u32_t)((LOS_TickCountGet() * OS_SYS_MS_PER_SECOND) /
95             LOSCFG_BASE_CORE_TICK_PER_SECOND);
96 }
97 
98 /**
99  * Protector
100  */
sys_arch_protect(void)101 sys_prot_t sys_arch_protect(void)
102 {
103 #if (LOSCFG_KERNEL_SMP)
104     /* Note that we are using spinlock instead of mutex for LiteOS-SMP here:
105      * 1. spinlock is more effective for short critical region protection.
106      * 2. this function is called only in task context, not in interrupt handler.
107      *    so it's not needed to disable interrupt.
108      */
109     if (lwprot_thread != LOS_CurTaskIDGet()) {
110         /* We are locking the spinlock where it has not been locked before
111          * or is being locked by another thread */
112         LOS_SpinLock(&arch_protect_spin);
113         lwprot_thread = LOS_CurTaskIDGet();
114         lwprot_count = 1;
115     } else {
116         /* It is already locked by THIS thread */
117         lwprot_count++;
118     }
119 #else
120     LOS_TaskLock();
121 #endif /* LOSCFG_KERNEL_SMP */
122     return 0; /* return value is unused */
123 }
124 
sys_arch_unprotect(sys_prot_t pval)125 void sys_arch_unprotect(sys_prot_t pval)
126 {
127     LWIP_UNUSED_ARG(pval);
128 #if (LOSCFG_KERNEL_SMP)
129     if (lwprot_thread == LOS_CurTaskIDGet()) {
130         lwprot_count--;
131         if (lwprot_count == 0) {
132             lwprot_thread = LOS_ERRNO_TSK_ID_INVALID;
133             LOS_SpinUnlock(&arch_protect_spin);
134         }
135     }
136 #else
137     LOS_TaskUnlock();
138 #endif /* LOSCFG_KERNEL_SMP */
139 }
140 
141 /**
142  * MessageBox
143  */
sys_mbox_new(sys_mbox_t * mbox,int size)144 err_t sys_mbox_new(sys_mbox_t *mbox, int size)
145 {
146     if (mbox == NULL) {
147         LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_new: mbox is null"));
148         return ERR_ARG;
149     }
150 
151     CHAR qName[] = "lwIP";
152     UINT32 ret = LOS_QueueCreate(qName, (UINT16)size, (UINT32 *)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     if (mbox == NULL) {
169         LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_post: mbox is null"));
170         return;
171     }
172 
173     /* Caution: the second parameter is NOT &msg */
174     UINT32 ret = LOS_QueueWrite(*mbox, msg, sizeof(char *), LOS_WAIT_FOREVER);
175     if (ret != LOS_OK) {
176         LWIP_DEBUGF(SYS_DEBUG, ("%s: LOS_QueueWrite error %u\n", __FUNCTION__, ret));
177     }
178 }
179 
sys_mbox_trypost(sys_mbox_t * mbox,void * msg)180 err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
181 {
182     if (mbox == NULL) {
183         LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_trypost: mbox is null"));
184         return ERR_ARG;
185     }
186 
187     /* Caution: the second parameter is NOT &msg */
188     UINT32 ret = LOS_QueueWrite(*mbox, msg, sizeof(char *), 0);
189     switch (ret) {
190         case LOS_OK:
191             return ERR_OK;
192         case LOS_ERRNO_QUEUE_ISFULL:
193             return ERR_MEM;
194         default:
195             break;
196     }
197     LWIP_DEBUGF(SYS_DEBUG, ("%s: LOS_QueueWrite error %u\n", __FUNCTION__, ret));
198     return ERR_ARG;
199 }
200 
sys_mbox_trypost_fromisr(sys_mbox_t * mbox,void * msg)201 err_t sys_mbox_trypost_fromisr(sys_mbox_t *mbox, void *msg)
202 {
203     (void)mbox;
204     (void)msg;
205     return ERR_ARG;
206 }
207 
sys_arch_mbox_fetch(sys_mbox_t * mbox,void ** msg,u32_t timeoutMs)208 u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeoutMs)
209 {
210     if (mbox == NULL) {
211         LWIP_DEBUGF(SYS_DEBUG, ("sys_arch_mbox_fetch: mbox is null"));
212         return  (u32_t)ERR_ARG;
213     }
214 
215     void *ignore = 0; /* if msg==NULL, the fetched msg should be dropped */
216     UINT64 tick = ROUND_UP_DIV((UINT64)timeoutMs * LOSCFG_BASE_CORE_TICK_PER_SECOND, OS_SYS_MS_PER_SECOND);
217     UINT32 ret = LOS_QueueRead(*mbox, msg ? msg : &ignore, sizeof(void *), tick ? (UINT32)tick : LOS_WAIT_FOREVER);
218     switch (ret) {
219         case LOS_OK:
220             return ERR_OK;
221         case LOS_ERRNO_QUEUE_ISEMPTY:
222         case LOS_ERRNO_QUEUE_TIMEOUT:
223             return SYS_ARCH_TIMEOUT;
224         default:
225             break;
226     }
227     LWIP_DEBUGF(SYS_DEBUG, ("%s: LOS_QueueRead error %u\n", __FUNCTION__, ret));
228     return SYS_ARCH_TIMEOUT; /* Errors should be treated as timeout */
229 }
230 
sys_arch_mbox_tryfetch(sys_mbox_t * mbox,void ** msg)231 u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
232 {
233     if (mbox == NULL) {
234         LWIP_DEBUGF(SYS_DEBUG, ("sys_arch_mbox_tryfetch: mbox is null"));
235         return  (u32_t)ERR_ARG;
236     }
237 
238     void *ignore = 0; /* if msg==NULL, the fetched msg should be dropped */
239     UINT32 ret = LOS_QueueRead(*mbox, msg ? msg : &ignore, sizeof(void *), 0);
240     switch (ret) {
241         case LOS_OK:
242             return ERR_OK;
243         case LOS_ERRNO_QUEUE_ISEMPTY:
244             return SYS_MBOX_EMPTY;
245         case LOS_ERRNO_QUEUE_TIMEOUT:
246             return SYS_ARCH_TIMEOUT;
247         default:
248             break;
249     }
250     LWIP_DEBUGF(SYS_DEBUG, ("%s: LOS_QueueRead error %u\n", __FUNCTION__, ret));
251     return SYS_MBOX_EMPTY; /* Errors should be treated as timeout */
252 }
253 
sys_mbox_free(sys_mbox_t * mbox)254 void sys_mbox_free(sys_mbox_t *mbox)
255 {
256     if (mbox == NULL) {
257         LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_free: mbox is null"));
258         return;
259     }
260 
261     (void)LOS_QueueDelete(*mbox);
262 }
263 
sys_mbox_valid(sys_mbox_t * mbox)264 int sys_mbox_valid(sys_mbox_t *mbox)
265 {
266     if (mbox == NULL) {
267         LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_valid: mbox is null"));
268         return ERR_ARG;
269     }
270 
271     QUEUE_INFO_S queueInfo;
272     return LOS_OK == LOS_QueueInfoGet(*mbox, &queueInfo);
273 }
274 
sys_mbox_set_invalid(sys_mbox_t * mbox)275 void sys_mbox_set_invalid(sys_mbox_t *mbox)
276 {
277     if (mbox == NULL) {
278         LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_set_invalid: mbox is null"));
279         return;
280     }
281 
282     *mbox = LOSCFG_BASE_IPC_QUEUE_LIMIT;
283 }
284 
285 /**
286  * Semaphore
287  */
sys_sem_new(sys_sem_t * sem,u8_t count)288 err_t sys_sem_new(sys_sem_t *sem, u8_t count)
289 {
290     if (sem == NULL) {
291         LWIP_DEBUGF(SYS_DEBUG, ("sys_sem_new: sem is null"));
292         return ERR_ARG;
293     }
294 
295     UINT32 ret = LOS_SemCreate(count, (UINT32 *)(sem));
296     if (ret != LOS_OK) {
297         return ERR_ARG;
298     }
299 
300     return ERR_OK;
301 }
302 
sys_sem_signal(sys_sem_t * sem)303 void sys_sem_signal(sys_sem_t *sem)
304 {
305     if (sem == NULL) {
306         LWIP_DEBUGF(SYS_DEBUG, ("sys_sem_signal: sem is null"));
307         return;
308     }
309     (void)LOS_SemPost(*sem);
310 }
311 
sys_arch_sem_wait(sys_sem_t * sem,u32_t timeoutMs)312 u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeoutMs)
313 {
314     if (sem == NULL) {
315         LWIP_DEBUGF(SYS_DEBUG, ("sys_arch_sem_wait: sem is null"));
316         return  (u32_t)ERR_ARG;
317     }
318 
319     UINT64 tick = ROUND_UP_DIV((UINT64)timeoutMs * LOSCFG_BASE_CORE_TICK_PER_SECOND, OS_SYS_MS_PER_SECOND);
320     UINT32 ret = LOS_SemPend(*sem, tick ? (UINT32)tick : LOS_WAIT_FOREVER); // timeoutMs 0 means wait forever
321     switch (ret) {
322         case LOS_OK:
323             return ERR_OK;
324         case LOS_ERRNO_SEM_TIMEOUT:
325             return SYS_ARCH_TIMEOUT;
326         default:
327             break;
328     }
329     LWIP_DEBUGF(SYS_DEBUG, ("%s: LOS_SemPend error %u\n", __FUNCTION__, ret));
330     return SYS_ARCH_TIMEOUT; /* Errors should be treated as timeout */
331 }
332 
sys_sem_free(sys_sem_t * sem)333 void sys_sem_free(sys_sem_t *sem)
334 {
335     if (sem == NULL) {
336         LWIP_DEBUGF(SYS_DEBUG, ("sys_sem_free: sem is null"));
337         return;
338     }
339     (void)LOS_SemDelete(*sem);
340 }
341 
sys_sem_valid(sys_sem_t * sem)342 int sys_sem_valid(sys_sem_t *sem)
343 {
344     if (sem == NULL) {
345         LWIP_DEBUGF(SYS_DEBUG, ("sys_sem_valid: sem is null"));
346         return ERR_ARG;
347     }
348     return *sem != LOSCFG_BASE_IPC_SEM_LIMIT;
349 }
350 
sys_sem_set_invalid(sys_sem_t * sem)351 void sys_sem_set_invalid(sys_sem_t *sem)
352 {
353     if (sem == NULL) {
354         LWIP_DEBUGF(SYS_DEBUG, ("sys_sem_set_invalid: sem is null"));
355         return;
356     }
357     *sem = LOSCFG_BASE_IPC_SEM_LIMIT;
358 }
359 
360 /**
361  * Mutex
362  */
sys_mutex_new(sys_mutex_t * mutex)363 err_t sys_mutex_new(sys_mutex_t *mutex)
364 {
365     if (mutex == NULL) {
366         LWIP_DEBUGF(SYS_DEBUG, ("sys_mutex_new: mutex is null"));
367         return ERR_ARG;
368     }
369 
370     UINT32 ret = LOS_MuxCreate((UINT32 *)mutex);
371     if (ret != LOS_OK) {
372         return ERR_ARG;
373     }
374 
375     return ERR_OK;
376 }
377 
sys_mutex_lock(sys_mutex_t * mutex)378 void sys_mutex_lock(sys_mutex_t *mutex)
379 {
380     if (mutex == NULL) {
381         LWIP_DEBUGF(SYS_DEBUG, ("sys_mutex_lock: mutex is null"));
382         return;
383     }
384 
385     (void)LOS_MuxPend(*mutex, LOS_WAIT_FOREVER);
386 }
387 
sys_mutex_unlock(sys_mutex_t * mutex)388 void sys_mutex_unlock(sys_mutex_t *mutex)
389 {
390     if (mutex == NULL) {
391         LWIP_DEBUGF(SYS_DEBUG, ("sys_mutex_unlock: mutex is null"));
392         return;
393     }
394 
395     (void)LOS_MuxPost(*mutex);
396 }
397 
sys_mutex_free(sys_mutex_t * mutex)398 void sys_mutex_free(sys_mutex_t *mutex)
399 {
400     if (mutex == NULL) {
401         LWIP_DEBUGF(SYS_DEBUG, ("sys_mutex_free: mutex is null"));
402         return;
403     }
404 
405     (void)LOS_MuxDelete(*mutex);
406 }
407 
sys_mutex_valid(sys_mutex_t * mutex)408 int sys_mutex_valid(sys_mutex_t *mutex)
409 {
410     if (mutex == NULL) {
411         LWIP_DEBUGF(SYS_DEBUG, ("sys_mutex_valid: mutex is null"));
412         return ERR_ARG;
413     }
414 
415     return *mutex != LOSCFG_BASE_IPC_MUX_LIMIT;
416 }
417 
sys_mutex_set_invalid(sys_mutex_t * mutex)418 void sys_mutex_set_invalid(sys_mutex_t *mutex)
419 {
420     if (mutex == NULL) {
421         LWIP_DEBUGF(SYS_DEBUG, ("sys_mutex_set_invalid: mutex is null"));
422         return;
423     }
424 
425     *mutex = LOSCFG_BASE_IPC_MUX_LIMIT;
426 }
427 
LwipLogPrintf(const char * fmt,...)428 void LwipLogPrintf(const char *fmt, ...)
429 {
430     if ((fmt == NULL) || (strlen(fmt) == 0)) {
431         return;
432     }
433 
434     int len;
435     char buf[LWIP_LOG_BUF_SIZE] = {0};
436     va_list ap;
437     va_start(ap, fmt);
438     len = vsprintf_s(buf, sizeof(buf) - 1, fmt, ap);
439     va_end(ap);
440     if (len < 0) {
441         LWIP_LOGGER("log param invalid or buf is not enough.");
442         return;
443     }
444     LWIP_LOGGER(buf);
445 }
446