ev3duder  0.3.0
EV3 Downloader/Uploader
bt-win.c
Go to the documentation of this file.
1 
7 #include <stdlib.h>
8 #include <errno.h>
9 
10 #include <windows.h>
11 
12 #include "defs.h"
13 
14 #define BT "COM1"
15 
21 void *bt_open(const char *device , const char* unused)
22 {
23  (void) unused;
24  HANDLE handle = CreateFileA(device ?: BT, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
25  return handle != INVALID_HANDLE_VALUE ? handle : NULL;
26 }
27 
36 int bt_write(void* handle, const u8* buf, size_t count)
37 {
38  DWORD dwBytesWritten;
39 
40  buf++;count--; // omit HID report number
41  if (!WriteFile(handle, buf, count, &dwBytesWritten, NULL))
42  return -1;
43 
44  return dwBytesWritten;
45 }
46 
56 int bt_read(void *handle, u8* buf, size_t count, int milliseconds)
57 {
58  (void) milliseconds; // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363190%28v=vs.85%29.aspx
59  DWORD dwBytesRead;
60  if (!ReadFile(handle, buf, count, &dwBytesRead, NULL))
61  return -1;
62  return dwBytesRead;
63 }
69 void bt_close(void *handle)
70 {
71  CloseHandle(handle);
72 }
73 
79 const wchar_t *bt_error(void* fd_) {
80  (void)fd_;
81  wchar_t **buf = NULL;
82  FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
83  NULL, GetLastError(), 0, (wchar_t*)&buf, 0, NULL); //leaks
84 
85  return buf ? *buf : L"Error in printing error";
86 }
87 
EXTERN void * handle
Definition: ev3_io.h:17
int bt_write(void *handle, const u8 *buf, size_t count)
writes buf[1] till buf[count - 2] to device
Definition: bt-win.c:36
void bt_close(void *handle)
calls CloseHandle on argument
Definition: bt-win.c:69
const wchar_t * bt_error(void *fd_)
Returns an error string describing the last error occured.
Definition: bt-win.c:79
int bt_read(void *handle, u8 *buf, size_t count, int milliseconds)
writes buf[1] till buf[count - 2] to device
Definition: bt-win.c:56
void * bt_open(const char *device, const char *unused)
opens COM Porte described by device. NULL leads to default action
Definition: bt-win.c:21
uint8_t u8
Definition: defs.h:9
#define BT
Definition: bt-win.c:14