try using endianness conversion functions instead of icky macros

This commit is contained in:
Ryan Pavlik
2011-09-02 16:14:27 -05:00
parent 8216db96f6
commit 44612529a7
7 changed files with 73 additions and 39 deletions

View File

@@ -262,6 +262,49 @@ void wiiuse_send_next_pending_read_request(struct wiimote_t* wm);
int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len);
int wiiuse_read_data_cb(struct wiimote_t* wm, wiiuse_read_cb read_cb, byte* buffer, unsigned int offset, uint16_t len);
static inline void to_big_endian_uint8_t(byte * buf, uint8_t val) {
memcpy(buf, &val, 1);
}
static inline uint8_t from_big_endian_uint8_t(byte * buf) {
uint8_t beVal;
memcpy(&beVal, buf, 1);
return beVal;
}
#define WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(_TYPE, _TOBE, _FROMBE) \
static inline void to_big_endian_##_TYPE(byte * buf, _TYPE val) { \
_TYPE beVal = _TOBE(val); \
memcpy(buf, &beVal, sizeof(_TYPE)); \
} \
static inline _TYPE from_big_endian_##_TYPE(byte * buf) { \
_TYPE beVal; \
memcpy(&beVal, buf, sizeof(_TYPE)); \
return _FROMBE(beVal); \
}
WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(uint16_t, htons, ntohs)
WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS(uint32_t, htonl, ntohl)
#undef WIIUSE_DECLARE_ENDIAN_CONVERSION_OPS
#define WIIUSE_DECLARE_BUFFERING_OPS(_TYPE) \
static inline void buffer_big_endian_##_TYPE (byte ** buf, _TYPE val) { \
to_big_endian_##_TYPE(*buf, val); \
*buf += sizeof(_TYPE); \
} \
static inline _TYPE unbuffer_big_endian_##_TYPE (byte ** buf) { \
byte * current = *buf; \
*buf += sizeof(_TYPE); \
return from_big_endian_##_TYPE(current); \
}
WIIUSE_DECLARE_BUFFERING_OPS(uint8_t)
WIIUSE_DECLARE_BUFFERING_OPS(uint16_t)
WIIUSE_DECLARE_BUFFERING_OPS(uint32_t)
#undef WIIUSE_DECLARE_BUFFERING_OPS
#ifdef __cplusplus
}
#endif