1 | --- io/hpiod/device.cpp.orig 2006-05-09 19:17:28.000000000 -0400 |
---|
2 | +++ io/hpiod/device.cpp 2006-05-08 02:50:09.000000000 -0400 |
---|
3 | @@ -99,16 +99,88 @@ |
---|
4 | |
---|
5 | int Device::Write(int fd, const void *buf, int size) |
---|
6 | { |
---|
7 | - syslog(LOG_ERR, "error Write: unimplemented (osx) %s %s %d\n", URI, __FILE__, __LINE__); |
---|
8 | + int r, ep; |
---|
9 | + |
---|
10 | + if (FD[fd].pHD == NULL) |
---|
11 | + { |
---|
12 | + syslog(LOG_ERR, "invalid Device::Write state (osx): %s %s %d\n", URI,__FILE__, __LINE__); |
---|
13 | return -1; |
---|
14 | } |
---|
15 | |
---|
16 | + ep = GetOutEP(dev, FD[fd].Config, FD[fd].Interface, FD[fd].AltSetting, USB_ENDPOINT_TYPE_BULK); |
---|
17 | + |
---|
18 | + if (ep < 0) |
---|
19 | + { |
---|
20 | + syslog(LOG_ERR, "invalid bulk out endpoint (osx) %s %s %d\n", URI, __FILE__, __LINE__); |
---|
21 | + return -1; |
---|
22 | + } |
---|
23 | + |
---|
24 | + r = usb_bulk_write(FD[fd].pHD, ep, (char *)buf, size, LIBUSB_TIMEOUT); |
---|
25 | + |
---|
26 | + if (r < 0) |
---|
27 | + { |
---|
28 | + syslog(LOG_ERR, "error Write (osx) %s: %s %d\n", URI, __FILE__, __LINE__); |
---|
29 | + return r; |
---|
30 | + } |
---|
31 | + |
---|
32 | + return r; |
---|
33 | +} |
---|
34 | + |
---|
35 | int Device::Read(int fd, void *buf, int size, int usec) |
---|
36 | { |
---|
37 | - syslog(LOG_ERR, "error Read: unimplemented (osx) %s %s %d\n", URI, __FILE__, __LINE__); |
---|
38 | + int r, ep; |
---|
39 | + struct timeval t1, t2; |
---|
40 | + int total_usec, tmo_usec=usec; |
---|
41 | + |
---|
42 | + if (FD[fd].pHD == NULL) |
---|
43 | + { |
---|
44 | + syslog(LOG_ERR, "invalid Device::Read state (osx): %s %s %d\n", URI,__FILE__, __LINE__); |
---|
45 | + return -2; |
---|
46 | + } |
---|
47 | + |
---|
48 | + if (FD[fd].ucnt) |
---|
49 | + return CutBuf(fd, buf, size); |
---|
50 | + |
---|
51 | + gettimeofday (&t1, NULL); /* get start time */ |
---|
52 | + |
---|
53 | + ep = GetInEP(dev, FD[fd].Config, FD[fd].Interface, FD[fd].AltSetting, USB_ENDPOINT_TYPE_BULK); |
---|
54 | + |
---|
55 | + if (ep < 0) |
---|
56 | + { |
---|
57 | + syslog(LOG_ERR, "invalid bulk in endpoint (osx) %s %s %d\n", URI, __FILE__, __LINE__); |
---|
58 | return -2; |
---|
59 | } |
---|
60 | |
---|
61 | + while (1) |
---|
62 | + { |
---|
63 | + r = usb_bulk_read(FD[fd].pHD, ep, (char *)&FD[fd].ubuf, sizeof(FD[fd].ubuf), tmo_usec/1000); |
---|
64 | + |
---|
65 | + if (r < 0) |
---|
66 | + { |
---|
67 | + syslog(LOG_ERR, "error Read bulk (osx) %s: %s %d\n", URI, __FILE__, __LINE__); |
---|
68 | + return r; |
---|
69 | + } |
---|
70 | + |
---|
71 | + if ((FD[fd].ucnt = r) == 0) |
---|
72 | + { |
---|
73 | + gettimeofday(&t2, NULL); /* get current time */ |
---|
74 | + |
---|
75 | + total_usec = (t2.tv_sec - t1.tv_sec)*1000000; |
---|
76 | + total_usec += (t2.tv_usec > t1.tv_usec) ? t2.tv_usec - t1.tv_usec : t1.tv_usec - t2.tv_usec; |
---|
77 | + if (total_usec > usec) |
---|
78 | + { |
---|
79 | + syslog(LOG_ERR, "error Read bulk timedout (osx) %s: %s %d\n", URI, __FILE__, __LINE__); |
---|
80 | + return -ETIMEDOUT; /* timeout */ |
---|
81 | + } |
---|
82 | + |
---|
83 | + tmo_usec = usec - total_usec; /* decrease timeout */ |
---|
84 | + continue; |
---|
85 | + } |
---|
86 | + |
---|
87 | + return (CutBuf(fd, buf, size)); |
---|
88 | + } |
---|
89 | +} |
---|
90 | + |
---|
91 | #else |
---|
92 | |
---|
93 | /* |
---|