diff options
Diffstat (limited to 'SDL_Core/src/components/transport_manager/src/usb/libusb')
3 files changed, 810 insertions, 0 deletions
diff --git a/SDL_Core/src/components/transport_manager/src/usb/libusb/platform_usb_device.cc b/SDL_Core/src/components/transport_manager/src/usb/libusb/platform_usb_device.cc new file mode 100644 index 000000000..2110ac3fc --- /dev/null +++ b/SDL_Core/src/components/transport_manager/src/usb/libusb/platform_usb_device.cc @@ -0,0 +1,79 @@ +/** + * \file platform_usb_device.cc + * \brief Libusb PlatformUsbDevice class source file. + * + * Copyright (c) 2013, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "transport_manager/usb/libusb/platform_usb_device.h" +#include "transport_manager/transport_adapter/transport_adapter_impl.h" + +namespace transport_manager { +namespace transport_adapter { + +PlatformUsbDevice::PlatformUsbDevice( + uint8_t bus_number, uint8_t address, + const libusb_device_descriptor& device_descriptor, + libusb_device* device_libusb, libusb_device_handle* device_handle_libusb) + : bus_number_(bus_number), + address_(address), + vendor_id_(device_descriptor.idVendor), + product_id_(device_descriptor.idProduct), + device_descriptor_(device_descriptor), + libusb_device_handle_(device_handle_libusb), + libusb_device_(device_libusb) {} + +std::string PlatformUsbDevice::GetDescString(uint8_t index) const { + unsigned char buf[128]; + const int libusb_ret = libusb_get_string_descriptor_ascii( + libusb_device_handle_, index, buf, sizeof(buf)); + if (libusb_ret < 0) { + LOG4CXX_INFO(logger_, "Failed to get USB string descriptor: " + << libusb_error_name(libusb_ret)); + return ""; + } + return std::string(reinterpret_cast<char*>(buf)); +} + +std::string PlatformUsbDevice::GetManufacturer() const { + return GetDescString(device_descriptor_.iManufacturer); +} + +std::string PlatformUsbDevice::GetProductName() const { + return GetDescString(device_descriptor_.iProduct); +} + +std::string PlatformUsbDevice::GetSerialNumber() const { + return GetDescString(device_descriptor_.iSerialNumber); +} + +} // namespace +} // namespace diff --git a/SDL_Core/src/components/transport_manager/src/usb/libusb/usb_connection.cc b/SDL_Core/src/components/transport_manager/src/usb/libusb/usb_connection.cc new file mode 100644 index 000000000..00c792de8 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/src/usb/libusb/usb_connection.cc @@ -0,0 +1,313 @@ +/** + * \file usb_connection.cc + * \brief UsbConnection class source file. + * + * Copyright (c) 2013, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <iomanip> + +#include <libusb/libusb.h> + +#include "transport_manager/usb/libusb/usb_connection.h" +#include "transport_manager/transport_adapter/transport_adapter_impl.h" + +namespace transport_manager { +namespace transport_adapter { + +UsbConnection::UsbConnection(const DeviceUID& device_uid, + const ApplicationHandle& app_handle, + TransportAdapterController* controller, + const UsbHandlerSptr& usb_handler, + PlatformUsbDevice* device) + : device_uid_(device_uid), + app_handle_(app_handle), + controller_(controller), + usb_handler_(usb_handler), + libusb_device_(device->GetLibusbDevice()), + device_handle_(device->GetLibusbHandle()), + in_endpoint_(0), + in_endpoint_max_packet_size_(0), + out_endpoint_(0), + out_endpoint_max_packet_size_(0), + in_buffer_(0), + in_transfer_(0), + out_transfer_(0), + out_messages_(), + current_out_message_(), + out_messages_mutex_(), + bytes_sent_(0), + disconnecting_(false), + waiting_in_transfer_cancel_(false), + waiting_out_transfer_cancel_(false) { + pthread_mutex_init(&out_messages_mutex_, 0); +} + +UsbConnection::~UsbConnection() { + Finalise(); + if (in_transfer_) { + libusb_free_transfer(in_transfer_); + } + if (in_buffer_) { + delete[] in_buffer_; + } + pthread_mutex_destroy(&out_messages_mutex_); +} + +void InTransferCallback(libusb_transfer* transfer) { + static_cast<UsbConnection*>(transfer->user_data)->OnInTransfer(transfer); +} + +void OutTransferCallback(libusb_transfer* transfer) { + static_cast<UsbConnection*>(transfer->user_data)->OnOutTransfer(transfer); +} + +bool UsbConnection::PostInTransfer() { + libusb_fill_bulk_transfer(in_transfer_, device_handle_, in_endpoint_, + in_buffer_, in_endpoint_max_packet_size_, + InTransferCallback, this, 0); + const int libusb_ret = libusb_submit_transfer(in_transfer_); + if (LIBUSB_SUCCESS != libusb_ret) { + LOG4CXX_ERROR(logger_, "libusb_submit_transfer failed: " + << libusb_error_name(libusb_ret)); + return false; + } + return true; +} + +void UsbConnection::OnInTransfer(libusb_transfer* transfer) { + if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { + if (logger_->isTraceEnabled()) { + std::ostringstream hexdata; + for (int i = 0; i < transfer->actual_length; ++i) { + hexdata << " " << std::hex << std::setw(2) << std::setfill('0') + << (int)transfer->buffer[i]; + } + LOG4CXX_TRACE(logger_, "USB incoming transfer, size:" + << transfer->actual_length + << ", data:" << hexdata.str()); + } + RawMessageSptr data(new protocol_handler::RawMessage( + 0, 0, in_buffer_, transfer->actual_length)); + controller_->DataReceiveDone(device_uid_, app_handle_, data); + } else { + LOG4CXX_ERROR(logger_, "USB transfer failed: " << transfer->status); + controller_->DataReceiveFailed(device_uid_, app_handle_, + DataReceiveError()); + } + if (disconnecting_) { + waiting_in_transfer_cancel_ = false; + } else { + if (!PostInTransfer()) { + controller_->ConnectionAborted(device_uid_, app_handle_, + CommunicationError()); + Disconnect(); + } + } +} + +void UsbConnection::PopOutMessage() { + bytes_sent_ = 0; + if (out_messages_.empty()) { + current_out_message_.reset(); + } else { + current_out_message_ = out_messages_.front(); + out_messages_.pop_front(); + PostOutTransfer(); + } +} + +bool UsbConnection::PostOutTransfer() { + out_transfer_ = libusb_alloc_transfer(0); + if (0 == out_transfer_) { + LOG4CXX_ERROR(logger_, "libusb_alloc_transfer failed"); + return false; + } + libusb_fill_bulk_transfer(out_transfer_, device_handle_, out_endpoint_, + current_out_message_->data() + bytes_sent_, + current_out_message_->data_size() - bytes_sent_, + OutTransferCallback, this, 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)); + controller_->ConnectionAborted(device_uid_, app_handle_, + CommunicationError()); + Disconnect(); + return false; + } + return true; +} + +void UsbConnection::OnOutTransfer(libusb_transfer* transfer) { + pthread_mutex_lock(&out_messages_mutex_); + if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { + bytes_sent_ += transfer->actual_length; + if (bytes_sent_ == current_out_message_->data_size()) { + LOG4CXX_INFO(logger_, "USB out transfer, data sent: " + << current_out_message_.get()); + controller_->DataSendDone(device_uid_, app_handle_, current_out_message_); + PopOutMessage(); + } + } else { + LOG4CXX_ERROR(logger_, "USB transfer failed: " << transfer->status); + controller_->DataSendFailed(device_uid_, app_handle_, current_out_message_, + DataSendError()); + PopOutMessage(); + } + libusb_free_transfer(transfer); + out_transfer_ = 0; + pthread_mutex_unlock(&out_messages_mutex_); + waiting_out_transfer_cancel_ = false; +} + +TransportAdapter::Error UsbConnection::SendData(RawMessageSptr message) { + if (disconnecting_) { + return TransportAdapter::BAD_STATE; + } + pthread_mutex_lock(&out_messages_mutex_); + if (current_out_message_.valid()) { + out_messages_.push_back(message); + } else { + current_out_message_ = message; + if (!PostOutTransfer()) { + controller_->DataSendFailed(device_uid_, app_handle_, message, + DataSendError()); + } + } + pthread_mutex_unlock(&out_messages_mutex_); + return TransportAdapter::OK; +} + +void UsbConnection::Finalise() { + LOG4CXX_INFO(logger_, "Finalise USB connection " << device_uid_); + pthread_mutex_lock(&out_messages_mutex_); + disconnecting_ = true; + if (out_transfer_) { + waiting_out_transfer_cancel_ = true; + if (LIBUSB_SUCCESS != libusb_cancel_transfer(out_transfer_)) { + waiting_out_transfer_cancel_ = false; + } + } + if (in_transfer_) { + waiting_in_transfer_cancel_ = true; + if (LIBUSB_SUCCESS != libusb_cancel_transfer(in_transfer_)) { + waiting_in_transfer_cancel_ = false; + } + } + for (std::list<RawMessageSptr>::iterator it = out_messages_.begin(); + it != out_messages_.end(); it = out_messages_.erase(it)) { + controller_->DataSendFailed(device_uid_, app_handle_, *it, DataSendError()); + } + pthread_mutex_unlock(&out_messages_mutex_); + + while (waiting_in_transfer_cancel_ || waiting_out_transfer_cancel_) { + pthread_yield(); + } +} + +TransportAdapter::Error UsbConnection::Disconnect() { + Finalise(); + LOG4CXX_INFO(logger_, "USB disconnect done " << device_uid_); + controller_->DisconnectDone(device_uid_, app_handle_); + return TransportAdapter::OK; +} + +bool UsbConnection::Init() { + if (!FindEndpoints()) { + return false; + } + + in_buffer_ = new unsigned char[in_endpoint_max_packet_size_]; + if (0 == in_buffer_) { + LOG4CXX_ERROR(logger_, "in buffer allocation failed (size " + << in_endpoint_max_packet_size_ << ")"); + return false; + } + + in_transfer_ = libusb_alloc_transfer(0); + if (0 == in_transfer_) { + LOG4CXX_ERROR(logger_, "libusb_alloc_transfer failed"); + return false; + } + + controller_->ConnectDone(device_uid_, app_handle_); + if (!PostInTransfer()) { + controller_->ConnectionAborted(device_uid_, app_handle_, + CommunicationError()); + return true; + } + + return true; +} + +bool UsbConnection::FindEndpoints() { + 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)); + return false; + } + + bool find_in_endpoint = true; + bool find_out_endpoint = true; + + for (int i = 0; i < config->bNumInterfaces; ++i) { + const libusb_interface& interface = config->interface[i]; + for (int i = 0; i < interface.num_altsetting; ++i) { + const libusb_interface_descriptor& iface_desc = interface.altsetting[i]; + for (int i = 0; i < iface_desc.bNumEndpoints; ++i) { + const libusb_endpoint_descriptor& endpoint_desc = + iface_desc.endpoint[i]; + + const uint8_t endpoint_dir = + endpoint_desc.bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK; + if (find_in_endpoint && endpoint_dir == LIBUSB_ENDPOINT_IN) { + in_endpoint_ = endpoint_desc.bEndpointAddress; + in_endpoint_max_packet_size_ = endpoint_desc.wMaxPacketSize; + find_in_endpoint = false; + } else if (find_out_endpoint && endpoint_dir == LIBUSB_ENDPOINT_OUT) { + out_endpoint_ = endpoint_desc.bEndpointAddress; + out_endpoint_max_packet_size_ = endpoint_desc.wMaxPacketSize; + find_out_endpoint = false; + } + } + } + } + libusb_free_config_descriptor(config); + + return !(find_in_endpoint || find_out_endpoint); +} + +} // namespace transport_adapter +} // namespace transport_manager diff --git a/SDL_Core/src/components/transport_manager/src/usb/libusb/usb_handler.cc b/SDL_Core/src/components/transport_manager/src/usb/libusb/usb_handler.cc new file mode 100644 index 000000000..88c4ee1da --- /dev/null +++ b/SDL_Core/src/components/transport_manager/src/usb/libusb/usb_handler.cc @@ -0,0 +1,418 @@ +/** + * \file usb_handler.cc + * \brief UsbHandler class source file. + * + * Copyright (c) 2013, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <cstring> +#include <cstdlib> + +#include "transport_manager/usb/common.h" +#include "transport_manager/transport_adapter/transport_adapter_impl.h" + +namespace transport_manager { +namespace transport_adapter { + +class UsbHandler::ControlTransferSequenceState { + public: + ControlTransferSequenceState(class UsbHandler* usb_handler, + UsbControlTransferSequence* sequence, + PlatformUsbDevice* device); + ~ControlTransferSequenceState(); + void Finish(); + bool Finished() const { return finished_; } + UsbControlTransfer* CurrentTransfer(); + UsbControlTransfer* Next(); + UsbHandler* usb_handler() const { return usb_handler_; } + PlatformUsbDevice* device() const { return device_; } + + private: + UsbHandler* usb_handler_; + PlatformUsbDevice* device_; + bool finished_; + UsbControlTransferSequence* sequence_; + UsbControlTransferSequence::Transfers::const_iterator current_transfer_; +}; + +UsbHandler::UsbHandler() + : shutdown_requested_(false), + thread_(), + usb_device_listeners_(), + devices_(), + transfer_sequences_(), + device_handles_to_close_(), + libusb_context_(NULL), + arrived_callback_handle_(), + left_callback_handle_() {} + +UsbHandler::~UsbHandler() { + shutdown_requested_ = true; + if (libusb_context_ != 0) { + libusb_hotplug_deregister_callback(libusb_context_, + arrived_callback_handle_); + libusb_hotplug_deregister_callback(libusb_context_, left_callback_handle_); + } + pthread_join(thread_, 0); + LOG4CXX_INFO(logger_, "UsbHandler thread finished"); + if (libusb_context_) { + libusb_exit(libusb_context_); + libusb_context_ = 0; + } +} + +void UsbHandler::DeviceArrived(libusb_device* device_libusb) { + const uint8_t bus_number = libusb_get_bus_number(device_libusb); + const uint8_t device_address = libusb_get_device_address(device_libusb); + libusb_device_descriptor descriptor; + int libusb_ret = libusb_get_device_descriptor(device_libusb, &descriptor); + if (LIBUSB_SUCCESS != libusb_ret) { + LOG4CXX_ERROR(logger_, + "libusb_get_device_descriptor failed: " << libusb_ret); + return; + } + + libusb_device_handle* device_handle_libusb; + libusb_ret = libusb_open(device_libusb, &device_handle_libusb); + if (libusb_ret != LIBUSB_SUCCESS) { + LOG4CXX_ERROR(logger_, + "libusb_open failed: " << libusb_error_name(libusb_ret)); + return; + } + + int configuration; + libusb_ret = libusb_get_configuration(device_handle_libusb, &configuration); + if (LIBUSB_SUCCESS != libusb_ret) { + LOG4CXX_INFO( + logger_, + "libusb_get_configuration failed: " << libusb_error_name(libusb_ret)); + return; + } + + if (configuration != kUsbConfiguration) { + libusb_ret = libusb_set_configuration(device_handle_libusb, kUsbConfiguration); + if (LIBUSB_SUCCESS != libusb_ret) { + LOG4CXX_INFO( + logger_, + "libusb_set_configuration failed: " << libusb_error_name(libusb_ret)); + return; + } + } + + libusb_ret = libusb_claim_interface(device_handle_libusb, 0); + if (LIBUSB_SUCCESS != libusb_ret) { + LOG4CXX_INFO(logger_, "libusb_claim_interface failed: " + << libusb_error_name(libusb_ret)); + CloseDeviceHandle(device_handle_libusb); + return; + } + + PlatformUsbDevice* device(new PlatformUsbDevice(bus_number, device_address, + descriptor, device_libusb, + device_handle_libusb)); + devices_.push_back(device); + + for (std::list<UsbDeviceListener*>::iterator it = + usb_device_listeners_.begin(); + it != usb_device_listeners_.end(); ++it) { + (*it)->OnDeviceArrived(device); + } +} + +void UsbHandler::DeviceLeft(libusb_device* device_libusb) { + PlatformUsbDevice* device = NULL; + for (Devices::iterator it = devices_.begin(); it != devices_.end(); ++it) { + if ((*it)->GetLibusbDevice() == device_libusb) { + device = *it; + break; + } + } + if (NULL == device) { + return; + } + + for (std::list<UsbDeviceListener*>::iterator it = + usb_device_listeners_.begin(); + it != usb_device_listeners_.end(); ++it) { + (*it)->OnDeviceLeft(device); + } + + for (Devices::iterator it = devices_.begin(); it != devices_.end(); ++it) { + if ((*it)->GetLibusbDevice() == device_libusb) { + devices_.erase(it); + break; + } + } + + if (device->GetLibusbHandle()) { + libusb_release_interface(device->GetLibusbHandle(), 0); + CloseDeviceHandle(device->GetLibusbHandle()); + } + delete device; +} + +void UsbHandler::StartControlTransferSequence( + UsbControlTransferSequence* sequence, PlatformUsbDevice* device) { + TransferSequences::iterator it = transfer_sequences_.insert( + transfer_sequences_.end(), + new ControlTransferSequenceState(this, sequence, device)); + SubmitControlTransfer(*it); +} + +void UsbHandler::CloseDeviceHandle(libusb_device_handle* device_handle) { + device_handles_to_close_.push_back(device_handle); +} + +void* UsbHandlerThread(void* data) { + static_cast<UsbHandler*>(data)->Thread(); + return 0; +} + +int ArrivedCallback(libusb_context* context, libusb_device* device, + libusb_hotplug_event event, void* data) { + LOG4CXX_INFO(logger_, "libusb device arrived (bus number " + << static_cast<int>(libusb_get_bus_number(device)) + << ", device address " + << static_cast<int>( + libusb_get_device_address(device)) << ")"); + UsbHandler* usb_handler = static_cast<UsbHandler*>(data); + usb_handler->DeviceArrived(device); + return 0; +} + +int LeftCallback(libusb_context* context, libusb_device* device, + libusb_hotplug_event event, void* data) { + LOG4CXX_INFO(logger_, "libusb device left (bus number " + << static_cast<int>(libusb_get_bus_number(device)) + << ", device address " + << static_cast<int>( + libusb_get_device_address(device)) << ")"); + UsbHandler* usb_handler = static_cast<UsbHandler*>(data); + usb_handler->DeviceLeft(device); + return 0; +} + +TransportAdapter::Error UsbHandler::Init() { + int libusb_ret = libusb_init(&libusb_context_); + + if (LIBUSB_SUCCESS != libusb_ret) { + LOG4CXX_ERROR(logger_, "libusb_init failed: " << libusb_ret); + return TransportAdapter::FAIL; + } + + if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) { + LOG4CXX_ERROR(logger_, "LIBUSB_CAP_HAS_HOTPLUG not supported"); + return TransportAdapter::FAIL; + } + + libusb_ret = libusb_hotplug_register_callback( + libusb_context_, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, + LIBUSB_HOTPLUG_ENUMERATE, LIBUSB_HOTPLUG_MATCH_ANY, + LIBUSB_HOTPLUG_MATCH_ANY, LIBUSB_HOTPLUG_MATCH_ANY, ArrivedCallback, this, + &arrived_callback_handle_); + + if (LIBUSB_SUCCESS != libusb_ret) { + LOG4CXX_ERROR(logger_, + "libusb_hotplug_register_callback failed: " << libusb_ret); + return TransportAdapter::FAIL; + } + + libusb_ret = libusb_hotplug_register_callback( + libusb_context_, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, + static_cast<libusb_hotplug_flag>(0), LIBUSB_HOTPLUG_MATCH_ANY, + LIBUSB_HOTPLUG_MATCH_ANY, LIBUSB_HOTPLUG_MATCH_ANY, LeftCallback, this, + &left_callback_handle_); + + if (LIBUSB_SUCCESS != libusb_ret) { + LOG4CXX_ERROR(logger_, + "libusb_hotplug_register_callback failed: " << libusb_ret); + return TransportAdapter::FAIL; + } + + const int thread_start_error = + pthread_create(&thread_, 0, &UsbHandlerThread, this); + if (0 == thread_start_error) { + LOG4CXX_INFO(logger_, "UsbHandler thread started"); + return TransportAdapter::OK; + } else { + LOG4CXX_ERROR(logger_, "USB device scanner thread start failed, error code " + << thread_start_error); + return TransportAdapter::FAIL; + } +} + +void UsbHandler::Thread() { + int completed = 0; + while (!shutdown_requested_) { + libusb_handle_events_completed(libusb_context_, &completed); + + for (TransferSequences::iterator it = transfer_sequences_.begin(); + it != transfer_sequences_.end();) { + ControlTransferSequenceState* sequence_state = *it; + if (sequence_state->Finished()) { + delete *it; + it = transfer_sequences_.erase(it); + } else { + ++it; + } + } + + for (std::list<libusb_device_handle*>::iterator it = + device_handles_to_close_.begin(); + it != device_handles_to_close_.end(); + it = device_handles_to_close_.erase(it)) { + libusb_close(*it); + } + } +} + +void UsbTransferSequenceCallback(libusb_transfer* transfer) { + UsbHandler::ControlTransferSequenceState* sequence_state = + static_cast<UsbHandler::ControlTransferSequenceState*>(transfer->user_data); + sequence_state->usb_handler()->ControlTransferCallback(transfer); +} + +void UsbHandler::SubmitControlTransfer( + ControlTransferSequenceState* sequence_state) { + UsbControlTransfer* transfer = sequence_state->CurrentTransfer(); + if (NULL == transfer) return; + + libusb_transfer* libusb_transfer = libusb_alloc_transfer(0); + if (0 == libusb_transfer) { + LOG4CXX_ERROR(logger_, "libusb_alloc_transfer failed"); + sequence_state->Finish(); + return; + } + + assert(transfer->Type() == UsbControlTransfer::VENDOR); + const libusb_request_type request_type = LIBUSB_REQUEST_TYPE_VENDOR; + + libusb_endpoint_direction endpoint_direction; + if (transfer->Direction() == UsbControlTransfer::IN) { + endpoint_direction = LIBUSB_ENDPOINT_IN; + } else if (transfer->Direction() == UsbControlTransfer::OUT) { + endpoint_direction = LIBUSB_ENDPOINT_OUT; + } else { + assert(0); + } + const uint8_t request = transfer->Request(); + const uint16_t value = transfer->Value(); + const uint16_t index = transfer->Index(); + const uint16_t length = transfer->Length(); + + unsigned char* buffer = + static_cast<unsigned char*>(malloc(length + LIBUSB_CONTROL_SETUP_SIZE)); + if (NULL == buffer) { + LOG4CXX_ERROR(logger_, "buffer allocation failed"); + libusb_free_transfer(libusb_transfer); + sequence_state->Finish(); + return; + } + + libusb_fill_control_setup(buffer, request_type | endpoint_direction, request, + value, index, length); + + if (0 != length && endpoint_direction == LIBUSB_ENDPOINT_OUT) { + const char* data = static_cast<UsbControlOutTransfer*>(transfer)->Data(); + memcpy(buffer + LIBUSB_CONTROL_SETUP_SIZE, data, length); + } + libusb_fill_control_transfer( + libusb_transfer, sequence_state->device()->GetLibusbHandle(), buffer, + UsbTransferSequenceCallback, sequence_state, 0); + libusb_transfer->flags = LIBUSB_TRANSFER_FREE_BUFFER; + + const int libusb_ret = libusb_submit_transfer(libusb_transfer); + if (LIBUSB_SUCCESS != libusb_ret) { + LOG4CXX_ERROR(logger_, "libusb_submit_transfer failed: " + << libusb_error_name(libusb_ret)); + libusb_free_transfer(libusb_transfer); + sequence_state->Finish(); + } +} + +void UsbHandler::ControlTransferCallback(libusb_transfer* transfer) { + ControlTransferSequenceState* sequence_state = + static_cast<ControlTransferSequenceState*>(transfer->user_data); + if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { + LOG4CXX_INFO(logger_, "USB control transfer completed"); + UsbControlTransfer* current_transfer = sequence_state->CurrentTransfer(); + bool submit_next = true; + if (current_transfer && + current_transfer->Direction() == UsbControlTransfer::IN) { + submit_next = + static_cast<UsbControlInTransfer*>(current_transfer) + ->OnCompleted(libusb_control_transfer_get_data(transfer)); + } + + sequence_state->Next(); + if (submit_next && !sequence_state->Finished()) { + SubmitControlTransfer(sequence_state); + } else { + sequence_state->Finish(); + } + } else { + LOG4CXX_ERROR(logger_, "USB control transfer failed: " << transfer->status); + sequence_state->Finish(); + } + libusb_free_transfer(transfer); +} + +UsbHandler::ControlTransferSequenceState::ControlTransferSequenceState( + UsbHandler* usb_handler, UsbControlTransferSequence* sequence, + PlatformUsbDevice* device) + : usb_handler_(usb_handler), + device_(device), + finished_(false), + sequence_(sequence), + current_transfer_(sequence->transfers().begin()) {} + +UsbHandler::ControlTransferSequenceState::~ControlTransferSequenceState() { + delete sequence_; +} + +UsbControlTransfer* UsbHandler::ControlTransferSequenceState::Next() { + if (finished_) return NULL; + if (++current_transfer_ == sequence_->transfers().end()) { + Finish(); + return NULL; + } else { + return *current_transfer_; + } +} + +UsbControlTransfer* UsbHandler::ControlTransferSequenceState::CurrentTransfer() { + return finished_ ? NULL : *current_transfer_; +} + +void UsbHandler::ControlTransferSequenceState::Finish() { finished_ = true; } + +} // namespace +} // namespace |