cnm_app.c 3.1 KB

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