a serial library to communicate with pebble time's smart strap interface
Dependents: xadow_smartstrap_for_pebble
Diff: encoding.c
- Revision:
- 0:e4dad9e53f06
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/encoding.c Wed Nov 04 09:58:41 2015 +0000 @@ -0,0 +1,46 @@ +#include "encoding.h" + +void encoding_streaming_decode_reset(EncodingStreamingContext *ctx) { + ctx->escape = false; +} + +bool encoding_streaming_decode(EncodingStreamingContext *ctx, uint8_t *data, bool *should_store, + bool *encoding_error) { + bool is_complete = false; + *encoding_error = false; + *should_store = false; + if (*data == ENCODING_FLAG) { + if (ctx->escape) { + // extra escape character before flag + ctx->escape = false; + *encoding_error = true; + } + // we've reached the end of the frame + is_complete = true; + } else if (*data == ENCODING_ESCAPE) { + if (ctx->escape) { + // invalid sequence + ctx->escape = false; + *encoding_error = true; + } else { + // ignore this character and escape the next one + ctx->escape = true; + } + } else { + if (ctx->escape) { + *data ^= ENCODING_ESCAPE_MASK; + ctx->escape = false; + } + *should_store = true; + } + + return is_complete; +} + +bool encoding_encode(uint8_t *data) { + if (*data == ENCODING_FLAG || *data == ENCODING_ESCAPE) { + *data ^= ENCODING_ESCAPE_MASK; + return true; + } + return false; +}