Modified to pass file descriptor instead of path

As an experiment, the file descriptor was passed on rather than
path to the tty. Refer
http://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection.html#getFileDescriptor()
for documentation regarding file descriptor to be transferred.
The idea is that Java side of the project will obtain permission for
usage of USB device and pass it on to the native side. Using this
file descriptor, the libdivecomputer is expected to import dives.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
Revert to passing name rather than fd

As experiment 2 failed with the Error ENOTTY : Not a typewriter
we would revert to the original mechanism.
The reason behind this failure is explained by Jef.
The file descriptor given by android is actually the file
descriptor of the USB device not the tty built over it.
Hence it would not be possible to use libdivecomputer with this
file descriptor.
But maybe libusb can utilise this?

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
Initial version serial_ftdi.c

This is based on serial_posix script.
There are a number of shortcomings.
Biggest of them is macro definition of VID and PID. Rather than this,
maintain a list of VID and PID of accepted FTDI devices.
Unimplemented functions like get_transmitted, send_break and set_break
return -1 on call.

For device_open(), vid pid would be better arguments than the path as
path is not too helpful for libftdi and libusb.

serial_enumerate gives back the path of all the devices present on all
hubs on android. Basically, all the files present in all the subfolders
of /dev/bus/usb.
A better implementation would be a set of ftdi devices attached to the
android. This could be done either using jni by enumerate_device in
android.cpp or using libusb and passing vid pid as an argument.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
Check for libftdi and build for android

Added check for libftdi during configure.
Suitable modifications to enable cross building for android platform.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
Corrected include statement for ftdi.h

Use libusb.h instead of libusb-1.0/libusb.h

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
Corrections in serial_open and serial_set_timeout

ftdi_context->timeout is the timeout used while libusb_bulk_transfer.
On the other hand, libdivecomputer context is used internally by
libdivecomputer. So inappropriate to set both here. By default libftdi
sets a timeout of 5000 ms on both read and write.

Usage of struct ftdi_context was wrong. Fixed it.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
Finally a working version. Try again on no data.

ftdi_read_data returns 0 if no data is received from
the device. The existing code understood it as timeout errror.
Removing this check might end up in an infinite loop. But it works
in OSTC3. This should be checked further.
Cross compilation of libdivecomputer on android and running it
gives the results same as the ones on pc.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
Check multiple pids while opening device

libftdi returns -3 when it could not find any device corresponding to
the pid and vid given. (Different from -3 of libusb which means you do
not have permissions to access the device)
Using this value, check among a list of accepted pids. VID is that of
ftdi ( file is called serial_ftdi :)
There is a problem when multiple devices are connected. It uses the
first one it can find in the list. This might not be correct always.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
Added custom pids of divecomputers

Some divecomputer manufacturers use custom pid for their ftdi chips.
Some of them are Cressi (Leonardo), Suunto and Oceanic.
These are added to accepted_pids.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
Again changed to pass usb file descriptor

As experiments continue, I visit this technique again.
But instead of using the file descriptor as tty device,
now using it to make a libusb_device. This could help us
in talking with the usb device after appropriate permissions
are taken. Crossed fingers.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
libftdi1 in pkg-config when building for android

If you are building for android, you would need libftdi package.
Hence add this in Required.private so that it may be included
automatically while cross compilation.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
Bug causing indefinite block on serial_close android

Mistakenly, I was trying to close the ftdi device after freeing its ftdi
context. This was causing an indefinite blockage on serial_close function call
at libusb_release_interface. Fixed it.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
Implement exponential backoff on 0 return

ftdi_read_data returns the number of bytes read (+ve), 0 when there is no data
to read, and negative when there is an error.
Instead of infinitely waiting for the data to appear, it is better to implement
an exponential backoff which times out after MAX_BACKOFF.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
Implementation of serial_enumerate

Proper implementation of serial_enumerate.c
Get a list of attached ftdi devices and pass the struct libusb_device in
callback function for every device found. For this, the definition of
serial_callback_t has been changed. Changing const char *name to const void
*params so that char as well as libusb_device can be passed.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>