struct cc_hex_read hex_buf[CC_NUM_HEX_READ];
int hex_count;
+ int show_input;
int remote;
};
}
}
+int cc_default_timeout = 5000;
+
/*
* Flush pending writes, fill pending reads
*/
static int
-_cc_usb_sync(struct cc_usb *cc, int wait_for_input)
+_cc_usb_sync(struct cc_usb *cc, int wait_for_input, int write_timeout)
{
int ret;
struct pollfd fds;
fds.fd = cc->fd;
for (;;) {
if (cc->hex_count || cc->out_count)
- timeout = 5000;
+ timeout = write_timeout;
else if (wait_for_input && cc->in_pos == cc->in_count)
timeout = wait_for_input;
else
cc->in_count += ret;
if (cc->hex_count)
cc_handle_hex_read(cc);
- } else if (ret < 0)
+ if (cc->show_input && cc->in_count) {
+ write(2, cc->in_buf, cc->in_count);
+ cc->in_count = 0;
+ }
+ } else if (ret <= 0) {
perror("read");
+ return -1;
+ }
}
if (fds.revents & POLLOUT) {
ret = write(cc->fd, cc->out_buf,
void
cc_usb_sync(struct cc_usb *cc)
{
- if (_cc_usb_sync(cc, 0) < 0) {
+ if (_cc_usb_sync(cc, 0, cc_default_timeout) < 0) {
fprintf(stderr, "USB link timeout\n");
exit(1);
}
cc_usb_getchar_timeout(struct cc_usb *cc, int timeout)
{
while (cc->in_pos == cc->in_count) {
- if (_cc_usb_sync(cc, timeout) < 0) {
+ if (_cc_usb_sync(cc, timeout, cc_default_timeout) < 0) {
fprintf(stderr, "USB link timeout\n");
exit(1);
}
int
cc_usb_getchar(struct cc_usb *cc)
{
- return cc_usb_getchar_timeout(cc, 5000);
+ return cc_usb_getchar_timeout(cc, cc_default_timeout);
}
void
cc_usb_printf(cc, "\nc F %d\nc c %s\np\nE 0\n", freq, call);
do {
cc->in_count = cc->in_pos = 0;
- _cc_usb_sync(cc, 100);
+ _cc_usb_sync(cc, 100, cc_default_timeout);
} while (cc->in_count > 0);
cc->remote = 1;
}
cc_usb_printf(cc, "\nE 0\nm 0\n");
do {
cc->in_count = cc->in_pos = 0;
- _cc_usb_sync(cc, 100);
+ _cc_usb_sync(cc, 100, cc_default_timeout);
} while (cc->in_count > 0);
return cc;
}
close (cc->fd);
free (cc);
}
+
+int
+cc_usb_write(struct cc_usb *cc, void *buf, int c)
+{
+ uint8_t *b;
+ int this_time;
+
+ b = buf;
+ cc->show_input = 1;
+ while (c > 0) {
+ this_time = c;
+ if (this_time > CC_OUT_BUF - cc->out_count)
+ this_time = CC_OUT_BUF - cc->out_count;
+ memcpy(cc->out_buf + cc->out_count, b, this_time);
+ cc->out_count += this_time;
+ c -= this_time;
+ b += this_time;
+ while (cc->out_count >= CC_OUT_BUF) {
+ _cc_usb_sync(cc, 0, -1);
+ }
+ }
+ return 1;
+}