diff --git a/libowlps/libowlps.c b/libowlps/libowlps.c index b75f005..3d8bfbf 100644 --- a/libowlps/libowlps.c +++ b/libowlps/libowlps.c @@ -132,6 +132,40 @@ uint_fast8_t owl_frequency_to_channel(const uint_fast16_t channel) /* *** Time *** */ +/* + * Sleeps for a given amount of milliseconds. + * 'time_ms' is an unsigned value, so please be careful: passing a + * negative value may not do what you think. + * In case of error, a message is displayed and a non-zero error code + * is returned. + */ +int owl_msleep(uint32_t time_ms) +{ + int ret ; + uint_fast32_t seconds, microseconds ; + + if (! time_ms) + return 0 ; + + seconds = time_ms / 1000 ; + microseconds = time_ms % 1000 * 1000 ; + + if ((ret = sleep(seconds))) + { + perror("Cannot sleep()") ; + return ret ; + } + + if ((ret = usleep(microseconds))) + { + perror("Cannot usleep()") ; + return ret ; + } + + return 0 ; +} + + /* * Sets the owl_timestamp 'now' at the current time. * Returns 0 in case of success non-zero otherwise. diff --git a/libowlps/owlps.h b/libowlps/owlps.h index 7a90c61..78a8f90 100644 --- a/libowlps/owlps.h +++ b/libowlps/owlps.h @@ -187,6 +187,7 @@ owl_bool owl_mac_equals(const uint8_t *const mac1, uint_fast8_t owl_frequency_to_channel(const uint_fast16_t channel) ; // Time +int owl_msleep(uint32_t time_ms) ; int owl_timestamp_now(owl_timestamp *const now) ; owl_timestamp owl_timespec_to_timestamp(const struct timespec d) ; owl_timestamp owl_timeval_to_timestamp(const struct timeval d) ;