diff options
Diffstat (limited to 'src/components/transport_manager/src/usb/libusb/usb_connection.cc')
-rw-r--r-- | src/components/transport_manager/src/usb/libusb/usb_connection.cc | 137 |
1 files changed, 71 insertions, 66 deletions
diff --git a/src/components/transport_manager/src/usb/libusb/usb_connection.cc b/src/components/transport_manager/src/usb/libusb/usb_connection.cc index 409209b2b2..8e3e4cdd4d 100644 --- a/src/components/transport_manager/src/usb/libusb/usb_connection.cc +++ b/src/components/transport_manager/src/usb/libusb/usb_connection.cc @@ -30,16 +30,18 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#ifdef OS_POSIX #include <pthread.h> #include <unistd.h> +#elif defined(OS_WINDOWS) +#include <io.h> +#endif #include <iomanip> -#include <libusb/libusb.h> - #include <sstream> -#include "transport_manager/usb/libusb/usb_connection.h" #include "transport_manager/transport_adapter/transport_adapter_impl.h" +#include "transport_manager/usb/libusb/usb_connection.h" #include "utils/logger.h" @@ -74,24 +76,24 @@ UsbConnection::UsbConnection(const DeviceUID& device_uid, , waiting_out_transfer_cancel_(false) {} UsbConnection::~UsbConnection() { - LOG4CXX_TRACE(logger_, "enter with this" << this); + LOGGER_TRACE(logger_, "enter with this" << this); Finalise(); libusb_free_transfer(in_transfer_); delete[] in_buffer_; - LOG4CXX_TRACE(logger_, "exit"); + LOGGER_TRACE(logger_, "exit"); } // Callback for handling income and outcome data from lib_usb -void InTransferCallback(libusb_transfer* transfer) { +void LIBUSB_CALL InTransferCallback(libusb_transfer* transfer) { static_cast<UsbConnection*>(transfer->user_data)->OnInTransfer(transfer); } -void OutTransferCallback(libusb_transfer* transfer) { +void LIBUSB_CALL OutTransferCallback(libusb_transfer* transfer) { static_cast<UsbConnection*>(transfer->user_data)->OnOutTransfer(transfer); } bool UsbConnection::PostInTransfer() { - LOG4CXX_TRACE(logger_, "enter"); + LOGGER_TRACE(logger_, "enter"); libusb_fill_bulk_transfer(in_transfer_, device_handle_, in_endpoint_, @@ -102,15 +104,15 @@ bool UsbConnection::PostInTransfer() { 0); const int libusb_ret = libusb_submit_transfer(in_transfer_); if (LIBUSB_SUCCESS != libusb_ret) { - LOG4CXX_ERROR( + LOGGER_ERROR( logger_, "libusb_submit_transfer failed: " << libusb_error_name(libusb_ret)); - LOG4CXX_TRACE( + LOGGER_TRACE( logger_, "exit with FALSE. Condition: LIBUSB_SUCCESS != libusb_submit_transfer"); return false; } - LOG4CXX_TRACE(logger_, "exit with TRUE"); + LOGGER_TRACE(logger_, "exit with TRUE"); return true; } @@ -125,19 +127,19 @@ std::string hex_data(const unsigned char* const buffer, } void UsbConnection::OnInTransfer(libusb_transfer* transfer) { - LOG4CXX_TRACE(logger_, "enter with Libusb_transfer*: " << transfer); + LOGGER_TRACE(logger_, "enter with Libusb_transfer*: " << transfer); if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { - LOG4CXX_DEBUG(logger_, - "USB incoming transfer, size:" - << transfer->actual_length << ", data:" - << hex_data(transfer->buffer, transfer->actual_length)); + LOGGER_DEBUG(logger_, + "USB incoming transfer, size:" + << transfer->actual_length << ", data:" + << hex_data(transfer->buffer, transfer->actual_length)); ::protocol_handler::RawMessagePtr data(new protocol_handler::RawMessage( 0, 0, in_buffer_, transfer->actual_length)); controller_->DataReceiveDone(device_uid_, app_handle_, data); } else { - LOG4CXX_ERROR(logger_, - "USB incoming transfer failed: " - << libusb_error_name(transfer->status)); + LOGGER_ERROR(logger_, + "USB incoming transfer failed: " + << libusb_error_name(transfer->status)); controller_->DataReceiveFailed( device_uid_, app_handle_, DataReceiveError()); } @@ -145,17 +147,17 @@ void UsbConnection::OnInTransfer(libusb_transfer* transfer) { waiting_in_transfer_cancel_ = false; } else { if (!PostInTransfer()) { - LOG4CXX_ERROR(logger_, - "USB incoming transfer failed with " - << "LIBUSB_TRANSFER_NO_DEVICE. Abort connection."); + LOGGER_ERROR(logger_, + "USB incoming transfer failed with " + << "LIBUSB_TRANSFER_NO_DEVICE. Abort connection."); AbortConnection(); } } - LOG4CXX_TRACE(logger_, "exit"); + LOGGER_TRACE(logger_, "exit"); } void UsbConnection::PopOutMessage() { - LOG4CXX_TRACE(logger_, "enter"); + LOGGER_TRACE(logger_, "enter"); bytes_sent_ = 0; if (out_messages_.empty()) { current_out_message_.reset(); @@ -164,15 +166,15 @@ void UsbConnection::PopOutMessage() { out_messages_.pop_front(); PostOutTransfer(); } - LOG4CXX_TRACE(logger_, "exit"); + LOGGER_TRACE(logger_, "exit"); } bool UsbConnection::PostOutTransfer() { - LOG4CXX_TRACE(logger_, "enter"); + LOGGER_TRACE(logger_, "enter"); out_transfer_ = libusb_alloc_transfer(0); if (0 == out_transfer_) { - LOG4CXX_ERROR(logger_, "libusb_alloc_transfer failed"); - LOG4CXX_TRACE(logger_, "exit with FALSE. Condition: 0 == out_transfer_"); + LOGGER_ERROR(logger_, "libusb_alloc_transfer failed"); + LOGGER_TRACE(logger_, "exit with FALSE. Condition: 0 == out_transfer_"); return false; } libusb_fill_bulk_transfer(out_transfer_, @@ -185,34 +187,33 @@ bool UsbConnection::PostOutTransfer() { 0); const int libusb_ret = libusb_submit_transfer(out_transfer_); if (LIBUSB_SUCCESS != libusb_ret) { - LOG4CXX_ERROR( - logger_, - "libusb_submit_transfer failed: " << libusb_error_name(libusb_ret) - << ". Abort connection."); + LOGGER_ERROR(logger_, + "libusb_submit_transfer failed: " + << libusb_error_name(libusb_ret) << ". Abort connection."); AbortConnection(); - LOG4CXX_TRACE(logger_, - "exit with FALSE. Condition: " - << "LIBUSB_SUCCESS != libusb_fill_bulk_transfer"); + LOGGER_TRACE(logger_, + "exit with FALSE. Condition: " + << "LIBUSB_SUCCESS != libusb_fill_bulk_transfer"); return false; } - LOG4CXX_TRACE(logger_, "exit with TRUE"); + LOGGER_TRACE(logger_, "exit with TRUE"); return true; } void UsbConnection::OnOutTransfer(libusb_transfer* transfer) { - LOG4CXX_TRACE(logger_, "enter with Libusb_transfer*: " << transfer); + LOGGER_TRACE(logger_, "enter with Libusb_transfer*: " << transfer); sync_primitives::AutoLock locker(out_messages_mutex_); if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { bytes_sent_ += transfer->actual_length; if (bytes_sent_ == current_out_message_->data_size()) { - LOG4CXX_DEBUG( + LOGGER_DEBUG( logger_, "USB out transfer, data sent: " << current_out_message_.get()); controller_->DataSendDone(device_uid_, app_handle_, current_out_message_); PopOutMessage(); } } else { - LOG4CXX_ERROR( + LOGGER_ERROR( logger_, "USB out transfer failed: " << libusb_error_name(transfer->status)); controller_->DataSendFailed( @@ -224,16 +225,16 @@ void UsbConnection::OnOutTransfer(libusb_transfer* transfer) { out_transfer_ = NULL; waiting_out_transfer_cancel_ = false; } - LOG4CXX_TRACE(logger_, "exit"); + LOGGER_TRACE(logger_, "exit"); } TransportAdapter::Error UsbConnection::SendData( ::protocol_handler::RawMessagePtr message) { - LOG4CXX_TRACE(logger_, "enter with RawMessagePtr: " << message.get()); + LOGGER_TRACE(logger_, "enter with RawMessagePtr: " << message.get()); if (disconnecting_) { - LOG4CXX_TRACE(logger_, - "exit with TransportAdapter::BAD_STATE. Condition: " - << "disconnecting_"); + LOGGER_TRACE(logger_, + "exit with TransportAdapter::BAD_STATE. Condition: " + << "disconnecting_"); return TransportAdapter::BAD_STATE; } sync_primitives::AutoLock locker(out_messages_mutex_); @@ -244,19 +245,19 @@ TransportAdapter::Error UsbConnection::SendData( if (!PostOutTransfer()) { controller_->DataSendFailed( device_uid_, app_handle_, message, DataSendError()); - LOG4CXX_TRACE( + LOGGER_TRACE( logger_, "exit with TransportAdapter::FAIL. Condition: !PostOutTransfer()"); return TransportAdapter::FAIL; } } - LOG4CXX_TRACE(logger_, "exit with TransportAdapter::OK."); + LOGGER_TRACE(logger_, "exit with TransportAdapter::OK."); return TransportAdapter::OK; } void UsbConnection::Finalise() { - LOG4CXX_TRACE(logger_, "enter"); - LOG4CXX_DEBUG(logger_, "Finalise USB connection " << device_uid_); + LOGGER_TRACE(logger_, "enter"); + LOGGER_DEBUG(logger_, "Finalise USB connection " << device_uid_); { sync_primitives::AutoLock locker(out_messages_mutex_); disconnecting_ = true; @@ -281,17 +282,21 @@ void UsbConnection::Finalise() { } } while (waiting_in_transfer_cancel_ || waiting_out_transfer_cancel_) { +#ifdef OS_POSIX pthread_yield(); +#elif defined(OS_WINDOWS) + SwitchToThread(); +#endif } - LOG4CXX_TRACE(logger_, "exit"); + LOGGER_TRACE(logger_, "exit"); } void UsbConnection::AbortConnection() { - LOG4CXX_TRACE(logger_, "enter"); + LOGGER_TRACE(logger_, "enter"); controller_->ConnectionAborted( device_uid_, app_handle_, CommunicationError()); Disconnect(); - LOG4CXX_TRACE(logger_, "exit"); + LOGGER_TRACE(logger_, "exit"); } TransportAdapter::Error UsbConnection::Disconnect() { @@ -301,44 +306,44 @@ TransportAdapter::Error UsbConnection::Disconnect() { } bool UsbConnection::Init() { - LOG4CXX_TRACE(logger_, "enter"); + LOGGER_TRACE(logger_, "enter"); if (!FindEndpoints()) { - LOG4CXX_ERROR(logger_, "EndPoints was not found"); - LOG4CXX_TRACE(logger_, "exit with FALSE. Condition: !FindEndpoints()"); + LOGGER_ERROR(logger_, "EndPoints was not found"); + LOGGER_TRACE(logger_, "exit with FALSE. Condition: !FindEndpoints()"); return false; } in_buffer_ = new unsigned char[in_endpoint_max_packet_size_]; in_transfer_ = libusb_alloc_transfer(0); if (NULL == in_transfer_) { - LOG4CXX_ERROR(logger_, "libusb_alloc_transfer failed"); - LOG4CXX_TRACE(logger_, "exit with FALSE. Condition: NULL == in_transfer_"); + LOGGER_ERROR(logger_, "libusb_alloc_transfer failed"); + LOGGER_TRACE(logger_, "exit with FALSE. Condition: NULL == in_transfer_"); return false; } controller_->ConnectDone(device_uid_, app_handle_); if (!PostInTransfer()) { - LOG4CXX_ERROR(logger_, "PostInTransfer failed. Call ConnectionAborted"); + LOGGER_ERROR(logger_, "PostInTransfer failed. Call ConnectionAborted"); controller_->ConnectionAborted( device_uid_, app_handle_, CommunicationError()); - LOG4CXX_TRACE(logger_, "exit with FALSE. Condition: !PostInTransfer()"); + LOGGER_TRACE(logger_, "exit with FALSE. Condition: !PostInTransfer()"); return false; } - LOG4CXX_TRACE(logger_, "exit with TRUE"); + LOGGER_TRACE(logger_, "exit with TRUE"); return true; } bool UsbConnection::FindEndpoints() { - LOG4CXX_TRACE(logger_, "enter"); + LOGGER_TRACE(logger_, "enter"); struct libusb_config_descriptor* config; const int libusb_ret = libusb_get_active_config_descriptor(libusb_device_, &config); if (LIBUSB_SUCCESS != libusb_ret) { - LOG4CXX_ERROR(logger_, - "libusb_get_active_config_descriptor failed: " - << libusb_error_name(libusb_ret)); - LOG4CXX_TRACE(logger_, - "exit with FALSE. Condition: LIBUSB_SUCCESS != libusb_ret"); + LOGGER_ERROR(logger_, + "libusb_get_active_config_descriptor failed: " + << libusb_error_name(libusb_ret)); + LOGGER_TRACE(logger_, + "exit with FALSE. Condition: LIBUSB_SUCCESS != libusb_ret"); return false; } @@ -370,7 +375,7 @@ bool UsbConnection::FindEndpoints() { libusb_free_config_descriptor(config); const bool result = !(find_in_endpoint || find_out_endpoint); - LOG4CXX_TRACE(logger_, "exit with " << (result ? "TRUE" : "FALSE")); + LOGGER_TRACE(logger_, "exit with " << (result ? "TRUE" : "FALSE")); return result; } } // namespace transport_adapter |