|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <kernel.h> |
| 7 | +#include <time.h> |
| 8 | +#include <errno.h> |
| 9 | +#include <string.h> |
| 10 | +#include <misc/printk.h> |
| 11 | + |
| 12 | +#define ACTIVE 1 |
| 13 | +#define NOT_ACTIVE 0 |
| 14 | + |
| 15 | +static void zephyr_timer_wrapper(struct k_timer *timer); |
| 16 | + |
| 17 | +struct timer_obj { |
| 18 | + struct k_timer ztimer; |
| 19 | + void (*sigev_notify_function)(sigval val); |
| 20 | + sigval val; |
| 21 | + struct timespec interval; /* Reload value */ |
| 22 | + u32_t reload; /* Reload value in ms */ |
| 23 | + u32_t status; |
| 24 | +}; |
| 25 | + |
| 26 | +K_MEM_SLAB_DEFINE(posix_timer_slab, sizeof(struct timer_obj), |
| 27 | + CONFIG_MAX_TIMER_COUNT, 4); |
| 28 | + |
| 29 | +static void zephyr_timer_wrapper(struct k_timer *ztimer) |
| 30 | +{ |
| 31 | + struct timer_obj *timer; |
| 32 | + |
| 33 | + timer = (struct timer_obj *)ztimer; |
| 34 | + |
| 35 | + if (timer->reload == 0) { |
| 36 | + timer->status = NOT_ACTIVE; |
| 37 | + } |
| 38 | + |
| 39 | + (timer->sigev_notify_function)(timer->val); |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * @brief Create a per-process timer. |
| 44 | + * |
| 45 | + * This API does not accept SIGEV_THREAD as valid signal event notification |
| 46 | + * type. |
| 47 | + * |
| 48 | + * See IEEE 1003.1 |
| 49 | + */ |
| 50 | +int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid) |
| 51 | +{ |
| 52 | + struct timer_obj *timer; |
| 53 | + |
| 54 | + if (clockid != CLOCK_MONOTONIC || evp == NULL || |
| 55 | + (evp->sigev_notify != SIGEV_NONE && |
| 56 | + evp->sigev_notify != SIGEV_SIGNAL)) { |
| 57 | + errno = EINVAL; |
| 58 | + return -1; |
| 59 | + } |
| 60 | + |
| 61 | + if (k_mem_slab_alloc(&posix_timer_slab, (void **)&timer, 100) == 0) { |
| 62 | + memset(timer, 0, sizeof(struct timer_obj)); |
| 63 | + } else { |
| 64 | + errno = ENOMEM; |
| 65 | + return -1; |
| 66 | + } |
| 67 | + |
| 68 | + timer->sigev_notify_function = evp->sigev_notify_function; |
| 69 | + timer->val = evp->sigev_value; |
| 70 | + timer->interval.tv_sec = 0; |
| 71 | + timer->interval.tv_nsec = 0; |
| 72 | + timer->reload = 0; |
| 73 | + timer->status = NOT_ACTIVE; |
| 74 | + |
| 75 | + if (evp->sigev_notify == SIGEV_NONE) { |
| 76 | + k_timer_init(&timer->ztimer, NULL, NULL); |
| 77 | + } else { |
| 78 | + k_timer_init(&timer->ztimer, zephyr_timer_wrapper, NULL); |
| 79 | + } |
| 80 | + |
| 81 | + *timerid = (timer_t)timer; |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * @brief Get amount of time left for expiration on a per-process timer. |
| 88 | + * |
| 89 | + * See IEEE 1003.1 |
| 90 | + */ |
| 91 | +int timer_gettime(timer_t timerid, struct itimerspec *its) |
| 92 | +{ |
| 93 | + struct timer_obj *timer = (struct timer_obj *)timerid; |
| 94 | + s32_t remaining, leftover; |
| 95 | + s64_t nsecs, secs; |
| 96 | + |
| 97 | + if (timer == NULL) { |
| 98 | + errno = EINVAL; |
| 99 | + return -1; |
| 100 | + } |
| 101 | + |
| 102 | + if (timer->status == ACTIVE) { |
| 103 | + remaining = k_timer_remaining_get(&timer->ztimer); |
| 104 | + secs = remaining / sys_clock_ticks_per_sec; |
| 105 | + leftover = remaining - (secs * sys_clock_ticks_per_sec); |
| 106 | + nsecs = leftover * NSEC_PER_SEC / sys_clock_ticks_per_sec; |
| 107 | + its->it_value.tv_sec = (s32_t) secs; |
| 108 | + its->it_value.tv_nsec = (s32_t) nsecs; |
| 109 | + } else { |
| 110 | + /* Timer is disarmed */ |
| 111 | + its->it_value.tv_sec = 0; |
| 112 | + its->it_value.tv_nsec = 0; |
| 113 | + } |
| 114 | + |
| 115 | + /* The interval last set by timer_settime() */ |
| 116 | + its->it_interval = timer->interval; |
| 117 | + return 0; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * @brief Sets expiration time of per-process timer. |
| 122 | + * |
| 123 | + * See IEEE 1003.1 |
| 124 | + */ |
| 125 | +int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, |
| 126 | + struct itimerspec *ovalue) |
| 127 | +{ |
| 128 | + struct timer_obj *timer = (struct timer_obj *) timerid; |
| 129 | + u32_t duration, current; |
| 130 | + |
| 131 | + if (timer == NULL || |
| 132 | + value->it_interval.tv_nsec < 0 || |
| 133 | + value->it_interval.tv_nsec >= NSEC_PER_SEC || |
| 134 | + value->it_value.tv_nsec < 0 || |
| 135 | + value->it_value.tv_nsec >= NSEC_PER_SEC) { |
| 136 | + errno = EINVAL; |
| 137 | + return -1; |
| 138 | + } |
| 139 | + |
| 140 | + /* Save time to expire and old reload value. */ |
| 141 | + if (ovalue) { |
| 142 | + timer_gettime(timerid, ovalue); |
| 143 | + } |
| 144 | + |
| 145 | + /* Stop the timer if the value is 0 */ |
| 146 | + if ((value->it_value.tv_sec == 0) && (value->it_value.tv_nsec == 0)) { |
| 147 | + if (timer->status == ACTIVE) { |
| 148 | + k_timer_stop(&timer->ztimer); |
| 149 | + } |
| 150 | + |
| 151 | + timer->status = NOT_ACTIVE; |
| 152 | + return 0; |
| 153 | + } |
| 154 | + |
| 155 | + /* Calculate timer period */ |
| 156 | + timer->reload = _ts_to_ms(&value->it_interval); |
| 157 | + timer->interval.tv_sec = value->it_interval.tv_sec; |
| 158 | + timer->interval.tv_nsec = value->it_interval.tv_nsec; |
| 159 | + |
| 160 | + /* Calcaulte timer duration */ |
| 161 | + duration = _ts_to_ms(&(value->it_value)); |
| 162 | + if (flags & TIMER_ABSTIME) { |
| 163 | + current = k_timer_remaining_get(&timer->ztimer); |
| 164 | + |
| 165 | + if (current >= duration) { |
| 166 | + duration = 0; |
| 167 | + } else { |
| 168 | + duration -= current; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + if (timer->status == ACTIVE) { |
| 173 | + k_timer_stop(&timer->ztimer); |
| 174 | + } |
| 175 | + |
| 176 | + timer->status = ACTIVE; |
| 177 | + k_timer_start(&timer->ztimer, duration, timer->reload); |
| 178 | + return 0; |
| 179 | +} |
| 180 | + |
| 181 | +/** |
| 182 | + * @brief Delete a per-process timer. |
| 183 | + * |
| 184 | + * See IEEE 1003.1 |
| 185 | + */ |
| 186 | +int timer_delete(timer_t timerid) |
| 187 | +{ |
| 188 | + struct timer_obj *timer = (struct timer_obj *) timerid; |
| 189 | + |
| 190 | + if (timer == NULL) { |
| 191 | + errno = EINVAL; |
| 192 | + return -1; |
| 193 | + } |
| 194 | + |
| 195 | + if (timer->status == ACTIVE) { |
| 196 | + timer->status = NOT_ACTIVE; |
| 197 | + k_timer_stop(&timer->ztimer); |
| 198 | + } |
| 199 | + |
| 200 | + k_mem_slab_free(&posix_timer_slab, (void *) &timer); |
| 201 | + |
| 202 | + return 0; |
| 203 | +} |
| 204 | + |
0 commit comments