diff --git a/msvc/libdivecomputer.vcproj b/msvc/libdivecomputer.vcproj
index 5dcad41..da5187d 100644
--- a/msvc/libdivecomputer.vcproj
+++ b/msvc/libdivecomputer.vcproj
@@ -398,6 +398,10 @@
RelativePath="..\src\parser.c"
>
+
+
diff --git a/src/Makefile.am b/src/Makefile.am
index b33acd0..e78f37d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -64,7 +64,7 @@ libdivecomputer_la_SOURCES = \
diverite_nitekq.h diverite_nitekq.c diverite_nitekq_parser.c \
citizen_aqualand.h citizen_aqualand.c citizen_aqualand_parser.c \
divesystem_idive.h divesystem_idive.c divesystem_idive_parser.c \
- platform.h \
+ platform.h platform.c \
ringbuffer.h ringbuffer.c \
rbstream.h rbstream.c \
checksum.h checksum.c \
diff --git a/src/irda.c b/src/irda.c
index 2cc58ab..8c5d612 100644
--- a/src/irda.c
+++ b/src/irda.c
@@ -202,11 +202,7 @@ dc_irda_iterator_new (dc_iterator_t **out, dc_context_t *context, dc_descriptor_
// modified by the previous getsockopt call.
size = sizeof (data);
-#ifdef _WIN32
- Sleep (1000);
-#else
- sleep (1);
-#endif
+ dc_platform_sleep (1000);
}
S_CLOSE (fd);
diff --git a/src/platform.c b/src/platform.c
new file mode 100644
index 0000000..16a3c08
--- /dev/null
+++ b/src/platform.c
@@ -0,0 +1,51 @@
+/*
+ * libdivecomputer
+ *
+ * Copyright (C) 2021 Jef Driesen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ */
+
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#define NOGDI
+#include
+#else
+#include
+#include
+#endif
+
+#include "platform.h"
+
+int
+dc_platform_sleep (unsigned int milliseconds)
+{
+#ifdef _WIN32
+ Sleep (milliseconds);
+#else
+ struct timespec ts;
+ ts.tv_sec = (milliseconds / 1000);
+ ts.tv_nsec = (milliseconds % 1000) * 1000000;
+
+ while (nanosleep (&ts, &ts) != 0) {
+ if (errno != EINTR ) {
+ return -1;
+ }
+ }
+#endif
+
+ return 0;
+}
diff --git a/src/platform.h b/src/platform.h
index ab82fb7..438aa7d 100644
--- a/src/platform.h
+++ b/src/platform.h
@@ -45,6 +45,8 @@ extern "C" {
#endif
#endif
+int dc_platform_sleep(unsigned int milliseconds);
+
#ifdef __cplusplus
}
#endif /* __cplusplus */
diff --git a/src/serial_posix.c b/src/serial_posix.c
index 4fbcb96..8872982 100644
--- a/src/serial_posix.c
+++ b/src/serial_posix.c
@@ -30,7 +30,6 @@
#include // fcntl
#include // tcgetattr, tcsetattr, cfsetispeed, cfsetospeed, tcflush, tcsendbreak
#include // ioctl
-#include // nanosleep
#ifdef HAVE_LINUX_SERIAL_H
#include
#endif
@@ -59,6 +58,7 @@
#include "iostream-private.h"
#include "iterator-private.h"
#include "descriptor-private.h"
+#include "platform.h"
#include "timer.h"
#define DIRNAME "/dev"
@@ -995,16 +995,10 @@ dc_serial_get_lines (dc_iostream_t *abstract, unsigned int *value)
static dc_status_t
dc_serial_sleep (dc_iostream_t *abstract, unsigned int timeout)
{
- struct timespec ts;
- ts.tv_sec = (timeout / 1000);
- ts.tv_nsec = (timeout % 1000) * 1000000;
-
- while (nanosleep (&ts, &ts) != 0) {
+ if (dc_platform_sleep (timeout) != 0) {
int errcode = errno;
- if (errcode != EINTR ) {
- SYSERROR (abstract->context, errcode);
- return syserror (errcode);
- }
+ SYSERROR (abstract->context, errcode);
+ return syserror (errcode);
}
return DC_STATUS_SUCCESS;
diff --git a/src/serial_win32.c b/src/serial_win32.c
index 2b009f0..aeeeb83 100644
--- a/src/serial_win32.c
+++ b/src/serial_win32.c
@@ -32,6 +32,7 @@
#include "iostream-private.h"
#include "iterator-private.h"
#include "descriptor-private.h"
+#include "platform.h"
static dc_status_t dc_serial_iterator_next (dc_iterator_t *iterator, void *item);
static dc_status_t dc_serial_iterator_free (dc_iterator_t *iterator);
@@ -836,7 +837,11 @@ dc_serial_get_lines (dc_iostream_t *abstract, unsigned int *value)
static dc_status_t
dc_serial_sleep (dc_iostream_t *abstract, unsigned int timeout)
{
- Sleep (timeout);
+ if (dc_platform_sleep (timeout) != 0) {
+ DWORD errcode = GetLastError ();
+ SYSERROR (abstract->context, errcode);
+ return syserror (errcode);
+ }
return DC_STATUS_SUCCESS;
}
diff --git a/src/socket.c b/src/socket.c
index 84d58a4..a99a8f2 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -20,6 +20,7 @@
*/
#include "socket.h"
+#include "platform.h"
#include "common-private.h"
#include "context-private.h"
@@ -357,21 +358,11 @@ dc_socket_ioctl (dc_iostream_t *abstract, unsigned int request, void *data, size
dc_status_t
dc_socket_sleep (dc_iostream_t *abstract, unsigned int timeout)
{
-#ifdef _WIN32
- Sleep (timeout);
-#else
- struct timespec ts;
- ts.tv_sec = (timeout / 1000);
- ts.tv_nsec = (timeout % 1000) * 1000000;
-
- while (nanosleep (&ts, &ts) != 0) {
- int errcode = errno;
- if (errcode != EINTR ) {
- SYSERROR (abstract->context, errcode);
- return dc_socket_syserror (errcode);
- }
+ if (dc_platform_sleep (timeout) != 0) {
+ s_errcode_t errcode = S_ERRNO;
+ SYSERROR (abstract->context, errcode);
+ return dc_socket_syserror(errcode);
}
-#endif
return DC_STATUS_SUCCESS;
}