ev3duder  0.3.0
EV3 Downloader/Uploader
bt-unix.c
Go to the documentation of this file.
1 
7 #include <unistd.h>
8 #include <termios.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <stdlib.h>
13 #include <errno.h>
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 
18 #include "defs.h"
20 #define BT "/dev/cu.EV3-SerialPort"
21 // ^ TODO: add ability to find differently named EV3's
28 enum {READ=0, WRITE=1};
29 void *bt_open(const char *file, const char*file2)
30 {
31  int *fd = malloc(2*sizeof(int));
32  if (file2)
33  {
34  fd[READ] = open(file ?: BT, O_RDONLY);
35  fd[WRITE] = open(file ?: BT, O_WRONLY);
36  }else
37  fd[0] = fd[1] = open(file ?: BT, O_RDWR);
38 
39  if (fd[WRITE] == -1 || fd[READ] == -1) return NULL;
40  return fd;
41 }
42 
51 int bt_write(void* fd_, const u8* buf, size_t count)
52 {
53  int fd = ((int*)fd_)[WRITE];
54  buf++;count--; // omit HID report number
55  size_t sent;
56  for (ssize_t ret = sent = 0; sent < count; sent += ret)
57  {
58  ret = write(fd, buf, count-sent);
59  if (ret == -1)
60  {
61  // set some error msg
62  break;
63  }
64  }
65  return sent;
66 }
67 
77 int bt_read(void* fd_, u8* buf, size_t count, int milliseconds)
78 {
79  (void) milliseconds;
80  int fd = ((int*)fd_)[READ];
81  size_t recvd =0;
82  size_t packet_len = 2;
83  ssize_t ret;
84  do {
85  ret = read(fd, buf+recvd, packet_len-recvd);
86  if (ret <= 0)
87  {
88  perror("read failed"); return -1;
89  }
90  }while ((recvd += ret) != 2);
91 
92  packet_len += buf[0] | (buf[1] << 8);
93 
94  if (packet_len > 2*count) //FIXME: remove 2*
95  return -1;
96 
97  for (ssize_t ret=recvd; recvd < packet_len; recvd += ret)
98  {
99  ret = read(fd, buf+recvd, packet_len-recvd);
100  if (ret == 0) break; // EOF; turning off bluetooth during transfer
101  //FIXME: would returning -1 for EOF make more sense?
102  if (ret == -1)
103  {
104  perror("read failed");
105  return -1;
106  }
107  }
108 
109 return recvd;
110 }
111 
116 void bt_close(void *handle)
117 {
118  close(((int*)handle)[WRITE]);
119  close(((int*)handle)[READ]);
120  free(handle);
121 }
127 const wchar_t *bt_error(void* fd_) {
128  (void)fd_;
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) ?
133  werrstr :
134  L"Error in printing error";
135 }
136 
EXTERN void * handle
Definition: ev3_io.h:17
void bt_close(void *handle)
Closes the file descriptor opened by bt_open()
Definition: bt-unix.c:116
void * bt_open(const char *file, const char *file2)
open a bluetooth device described by device. NULL leads to default action
Definition: bt-unix.c:29
int bt_write(void *fd_, const u8 *buf, size_t count)
writes buf[1] till buf[count - 2] to device
Definition: bt-unix.c:51
int bt_read(void *fd_, u8 *buf, size_t count, int milliseconds)
writes buf[1] till buf[count - 2] to device
Definition: bt-unix.c:77
#define BT
default serial port name on OS X
Definition: bt-unix.c:20
Definition: bt-unix.c:28
Definition: bt-unix.c:28
uint8_t u8
Definition: defs.h:9
const wchar_t * bt_error(void *fd_)
Returns an error string describing the last error occured.
Definition: bt-unix.c:127