-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cbor: CBOR implementation for RIOT-OS (SWP) #1415
Conversation
krf
commented
Jul 14, 2014
More squashing wasn't possible without destroying the logical order of the commits. We could still squash, but then we would end up with just a very few commits. |
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#define CBOR_TYPE_MASK 0xE0 // top 3 bits |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use C style comments (/* … */
).
Blacklist the failing unittests (with insufficient memory where it applies). |
/** | ||
* Convert float @p x to network format | ||
*/ | ||
static float htonf(float x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This operation does not seem safe for arbitrary ABIs. If a floating point number is passed and/or returned in special FPU registers, then the multiple representations for ±NaN might get broken. I'd use int32_t or char[4] to pass the values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed that by using the appropriate uintXX_t types for input/output. Please check again.
What I'd really like for this PR would be feature subsetting, e.g. that the user could decide not to include floating point support. |
Also your code does not need to be defensive. If the user uses the API wrong, then let the application crash mercilessly. We have the macro #ifdef DEVELHELP
if (!s) {
puts("cbor: s==NULL, your application will now crash");
/* No return here. Let it crash. */
}
#endif Input streams and buffers should still be tested, though, or the remote side could exploit the implementation. |
Regarding squashing: I'd say it's OK if there's just one big 'initial import' commit (without looking at the actual change set..). |
I've introduced the following macros to be able to conditionally leave out parts of the CBOR module: I've squashed the PR into a single commit and extended the commit message. Regarding "not writing defensive code": Why is that bad? I really don't like code to be crashy just because we don't check user input. |
@OlegHahm "Blacklist the failing unittests" <- I don't quite understand. Isn't the problem that we're packing too many code into the unittest executable? At least that's how I'm interpreting the error message when running
|
Normally defensive code is a good thing 👍, but not if you have a very limited ROM 👎 like some platforms we are targeting. Like you saw in this PR, the ROM for chronos is overflowing for the unittests. In such an environment you need to trust the programmer to write good code. |
s->data[s->pos++] = major_type | val; | ||
return 1; | ||
} | ||
else if (val <= 0xff) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could refactor everything after the trivial case to first find the length, and then use a loop. Something like (untested):
int result;
if (val <= 0xff) {
major_type |= CBOR_UINT8_FOLLOWS;
result = 2;
}
else …
CBOR_ENSURE_SIZE(s, result);
s->data[s->pos++] = major_type;
for (int i = result - 1; i >= 0; --i) {
s->data[s->pos++] = (val >> (8 * i)) & 0xff;
}
return result;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks for the hint.
This and my changes to decode_int reduces the code size in about 1K on native. Good.
@krf:
Yes, you're interpreting it right. Usually, if an example (or test) application doesn't fit into the RAM (or ROM), we add the board to a black list in the Makefile, e.g. |
I can only add that to |
Okay. New changeset:
What else can I do to get this merged? :) |
I see no show stoppers in here. Well done 👍 |
That's an ACK? |
Squashed + rebased onto master another time. Let me know if there's anything left I can do. |
Debug printing must be disabled by default. |
@krf, ping. |
Updated PR. Note: I'm not sure what is wrong with https://travis-ci.org/RIOT-OS/RIOT/builds/31483750. -- It also happens when running the 'compile_test.py' script for branch 'master'. |
|
return bytes_follow + 1; | ||
} | ||
|
||
static size_t encode_bytes(unsigned char major_type, cbor_stream_t *s, const char *data, size_t length) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long, see https://github.com/RIOT-OS/RIOT/wiki/Coding-conventions#general
Issues fixed |
As far as I can see the Travis error happens already in master - seems to be a problem with q386. @Kijewski, can you take a look?
"And Then There Was Silence" (guess the artist). |
No, the build history is happy: https://travis-ci.org/RIOT-OS/RIOT/builds. |
Hm, so let's merge this? |
Maybe you can squash the formatting commits. |
I give an ACK when suqashed |
This is a malloc-free implementation of the Concise Binary Object Representation (CBOR) data format for the RIOT-OS. This implementation mostly stand-alone, and it should be pretty easy to port to other platforms. We're only using the C STL and some custom network-related functionaliy which could be easily replaced by depending on arpa/inet.h. The CBOR API is straight-forward to use and provides encoding/decoding functionality for all major C types, such as: - int - uint64_t - int64_t - float - double - char* - struct tm - time_t It is possible to conditionally compile this module via CFLAGS: - CBOR_NO_SEMANTIC_TAGGING: All semantic-tagging features removed - CBOR_NO_CTIME: All ctime related features removed - CBOR_NO_FLOAT: All floating-point related features removed - CBOR_NO_PRINT: All features depending on printf removed
Squashed. |
cbor: CBOR implementation for RIOT-OS (SWP)
@OlegHahm, you merged this even though there were outstanding Travis errors. Every build since then fails. |
Since this is a Travis problem (at least to according to the discussion above) and not a problem with this PR, I think this is alright. |
I cannot really investigate the problem, as I failed to get Travis to email me the compiled .elf file. |
What @authmillenon says. |
But it wasn't. https://travis-ci.org/RIOT-OS/RIOT/builds |
I checked locally. Currently I'm not trusting Travis too much. |