From 713456b563a86c43ce328ab756748a281d52e3ea Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 16:23:10 -0500 Subject: [PATCH 01/17] In expansion handshake, read data after saying we'd like it. --- src/events.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/events.c b/src/events.c index ba6cc3a..2820ec1 100644 --- a/src/events.c +++ b/src/events.c @@ -791,10 +791,9 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) disable_expansion(wm); handshake_buf = malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); - wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); - /* tell the wiimote to send expansion data */ WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP); + wiiuse_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); break; case 3: if(!data || !len) { From e8a7ad62fc94ce8ee48ba9d14ce4b7b07eee17f7 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 16:23:56 -0500 Subject: [PATCH 02/17] If no handshake data received from the expansion, disable it. Should give it a chance to re-handshake instead of getting stuck in "case 3" --- src/events.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/events.c b/src/events.c index 2820ec1..884f313 100644 --- a/src/events.c +++ b/src/events.c @@ -798,6 +798,7 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { case 3: if(!data || !len) { WIIUSE_DEBUG("no handshake data received from expansion"); + disable_expansion(wm); return; } id = from_big_endian_uint32_t(data + 220); From 3d21a258228997dd46d9ae803fdfc97a46ef2957 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 16:24:50 -0500 Subject: [PATCH 03/17] Make disable_expansion reset expansion state to 0. Lets you unplug expansions and replug or plug a different one. --- src/events.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/events.c b/src/events.c index 884f313..6e06b5f 100644 --- a/src/events.c +++ b/src/events.c @@ -890,6 +890,7 @@ void disable_expansion(struct wiimote_t* wm) { WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP); wm->exp.type = EXP_NONE; + wm->expansion_state = 0; } From 2f6901cde3f74f28283dea5daa7530531d21205c Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 16:26:30 -0500 Subject: [PATCH 04/17] Only move from WIIMOTE_STATE_EXP_HANDSHAKE to WIIMOTE_STATE_EXP if the expansion's handshake code was successful. --- src/events.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/events.c b/src/events.c index 6e06b5f..c856a2a 100644 --- a/src/events.c +++ b/src/events.c @@ -762,6 +762,7 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { byte val = 0; byte buf = 0x00; byte* handshake_buf; + int gotIt = 0; switch(wm->expansion_state) { /* These two initialization writes disable the encryption */ @@ -804,18 +805,24 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { id = from_big_endian_uint32_t(data + 220); switch(id) { case EXP_ID_CODE_NUNCHUK: - if (nunchuk_handshake(wm, &wm->exp.nunchuk, data, len)) + if (nunchuk_handshake(wm, &wm->exp.nunchuk, data, len)) { wm->event = WIIUSE_NUNCHUK_INSERTED; + gotIt = 1; + } break; case EXP_ID_CODE_CLASSIC_CONTROLLER: - if (classic_ctrl_handshake(wm, &wm->exp.classic, data, len)) + if (classic_ctrl_handshake(wm, &wm->exp.classic, data, len)) { wm->event = WIIUSE_CLASSIC_CTRL_INSERTED; + gotIt = 1; + } break; case EXP_ID_CODE_GUITAR: - if (guitar_hero_3_handshake(wm, &wm->exp.gh3, data, len)) + if (guitar_hero_3_handshake(wm, &wm->exp.gh3, data, len)) { wm->event = WIIUSE_GUITAR_HERO_3_CTRL_INSERTED; + gotIt = 1; + } break; case EXP_ID_CODE_MOTION_PLUS: @@ -823,11 +830,14 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { case EXP_ID_CODE_MOTION_PLUS_NUNCHUK: /* wiiuse_motion_plus_handshake(wm, data, len); */ wm->event = WIIUSE_MOTION_PLUS_ACTIVATED; + gotIt = 1; break; case EXP_ID_CODE_WII_BOARD: - if(wii_board_handshake(wm, &wm->exp.wb, data, len)) - wm->event = WIIUSE_WII_BOARD_CTRL_INSERTED; + if(wii_board_handshake(wm, &wm->exp.wb, data, len)) { + wm->event = WIIUSE_WII_BOARD_CTRL_INSERTED; + gotIt = 1; + } break; default: @@ -835,8 +845,12 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { break; } free(data); - WIIMOTE_DISABLE_STATE(wm,WIIMOTE_STATE_EXP_HANDSHAKE); - WIIMOTE_ENABLE_STATE(wm,WIIMOTE_STATE_EXP); + if (gotIt) { + WIIMOTE_DISABLE_STATE(wm,WIIMOTE_STATE_EXP_HANDSHAKE); + WIIMOTE_ENABLE_STATE(wm,WIIMOTE_STATE_EXP); + } else { + WIIUSE_WARNING("Could not handshake with expansion id: 0x%x", id); + } wiiuse_set_ir_mode(wm); wiiuse_status(wm); break; From f9bde9421060acb79bd51e6d3195e62eb84be81f Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 16:50:40 -0500 Subject: [PATCH 05/17] Comment out decryption in nunchuk - needed to work with a simulated Nunchuk. Not sure why this was needed - will test with a real nunchuk. --- src/nunchuk.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/nunchuk.c b/src/nunchuk.c index 94f9c46..b080e9b 100644 --- a/src/nunchuk.c +++ b/src/nunchuk.c @@ -61,9 +61,10 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un nc->accel_calib.st_alpha = wm->accel_calib.st_alpha; /* decrypt data */ +/* for (i = 0; i < len; ++i) data[i] = (data[i] ^ 0x17) + 0x17; - +*/ if (data[offset] == 0xFF) { /* * Sometimes the data returned here is not correct. @@ -139,9 +140,10 @@ void nunchuk_event(struct nunchuk_t* nc, byte* msg) { int i; /* decrypt data */ +/* for (i = 0; i < 6; ++i) msg[i] = (msg[i] ^ 0x17) + 0x17; - +*/ /* get button states */ nunchuk_pressed_buttons(nc, msg[5]); From f24cfb6e5a67a9325778c21f5aeabd7904f6edb3 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 16:53:26 -0500 Subject: [PATCH 06/17] Fully remove nunchuk decryption code: verified with real nunchuk. --- src/nunchuk.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/nunchuk.c b/src/nunchuk.c index b080e9b..e5ff240 100644 --- a/src/nunchuk.c +++ b/src/nunchuk.c @@ -61,10 +61,7 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un nc->accel_calib.st_alpha = wm->accel_calib.st_alpha; /* decrypt data */ -/* - for (i = 0; i < len; ++i) - data[i] = (data[i] ^ 0x17) + 0x17; -*/ + if (data[offset] == 0xFF) { /* * Sometimes the data returned here is not correct. @@ -139,11 +136,6 @@ void nunchuk_disconnected(struct nunchuk_t* nc) { void nunchuk_event(struct nunchuk_t* nc, byte* msg) { int i; - /* decrypt data */ -/* - for (i = 0; i < 6; ++i) - msg[i] = (msg[i] ^ 0x17) + 0x17; -*/ /* get button states */ nunchuk_pressed_buttons(nc, msg[5]); From f315d20bd68b437a7e1ad090f0fadcfd463fae46 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 16:59:27 -0500 Subject: [PATCH 07/17] Based on my nunchuk findings, remove decryption from classic and gh3 --- src/classic.c | 4 ---- src/guitar_hero_3.c | 4 ---- 2 files changed, 8 deletions(-) diff --git a/src/classic.c b/src/classic.c index d543409..cd2f914 100644 --- a/src/classic.c +++ b/src/classic.c @@ -59,10 +59,6 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte cc->r_shoulder = 0; cc->l_shoulder = 0; - /* decrypt data */ - for (i = 0; i < len; ++i) - data[i] = (data[i] ^ 0x17) + 0x17; - if (data[offset] == 0xFF) { /* * Sometimes the data returned here is not correct. diff --git a/src/guitar_hero_3.c b/src/guitar_hero_3.c index 4b582db..cefe5ca 100644 --- a/src/guitar_hero_3.c +++ b/src/guitar_hero_3.c @@ -65,10 +65,6 @@ int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, b gh3->btns_released = 0; gh3->whammy_bar = 0.0f; - /* decrypt data */ - for (i = 0; i < len; ++i) - data[i] = (data[i] ^ 0x17) + 0x17; - if (data[offset] == 0xFF) { /* * Sometimes the data returned here is not correct. From 97eb4d2557746f1d9181f44052e0a153a0ea6235 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 17:00:01 -0500 Subject: [PATCH 08/17] Cleanup use of calib data. --- src/classic.c | 31 +++++++++++++++---------------- src/guitar_hero_3.c | 11 +++++++---- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/classic.c b/src/classic.c index cd2f914..69869a8 100644 --- a/src/classic.c +++ b/src/classic.c @@ -51,7 +51,6 @@ static void classic_ctrl_pressed_buttons(struct classic_ctrl_t* cc, short now); */ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte* data, unsigned short len) { int i; - int offset = 0; cc->btns = 0; cc->btns_held = 0; @@ -59,7 +58,7 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte cc->r_shoulder = 0; cc->l_shoulder = 0; - if (data[offset] == 0xFF) { + if (data[0] == 0xFF) { /* * Sometimes the data returned here is not correct. * This might happen because the wiimote is lagging @@ -70,7 +69,7 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte * but since the next 16 bytes are the same, just use * those. */ - if (data[offset + 16] == 0xFF) { + if (data[16] == 0xFF) { /* get the calibration data */ byte* handshake_buf = (byte *)malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); @@ -79,24 +78,24 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte return 0; } else - offset += 16; + data += 16; } /* joystick stuff */ - cc->ljs.max.x = data[0 + offset] / 4; - cc->ljs.min.x = data[1 + offset] / 4; - cc->ljs.center.x = data[2 + offset] / 4; - cc->ljs.max.y = data[3 + offset] / 4; - cc->ljs.min.y = data[4 + offset] / 4; - cc->ljs.center.y = data[5 + offset] / 4; + cc->ljs.max.x = data[0] / 4; + cc->ljs.min.x = data[1] / 4; + cc->ljs.center.x = data[2] / 4; + cc->ljs.max.y = data[3] / 4; + cc->ljs.min.y = data[4] / 4; + cc->ljs.center.y = data[5] / 4; - cc->rjs.max.x = data[6 + offset] / 8; - cc->rjs.min.x = data[7 + offset] / 8; - cc->rjs.center.x = data[8 + offset] / 8; - cc->rjs.max.y = data[9 + offset] / 8; - cc->rjs.min.y = data[10 + offset] / 8; - cc->rjs.center.y = data[11 + offset] / 8; + cc->rjs.max.x = data[6] / 8; + cc->rjs.min.x = data[7] / 8; + cc->rjs.center.x = data[8] / 8; + cc->rjs.max.y = data[9] / 8; + cc->rjs.min.y = data[10] / 8; + cc->rjs.center.y = data[11] / 8; /* handshake done */ wm->exp.type = EXP_CLASSIC; diff --git a/src/guitar_hero_3.c b/src/guitar_hero_3.c index cefe5ca..cc49bcb 100644 --- a/src/guitar_hero_3.c +++ b/src/guitar_hero_3.c @@ -52,7 +52,6 @@ static void guitar_hero_3_pressed_buttons(struct guitar_hero_3_t* gh3, short now */ int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, byte* data, unsigned short len) { int i; - int offset = 0; /* * The good fellows that made the Guitar Hero 3 controller @@ -65,7 +64,11 @@ int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, b gh3->btns_released = 0; gh3->whammy_bar = 0.0f; - if (data[offset] == 0xFF) { + /* + TODO: If we're not using anything from calibration data, why are we + even bothering here? + */ + if (data[0] == 0xFF) { /* * Sometimes the data returned here is not correct. * This might happen because the wiimote is lagging @@ -76,7 +79,7 @@ int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, b * but since the next 16 bytes are the same, just use * those. */ - if (data[offset + 16] == 0xFF) { + if (data[16] == 0xFF) { /* get the calibration data */ byte* handshake_buf = malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); @@ -85,7 +88,7 @@ int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, b return 0; } else - offset += 16; + data += 16; } /* joystick stuff */ From 4d793734285640e60c99d3a1d035a388afe88f0c Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 17:03:49 -0500 Subject: [PATCH 09/17] Similar cleanup on nunchuk --- src/nunchuk.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/nunchuk.c b/src/nunchuk.c index e5ff240..ede5a7e 100644 --- a/src/nunchuk.c +++ b/src/nunchuk.c @@ -50,7 +50,6 @@ */ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, unsigned short len) { int i; - int offset = 0; nc->btns = 0; nc->btns_held = 0; @@ -62,7 +61,7 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un /* decrypt data */ - if (data[offset] == 0xFF) { + if (data[0] == 0xFF) { /* * Sometimes the data returned here is not correct. * This might happen because the wiimote is lagging @@ -73,7 +72,7 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un * but since the next 16 bytes are the same, just use * those. */ - if (data[offset + 16] == 0xFF) { + if (data[16] == 0xFF) { /* get the calibration data */ byte* handshake_buf = (byte *)malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); @@ -82,21 +81,21 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un return 0; } else - offset += 16; + data += 16; } - nc->accel_calib.cal_zero.x = data[offset + 0]; - nc->accel_calib.cal_zero.y = data[offset + 1]; - nc->accel_calib.cal_zero.z = data[offset + 2]; - nc->accel_calib.cal_g.x = data[offset + 4]; - nc->accel_calib.cal_g.y = data[offset + 5]; - nc->accel_calib.cal_g.z = data[offset + 6]; - nc->js.max.x = data[offset + 8]; - nc->js.min.x = data[offset + 9]; - nc->js.center.x = data[offset + 10]; - nc->js.max.y = data[offset + 11]; - nc->js.min.y = data[offset + 12]; - nc->js.center.y = data[offset + 13]; + nc->accel_calib.cal_zero.x = data[0]; + nc->accel_calib.cal_zero.y = data[1]; + nc->accel_calib.cal_zero.z = data[2]; + nc->accel_calib.cal_g.x = data[4]; + nc->accel_calib.cal_g.y = data[5]; + nc->accel_calib.cal_g.z = data[6]; + nc->js.max.x = data[8]; + nc->js.min.x = data[9]; + nc->js.center.x = data[10]; + nc->js.max.y = data[11]; + nc->js.min.y = data[12]; + nc->js.center.y = data[13]; WIIUSE_DEBUG("Nunchuk calibration X: min %x, max %x, center %x Y: min %x, max %x, center %x", nc->js.min.x, nc->js.max.x, nc->js.center.x, nc->js.min.y, nc->js.max.y, nc->js.center.y); From 0de7c661b7c39f2d13a0db29f3f09803a5e577db Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 17:05:39 -0500 Subject: [PATCH 10/17] Added bounds safety checking --- src/classic.c | 5 +++-- src/nunchuk.c | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/classic.c b/src/classic.c index 69869a8..41e7ad0 100644 --- a/src/classic.c +++ b/src/classic.c @@ -49,6 +49,7 @@ static void classic_ctrl_pressed_buttons(struct classic_ctrl_t* cc, short now); * * @return Returns 1 if handshake was successful, 0 if not. */ +#define HANDSHAKE_BYTES_USED 12 int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte* data, unsigned short len) { int i; @@ -58,7 +59,7 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte cc->r_shoulder = 0; cc->l_shoulder = 0; - if (data[0] == 0xFF) { + if (data[0] == 0xFF || len < HANDSHAKE_BYTES_USED) { /* * Sometimes the data returned here is not correct. * This might happen because the wiimote is lagging @@ -69,7 +70,7 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte * but since the next 16 bytes are the same, just use * those. */ - if (data[16] == 0xFF) { + if (len < 17 || len < HANDSHAKE_BYTES_USED + 16 || data[16] == 0xFF) { /* get the calibration data */ byte* handshake_buf = (byte *)malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); diff --git a/src/nunchuk.c b/src/nunchuk.c index ede5a7e..db23d0f 100644 --- a/src/nunchuk.c +++ b/src/nunchuk.c @@ -48,6 +48,7 @@ * * @return Returns 1 if handshake was successful, 0 if not. */ ++#define HANDSHAKE_BYTES_USED 14 int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, unsigned short len) { int i; @@ -60,8 +61,7 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un nc->accel_calib.st_alpha = wm->accel_calib.st_alpha; /* decrypt data */ - - if (data[0] == 0xFF) { + if (data[0] == 0xFF || len < HANDSHAKE_BYTES_USED) { /* * Sometimes the data returned here is not correct. * This might happen because the wiimote is lagging @@ -72,7 +72,7 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un * but since the next 16 bytes are the same, just use * those. */ - if (data[16] == 0xFF) { + if (len < 17 || len < HANDSHAKE_BYTES_USED + 16 || data[16] == 0xFF) { /* get the calibration data */ byte* handshake_buf = (byte *)malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); From bfa81e3f0ca5862927200477cc8211dfbc204fd2 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 17:05:45 -0500 Subject: [PATCH 11/17] remove stray comment. --- src/nunchuk.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/nunchuk.c b/src/nunchuk.c index db23d0f..24767e9 100644 --- a/src/nunchuk.c +++ b/src/nunchuk.c @@ -60,7 +60,6 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un nc->flags = &wm->flags; nc->accel_calib.st_alpha = wm->accel_calib.st_alpha; - /* decrypt data */ if (data[0] == 0xFF || len < HANDSHAKE_BYTES_USED) { /* * Sometimes the data returned here is not correct. From 1a948b0c47bf3868911c36a6a815adf75d786ec5 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 17:07:11 -0500 Subject: [PATCH 12/17] Oops, fix build. --- src/nunchuk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nunchuk.c b/src/nunchuk.c index 24767e9..5194f90 100644 --- a/src/nunchuk.c +++ b/src/nunchuk.c @@ -48,7 +48,7 @@ * * @return Returns 1 if handshake was successful, 0 if not. */ -+#define HANDSHAKE_BYTES_USED 14 +#define HANDSHAKE_BYTES_USED 14 int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, unsigned short len) { int i; From ee82f2779271e55ffa5544bc0a3aa59de5bb655c Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 17:09:15 -0500 Subject: [PATCH 13/17] Remove unused variables --- src/classic.c | 1 - src/guitar_hero_3.c | 1 - src/nunchuk.c | 3 --- 3 files changed, 5 deletions(-) diff --git a/src/classic.c b/src/classic.c index 41e7ad0..a9d160e 100644 --- a/src/classic.c +++ b/src/classic.c @@ -51,7 +51,6 @@ static void classic_ctrl_pressed_buttons(struct classic_ctrl_t* cc, short now); */ #define HANDSHAKE_BYTES_USED 12 int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte* data, unsigned short len) { - int i; cc->btns = 0; cc->btns_held = 0; diff --git a/src/guitar_hero_3.c b/src/guitar_hero_3.c index cc49bcb..e085130 100644 --- a/src/guitar_hero_3.c +++ b/src/guitar_hero_3.c @@ -51,7 +51,6 @@ static void guitar_hero_3_pressed_buttons(struct guitar_hero_3_t* gh3, short now * @return Returns 1 if handshake was successful, 0 if not. */ int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, byte* data, unsigned short len) { - int i; /* * The good fellows that made the Guitar Hero 3 controller diff --git a/src/nunchuk.c b/src/nunchuk.c index 5194f90..d34b668 100644 --- a/src/nunchuk.c +++ b/src/nunchuk.c @@ -50,8 +50,6 @@ */ #define HANDSHAKE_BYTES_USED 14 int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, unsigned short len) { - int i; - nc->btns = 0; nc->btns_held = 0; nc->btns_released = 0; @@ -132,7 +130,6 @@ void nunchuk_disconnected(struct nunchuk_t* nc) { * @param msg The message specified in the event packet. */ void nunchuk_event(struct nunchuk_t* nc, byte* msg) { - int i; /* get button states */ nunchuk_pressed_buttons(nc, msg[5]); From 5f3d72c2c127b2b7d218a0b6c6616e01a546b036 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 17:10:34 -0500 Subject: [PATCH 14/17] Remove remaining decryption code. --- src/classic.c | 6 +----- src/guitar_hero_3.c | 5 ----- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/classic.c b/src/classic.c index a9d160e..a87d1ef 100644 --- a/src/classic.c +++ b/src/classic.c @@ -126,13 +126,9 @@ void classic_ctrl_disconnected(struct classic_ctrl_t* cc) { * @param msg The message specified in the event packet. */ void classic_ctrl_event(struct classic_ctrl_t* cc, byte* msg) { - int i, lx, ly, rx, ry; + int lx, ly, rx, ry; byte l, r; - /* decrypt data */ - for (i = 0; i < 6; ++i) - msg[i] = (msg[i] ^ 0x17) + 0x17; - classic_ctrl_pressed_buttons(cc, from_big_endian_uint16_t(msg + 4)); /* left/right buttons */ diff --git a/src/guitar_hero_3.c b/src/guitar_hero_3.c index e085130..296cb43 100644 --- a/src/guitar_hero_3.c +++ b/src/guitar_hero_3.c @@ -127,11 +127,6 @@ void guitar_hero_3_disconnected(struct guitar_hero_3_t* gh3) { * @param msg The message specified in the event packet. */ void guitar_hero_3_event(struct guitar_hero_3_t* gh3, byte* msg) { - int i; - - /* decrypt data */ - for (i = 0; i < 6; ++i) - msg[i] = (msg[i] ^ 0x17) + 0x17; guitar_hero_3_pressed_buttons(gh3, from_big_endian_uint16_t(msg + 4)); From 9f6e0cd2c000c2e3ead7fa0dffa3335dab08244a Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 17:15:44 -0500 Subject: [PATCH 15/17] De-duplication of wiiuse_read_data --- src/wiiuse.c | 40 ++-------------------------------------- 1 file changed, 2 insertions(+), 38 deletions(-) diff --git a/src/wiiuse.c b/src/wiiuse.c index 2b13d29..d0efe7a 100644 --- a/src/wiiuse.c +++ b/src/wiiuse.c @@ -354,7 +354,7 @@ int wiiuse_read_data_cb(struct wiimote_t* wm, wiiuse_read_cb read_cb, byte* buff if (!wm || !WIIMOTE_IS_CONNECTED(wm)) return 0; - if (!buffer || !len || !read_cb) + if (!buffer || !len) return 0; /* make this request structure */ @@ -407,43 +407,7 @@ int wiiuse_read_data_cb(struct wiimote_t* wm, wiiuse_read_cb read_cb, byte* buff * finishes. */ int wiiuse_read_data(struct wiimote_t* wm, byte* buffer, unsigned int addr, uint16_t len) { - struct read_req_t* req; - - if (!wm || !WIIMOTE_IS_CONNECTED(wm)) - return 0; - if (!buffer || !len) - return 0; - - /* make this request structure */ - req = (struct read_req_t*)malloc(sizeof(struct read_req_t)); - if (req == NULL) - return 0; - req->cb = NULL; - req->buf = buffer; - req->addr = addr; - req->size = len; - req->wait = len; - req->dirty = 0; - req->next = NULL; - - /* add this to the request list */ - if (!wm->read_req) { - /* root node */ - wm->read_req = req; - - WIIUSE_DEBUG("Data read request can be sent out immediately."); - - /* send the request out immediately */ - wiiuse_send_next_pending_read_request(wm); - } else { - struct read_req_t* nptr = wm->read_req; - for (; nptr->next; nptr = nptr->next); - nptr->next = req; - - WIIUSE_DEBUG("Added pending data read request."); - } - - return 1; + return wiiuse_read_data_cb(wm, NULL, buffer, addr, len); } From 300c91398d8a33ca879df58d85841f3badab89f3 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 17:24:22 -0500 Subject: [PATCH 16/17] Fix an error in a comment --- src/events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/events.c b/src/events.c index c856a2a..9d063a5 100644 --- a/src/events.c +++ b/src/events.c @@ -298,7 +298,7 @@ static void clear_dirty_reads(struct wiimote_t* wm) { /** * @brief Analyze the event that occurred on a wiimote. * - * @param wm An array of pointers to wiimote_t structures. + * @param wm Pointer to a wiimote_t structure. * @param event The event that occurred. * @param msg The message specified in the event packet. * From 9124fa7cc7fa5aa67494295201416d25d9c977ad Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Thu, 28 Jun 2012 17:24:41 -0500 Subject: [PATCH 17/17] De-duplicate handling of wiimote accelerometer data. --- src/events.c | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/src/events.c b/src/events.c index 9d063a5..d53faa4 100644 --- a/src/events.c +++ b/src/events.c @@ -294,6 +294,24 @@ static void clear_dirty_reads(struct wiimote_t* wm) { } } +/** + * @brief Handle accel data in a wiimote message. + * + * @param wm Pointer to a wiimote_t structure. + * @param msg The message specified in the event packet. + */ +static void handle_wm_accel(struct wiimote_t* wm, byte* msg) { + wm->accel.x = msg[2]; + wm->accel.y = msg[3]; + wm->accel.z = msg[4]; + + /* calculate the remote orientation */ + calculate_orientation(&wm->accel_calib, &wm->accel, &wm->orient, WIIMOTE_IS_FLAG_SET(wm, WIIUSE_SMOOTHING)); + + /* calculate the gforces on each axis */ + calculate_gforce(&wm->accel_calib, &wm->accel, &wm->gforce); +} + /** * @brief Analyze the event that occurred on a wiimote. @@ -319,15 +337,7 @@ void propagate_event(struct wiimote_t* wm, byte event, byte* msg) { /* button - motion */ wiiuse_pressed_buttons(wm, msg); - wm->accel.x = msg[2]; - wm->accel.y = msg[3]; - wm->accel.z = msg[4]; - - /* calculate the remote orientation */ - calculate_orientation(&wm->accel_calib, &wm->accel, &wm->orient, WIIMOTE_IS_FLAG_SET(wm, WIIUSE_SMOOTHING)); - - /* calculate the gforces on each axis */ - calculate_gforce(&wm->accel_calib, &wm->accel, &wm->gforce); + handle_wm_accel(wm, msg); break; } @@ -360,12 +370,7 @@ void propagate_event(struct wiimote_t* wm, byte event, byte* msg) { /* button - motion - expansion */ wiiuse_pressed_buttons(wm, msg); - wm->accel.x = msg[2]; - wm->accel.y = msg[3]; - wm->accel.z = msg[4]; - - calculate_orientation(&wm->accel_calib, &wm->accel, &wm->orient, WIIMOTE_IS_FLAG_SET(wm, WIIUSE_SMOOTHING)); - calculate_gforce(&wm->accel_calib, &wm->accel, &wm->gforce); + handle_wm_accel(wm, msg); handle_expansion(wm, msg+5); @@ -376,12 +381,7 @@ void propagate_event(struct wiimote_t* wm, byte event, byte* msg) { /* button - motion - ir */ wiiuse_pressed_buttons(wm, msg); - wm->accel.x = msg[2]; - wm->accel.y = msg[3]; - wm->accel.z = msg[4]; - - calculate_orientation(&wm->accel_calib, &wm->accel, &wm->orient, WIIMOTE_IS_FLAG_SET(wm, WIIUSE_SMOOTHING)); - calculate_gforce(&wm->accel_calib, &wm->accel, &wm->gforce); + handle_wm_accel(wm, msg); /* ir */ calculate_extended_ir(wm, msg+5); @@ -404,12 +404,7 @@ void propagate_event(struct wiimote_t* wm, byte event, byte* msg) { /* button - motion - ir - expansion */ wiiuse_pressed_buttons(wm, msg); - wm->accel.x = msg[2]; - wm->accel.y = msg[3]; - wm->accel.z = msg[4]; - - calculate_orientation(&wm->accel_calib, &wm->accel, &wm->orient, WIIMOTE_IS_FLAG_SET(wm, WIIUSE_SMOOTHING)); - calculate_gforce(&wm->accel_calib, &wm->accel, &wm->gforce); + handle_wm_accel(wm, msg); handle_expansion(wm, msg+15);