// do all chunk/socket stuff pipe_t serial_flush(pipe_t pipe) { int ret, count; uint8_t len; lob_t packet; uint8_t c1; pipe_serial_t to; if(!pipe || !(to = (pipe_serial_t)pipe->arg)) return NULL; count = 0; while((ret = to->read()) >= 0) { count++; c1 = ret; util_chunks_read(to->chunks, &c1, 1); } if(count) { LOG("read %d bytes from %s",count,pipe->id); } // any incoming full packets can be received while((packet = util_chunks_receive(to->chunks))) mesh_receive(to->net->mesh, packet, pipe); // write the next waiting chunk while((len = util_chunks_len(to->chunks))) { if((ret = to->write(util_chunks_write(to->chunks), len)) > 0) { LOG("wrote %d size chunk to %s",ret,pipe->id); // blocks till next incoming chunk is read before sending more util_chunks_written(to->chunks,len); }else{ LOG("chunk write failed, %d != %d",ret,len); // TODO, write a full chunk of zeros to clear any line errors and reset state? break; } } return pipe; }
// return the next block of data to be written to the stream transport uint8_t *util_chunks_write(util_chunks_t chunks) { if(!util_chunks_len(chunks)) return NULL; return chunks->writing+chunks->writeat; }