• 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 "cmsis_os2.h"
33 #include "kal.h"
34 #include "los_event.h"
35 #include "los_membox.h"
36 #include "los_memory.h"
37 #include "los_interrupt.h"
38 #include "los_mux.h"
39 #include "los_queue.h"
40 #include "los_sem.h"
41 #include "los_swtmr.h"
42 #include "los_task.h"
43 #include "los_timer.h"
44 #include "los_debug.h"
45 #if (LOSCFG_MUTEX_CREATE_TRACE == 1)
46 #include "los_arch.h"
47 #endif
48 
49 #include "string.h"
50 #include "securec.h"
51 
52 /* Kernel initialization state */
53 static osKernelState_t g_kernelState;
54 
55 extern BOOL g_taskScheduled;
56 
57 /* LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO <---> osPriorityNormal */
58 #define LOS_PRIORITY(cmsisPriority) (LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO - ((cmsisPriority) - osPriorityNormal))
59 #define CMSIS_PRIORITY(losPriority) (osPriorityNormal + (LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO - (losPriority)))
60 
61 /* OS_TASK_PRIORITY_HIGHEST and OS_TASK_PRIORITY_LOWEST is reserved for internal TIMER and IDLE task use only. */
62 #define ISVALID_LOS_PRIORITY(losPrio) ((losPrio) > OS_TASK_PRIORITY_HIGHEST && (losPrio) < OS_TASK_PRIORITY_LOWEST)
63 
64 const osVersion_t g_stLosVersion = { 001, 001 };
65 
66 #define LITEOS_VERSION_MAJOR 1
67 #define LITEOS_VERSION_MINOR 0
68 #define LITEOS_VERSION_BUILD 0
69 
70 /* Kernel version and identification string definition */
71 #define KERNEL_VERSION            (((UINT32)LITEOS_VERSION_MAJOR * 10000000UL) | \
72                                    ((UINT32)LITEOS_VERSION_MINOR *    10000UL) | \
73                                    ((UINT32)LITEOS_VERSION_BUILD *        1UL))
74 
75 #define KERNEL_ID "HUAWEI-LiteOS"
76 
77 #define KERNEL_UNLOCKED 0
78 #define KERNEL_LOCKED   1
79 
80 //  ==== Kernel Management Functions ====
osKernelInitialize(void)81 osStatus_t osKernelInitialize(void)
82 {
83     if (OS_INT_ACTIVE) {
84         return osErrorISR;
85     }
86 
87     if (g_kernelState != osKernelInactive) {
88         return osError;
89     }
90 
91     if (LOS_KernelInit() == LOS_OK) {
92         g_kernelState = osKernelReady;
93         return osOK;
94     } else {
95         return osError;
96     }
97 }
98 
osKernelGetInfo(osVersion_t * version,char * id_buf,uint32_t id_size)99 osStatus_t osKernelGetInfo(osVersion_t *version, char *id_buf, uint32_t id_size)
100 {
101     errno_t ret;
102 
103     if ((version == NULL) || (id_buf == NULL) || (id_size == 0)) {
104         return osError;
105     }
106 
107     ret = memcpy_s(id_buf, id_size, KERNEL_ID, sizeof(KERNEL_ID));
108     if (ret == EOK) {
109         version->api = g_stLosVersion.api;
110         version->kernel = g_stLosVersion.kernel;
111         return osOK;
112     } else {
113         PRINT_ERR("[%s] memcpy_s failed, error type = %d\n", __func__, ret);
114         return osError;
115     }
116 }
117 
osKernelGetState(void)118 osKernelState_t osKernelGetState(void)
119 {
120     if (!g_taskScheduled) {
121         if (g_kernelState == osKernelReady) {
122             return osKernelReady;
123         } else {
124             return osKernelInactive;
125         }
126     } else if (g_losTaskLock > 0) {
127         return osKernelLocked;
128     } else {
129         return osKernelRunning;
130     }
131 }
132 
osKernelStart(void)133 osStatus_t osKernelStart(void)
134 {
135     if (OS_INT_ACTIVE) {
136         return osErrorISR;
137     }
138     if (g_kernelState != osKernelReady) {
139         return osError;
140     }
141 
142     if (LOS_Start() == LOS_OK) {
143         g_kernelState = osKernelRunning;
144         return osOK;
145     } else {
146         return osError;
147     }
148 }
149 
osKernelLock(void)150 int32_t osKernelLock(void)
151 {
152     int32_t lock;
153 
154     if (OS_INT_ACTIVE) {
155         return (int32_t)osErrorISR;
156     }
157 
158     if (!g_taskScheduled) {
159         return (int32_t)osError;
160     }
161 
162     if (g_losTaskLock > 0) {
163         lock = KERNEL_LOCKED;
164     } else {
165         LOS_TaskLock();
166         lock = KERNEL_UNLOCKED;
167     }
168 
169     return lock;
170 }
171 
osKernelUnlock(void)172 int32_t osKernelUnlock(void)
173 {
174     int32_t lock;
175 
176     if (OS_INT_ACTIVE) {
177         return (int32_t)osErrorISR;
178     }
179 
180     if (!g_taskScheduled) {
181         return (int32_t)osError;
182     }
183 
184     if (g_losTaskLock > 0) {
185         LOS_TaskUnlock();
186         if (g_losTaskLock != 0) {
187             return (int32_t)osError;
188         }
189         lock = KERNEL_LOCKED;
190     } else {
191         lock = KERNEL_UNLOCKED;
192     }
193 
194     return lock;
195 }
196 
osKernelRestoreLock(int32_t lock)197 int32_t osKernelRestoreLock(int32_t lock)
198 {
199     if (OS_INT_ACTIVE) {
200         return (int32_t)osErrorISR;
201     }
202 
203     if (!g_taskScheduled) {
204         return (int32_t)osError;
205     }
206 
207     switch (lock) {
208         case KERNEL_UNLOCKED:
209             LOS_TaskUnlock();
210             if (g_losTaskLock != 0) {
211                 break;
212             }
213             return KERNEL_UNLOCKED;
214         case KERNEL_LOCKED:
215             LOS_TaskLock();
216             return KERNEL_LOCKED;
217         default:
218             break;
219     }
220 
221     return (int32_t)osError;
222 }
223 
osKernelGetTickCount(void)224 uint32_t osKernelGetTickCount(void)
225 {
226     uint64_t ticks = LOS_TickCountGet();
227     return (uint32_t)ticks;
228 }
229 
osKernelGetTickFreq(void)230 uint32_t osKernelGetTickFreq(void)
231 {
232     return (uint32_t)LOSCFG_BASE_CORE_TICK_PER_SECOND;
233 }
234 
osKernelGetSysTimerCount(void)235 uint32_t osKernelGetSysTimerCount(void)
236 {
237     return (uint32_t)LOS_SysCycleGet();
238 }
239 
osKernelGetSysTimerFreq(void)240 uint32_t osKernelGetSysTimerFreq(void)
241 {
242     return g_sysClock;
243 }
244 
245 //  ==== Thread Management Functions ====
osThreadNew(osThreadFunc_t func,void * argument,const osThreadAttr_t * attr)246 osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
247 {
248     UINT32 tid;
249     UINT32 ret;
250     osThreadAttr_t attrTemp = {0};
251     TSK_INIT_PARAM_S stTskInitParam = {0};
252     UINT16 priority;
253 
254     if (OS_INT_ACTIVE || (func == NULL)) {
255         return (osThreadId_t)NULL;
256     }
257 
258     if (attr == NULL) {
259         attrTemp.priority = osPriorityNormal,
260         attr = &attrTemp;
261     }
262 
263     priority = LOS_PRIORITY(attr->priority);
264     if (!ISVALID_LOS_PRIORITY(priority)) {
265         /* unsupported priority */
266         return (osThreadId_t)NULL;
267     }
268     stTskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)func;
269     stTskInitParam.uwArg = (UINT32)argument;
270     if ((attr->stack_mem != NULL) && (attr->stack_size != 0)) {
271         stTskInitParam.stackAddr = (UINTPTR)attr->stack_mem;
272         stTskInitParam.uwStackSize = attr->stack_size;
273     } else if (attr->stack_size != 0) {
274         stTskInitParam.uwStackSize = attr->stack_size;
275     } else {
276         stTskInitParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
277     }
278     if (attr->name != NULL) {
279         stTskInitParam.pcName = (char *)attr->name;
280     } else {
281         stTskInitParam.pcName = "CmsisTask";
282     }
283     if (attr->attr_bits == osThreadJoinable) {
284         stTskInitParam.uwResved = LOS_TASK_ATTR_JOINABLE;
285     }
286     stTskInitParam.usTaskPrio = priority;
287     ret = LOS_TaskCreate(&tid, &stTskInitParam);
288     if (ret != LOS_OK) {
289         return (osThreadId_t)NULL;
290     }
291 
292     return (osThreadId_t)OS_TCB_FROM_TID(tid);
293 }
294 
osThreadGetName(osThreadId_t thread_id)295 const char *osThreadGetName(osThreadId_t thread_id)
296 {
297     LosTaskCB *pstTaskCB = NULL;
298 
299     if (OS_INT_ACTIVE || thread_id == NULL) {
300         return NULL;
301     }
302 
303     pstTaskCB = (LosTaskCB *)thread_id;
304 
305     return pstTaskCB->taskName;
306 }
307 
osThreadGetId(void)308 osThreadId_t osThreadGetId(void)
309 {
310     return (osThreadId_t)(g_losTask.runTask);
311 }
312 
osThreadGetArgument(void)313 void *osThreadGetArgument(void)
314 {
315     if (OS_INT_ACTIVE) {
316         return 0;
317     }
318 
319     LosTaskCB *taskCb = (LosTaskCB *)osThreadGetId();
320     if (taskCb == NULL) {
321         return NULL;
322     }
323     return (void *)(taskCb->arg);
324 }
325 
osThreadGetState(osThreadId_t thread_id)326 osThreadState_t osThreadGetState(osThreadId_t thread_id)
327 {
328     UINT16 taskStatus;
329     osThreadState_t stState;
330     LosTaskCB *pstTaskCB = NULL;
331 
332     if (OS_INT_ACTIVE || thread_id == NULL) {
333         return osThreadError;
334     }
335 
336     pstTaskCB = (LosTaskCB *)thread_id;
337     taskStatus = pstTaskCB->taskStatus;
338 
339     if (taskStatus & OS_TASK_STATUS_RUNNING) {
340         stState = osThreadRunning;
341     } else if (taskStatus & OS_TASK_STATUS_READY) {
342         stState = osThreadReady;
343     } else if (taskStatus &
344         (OS_TASK_STATUS_DELAY | OS_TASK_STATUS_PEND |
345          OS_TASK_STATUS_SUSPEND | OS_TASK_STATUS_PEND_TIME)) {
346         stState = osThreadBlocked;
347     } else if (taskStatus & OS_TASK_STATUS_UNUSED) {
348         stState = osThreadInactive;
349     } else {
350         stState = osThreadError;
351     }
352 
353     return stState;
354 }
355 
osThreadGetStackSize(osThreadId_t thread_id)356 uint32_t osThreadGetStackSize(osThreadId_t thread_id)
357 {
358     LosTaskCB *pstTaskCB = NULL;
359 
360     if (OS_INT_ACTIVE || thread_id == NULL) {
361         return 0U;
362     }
363 
364     pstTaskCB = (LosTaskCB *)thread_id;
365 
366     return pstTaskCB->stackSize;
367 }
368 
osTaskStackWaterMarkGet(UINT32 taskID)369 uint32_t osTaskStackWaterMarkGet(UINT32 taskID)
370 {
371     UINT32 count = 0;
372     UINT32 *ptopOfStack = NULL;
373     UINT32 intSave;
374     LosTaskCB *pstTaskCB = NULL;
375 
376     if (taskID > LOSCFG_BASE_CORE_TSK_LIMIT) {
377         return 0;
378     }
379 
380     intSave = LOS_IntLock();
381 
382     pstTaskCB = OS_TCB_FROM_TID(taskID);
383     if (OS_TASK_STATUS_UNUSED & (pstTaskCB->taskStatus)) {
384         LOS_IntRestore(intSave);
385         return 0;
386     }
387 
388     // first 4 bytes is OS_TASK_MAGIC_WORD, skip
389     ptopOfStack = (UINT32 *)(UINTPTR)pstTaskCB->topOfStack + 1;
390 
391     while (*ptopOfStack == (UINT32)OS_TASK_STACK_INIT) {
392         ++ptopOfStack;
393         ++count;
394     }
395 
396     count *= sizeof(UINT32);
397 
398     LOS_IntRestore(intSave);
399     return count;
400 }
401 
osThreadGetStackSpace(osThreadId_t thread_id)402 uint32_t osThreadGetStackSpace(osThreadId_t thread_id)
403 {
404     LosTaskCB *pstTaskCB = NULL;
405 
406     if (OS_INT_ACTIVE || thread_id == NULL) {
407         return 0U;
408     }
409 
410     pstTaskCB = (LosTaskCB *)thread_id;
411 
412     return osTaskStackWaterMarkGet(pstTaskCB->taskID);
413 }
414 
osThreadSetPriority(osThreadId_t thread_id,osPriority_t priority)415 osStatus_t osThreadSetPriority(osThreadId_t thread_id, osPriority_t priority)
416 {
417     UINT32 ret;
418     UINT16 prio;
419     LosTaskCB *pstTaskCB = NULL;
420 
421     if (OS_INT_ACTIVE) {
422         return osErrorISR;
423     }
424 
425     if (thread_id == NULL) {
426         return osErrorParameter;
427     }
428 
429     prio = LOS_PRIORITY(priority);
430     if (!ISVALID_LOS_PRIORITY(prio)) {
431         return osErrorParameter;
432     }
433 
434     pstTaskCB = (LosTaskCB *)thread_id;
435     ret = LOS_TaskPriSet(pstTaskCB->taskID, prio);
436     switch (ret) {
437         case LOS_ERRNO_TSK_PRIOR_ERROR:
438         case LOS_ERRNO_TSK_OPERATE_SYSTEM_TASK:
439         case LOS_ERRNO_TSK_ID_INVALID:
440             return osErrorParameter;
441 
442         case LOS_ERRNO_TSK_NOT_CREATED:
443             return osErrorResource;
444 
445         default:
446             return osOK;
447     }
448 }
449 
osThreadGetPriority(osThreadId_t thread_id)450 osPriority_t osThreadGetPriority(osThreadId_t thread_id)
451 {
452     UINT16 ret;
453     LosTaskCB *pstTaskCB = NULL;
454 
455     if (OS_INT_ACTIVE || thread_id == NULL) {
456         return osPriorityError;
457     }
458 
459     pstTaskCB = (LosTaskCB *)thread_id;
460     ret = LOS_TaskPriGet(pstTaskCB->taskID);
461 
462     if ((ret == (UINT16)OS_INVALID) || (ret > OS_TASK_PRIORITY_LOWEST)) {
463         return osPriorityError;
464     }
465 
466     return (osPriority_t)CMSIS_PRIORITY(ret);
467 }
468 
osThreadYield(void)469 osStatus_t osThreadYield(void)
470 {
471     UINT32 ret;
472 
473     if (OS_INT_ACTIVE) {
474         return osErrorISR;
475     }
476 
477     ret = LOS_TaskYield();
478 
479     if (ret == LOS_OK) {
480         return osOK;
481     }
482 
483     return osError;
484 }
485 
osThreadSuspend(osThreadId_t thread_id)486 osStatus_t osThreadSuspend(osThreadId_t thread_id)
487 {
488     UINT32 ret;
489     LosTaskCB *pstTaskCB = NULL;
490 
491     if (OS_INT_ACTIVE) {
492         return osErrorISR;
493     }
494 
495     if (thread_id == NULL) {
496         return osErrorParameter;
497     }
498 
499     pstTaskCB = (LosTaskCB *)thread_id;
500     ret = LOS_TaskSuspend(pstTaskCB->taskID);
501     switch (ret) {
502         case LOS_ERRNO_TSK_OPERATE_IDLE:
503         case LOS_ERRNO_TSK_SUSPEND_SWTMR_NOT_ALLOWED:
504         case LOS_ERRNO_TSK_ID_INVALID:
505             return osErrorParameter;
506 
507         case LOS_ERRNO_TSK_NOT_CREATED:
508         case LOS_ERRNO_TSK_ALREADY_SUSPENDED:
509         case LOS_ERRNO_TSK_SUSPEND_LOCKED:
510             return osErrorResource;
511 
512         default:
513             return osOK;
514     }
515 }
516 
osThreadResume(osThreadId_t thread_id)517 osStatus_t osThreadResume(osThreadId_t thread_id)
518 {
519     UINT32 ret;
520     LosTaskCB *pstTaskCB = NULL;
521 
522     if (OS_INT_ACTIVE) {
523         return osErrorISR;
524     }
525 
526     if (thread_id == NULL) {
527         return osErrorParameter;
528     }
529 
530     pstTaskCB = (LosTaskCB *)thread_id;
531     ret = LOS_TaskResume(pstTaskCB->taskID);
532 
533     switch (ret) {
534         case LOS_ERRNO_TSK_ID_INVALID:
535             return osErrorParameter;
536 
537         case LOS_ERRNO_TSK_NOT_CREATED:
538         case LOS_ERRNO_TSK_NOT_SUSPENDED:
539             return osErrorResource;
540 
541         default:
542             return osOK;
543     }
544 }
545 
osThreadDetach(osThreadId_t thread_id)546 osStatus_t osThreadDetach(osThreadId_t thread_id)
547 {
548     UINT32 ret;
549     LosTaskCB *taskCB = (LosTaskCB *)thread_id;
550 
551     if (thread_id == NULL) {
552         return osErrorParameter;
553     }
554 
555     ret = LOS_TaskDetach(taskCB->taskID);
556     if (ret == LOS_ERRNO_TSK_NOT_ALLOW_IN_INT) {
557         return osErrorISR;
558     } else if (ret != LOS_OK) {
559         return osErrorResource;
560     }
561 
562     return osOK;
563 }
564 
osThreadJoin(osThreadId_t thread_id)565 osStatus_t osThreadJoin(osThreadId_t thread_id)
566 {
567     UINT32 ret;
568     LosTaskCB *taskCB = (LosTaskCB *)thread_id;
569 
570     if (thread_id == NULL) {
571         return osErrorParameter;
572     }
573 
574     ret = LOS_TaskJoin(taskCB->taskID, NULL);
575     if (ret == LOS_ERRNO_TSK_NOT_ALLOW_IN_INT) {
576         return osErrorISR;
577     } else if (ret != LOS_OK) {
578         return osErrorResource;
579     }
580 
581     return osOK;
582 }
583 
osThreadTerminate(osThreadId_t thread_id)584 osStatus_t osThreadTerminate(osThreadId_t thread_id)
585 {
586     UINT32 ret;
587     LosTaskCB *pstTaskCB = NULL;
588 
589     if (OS_INT_ACTIVE) {
590         return osErrorISR;
591     }
592 
593     if (thread_id == NULL) {
594         return osErrorParameter;
595     }
596 
597     pstTaskCB = (LosTaskCB *)thread_id;
598     ret = LOS_TaskDelete(pstTaskCB->taskID);
599 
600     switch (ret) {
601         case LOS_ERRNO_TSK_OPERATE_IDLE:
602         case LOS_ERRNO_TSK_SUSPEND_SWTMR_NOT_ALLOWED:
603         case LOS_ERRNO_TSK_ID_INVALID:
604             return osErrorParameter;
605 
606         case LOS_ERRNO_TSK_NOT_CREATED:
607             return osErrorResource;
608 
609         default:
610             return osOK;
611     }
612 }
613 
osThreadGetCount(void)614 uint32_t osThreadGetCount(void)
615 {
616     uint32_t count = 0;
617 
618     if (OS_INT_ACTIVE) {
619         return 0U;
620     }
621 
622     for (uint32_t index = 0; index <= LOSCFG_BASE_CORE_TSK_LIMIT; index++) {
623         if (!((g_taskCBArray + index)->taskStatus & OS_TASK_STATUS_UNUSED)) {
624             count++;
625         }
626     }
627 
628     return count;
629 }
630 
osThreadExit(void)631 void osThreadExit(void)
632 {
633     (void)LOS_TaskDelete(LOS_CurTaskIDGet());
634     UNREACHABLE;
635 }
636 
637 /* before kernel running osDelay is implemented by HalDelay interface */
HalDelay(UINT32 ticks)638 WEAK VOID HalDelay(UINT32 ticks)
639 {
640 
641 }
642 
osDelay(uint32_t ticks)643 osStatus_t osDelay(uint32_t ticks)
644 {
645     UINT32 ret;
646     if (OS_INT_ACTIVE) {
647         return osErrorISR;
648     }
649     if (ticks == 0) {
650         return osErrorParameter;
651     }
652 
653     if (osKernelGetState() != osKernelRunning) {
654         HalDelay(ticks);
655         return osOK;
656     }
657 
658     ret = LOS_TaskDelay(ticks);
659     if (ret == LOS_OK) {
660         return osOK;
661     } else {
662         return osError;
663     }
664 }
665 
osDelayUntil(uint32_t ticks)666 osStatus_t osDelayUntil(uint32_t ticks)
667 {
668     UINT32 ret;
669     UINT32 uwTicks;
670     UINT32 tickCount = osKernelGetTickCount();
671 
672     if (OS_INT_ACTIVE) {
673         return osErrorISR;
674     }
675 
676     if (ticks < tickCount) {
677         return osError;
678     }
679 
680     uwTicks = (UINT32)(ticks - tickCount);
681 
682     ret = LOS_TaskDelay(uwTicks);
683     if (ret == LOS_OK) {
684         return osOK;
685     } else {
686         return osError;
687     }
688 }
689 
690 //  ==== Timer Management Functions ====
691 #if (LOSCFG_BASE_CORE_SWTMR == 1)
osTimerNew(osTimerFunc_t func,osTimerType_t type,void * argument,const osTimerAttr_t * attr)692 osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
693 {
694     UNUSED(attr);
695     UINT32 swtmrId;
696     UINT8 mode;
697 
698     if ((func == NULL) || OS_INT_ACTIVE) {
699         return NULL;
700     }
701 
702     if (type == osTimerOnce) {
703         mode = LOS_SWTMR_MODE_NO_SELFDELETE;
704     } else if (type == osTimerPeriodic) {
705         mode = LOS_SWTMR_MODE_PERIOD;
706     } else {
707         return NULL;
708     }
709 
710 #if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
711     if (LOS_SwtmrCreate(1, mode, (SWTMR_PROC_FUNC)func, &swtmrId, (UINT32)(UINTPTR)argument,
712         osTimerRousesAllow, osTimerAlignIgnore) != LOS_OK) {
713         return (osTimerId_t)NULL;
714     }
715 #else
716     if (LOS_SwtmrCreate(1, mode, (SWTMR_PROC_FUNC)func, &swtmrId, (UINT32)(UINTPTR)argument) != LOS_OK) {
717         return (osTimerId_t)NULL;
718     }
719 #endif
720     return (osTimerId_t)OS_SWT_FROM_SID(swtmrId);
721 }
722 
723 #if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
osTimerExtNew(osTimerFunc_t func,osTimerType_t type,void * argument,const osTimerAttr_t * attr,osTimerRouses_t ucRouses,osTimerAlign_t ucSensitive)724 osTimerId_t osTimerExtNew(osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr,
725     osTimerRouses_t ucRouses, osTimerAlign_t ucSensitive)
726 {
727     UNUSED(attr);
728     UINT32 usSwTmrID;
729     UINT8 mode;
730 
731     if ((OS_INT_ACTIVE) || (NULL == func) || ((osTimerOnce != type) && (osTimerPeriodic != type))) {
732         return (osTimerId_t)NULL;
733     }
734 
735     if (osTimerOnce == type) {
736         mode = LOS_SWTMR_MODE_NO_SELFDELETE;
737     } else {
738         mode = LOS_SWTMR_MODE_PERIOD;
739     }
740     if (LOS_OK != LOS_SwtmrCreate(1, mode, (SWTMR_PROC_FUNC)func, &usSwTmrID,
741         (UINT32)(UINTPTR)argument, ucRouses, ucSensitive)) {
742         return (osTimerId_t)NULL;
743     }
744 
745     return (osTimerId_t)OS_SWT_FROM_SID(usSwTmrID);
746 }
747 #endif
748 
osTimerStart(osTimerId_t timer_id,uint32_t ticks)749 osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks)
750 {
751     UINT32 ret;
752     SWTMR_CTRL_S *pstSwtmr = NULL;
753     if (OS_INT_ACTIVE) {
754         return osErrorISR;
755     }
756     if ((ticks == 0) || (timer_id == NULL)) {
757         return osErrorParameter;
758     }
759 
760     UINT32 intSave = LOS_IntLock();
761     pstSwtmr = (SWTMR_CTRL_S *)timer_id;
762     pstSwtmr->uwInterval = ticks;
763     ret = LOS_SwtmrStart(pstSwtmr->usTimerID);
764     LOS_IntRestore(intSave);
765     if (ret == LOS_OK) {
766         return osOK;
767     } else if (ret == LOS_ERRNO_SWTMR_ID_INVALID) {
768         return osErrorParameter;
769     } else {
770         return osErrorResource;
771     }
772 }
773 
osTimerGetName(osTimerId_t timer_id)774 const char *osTimerGetName(osTimerId_t timer_id)
775 {
776     UNUSED(timer_id);
777     return (const char *)NULL;
778 }
779 
osTimerStop(osTimerId_t timer_id)780 osStatus_t osTimerStop(osTimerId_t timer_id)
781 {
782     UINT32 ret;
783     SWTMR_CTRL_S *pstSwtmr = (SWTMR_CTRL_S *)timer_id;
784 
785     if (OS_INT_ACTIVE) {
786         return osErrorISR;
787     }
788 
789     if (pstSwtmr == NULL) {
790         return osErrorParameter;
791     }
792 
793     ret = LOS_SwtmrStop(pstSwtmr->usTimerID);
794     if (ret == LOS_OK) {
795         return osOK;
796     } else if (ret == LOS_ERRNO_SWTMR_ID_INVALID) {
797         return osErrorParameter;
798     } else {
799         return osErrorResource;
800     }
801 }
802 
osTimerIsRunning(osTimerId_t timer_id)803 uint32_t osTimerIsRunning(osTimerId_t timer_id)
804 {
805     if (OS_INT_ACTIVE) {
806         return (uint32_t)osErrorISR;
807     }
808     if (timer_id == NULL) {
809         return 0;
810     }
811 
812     return (OS_SWTMR_STATUS_TICKING == ((SWTMR_CTRL_S *)timer_id)->ucState);
813 }
814 
osTimerDelete(osTimerId_t timer_id)815 osStatus_t osTimerDelete(osTimerId_t timer_id)
816 {
817     UINT32 ret;
818     SWTMR_CTRL_S *pstSwtmr = (SWTMR_CTRL_S *)timer_id;
819 
820     if (OS_INT_ACTIVE) {
821         return osErrorISR;
822     }
823     if (pstSwtmr == NULL) {
824         return osErrorParameter;
825     }
826     ret = LOS_SwtmrDelete(pstSwtmr->usTimerID);
827     if (ret == LOS_OK) {
828         return osOK;
829     } else if (ret == LOS_ERRNO_SWTMR_ID_INVALID) {
830         return osErrorParameter;
831     } else {
832         return osErrorResource;
833     }
834 }
835 #endif
836 
osEventFlagsNew(const osEventFlagsAttr_t * attr)837 osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr)
838 {
839     PEVENT_CB_S pstEventCB;
840     UINT32 ret;
841 
842     UNUSED(attr);
843 
844     if (OS_INT_ACTIVE) {
845         return (osEventFlagsId_t)NULL;
846     }
847 
848     pstEventCB = (PEVENT_CB_S)LOS_MemAlloc(m_aucSysMem0, sizeof(EVENT_CB_S));
849     if (pstEventCB == NULL) {
850         return (osEventFlagsId_t)NULL;
851     }
852 
853     ret = LOS_EventInit(pstEventCB);
854     if (ret == LOS_OK) {
855         return (osEventFlagsId_t)pstEventCB;
856     } else {
857         if (LOS_MemFree(m_aucSysMem0, pstEventCB) != LOS_OK) {
858             PRINT_ERR("[%s] memory free fail!\n", __func__);
859         }
860         return NULL;
861     }
862 }
863 
osEventFlagsGetName(osEventFlagsId_t ef_id)864 const char *osEventFlagsGetName(osEventFlagsId_t ef_id)
865 {
866     (void)ef_id;
867     if (OS_INT_ACTIVE) {
868         return NULL;
869     }
870     return NULL;
871 }
872 
osEventFlagsSet(osEventFlagsId_t ef_id,uint32_t flags)873 uint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags)
874 {
875     PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
876     UINT32 ret;
877     uint32_t rflags;
878 
879     if (pstEventCB == NULL) {
880         return osFlagsErrorParameter;
881     }
882 
883     ret = LOS_EventWrite(pstEventCB, (UINT32)flags);
884     if (ret == LOS_OK) {
885         rflags = pstEventCB->uwEventID;
886         return rflags;
887     } else {
888         return (uint32_t)osFlagsErrorResource;
889     }
890 }
891 
osEventFlagsClear(osEventFlagsId_t ef_id,uint32_t flags)892 uint32_t osEventFlagsClear(osEventFlagsId_t ef_id, uint32_t flags)
893 {
894     PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
895     UINT32 intSave;
896     uint32_t rflags;
897     UINT32 ret;
898 
899     if (pstEventCB == NULL) {
900         return (uint32_t)osFlagsErrorParameter;
901     }
902 
903     intSave = LOS_IntLock();
904     rflags = pstEventCB->uwEventID;
905 
906     ret = LOS_EventClear(pstEventCB, ~flags);
907     LOS_IntRestore(intSave);
908     if (ret == LOS_OK) {
909         return rflags;
910     } else {
911         return (uint32_t)osFlagsErrorResource;
912     }
913 }
914 
osEventFlagsGet(osEventFlagsId_t ef_id)915 uint32_t osEventFlagsGet(osEventFlagsId_t ef_id)
916 {
917     PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
918     UINT32 intSave;
919     uint32_t rflags;
920 
921     if (pstEventCB == NULL) {
922         return 0;
923     }
924 
925     intSave = LOS_IntLock();
926     rflags = pstEventCB->uwEventID;
927     LOS_IntRestore(intSave);
928 
929     return rflags;
930 }
931 
osEventFlagsWait(osEventFlagsId_t ef_id,uint32_t flags,uint32_t options,uint32_t timeout)932 uint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout)
933 {
934     PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
935     UINT32 mode = 0;
936     UINT32 ret;
937     uint32_t rflags;
938 
939     if (OS_INT_ACTIVE && (timeout != 0)) {
940         return (uint32_t)osFlagsErrorParameter;
941     }
942 
943     if (options > (osFlagsWaitAny | osFlagsWaitAll | osFlagsNoClear)) {
944         return (uint32_t)osFlagsErrorParameter;
945     }
946 
947     if ((options & osFlagsWaitAll) == osFlagsWaitAll) {
948         mode |= LOS_WAITMODE_AND;
949     } else {
950         mode |= LOS_WAITMODE_OR;
951     }
952 
953     if ((options & osFlagsNoClear) == osFlagsNoClear) {
954         mode &= ~LOS_WAITMODE_CLR;
955     } else {
956         mode |= LOS_WAITMODE_CLR;
957     }
958 
959     ret = LOS_EventRead(pstEventCB, (UINT32)flags, mode, (UINT32)timeout);
960     switch (ret) {
961         case LOS_ERRNO_EVENT_PTR_NULL:
962         case LOS_ERRNO_EVENT_EVENTMASK_INVALID:
963         case LOS_ERRNO_EVENT_FLAGS_INVALID:
964         case LOS_ERRNO_EVENT_SETBIT_INVALID:
965             return (uint32_t)osFlagsErrorParameter;
966 
967         case LOS_ERRNO_EVENT_READ_IN_INTERRUPT:
968         case LOS_ERRNO_EVENT_READ_IN_LOCK:
969             return (uint32_t)osFlagsErrorResource;
970 
971         case LOS_ERRNO_EVENT_READ_TIMEOUT:
972             return (uint32_t)osFlagsErrorTimeout;
973 
974         default:
975             rflags = (uint32_t)ret;
976             return rflags;
977     }
978 }
979 
osEventFlagsDelete(osEventFlagsId_t ef_id)980 osStatus_t osEventFlagsDelete(osEventFlagsId_t ef_id)
981 {
982     PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
983     UINT32 intSave;
984     osStatus_t ret = osOK;
985     if (OS_INT_ACTIVE) {
986         return osErrorISR;
987     }
988     intSave = LOS_IntLock();
989     if (LOS_EventDestroy(pstEventCB) != LOS_OK) {
990         ret = osErrorParameter;
991     }
992     LOS_IntRestore(intSave);
993 
994     if (LOS_MemFree(m_aucSysMem0, (void *)pstEventCB) != LOS_OK) {
995         ret = osErrorParameter;
996     }
997 
998     return ret;
999 }
1000 
1001 //  ==== Mutex Management Functions ====
1002 #if (LOSCFG_BASE_IPC_MUX == 1)
osMutexNew(const osMutexAttr_t * attr)1003 osMutexId_t osMutexNew(const osMutexAttr_t *attr)
1004 {
1005     UINT32 ret;
1006     UINT32 muxId;
1007 
1008 #if (LOSCFG_MUTEX_CREATE_TRACE == 1)
1009     UINTPTR regLR = ArchLRGet();
1010 #endif
1011 
1012     UNUSED(attr);
1013 
1014     if (OS_INT_ACTIVE) {
1015         return NULL;
1016     }
1017 
1018     ret = LOS_MuxCreate(&muxId);
1019     if (ret == LOS_OK) {
1020 #if (LOSCFG_MUTEX_CREATE_TRACE == 1)
1021         OsSetMutexCreateInfo(GET_MUX(muxId), regLR);
1022 #endif
1023         return (osMutexId_t)(GET_MUX(muxId));
1024     } else {
1025         return (osMutexId_t)NULL;
1026     }
1027 }
1028 
osMutexAcquire(osMutexId_t mutex_id,uint32_t timeout)1029 osStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout)
1030 {
1031     LosMuxCB *muxCB = (LosMuxCB *)mutex_id;
1032     UINT32 ret;
1033 
1034     if (muxCB == NULL) {
1035         return osErrorParameter;
1036     }
1037     if (OS_INT_ACTIVE) {
1038         return osErrorISR;
1039     }
1040 
1041     ret = LOS_MuxPend(muxCB->muxID, timeout);
1042     if (ret == LOS_OK) {
1043         return osOK;
1044     } else if (ret == LOS_ERRNO_MUX_INVALID) {
1045         return osErrorParameter;
1046     } else if (ret == LOS_ERRNO_MUX_TIMEOUT) {
1047         return osErrorTimeout;
1048     } else {
1049         return osErrorResource;
1050     }
1051 }
1052 
osMutexRelease(osMutexId_t mutex_id)1053 osStatus_t osMutexRelease(osMutexId_t mutex_id)
1054 {
1055     LosMuxCB *muxCB = (LosMuxCB *)mutex_id;
1056     UINT32 ret;
1057 
1058     if (muxCB == NULL) {
1059         return osErrorParameter;
1060     }
1061     if (OS_INT_ACTIVE) {
1062         return osErrorISR;
1063     }
1064 
1065     ret = LOS_MuxPost(muxCB->muxID);
1066     if (ret == LOS_OK) {
1067         return osOK;
1068     } else if (ret == LOS_ERRNO_MUX_INVALID) {
1069         return osErrorParameter;
1070     } else {
1071         return osErrorResource;
1072     }
1073 }
1074 
osMutexGetOwner(osMutexId_t mutex_id)1075 osThreadId_t osMutexGetOwner(osMutexId_t mutex_id)
1076 {
1077     UINT32 intSave;
1078     LosTaskCB *pstTaskCB = NULL;
1079 
1080     if (OS_INT_ACTIVE) {
1081         return NULL;
1082     }
1083 
1084     if (mutex_id == NULL) {
1085         return NULL;
1086     }
1087 
1088     intSave = LOS_IntLock();
1089     pstTaskCB = ((LosMuxCB *)mutex_id)->owner;
1090     LOS_IntRestore(intSave);
1091 
1092     return (osThreadId_t)pstTaskCB;
1093 }
1094 
osMutexDelete(osMutexId_t mutex_id)1095 osStatus_t osMutexDelete(osMutexId_t mutex_id)
1096 {
1097     UINT32 ret;
1098 
1099     if (OS_INT_ACTIVE) {
1100         return osErrorISR;
1101     }
1102 
1103     if (mutex_id == NULL) {
1104         return osErrorParameter;
1105     }
1106 
1107     ret = LOS_MuxDelete(((LosMuxCB *)mutex_id)->muxID);
1108     if (ret == LOS_OK) {
1109         return osOK;
1110     } else if (ret == LOS_ERRNO_MUX_INVALID) {
1111         return osErrorParameter;
1112     } else {
1113         return osErrorResource;
1114     }
1115 }
1116 #endif
1117 
1118 //  ==== Semaphore Management Functions ====
1119 #if (LOSCFG_BASE_IPC_SEM == 1)
osSemaphoreNew(uint32_t max_count,uint32_t initial_count,const osSemaphoreAttr_t * attr)1120 osSemaphoreId_t osSemaphoreNew(uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr)
1121 {
1122     UINT32 ret;
1123     UINT32 semId;
1124 
1125     UNUSED(attr);
1126 
1127     if ((initial_count > max_count) || (max_count > OS_SEM_COUNTING_MAX_COUNT) || (max_count == 0) || OS_INT_ACTIVE) {
1128         return NULL;
1129     }
1130 
1131     if (max_count == 1) {
1132         ret = LOS_BinarySemCreate((UINT16)initial_count, &semId);
1133     } else {
1134         ret = LOS_SemCreate((UINT16)initial_count, &semId);
1135     }
1136 
1137     if (ret == LOS_OK) {
1138         return (osSemaphoreId_t)(GET_SEM(semId));
1139     } else {
1140         return (osSemaphoreId_t)NULL;
1141     }
1142 }
1143 
osSemaphoreGetName(osSemaphoreId_t semaphore_id)1144 const char *osSemaphoreGetName(osSemaphoreId_t semaphore_id)
1145 {
1146     UNUSED(semaphore_id);
1147     return NULL;
1148 }
1149 
osSemaphoreAcquire(osSemaphoreId_t semaphore_id,uint32_t timeout)1150 osStatus_t osSemaphoreAcquire(osSemaphoreId_t semaphore_id, uint32_t timeout)
1151 {
1152     LosSemCB *semCB = (LosSemCB *)semaphore_id;
1153     UINT32 ret;
1154 
1155     if ((semCB == NULL) || (OS_INT_ACTIVE && (timeout != 0))) {
1156         return osErrorParameter;
1157     }
1158 
1159     ret = LOS_SemPend(semCB->semID, timeout);
1160     if (ret == LOS_OK) {
1161         return osOK;
1162     } else if (ret == LOS_ERRNO_SEM_INVALID) {
1163         return osErrorParameter;
1164     } else if (ret == LOS_ERRNO_SEM_TIMEOUT) {
1165         return osErrorTimeout;
1166     } else {
1167         return osErrorResource;
1168     }
1169 }
1170 
osSemaphoreRelease(osSemaphoreId_t semaphore_id)1171 osStatus_t osSemaphoreRelease(osSemaphoreId_t semaphore_id)
1172 {
1173     UINT32 ret;
1174 
1175     if (semaphore_id == NULL) {
1176         return osErrorParameter;
1177     }
1178 
1179     ret = LOS_SemPost(((LosSemCB *)semaphore_id)->semID);
1180     if (ret == LOS_OK) {
1181         return osOK;
1182     } else if (ret == LOS_ERRNO_SEM_INVALID) {
1183         return osErrorParameter;
1184     } else {
1185         return osErrorResource;
1186     }
1187 }
1188 
osSemaphoreGetCount(osSemaphoreId_t semaphore_id)1189 uint32_t osSemaphoreGetCount(osSemaphoreId_t semaphore_id)
1190 {
1191     LosSemCB *semCB = (LosSemCB *)semaphore_id;
1192     UINT32 intSave;
1193     UINT16 count;
1194 
1195     if (semCB == NULL) {
1196         return 0;
1197     }
1198 
1199     intSave = LOS_IntLock();
1200     if (semCB->semStat == 0) {
1201         LOS_IntRestore(intSave);
1202         return 0;
1203     }
1204 
1205     count = semCB->semCount;
1206     LOS_IntRestore(intSave);
1207 
1208     return (uint32_t)count;
1209 }
1210 
osSemaphoreDelete(osSemaphoreId_t semaphore_id)1211 osStatus_t osSemaphoreDelete(osSemaphoreId_t semaphore_id)
1212 {
1213     UINT32 ret;
1214 
1215     if (OS_INT_ACTIVE) {
1216         return osErrorISR;
1217     }
1218 
1219     if (semaphore_id == NULL) {
1220         return osErrorParameter;
1221     }
1222 
1223     ret = LOS_SemDelete(((LosSemCB *)semaphore_id)->semID);
1224     if (ret == LOS_OK) {
1225         return osOK;
1226     } else if (ret == LOS_ERRNO_SEM_INVALID) {
1227         return osErrorParameter;
1228     } else {
1229         return osErrorResource;
1230     }
1231 }
1232 #endif
1233 
1234 //  ==== Message Queue Management Functions ====
1235 #if (LOSCFG_BASE_IPC_QUEUE == 1)
1236 typedef enum {
1237     ATTR_CAPACITY = 0,
1238     ATTR_MSGSIZE = 1,
1239     ATTR_COUNT = 2,
1240     ATTR_SPACE = 3
1241 } QueueAttribute;
1242 
osMessageQueueNew(uint32_t msg_count,uint32_t msg_size,const osMessageQueueAttr_t * attr)1243 osMessageQueueId_t osMessageQueueNew(uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr)
1244 {
1245     UINT32 queueId;
1246     UINT32 ret;
1247     osMessageQueueId_t handle;
1248     const char *queueName = NULL;
1249 
1250 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
1251     UINT32 queueSize = 0;
1252     UINT8 *staticMem = NULL;
1253 #endif
1254 
1255     if ((msg_count == 0) || (msg_size == 0) || OS_INT_ACTIVE) {
1256         return (osMessageQueueId_t)NULL;
1257     }
1258 
1259     if (attr != NULL) {
1260         queueName = attr->name;
1261 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
1262         queueSize = attr->mq_size;
1263         staticMem = attr->mq_mem;
1264         if (((queueSize == 0) && (staticMem != NULL)) || ((queueSize != 0) && (staticMem == NULL))) {
1265             return (osMessageQueueId_t)NULL;
1266         }
1267 #endif
1268     }
1269 
1270 #if (LOSCFG_BASE_IPC_QUEUE_STATIC == 1)
1271     if (staticMem != NULL) {
1272         ret = LOS_QueueCreateStatic((const CHAR *)queueName, (UINT16)msg_count, &queueId, \
1273                                     (UINT8 *)staticMem, 0, (UINT16)queueSize / msg_count);
1274     } else {
1275         ret = LOS_QueueCreate((const CHAR *)queueName, (UINT16)msg_count, &queueId, 0, (UINT16)msg_size);
1276     }
1277 #else
1278     ret = LOS_QueueCreate((const CHAR *)queueName, (UINT16)msg_count, &queueId, 0, (UINT16)msg_size);
1279 #endif
1280 
1281     if (ret == LOS_OK) {
1282         handle = (osMessageQueueId_t)(GET_QUEUE_HANDLE(queueId));
1283     } else {
1284         handle = (osMessageQueueId_t)NULL;
1285     }
1286 
1287     return handle;
1288 }
1289 
osMessageQueueOp(osMessageQueueId_t mq_id,VOID * msg_ptr,UINT32 timeout,QueueReadWrite rw)1290 STATIC osStatus_t osMessageQueueOp(osMessageQueueId_t mq_id, VOID *msg_ptr, UINT32 timeout, QueueReadWrite rw)
1291 {
1292     LosQueueCB *queueCB = (LosQueueCB *)mq_id;
1293     UINT32 ret;
1294     UINT32 bufferSize;
1295 
1296     if ((queueCB == NULL) || (msg_ptr == NULL) || (OS_INT_ACTIVE && (timeout != 0))) {
1297         return osErrorParameter;
1298     }
1299 
1300     bufferSize = (UINT32)(queueCB->queueSize - sizeof(UINT32));
1301     if (rw == OS_QUEUE_WRITE) {
1302         ret = LOS_QueueWriteCopy(queueCB->queueID, msg_ptr, bufferSize, timeout);
1303     } else {
1304         ret = LOS_QueueReadCopy(queueCB->queueID, msg_ptr, &bufferSize, timeout);
1305     }
1306 
1307     if (ret == LOS_OK) {
1308         return osOK;
1309     } else if ((ret == LOS_ERRNO_QUEUE_INVALID) || (ret == LOS_ERRNO_QUEUE_NOT_CREATE)) {
1310         return osErrorParameter;
1311     } else if (ret == LOS_ERRNO_QUEUE_TIMEOUT) {
1312         return osErrorTimeout;
1313     } else {
1314         return osErrorResource;
1315     }
1316 }
1317 
osMessageQueuePut(osMessageQueueId_t mq_id,const void * msg_ptr,uint8_t msg_prio,uint32_t timeout)1318 osStatus_t osMessageQueuePut(osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout)
1319 {
1320     UNUSED(msg_prio);
1321     return osMessageQueueOp(mq_id, (VOID *)msg_ptr, (UINT32)timeout, OS_QUEUE_WRITE);
1322 }
1323 
osMessageQueueGet(osMessageQueueId_t mq_id,void * msg_ptr,uint8_t * msg_prio,uint32_t timeout)1324 osStatus_t osMessageQueueGet(osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout)
1325 {
1326     UNUSED(msg_prio);
1327     return osMessageQueueOp(mq_id, (VOID *)msg_ptr, (UINT32)timeout, OS_QUEUE_READ);
1328 }
1329 
osMessageQueueGetAttr(osMessageQueueId_t mq_id,QueueAttribute attr)1330 STATIC UINT16 osMessageQueueGetAttr(osMessageQueueId_t mq_id, QueueAttribute attr)
1331 {
1332     LosQueueCB *queueCB = (LosQueueCB *)mq_id;
1333     UINT16 attrVal = 0;
1334 
1335     if (queueCB == NULL) {
1336         return 0;
1337     }
1338 
1339     if (queueCB->queueState == OS_QUEUE_UNUSED) {
1340         return 0;
1341     }
1342 
1343     switch (attr) {
1344         case ATTR_CAPACITY:
1345             attrVal = queueCB->queueLen;
1346             break;
1347         case ATTR_MSGSIZE:
1348             attrVal = queueCB->queueSize - sizeof(UINT32);
1349             break;
1350         case ATTR_COUNT:
1351             attrVal = queueCB->readWriteableCnt[OS_QUEUE_READ];
1352             break;
1353         case ATTR_SPACE:
1354             attrVal = queueCB->readWriteableCnt[OS_QUEUE_WRITE];
1355             break;
1356         default:
1357             break;
1358     }
1359 
1360     return attrVal;
1361 }
1362 
osMessageQueueGetCapacity(osMessageQueueId_t mq_id)1363 uint32_t osMessageQueueGetCapacity(osMessageQueueId_t mq_id)
1364 {
1365     return (uint32_t)osMessageQueueGetAttr(mq_id, ATTR_CAPACITY);
1366 }
1367 
osMessageQueueGetMsgSize(osMessageQueueId_t mq_id)1368 uint32_t osMessageQueueGetMsgSize(osMessageQueueId_t mq_id)
1369 {
1370     return (uint32_t)osMessageQueueGetAttr(mq_id, ATTR_MSGSIZE);
1371 }
1372 
osMessageQueueGetCount(osMessageQueueId_t mq_id)1373 uint32_t osMessageQueueGetCount(osMessageQueueId_t mq_id)
1374 {
1375     return (uint32_t)osMessageQueueGetAttr(mq_id, ATTR_COUNT);
1376 }
1377 
osMessageQueueGetSpace(osMessageQueueId_t mq_id)1378 uint32_t osMessageQueueGetSpace(osMessageQueueId_t mq_id)
1379 {
1380     return (uint32_t)osMessageQueueGetAttr(mq_id, ATTR_SPACE);
1381 }
1382 
osMessageQueueDelete(osMessageQueueId_t mq_id)1383 osStatus_t osMessageQueueDelete(osMessageQueueId_t mq_id)
1384 {
1385     LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1386     UINT32 ret;
1387 
1388     if (pstQueue == NULL) {
1389         return osErrorParameter;
1390     }
1391 
1392     if (OS_INT_ACTIVE) {
1393         return osErrorISR;
1394     }
1395 
1396     ret = LOS_QueueDelete((UINT32)pstQueue->queueID);
1397     if (ret == LOS_OK) {
1398         return osOK;
1399     } else if (ret == LOS_ERRNO_QUEUE_NOT_FOUND || ret == LOS_ERRNO_QUEUE_NOT_CREATE) {
1400         return osErrorParameter;
1401     } else {
1402         return osErrorResource;
1403     }
1404 }
1405 
osMessageQueueGetName(osMessageQueueId_t mq_id)1406 const char *osMessageQueueGetName(osMessageQueueId_t mq_id)
1407 {
1408     if (mq_id == NULL) {
1409         return NULL;
1410     }
1411 
1412     LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1413 
1414     return (const char *)pstQueue->queueName;
1415 }
1416 #endif
1417 
1418 #define MP_ALLOC        1U
1419 #define MD_ALLOC        2U
1420 #define MEM_POOL_VALID  0xFFEEFF00
1421 
1422 typedef struct {
1423     LOS_MEMBOX_INFO poolInfo;
1424     void            *poolBase;
1425     uint32_t        poolSize;
1426     uint32_t        status;
1427     const char      *name;
1428 } MemPoolCB;
1429 
osMemoryPoolNew(uint32_t block_count,uint32_t block_size,const osMemoryPoolAttr_t * attr)1430 osMemoryPoolId_t osMemoryPoolNew(uint32_t block_count, uint32_t block_size, const osMemoryPoolAttr_t *attr)
1431 {
1432     MemPoolCB *mp = NULL;
1433     const char *name = NULL;
1434     LOS_MEMBOX_NODE *node = NULL;
1435     uint32_t memCB = 0;
1436     uint32_t memMP = 0;
1437     uint32_t size;
1438     uint32_t index;
1439 
1440     if (OS_INT_ACTIVE) {
1441         return NULL;
1442     }
1443 
1444     if ((block_count == 0) || (block_size == 0)) {
1445         return NULL;
1446     }
1447 
1448     size = block_count * block_size;
1449 
1450     if (attr != NULL) {
1451         if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(MemPoolCB))) {
1452             memCB = 1;
1453         }
1454 
1455         if ((attr->mp_mem != NULL) &&
1456             (((UINTPTR)attr->mp_mem & 0x3) == 0) &&  /* 0x3: Check if array is 4-byte aligned. */
1457             (attr->mp_size >= size)) {
1458             memMP = 1;
1459         }
1460         name = attr->name;
1461     }
1462 
1463     if (memCB == 0) {
1464         mp = LOS_MemAlloc(OS_SYS_MEM_ADDR, sizeof(MemPoolCB));
1465         if (mp == NULL) {
1466             return NULL;
1467         }
1468         mp->status = MP_ALLOC;
1469     } else if ((attr != NULL) && (attr->cb_mem != NULL)) {
1470         mp = attr->cb_mem;
1471         mp->status = 0;
1472     } else {
1473         return NULL;
1474     }
1475 
1476     if (memMP == 0) {
1477         mp->poolBase = LOS_MemAlloc(OS_SYS_MEM_ADDR, size);
1478         if ((mp->poolBase == NULL) && (mp->status & MP_ALLOC)) {
1479             (void)LOS_MemFree(OS_SYS_MEM_ADDR, mp);
1480             return NULL;
1481         }
1482         mp->status |= MD_ALLOC;
1483     } else if ((attr != NULL) && (attr->mp_mem != NULL)) {
1484         mp->poolBase = attr->mp_mem;
1485     } else {
1486         if (mp->status & MP_ALLOC) {
1487             (void)LOS_MemFree(OS_SYS_MEM_ADDR, mp);
1488         }
1489         return NULL;
1490     }
1491     mp->poolSize = size;
1492     mp->name = name;
1493     mp->poolInfo.uwBlkCnt = 0;
1494     mp->poolInfo.uwBlkNum = block_count;
1495     mp->poolInfo.uwBlkSize = block_size;
1496 
1497     node = (LOS_MEMBOX_NODE *)mp->poolBase;
1498     mp->poolInfo.stFreeList.pstNext = node;
1499     for (index = 0; index < block_count - 1; ++index) {
1500         node->pstNext = OS_MEMBOX_NEXT(node, block_size);
1501         node = node->pstNext;
1502     }
1503     node->pstNext = NULL;
1504 
1505     mp->status |= MEM_POOL_VALID;
1506 
1507     return mp;
1508 }
1509 
osMemoryPoolAlloc(osMemoryPoolId_t mp_id,uint32_t timeout)1510 void *osMemoryPoolAlloc(osMemoryPoolId_t mp_id, uint32_t timeout)
1511 {
1512     MemPoolCB *mp = (MemPoolCB *)mp_id;
1513     LOS_MEMBOX_NODE *node = NULL;
1514     UINT32 intSave;
1515 
1516     UNUSED(timeout);
1517 
1518     if (mp_id == NULL) {
1519         return NULL;
1520     }
1521 
1522     intSave = LOS_IntLock();
1523     if ((mp->status & MEM_POOL_VALID) == MEM_POOL_VALID) {
1524         node = mp->poolInfo.stFreeList.pstNext;
1525         if (node != NULL) {
1526             mp->poolInfo.stFreeList.pstNext = node->pstNext;
1527             mp->poolInfo.uwBlkCnt++;
1528         }
1529     }
1530     LOS_IntRestore(intSave);
1531 
1532     return node;
1533 }
1534 
osMemoryPoolFree(osMemoryPoolId_t mp_id,void * block)1535 osStatus_t osMemoryPoolFree(osMemoryPoolId_t mp_id, void *block)
1536 {
1537 
1538     MemPoolCB *mp = (MemPoolCB *)mp_id;
1539     LOS_MEMBOX_NODE *node = NULL;
1540     LOS_MEMBOX_NODE *nodeTmp = NULL;
1541     UINT32 intSave;
1542 
1543     if ((mp_id == NULL) || (block == NULL)) {
1544         return osErrorParameter;
1545     }
1546 
1547     intSave = LOS_IntLock();
1548     if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
1549         LOS_IntRestore(intSave);
1550         return osErrorResource;
1551     }
1552 
1553     if (((UINTPTR)block < (UINTPTR)mp->poolBase) ||
1554         ((UINTPTR)block >= ((UINTPTR)mp->poolBase + (UINTPTR)mp->poolSize))) {
1555         LOS_IntRestore(intSave);
1556         return osErrorParameter;
1557     }
1558 
1559     node = (LOS_MEMBOX_NODE *)block;
1560     nodeTmp = mp->poolInfo.stFreeList.pstNext;
1561     mp->poolInfo.stFreeList.pstNext = node;
1562     node->pstNext = nodeTmp;
1563     mp->poolInfo.uwBlkCnt--;
1564     LOS_IntRestore(intSave);
1565 
1566     return osOK;
1567 }
1568 
osMemoryPoolDelete(osMemoryPoolId_t mp_id)1569 osStatus_t osMemoryPoolDelete(osMemoryPoolId_t mp_id)
1570 {
1571     MemPoolCB *mp = (MemPoolCB *)mp_id;
1572     UINT32 intSave;
1573 
1574     if (OS_INT_ACTIVE) {
1575         return osErrorISR;
1576     }
1577 
1578     if (mp_id == NULL) {
1579         return osErrorParameter;
1580     }
1581 
1582     intSave = LOS_IntLock();
1583     if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
1584         LOS_IntRestore(intSave);
1585         return osErrorResource;
1586     }
1587 
1588     if (mp->status & MD_ALLOC) {
1589         (void)LOS_MemFree(OS_SYS_MEM_ADDR, mp->poolBase);
1590         mp->poolBase = NULL;
1591     }
1592 
1593     mp->name = NULL;
1594     mp->status &= ~MEM_POOL_VALID;
1595 
1596     if (mp->status & MP_ALLOC) {
1597         (void)LOS_MemFree(OS_SYS_MEM_ADDR, mp);
1598     }
1599     LOS_IntRestore(intSave);
1600 
1601     return osOK;
1602 }
1603 
osMemoryPoolGetCapacity(osMemoryPoolId_t mp_id)1604 uint32_t osMemoryPoolGetCapacity(osMemoryPoolId_t mp_id)
1605 {
1606     MemPoolCB *mp = (MemPoolCB *)mp_id;
1607     UINT32 intSave;
1608     uint32_t num;
1609 
1610     if (mp_id == NULL) {
1611         return 0;
1612     }
1613 
1614     intSave = LOS_IntLock();
1615     if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
1616         num = 0;
1617     } else {
1618         num = mp->poolInfo.uwBlkNum;
1619     }
1620     LOS_IntRestore(intSave);
1621 
1622     return num;
1623 }
1624 
osMemoryPoolGetBlockSize(osMemoryPoolId_t mp_id)1625 uint32_t osMemoryPoolGetBlockSize(osMemoryPoolId_t mp_id)
1626 {
1627     MemPoolCB *mp = (MemPoolCB *)mp_id;
1628     UINT32 intSave;
1629     uint32_t size;
1630 
1631     if (mp_id == NULL) {
1632         return 0;
1633     }
1634 
1635     intSave = LOS_IntLock();
1636     if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
1637         size = 0;
1638     } else {
1639         size = mp->poolInfo.uwBlkSize;
1640     }
1641     LOS_IntRestore(intSave);
1642 
1643     return size;
1644 }
1645 
osMemoryPoolGetCount(osMemoryPoolId_t mp_id)1646 uint32_t osMemoryPoolGetCount(osMemoryPoolId_t mp_id)
1647 {
1648     MemPoolCB *mp = (MemPoolCB *)mp_id;
1649     UINT32 intSave;
1650     uint32_t count;
1651 
1652     if (mp_id == NULL) {
1653         return 0;
1654     }
1655 
1656     intSave = LOS_IntLock();
1657     if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
1658         count = 0;
1659     } else {
1660         count = mp->poolInfo.uwBlkCnt;
1661     }
1662     LOS_IntRestore(intSave);
1663 
1664     return count;
1665 }
1666 
osMemoryPoolGetSpace(osMemoryPoolId_t mp_id)1667 uint32_t osMemoryPoolGetSpace(osMemoryPoolId_t mp_id)
1668 {
1669     MemPoolCB *mp = (MemPoolCB *)mp_id;
1670     UINT32 intSave;
1671     uint32_t space;
1672 
1673     if (mp_id == NULL) {
1674         return 0;
1675     }
1676 
1677     intSave = LOS_IntLock();
1678     if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
1679         space = 0;
1680     } else {
1681         space = mp->poolInfo.uwBlkNum - mp->poolInfo.uwBlkCnt;
1682     }
1683     LOS_IntRestore(intSave);
1684 
1685     return space;
1686 
1687 }
1688 
osMemoryPoolGetName(osMemoryPoolId_t mp_id)1689 const char *osMemoryPoolGetName(osMemoryPoolId_t mp_id)
1690 {
1691     MemPoolCB *mp = (MemPoolCB *)mp_id;
1692     const char *p = NULL;
1693     UINT32 intSave;
1694 
1695     if (mp_id == NULL) {
1696         return NULL;
1697     }
1698 
1699     if (OS_INT_ACTIVE) {
1700         return NULL;
1701     }
1702 
1703     intSave = LOS_IntLock();
1704     if ((mp->status & MEM_POOL_VALID) == MEM_POOL_VALID) {
1705         p = mp->name;
1706     }
1707     LOS_IntRestore(intSave);
1708 
1709     return p;
1710 }
1711 
1712 //  ==== Thread Flags Functions ====
osThreadFlagsSet(osThreadId_t thread_id,uint32_t flags)1713 uint32_t osThreadFlagsSet(osThreadId_t thread_id, uint32_t flags)
1714 {
1715     LosTaskCB *taskCB = (LosTaskCB *)thread_id;
1716     UINT32 ret;
1717     EVENT_CB_S *eventCB = NULL;
1718     UINT32 eventSave;
1719 
1720     if (taskCB == NULL) {
1721         return (uint32_t)osFlagsErrorParameter;
1722     }
1723 
1724     eventCB = &(taskCB->event);
1725     eventSave = eventCB->uwEventID;
1726     ret = LOS_EventWrite(eventCB, (UINT32)flags);
1727     if (ret == LOS_OK) {
1728         return ((uint32_t)eventSave | flags);
1729     } else if (ret == LOS_ERRNO_EVENT_SETBIT_INVALID) {
1730         return (uint32_t)osFlagsErrorParameter;
1731     } else {
1732         return (uint32_t)osFlagsErrorResource;
1733     }
1734 }
1735 
osThreadFlagsClear(uint32_t flags)1736 uint32_t osThreadFlagsClear(uint32_t flags)
1737 {
1738     UINT32 ret;
1739     UINT32 saveFlags;
1740     LosTaskCB *runTask = NULL;
1741     EVENT_CB_S *eventCB = NULL;
1742 
1743     if (OS_INT_ACTIVE) {
1744         return (uint32_t)osFlagsErrorUnknown;
1745     }
1746 
1747     runTask = g_losTask.runTask;
1748     eventCB = &(runTask->event);
1749     saveFlags = eventCB->uwEventID;
1750 
1751     ret = LOS_EventClear(eventCB, ~(UINT32)flags);
1752     if (ret == LOS_OK) {
1753         return (uint32_t)saveFlags;
1754     }
1755 
1756     return (uint32_t)osFlagsErrorResource;
1757 }
1758 
osThreadFlagsGet(void)1759 uint32_t osThreadFlagsGet(void)
1760 {
1761     LosTaskCB *runTask = NULL;
1762     EVENT_CB_S *eventCB = NULL;
1763 
1764     if (OS_INT_ACTIVE) {
1765         return (uint32_t)osFlagsErrorUnknown;
1766     }
1767 
1768     runTask = g_losTask.runTask;
1769     eventCB = &(runTask->event);
1770 
1771     return (uint32_t)(eventCB->uwEventID);
1772 }
1773 
osThreadFlagsWait(uint32_t flags,uint32_t options,uint32_t timeout)1774 uint32_t osThreadFlagsWait(uint32_t flags, uint32_t options, uint32_t timeout)
1775 {
1776     UINT32 ret;
1777     UINT32 mode = 0;
1778     LosTaskCB *runTask = NULL;
1779     EVENT_CB_S *eventCB = NULL;
1780 
1781     if (OS_INT_ACTIVE) {
1782         return (uint32_t)osFlagsErrorUnknown;
1783     }
1784 
1785     if (options > (osFlagsWaitAny | osFlagsWaitAll | osFlagsNoClear)) {
1786         return (uint32_t)osFlagsErrorParameter;
1787     }
1788 
1789     if ((options & osFlagsWaitAll) == osFlagsWaitAll) {
1790         mode |= LOS_WAITMODE_AND;
1791     } else {
1792         mode |= LOS_WAITMODE_OR;
1793     }
1794 
1795     if ((options & osFlagsNoClear) == osFlagsNoClear) {
1796         mode &= ~LOS_WAITMODE_CLR;
1797     } else {
1798         mode |= LOS_WAITMODE_CLR;
1799     }
1800 
1801     runTask = g_losTask.runTask;
1802     eventCB = &(runTask->event);
1803 
1804     ret = LOS_EventRead(eventCB, (UINT32)flags, mode, (UINT32)timeout);
1805     if (!(ret & LOS_ERRTYPE_ERROR)) {
1806         return (uint32_t)eventCB->uwEventID | ret;
1807     }
1808 
1809     switch (ret) {
1810         case LOS_ERRNO_EVENT_PTR_NULL:
1811         case LOS_ERRNO_EVENT_SETBIT_INVALID:
1812         case LOS_ERRNO_EVENT_EVENTMASK_INVALID:
1813         case LOS_ERRNO_EVENT_FLAGS_INVALID:
1814             return (uint32_t)osFlagsErrorParameter;
1815         case LOS_ERRNO_EVENT_READ_TIMEOUT:
1816             return (uint32_t)osFlagsErrorTimeout;
1817         default:
1818             return (uint32_t)osFlagsErrorResource;
1819     }
1820 }
1821