[FE training-materials-updates] Add the solution for the native binary

maxime.ripard at free-electrons.com maxime.ripard at free-electrons.com
Tue Jun 19 17:21:03 CEST 2012


- Log -----------------------------------------------------------------
http://git.free-electrons.com/training-materials/commit/?id=bffb87db7d3587829efecf803bfd25ab5caac0aa

commit bffb87db7d3587829efecf803bfd25ab5caac0aa
Author: Maxime Ripard <maxime.ripard at free-electrons.com>
Date:   Tue Jun 19 17:20:34 2012 +0200

    Add the solution for the native binary

diff --git a/lab-data/android/native-app/mlbin.c b/lab-data/android/native-app/mlbin.c
index d30c5c5..4150d34 100644
--- a/lab-data/android/native-app/mlbin.c
+++ b/lab-data/android/native-app/mlbin.c
@@ -3,8 +3,8 @@
 
 #include <libusb.h>
 
-#define ML_VENDOR_ID		0x0416
-#define ML_DEVICE_ID		0x9391
+#define ML_VENDOR_ID			0x0416
+#define ML_DEVICE_ID			0x9391
 
 #define ML_ACTION_FIRE			0x10
 #define ML_ACTION_MOVE_DOWN		0x1
@@ -17,46 +17,133 @@ static struct libusb_device_handle *devh;
 
 int mlbin_init_usb(void)
 {
+	libusb_device **list;
+	libusb_device *device = NULL;
+	int count, ret, i;
+
+	printf("Calling mlbin_init\n");
+
+	ret = libusb_init(NULL);
+
+	if(ret < 0) {
+		printf("Couldn't initialize libusb.\n");
+		goto error;
+	}
+
+	count = libusb_get_device_list(NULL, &list);
+	if (count < 0) {
+		printf("Couldn't get device list\n");
+		goto list_error;
+	}
+
+	for (i = 0; i < count; i++) {
+		struct libusb_device_descriptor desc;
+		device = list[i];
+		libusb_get_device_descriptor(device, &desc);
+		printf("Found a new device : %x:%x\n", desc.idVendor, desc.idProduct);
+		if (desc.idVendor == ML_VENDOR_ID && desc.idProduct == ML_DEVICE_ID)
+			break;
+		device = NULL;
+	}
+
+	if (!device) {
+		printf("Couldn't find the device\n");
+		goto not_found_error;
+	}
+
+	ret = libusb_open(device, &devh);
+	if (ret) {
+		printf("Couldn't open device: %d\n", ret);
+		goto open_dev_error;
+	}
+
+	ret = libusb_detach_kernel_driver(devh, 0);
+	if (ret) {
+		printf("Couldn't detach kernel driver: %d\n", ret);
+	}
+
+	ret = libusb_claim_interface(devh, 0);
+	if(ret < 0) {
+    	printf("Couldn't claim the interface : %d.\n", ret);
+		goto if_error;
+	}
+
+	libusb_free_device_list(list, count);
+
+	printf("Interface setup.\n");
 	return 0;
+
+if_error:
+	libusb_close(devh);
+detach_error:
+open_dev_error:
+not_found_error:
+	libusb_free_device_list(list, count);
+list_error:
+	libusb_exit(NULL);
+error:
+	exit(1);
 }
 
 int mlbin_free_usb(void)
 {
+	libusb_release_interface(devh, 0);
+	libusb_close(devh);
+	libusb_exit(NULL);
+	printf("mlbin exiting\n");
 	return 0;
 }
 
 int mlbin_fire(void)
 {
+  unsigned char data[] = {0x5f, ML_ACTION_FIRE, 0xe0, 0xff, 0xfe};
+  libusb_control_transfer(devh, 0x21, 0x09, 0, 0, data, 5, 300);
+
 	printf("Fire!\n");
 	return 0;
 }
 
 int mlbin_move_down(void)
 {
+  unsigned char data[] = {0x5f, ML_ACTION_MOVE_DOWN, 0xe0, 0xff, 0xfe};
+  libusb_control_transfer(devh, 0x21, 0x09, 0, 0, data, 5, 300);
+
 	printf("Move Down!\n");
 	return 0;
 }
 
 int mlbin_move_left(void)
 {
+  unsigned char data[] = {0x5f, ML_ACTION_MOVE_LEFT, 0xe0, 0xff, 0xfe};
+  libusb_control_transfer(devh, 0x21, 0x09, 0, 0, data, 5, 300);
+
 	printf("Move Left!\n");
 	return 0;
 }
 
 int mlbin_move_right(void)
 {
+  unsigned char data[] = {0x5f, ML_ACTION_MOVE_RIGHT, 0xe0, 0xff, 0xfe};
+  libusb_control_transfer(devh, 0x21, 0x09, 0, 0, data, 5, 300);
+
 	printf("Move Right!\n");
 	return 0;
 }
 
 int mlbin_move_up(void)
 {
+  unsigned char data[] = {0x5f, ML_ACTION_MOVE_UP, 0xe0, 0xff, 0xfe};
+  libusb_control_transfer(devh, 0x21, 0x09, 0, 0, data, 5, 300);
+
 	printf("Move Up!\n");
 	return 0;
 }
 
 int mlbin_stop(void)
 {
+  unsigned char data[] = {0x5f, ML_ACTION_STOP, 0xe0, 0xff, 0xfe};
+  libusb_control_transfer(devh, 0x21, 0x09, 0, 0, data, 5, 300);
+
 	printf("Stop!\n");
 	return 0;
 }

-----------------------------------------------------------------------

Summary of changes:
 lab-data/android/native-app/mlbin.c |   91 ++++++++++++++++++++++++++++++++++-
 1 file changed, 89 insertions(+), 2 deletions(-)


More information about the training-materials-updates mailing list