/* Nanopb input streams for writing/reading data from the GPRS unit. * These implement this simple packet format: * LEN LEN MSGTYPE PAYLOAD CHECKSUM * * LEN LEN is 16-bit length of the rest of the message, big-endian. * MSGTYPE is 8-bit byte corresponding to RPCCommand enumeration in .proto * CHECKSUM is 0xFF xorred with each byte after LEN. */ #pragma once #include #include #include #include #include "paatti.pb.h" typedef struct { pb_istream_t stream; FILE *file; RPCCommand msgtype; uint8_t checksum; } rpc_istream_t; // Reads a packet header from the input stream. // You must call this function prior to using the stream for reading. // After that, you can read stream->msgtype. // stream->file must be set before calling this. // Returns false if read fails or packet header is invalid. bool rpc_istream_start(rpc_istream_t *stream); // Read the packet to end (if not already read) and verify checksum. // Returns false if read fails or checksum is invalid. bool rpc_istream_end(rpc_istream_t *stream); typedef struct { pb_ostream_t stream; FILE *file; uint8_t checksum; } rpc_ostream_t; // Writes a packet header. // stream->file must be set before calling this. // length is the length to be written in the packet header, i.e. payload + 2 bool rpc_ostream_start(rpc_ostream_t *stream, RPCCommand msgtype, size_t length); // Writes packet CRC // Returns false if actual written length was different than expected or // if write failed. bool rpc_ostream_end(rpc_ostream_t *stream);