Hi,
> Once the second half was filled I would then begin writing it to file
> and start logging data into the first half, and so on.
So a double buffer. Yeah, seems quite normal.
> 1) If I call pb_encoder() on the same stream more than once, does it take
> care of incrementing a pointer to the correct location in the buffer passed
> to pb_ostream_from_buffer()?
Yeah, the stream remembers where it left off.
> 2) Can I simply accumulate stream.bytes_written after each call to
> pb_encode(), and then trigger a filesystem write with that number of bytes
> when I get "close" to the end of the first half of the buffer?
Yes.
Defining 'close' is difficult of course. You could either hardcode
something, or implement your own pb_ostream callback that would handle
the switch at the exact byte position when the first buffer fills up.
> 3) Do I need to close the stream in any way before I call
> pb_ostream_from_buffer() again?
No. Just don't use the old stream anymore after you begin a new one.
> 4) Do I need to do anything in between each call to encode as far as
> separating messages go?
Yes.
If you use nanopb for decoding also, you can simply write a '0' byte
between the messages, e.g. pb_write(&stream, (const uint8_t*)"\0", 1);
However, other decoders don't usually support zero-delimited messages.
So for them you may need to do something more complex, like writing the
length of the message before each message.
If your purpose is to write a log file in nanopb and read it in
something else, I would probably do this:
message LogMessage {
time, date, whatever..
}
message LogFile {
repeated LogMessage messages = 1;
}
Thanks to the way the protocol buffers format works, if you encode
multiple LogFiles after each other without separators, they'll all be
combined into a single message. It's actually fully specified operation
and is called 'merging' in Google's protobuf documentation.
--
Petteri