Skip to content

Commit 5ceca38

Browse files
JakubVaneka3f
authored andcommitted
fix(bt-win): add inter-byte timeout support
Suggested by @germanicianus Fixes c4ev3#40
1 parent 312fa64 commit 5ceca38

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/bt-win.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,26 @@ void *bt_open(const char *device, const char *unused)
2222
{
2323
(void) unused;
2424
HANDLE handle = CreateFileA(device ?: BT, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
25-
return handle != INVALID_HANDLE_VALUE ? handle : NULL;
25+
26+
if (handle == INVALID_HANDLE_VALUE)
27+
return NULL;
28+
29+
COMMTIMEOUTS timeouts = {0};
30+
if (!GetCommTimeouts(handle, &timeouts))
31+
{
32+
CloseHandle(handle);
33+
return NULL;
34+
}
35+
36+
timeouts.ReadIntervalTimeout = 100;
37+
38+
if (!SetCommTimeouts(handle, &timeouts))
39+
{
40+
CloseHandle(handle);
41+
return NULL;
42+
}
43+
44+
return handle;
2645
}
2746

2847
/**
@@ -57,6 +76,9 @@ int bt_write(void *handle, const u8 *buf, size_t count)
5776
int bt_read(void *handle, u8 *buf, size_t count, int milliseconds)
5877
{
5978
(void) milliseconds; // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363190%28v=vs.85%29.aspx
79+
80+
// TODO: read size header first, then read remaining bytes (Unix code does this)
81+
6082
DWORD dwBytesRead;
6183
if (!ReadFile(handle, buf, count, &dwBytesRead, NULL))
6284
return -1;

0 commit comments

Comments
 (0)