1 /* 2 * Copyright (c) 2020 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "acelite_config.h" 17 18 #if (FEATURE_TIMER_MODULE == 1) 19 #include "js_timer_list.h" 20 #include "presets/timer_module.h" 21 #include "ace_mem_base.h" 22 23 namespace OHOS { 24 namespace ACELite { AddTimer(timerHandle_t timerId,Arguments * & arg)25jerry_value_t TimerList::AddTimer(timerHandle_t timerId, Arguments *&arg) 26 { 27 TimerNode *timer = static_cast<TimerNode *>(ace_malloc(sizeof(TimerNode))); 28 if (timer == nullptr) { 29 return UNDEFINED; 30 } 31 uint8_t index = GetIndex(); 32 timer->index = index; 33 timer->next = timerListHead_; 34 timer->timerId = timerId; 35 arg->index = index; 36 timer->arguments = arg; 37 timerListHead_ = timer; 38 timer = nullptr; 39 return jerry_create_number(index); 40 } 41 GetTimer(uint8_t id)42TimerList::TimerNode* TimerList::GetTimer(uint8_t id) 43 { 44 TimerNode *current = timerListHead_; 45 while (current != nullptr) { 46 if (current->index == id) { 47 break; 48 } 49 current = current->next; 50 } 51 return current; 52 } 53 DeleteTimer(uint8_t timer)54void TimerList::DeleteTimer(uint8_t timer) 55 { 56 if (timerListHead_ == nullptr) { 57 return; 58 } 59 TimerNode *current = timerListHead_; 60 TimerNode *preNode = nullptr; 61 while ((timer != current->index) && (current->next != nullptr)) { 62 preNode = current; 63 current = current->next; 64 } 65 if (timer == current->index) { 66 if (current == timerListHead_) { 67 timerListHead_ = current->next; 68 } else { 69 preNode->next = current->next; 70 } 71 ReleaseTimer(current); 72 } 73 } 74 ClearTimerList()75void TimerList::ClearTimerList() 76 { 77 while (timerListHead_ != nullptr) { 78 TimerNode *current = timerListHead_; 79 timerListHead_ = timerListHead_->next; 80 StopTimerTask(current->timerId); 81 ReleaseTimer(current); 82 } 83 } 84 ReleaseTimer(TimerNode * & current)85void TimerList::ReleaseTimer(TimerNode *¤t) 86 { 87 if (current == nullptr) { 88 return; 89 } 90 ReleaseArguments(current->arguments); 91 ace_free(current); 92 current = nullptr; 93 } 94 ReleaseArguments(Arguments * & argument)95void TimerList::ReleaseArguments(Arguments *&argument) 96 { 97 if (argument) { 98 jerry_release_value(argument->func); 99 if ((argument->argsNum > 0) && (argument->args != nullptr)) { 100 for (uint32_t i = 0; i < argument->argsNum; i++) { 101 jerry_release_value((argument->args)[i]); 102 } 103 } 104 ACE_FREE(argument->args); 105 delete argument; 106 argument = nullptr; 107 } 108 } 109 } // namespace ACELite 110 } // namespace OHOS 111 #endif // FEATURE_TIMER_MODULE 112