try using endianness conversion functions instead of icky macros
This commit is contained in:
@@ -138,7 +138,7 @@ void classic_ctrl_event(struct classic_ctrl_t* cc, byte* msg) {
|
||||
for (i = 0; i < 6; ++i)
|
||||
msg[i] = (msg[i] ^ 0x17) + 0x17;
|
||||
|
||||
classic_ctrl_pressed_buttons(cc, BIG_ENDIAN_SHORT(*(short*)(msg + 4)));
|
||||
classic_ctrl_pressed_buttons(cc, from_big_endian_uint16_t(msg + 4));
|
||||
|
||||
/* left/right buttons */
|
||||
l = (((msg[2] & 0x60) >> 2) | ((msg[3] & 0xE0) >> 5));
|
||||
|
||||
@@ -81,14 +81,6 @@ extern FILE* logtarget[];
|
||||
#define RAD_TO_DEGREE(r) ((r * 180.0f) / WIIMOTE_PI)
|
||||
#define DEGREE_TO_RAD(d) (d * (WIIMOTE_PI / 180.0f))
|
||||
|
||||
/* Convert to big endian */
|
||||
#define BIG_ENDIAN_LONG(i) (htonl(i))
|
||||
#define BIG_ENDIAN_SHORT(i) (htons(i))
|
||||
|
||||
/* Convert from big endian */
|
||||
#define FROM_BIG_ENDIAN_LONG(i) (ntohl(i))
|
||||
#define FROM_BIG_ENDIAN_SHORT(i) (ntohs(i))
|
||||
|
||||
#define absf(x) ((x >= 0) ? (x) : (x * -1.0f))
|
||||
#define diff_f(x, y) ((x >= y) ? (absf(x - y)) : (absf(y - x)))
|
||||
|
||||
|
||||
@@ -420,8 +420,8 @@ static void propagate_event(struct wiimote_t* wm, byte event, byte* msg) {
|
||||
void wiiuse_pressed_buttons(struct wiimote_t* wm, byte* msg) {
|
||||
int16_t now;
|
||||
|
||||
/* convert to big endian */
|
||||
now = BIG_ENDIAN_SHORT(*(int16_t*)msg) & WIIMOTE_BUTTON_ALL;
|
||||
/* convert from big endian */
|
||||
now = from_big_endian_uint16_t(msg) & WIIMOTE_BUTTON_ALL;
|
||||
|
||||
/* pressed now & were pressed, then held */
|
||||
wm->btns_held = (now & wm->btns);
|
||||
@@ -489,7 +489,7 @@ static void event_data_read(struct wiimote_t* wm, byte* msg) {
|
||||
}
|
||||
|
||||
len = ((msg[2] & 0xF0) >> 4) + 1;
|
||||
offset = BIG_ENDIAN_SHORT(*(uint16_t*)(msg + 3));
|
||||
offset = from_big_endian_uint16_t(msg + 3);
|
||||
req->addr = (req->addr & 0xFFFF);
|
||||
|
||||
req->wait -= len;
|
||||
@@ -691,7 +691,7 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) {
|
||||
return;
|
||||
}
|
||||
|
||||
id = BIG_ENDIAN_LONG(*(int*)(data + 220));
|
||||
id = from_big_endian_uint32_t(data + 220);
|
||||
|
||||
/* call the corresponding handshake function for this expansion */
|
||||
switch (id) {
|
||||
|
||||
@@ -135,7 +135,7 @@ void guitar_hero_3_event(struct guitar_hero_3_t* gh3, byte* msg) {
|
||||
for (i = 0; i < 6; ++i)
|
||||
msg[i] = (msg[i] ^ 0x17) + 0x17;
|
||||
|
||||
guitar_hero_3_pressed_buttons(gh3, BIG_ENDIAN_SHORT(*(short*)(msg + 4)));
|
||||
guitar_hero_3_pressed_buttons(gh3, from_big_endian_uint16_t(msg + 4));
|
||||
|
||||
/* whammy bar */
|
||||
gh3->whammy_bar = (msg[3] - GUITAR_HERO_3_WHAMMY_BAR_MIN) / (float)(GUITAR_HERO_3_WHAMMY_BAR_MAX - GUITAR_HERO_3_WHAMMY_BAR_MIN);
|
||||
|
||||
@@ -48,7 +48,6 @@
|
||||
*/
|
||||
|
||||
int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data, uint16_t len) {
|
||||
uint16_t *handshake_short;
|
||||
|
||||
/* decrypt data */
|
||||
#ifdef WITH_WIIUSE_DEBUG
|
||||
@@ -68,22 +67,21 @@ int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
handshake_short = (uint16_t*)data;
|
||||
byte * bufptr = data + 4;
|
||||
wb->ctr[0] = unbuffer_big_endian_uint16_t(&bufptr);
|
||||
wb->cbr[0] = unbuffer_big_endian_uint16_t(&bufptr);
|
||||
wb->ctl[0] = unbuffer_big_endian_uint16_t(&bufptr);
|
||||
wb->cbl[0] = unbuffer_big_endian_uint16_t(&bufptr);
|
||||
|
||||
wb->ctr[0] = FROM_BIG_ENDIAN_SHORT(handshake_short[2]);
|
||||
wb->cbr[0] = FROM_BIG_ENDIAN_SHORT(handshake_short[3]);
|
||||
wb->ctl[0] = FROM_BIG_ENDIAN_SHORT(handshake_short[4]);
|
||||
wb->cbl[0] = FROM_BIG_ENDIAN_SHORT(handshake_short[5]);
|
||||
wb->ctr[1] = unbuffer_big_endian_uint16_t(&bufptr);
|
||||
wb->cbr[1] = unbuffer_big_endian_uint16_t(&bufptr);
|
||||
wb->ctl[1] = unbuffer_big_endian_uint16_t(&bufptr);
|
||||
wb->cbl[1] = unbuffer_big_endian_uint16_t(&bufptr);
|
||||
|
||||
wb->ctr[1] = FROM_BIG_ENDIAN_SHORT(handshake_short[6]);
|
||||
wb->cbr[1] = FROM_BIG_ENDIAN_SHORT(handshake_short[7]);
|
||||
wb->ctl[1] = FROM_BIG_ENDIAN_SHORT(handshake_short[8]);
|
||||
wb->cbl[1] = FROM_BIG_ENDIAN_SHORT(handshake_short[9]);
|
||||
|
||||
wb->ctr[2] = FROM_BIG_ENDIAN_SHORT(handshake_short[10]);
|
||||
wb->cbr[2] = FROM_BIG_ENDIAN_SHORT(handshake_short[11]);
|
||||
wb->ctl[2] = FROM_BIG_ENDIAN_SHORT(handshake_short[12]);
|
||||
wb->cbl[2] = FROM_BIG_ENDIAN_SHORT(handshake_short[13]);
|
||||
wb->ctr[2] = unbuffer_big_endian_uint16_t(&bufptr);
|
||||
wb->cbr[2] = unbuffer_big_endian_uint16_t(&bufptr);
|
||||
wb->ctl[2] = unbuffer_big_endian_uint16_t(&bufptr);
|
||||
wb->cbl[2] = unbuffer_big_endian_uint16_t(&bufptr);
|
||||
|
||||
/* handshake done */
|
||||
wm->event = WIIUSE_WII_BOARD_CTRL_INSERTED;
|
||||
@@ -126,11 +124,11 @@ static float do_interpolate(uint16_t raw, uint16_t cal[3]) {
|
||||
* @param msg The message specified in the event packet.
|
||||
*/
|
||||
void wii_board_event(struct wii_board_t* wb, byte* msg) {
|
||||
uint16_t *shmsg = (uint16_t*)(msg);
|
||||
wb->rtr = FROM_BIG_ENDIAN_SHORT(shmsg[0]);
|
||||
wb->rbr = FROM_BIG_ENDIAN_SHORT(shmsg[1]);
|
||||
wb->rtl = FROM_BIG_ENDIAN_SHORT(shmsg[2]);
|
||||
wb->rbl = FROM_BIG_ENDIAN_SHORT(shmsg[3]);
|
||||
byte * bufPtr = msg;
|
||||
wb->rtr = unbuffer_big_endian_uint16_t(&bufPtr);
|
||||
wb->rbr = unbuffer_big_endian_uint16_t(&bufPtr);
|
||||
wb->rtl = unbuffer_big_endian_uint16_t(&bufPtr);
|
||||
wb->rbl = unbuffer_big_endian_uint16_t(&bufPtr);
|
||||
|
||||
/*
|
||||
Interpolate values
|
||||
|
||||
11
src/wiiuse.c
11
src/wiiuse.c
@@ -488,10 +488,10 @@ void wiiuse_send_next_pending_read_request(struct wiimote_t* wm) {
|
||||
return;
|
||||
|
||||
/* the offset is in big endian */
|
||||
*(int32_t*)(buf) = BIG_ENDIAN_LONG(req->addr);
|
||||
to_big_endian_uint32_t(buf, req->addr);
|
||||
|
||||
/* the length is in big endian */
|
||||
*(int16_t*)(buf + 4) = BIG_ENDIAN_SHORT(req->size);
|
||||
to_big_endian_uint16_t(buf + 4, req->size);
|
||||
|
||||
WIIUSE_DEBUG("Request read at address: 0x%x length: %i", req->addr, req->size);
|
||||
wiiuse_send(wm, WM_CMD_READ_DATA, buf, 6);
|
||||
@@ -570,14 +570,15 @@ int wiiuse_write_data(struct wiimote_t* wm, unsigned int addr, byte* data, byte
|
||||
}
|
||||
#endif
|
||||
|
||||
byte * bufPtr = buf;
|
||||
/* the offset is in big endian */
|
||||
*(int*)(buf) = BIG_ENDIAN_LONG(addr);
|
||||
buffer_big_endian_uint32_t(&bufPtr, addr);
|
||||
|
||||
/* length */
|
||||
*(byte*)(buf + 4) = len;
|
||||
buffer_big_endian_uint8_t(&bufPtr, len);
|
||||
|
||||
/* data */
|
||||
memcpy(buf + 5, data, len);
|
||||
memcpy(bufPtr, data, len);
|
||||
|
||||
wiiuse_send(wm, WM_CMD_WRITE_DATA, buf, 21);
|
||||
return 1;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user