|
|
/* Called from OBEX-lib when data needs to be written */ | /* Called from OBEX-lib when data needs to be written */ |
int cobex_write(obex_t *self, void *data, uint8_t *buffer, int length) | int cobex_write(obex_t *self, void *data, uint8_t *buffer, int length) |
{ | { |
int actual; |
int written; |
cobex_t *c; | cobex_t *c; |
return_val_if_fail (self != NULL, -1); | return_val_if_fail (self != NULL, -1); |
return_val_if_fail (data != NULL, -1); | return_val_if_fail (data != NULL, -1); |
|
|
DEBUG(3, "%s() Data %d bytes\n", __func__, length); | DEBUG(3, "%s() Data %d bytes\n", __func__, length); |
| |
if (c->type == CT_ERICSSON || c->type == CT_SIEMENS) { | if (c->type == CT_ERICSSON || c->type == CT_SIEMENS) { |
actual = write(c->fd, buffer, length); |
int retries=0, chunk, fails=0; |
if (actual < length) { |
written = 0; |
DEBUG(1, "Error writing to port (%d expected %d)\n", actual, length); |
for (retries = 0; written < length; retries++) { |
return actual; /* or -1? */ |
chunk = write(c->fd, buffer+written, length-written); |
|
if (chunk <= 0) { |
|
if ( ++fails >= 10 ) { // to avoid infinite looping if something is really wrong |
|
DEBUG(1, "%s() Error writing to port (written %d bytes out of %d, in %d retries)\n", __func__, written, length, retries); |
|
return written; |
|
} |
|
usleep(1); // This mysteriously avoids a resource not available error on write() |
|
} else { |
|
written += chunk; |
|
fails = 0; // Reset error counter on successful write op |
|
} |
} | } |
return actual; |
|
|
if (retries > 0) |
|
DEBUG(2, "%s() Wrote %d bytes in %d retries\n", __func__, written, retries); |
|
return written; |
} | } |
| |
if (c->seq == 0){ | if (c->seq == 0){ |
actual = bfb_send_first(c->fd, buffer, length); |
written = bfb_send_first(c->fd, buffer, length); |
DEBUG(2, "%s() Wrote %d first packets (%d bytes)\n", __func__, actual, length); |
DEBUG(2, "%s() Wrote %d first packets (%d bytes)\n", __func__, written, length); |
} else { | } else { |
actual = bfb_send_next(c->fd, buffer, length, c->seq); |
written = bfb_send_next(c->fd, buffer, length, c->seq); |
DEBUG(2, "%s() Wrote %d packets (%d bytes)\n", __func__, actual, length); |
DEBUG(2, "%s() Wrote %d packets (%d bytes)\n", __func__, written, length); |
} | } |
c->seq++; | c->seq++; |
| |
return actual; |
return written; |
} | } |
| |
/* Called when input data is needed */ | /* Called when input data is needed */ |