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