Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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;
+}