Skip to content

Instantly share code, notes, and snippets.

@mrquincle
Last active March 11, 2017 16:01
Show Gist options
  • Save mrquincle/c44b5e6422a407d7a406544ad3b2c80e to your computer and use it in GitHub Desktop.
Save mrquincle/c44b5e6422a407d7a406544ad3b2c80e to your computer and use it in GitHub Desktop.
command_message_t
typedef uint8_t* uint8_t_array;
typedef uint16_t* uint16_t_array;
template<size_t offset = 1>
struct __attribute__((__packed__)) uint16_t_offset_array {
uint8_t skip[offset];
uint16_t_array array;
};
template<size_t M = MAX_COMMAND_MESSAGE_PAYLOAD_LENGTH>
class command_message_t {
// store just a reference to the data
uint8_t * _data;
size_t _length;
const int _max_length = M;
public:
uint8_t *get() {
return _data;
}
void set(uint8_t *data, size_t length) {
assert (length <= _max_length, "too large");
_data = data;
_length = length;
}
uint16_t getType() {
// assume endianness of platform representation
return ((uint16_t_array)_data)[0];
}
void setType(const uint16_t type) {
memcpy(_data, &type, 2);
}
uint8_t numOfIds() {
return _data[2];
}
void get_ids(uint16_t* ids, const size_t length) {
assert (length <= numOfIds, "too many IDs requested");
ids = ((uint16_t_offset_array<3>)_data).array;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment