cnm_app.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2019, Chips&Media
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "vdi_osal.h"
  27. #include "cnm_app.h"
  28. #include "cnm_app_internal.h"
  29. STATIC CNMAppContext appCtx;
  30. Int32 CnmErrorStatus = 0;
  31. void CNMAppInit(void)
  32. {
  33. osal_memset((void*)&appCtx, 0x00, sizeof(CNMAppContext));
  34. osal_init_keyboard();
  35. }
  36. BOOL CNMAppAdd(CNMTask task)
  37. {
  38. CNMTaskContext* taskCtx = (CNMTaskContext*)task;
  39. if (appCtx.numTasks >= MAX_TASKS_IN_APP) {
  40. return FALSE;
  41. }
  42. taskCtx->oneTimeRun = TRUE;
  43. appCtx.taskList[appCtx.numTasks++] = task;
  44. return TRUE;
  45. }
  46. BOOL CNMAppRun(void)
  47. {
  48. CNMTask task;
  49. Uint32 i;
  50. BOOL success;
  51. Uint32 runningTask;
  52. success = TRUE;
  53. runningTask = appCtx.numTasks;
  54. for (i=0; i<appCtx.numTasks; i++) {
  55. task=(CNMTask)appCtx.taskList[i];
  56. if ( FALSE == CNMTaskRun((CNMTask)task))
  57. return FALSE;
  58. }
  59. while (0 < runningTask) {
  60. for (i=0; i<appCtx.numTasks; i++) {
  61. if (NULL == (task=(CNMTask)appCtx.taskList[i])) continue;
  62. if (FALSE == supportThread) {
  63. if (FALSE == CNMTaskRun((CNMTask)task)) break;
  64. }
  65. if (CNM_TASK_RUNNING != CNMTaskWait(task)) {
  66. success &= CNMTaskStop(task);
  67. CNMTaskDestroy(task);
  68. appCtx.taskList[i] = NULL;
  69. runningTask--;
  70. }
  71. }
  72. }
  73. osal_memset((void*)&appCtx, 0x00, sizeof(CNMAppContext));
  74. osal_close_keyboard();
  75. return (0 == CnmErrorStatus) ? success : FALSE;
  76. }
  77. void CNMAppStop(void)
  78. {
  79. Uint32 i;
  80. for (i=0; i<appCtx.numTasks; i++) {
  81. CNMTaskStop(appCtx.taskList[i]);
  82. }
  83. }
  84. void CNMErrorSet(Int32 val)
  85. {
  86. CnmErrorStatus = val;
  87. }
  88. Int32 CNMErrorGet(void)
  89. {
  90. return CnmErrorStatus;
  91. }