1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #ifndef TIMER_CORE_H 10 #define TIMER_CORE_H 11 12 #include "hdf_base.h" 13 #include "hdf_device_desc.h" 14 #include "osal_mutex.h" 15 #include "timer_if.h" 16 17 #ifdef __cplusplus 18 #if __cplusplus 19 extern "C" { 20 #endif 21 #endif /* __cplusplus */ 22 23 #define CHECK_NULL_PTR_RETURN_VALUE(ptr, ret) do { \ 24 if ((ptr) == NULL) { \ 25 HDF_LOGE("%s:line %d pointer is null and return ret", __func__, __LINE__); \ 26 return (ret); \ 27 } \ 28 } while (0) 29 30 #define CHECK_NULL_PTR_RETURN(ptr) do { \ 31 if ((ptr) == NULL) { \ 32 HDF_LOGE("%s:line %d pointer is null and return", __func__, __LINE__); \ 33 return; \ 34 } \ 35 } while (0) 36 37 #define CHECK_PARSER_RESULT_RETURN_VALUE(ret, str) do { \ 38 if ((ret) != HDF_SUCCESS) { \ 39 HDF_LOGE("%s:line %d %s fail, ret = %d!", __func__, __LINE__, (str), (ret)); \ 40 return HDF_FAILURE; \ 41 } \ 42 } while (0) 43 44 struct TimerInfo { 45 uint32_t number; 46 TimerHandleCb cb; 47 uint32_t useconds; 48 bool isPeriod; 49 }; 50 51 struct TimerCntrl { 52 struct TimerInfo info; 53 struct DListHead node; 54 struct TimerCntrlMethod *ops; 55 void *priv; 56 struct OsalMutex lock; 57 }; 58 59 struct TimerCntrlMethod { 60 int32_t (*Remove)(struct TimerCntrl *cntrl); 61 int32_t (*Open)(struct TimerCntrl *cntrl); 62 int32_t (*Close)(struct TimerCntrl *cntrl); 63 int32_t (*Start)(struct TimerCntrl *cntrl); 64 int32_t (*Stop)(struct TimerCntrl *cntrl); 65 int32_t (*Set)(struct TimerCntrl *cntrl, uint32_t useconds, TimerHandleCb cb); 66 int32_t (*SetOnce)(struct TimerCntrl *cntrl, uint32_t useconds, TimerHandleCb cb); 67 int32_t (*Get)(struct TimerCntrl *cntrl, uint32_t *useconds, bool *isPeriod); 68 }; 69 70 /** 71 * @brief add a timer controller to manager list 72 * @param cntrl Indicates a timer controller. 73 * @constraints: 74 * @return success or fail 75 */ 76 int32_t TimerCntrlAdd(struct TimerCntrl *cntrl); 77 78 /** 79 * @brief remove a timer controller from manager list 80 * @param number Indicates a timer id. 81 * @constraints: 82 * @return success or fail 83 */ 84 int32_t TimerCntrlRemoveByNumber(const uint32_t number); 85 86 /** 87 * @brief Find and return a timer controller by number 88 * @param number Indicates a timer id. 89 * @return a timer controller 90 */ 91 struct TimerCntrl *TimerCntrlOpen(const uint32_t number); 92 93 /** 94 * @brief close a timer controller 95 * @param cntrl Indicates a timer controller. 96 * @return success or fail 97 */ 98 int32_t TimerCntrlClose(struct TimerCntrl *cntrl); 99 100 /** 101 * @brief set a timer controller 102 * @param cntrl Indicates a period timer controller. 103 * @param useconds Indicates a timer timerout time us. 104 * @param cb Indicates a timer callback. 105 * @return success or fail 106 */ 107 int32_t TimerCntrlSet(struct TimerCntrl *cntrl, uint32_t useconds, TimerHandleCb cb); 108 109 /** 110 * @brief set a timer controller 111 * @param cntrl Indicates a timer controller. 112 * @param useconds Indicates a timer timerout time us. 113 * @param cb Indicates a timer callback. 114 * @return success or fail 115 */ 116 int32_t TimerCntrlSetOnce(struct TimerCntrl *cntrl, uint32_t useconds, TimerHandleCb cb); 117 118 /** 119 * @brief get a timer controller 120 * @param cntrl Indicates a timer controller. 121 * @param useconds Represents the timer interval. 122 * @param isPeriod Represents whether the timer call once 123 * @return success or fail 124 */ 125 int32_t TimerCntrlGet(struct TimerCntrl *cntrl, uint32_t *useconds, bool *isPeriod); 126 127 /** 128 * @brief start a timer controller 129 * @param cntrl Indicates a timer controller. 130 * @return success or fail 131 */ 132 int32_t TimerCntrlStart(struct TimerCntrl *cntrl); 133 134 /** 135 * @brief stop a timer controller 136 * @param cntrl Indicates a timer controller. 137 * @return success or fail 138 */ 139 int32_t TimerCntrlStop(struct TimerCntrl *cntrl); 140 141 #ifdef __cplusplus 142 #if __cplusplus 143 } 144 #endif 145 #endif /* __cplusplus */ 146 147 #endif /* TIMER_CORE_H */ 148