-
Notifications
You must be signed in to change notification settings - Fork 5
/
IoThread.cpp
144 lines (122 loc) · 3.2 KB
/
IoThread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <inttypes.h>
#include <err.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h> // valloc()
#include <sys/select.h>
#include "usbops.hpp"
#include "IoThread.hpp"
// usbfsのバルク転送上限サイズ 16KB
#define TSBULKSIZE 16384
// TS Buffer (8sec * 2040KB/s / 16KB/unit = 1020unit)
#define TSBUFFERSIZE 1020
#define TSSIZE_INVALID (TSBULKSIZE+1)
TsIoThread::TsIoThread (const int fd, const int endpoint)
: fd(fd), endpoint(endpoint), is_cancelled(false),
buf_pushIndex(0), buf_popIndex(0)
{
//buf = new uint8_t[TSBULKSIZE * TSBUFFERSIZE];
buf = (uint8_t*)valloc(TSBULKSIZE * TSBUFFERSIZE);
actualSize = new size_t[TSBUFFERSIZE];
}
TsIoThread::~TsIoThread()
{
//delete [] buf;
free(buf);
delete [] actualSize;
}
void TsIoThread::operator()()
{
for(int i = 0; i < IOREQ_RESERVE_NUM; i++) {
// 非同期URB要求発行
memset(&(urb[i]), 0, sizeof(usbdevfs_urb));
urb[i].type = USBDEVFS_URB_TYPE_BULK;
urb[i].endpoint = endpoint;
SetBufferPtrOfUrb(&(urb[i]));
int r = usb_submiturb(fd, &(urb[i]));
if (r < 0) {
err(1, "can't send urb");
}
}
issuedRequestNum = IOREQ_RESERVE_NUM;
fd_set writefds;
while(!is_cancelled || issuedRequestNum > 0) {
FD_ZERO(&writefds);
FD_SET(fd, &writefds);
int r = select(fd+1, NULL, &writefds, NULL, NULL);
if (r < 0) {
err(1, "select failed.");
}
if (FD_ISSET(fd, &writefds)) {
// usbfsから何か来た
reapUrbs();
}
}
}
void TsIoThread::cancel()
{
is_cancelled = true;
usleep(150000);
if(issuedRequestNum > 0) {
for(int i = 0; i < IOREQ_RESERVE_NUM; i++) {
if(urb[i].buffer_length == 0) continue;
int ret = usb_discardurb(fd, &(urb[i]));
if (ret < 0) {
err(1, "can't discard urb");
}
}
}
}
void TsIoThread::SetBufferPtrOfUrb(usbdevfs_urb* urbp)
{
actualSize[buf_pushIndex] = TSSIZE_INVALID;
urbp->usercontext = &(actualSize[buf_pushIndex]);
urbp->buffer = buf + (buf_pushIndex * TSBULKSIZE);
urbp->buffer_length = TSBULKSIZE;
if(buf_pushIndex < (TSBUFFERSIZE - 1)) buf_pushIndex++;
else buf_pushIndex = 0;
}
void TsIoThread::reapUrbs()
{
usbdevfs_urb *urbp;
while (usb_reapurb_ndelay(fd, &urbp) >= 0) {
// 読み込めた
size_t *actSz = reinterpret_cast<size_t *>(urbp->usercontext);
*actSz = (urbp->status == 0) ? urbp->actual_length : 0;
if(is_cancelled) {
// このURB構造体が発行されていないことを示す
urbp->buffer_length = 0;
issuedRequestNum--;
}else{
// 次の非同期URB要求発行
memset(urbp, 0, sizeof(usbdevfs_urb));
urbp->type = USBDEVFS_URB_TYPE_BULK;
urbp->endpoint = endpoint;
SetBufferPtrOfUrb(urbp);
int r = usb_submiturb(fd, urbp);
if (r < 0) {
err(1, "can't send urb");
}
}
}
}
int TsIoThread::readBuffer(const void **ptr)
{
*ptr = buf + (buf_popIndex * TSBULKSIZE);
if(buf_pushIndex == buf_popIndex)
return 0;
int i = buf_popIndex, remain = -1;
for(; i < TSBUFFERSIZE && i != buf_pushIndex; i++) {
if(actualSize[i] == TSSIZE_INVALID) {
break;
}else if(actualSize[i] < TSBULKSIZE) {
remain = actualSize[i];
break;
}
}
int continuousSize = (i - buf_popIndex) * TSBULKSIZE + ((remain < 0) ? 0 : remain);
if(remain >= 0) i++;
buf_popIndex = (i < TSBUFFERSIZE) ? i : 0;
return continuousSize;
}
/* */