15 #include <sys/types.h>
20 #define BT "/dev/cu.EV3-SerialPort"
29 void *
bt_open(
const char *file,
const char*file2)
31 int *fd = malloc(2*
sizeof(
int));
34 fd[
READ] = open(file ?:
BT, O_RDONLY);
35 fd[
WRITE] = open(file ?: BT, O_WRONLY);
37 fd[0] = fd[1] = open(file ?:
BT, O_RDWR);
39 if (fd[
WRITE] == -1 || fd[
READ] == -1)
return NULL;
53 int fd = ((
int*)fd_)[
WRITE];
56 for (ssize_t ret = sent = 0; sent < count; sent += ret)
58 ret = write(fd, buf, count-sent);
77 int bt_read(
void* fd_,
u8* buf,
size_t count,
int milliseconds)
80 int fd = ((
int*)fd_)[
READ];
82 size_t packet_len = 2;
85 ret = read(fd, buf+recvd, packet_len-recvd);
88 perror(
"read failed");
return -1;
90 }
while ((recvd += ret) != 2);
92 packet_len += buf[0] | (buf[1] << 8);
94 if (packet_len > 2*count)
97 for (ssize_t ret=recvd; recvd < packet_len; recvd += ret)
99 ret = read(fd, buf+recvd, packet_len-recvd);
104 perror(
"read failed");
118 close(((
int*)handle)[
WRITE]);
119 close(((
int*)handle)[
READ]);
129 const char *errstr = strerror(errno);
130 size_t wlen = strlen(errstr);
131 wchar_t *werrstr = malloc(
sizeof (
wchar_t[wlen]));
132 return (mbstowcs(werrstr, errstr, wlen) != (
size_t)-1) ?
134 L
"Error in printing error";
void bt_close(void *handle)
Closes the file descriptor opened by bt_open()
void * bt_open(const char *file, const char *file2)
open a bluetooth device described by device. NULL leads to default action
int bt_write(void *fd_, const u8 *buf, size_t count)
writes buf[1] till buf[count - 2] to device
int bt_read(void *fd_, u8 *buf, size_t count, int milliseconds)
writes buf[1] till buf[count - 2] to device
#define BT
default serial port name on OS X
const wchar_t * bt_error(void *fd_)
Returns an error string describing the last error occured.