bbcmux.c
26.5 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>
#include <io.h>
#include <process.h>
#include <errno.h>
#include <fcntl.h>
#include <Assert.h>
#include <PR/rdb.h>
#include "../bbclocal.h"
/* mux/demux streams for:
*
* data
* debug
*
*/
#define DEV_DATA 0
#define DEV_DEBUG 1
#define MAX_WRITE_SIZE 0x8000
/* Access to shared global variables need to be protected by
a CriticalSection */
struct stream_t {
char *name;
int lfd;
int ifd;
int ofd;
int incoming_ct;
int outgoing_ct;
unsigned int message;
int flag;
#define F_WRITE 1
#define F_READ 2
int write_cmd;
int write_count;
int write_ptr;
unsigned char *write_buf;
} stream[] = {
{ "/tmp/u64_data", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ "/tmp/u64_debug", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
volatile int write_stream = -1; /* current writer */
volatile HANDLE hUsbWrite = INVALID_HANDLE_VALUE;
volatile HANDLE hUsbRead = INVALID_HANDLE_VALUE;
#define USB_BUFSIZE 20480
/*
* Sends a buffer through the USB
* - Returns the number of bytes sent
* - Sets hUSbWrite handle to INVALID_HANDLE_VALUE if error.
*/
static int
send_usb(unsigned char* buf, int buflen)
{
int wlen = 0, rv;
/* Make sure only one thread writes to USB at a time */
BBC_LOG(MSG_DEBUG, "send_usb: Writing %d bytes to USB\n", buflen);
myEnterCriticalSection( &CriticalSection );
/* Check the usb handle */
if (hUsbWrite == INVALID_HANDLE_VALUE) {
myLeaveCriticalSection( &CriticalSection );
BBC_LOG(MSG_ERR, "send_usb: hUsbWrite is invalid\n");
return 0;
}
rv = WriteFile(hUsbWrite, buf, buflen, &wlen, NULL);
if (!rv || (int) wlen != buflen) {
BBC_LOG_SYSERROR("send_usb: Write failed");
/* Set hUsbWrite so we know not to send again,
something bad, bad bad has happened */
hUsbWrite = INVALID_HANDLE_VALUE;
}
myLeaveCriticalSection( &CriticalSection );
BBC_LOG(MSG_DEBUG, "send_usb: wrote %d bytes to USB\n", wlen);
return wlen;
}
/**
* The following three functions implements the RDB protocol
* A normal RDB packet is composed of up to 4 bytes:
* Byte 1 contains the RDB message type (1st 6 bits),
* and the length (last 2 bits)
* Bytes 2-4 make up the RDB data.
*
* For block mode (see rdb_send_block_data), the RDB packet
* can have up to 256 bytes.
* Byte 1 contains the RDB message type (1st 6 bits),
* and the last 2 bits are ignored.
* Byte 2 contains the length (up to 254)
* Bytes 3-254 make up the RDB data.
*
* A typical RDB transaction with the BBplayer will involve the
* the following messages:
*
* The BBPLayer sending data to the PC host:
* RDB_TYPE_GtoH_DATA_CT Normal Sent by the BB player at the start of a data
* transaction to indicate how much data it will
* send.
* RDB_TYPE_GtoH_DATA Normal Data from the BB Player to the host.
* RDB_TYPE_HtoG_DATA_DONE Normal Signals that the host got all the data from
* the BB Player.
*
* Host sending data to BB Player
* (Note: The BBPlayer needs to know before hand how much the host will send):
* RDB_TYPE_GtoH_READY_FOR_DATA Normal Signals that the BB Player is ready for
* for more data from the host.
* RDB_TYPE_HtoG_DATAB Block Used for data that is > 16 bytes
* or
* RDB_TYPE_HtoG_DATA Normal Used for data that is <= 16 bytes
*
*/
/*
* Sends a RDB command (with 0 data bytes) through USB
* - Returns the number of bytes sent (1 if successful, 0 otherwise)
*/
static void
rdb_send_cmd(unsigned char cmd)
{
unsigned char data[4];
int datalen = 0;
memset(data, 0, sizeof data);
data[0] = (cmd << 2); /* no data - length bits are 0 */
datalen = 1;
(void) send_usb(data, datalen);
}
/*
* Sends the RDB data using block mode (see above for format) through USB
* The only block RDB message type is RDB_TYPE_HtoG_DATAB
* - The input buffer is a circular buffer:
* - buf is the pointer to the start of the buffer
* - buflen is the total length of the buffer
* - bufptr is the index of the first valid data in the buffer
* - bufcount is the amount of data currently in the buffer
*/
static int
rdb_send_block_data(unsigned char cmd, unsigned char* buf, int buflen,
int* bufptr, int* bufcount)
{
int i, count, wcount, wtotal, wstart;
static unsigned char data[USB_BUFSIZE];
unsigned char *datap;
int wlen, bufindex;
/* Verify that the cmd is a block data command */
assert(cmd == RDB_TYPE_HtoG_DATAB);
/* Verify that the circular buffer is okay */
assert(buf != NULL);
assert(buflen > 0);
assert(*bufcount <= buflen);
assert(*bufptr >= 0 && *bufptr < buflen);
memset(data, 0, sizeof data);
wcount = *bufcount;
/*
* Clamp write count to size of intermediate buffer and
* only force a USB write if we can handle the entire
* contents of the circular buffer.
*/
if (wcount > 80*254) {
wcount = 80*254;
}
wtotal = 0;
wstart = 0;
bufindex = *bufptr;
datap = &data[0];
while (wtotal < wcount) {
count = wcount - wtotal;
if (count > 254) count = 254;
wstart += 2;
*datap++ = (cmd << 2) | 3;
*datap++ = count;
for(i = 0; i < count; i++) {
*datap++ = buf[bufindex++];
if (bufindex >= buflen)
bufindex = 0;
}
wtotal += count;
}
// BBC_LOG(MSG_ERR, "rdb_send_block_data: Sending %d of %d bytes for %x (total %d)\n",
// wcount, buflen, (int) cmd, wcount+wstart);
wlen = send_usb(data, wcount+wstart);
if (wlen < wcount+wstart) {
/* Error!!!! */
BBC_LOG(MSG_ERR, "rdb_send_block_data: Sending %d of %d bytes, "
"USB only sent %d of %d.\n", wcount, *bufcount,
wlen, wcount+wstart);
return 0;
}
// BBC_LOG(MSG_ERR, "rdb_send_block_data: Done sending %d of %d bytes for %x (total %d), sent %d\n",
// wcount, buflen, (int) cmd, wcount+wstart, wlen);
/* Update the buffer pointer and count of the circular buffer */
*bufptr = bufindex;
*bufcount -= wcount;
return wcount;
}
/*
* Sends the RDB data using normal or block mode through USB
* - The input buffer is a circular buffer:
* - buf is the pointer to the start of the buffer
* - buflen is the total length of the buffer
* - bufptr is the index of the first valid data in the buffer
* - bufcount is the amount of data currently in the buffer
*/
static int
rdb_send_data(unsigned char cmd, unsigned char* buf, int buflen,
int* bufptr, int* bufcount)
{
unsigned char data[256];
int bufindex;
int i, rv, datalen = 0, count = 0;
int bytes_left=0, bytes_sent=0;
/* Verify that the circular buffer is okay */
assert(buf != NULL);
assert(buflen > 0);
assert(*bufcount <= buflen);
assert(*bufptr >= 0 && *bufptr < buflen);
/* Use block mode if there is more than 16 bytes to send */
if (cmd == RDB_TYPE_HtoG_DATA && *bufcount > 16) {
return rdb_send_block_data(RDB_TYPE_HtoG_DATAB, buf, buflen,
bufptr, bufcount);
}
/* Since each normal RDB packet can only have up to 3 bytes of data,
break the buffer into 3 byte data chunks to send */
bufindex = *bufptr;
bytes_left = *bufcount;
while (bytes_left > 0) {
count = (bytes_left > 3)? 3: bytes_left;
data[datalen] = (cmd << 2) | (count&3);
for(i = 1; i <= count; i++) {
data[datalen+i] = buf[bufindex++];
if (bufindex >= buflen)
bufindex = 0;
}
datalen += count + 1;
bytes_sent += count;
bytes_left -= count;
}
rv = send_usb(data, datalen);
if (rv < datalen) {
BBC_LOG(MSG_ERR, "rdb_send_data: Sending buffer of %d bytes, "
"USB only sent %d of %d\n",
*bufcount, rv, datalen);
}
/* Update the buffer pointer and count of the circular buffer */
*bufptr = bufindex;
*bufcount -= bytes_sent;
return bytes_sent;
}
/*
* Sends the data in the write_stream stream, and picks another stream with
* data to send. Returns the number of bytes sent.
*/
static int
send_next() {
int i, count;
unsigned char cmd;
int wlen;
// BBC_LOG(MSG_ERR, "send_next: Enter critical section\n");
myEnterCriticalSection( &CriticalSection );
if (write_stream == -1) {
// BBC_LOG(MSG_ERR, "send_next: No write stream\n");
myLeaveCriticalSection( &CriticalSection );
return 0;
}
count = stream[write_stream].write_count;
cmd = stream[write_stream].write_cmd;
if (count == 0 && cmd == 0) goto next;
BBC_LOG(MSG_DEBUG, "send_next: sending %d bytes to stream %d cmd %x count %d\n",
count, write_stream, cmd, stream[write_stream].write_count);
// Package the data in write_buf as RDB packets and sends it
// wlen is the number of data bytes sent, can be less than
// write_count, but should be > 0
wlen = rdb_send_data(cmd,
stream[write_stream].write_buf,
MAX_WRITE_SIZE,
&stream[write_stream].write_ptr,
&stream[write_stream].write_count);
BBC_LOG(MSG_ALL, "send_next: %d bytes sent to stream %d cmd %x count %d\n",
wlen, write_stream, cmd, stream[write_stream].write_count);
if (stream[write_stream].write_count)
{
BBC_LOG(MSG_DEBUG, "send_next: stream %d, %d bytes remaining\n",
write_stream, stream[write_stream].write_count);
myLeaveCriticalSection( &CriticalSection );
return wlen;
}
/*
* If both outgoing_ct and write_count are zero, then the
* buffer is empty and we can reset to the beginning.
*/
if (stream[write_stream].outgoing_ct == 0) {
stream[write_stream].write_ptr = 0;
if (write_stream == DEV_DATA)
SetEvent(hDeviceWriteDoneEvent);
}
BBC_LOG(MSG_DEBUG, "send_next: stream %d, %d bytes remaining\n",
write_stream, stream[write_stream].write_count);
stream[write_stream].write_cmd = 0;
next:
/* pick new stream, starting at write_stream */
for(i = write_stream;;) {
if (stream[i].write_cmd) {
write_stream = i;
break;
}
if (!stream[++i].name) i = 0;
if (i == write_stream) { /* wrapped */
write_stream = -1;
break;
}
}
BBC_LOG(MSG_ALL, "send_next: new write stream %d\n", write_stream);
myLeaveCriticalSection( &CriticalSection );
return wlen;
}
static int
empty_stream(int s, int warn) {
char buf[256];
if (warn) {
if (stream[s].name)
BBC_LOG(MSG_WARNING, "unexpected input on stream %s\n", stream[s].name);
else
BBC_LOG(MSG_WARNING, "unexpected input on stream %d\n", s);
}
//write(stream[s].ofd, "bah humbug\n", sizeof "bah humbug\n"-1);
return read(stream[s].ifd, buf, sizeof buf);
}
/* Copies data from the input pipe and stores it into the write buffer for sending */
static int
copy_in(int s, int cmd) {
int rv, count = 0;
int wp, wc;
// Check stream index
if ((s < 0) || (s >= sizeof(stream)/sizeof(stream[0]))) {
/* Invalid stream index */
BBC_LOG(MSG_ERR, "copy_in: Invalid stream index %d", s);
return 0;
}
// BBC_LOG(MSG_ERR, "copy_in: Enter critical section\n");
myEnterCriticalSection( &CriticalSection );
wp = stream[s].write_ptr;
wc = stream[s].write_count;
if (stream[s].write_cmd == 0)
stream[s].write_cmd = cmd;
else if (stream[s].write_cmd != cmd) {
/* Some other command has this stream - should wait for it to empty */
/* Return non-zero so we don't close the fd */
BBC_LOG(MSG_WARNING, "copy_in: Stream %d already owned by cmd %d\n",
s, stream[s].write_cmd);
myLeaveCriticalSection( &CriticalSection );
return 1;
}
/* if no space in buffer, return non-zero so we don't close the fd */
if (wc >= MAX_WRITE_SIZE) {
BBC_LOG(MSG_WARNING, "copy_in: Stream %d full\n", s);
myLeaveCriticalSection( &CriticalSection );
return 1;
}
/* Copy data from input pipe into write_buf */
if (wp + wc < MAX_WRITE_SIZE) {
/*
* If there is still space in the buffer after (wp+wc),
* fill up that space first (outgoing_ct bytes at most).
* ------- wc -------- ----n-----
* |---------- xxxxxxxxxxxxxxxxxxxx ----------|
* wp wp+wc MAX_WRITE_SIZE
*/
int n = MAX_WRITE_SIZE-wp-wc;
if (n > stream[s].outgoing_ct) n = stream[s].outgoing_ct;
rv = read(stream[s].ifd, stream[s].write_buf+wp+wc, n);
if (rv < 0) {
BBC_LOG(MSG_ERR, "copy_in: Stream %d error %d\n", s, rv);
myLeaveCriticalSection( &CriticalSection );
return rv;
}
wc += rv;
count += rv;
stream[s].outgoing_ct -= rv;
}
if (wp + wc >= MAX_WRITE_SIZE) {
/*
* If there is still space in the buffer after wrapping wp+wc
* fill up the buffer from the wrap point outgoing_ct bytes at most).
* ------n--------
* |xxxxxxx---------------xxxxxxxxxxxxxxxxxxxx|
* wp+wc wp MAX_WRITE_SIZE
* -MAX_WRITE_SIZE
*/
int n = MAX_WRITE_SIZE-wc;
if (n > stream[s].outgoing_ct) n = stream[s].outgoing_ct;
rv = read(stream[s].ifd, stream[s].write_buf+wc+wp-MAX_WRITE_SIZE, n);
if (rv < 0) {
BBC_LOG(MSG_ERR, "copy_in: Stream %d error %d\n", s, rv);
myLeaveCriticalSection( &CriticalSection );
return rv;
}
wc += rv;
count += rv;
stream[s].outgoing_ct -= rv;
}
/* Update the number of bytes in the write buffer */
stream[s].write_count = wc;
BBC_LOG(MSG_DEBUG, "copy_in: got %d bytes from stream %d\n", count, s);
/* If we finished copying data from the input pipe into the write buffer,
block the stream until we get a response from the BB player */
if (stream[s].outgoing_ct == 0) {
stream[s].flag &= ~F_WRITE;
BBC_LOG(MSG_DEBUG, "copy_in: block stream %d\n", s);
}
/* kick start if necessary */
if (write_stream == -1)
write_stream = s;
myLeaveCriticalSection( &CriticalSection );
// BBC_LOG(MSG_ERR, "copy_in: Leaving critical section\n");
return count;
}
/* Copies data from the USB and writes it to the output pipe
* Sends an acknowledge back to the BB Player if all data is received.
*/
static int
copy_data(const char* buf, int len, int s) {
int rv = 0;
if ((s < 0) || (s >= sizeof(stream)/sizeof(stream[0]))) {
/* Invalid stream index */
BBC_LOG(MSG_ERR, "copy_data: Invalid stream index %d", s);
return BBC_BADARG;
}
//BBC_LOG(MSG_ALL, "copy_data: Enter critical section\n");
if (stream[s].ofd) {
//BBC_LOG(MSG_INFO, "copy_data: got %d bytes, expect %d more\n", len, stream[s].incoming_ct - len);
myEnterCriticalSection( &CriticalSection );
if (stream[s].incoming_ct) {
stream[s].incoming_ct -= len;
if (!stream[s].incoming_ct) {
BBC_LOG(MSG_ALL, "copy_data: incoming_ct zero on stream %d, send ACK\n", s);
rdb_send_cmd((unsigned char) stream[s].message);
}
}
myLeaveCriticalSection( &CriticalSection );
rv = write(stream[s].ofd, buf, len);
//BBC_LOG(MSG_ALL, "copy_data: wrote %d bytes\n", rv);
}
//BBC_LOG(MSG_ALL, "copy_data: Leaving critical section\n");
return rv;
}
/* Called by the USB thread (dousb) to process RDB packets */
int
recv_next(const char* buf, unsigned int buflen) {
unsigned int cmd, len, i;
static int databuf_len = 0, data_count = 0;
static char databuf[USB_BUFSIZE]; /* buffer for DEV_DATA packets */
if (buflen & 0x3) {
BBC_LOG(MSG_ERR, "Wrong RDB length %d %x \n", buflen, buflen);
}
for (i=0; i<buflen; i+=4, buf+=4) {
if (myWaitForSingleObject(hBBCCloseEvent, 0) == WAIT_OBJECT_0) {
// Closing - abort
break;
}
cmd = (buf[0] >> 2) & 0x3f;
len = buf[0] & 3;
switch(cmd) {
case RDB_TYPE_GtoH_READY_FOR_DATA:
myEnterCriticalSection( &CriticalSection );
stream[DEV_DATA].flag |= F_WRITE;
myLeaveCriticalSection( &CriticalSection );
ResetEvent(hDeviceWriteDoneEvent);
SetEvent(hDeviceReadyEvent);
BBC_LOG(MSG_DEBUG, "recv_next: unblock DATA stream\n");
break;
case RDB_TYPE_GtoH_DATA_CT:
{
unsigned int *pcount = (unsigned int*) buf;
int count = ntohl(*pcount) & 0xffffff;
data_count = count;
if (databuf_len) {
copy_data(databuf, databuf_len, DEV_DATA);
databuf_len = 0;
}
myEnterCriticalSection( &CriticalSection );
if (stream[DEV_DATA].incoming_ct != 0)
BBC_LOG(MSG_ERR, "recv_next: Got RDB_TYPE_GtoH_DATA_CT %d while %d still left\n", count, stream[DEV_DATA].incoming_ct);
stream[DEV_DATA].incoming_ct = count;
stream[DEV_DATA].message = RDB_TYPE_HtoG_DATA_DONE;
myLeaveCriticalSection( &CriticalSection );
BBC_LOG(MSG_DEBUG, "recv_next: expecting DATA COUNT %d bytes\n", count);
}
break;
case RDB_TYPE_GtoH_DATA:
if (databuf_len + len > sizeof(databuf)) {
copy_data(databuf, databuf_len, DEV_DATA);
data_count -= databuf_len;
databuf_len = 0;
}
memcpy(&databuf[databuf_len], buf+1, len);
databuf_len += len;
break;
/* NOTE: The following RDB commands are not really supported */
case RDB_TYPE_GtoH_DEBUG:
copy_data(buf+1, len, DEV_DEBUG); break;
case RDB_TYPE_GtoH_DEBUG_DONE:
myEnterCriticalSection( &CriticalSection );
stream[DEV_DEBUG].flag |= F_READ;
myLeaveCriticalSection( &CriticalSection );
break;
case RDB_TYPE_GtoH_DEBUG_READY:
myEnterCriticalSection( &CriticalSection );
stream[DEV_DEBUG].flag |= F_WRITE;
myLeaveCriticalSection( &CriticalSection );
BBC_LOG(MSG_DEBUG, "recv_next: unblock DEV_DEBUG stream\n");
break;
case RDB_TYPE_GtoH_SYNC:
rdb_send_cmd(RDB_TYPE_HtoG_SYNC_DONE); break;
default:
BBC_LOG(MSG_ERR, "recv_next: RDB_TYPE_UNKNOWN %x\n", cmd);
break;
} /* switch */
} /* for */
if (databuf_len && (databuf_len == data_count)) {
copy_data(databuf, databuf_len, DEV_DATA);
data_count = 0;
databuf_len = 0;
}
if (i > buflen) i = buflen;
return i;
}
/*
* Main mux thread - loops reading from pipe from main libbbc application
* thread, formating the data as RDB packets and sending it down the
* USB pipe
*/
int
domux(char *devname, HANDLE hread, HANDLE hwrite, int ifd, int ofd)
{
int i, rv = 0, count = 0;
int dbgpipe[2];
if (_pipe(dbgpipe, MAX_PIPE_DATA, O_BINARY) < 0) {
BBC_LOG_PERROR("domux: pipe failed");
rv = 1;
goto err;
}
close(dbgpipe[0]);
myEnterCriticalSection( &CriticalSection );
hUsbWrite = hwrite;
hUsbRead = hread;
write_stream = -1;
memset(stream, 0, sizeof(stream));
stream[DEV_DATA].name = devname;
stream[DEV_DATA].ifd = ifd;
stream[DEV_DATA].ofd = ofd;
stream[DEV_DATA].lfd = 0;
// DEV_DATA stream will block until the BB Player is ready and sends
// a RDB_TYPE_GtoH_READY_FOR_DATA
// stream[DEV_DATA].flag |= F_WRITE;
stream[DEV_DEBUG].name = "debug";
stream[DEV_DEBUG].ifd = 0;
stream[DEV_DEBUG].ofd = dbgpipe[1];
stream[DEV_DEBUG].lfd = 0;
/* Allocate stream buffers dynamically */
for (i = 0; stream[i].name != NULL; i++) {
stream[i].write_buf = (unsigned char *) malloc(MAX_WRITE_SIZE);
if (stream[i].write_buf == NULL) {
BBC_LOG_SYSERROR("domux: malloc failed");
rv = 1;
myLeaveCriticalSection( &CriticalSection );
goto err;
}
}
myLeaveCriticalSection( &CriticalSection );
SetEvent(hMuxReadyEvent);
for(;;) {
// BBC_LOG(MSG_ALL, "Doing mux loop\n");
/* Check to see if we are closing */
if (myWaitForSingleObject(hBBCCloseEvent, 0) == WAIT_OBJECT_0) {
BBC_LOG(MSG_INFO, "domux: Closing mux pipes\n");
rv = 0;
break;
}
/* To be safe, let's be in the critical section when we
fiddle with the USB Handles too */
myEnterCriticalSection( &CriticalSection );
if ((hUsbWrite == INVALID_HANDLE_VALUE)
|| (hUsbRead == INVALID_HANDLE_VALUE)
|| myWaitForSingleObject(hDeviceErrorEvent, 0) == WAIT_OBJECT_0) {
BBC_LOG(MSG_ERR, "USB Error: Closing mux pipes\n");
/* Something went wrong - make sure both handles are marked invalid */
if (hUsbWrite != INVALID_HANDLE_VALUE) {
hUsbWrite = INVALID_HANDLE_VALUE;
}
if (hUsbRead != INVALID_HANDLE_VALUE) {
hUsbRead = INVALID_HANDLE_VALUE;
}
myLeaveCriticalSection( &CriticalSection );
SetEvent(hDeviceErrorEvent);
rv = 1;
break;
}
myLeaveCriticalSection( &CriticalSection );
for(i = 0; stream[i].name; i++) {
if (stream[i].ifd) {
/* handle request */
// printf("request %s ct %d\n", stream[i].name, stream[i].outgoing_ct);
switch(i) {
case DEV_DATA:
/* Dev data is currently the only working stream */
if (!stream[i].outgoing_ct) {
DWORD dwDeviceWaitResult;
HANDLE hDeviceEvents[2];
hDeviceEvents[0] = hDeviceErrorEvent;
hDeviceEvents[1] = hDeviceDataSendEvent;
// BBC_LOG(MSG_ERR, "domux: Waiting for data to send or error event\n");
/*
* Waits for program to indicate that it's going to send
* more data (at that point the program hasn't sent anything)
* Should be okay assuming that the program sends the
* indicated # of bytes down the pipe, and that
* the pipe is reliable (otherwise, we will block forever on the read)
*/
dwDeviceWaitResult = myWaitForMultipleObjects(
2, /* number of handles in array */
hDeviceEvents, /* array of device-event handles */
FALSE, /* wait until only one is signaled */
100); /* wait for 100 ms */
if (dwDeviceWaitResult == WAIT_OBJECT_0 + 1) {
BBC_LOG(MSG_DEBUG, "domux: reading size\n");
rv = read(stream[i].ifd, &count, sizeof count);
BBC_LOG(MSG_DEBUG, "domux: size read %d\n", count);
myEnterCriticalSection( &CriticalSection );
stream[i].outgoing_ct = count;
myLeaveCriticalSection( &CriticalSection );
if (rv < 0) BBC_LOG_PERROR(stream[i].name);
if (rv <= 0) break;
ResetEvent(hDeviceDataSendEvent);
}
}
myEnterCriticalSection( &CriticalSection );
if ((stream[i].flag & F_WRITE) && stream[i].outgoing_ct)
{
BBC_LOG(MSG_DEBUG, "domux: copy_in %d\n", stream[i].outgoing_ct);
rv = copy_in(i, RDB_TYPE_HtoG_DATA);
BBC_LOG(MSG_DEBUG, "domux: copy_in done\n");
myLeaveCriticalSection( &CriticalSection );
}
else {
myLeaveCriticalSection( &CriticalSection );
/* Nothing to do for now */
continue;
}
break;
case DEV_DEBUG:
rv = copy_in(i, RDB_TYPE_HtoG_DEBUG);
break;
}
if (rv == 0) {
myEnterCriticalSection( &CriticalSection );
close(stream[i].ifd);
stream[i].ifd = 0;
stream[i].ofd = 0;
stream[i].flag &= ~F_WRITE;
stream[i].outgoing_ct = stream[i].write_cmd =
stream[i].write_count = stream[i].write_ptr = 0;
BBC_LOG(MSG_INFO, "domux: close %s\n", stream[i].name);
myLeaveCriticalSection( &CriticalSection );
}
}
}
// Sends the information we've collected down the USB pipe
while (send_next());
} /* for(;;) */
err:
// Time to shut down
myEnterCriticalSection( &CriticalSection );
for(i = 0; stream[i].name; i++) {
if (stream[i].lfd)
close(stream[i].lfd);
if (stream[i].ifd)
close(stream[i].ifd);
if (stream[i].ofd)
close(stream[i].ofd);
//unlink(stream[i].name);
if (stream[i].write_buf != NULL) {
free(stream[i].write_buf);
stream[i].write_buf = NULL;
}
}
write_stream = -1;
memset(stream, 0, sizeof(stream));
myLeaveCriticalSection( &CriticalSection );
return rv;
}
unsigned __stdcall domux_win32(LPVOID lpParameter)
{
int rv = domux(((BBCMuxParam*)lpParameter)->devname,
((BBCMuxParam*)lpParameter)->rh, ((BBCMuxParam*)lpParameter)->wh,
((BBCMuxParam*)lpParameter)->ifd, ((BBCMuxParam*)lpParameter)->ofd);
_endthreadex(rv);
return rv;
}