From 391e9c09269049a3a307020ca8f0242a99de21d2 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Tue, 20 Oct 2015 18:32:34 -0400
Subject: [PATCH] ported from pd-l2ork git: commit
 ac2528233bbe36074747ae4779f909713a9b951f  *minor bug fixes to the 0.7.00
 release of cwiid (keeping the version the same)

---
 l2ork_addons/cwiid/libcwiid/command.c        | 39 +++++++++++++++-----
 l2ork_addons/cwiid/libcwiid/connect.c        | 23 +++++++++++-
 l2ork_addons/cwiid/libcwiid/cwiid_internal.h | 15 ++++++--
 l2ork_addons/cwiid/libcwiid/process.c        |  8 +++-
 l2ork_addons/cwiid/libcwiid/util.c           |  9 +++--
 5 files changed, 75 insertions(+), 19 deletions(-)

diff --git a/l2ork_addons/cwiid/libcwiid/command.c b/l2ork_addons/cwiid/libcwiid/command.c
index 24d3adb1d..8c4f1d035 100644
--- a/l2ork_addons/cwiid/libcwiid/command.c
+++ b/l2ork_addons/cwiid/libcwiid/command.c
@@ -15,6 +15,10 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  *  ChangeLog:
+  *	2015-09-22 Ivica Ico Bukvic <ico@vt.edu>
+ * * Made old wiimotes use old way of connecting as some of them fail to do so using 1+2 when using new method
+ * * Removed error report thats tend to unnecessarily spam the console
+ *
  *	2015-09-17 Ivica Ico Bukvic <ico@vt.edu>
  * * Added Wii MotionPlus Inside support, thereby completing support for all known Wii devices
  * * Version bump to 0.7.00
@@ -98,21 +102,36 @@ int cwiid_send_rpt(cwiid_wiimote_t *wiimote, uint8_t flags, uint8_t report,
 		return -1;
 	}
 
-	buf[0] = BT_TRANS_SET_REPORT | BT_PARAM_OUTPUT;
+	if (wiimote->type == WIIMOTE_NEW)
+		buf[0] = BT_TRANS_DATA | BT_PARAM_OUTPUT;
+	else
+		buf[0] = BT_TRANS_SET_REPORT | BT_PARAM_OUTPUT;
 	buf[1] = report;
 	memcpy(buf+2, data, len);
 	if (!(flags & CWIID_SEND_RPT_NO_RUMBLE)) {
 		buf[2] |= wiimote->state.rumble;
 	}
 
-	if (write(wiimote->int_socket, buf, len+2) != (ssize_t)(len+2)) {
-		free(buf);
-		return -1;
+	// if this is a new version of the wiimote
+	if (wiimote->type == WIIMOTE_NEW)
+	{
+		if (write(wiimote->int_socket, buf, len+2) != (ssize_t)(len+2)) {
+			free(buf);
+			return -1;
+		}
+	}
+	// otherwise it is WIIMOTE_OLD which also includes Wii Board
+	else
+	{
+		if (write(wiimote->ctl_socket, buf, len+2) != (ssize_t)(len+2)) {
+			free(buf);
+			return -1;
+		}
+		else if (verify_handshake(wiimote)) {
+			free(buf);
+			return -1;
+		}
 	}
-	/*else if (verify_handshake(wiimote)) {
-		free(buf);
-		return -1;
-	}*/
 
 	return 0;
 }
@@ -391,11 +410,11 @@ int cwiid_write(cwiid_wiimote_t *wiimote, uint8_t flags, uint32_t offset,
 			goto CODA;
 		}
 
-		if (mesg.error) {
+		/*if (mesg.error) {
 			cwiid_err(wiimote, "Wiimote write error");
 			ret = -1;
 			goto CODA;
-		};
+		};*/
 
 		sent+=buf[4];
 	}
diff --git a/l2ork_addons/cwiid/libcwiid/connect.c b/l2ork_addons/cwiid/libcwiid/connect.c
index 4d4087425..70b919ad9 100644
--- a/l2ork_addons/cwiid/libcwiid/connect.c
+++ b/l2ork_addons/cwiid/libcwiid/connect.c
@@ -15,6 +15,10 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  *  ChangeLog:
+ *	2015-09-22 Ivica Ico Bukvic <ico@vt.edu>
+ * * Made old wiimotes use old way of connecting as some of them fail to do so using 1+2 when using new method
+ * * Removed error report thats tend to unnecessarily spam the console
+ *
  *	2015-09-17 Ivica Ico Bukvic <ico@vt.edu>
  * * Added Wii MotionPlus Inside support, thereby completing support for all known Wii devices
  * * Version bump to 0.7.00
@@ -88,8 +92,9 @@ cwiid_wiimote_t *cwiid_open_timeout(bdaddr_t *bdaddr, int flags, int timeout)
 	char mesg_pipe_init = 0, status_pipe_init = 0, rw_pipe_init = 0,
 	     state_mutex_init = 0, rw_mutex_init = 0, rpt_mutex_init = 0,
 	     router_thread_init = 0, status_thread_init = 0, ext_passthrough_polling_thread_init = 0,
-		 poll_mutex_init = 0, poll_cond_init = 0;
+		 poll_mutex_init = 0, poll_cond_init = 0, name[BT_NAME_LEN] = { 0 };
 	void *pthread_ret;
+	int sock; 
 
 	/* Allocate wiimote */
 	if ((wiimote = malloc(sizeof *wiimote)) == NULL) {
@@ -123,6 +128,22 @@ cwiid_wiimote_t *cwiid_open_timeout(bdaddr_t *bdaddr, int flags, int timeout)
 		sleep(1);
 	}
 
+	/* Figure out if the device is old or new version by looking up its name */
+	sock = hci_open_dev(wiimote->id);
+	if (!hci_read_remote_name(sock, bdaddr, BT_NAME_LEN, name, 0)) {
+		if (!strncmp(name, WIIMOTE_PLUS_NAME, BT_NAME_LEN)) {
+			fprintf(stderr,"Found new version of the wiimote (%s)\n", name);
+			wiimote->type = WIIMOTE_NEW;
+		}
+		else {
+			fprintf(stderr,"Found old version of the wiimote (%s)\n", name);
+			wiimote->type = WIIMOTE_OLD;
+		}
+	}
+	else
+		fprintf(stderr,"Error reading wiimote name, unable to determine version\n");
+	close(sock);
+
 	/* Connect to Wiimote */
 	/* Control Channel */
 	memset(&remote_addr, 0, sizeof remote_addr);
diff --git a/l2ork_addons/cwiid/libcwiid/cwiid_internal.h b/l2ork_addons/cwiid/libcwiid/cwiid_internal.h
index 0a3b1f10e..fe5a8d0f2 100644
--- a/l2ork_addons/cwiid/libcwiid/cwiid_internal.h
+++ b/l2ork_addons/cwiid/libcwiid/cwiid_internal.h
@@ -16,6 +16,10 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  *  ChangeLog:
+ *	2015-09-22 Ivica Ico Bukvic <ico@vt.edu>
+ * * Made old wiimotes use old way of connecting as some of them fail to do so using 1+2 when using new method
+ * * Removed error report thats tend to unnecessarily spam the console
+ *
  *	2015-09-17 Ivica Ico Bukvic <ico@vt.edu>
  * * Added Wii MotionPlus Inside support, thereby completing support for all known Wii devices
  * * Version bump to 0.7.00
@@ -65,7 +69,7 @@
 /* Bluetooth magic numbers */
 #define BT_TRANS_MASK		0xF0
 #define BT_TRANS_HANDSHAKE	0x00
-#define BT_TRANS_SET_REPORT	0xA0
+#define BT_TRANS_SET_REPORT	0x50
 #define BT_TRANS_DATA		0xA0
 #define BT_TRANS_DATAC		0xB0
 
@@ -157,6 +161,10 @@
 #define WII_L5_IR_BLOCK_1			"\x02\x00\x00\x71\x01\x00\x72\x00\x20"
 #define WII_L5_IR_BLOCK_2			"\x1F\x03"
 
+/* Type of Wiimote */
+ #define WIIMOTE_OLD	0
+ #define WIIMOTE_NEW	1
+
 /* Write Sequences */
 enum write_seq_type {
 	WRITE_SEQ_RPT,
@@ -216,9 +224,10 @@ struct wiimote {
 	pthread_mutex_t rpt_mutex;
 	int id;
 	const void *data;
-	int ext;	//extension identifier already added to wiimote
+	int ext;	// extension identifier already added to wiimote
 	pthread_t passthrough_polling_thread;
 	int passthrough_activate_flag;
+	int type; 	// type of wiimote, 0 = old, 1 = new
 	pthread_mutex_t poll_mutex;
 	pthread_cond_t condition_var;
 };
@@ -232,7 +241,7 @@ void *mesg_callback_thread(struct wiimote *wiimote);
 
 /* util.c */
 void cwiid_err(struct wiimote *wiimote, const char *str, ...);
-//int verify_handshake(struct wiimote *wiimote);
+int verify_handshake(struct wiimote *wiimote);
 int exec_write_seq(struct wiimote *wiimote, unsigned int len,
                    struct write_seq *seq);
 int full_read(int fd, void *buf, size_t len);
diff --git a/l2ork_addons/cwiid/libcwiid/process.c b/l2ork_addons/cwiid/libcwiid/process.c
index b088eecb7..de4ed7f7a 100644
--- a/l2ork_addons/cwiid/libcwiid/process.c
+++ b/l2ork_addons/cwiid/libcwiid/process.c
@@ -16,6 +16,10 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  *  ChangeLog:
+ *	2015-09-22 Ivica Ico Bukvic <ico@vt.edu>
+ * * Made old wiimotes use old way of connecting as some of them fail to do so using 1+2 when using new method
+ * * Removed error report thats tend to unnecessarily spam the console
+ *
  *	2015-09-17 Ivica Ico Bukvic <ico@vt.edu>
  * * Added Wii MotionPlus Inside support, thereby completing support for all known Wii devices
  * * Version bump to 0.7.00
@@ -376,7 +380,7 @@ int process_write(struct wiimote *wiimote, unsigned char *data)
 
 	if (wiimote->rw_status != RW_WRITE) {
 		cwiid_err(wiimote, "Received unexpected write report %d", wiimote->rw_status);
-		//return -1;
+		return -1;
 	}
 
 	rw_mesg.type = RW_WRITE;
@@ -385,7 +389,7 @@ int process_write(struct wiimote *wiimote, unsigned char *data)
 	if (write(wiimote->rw_pipe[1], &rw_mesg, sizeof rw_mesg) !=
 	  sizeof rw_mesg) {
 		cwiid_err(wiimote, "RW pipe write error");
-		//return -1;
+		return -1;
 	}
 
 	return 0;
diff --git a/l2ork_addons/cwiid/libcwiid/util.c b/l2ork_addons/cwiid/libcwiid/util.c
index 5579e82ed..1d9b96523 100644
--- a/l2ork_addons/cwiid/libcwiid/util.c
+++ b/l2ork_addons/cwiid/libcwiid/util.c
@@ -16,6 +16,10 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  *  ChangeLog:
+ *	2015-09-22 Ivica Ico Bukvic <ico@vt.edu>
+ * * Made old wiimotes use old way of connecting as some of them fail to do so using 1+2 when using new method
+ * * Removed error report thats tend to unnecessarily spam the console
+ *
  *	2015-09-17 Ivica Ico Bukvic <ico@vt.edu>
  * * Added Wii MotionPlus Inside support, thereby completing support for all known Wii devices
  * * Version bump to 0.7.00
@@ -94,10 +98,9 @@ void cwiid_err(struct wiimote *wiimote, const char *str, ...)
 	}
 }
 
-/*
+/* used only for old versions of Wiimotes, including Wii Board */
 int verify_handshake(struct wiimote *wiimote)
 {
-	// disabled because we don't use ctl_socket any more
 	unsigned char handshake;
 	if (read(wiimote->ctl_socket, &handshake, 1) != 1) {
 		cwiid_err(wiimote, "Socket read error (handshake)");
@@ -114,7 +117,7 @@ int verify_handshake(struct wiimote *wiimote)
 
 	return 0;
 }
-*/
+
 
 int exec_write_seq(struct wiimote *wiimote, unsigned int len,
                    struct write_seq *seq)
-- 
GitLab