Re: [PATCH 3/3] Add a simple cp2130 libusb driver

2016-09-18 Thread Anton Lundin
Fnuk. I managed to do a git add -u, when having other dirty bits in the
tree. Disregard this one. I'll send a new one.

//Anton

On 18 September, 2016 - Anton Lundin wrote:

> This adds a simple cp2130 userspace driver. Its probably unusable in the
> real world but its a great base to build upon.
> 
> Signed-off-by: Anton Lundin 
> ---
>  CMakeLists.txt|   1 +
>  android/res/xml/device_filter.xml |   3 +
>  core/CMakeLists.txt   |   7 +
>  core/libdivecomputer.c|   4 +
>  core/libdivecomputer.h|   1 +
>  core/serial_cp2130.c  | 283 
> ++
>  packaging/android/build.sh|   2 +-
>  scripts/build.sh  |  13 +-
>  8 files changed, 310 insertions(+), 4 deletions(-)
>  create mode 100644 core/serial_cp2130.c
> 
> diff --git a/CMakeLists.txt b/CMakeLists.txt
> index 095160c..104c323 100644
> --- a/CMakeLists.txt
> +++ b/CMakeLists.txt
> @@ -33,6 +33,7 @@ option(NO_USERMANUAL "don't include a viewer for the user 
> manual" OFF)
>  option(FBSUPPORT "allow posting to Facebook" ON)
>  option(BTSUPPORT "enable support for QtBluetooth (requires Qt5.4 or newer)" 
> ON)
>  option(FTDISUPPORT "enable support for libftdi based serial" OFF)
> +option(CP2130SUPPORT "enable support for cp2130 based serial" OFF)
>  
>  # Options regarding What should we build on subsurface
>  option(MAKE_TESTS "Make the tests" ON)
> diff --git a/android/res/xml/device_filter.xml 
> b/android/res/xml/device_filter.xml
> index fc9333b..19e535e 100644
> --- a/android/res/xml/device_filter.xml
> +++ b/android/res/xml/device_filter.xml
> @@ -11,4 +11,7 @@
>  
>  
>  
> +
> + 
> + 
>  
> diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt
> index 5c22e23..dff8361 100644
> --- a/core/CMakeLists.txt
> +++ b/core/CMakeLists.txt
> @@ -18,6 +18,12 @@ if(FTDISUPPORT)
>   set(SERIAL_FTDI serial_ftdi.c)
>  endif()
>  
> +if(CP2130SUPPORT)
> + message(STATUS "building with cp2130 support")
> + add_definitions(-DSERIAL_CP2130)
> + set(SERIAL_CP2130 serial_cp2130.c)
> +endif()
> +
>  if(BTSUPPORT)
>   add_definitions(-DBT_SUPPORT)
>   set(BT_SRC_FILES desktop-widgets/btdeviceselectiondialog.cpp)
> @@ -89,6 +95,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
>   subsurface-qt/CylinderObjectHelper.cpp
>   subsurface-qt/SettingsObjectWrapper.cpp
>   ${SERIAL_FTDI}
> + ${SERIAL_CP2130}
>   ${PLATFORM_SRC}
>   ${BT_CORE_SRC_FILES}
>  )
> diff --git a/core/libdivecomputer.c b/core/libdivecomputer.c
> index cd1b69b..f76a654 100644
> --- a/core/libdivecomputer.c
> +++ b/core/libdivecomputer.c
> @@ -1034,6 +1034,10 @@ const char *do_libdivecomputer_import(device_data_t 
> *data)
>   } else if (!strcmp(data->devname, "ftdi")) {
>   rc = dc_context_set_custom_serial(data->context, 
> &serial_ftdi_ops);
>  #endif
> +#ifdef SERIAL_CP2130
> + } else if (!strcmp(data->devname, "cp2130")) {
> + rc = dc_context_set_custom_serial(data->context, 
> &cp2130_serial_ops);
> +#endif
>   }
>  
>   if (rc != DC_STATUS_SUCCESS) {
> diff --git a/core/libdivecomputer.h b/core/libdivecomputer.h
> index f2894b0..8a91fcf 100644
> --- a/core/libdivecomputer.h
> +++ b/core/libdivecomputer.h
> @@ -65,6 +65,7 @@ extern char *dumpfile_name;
>  // Thats why I've worked around it with a stupid helper returning it.
>  dc_custom_serial_t* get_qt_serial_ops();
>  extern dc_custom_serial_t serial_ftdi_ops;
> +extern dc_custom_serial_t cp2130_serial_ops;
>  #endif
>  
>  #ifdef __cplusplus
> diff --git a/core/serial_cp2130.c b/core/serial_cp2130.c
> new file mode 100644
> index 000..e6fb659
> --- /dev/null
> +++ b/core/serial_cp2130.c
> @@ -0,0 +1,283 @@
> +/*
> + * This is code from and inspired by 
> https://www.silabs.com/Support%20Documents/TechnicalDocs/AN792.pdf
> + */
> +
> +#include  // memset
> +#include   // malloc, free
> +#include  // bool
> +/*
> +#include   // errno
> +#include// gettimeofday
> +#include// nanosleep
> +#include 
> +*/
> +
> +#include 
> +#include 
> +
> +#include 
> +
> +typedef struct cp2130_serial_t {
> + /** libusb's context */
> + struct libusb_context *context;
> + /** libusb's usb_dev_handle */
> + struct libusb_device_handle *cp2130Handle;
> + /** Should we re-attach native driver? */
> + bool kernelAttached;
> +
> + long timeout;
> +} cp2130_serial_t;
> +
> +static dc_status_t cp2130_serial_close (void **userdata);
> +/*
> +8.1.3. Initialization and Device Discovery
> +The sample application shows the calls necessary to initialize and discover 
> a device.
> +The steps that need to be taken to get a handle to the CP2130 device are:
> +1. Initialize LibUSB using libusb_init().
> +2. Get the device list using libusb_get_device_list() and find a device to 
> connect to.
> +3. Open the device with LibUSB using libusb_open().
> +4. Detach any existing kernel connection by checking 
> libusb_k

[PATCH 3/3] Add a simple cp2130 libusb driver

2016-09-18 Thread Anton Lundin
This adds a simple cp2130 userspace driver. Its probably unusable in the
real world but its a great base to build upon.

Signed-off-by: Anton Lundin 
---
 CMakeLists.txt|   1 +
 android/res/xml/device_filter.xml |   3 +
 core/CMakeLists.txt   |   7 +
 core/libdivecomputer.c|   4 +
 core/libdivecomputer.h|   1 +
 core/serial_cp2130.c  | 283 ++
 packaging/android/build.sh|   2 +-
 scripts/build.sh  |  13 +-
 8 files changed, 310 insertions(+), 4 deletions(-)
 create mode 100644 core/serial_cp2130.c

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 095160c..104c323 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -33,6 +33,7 @@ option(NO_USERMANUAL "don't include a viewer for the user 
manual" OFF)
 option(FBSUPPORT "allow posting to Facebook" ON)
 option(BTSUPPORT "enable support for QtBluetooth (requires Qt5.4 or newer)" ON)
 option(FTDISUPPORT "enable support for libftdi based serial" OFF)
+option(CP2130SUPPORT "enable support for cp2130 based serial" OFF)
 
 # Options regarding What should we build on subsurface
 option(MAKE_TESTS "Make the tests" ON)
diff --git a/android/res/xml/device_filter.xml 
b/android/res/xml/device_filter.xml
index fc9333b..19e535e 100644
--- a/android/res/xml/device_filter.xml
+++ b/android/res/xml/device_filter.xml
@@ -11,4 +11,7 @@
 
 
 
+
+   
+   
 
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt
index 5c22e23..dff8361 100644
--- a/core/CMakeLists.txt
+++ b/core/CMakeLists.txt
@@ -18,6 +18,12 @@ if(FTDISUPPORT)
set(SERIAL_FTDI serial_ftdi.c)
 endif()
 
+if(CP2130SUPPORT)
+   message(STATUS "building with cp2130 support")
+   add_definitions(-DSERIAL_CP2130)
+   set(SERIAL_CP2130 serial_cp2130.c)
+endif()
+
 if(BTSUPPORT)
add_definitions(-DBT_SUPPORT)
set(BT_SRC_FILES desktop-widgets/btdeviceselectiondialog.cpp)
@@ -89,6 +95,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
subsurface-qt/CylinderObjectHelper.cpp
subsurface-qt/SettingsObjectWrapper.cpp
${SERIAL_FTDI}
+   ${SERIAL_CP2130}
${PLATFORM_SRC}
${BT_CORE_SRC_FILES}
 )
diff --git a/core/libdivecomputer.c b/core/libdivecomputer.c
index cd1b69b..f76a654 100644
--- a/core/libdivecomputer.c
+++ b/core/libdivecomputer.c
@@ -1034,6 +1034,10 @@ const char *do_libdivecomputer_import(device_data_t 
*data)
} else if (!strcmp(data->devname, "ftdi")) {
rc = dc_context_set_custom_serial(data->context, 
&serial_ftdi_ops);
 #endif
+#ifdef SERIAL_CP2130
+   } else if (!strcmp(data->devname, "cp2130")) {
+   rc = dc_context_set_custom_serial(data->context, 
&cp2130_serial_ops);
+#endif
}
 
if (rc != DC_STATUS_SUCCESS) {
diff --git a/core/libdivecomputer.h b/core/libdivecomputer.h
index f2894b0..8a91fcf 100644
--- a/core/libdivecomputer.h
+++ b/core/libdivecomputer.h
@@ -65,6 +65,7 @@ extern char *dumpfile_name;
 // Thats why I've worked around it with a stupid helper returning it.
 dc_custom_serial_t* get_qt_serial_ops();
 extern dc_custom_serial_t serial_ftdi_ops;
+extern dc_custom_serial_t cp2130_serial_ops;
 #endif
 
 #ifdef __cplusplus
diff --git a/core/serial_cp2130.c b/core/serial_cp2130.c
new file mode 100644
index 000..e6fb659
--- /dev/null
+++ b/core/serial_cp2130.c
@@ -0,0 +1,283 @@
+/*
+ * This is code from and inspired by 
https://www.silabs.com/Support%20Documents/TechnicalDocs/AN792.pdf
+ */
+
+#include  // memset
+#include // malloc, free
+#include// bool
+/*
+#include   // errno
+#include// gettimeofday
+#include// nanosleep
+#include 
+*/
+
+#include 
+#include 
+
+#include 
+
+typedef struct cp2130_serial_t {
+   /** libusb's context */
+   struct libusb_context *context;
+   /** libusb's usb_dev_handle */
+   struct libusb_device_handle *cp2130Handle;
+   /** Should we re-attach native driver? */
+   bool kernelAttached;
+
+   long timeout;
+} cp2130_serial_t;
+
+static dc_status_t cp2130_serial_close (void **userdata);
+/*
+8.1.3. Initialization and Device Discovery
+The sample application shows the calls necessary to initialize and discover a 
device.
+The steps that need to be taken to get a handle to the CP2130 device are:
+1. Initialize LibUSB using libusb_init().
+2. Get the device list using libusb_get_device_list() and find a device to 
connect to.
+3. Open the device with LibUSB using libusb_open().
+4. Detach any existing kernel connection by checking 
libusb_kernel_driver_active() and using
+libusb_detach_kernel_driver() if it is connected to the kernel.
+5. Claim the interface using libusb_claim_interface().
+Here is the program listing from the sample application with comments for 
reference:
+*/
+static dc_status_t cp2130_serial_open (void **userdata, const char* name) {
+   // Allocate memory.
+   cp2130_serial_t *device = (cp2130_serial_t*) malloc (size