diff --git a/lib/aos/aos_rpc.c b/lib/aos/aos_rpc.c index 652ec1e..d0072d0 100644 --- a/lib/aos/aos_rpc.c +++ b/lib/aos/aos_rpc.c @@ -126,34 +126,78 @@ aos_rpc_serial_putchar(struct aos_rpc *rpc, char c) { ); } -// putchar for terminal printing is too slow so we send buffers instead -errval_t +// putchar for terminal printing is too slow so we write buffers instead +// the corresponding interface does not know about errors, just partial writes +size_t aos_rpc_serial_write(struct aos_rpc *rpc, const char *buf, size_t buf_len) { errval_t err; // split the buffer into chunks for sending + size_t total_bytes = 0; while(buf_len > 0) { - size_t chunk_len = buf_len; - if (chunk_len > RPC_SHARED_SIZE) { - chunk_len = RPC_SHARED_SIZE; - } + size_t chunk_len = MIN(buf_len, RPC_SHARED_SIZE); memcpy(rpc->shared_mem, buf, chunk_len); + size_t written_bytes; err = do_aos_rpc( rpc, RPC_MTYPE_SERIAL_WRITE, NULL_CAP, chunk_len, 0, 0, - NULL, NULL, NULL, NULL + NULL, NULL, &written_bytes, NULL ); if (err_is_fail(err)) { - return err; + return total_bytes; + } + + total_bytes += written_bytes; + + // if the receiver was not able to write all we have given, return for now + if (written_bytes < chunk_len) { + return total_bytes; } buf += chunk_len; buf_len -= chunk_len; } - return SYS_ERR_OK; + return total_bytes; +} + +// getchar for terminal reading is too slow so we read buffers instead +// the corresponding interface does not know about errors, just partial reads +size_t +aos_rpc_serial_read(struct aos_rpc *rpc, const char *buf, size_t buf_len) { + errval_t err; + + // split the buffer into chunks for reading + size_t total_bytes = 0; + while(buf_len > 0) { + size_t chunk_len = MIN(buf_len, RPC_SHARED_SIZE); + + memcpy(rpc->shared_mem, buf, chunk_len); + + size_t written_bytes; + err = do_aos_rpc( + rpc, RPC_MTYPE_SERIAL_WRITE, + NULL_CAP, chunk_len, 0, 0, + NULL, NULL, &written_bytes, NULL + ); + if (err_is_fail(err)) { + return total_bytes; + } + + total_bytes += written_bytes; + + // if the receiver was not able to write all we have given, return for now + if (written_bytes < chunk_len) { + return total_bytes; + } + + buf += chunk_len; + buf_len -= chunk_len; + } + + return total_bytes; } errval_t