1 /* 2 * Copyright (c) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. 3 * Copyright (c) 2023-2023 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 #ifndef LWIP_HDR_LOWPOWER_H 32 #define LWIP_HDR_LOWPOWER_H 33 34 #include "lwip/opt.h" 35 36 #if LWIP_LOWPOWER 37 #include "lwip/err.h" 38 #if !NO_SYS 39 #include "lwip/sys.h" 40 #endif 41 42 #define LOWPOWER_TIMER_DEBUG 0 43 #define TIMEOUT_TICK 100 /* 100ms */ 44 #define TIMEOUT_CHECK 30 45 46 #if LOWPOWER_TIMER_DEBUG 47 #define TCP_FASTTMR_NAME , "tcp_fasttmr" 48 #define TCP_SLOWTMR_NAME , "tcp_slowtmr" 49 #define IP_REASSTRM_NAME , "ip_reass_tmr" 50 #define ETHARPTMR_NAME , "etharp_tmr" 51 #define DHCP_COARSETMR_NAME , "dhcp_coarse_tmr" 52 #define DHCP_FINETMR_NAME , "dhcp_fine_tmr" 53 #define AUTOIPTMR_NAME , "autoip_tmr" 54 #define IGMPTMR_NAME , "igmp_tmr" 55 #define DNSTMR_NAME , "dns_tmr" 56 #define NAT64TMR_NAME , "nat64_tmr" 57 #define ND6TMR_NAME , "nd6_tmr" 58 #define IP6_TREASSTMR_NAME , "ip6_reass_tmr" 59 #define MLD6TMR_NAME , "mld6_tmr" 60 #define DHCP6TMR_NAME , "dhcp6_tmr" 61 #define LOWPAN6TMR_NAME , "lowpan6_tmr" 62 #else 63 #define TCP_FASTTMR_NAME 64 #define TCP_SLOWTMR_NAME 65 #define IP_REASSTRM_NAME 66 #define ETHARPTMR_NAME 67 #define DHCP_COARSETMR_NAME 68 #define DHCP_FINETMR_NAME 69 #define AUTOIPTMR_NAME 70 #define IGMPTMR_NAME 71 #define DNSTMR_NAME 72 #define NAT64TMR_NAME 73 #define ND6TMR_NAME 74 #define IP6_TREASSTMR_NAME 75 #define MLD6TMR_NAME 76 #define DHCP6TMR_NAME 77 #define LOWPAN6TMR_NAME 78 #endif 79 80 #define SET_TMR_TICK(tick, val) do { \ 81 if ((val) > 0) { \ 82 if ((tick) == 0) { \ 83 (tick) = (val); \ 84 } else { \ 85 (tick) = (tick) > (val) ? (val) : (tick); \ 86 } \ 87 } \ 88 } while (0) 89 90 typedef void (*lwip_timer_handler)(void); 91 typedef void (*sys_timeout_handler)(void *args); 92 93 /* get time count to trigger */ 94 typedef u32_t (*get_next_timeout)(void); 95 96 struct timer_handler { 97 u32_t interval; 98 lwip_timer_handler handler; 99 get_next_timeout next_tick; 100 #if LOWPOWER_TIMER_DEBUG 101 char *name; 102 #endif 103 }; 104 105 struct timer_entry { 106 u32_t clock_max; /* tmr interval */ 107 u32_t timeout; 108 sys_timeout_handler handler; 109 void *args; 110 get_next_timeout next_tick; 111 struct timer_entry *next; 112 #if LOWPOWER_TIMER_DEBUG 113 char *name; 114 #endif 115 u8_t enable; 116 }; 117 118 enum timer_state { 119 LOW_TMR_GETING_TICKS = 0, 120 LOW_TMR_TIMER_WAITING, 121 LOW_TMR_TIMER_HANDLING, 122 }; 123 124 struct timer_mng { 125 enum timer_state state; 126 u32_t waiting_time; 127 }; 128 129 enum lowpower_msg_type { 130 LOW_NON_BLOCK = 0, 131 LOW_BLOCK = 1, 132 LOW_FORCE_NON_BLOCK = 2, 133 }; 134 135 enum lowpower_mod { 136 LOW_TMR_LOWPOWER_MOD = 0, 137 LOW_TMR_NORMAL_MOD = 1, 138 }; 139 140 #define lowpower_sem_t sys_sem_t 141 #define LOWPOWER_SEM_WAIT(lock) sys_sem_wait(&(lock)) 142 #define LOWPOWER_SIGNAL(lock) sys_sem_signal(&(lock)) 143 #define LOWPOWER_SEM_NEW(lock, val) \ 144 do { \ 145 (val) = sys_sem_new(&(lock), 0); \ 146 } while (0) 147 #define LOWPOWER_SEM_FREE(lock) sys_sem_free(&(lock)) 148 149 /* all timer use the same timeout step */ 150 #define STEP_TIMEOUT_TO_TICK(step, type) (((step) * lwip_cyclic_timers[(type)].interval_ms) / TIMEOUT_TICK) 151 #define STEP_TICK_TO_TIMEOUT(tick, type) (((tick) * TIMEOUT_TICK) / lwip_cyclic_timers[(type)].interval_ms) 152 153 #define LOW_TMR_DELAY 20 154 void sys_timeout_set_wake_time(u32_t val); 155 u32_t sys_timeout_get_wake_time(void); 156 157 void sys_untimeout(sys_timeout_handler handler, void *arg); 158 void sys_restart_timeouts(void); 159 void tcpip_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg); 160 void sys_timeouts_init(void); 161 err_t sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg); 162 u8_t sys_timeout_waiting_long(void); 163 164 void set_lowpower_mod(enum lowpower_mod sw); 165 enum lowpower_mod get_lowpowper_mod(void); 166 167 #endif /* LOWEPOWER */ 168 169 #endif /* LWIP_HDR_LOWPOWER_H */ 170