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