diff options
author | Justin Dickow <jjdickow@gmail.com> | 2014-03-18 13:46:46 -0400 |
---|---|---|
committer | Justin Dickow <jjdickow@gmail.com> | 2014-03-18 13:46:46 -0400 |
commit | ba492fb83c258bc60ca68120ce964a95b41133b5 (patch) | |
tree | af99c087941d65a19831397c1ec5eb34850cfc7f /SDL_Core/src/components/transport_manager/include/transport_manager | |
parent | 8504605b01177da2e55bee6abe4c3f20c82da379 (diff) | |
download | smartdevicelink-ba492fb83c258bc60ca68120ce964a95b41133b5.tar.gz |
initial commit for API 3.0 (replaced all)
Diffstat (limited to 'SDL_Core/src/components/transport_manager/include/transport_manager')
43 files changed, 6062 insertions, 0 deletions
diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_connection_factory.h b/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_connection_factory.h new file mode 100644 index 000000000..1bf8cbbcb --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_connection_factory.h @@ -0,0 +1,98 @@ +/** + * \file bluetooth_connection_factory.h + * \brief BluetoothConnectionFactory class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_BLUETOOTH_BLUETOOTH_CONNECTION_FACTORY_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_BLUETOOTH_BLUETOOTH_CONNECTION_FACTORY_H_ + +#include "transport_manager/transport_adapter/server_connection_factory.h" + +namespace transport_manager { +namespace transport_adapter { + +class TransportAdapterController; + +/** + * @brief Create connections. + */ +class BluetoothConnectionFactory : public ServerConnectionFactory { + public: + + /** + * @brief Constructor. + * + * @param controller Pointer to the device adapter controller. + */ + BluetoothConnectionFactory(TransportAdapterController* controller); + protected: + + /** + * @brief Start BT connection factory. + */ + virtual TransportAdapter::Error Init(); + + /** + * @brief Create bluetooth socket connection. + * + * @param device_uid Device unique identifier. + * @param ap_handle Handle of application. + */ + virtual TransportAdapter::Error CreateConnection(const DeviceUID& device_uid, + const ApplicationHandle& app_handle); + + /** + * @brief + */ + virtual void Terminate(); + + /** + * @brief Check for initialization. + * + * @return true - initialized. + * false - not initialized. + */ + virtual bool IsInitialised() const; + + /** + * @brief Destructor. + */ + virtual ~BluetoothConnectionFactory(); + private: + TransportAdapterController* controller_; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_BLUETOOTH_CONNECTION_FACTORY_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_device.h b/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_device.h new file mode 100644 index 000000000..f34fd089b --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_device.h @@ -0,0 +1,128 @@ +/** + * \file bluetooth_device.h + * \brief BluetoothDevice class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_BLUETOOTH_BLUETOOTH_DEVICE_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_BLUETOOTH_BLUETOOTH_DEVICE_H_ + +#include <bluetooth/bluetooth.h> +#include <bluetooth/hci.h> +#include <bluetooth/hci_lib.h> +#include <bluetooth/sdp.h> +#include <bluetooth/sdp_lib.h> +#include <bluetooth/rfcomm.h> + +#include <vector> + +#include "transport_manager/common.h" +#include "transport_manager/transport_adapter/device.h" + +namespace transport_manager { +namespace transport_adapter { + +/** + * @brief Type definition for vector that holds uint8_t variables. + */ +typedef std::vector<uint8_t> RfcommChannelVector; + +/** + * @brief Information about device that use bluetooth transport. + */ +class BluetoothDevice : public Device { + public: + + /** + * @brief Return device unique identifier. + * + * @return string with device unique identifier. + */ + static std::string GetUniqueDeviceId(const bdaddr_t& device_address); + + /** + * @brief Constructor. + * + * @param address Bluetooth address. + * @param name Human-readable device name. + * @param rfcomm_channels List of RFCOMM channels where SmartDeviceLink service has been discovered. + **/ + BluetoothDevice(const bdaddr_t& device_address, const char* device_name, + const RfcommChannelVector& rfcomm_channels); + + /** + * @brief Compare devices. + * + * This method checks whether two SBluetoothDevice structures + * refer to the same device. + * + * @param other Device to compare with. + * + * @return true if devices are equal, false otherwise. + **/ + virtual bool IsSameAs(const Device* other) const; + + bool GetRfcommChannel(const ApplicationHandle app_handle, + uint8_t* channel_out); + + /** + * @brief Update list of applications available on device. + * + * @return Container with list of applications. + */ + virtual ApplicationList GetApplicationList() const; + + /** + * @brief Return device bluetooth address. + * + * @return Device bluetooth address. + */ + const bdaddr_t& address() const { + return address_; + } + + private: + /** + * @brief Device bluetooth address. + **/ + bdaddr_t address_; + + /** + * @brief List of RFCOMM channels where SmartDeviceLink service has been discovered. + **/ + RfcommChannelVector rfcomm_channels_; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_BLUETOOTH_DEVICE_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_device_scanner.h b/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_device_scanner.h new file mode 100644 index 000000000..b89082209 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_device_scanner.h @@ -0,0 +1,178 @@ +/** + * \file bluetooth_device_scanner.h + * \brief BluetoothDeviceScanner class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_BLUETOOTH_BLUETOOTH_DEVICE_SCANNER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_BLUETOOTH_BLUETOOTH_DEVICE_SCANNER_H_ + +#include <bluetooth/bluetooth.h> +#include <bluetooth/hci.h> +#include <bluetooth/hci_lib.h> +#include <bluetooth/sdp.h> +#include <bluetooth/sdp_lib.h> +#include <bluetooth/rfcomm.h> + +#include "transport_manager/transport_adapter/device_scanner.h" +#include "utils/conditional_variable.h" +#include "utils/lock.h" + +namespace transport_manager { +namespace transport_adapter { + +class TransportAdapterController; + +/** + * @brief Scan for devices using bluetooth. + */ +class BluetoothDeviceScanner : public DeviceScanner { + public: + + /** + * @brief Constructor. + * @param controller Transport adapter controller + * @param auto_repeat_search true - autorepeated or continous device search, false - search on demand + * @param repeat_search_pause_sec - pause between device searches, 0 means continous search + */ + BluetoothDeviceScanner(TransportAdapterController* controller, + bool auto_repeat_search, int repeat_search_pause_sec); + + /** + * @brief Destructor. + */ + ~BluetoothDeviceScanner(); + + /** + * @brief Main thread initialization. + */ + void Thread(); + protected: + + /** + * @brief Start device scanner. + * + * @return Error information about reason of initialization failure. + */ + virtual TransportAdapter::Error Init(); + + /** + * @brief Terminates device scanner thread + */ + virtual void Terminate(); + + /** + * @brief Request device search + * + * @return Error information about reason of Scan failure. + */ + virtual TransportAdapter::Error Scan(); + + /** + * @brief Check device scanner for initialization. + * + * @return true - initialized. + * false - not initialized. + */ + virtual bool IsInitialised() const; + private: + + typedef std::vector<uint8_t> RfcommChannelVector; + + /** + * @brief Waits for external scan request or time out for repeated search or terminate request + */ + void TimedWaitForDeviceScanRequest(); + + /** + * @brief Finds RFCOMM-channels of SDL enabled applications for set of devices + * @param device_addresses Bluetooth addresses to search on + * @return List of RFCOMM-channels lists + */ + std::vector<RfcommChannelVector> DiscoverSmartDeviceLinkRFCOMMChannels( + const std::vector<bdaddr_t>& device_addresses); + + /** + * @brief Finds RFCOMM-channels of SDL enabled applications for given device + * @param[out] discovered List of RFCOMM-channels to fill + * @return true - if search was OK, false if it failed + */ + bool DiscoverSmartDeviceLinkRFCOMMChannels(const bdaddr_t& device_address, + RfcommChannelVector* discovered); + + /** + * @brief Summarizes the total list of devices (paired and scanned) and notifies controller + */ + void UpdateTotalDeviceList(); + + /** + * @brief Performs search of SDL-enabled devices + */ + void DoInquiry(); + + /** + * @brief Checks if given devices have SDL service and creates appropriate BluetoothDevice objects + * @param bd_address List of bluetooth addresses to check + * @param device_handle HCI handle + * @param[out] discovered_devices List of created BluetoothDevice objects to fill + */ + void CheckSDLServiceOnDevices(const std::vector<bdaddr_t>& bd_address, + int device_handle, + DeviceVector* discovered_devices); + + TransportAdapterController* controller_; + pthread_t thread_; + bool thread_started_; + bool shutdown_requested_; + bool device_scan_requested_; + bool ready_; + sync_primitives::Lock device_scan_requested_lock_; + sync_primitives::ConditionalVariable device_scan_requested_cv_; + + std::vector<bdaddr_t> paired_devices_; + + DeviceVector paired_devices_with_sdl_; + DeviceVector found_devices_with_sdl_; + + /** + * @brief UUID of SmartDeviceLink service. + **/ + uuid_t smart_device_link_service_uuid_; + + const bool auto_repeat_search_; + const int auto_repeat_pause_sec_; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif /* BLUETOOTH_DEVICE_SCANNER_H_ */ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_socket_connection.h b/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_socket_connection.h new file mode 100644 index 000000000..e1606da87 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_socket_connection.h @@ -0,0 +1,83 @@ +/** + * \file bluetooth_socket_connection.h + * \brief BluetoothSocketConnection class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_BLUETOOTH_BLUETOOTH_SOCKET_CONNECTION_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_BLUETOOTH_BLUETOOTH_SOCKET_CONNECTION_H_ + +#include "transport_manager/transport_adapter/threaded_socket_connection.h" + +namespace transport_manager { +namespace transport_adapter { + +class TransportAdapterController; + +/** + * @brief Class responsible for communication over bluetooth sockets. + */ +class BluetoothSocketConnection : public ThreadedSocketConnection { + public: + + /** + * @brief Constructor. + * + * @param device_uid Device unique identifier. + * @param app_handle Handle of device. + * @param controller Pointer to the device adapter controller. + */ + BluetoothSocketConnection(const DeviceUID& device_uid, + const ApplicationHandle& app_handle, + TransportAdapterController* controller); + + /** + * @brief Destructor. + */ + virtual ~BluetoothSocketConnection(); + protected: + + /** + * @brief Establish connection. + * + * @param error Connection error. + * + * @return True - connection established. + * false - connection not established. + */ + virtual bool Establish(ConnectError** error); +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif /* BLUETOOTH_SOCKET_CONNECTION_H_ */ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_transport_adapter.h b/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_transport_adapter.h new file mode 100644 index 000000000..2816581a2 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/bluetooth/bluetooth_transport_adapter.h @@ -0,0 +1,81 @@ +/** + * \file bluetooth_transport_adapter.h + * \brief BluetoothAdapter class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_BLUETOOTH_BLUETOOTH_ADAPTER_H +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_BLUETOOTH_BLUETOOTH_ADAPTER_H + +#include "transport_manager/transport_adapter/transport_adapter_impl.h" + +namespace transport_manager { +namespace transport_adapter { + +/** + * @brief Transport adapter that use bluetooth transport. + */ +class BluetoothTransportAdapter : public TransportAdapterImpl { + public: + /** + * @brief Constructor. + */ + BluetoothTransportAdapter(); + + /** + * @brief Destructor. + */ + virtual ~BluetoothTransportAdapter(); + protected: + + /** + * @brief Return type of device. + */ + virtual DeviceType GetDeviceType() const; + + /** + * @brief Store adapter state in last state singleton + */ + virtual void Store() const; + + /** + * @brief Restore adapter state from last state singleton + * + * @return True on success false otherwise + */ + virtual bool Restore(); +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_BLUETOOTH_ADAPTER diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/common.h b/SDL_Core/src/components/transport_manager/include/transport_manager/common.h new file mode 100644 index 000000000..62ee1ff12 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/common.h @@ -0,0 +1,86 @@ +/** + * \file common.h + * \brief Common definitions. + * 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_COMMON_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_COMMON_H_ + +#include <set> +#include <vector> +#include <string> + +#include "protocol_handler/raw_message.h" +#include "utils/shared_ptr.h" + +/** + * @brief - transport_manager namespace + */ +namespace transport_manager { + +/** + * @brief Shared pointer for RawMessage class + **/ + +/** + * @brief Type definition for variable that hold shared pointer to raw message. + */ +typedef utils::SharedPtr<protocol_handler::RawMessage> RawMessageSptr; + +/** + * @brief Type definition for variable that hold handle of device. + */ +typedef unsigned int DeviceHandle; + +/** + * @brief Type definition for variable that hold connection unique identifier. + */ +typedef unsigned int ConnectionUID; + +/** + * @brief Type definition for variable that hold device unique identifier. + */ +typedef std::string DeviceUID; + +/** + * @brief Type definition for variable that hold handle of application. + */ +typedef int ApplicationHandle; + +/** + * @brief Type definition for vector that contain ApplicationHandle variables. + */ +typedef std::vector<ApplicationHandle> ApplicationList; + +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_COMMON.H diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/error.h b/SDL_Core/src/components/transport_manager/include/transport_manager/error.h new file mode 100644 index 000000000..43bcd1630 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/error.h @@ -0,0 +1,147 @@ +/** + * \file error.h + * \brief Error classes header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_ERROR_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_ERROR_H_ + +#include <string> + +namespace transport_manager { + +/** + * @brief Class that hold information about error. + */ +class BaseError { + public: + /** + * @brief Constructor. + */ + BaseError() {} + + /** + * @brief Constructor. + * + * @param desc Description of error. + */ + explicit BaseError(const std::string& desc) : description_(desc) {} + + /** + * @brief Constructor. + * + * @param other Error class. + */ + BaseError(const BaseError& other) : description_(other.description_) {} + + /** + * @brief Destructor. + */ + virtual ~BaseError() {} + + /** + * @brief Return description of error. + * + * @return string with description of error. + */ + virtual const std::string& text() const { + return description_; + } + + private: + std::string description_; +}; + +/** + * @brief Error that originates during device search. + */ +class SearchDeviceError : public BaseError { +}; + +/** + * @brief Error that originates during connection. + */ +class ConnectError : public BaseError { +}; + +/** + * @brief Error that originates during disconnection. + */ +class DisconnectError : public BaseError { +}; + +/** + * @brief Error that originates during device disconnection. + */ +class DisconnectDeviceError : public BaseError { +}; + +/** + * @brief Error that originates during data receive.. + */ +class DataReceiveError : public BaseError { +}; + +/** + * @brief Error that originates during data send. + */ +class DataSendError : public BaseError { + public: + /** + * @brief Constructor. + */ + DataSendError() : BaseError() {} + + /** + * @brief Constructor. + * + * @param Error description. + */ + explicit DataSendError(const std::string &desc) : BaseError(desc) {} +}; + +/** + * @brief Error that originates during data sending timeout. + */ +class DataSendTimeoutError : public DataSendError { +}; + + +/** + * @brief Error that originates during communication. + */ +class CommunicationError : public BaseError { +}; + +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_ERROR_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/info.h b/SDL_Core/src/components/transport_manager/include/transport_manager/info.h new file mode 100644 index 000000000..21b8d9952 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/info.h @@ -0,0 +1,161 @@ +/* + * \file info.h + * \brief Information classes header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_INFO_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_INFO_H_ + +#include <string> +#include "transport_manager/common.h" + +/** + * @brief transport_manager name space + */ +namespace transport_manager { + +/** + * @brief Base information class. + */ +class Info { + protected: + + /** + * @brief Variable that hold name. + */ + std::string name_; + + public: + + /** + * @brief Constructor. + */ + Info() {} + + /** + * @brief Constructor + * + * @param name Info class object name. + */ + explicit Info(std::string name) + : name_(name) { + } + + /** + * @brief Return string with name. + */ + std::string name() const{ + return name_; + } + + /** + * @brief Destructor. + */ + virtual ~Info() {} +}; + +/** + * @brief Hold information about device. + */ +class DeviceInfo : public Info { + protected: + + /** + * @brief Variable that hold MAC adress of device. + */ + std::string mac_address_; + + /** + * @brief Variable that hold handle of device. + */ + DeviceHandle device_handle_; + + public: + + /** + * @brief Constructor. + * + * @param device_handle Handle of device. + * @param mac_adress MAC adress of device. + * @param name Name of device. + */ + DeviceInfo(DeviceHandle device_handle, std::string mac_address, + std::string name) + : Info(name), + mac_address_(mac_address), + device_handle_(device_handle) { + } + + /** + * @brief Return mac_adress. + */ + std::string mac_address() const { + return mac_address_; + } + + /** + * @brief Return device_handle field. + */ + DeviceHandle device_handle() const { + return device_handle_; + } + + /** + * @brief Overloaded operator "==". + */ + friend bool operator ==(const DeviceInfo &first, const DeviceInfo &second); + + // Needed for std::set container + bool operator <(const DeviceInfo& than) const { + return device_handle_ < than.device_handle_; + } +}; + +/** + * @brief Assign fields of one DeviceInfo class to another. + */ +inline bool operator ==(const DeviceInfo &first, const DeviceInfo &second) { + return first.name_ == second.name_ + && first.mac_address_ == second.mac_address_ + && first.device_handle_ == second.device_handle_; +} + +/** + * @brief AdapterInfo class + */ +class AdapterInfo : public Info { + +}; +} + +#endif /* SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_INFO_H_ */ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/dnssd_service_browser.h b/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/dnssd_service_browser.h new file mode 100644 index 000000000..9e8523f1b --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/dnssd_service_browser.h @@ -0,0 +1,133 @@ +/** + * \file dnssd_service_browser.h + * \brief DnssdServiceBrowser class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_DNSSD_SERVICE_BROWSER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_DNSSD_SERVICE_BROWSER_H_ + +#include <string> +#include <vector> +#include <pthread.h> +#include <avahi-client/client.h> +#include <avahi-client/lookup.h> +#include <avahi-common/error.h> +#include <avahi-common/thread-watch.h> + +#include "transport_manager/transport_adapter/device_scanner.h" +#include "transport_manager/transport_adapter/transport_adapter.h" + +namespace transport_manager { + +namespace transport_adapter { + +struct DnssdServiceRecord { + AvahiIfIndex interface; + AvahiProtocol protocol; + std::string domain_name; + std::string host_name; + std::string name; + std::string type; + uint16_t port; + uint32_t addr; +}; + +#define DNSSD_DEFAULT_SERVICE_TYPE "_ford-sdlapp._tcp" + +/** + * @brief DNS service discovery class. + */ +class DnssdServiceBrowser : public DeviceScanner { + public: + /** + * @brief Constructor. + * + * @param controller Pointer to the device adapter controller. + */ + DnssdServiceBrowser(class TransportAdapterController* controller); + virtual ~DnssdServiceBrowser(); + protected: + virtual TransportAdapter::Error Init(); + virtual TransportAdapter::Error Scan(); + virtual void Terminate(); + virtual bool IsInitialised() const; + private: + TransportAdapter::Error CreateAvahiClientAndBrowser(); + void AddService(AvahiIfIndex interface, AvahiProtocol protocol, + const char *name, const char *type, const char *domain); + void RemoveService(AvahiIfIndex interface, AvahiProtocol protocol, + const char *name, const char *type, const char *domain); + + void OnClientConnected(); + void OnClientFailure(); + + DeviceVector PrepareDeviceVector() const; + + void ServiceResolved(const DnssdServiceRecord& service_record); + void ServiceResolveFailed(const DnssdServiceRecord& service_record); + + friend void AvahiClientCallback(AvahiClient *avahi_client, + AvahiClientState avahi_client_state, + void* data); + friend void AvahiServiceBrowserCallback( + AvahiServiceBrowser *avahi_service_browser, AvahiIfIndex interface, + AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, + const char *type, const char *domain, AvahiLookupResultFlags flags, + void* data); + + friend void AvahiServiceResolverCallback( + AvahiServiceResolver* avahi_service_resolver, AvahiIfIndex interface, + AvahiProtocol protocol, AvahiResolverEvent event, const char* name, + const char* type, const char* domain, const char* host_name, + const AvahiAddress* avahi_address, uint16_t port, AvahiStringList* txt, + AvahiLookupResultFlags flags, void *data); + + TransportAdapterController* controller_; + + AvahiServiceBrowser* avahi_service_browser_; + AvahiThreadedPoll* avahi_threaded_poll_; + AvahiClient* avahi_client_; + + typedef std::vector<DnssdServiceRecord> ServiceRecords; + ServiceRecords service_records_; + + pthread_mutex_t mutex_; + + bool initialised_; +} +; + +} // namespace +} // namespace + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_DNSSD_SERVICE_BROWSER diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_client_listener.h b/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_client_listener.h new file mode 100644 index 000000000..34269117e --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_client_listener.h @@ -0,0 +1,115 @@ +/** + * \file tcp_client_listener.h + * \brief TcpClientListener class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_TCP_CLIENT_LISTENER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_TCP_CLIENT_LISTENER_H_ + +#include "transport_manager/transport_adapter/client_connection_listener.h" + +namespace transport_manager { +namespace transport_adapter { + +class TransportAdapterController; + +/** + * @brief Listener of device adapter that use TCP transport. + */ +class TcpClientListener : public ClientConnectionListener { + public: + + /** + * @breaf Constructor. + * + * @param controller Pointer to the device adapter controller. + * @param port Port No. + */ + TcpClientListener(TransportAdapterController* controller, const uint16_t port); + + /** + * @brief Start TCP client listener thread. + */ + void Thread(); + protected: + + /** + * @brief Destructor. + */ + virtual ~TcpClientListener(); + + /** + * @brief Run TCP client listener. + * + * @return Error information about possible reason of starting TCP listener listener failure. + */ + virtual TransportAdapter::Error Init(); + + /** + * @brief Stop TCP client listener. + */ + virtual void Terminate(); + + /** + * @brief Check initialization. + * + * @return True if initialized. + * @return False if not initialized. + */ + virtual bool IsInitialised() const; + + /** + * @brief + * + * @return Error information about possible reason of failure. + */ + virtual TransportAdapter::Error StartListening(); + + /** + * @brief Terminate TCP client listener thread. + */ + virtual TransportAdapter::Error StopListening(); + private: + const uint16_t port_; + TransportAdapterController* controller_; + pthread_t thread_; + int socket_; + bool thread_started_; + bool shutdown_requested_; + bool thread_stop_requested_; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif /* TCP_CLIENT_LISTENER_H_ */ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_connection_factory.h b/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_connection_factory.h new file mode 100644 index 000000000..ac2ea74ac --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_connection_factory.h @@ -0,0 +1,99 @@ +/** + * \file tcp_connection_factory.h + * \brief TcpConnectionFactory class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_TCP_CONNECTION_FACTORY_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_TCP_CONNECTION_FACTORY_H_ + +#include "transport_manager/transport_adapter/server_connection_factory.h" +#include "transport_manager/transport_adapter/transport_adapter_controller.h" + +namespace transport_manager { +namespace transport_adapter { + +/** + * @brief Create connections. + */ +class TcpConnectionFactory : public ServerConnectionFactory { + public: + + /** + * @brief Constructor. + * + * @param controller Pointer to the device adapter controller. + */ + TcpConnectionFactory(TransportAdapterController* controller); + protected: + + /** + * @brief Start TCP connection factory. + */ + virtual TransportAdapter::Error Init(); + + /** + * @brief Constructor. + * + * @param device_uid device unique identifier. + * @param app_handle Handle of application. + * + * @return Error information about possible reason of failure. + */ + virtual TransportAdapter::Error CreateConnection(const DeviceUID& device_uid, + const ApplicationHandle& app_handle); + + /** + * @brief + */ + virtual void Terminate(); + + /** + * @brief Check for initialization. + * + * @return true - initialized. + * false - not initialized. + */ + virtual bool IsInitialised() const; + + /** + * @brief Destructor. + */ + virtual ~TcpConnectionFactory(); + private: + TransportAdapterController* controller_; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_CONNECTION_FACTORY_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_device.h b/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_device.h new file mode 100644 index 000000000..08821ab3c --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_device.h @@ -0,0 +1,148 @@ +/** + * \file tcp_device.h + * \brief TcpDevice class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_TCP_DEVICE_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_TCP_DEVICE_H_ + +#include "transport_manager/transport_adapter/device.h" + +#include <map> +#include <memory.h> +#include <signal.h> +#include <errno.h> +#include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/types.h> +#include <sys/socket.h> + +namespace transport_manager { +namespace transport_adapter { + +/** + * @brief Information about device that use TCP transport. + */ +class TcpDevice : public Device { + public: + /** + * @brief Constructor. + * + * @param in_addr Address. + * @param name Device Name. + **/ + TcpDevice(const in_addr_t& in_addr, const std::string& name); + + virtual ~TcpDevice(); + + /** + * @brief Compare devices. + * + * @param other Device to compare with. + * + * @return True if devices are equal, false otherwise. + **/ + virtual bool IsSameAs(const Device* other) const; + + /** + * @brief Update list of applications available on device. + * + * @return Container with list of applications. + */ + virtual ApplicationList GetApplicationList() const; + + /** + * @brief Add an application to the container of applications. + * + * @param socket Value of socket. + */ + ApplicationHandle AddIncomingApplication(int socket_fd); + + /** + * @brief Add application that was discovered before. + * + * @param Port No. + */ + ApplicationHandle AddDiscoveredApplication(int port); + + /** + * @brief Remove application from the container of applications. + * + * @param app_handle Handle of application. + */ + void RemoveApplication(const ApplicationHandle app_handle); + + /** + * @brief Return application's socket value. + * + * @param app_handle Handle of application. + * + * @return Application's socket value. + */ + int GetApplicationSocket(const ApplicationHandle app_handle) const; + + /** + * @brief Return application's port No. + * + * @param app_handle Handle of application. + * + * @return Application's port No. + */ + int GetApplicationPort(const ApplicationHandle app_handle) const; + + /** + * @brief Return address. + * + * @return Address. + */ + in_addr_t in_addr() const { + return in_addr_; + } + + private: + struct Application { + bool incoming; + int socket; + uint16_t port; + }; + std::map<ApplicationHandle, Application> applications_; + mutable pthread_mutex_t applications_mutex_; + const in_addr_t in_addr_; + const std::string name_; + ApplicationHandle last_handle_; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif /* TCP_DEVICE_H_ */ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_socket_connection.h b/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_socket_connection.h new file mode 100644 index 000000000..592c79109 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_socket_connection.h @@ -0,0 +1,112 @@ +/** + * \file tcp_socket_connection.h + * \brief TcpSocketConnection class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_TCP_SOCKET_CONNECTION_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_TCP_SOCKET_CONNECTION_H_ + +#include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/types.h> +#include <sys/socket.h> + +#include "transport_manager/transport_adapter/threaded_socket_connection.h" + +namespace transport_manager { +namespace transport_adapter { + +class TransportAdapterController; + +/** + * @brief Class responsible for communication over TCP sockets. + */ +class TcpSocketConnection : public ThreadedSocketConnection { + public: + + /** + * @brief Constructor. + * + * @param device_uid Device unique identifier. + * @param app_handle Handle of application. + * @param controller Pointer to the TCP device adapter controller. + */ + TcpSocketConnection(const DeviceUID& device_uid, + const ApplicationHandle& app_handle, + TransportAdapterController* controller); + + /** + * @brief Destructor. + */ + virtual ~TcpSocketConnection(); + protected: + + /** + * @brief + */ + virtual bool Establish(ConnectError** error); +}; + +/** + * @brief Class responsible for communication over sockets that originated by server. + */ +class TcpServerOiginatedSocketConnection : public ThreadedSocketConnection { + public: + + /** + * @brief Constructor. + * + * @param device_uid Device unique identifier. + * @param app_handle Handle of application. + * @param controller Pointer to the device adapter controller. + */ + TcpServerOiginatedSocketConnection(const DeviceUID& device_uid, + const ApplicationHandle& app_handle, + TransportAdapterController* controller); + + /** + * @brief Destructor. + */ + virtual ~TcpServerOiginatedSocketConnection(); + protected: + + /** + * @brief + */ + virtual bool Establish(ConnectError** error); +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_SOCKET_CONNECTION_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_transport_adapter.h b/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_transport_adapter.h new file mode 100644 index 000000000..2d2f2a026 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/tcp/tcp_transport_adapter.h @@ -0,0 +1,88 @@ +/** + * \file tcp_transport_adapter.h + * \brief TcpTransportAdapter class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_TCP_ADAPTER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_TCP_ADAPTER_H_ + +#include "transport_manager/transport_adapter/transport_adapter_impl.h" + +namespace transport_manager { +namespace transport_adapter { + +/** + * @brief Transport adapter that use TCP transport. + */ +class TcpTransportAdapter : public TransportAdapterImpl { + public: + /** + * @brief Constructor. + */ + TcpTransportAdapter(); + + /** + * @brief Destructor. + */ + virtual ~TcpTransportAdapter(); + + /** + * @brief Default port. + */ + static const uint16_t default_port = 12345; + protected: + + /** + * @brief Return type of device. + * + * @return String with device type. + */ + virtual DeviceType GetDeviceType() const; + + /** + * @brief Store adapter state in last state singleton + */ + virtual void Store() const; + + /** + * @brief Restore adapter state from last state singleton + * + * @return True on success false otherwise + */ + virtual bool Restore(); +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_ADAPTER diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/client_connection_listener.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/client_connection_listener.h new file mode 100644 index 000000000..41658fb45 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/client_connection_listener.h @@ -0,0 +1,91 @@ +/** + * \file client_connection_listener.h + * \brief ClientConnectionListener class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_CLIENT_CONNECTION_LISTENER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_CLIENT_CONNECTION_LISTENER_H_ + +#include "transport_manager/transport_adapter/transport_adapter.h" + + +namespace transport_manager { + + +namespace transport_adapter { + +/** + * @brief Abstract class for listener of client connection. + */ +class ClientConnectionListener { + public: + /** + * @brief Run client connection listener. + * + * @return Error information about possible reason of starting client listener failure. + */ + virtual TransportAdapter::Error Init() = 0; + + /** + * @brief Stop client connection listener. + */ + virtual void Terminate() = 0; + + /** + * @brief Check initialization. + * + * @return True if initialized. + * @return False if not initialized. + */ + virtual bool IsInitialised() const = 0; + + /** + * @brief Start to listen for connection from client. + */ + virtual TransportAdapter::Error StartListening() = 0; + + /** + * @brief Stop to listen for connection from client. + */ + virtual TransportAdapter::Error StopListening() = 0; + + /** + * @brief Destructor. + */ + virtual ~ClientConnectionListener() { + } +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif /* CLIENT_CONNECTION_LISTENER_H_ */ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/connection.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/connection.h new file mode 100644 index 000000000..3e82f7fd2 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/connection.h @@ -0,0 +1,84 @@ +/** + * \file connection.h + * \brief Connection class header. + * 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_CONNECTION_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_CONNECTION_H_ + +#include "utils/shared_ptr.h" +#include "transport_manager/transport_adapter/transport_adapter.h" + +using utils::SharedPtr; + +namespace transport_manager { + +namespace transport_adapter { + +/** + * @brief Application connection. + **/ +class Connection { + public: + /** + * @brief Constructor. + */ + Connection() {} + /** + * @brief Destructor. + **/ + virtual ~Connection() {} + + /** + * @brief Send data frame. + * + * @param Message Smart pointer to the raw message. + * + * @return Error Information about possible reason of sending data failure. + */ + virtual TransportAdapter::Error SendData(RawMessageSptr message) = 0; + + /** + * @brief Disconnect the current connection. + */ + virtual TransportAdapter::Error Disconnect() = 0; +}; + +/** + * @typedef Type definition of smart pointer to the Connection class. + */ +typedef utils::SharedPtr<Connection> ConnectionSptr; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif /* CONNECTION_H_ */ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/device.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/device.h new file mode 100644 index 000000000..8330a8208 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/device.h @@ -0,0 +1,128 @@ +/** + * \file device.h + * \brief Device class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_DEVICE_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_DEVICE_H_ + +#include <string> + +#include "transport_manager/common.h" + +namespace transport_manager { +namespace transport_adapter { + +/** + * @brief Internal class describing device. + **/ +class Device { + public: + /** + * @brief Constructor. + * + * @param name User-friendly device name. + * @param unique_device_id device unique identifier. + **/ + Device(const std::string& name, const DeviceUID& unique_device_id) + : name_(name), + unique_device_id_(unique_device_id), + keep_on_disconnect_(false) {} + /** + * @brief Destructor. + **/ + virtual ~Device() {} + + /** + * @brief Compare devices. + * + * This method checks whether two SDevice structures + * refer to the same device. + * + * @param other_Ddvice Device to compare with. + * + * @return true if devices are equal, false otherwise. + **/ + virtual bool IsSameAs(const Device* other_device) const = 0; + + virtual ApplicationList GetApplicationList() const = 0; + + const DeviceUID& unique_device_id() const { + return unique_device_id_; + } + + /** + * @brief Return name of device. + */ + const std::string& name() const { + return name_; + } + + /** + * @brief Get @link keep_on_disconnect_ @endlink value + */ + bool keep_on_disconnect() const { + return keep_on_disconnect_; + } + + /** + * @brief Set @link keep_on_disconnect_ @endlink value + * @param keep_on_disconnect new value + */ + void set_keep_on_disconnect(bool keep_on_disconnect) { + keep_on_disconnect_ = keep_on_disconnect; + } + + private: + /** + * @brief Device user-friendly name. + **/ + std::string name_; + + /** + * @brief Unique device identifier across all devices. + **/ + DeviceUID unique_device_id_; + + /** + * @brief If true, device will remain in list even if all its connections finished. + **/ + bool keep_on_disconnect_; +}; + +typedef utils::SharedPtr<Device> DeviceSptr; +typedef std::vector<DeviceSptr> DeviceVector; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif /* DEVICE_H_ */ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/device_scanner.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/device_scanner.h new file mode 100644 index 000000000..5b09a726e --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/device_scanner.h @@ -0,0 +1,86 @@ +/** + * \file device_scanner.h + * \brief DeviceScanner class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_DEVICE_SCANNER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_DEVICE_SCANNER_H_ + +#include "transport_manager/transport_adapter/device.h" +#include "transport_manager/transport_adapter/transport_adapter.h" + +namespace transport_manager { +namespace transport_adapter { + +/** + * @brief Abstract class for device scanner. + */ +class DeviceScanner { + public: + /** + * @brief Start device scanner. + * + * @return Error information about reason of initialization failure. + */ + virtual TransportAdapter::Error Init() = 0; + + /** + * @brief + * + * @return Error information about reason of Scan failure. + */ + virtual TransportAdapter::Error Scan() = 0; + + /** + * @brief Stop device scanner. + */ + virtual void Terminate() = 0; + + /** + * @brief Check device scanner for initialization. + * + * @return true - initialized. + * false - not initialized. + */ + virtual bool IsInitialised() const = 0; + + /** + * @brief Destructor. + */ + virtual ~DeviceScanner() { + } +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif /* DEVICE_SCANNER_H_ */ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/server_connection_factory.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/server_connection_factory.h new file mode 100644 index 000000000..d944594d8 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/server_connection_factory.h @@ -0,0 +1,89 @@ +/** + * \file server_connection_factory.h + * \brief ServerConnectionFactory class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_SERVER_CONNECTION_FACTORY_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_SERVER_CONNECTION_FACTORY_H_ + +#include "transport_manager/transport_adapter/transport_adapter.h" + +namespace transport_manager { +namespace transport_adapter { + +/** + * @brief Implement transport dependent connection that was originated by the user. + */ +class ServerConnectionFactory { + public: + /** + * @brief Start server connection factory. + * + * @return Information about possible reason of error during initialization. + */ + virtual TransportAdapter::Error Init() = 0; + + /** + * @brief + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * + * @return Information about posible reason of error. + */ + virtual TransportAdapter::Error CreateConnection( + const DeviceUID& device_handle, const ApplicationHandle& app_handle) = 0; + + /** + * @brief + */ + virtual void Terminate() = 0; + + /** + * @brief Check device scanner for initialization. + * + * @return true - initialized. + * false - not initialized. + */ + virtual bool IsInitialised() const = 0; + + /** + * @brief Destructor. + */ + virtual ~ServerConnectionFactory() { + } +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif /* SERVER_CONNECTION_FACTORY_H_ */ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/threaded_socket_connection.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/threaded_socket_connection.h new file mode 100644 index 000000000..1778e5ef1 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/threaded_socket_connection.h @@ -0,0 +1,165 @@ +/** + * \file threaded_socket_connection.h + * \brief Header for classes responsible for communication over sockets. + * 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_THREADED_SOCKET_CONNECTION_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_THREADED_SOCKET_CONNECTION_H_ + +#include <poll.h> + +#include <queue> + +#include "utils/logger.h" +#include "transport_manager/transport_adapter/connection.h" + +using ::transport_manager::transport_adapter::Connection; + +namespace transport_manager { +namespace transport_adapter { + +class TransportAdapterController; + +/** + * @brief Class responsible for communication over sockets. + */ +class ThreadedSocketConnection : public Connection { + public: + + /** + * @brief Send data frame. + * + * @param message Smart pointer to the raw message. + * + * @return Error Information about possible reason of sending data failure. + */ + TransportAdapter::Error SendData(RawMessageSptr message); + + /** + * @brief Disconnect the current connection. + * + * @return Error Information about possible reason of Disconnect failure. + */ + TransportAdapter::Error Disconnect(); + + /** + * @brief Start thread creation. + * + * @return Information about possible reason of thread creation error. + */ + TransportAdapter::Error Start(); + + /** + * @brief Set variable that hold socket No. + */ + void set_socket(int socket) { + socket_ = socket; + } + protected: + + /** + * @brief Constructor. + * + * @param device_uid Device unique identifier. + * @param app_handle Handle of application. + * @param controller Pointer to the device adapter controller. + */ + ThreadedSocketConnection(const DeviceUID& device_uid, + const ApplicationHandle& app_handle, + TransportAdapterController* controller); + + /** + * @brief Destructor. + */ + virtual ~ThreadedSocketConnection(); + + + virtual bool Establish(ConnectError** error) = 0; + + /** + * @brief Return pointer to the device adapter controller. + */ + TransportAdapterController* controller() { + return controller_; + } + + /** + * @brief Return device unique identifier. + */ + DeviceUID device_handle() const { + return device_uid_; + } + + /** + * @brief Return handle of application. + */ + ApplicationHandle application_handle() const { + return app_handle_; + } + + private: + + int read_fd_; + int write_fd_; + void Thread(); + void Transmit(); + void Finalize(); + TransportAdapter::Error Notify() const; + bool Receive(); + bool Send(); + void Abort(); + + friend void* StartThreadedSocketConnection(void*); + + TransportAdapterController* controller_; + /** + * @brief Frames that must be sent to remote device. + **/ + typedef std::queue<RawMessageSptr> FrameQueue; + FrameQueue frames_to_send_; + mutable pthread_mutex_t frames_to_send_mutex_; + + pthread_t thread_; + + int socket_; + bool terminate_flag_; + bool unexpected_disconnect_; + const DeviceUID device_uid_; + const ApplicationHandle app_handle_; +}; + +extern log4cxx::LoggerPtr logger_; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif //SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_transport_adapter_SOCKET_COMMUNICATION diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter.h new file mode 100644 index 000000000..a65e64b8d --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter.h @@ -0,0 +1,251 @@ +/** + * \file transport_adapter.h + * \brief TransportAdapter class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_TRANSPORT_ADAPTER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_TRANSPORT_ADAPTER_H_ + +#include <string> +#include <vector> +#include <list> +#include "utils/shared_ptr.h" +#include "transport_manager/common.h" +#include "transport_manager/error.h" + +namespace transport_manager { +namespace transport_adapter { + +class TransportAdapterListener; +typedef std::string DeviceType; +typedef int ApplicationHandle; +typedef std::vector<ApplicationHandle> ApplicationList; +/** + * @brief Type definition of container(vector) that holds device unique + * identifiers. + */ +typedef std::vector<DeviceUID> DeviceList; +/** + * @brief Type definition for container(list) that holds pointers to device + * adapter listeners + */ +typedef std::list<TransportAdapterListener*> TransportAdapterListenerList; + +class TransportAdapter { + public: + /** + * @enum Available types of errors. + */ + enum Error { + OK, + FAIL, + NOT_SUPPORTED, + ALREADY_EXISTS, + BAD_STATE, + BAD_PARAM + }; + + public: + /** + * @brief Destructor. + */ + virtual ~TransportAdapter() {} + + /** + * @brief + */ + virtual DeviceType GetDeviceType() const = 0; + + /* TODO + virtual Error LoadState(TransportAdapterState* state) = 0; + virtual void SaveState(TransportAdapterState* state) = 0; + */ + + /** + * @brief Check initialization. + * + * @return true if initialized. + * @return false if not initialized. + */ + virtual bool IsInitialised() const = 0; + + /** + * @brief Run device adapter. + * + * Called from transport manager to start device adapter. + * + * @return Error information about possible reason of starting client listener + *failure. + **/ + virtual Error Init() = 0; + + /** + * @brief Add listener to the container(list) of device adapter listeners. + * + * @param listener Pointer to the device adapter listener. + */ + virtual void AddListener(TransportAdapterListener* listener) = 0; + + /** + * @brief Notify that device scanner is available. + * + * @return true - available, false - not available. + */ + virtual bool IsSearchDevicesSupported() const = 0; + + /** + * @brief Start scanning for new devices. + * + * List of new devices will be supplied in onDeviceListUpdated callback. + * + * @return Error information about possible reason of searching devices + *failure. + **/ + virtual Error SearchDevices() = 0; + + /** + * @brief Notify that server connection factory is available. + * + * @return true - available, false - not available. + */ + virtual bool IsServerOriginatedConnectSupported() const = 0; + + /** + * @brief Connect to the specified application discovered on device. + * + * @param device_handle Handle of device to connect to. + * @param app_handle Handle of application to connect to. + * + * @return Error information about possible reason of connection to the device + *failure. + **/ + virtual Error Connect(const DeviceUID& device_handle, + const ApplicationHandle& app_handle) = 0; + + /** + * @brief Connect to all applications discovered on device + * @param device_handle Handle of device + * @return Error information about connecting applications on device + */ + virtual Error ConnectDevice(const DeviceUID& device_handle) = 0; + + /** + * @brief Notify that listener of client connection is available. + * + * @return true - available, false - not available. + */ + virtual bool IsClientOriginatedConnectSupported() const = 0; + + /** + * @brief Start client listener. + * + * @return Error information about possible reason of starting client listener + *failure. + */ + virtual Error StartClientListening() = 0; + + /** + * @brief Stop client listener. + * + * @return Error information about possible reason of stopping client listener + *failure. + */ + virtual Error StopClientListening() = 0; + + /** + * @brief Disconnect from specified session. + * + * @param device_handle Handle of device to Disconnect from. + * @param app_handle Handle of application. + * + * @return Error information about possible reason of disconnection from the + *device failure. + **/ + virtual Error Disconnect(const DeviceUID& device_handle, + const ApplicationHandle& app_handle) = 0; + + /** + * @brief Disconnect from all sessions on specified device. + * + * @param device_handle Device handle to Disconnect. + * + * @return Error information about possible reason of disconnecting from + *device failure + **/ + virtual Error DisconnectDevice(const DeviceUID& device_handle) = 0; + + /** + * @brief Send frame. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * @param data Smart pointer to the raw message. + * + * @return Error information about possible reason of sending data failure. + **/ + virtual Error SendData(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const RawMessageSptr data) = 0; + + /** + * @brief Create container(vector) of device unique identifiers. + * + * @return container(vector) of device unique identifiers. + */ + virtual DeviceList GetDeviceList() const = 0; + + /** + * @brief Get container(vector) of application unique identifiers that + *available at specified device. + * + * @param device_handle Device unique identifier. + * + * @return Container(vector) that holds application unique identifiers. + */ + virtual ApplicationList GetApplicationList(const DeviceUID& device_handle) + const = 0; + + /** + * @brief Return name of device. + * + * @param device_id device unique identifier. + * + * @return string. + */ + virtual std::string DeviceName(const DeviceUID& device_id) const = 0; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_transport_adapter_transport_adapter diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_controller.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_controller.h new file mode 100644 index 000000000..44565c84d --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_controller.h @@ -0,0 +1,192 @@ +/** + * \file transport_adapter_controller.h + * \brief TransportAdapterController class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_TRANSPORT_ADAPTER_CONTROLLER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_TRANSPORT_ADAPTER_CONTROLLER_H_ + +#include "transport_manager/transport_adapter/device.h" +#include "transport_manager/transport_adapter/connection.h" + +namespace transport_manager { + +namespace transport_adapter { + + +class TransportAdapterController { + public: + + virtual ~TransportAdapterController() { + } + + /** + * @brief Add device to the container(map), if container doesn't hold it yet. + * + * @param device Smart pointer to the device. + * + * @return Smart pointer to the device. + */ + virtual DeviceSptr AddDevice(DeviceSptr device) = 0; + + /** + * @brief Search for device in container of devices, if it is not there - adds it. + * + * @param devices Container(vector) of smart pointers to devices. + */ + virtual void SearchDeviceDone(const DeviceVector& devices) = 0; + + /** + * @brief Launch OnSearchDeviceFailed event in device adapter listener. + * + * @param error Error class that contains details of this error situation. + */ + virtual void SearchDeviceFailed(const SearchDeviceError& error) = 0; + + /** + * @brief Find device in the internal container(map). + * + * @param device_handle Device unique identifier. + * + * @return Smart pointer to the device. + */ + virtual DeviceSptr FindDevice(const DeviceUID& device_handle) const = 0; + + /** + * @brief Create connection and fill its parameters. + * + * @param connection Smart pointer to the connection. + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + */ + virtual void ConnectionCreated(ConnectionSptr connection, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle) = 0; + + /** + * @brief Make state of specified connection - ESTABLISHED and launch OnConnectDone event in device adapter listener. + * + * @param devcie_handle Device unique identifier. + * @param app_handle Handle of application. + */ + virtual void ConnectDone(const DeviceUID& device_handle, + const ApplicationHandle& app_handle) = 0; + + /** + * @brief Delete connection from the container of connections and launch OnConnectFailed event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + */ + virtual void ConnectFailed(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const ConnectError& error) = 0; + + /** + * @brief Make state of specified connection - FINILIZING. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + */ + virtual void ConnectionFinished(const DeviceUID& device_handle, + const ApplicationHandle& app_handle) = 0; + + /** + * @brief Set specified connection state to FINILIZING and launch OnUnexpectedDisconnect event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * @param error Error class that contains details of this error situation. + */ + virtual void ConnectionAborted(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const CommunicationError& error) = 0; + + /** + * @brief Delete specified connection from the container(map) of connections and launch event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + */ + virtual void DisconnectDone(const DeviceUID& device_handle, + const ApplicationHandle& app_handle) = 0; + + /** + * @brief Launch OnDataReceiveDone event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * @param message Smart pointer to the raw message. + */ + virtual void DataReceiveDone(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + RawMessageSptr message) = 0; + + /** + * @brief Launch OnDataReceiveFailed event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * @param error Class that contains details of this error situation. + */ + virtual void DataReceiveFailed(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const DataReceiveError&) = 0; + + /** + * @brief Launch OnDataSendDone event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * @param message Smart pointer to raw message. + */ + virtual void DataSendDone(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + RawMessageSptr message) = 0; + + /** + * @brief Launch OnDataSendFailed event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * @param message Smart pointer to raw message. + * @param error Class that contains details of this error situation. + */ + virtual void DataSendFailed(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + RawMessageSptr message, const DataSendError&) = 0; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif /* transport_adapter_CONTROLLER_H_ */ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_event.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_event.h new file mode 100644 index 000000000..05e3cbd48 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_event.h @@ -0,0 +1,163 @@ +/** + * \file transport_adapter_event.h + * \brief TransportAdapterEvent class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_TRANSPORT_ADAPTER_EVENT_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_TRANSPORT_ADAPTER_EVENT_H_ + +#include "transport_manager/common.h" +#include "transport_manager/transport_adapter/transport_adapter.h" + +namespace transport_manager { + +class TransportAdapterEvent { + public: + /** + * @brief Constructor. + * + * @param type Event type. + * @param transport_adapter Transport adapter + * @param device_handle Handle of device. + * @param application_id Handle of application. + * @param data Smart pointer to the raw message. + * @param error Error class that contains details of this error situation. + */ + TransportAdapterEvent(int type, + transport_adapter::TransportAdapter *transport_adapter, + const DeviceUID &device_handle, + const ApplicationHandle &application_id, + RawMessageSptr data, BaseError *error); + + /** + * @brief Destructor. + */ + ~TransportAdapterEvent(); + + /** + * @brief Set value that describe event type. + * + * @param type Type of the event. + */ + void set_event_type(int type); + + /** + * @brief Set connection unique identifier field. + * + * @param id Connection unique identifier. + */ + void set_connection_uid(ConnectionUID id); + + /** + * @brief Set device adapter field. + * + * @param transport_adapter Smart pointer to the device adapter. + */ + void set_transport_adapter( + transport_adapter::TransportAdapter *transport_adapter); + + /** + * @brief Set pointer to the data. + * + * @param message Smart pointer to the raw message. + */ + void set_data(RawMessageSptr message); + + /** + * @brief Set field that responsible for the pointer to error. + * + * @param error Error class that contains details of this error situation. + */ + void set_error(BaseError *error); + + /** + * @brief Set device handle field. + * + * @param device_handle Device unique identifier. + */ + void set_device_handle(const DeviceUID &device_handle); + + /** + * @brief Return device unique identifier value. + * + * @return Device unique identifier. + */ + const DeviceUID &device_uid() const; + + /** + * @brief Return value that describe event type. + * + * @return Value that describe event type. + */ + int event_type(void) const; + + /** + * @brief Return handle of application value. + * + * @return Handle of application. + */ + ApplicationHandle application_id(void) const; + + /** + * @brief Return smart pointer to the device adapter. + * + * @return Transport adapter. + */ + transport_adapter::TransportAdapter *transport_adapter(void) const; + + /** + * @brief Return smart pointer to the raw message. + * + * @return Smart pointer to the raw message. + */ + RawMessageSptr data(void) const; + + /** + * @brief Return pointer to the class that contain details of error. + * + * @return Pointer to the class that contain details of error. + */ + BaseError *event_error(void) const; + + private: + DeviceUID device_uid_; /**< Device unique identifier. */ + int event_type_; /**< Value that describe event type. */ + ApplicationHandle application_id_; /**< Handle of application. */ + transport_adapter::TransportAdapter *transport_adapter_; + RawMessageSptr event_data_; /**< Smart pointer to the raw message. */ + BaseError *event_error_; /** Pointer to the class that contain details of + error */ +}; + +} // namespace + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_transport_adapter_EVENT diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_impl.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_impl.h new file mode 100644 index 000000000..664a5c8d5 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_impl.h @@ -0,0 +1,498 @@ +/** + * \file transport_adapter_impl.h + * \brief TransportAdapterImpl class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_TRANSPORT_ADAPTER_IMPL_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_TRANSPORT_ADAPTER_IMPL_H_ + +#include <queue> +#include <set> +#include <memory> +#include <string> + +#include "utils/logger.h" + +#include "transport_manager/transport_adapter/transport_adapter.h" +#include "transport_manager/transport_adapter/transport_adapter_controller.h" +#include "transport_manager/transport_adapter/connection.h" + +namespace transport_manager { + +namespace transport_adapter { + +class TransportAdapterListener; +class DeviceScanner; +class ServerConnectionFactory; +class ClientConnectionListener; + +/* + * @brief Implementation of device adapter class. + **/ +class TransportAdapterImpl : public TransportAdapter, + public TransportAdapterController { + protected: + /** + * @brief Constructor. + * + * @param device_scanner Pointer to device scanner. + * @param server_connection_factory Pointer to the factory that create + *connections from server. + * @param client_connection_listener Pointer to the listener of client + *connection. + **/ + TransportAdapterImpl(DeviceScanner* device_scanner, + ServerConnectionFactory* server_connection_factory, + ClientConnectionListener* client_connection_listener); + + /** + * @brief Destructor. + **/ + virtual ~TransportAdapterImpl(); + + /** + * @brief Check initialization. + * + * @return true if initialized. + * @return false if not initialized. + */ + virtual bool IsInitialised() const; + + public: + /** + * @brief Initiate device adapter. + * + * Called from transport manager to start device adapter. + * + * @return Error information about possible reason of starting client listener + *failure. + **/ + virtual TransportAdapter::Error Init(); + + /** + * @brief Add listener to the container(list) of device adapter listeners. + * + * @param listener Pointer to the device adapter listener. + */ + virtual void AddListener(TransportAdapterListener* listener); + + /** + * @brief Start scanning for new devices. + * + * List of new devices will be supplied in onDeviceListUpdated callback. + * + * @return Error information about possible reason of search devices failure. + **/ + virtual TransportAdapter::Error SearchDevices(); + + /** + * @brief Connect to the specified application discovered on device. + * + * @param device_handle Device unique identifier to connect to. + * @param app_handle Handle of application to connect to. + * + * @return Error information about possible reason of connecting to device + *failure. + **/ + virtual TransportAdapter::Error Connect(const DeviceUID& device_handle, + const ApplicationHandle& app_handle); + + /** + * @brief Connect to all applications discovered on device + * @param device_handle Handle of device + * @return Error information about connecting applications on device + */ + virtual TransportAdapter::Error ConnectDevice(const DeviceUID& device_handle); + + /** + * @brief Disconnect from specified session. + * + * @param devcie_handle Device unique identifier. + * @param app_handle Handle of application. + * + * @return Error information about possible reason of disconnecting failure. + **/ + virtual TransportAdapter::Error Disconnect( + const DeviceUID& device_handle, const ApplicationHandle& app_handle); + + /** + * @brief Disconnect from all sessions on specified device. + * + * @param device_handle Device handle to Disconnect. + * + * @return Error information about possible reason of disconnecting from + *specified device failure. + **/ + virtual TransportAdapter::Error DisconnectDevice( + const DeviceUID& device_handle); + + /** + * @brief Send frame of data. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * @param data Smart pointer to the raw message. + * + * @return Error information about possible reason of sending data failure + **/ + virtual TransportAdapter::Error SendData(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const RawMessageSptr data); + + /** + * @brief Start client listener. + * + * @return Error information about possible reason of starting client listener + *failure. + */ + virtual TransportAdapter::Error StartClientListening(); + + /** + * @brief Stop client listener. + * + * @return Error information about possible reason of stopping client listener + *failure. + */ + virtual TransportAdapter::Error StopClientListening(); + + /** + * @brief Notify that device scanner is available. + * + * @return True - available, false - not available. + */ + virtual bool IsSearchDevicesSupported() const; + + /** + * @brief Notify that server connection factory is available. + * + * @return True - available, false - not available. + */ + virtual bool IsServerOriginatedConnectSupported() const; + + /** + * @brief Notify that listener of client connection is available. + * + * @return True - available, false - not available. + */ + virtual bool IsClientOriginatedConnectSupported() const; + + /** + * @brief Create container(vector) of device unique identifiers. + * + * @return Container(vector) of device unique identifiers. + */ + virtual DeviceList GetDeviceList() const; + + /** + * @brief Get container(vector) of application unique identifiers that + *available at specified device. + * + * @param device_handle Device unique identifier. + * + * @return Container(vector) that holds application unique identifiers. + */ + virtual ApplicationList GetApplicationList(const DeviceUID& device_handle) + const; + + /** + * @brief Find device in the internal container(map). + * + * @param device_handle Device unique identifier. + * + * @return Smart pointer to device. + */ + virtual DeviceSptr FindDevice(const DeviceUID& device_handle) const; + + /** + * @brief Search for device in container of devices, if it is not there - adds + *it. + * + * @param devices Container(vector) of smart pointers to devices. + */ + virtual void SearchDeviceDone(const DeviceVector& devices); + + /** + * @brief Launch OnSearchDeviceFailed event in device adapter listener. + * + * @param error Error class that contains details of this error situation. + */ + virtual void SearchDeviceFailed(const SearchDeviceError& error); + + /** + * @brief Add device to the container(map), if container doesn't hold it yet. + * + * @param device Smart pointer to the device. + * + * @return Smart pointer to the device. + */ + virtual DeviceSptr AddDevice(DeviceSptr device); + + /** + * @brief Create connection and fill its parameters. + * + * @param connection Smart pointer to the connection. + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + */ + virtual void ConnectionCreated(ConnectionSptr connection, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle); + + /** + * @brief Set state of specified connection - FINILIZING. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + */ + virtual void ConnectionFinished(const DeviceUID& device_handle, + const ApplicationHandle& app_handle); + + /** + * @brief Set specified connection state to FINILIZING and launch + *OnUnexpectedDisconnect event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * @param error Error class that contains details of this error situation. + */ + virtual void ConnectionAborted(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const CommunicationError& error); + + /** + * @brief Set state of specified connection - ESTABLISHED and launch + *OnConnectDone event in device adapter listener. + * + * @param devcie_handle Device unique identifier. + * @param app_handle Handle of application. + */ + virtual void ConnectDone(const DeviceUID& device_handle, + const ApplicationHandle& app_handle); + + /** + * @brief Delete connection from the container of connections and launch + *OnConnectFailed event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + */ + virtual void ConnectFailed(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const ConnectError& error); + + /** + * @brief Delete specified connection from the container(map) of connections + *and launch event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + */ + virtual void DisconnectDone(const DeviceUID& device_handle, + const ApplicationHandle& app_handle); + + /** + * @brief Launch OnDataReceiveDone event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * @param message Smart pointer to the raw message. + */ + virtual void DataReceiveDone(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + RawMessageSptr message); + + /** + * @brief Launch OnDataReceiveFailed event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * @param error Class that contains details of this error situation. + */ + virtual void DataReceiveFailed(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const DataReceiveError& error); + + /** + * @brief Launch OnDataSendDone event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * @param message Smart pointer to raw message. + */ + virtual void DataSendDone(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + RawMessageSptr message); + + /** + * @brief Launch OnDataSendFailed event in the device adapter listener. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * @param message Smart pointer to raw message. + * @param error Class that contains details of this error situation. + */ + virtual void DataSendFailed(const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + RawMessageSptr message, + const DataSendError& error); + + /** + * @brief Return name of device. + * + * @param device_id Device unique identifier. + * + * @return String with name. + */ + virtual std::string DeviceName(const DeviceUID& device_id) const; + + protected: + + /** + * @brief Store adapter state where applicable + */ + virtual void Store() const; + + /** + * @brief Restore adapter state where applicable + * + * @return True on success false otherwise (or vacuous true) + */ + virtual bool Restore(); + + /** + * @brief Returns true if \a device is to be connected automatically + */ + virtual bool ToBeAutoConnected(DeviceSptr device) const; + + private: + /** + * @brief Find connection that has state - ESTABLISHED. + * + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + * + * @return ConnectionSptr smart pointer to the connection. + */ + ConnectionSptr FindEstablishedConnection(const DeviceUID& device_handle, + const ApplicationHandle& app_handle); + /** + * @brief Connect to all applications discovered on device + * @param device Pointer to device + * @return Error information about connecting applications on device + */ + TransportAdapter::Error ConnectDevice(DeviceSptr device); + + /** + * @brief Listener for device adapter notifications. + **/ + TransportAdapterListenerList listeners_; + + /** + * @brief Flag variable that notify initialized device adapter or not. + */ + bool initialised_; + + /** + * @brief Type definition of container(map) that holds device unique + *identifier(key value) and smart pointer to the device(mapped value). + **/ + typedef std::map<DeviceUID, DeviceSptr> DeviceMap; + + /** + * @brief Structure that holds information about connection. + */ + struct ConnectionInfo { + ConnectionSptr connection; + DeviceUID device_id; + ApplicationHandle app_handle; + enum { + NEW, + ESTABLISHED, + FINALISING + } state; + }; + + /** + * @brief Type definition of container(map) that holds pair<device unique + * identifier, handle of application>(key value) and structure that holds + * information + * about connection(mapped value). + */ + typedef std::map<std::pair<DeviceUID, ApplicationHandle>, ConnectionInfo> + ConnectionMap; + + /** + * @brief Map of device handle to device. + * + * This map contains all currently available devices. + **/ + DeviceMap devices_; + + /** + * @brief Mutex restricting access to device map. + **/ + mutable pthread_mutex_t devices_mutex_; + + /** + * @brief Container(map) of connections. + **/ + ConnectionMap connections_; + + /** + * @brief Mutex restricting access to connections map. + **/ + mutable pthread_mutex_t connections_mutex_; + + protected: + /** + * @brief Pointer to the device scanner. + */ + DeviceScanner* device_scanner_; + + /** + * @brief Pointer to the factory if connections initiated from server. + */ + ServerConnectionFactory* server_connection_factory_; + + /** + * @brief Pointer to the factory of connections initiated from client. + */ + ClientConnectionListener* client_connection_listener_; +}; + +extern log4cxx::LoggerPtr logger_; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // #ifndef \ + // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_transport_adapter_IMPL_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_listener.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_listener.h new file mode 100644 index 000000000..186ed9967 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_listener.h @@ -0,0 +1,252 @@ +/** + * \file transport_adapter_listener.h + * \brief TransportAdapterListener class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_TRANSPORT_ADAPTER_LISTENER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_TRANSPORT_ADAPTER_LISTENER_H_ + +#include "transport_manager/common.h" +#include "transport_manager/error.h" + +namespace transport_manager { + +namespace transport_adapter { + +class TransportAdapter; + +/** + * @brief Abstract class for device adapter listener. + */ +class TransportAdapterListener { + public: + /** + * @brief Destructor. + */ + virtual ~TransportAdapterListener() { + } + + /** + * @brief Search specified device adapter in the container of shared pointers to device adapters to be sure it is available, + * launch event ON_SEARCH_DONE in transport manager. + * + * @param transport_adapter Pointer to the device adapter. + */ + virtual void OnSearchDeviceDone( + const TransportAdapter* transport_adapter) = 0; + + /** + * @brief Search specified device adapter in the container of shared pointers to device adapters to be sure it is available, create search device error, + * launch event ON_SEARCH_FAIL in transport manager. + * + * @param transport_adapter Pointer to the device adapter. + * @param error Error class with information about possible reason of search device failure. + */ + virtual void OnSearchDeviceFailed(const TransportAdapter* transport_adapter, + const SearchDeviceError& error) = 0; + + /** + * @brief Notification by transport adapter that list of known devices has + * changed. Updated list can be obtained using + * TransportAdapter::GetDeviceList() + * + * @param transport_adapter Transport adapter that sent notifcation + */ + virtual void OnDeviceListUpdated( + const TransportAdapter* transport_adapter) = 0; + + /** + * @brief Search specified device adapter in the container of shared pointers to device adapters to be sure it is available, + * launch event ON_CONNECT_DONE in transport manager. + * + * @param device_adater Pointer to the device adapter. + * @param device_handle Device unique identifier. + * @param app_id Handle of application. + */ + virtual void OnConnectDone(const TransportAdapter* transport_adapter, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle) = 0; + + /** + * @brief Search specified device adapter in the container of shared pointers to device adapters to be sure it is available, + * launch event ON_CONNECT_FAIL in transport manager. + * + * @param transport_adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param app_id Handle of application. + * @param error Error class with information about possible reason of connect failure. + */ + virtual void OnConnectFailed(const TransportAdapter* transport_adapter, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const ConnectError& error) = 0; + + /** + * @brief + * + * @param transport_adapter Pointer to the device adapter. + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + */ + virtual void OnConnectRequested(const TransportAdapter* transport_adapter, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle) = 0; + + /** + * @brief Search specified device adapter in the container of shared pointers to device adapters to be sure it is available, + * launch event ON_UNEXPECTED_DISCONNECT in transport manager. + * + * @param transport_adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param app_id Handle of application. + * @param error Error class with information about possible reason of unexpected Disconnect. + */ + virtual void OnUnexpectedDisconnect(const TransportAdapter* transport_adapter, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const CommunicationError& error) = 0; + /** + * @brief Search specified device adapter in the container of shared pointers to device adapters to be sure it is available, + * launch event ON_DISCONNECT_DONE in transport manager. + * + * @param transport_adapter Pointer to the device adapter. + * @param devcie_id Device unique identifier. + * @param app_id Handle of application. + */ + virtual void OnDisconnectDone(const TransportAdapter* transport_adapter, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle) = 0; + + /** + * @brief Search specified device adapter in the container of shared pointers to device adapters to be sure it is available, create Disconnect error, + * launch event ON_DISCONNECT_FAIL in transport manager. + * + * @param transport_adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param app_id Handle of application. + * @param error Error class with information about possible reason of Disconnect failure. + */ + virtual void OnDisconnectFailed(const TransportAdapter* transport_adapter, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const DisconnectError& error) = 0; + + /** + * @brief + * + * @param transport_adapter Pointer to the device adapter. + * @param devic Device unique identifier. + */ + virtual void OnDisconnectDeviceDone(const TransportAdapter* transport_adapter, + const DeviceUID& device_handle) = 0; + + /** + * @brief + * + * @param transport_adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param error Error class with information about possible reason of Disconnect from device failure. + */ + virtual void OnDisconnectDeviceFailed( + const TransportAdapter* transport_adapter, const DeviceUID& device_handle, + const DisconnectDeviceError& error) = 0; + + /** + * @brief Search specified device adapter in the container of shared pointers to device adapters to be sure it is available, create error, + * launch event ON_RECEIVED_DONE in transport manager. + * + * @param transport_adapter Pointer to the device adapter. + * @param devcie Device unique identifier. + * @param app_id Handle of application. + * @param data_container Smart pointer to the raw message. + */ + virtual void OnDataSendDone(const TransportAdapter* transport_adapter, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const RawMessageSptr data_container) = 0; + + /** + * @brief Search specified device adapter in the container of shared pointers to device adapters to be sure it is available, create data receive error, + * launch event ON_RECEIVED_DONE in transport manager. + * + * @param transport_adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param app_id Handle of application. + * @param error Error class with information about possible reason of data receive failure. + */ + virtual void OnDataSendFailed(const TransportAdapter* transport_adapter, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const RawMessageSptr data_container, + const DataSendError& error) = 0; + + /** + * @brief Search specified device adapter in the container of shared pointers to device adapters to be sure it is available, create error, + * launch event ON_SEND_DONE in transport manager. + * + * @param transport_adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param app_id Handle of application. + * @param data_container Smart pointer to the raw message. + */ + virtual void OnDataReceiveDone(const TransportAdapter* transport_adapter, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const RawMessageSptr data_container) = 0; + + /** + * @brief Search specified device adapter in the container of shared pointers to device adapters to be sure it is available, create data send error, + * launch event ON_SEND_FAIL in transport manager. + */ + virtual void OnDataReceiveFailed(const TransportAdapter* transport_adapter, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle, + const DataReceiveError& error) = 0; + + /** + * @brief Search specified device adapter in the container of shared pointers to device adapters to be sure it is available, create error, + * launch event ON_COMMUNICATION_ERROR in transport manager. + * + * @param transport_adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param app_id Handle of application. + */ + virtual void OnCommunicationError(const TransportAdapter* transport_adapter, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle) = 0; +}; + +} // transport_adapter namespace +} // transport_manager namespace + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_transport_adapter_LISTENER diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_listener_impl.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_listener_impl.h new file mode 100644 index 000000000..3a9975e86 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_listener_impl.h @@ -0,0 +1,303 @@ +/** + * \file transport_adapter_listener_impl.h + * \brief TransportAdapterListenerImpl class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_TRANSPORT_ADAPTER_LISTENER_IMPL_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_TRANSPORT_ADAPTER_LISTENER_IMPL_H_ + +#include "transport_manager/common.h" +#include "transport_manager/transport_adapter/transport_adapter_listener.h" +#include "transport_manager/transport_adapter/transport_adapter.h" +#include "utils/logger.h" + +using transport_manager::transport_adapter::TransportAdapter; + +namespace transport_manager { + +class TransportManagerImpl; + +/** + * @brief Implementation of TransportAdapterListener class. + */ +class TransportAdapterListenerImpl + : public transport_adapter::TransportAdapterListener { + public: + /** + * @enum Available types of events. + */ + enum EventTypeEnum { + ON_SEARCH_DONE = 0, + ON_SEARCH_FAIL, + ON_DEVICE_LIST_UPDATED, + ON_CONNECT_DONE, + ON_CONNECT_FAIL, + ON_DISCONNECT_DONE, + ON_DISCONNECT_FAIL, + ON_SEND_DONE, + ON_SEND_FAIL, + ON_RECEIVED_DONE, + ON_RECEIVED_FAIL, + ON_COMMUNICATION_ERROR, + ON_UNEXPECTED_DISCONNECT + }; + + /** + * @brief Constructor. + * + * @param manager Pointer to the transport manager implementation class. + * @param adapter Pointer to the transport adapter associated with listener. + */ + TransportAdapterListenerImpl(TransportManagerImpl* manager, + TransportAdapter* adapter); + + /** + * @brief Dectructor. + */ + virtual ~TransportAdapterListenerImpl(); + + /** + * @brief Search specified device adapter in the container of shared pointers + *to device adapters to be sure it is available, + * launch event ON_SEARCH_DONE in transport manager. + * + * @param adapter Pointer to the device adapter. + */ + virtual void OnSearchDeviceDone(const TransportAdapter* adapter); + + /** + * @brief Search specified device adapter in the container of shared pointers + *to device adapters to be sure it is available, create search device error, + * launch event ON_SEARCH_FAIL in transport manager. + * + * @param adapter Pointer to the device adapter. + * @param error Error class with information about possible reason of search + *device failure. + */ + virtual void OnSearchDeviceFailed(const TransportAdapter* adapter, + const SearchDeviceError& error); + + /** + * @brief Passes notification to TransportManagerImpl + * + * @param adapter Transport adapter that sent notification + */ + virtual void OnDeviceListUpdated(const TransportAdapter* adapter); + + /** + * @brief Search specified device adapter in the container of shared pointers + *to device adapters to be sure it is available, + * launch event ON_CONNECT_DONE in transport manager. + * + * @param device_adater Pointer to the device adapter. + * @param device_handle Device unique identifier. + * @param app_id Handle of application. + */ + virtual void OnConnectDone(const TransportAdapter* adapter, + const DeviceUID& device_handle, + const ApplicationHandle& app_id); + + /** + * @brief Search specified device adapter in the container of shared pointers + *to device adapters to be sure it is available, + * launch event ON_CONNECT_FAIL in transport manager. + * + * @param adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param app_id Handle of application. + * @param error Error class with information about possible reason of connect + *failure. + */ + virtual void OnConnectFailed(const TransportAdapter* adapter, + const DeviceUID& device, + const ApplicationHandle& app_id, + const ConnectError& error); + + /** + * @brief + * + * @param adapter Pointer to the device adapter. + * @param device_handle Device unique identifier. + * @param app_handle Handle of application. + */ + virtual void OnConnectRequested(const TransportAdapter* adapter, + const DeviceUID& device_handle, + const ApplicationHandle& app_handle); + + /** + * @brief Search specified device adapter in the container of shared pointers + *to device adapters to be sure it is available, + * launch event ON_UNEXPECTED_DISCONNECT in transport manager. + * + * @param adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param app_id Handle of application. + * @param error Error class with information about possible reason of + *unexpected Disconnect. + */ + virtual void OnUnexpectedDisconnect(const TransportAdapter* adapter, + const DeviceUID& device, + const ApplicationHandle& app_id, + const CommunicationError& error); + + /** + * @brief Search specified device adapter in the container of shared pointers + *to device adapters to be sure it is available, + * launch event ON_DISCONNECT_DONE in transport manager. + * + * @param adapter Pointer to the device adapter. + * @param devcie_id Device unique identifier. + * @param app_id Handle of application. + */ + virtual void OnDisconnectDone(const TransportAdapter* adapter, + const DeviceUID& device_id, + const ApplicationHandle& app_id); + + /** + * @brief Search specified device adapter in the container of shared pointers + *to device adapters to be sure it is available, create Disconnect error, + * launch event ON_DISCONNECT_FAIL in transport manager. + * + * @param adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param app_id Handle of application. + * @param error Error class with information about possible reason of + *Disconnect failure. + */ + virtual void OnDisconnectFailed(const TransportAdapter* adapter, + const DeviceUID& device, + const ApplicationHandle& app_id, + const DisconnectError& error); + + /** + * @brief + * + * @param adapter Pointer to the device adapter. + * @param devic Device unique identifier. + */ + virtual void OnDisconnectDeviceDone(const TransportAdapter* adapter, + const DeviceUID& device); + + /** + * @brief + * + * @param adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param error Error class with information about possible reason of + *Disconnect from device failure. + */ + virtual void OnDisconnectDeviceFailed(const TransportAdapter* adapter, + const DeviceUID& device, + const DisconnectDeviceError& error); + + /** + * @brief Search specified device adapter in the container of shared pointers + *to device adapters to be sure it is available, create error, + * launch event ON_RECEIVED_DONE in transport manager. + * + * @param adapter Pointer to the device adapter. + * @param devcie Device unique identifier. + * @param app_id Handle of application. + * @param data_container Smart pointer to the raw message. + */ + virtual void OnDataReceiveDone(const TransportAdapter* adapter, + const DeviceUID& device, + const ApplicationHandle& app_id, + const RawMessageSptr data_container); + + /** + * @brief Search specified device adapter in the container of shared pointers + *to device adapters to be sure it is available, create data receive error, + * launch event ON_RECEIVED_DONE in transport manager. + * + * @param adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param app_id Handle of application. + * @param error Error class with information about possible reason of data + *receive failure. + */ + virtual void OnDataReceiveFailed(const TransportAdapter* adapter, + const DeviceUID& device, + const ApplicationHandle& app_id, + const DataReceiveError& error); + + /** + * @brief Search specified device adapter in the container of shared pointers + *to device adapters to be sure it is available, create error, + * launch event ON_SEND_DONE in transport manager. + * + * @param adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param app_id Handle of application. + * @param data_container Smart pointer to the raw message. + */ + virtual void OnDataSendDone(const TransportAdapter* adapter, + const DeviceUID& device, + const ApplicationHandle& app_id, + const RawMessageSptr data_container); + + /** + * @brief Search specified device adapter in the container of shared pointers + * to device adapters to be sure it is available, create data send error, + * launch event ON_SEND_FAIL in transport manager. + */ + virtual void OnDataSendFailed(const TransportAdapter* adapter, + const DeviceUID& device, + const ApplicationHandle& app_id, + const RawMessageSptr data_container, + const DataSendError& error); + + /** + * @brief Search specified device adapter in the container of shared pointers + *to device adapters to be sure it is available, create error, + * launch event ON_COMMUNICATION_ERROR in transport manager. + * + * @param adapter Pointer to the device adapter. + * @param device Device unique identifier. + * @param app_id Handle of application. + */ + virtual void OnCommunicationError(const TransportAdapter* adapter, + const DeviceUID& device, + const ApplicationHandle& app_id); + + private: + /** + * \brief For logging. + */ + static log4cxx::LoggerPtr logger_; + TransportManagerImpl* transport_manager_impl_; + TransportAdapter* transport_adapter_; +}; +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_transport_adapter_transport_adapter_LISTENER_IMPL_H diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager.h new file mode 100644 index 000000000..8ce2b1247 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager.h @@ -0,0 +1,166 @@ +/** + * \file transport_manager.h + * \brief TransportManager class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_MANAGER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_MANAGER_H_ + +#include "protocol_handler/protocol_handler.h" //YK: temp solution until B1.0 release +#include "transport_manager/common.h" +#include "transport_manager/info.h" + +namespace transport_manager { + +class TransportAdapterEvent; +class TransportManagerListener; + +namespace transport_adapter { +class TransportAdapter; +} // namespace transport_adapter + +/** + * @brief Interface of transport manager. + **/ +class TransportManager { + public: + /** + * @brief Destructor. + **/ + virtual ~TransportManager() {} + + /** + * @brief Initialize transport manager. + * @return Error code. + */ + virtual int Init() = 0; + + /** + * @brief Start scanning for new devices. + * + * @return Code error. + **/ + virtual int SearchDevices(void) = 0; + + /** + * @brief Connect to all applications discovered on device. + * + * @param device_id Handle of device to connect to. + * + * @return Code error. + **/ + virtual int ConnectDevice(const DeviceHandle &device_id) = 0; + + /** + * @brief Disconnect from all applications connected on device. + * + * @param device_id Handle of device to Disconnect from. + * + * @return Code error. + **/ + virtual int DisconnectDevice(const DeviceHandle &device_id) = 0; + + /** + * @brief Disconnect from applications connected on device by connection + *unique identifier. + * + * @param connection Connection unique identifier. + * + * @return Code error. + **/ + virtual int Disconnect(const ConnectionUID &connection_id) = 0; + + /** + * @brief Post new message in queue for massages destined to device. + * + * @param message Smart pointer to the raw massage. + * + * @return Code error. + **/ + virtual int SendMessageToDevice(const RawMessageSptr message) = 0; + + /** + * @brief Post event in the event queue. + * + * @param event Current event information. + * + * @return Code error. + **/ + virtual int ReceiveEventFromDevice(const TransportAdapterEvent &event) = 0; + + /** + * @brief Add transport adapter. + * + * @param transport_adapter Transport adapter + * + * @return Error code. + **/ + virtual int AddTransportAdapter( + transport_adapter::TransportAdapter *transport_adapter) = 0; + + /** + * @brief Post listener to the container of transport manager listeners. + * + * @param listener Pointer to the transport manager listener. + * + * @return Code error. + **/ + virtual int AddEventListener(TransportManagerListener *listener) = 0; + + /** + * @brief Stop work finally. No new events guaranteed after method finish. + * + * @return Error code. + **/ + virtual int Stop() = 0; + + /** + * @brief Remove device from the container that hold devices. + * + * @param device Handle of device. + * + * @return Code error. + **/ + virtual int RemoveDevice(const DeviceHandle &device_handle) = 0; + + /** + * @brief Turns on or off visibility of SDL to mobile devices + * when visibility is ON (on_off = true) mobile devices are able to connect + * otherwise ((on_off = false)) SDL is not visible from outside + * + * @return Code error. + */ + virtual int Visibility(const bool &on_off) const = 0; +}; +} + +#endif diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager_default.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager_default.h new file mode 100644 index 000000000..6857a5c67 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager_default.h @@ -0,0 +1,71 @@ +/* + * \file transport_manager_default.h + * \brief Transport manager default class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_MANAGER_DEFAULT_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_MANAGER_DEFAULT_H_ + +#include "utils/singleton.h" + +#include "transport_manager/transport_manager_impl.h" + +namespace transport_manager { + +/** + * @brief Default realization of transport_manager_impl class. + */ +class TransportManagerDefault : public TransportManagerImpl, + public utils::Singleton<TransportManagerDefault> { + + /** + * @brief Initialize transport manager. + * + * @return Code error. + */ + virtual int Init(); + + /** + * @brief Destructor. + */ + virtual ~TransportManagerDefault(); + + TransportManagerDefault(); + + DISALLOW_COPY_AND_ASSIGN(TransportManagerDefault); + + FRIEND_BASE_SINGLETON_CLASS(TransportManagerDefault); +}; +} + +#endif /* TRANSPORT_MANAGER_DEFAULT_H_ */ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager_impl.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager_impl.h new file mode 100755 index 000000000..c46301758 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager_impl.h @@ -0,0 +1,495 @@ +/** + * \file transport_manager_impl.h + * \brief TransportManagerImpl class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_MANAGER_IMPL_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_MANAGER_IMPL_H_ + +#include <pthread.h> + +#ifdef RWLOCK_SUPPORT +# if (defined(OS_LINUX) && (defined(__USE_UNIX98) || defined(__USE_XOPEN2K))) || \ + (defined(OS_QNX) && (defined(__EXT_POSIX1_200112))) +# define USE_RWLOCK +# endif +#endif + +#include <queue> +#include <map> +#include <list> +#include <algorithm> +#include <memory> + +#include "utils/logger.h" +#include "utils/timer_thread.h" +#include "transport_manager/common.h" +#include "transport_manager/transport_manager.h" +#include "transport_manager/transport_manager_listener.h" +#include "transport_manager/transport_adapter/transport_adapter_listener_impl.h" + +using ::transport_manager::transport_adapter::TransportAdapterListener; + +namespace transport_manager { + +/** + * @enum Transport manager states. + */ +enum { + E_SUCCESS = 0, + E_TM_IS_NOT_INITIALIZED, + E_INVALID_HANDLE, + E_CONNECTION_IS_TO_SHUTDOWN, + E_CONNECTION_EXISTS, + E_ADAPTER_EXISTS, + E_ADAPTERS_FAIL, + E_INTERNAL_ERROR +}; + +/** + * @brief Implementation of transport manager. + */ +class TransportManagerImpl : public TransportManager { + public: + /** + * @brief Hold connection parameters. + */ + struct Connection { + ConnectionUID id; + DeviceUID device; + ApplicationHandle application; + }; + + private: + /** + * @brief Structure that contains internal connection parameters + */ + struct ConnectionInternal: public Connection { + TransportManagerImpl* transport_manager; + TransportAdapter* transport_adapter; + typedef timer::TimerThread<ConnectionInternal> TimerInternal; + typedef utils::SharedPtr<TimerInternal> TimerInternalSharedPointer; + TimerInternalSharedPointer timer; + bool shutDown; + int messages_count; + + ConnectionInternal(TransportManagerImpl* transport_manager, + TransportAdapter* transport_adapter, + const ConnectionUID& id, const DeviceUID& dev_id, + const ApplicationHandle& app_id) + : transport_manager(transport_manager), + transport_adapter(transport_adapter), + timer(new TimerInternal(this, &ConnectionInternal::DisconnectFailedRoutine)), + shutDown(false), + messages_count(0) { + Connection::id = id; + Connection::device = dev_id; + Connection::application = app_id; + } + + void DisconnectFailedRoutine() { + LOG4CXX_INFO(logger_, "Disconnection failed"); + transport_manager->RaiseEvent(&TransportManagerListener::OnDisconnectFailed, + transport_manager->converter_.UidToHandle(device), + DisconnectDeviceError()); + shutDown = false; + timer->stop(); + } + + }; + public: + + /** + * @brief Destructor. + **/ + virtual ~TransportManagerImpl(void); + + /** + * @brief Initialize transport manager. + * + * @return Code error. + */ + virtual int Init(); + + /** + * @brief Start scanning for new devices. + * + * @return Code error. + **/ + virtual int SearchDevices(void); + + /** + * @brief Connect to all applications discovered on device. + * + * @param device_id Handle of device to connect to. + * + * @return Code error. + **/ + virtual int ConnectDevice(const DeviceHandle& device_id); + + /** + * @brief Disconnect from all applications connected on device. + * + * @param device_id Handle of device to Disconnect from. + * + * @return Code error. + **/ + virtual int DisconnectDevice(const DeviceHandle& device_id); + + /** + * @brief Disconnect from applications connected on device by connection + *unique identifier. + * + * @param connection Connection unique identifier. + * + * @return Code error. + **/ + virtual int Disconnect(const ConnectionUID& connection_id); + + /** + * @brief Disconnect and clear all unreceived data. + * + * @param connection Connection unique identifier. + */ + virtual int DisconnectForce(const ConnectionUID& connection_id); + /** + * @brief Post new message in queue for massages destined to device. + * + * @param message Smart pointer to the raw massage. + * + * @return Code error. + **/ + virtual int SendMessageToDevice(const RawMessageSptr message); + + /** + * @brief Post event in the event queue. + * + * @param event Current event information. + * + * @return Code error. + **/ + virtual int ReceiveEventFromDevice(const TransportAdapterEvent& event); + + /** + * @brief Post listener to the container of transport manager listeners. + * + * @param listener Pointer to the transport manager listener. + * + * @return Code error. + **/ + virtual int AddEventListener(TransportManagerListener* listener); + + virtual int Stop(); + + /** + * @brief Add device adapter to the container of device adapters. + * + * @param transport_adapter Smart pointer to the device adapter. + * + * @return Code error. + **/ + virtual int AddTransportAdapter( + transport_adapter::TransportAdapter* transport_adapter); + + /** + * @brief Remove device from the container that hold devices. + * + * @param device Handle of device. + * + * @return Code error. + **/ + virtual int RemoveDevice(const DeviceHandle& device); + + /** + * @brief Turns on or off visibility of SDL to mobile devices + * when visibility is ON (on_off = true) mobile devices are able to connect + * otherwise ((on_off = false)) SDL is not visible from outside + * + * @return Code error. + */ + virtual int Visibility(const bool& on_off) const; + + /** + * @brief Updates total device list with info from specific transport adapter. + * @param ta Transport adapter + */ + void UpdateDeviceList(TransportAdapter* ta); + + /** + * @brief Constructor. + **/ + TransportManagerImpl(); + + protected: + + template <class Proc, class... Args> + void RaiseEvent(Proc proc, Args... args) { + for (TransportManagerListenerList::iterator it = + transport_manager_listener_.begin(); + it != transport_manager_listener_.end(); ++it) { + ((*it)->*proc)(args...); + } + } + + /** + * @brief Put massage in the container of massages. + * + * @param message Smart pointer to the raw massage. + **/ + void PostMessage(const RawMessageSptr message); + + /** + * @brief update message in queue + * + * @param message shared pointer to raw massage + * + * @see @ref components_transportmanager_client_connection_management + **/ + /*not clear when this function shall be used + * void updateMessage(const RawMessageSptr old_message, const RawMessageSptr + * new_message);*/ + + /** + * @brief Remove message from the container of massages. + * + * @param message Smart pointer to the raw massage. + **/ + void RemoveMessage(const RawMessageSptr message); + + /** + * @brief Post event to the container of events. + * + * @param event Event of device adapter. + **/ + void PostEvent(const TransportAdapterEvent& event); + + /** + * @brief Type definition of container that holds smart pointer to the raw + *massages. + **/ + typedef std::list<RawMessageSptr> MessageQueue; + + /** + * @brief Type definition of container that holds events of device adapters. + **/ + typedef std::vector<TransportAdapterEvent> EventQueue; + + static void* MessageQueueStartThread(void* data); + + /** + * @brief Scan message's queue and pull messages according to priority and + *serial number + * + * @param + * + * @see @ref components_transportmanager_client_connection_management + */ + void MessageQueueThread(void); + + /** + * @brief Launch EventListenerThread(void). + */ + static void* EventListenerStartThread(void*); + /** + * @brief wait until event happens + * + * @param + * + * @see @ref components_transportmanager_client_connection_management + */ + void EventListenerThread(void); + + /** + * \brief For logging. + */ + static log4cxx::LoggerPtr logger_; + + /** + * @brief store messages + * + * @param + * + * @see @ref components_transportmanager_client_connection_management + **/ + MessageQueue message_queue_; + + /** + * @brief Mutex restricting access to messages. + **/ +#ifdef USE_RWLOCK + mutable pthread_rwlock_t message_queue_rwlock_; +#endif + mutable pthread_mutex_t message_queue_mutex_; + + pthread_cond_t message_queue_cond_; + + /** + * @brief store events from comming device + * + * @param + * + * @see @ref components_transportmanager_client_connection_management + **/ + EventQueue event_queue_; + + /** + * @brief flag that indicates that thread is active + * if it is false then threads exist main loop + **/ + volatile bool all_thread_active_; + + typedef std::list<TransportManagerListener*> TransportManagerListenerList; + /** + * @brief listener that would be called when TM's event happened. + **/ + TransportManagerListenerList transport_manager_listener_; + + /** + * @brief ID of message queue processing thread + **/ + pthread_t message_queue_thread_; + + /** + * @brief Conditional event thread + **/ + pthread_t event_queue_thread_; + + /** + * @brief Condition variable to wake up event + **/ + pthread_cond_t device_listener_thread_wakeup_; + + /** + * @brief Mutex restricting access to events. + **/ +#ifdef USE_RWLOCK + mutable pthread_rwlock_t event_queue_rwlock_; +#endif + mutable pthread_mutex_t event_queue_mutex_; + + /** + * @brief Flag that TM is initialized + */ + bool is_initialized_; + + private: + /** + * @brief Returns size of frame to be formed from raw bytes. + * expects first bytes of message which will be treated as frame header. + */ + // TODO this function should be moved outside of TM to protocol handler or + // somewhere else + unsigned int GetPacketSize(unsigned int size, unsigned char* data); + + /** + * @brief Structure that contains conversion functions (Device ID -> Device + * Handle; Device Handle -> Device ID) + */ + struct Handle2GUIDConverter { + typedef std::vector<DeviceUID> ConversionTable; + + DeviceHandle UidToHandle(const DeviceUID& dev_uid) { + bool is_new = true; + return UidToHandle(dev_uid, is_new); + } + + DeviceHandle UidToHandle(const DeviceUID& dev_uid, bool& is_new) { + ConversionTable::iterator it = std::find( + conversion_table_.begin(), conversion_table_.end(), dev_uid); + if (it != conversion_table_.end()) { + is_new = false; + return std::distance(conversion_table_.begin(), it) + + 1; // handle begin since 1 (one) + } + is_new = true; + conversion_table_.push_back(dev_uid); + return conversion_table_.size(); // handle begin since 1 (one) + } + + DeviceUID HandleToUid(DeviceHandle handle) { + if (handle == 0 || handle > conversion_table_.size()) { + return DeviceUID(); + } + return conversion_table_[handle - 1]; // handle begin since 1 (one) + } + + ConversionTable conversion_table_; + }; + + /** + * @brief Converter variable (Device ID -> Device Handle; Device Handle -> + * Device ID) + */ + Handle2GUIDConverter converter_; + + explicit TransportManagerImpl(const TransportManagerImpl&); + int connection_id_counter_; + std::vector<ConnectionInternal> connections_; + std::map<DeviceUID, TransportAdapter*> device_to_adapter_map_; + std::vector<TransportAdapter*> transport_adapters_; + /** For keep listeners which were add TMImpl */ + std::map<TransportAdapter*, TransportAdapterListenerImpl*> + transport_adapter_listeners_; + + typedef std::vector<std::pair<const TransportAdapter*, DeviceInfo> > + DeviceList; + DeviceList device_list_; + + class IncomingDataHandler; + std::auto_ptr<IncomingDataHandler> incoming_data_handler_; + + void AddConnection(const ConnectionInternal& c); + void RemoveConnection(int id); + ConnectionInternal* GetConnection(const ConnectionUID& id); + ConnectionInternal* GetConnection(const DeviceUID& device, + const ApplicationHandle& application); + + void AddDataToContainer( + ConnectionUID id, + std::map<ConnectionUID, std::pair<unsigned int, unsigned char*> >& + container, + unsigned char* data, unsigned int data_size); + bool GetFrameSize(unsigned char* data, unsigned int data_size, + unsigned int& frame_size); + bool GetFrame(std::map<ConnectionUID, + std::pair<unsigned int, unsigned char*> >& container, + ConnectionUID id, unsigned int frame_size, + unsigned char** frame); + + void OnDeviceListUpdated(TransportAdapter* ta); + static Connection convert(ConnectionInternal& p); +}; +// class ; + +} // namespace transport_manager + +#endif diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager_listener.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager_listener.h new file mode 100644 index 000000000..a4b240472 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager_listener.h @@ -0,0 +1,170 @@ +/** + * \file transport_manager_listener.h + * \brief TransportManagerListener class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_MANAGER_LISTENER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_MANAGER_LISTENER_H_ + +#include "transport_manager/common.h" +#include "transport_manager/info.h" +#include "transport_manager/error.h" + +namespace transport_manager { + +class TransportManagerListener { + public: + /** + * @brief Destructor. + */ + virtual ~TransportManagerListener() { + } + + /** + * @brief Reaction to the event, when the list of devices is updated. + * + * @param Container that holds information about devices. + */ + virtual void OnDeviceListUpdated(const std::vector<DeviceInfo>&) = 0; + + /** + * @brief Reaction to the event, when the device is found. + * + * @param device_info Variable that hold information about device. + */ + virtual void OnDeviceFound(const DeviceInfo &device_info) = 0; + virtual void OnDeviceAdded(const DeviceInfo &device_info) = 0; + virtual void OnDeviceRemoved(const DeviceInfo &device_info) = 0; + + /** + * @brief Reaction to the event, when scanning of devices is finished. + */ + virtual void OnScanDevicesFinished() = 0; + + /** + * @brief Reaction to the event, when scanning of devices is failed. + * + * @param error Error information about possible reason of scanning of devices failure. + */ + virtual void OnScanDevicesFailed(const SearchDeviceError& error) = 0; + + /** + * @brief Reaction to the event, when connection is established. + * + * @param devcie_info Variable that hold information about device. + * @param connection_id connection unique identifier. + */ + virtual void OnConnectionEstablished(const DeviceInfo &device_info, + const ConnectionUID &connection_id) = 0; + + /** + * @brief Reaction to the event, when connection to the device is failed. + * + * @param device_info Variable that hold information about device. + * @param error Error information about possible reason of connect failure. + */ + virtual void OnConnectionFailed(const DeviceInfo &device_info, + const ConnectError& error) = 0; + + /** + * @brief Reaction to the event, when connection is closed. + * + * @param connection_id Connection unique identifier. + */ + virtual void OnConnectionClosed(ConnectionUID connection_id) = 0; + + /** + * @brief Called when connection is closed unexpectedly, i.e. disconnect was not requested + * + * @param connection_id Connection ID. + * @param error Error information. + */ + virtual void OnUnexpectedDisconnect(ConnectionUID connection_id, + const CommunicationError& error) = 0; + + /** + * @brief Reaction to the event, when connection close is failed. + * + * @param connection_id Connection unique identifier. + * @param error Error information about possible reason of failure. + */ + virtual void OnConnectionClosedFailure(ConnectionUID connection_id, + const DisconnectError& error) = 0; + + /** + * \brief Inform about losing connection with device. + * \param device Handle of device. + * \param error Error information about possible reason of loosing connection. + */ + virtual void OnDeviceConnectionLost(const DeviceHandle& device, + const DisconnectDeviceError& error) = 0; + + /** + * \brief Inform about failure during DisconnectDevice procedure of transport manager. + * \param device Handle of device. + * \param error Error information about possible reason of disconnecting failure. + */ + virtual void OnDisconnectFailed(const DeviceHandle& device, + const DisconnectDeviceError& error) = 0; + /** + * @brief Notifies about recieving message from TM. + * + * @param message Recieved message + **/ + virtual void OnTMMessageReceived(const RawMessageSptr message) = 0; + + /** + * @brief Reaction to the event, when receiving of massage for transport manager is failed. + * + * @param connection_id connection unique identifier. + * @param error Error information about possible reason of failure. + */ + virtual void OnTMMessageReceiveFailed(ConnectionUID connection_id, + const DataReceiveError& error) = 0; + + /** + * @brief Reaction to the event, when transport manager sent a massage. + */ + virtual void OnTMMessageSend(const RawMessageSptr message) = 0; + + /** + * @brief Reaction to the event, when sending of massage by transport manager is failed. + * + * @param error Error information about possible reason of failure. + * @param message Smart pointer to the raw massage. + */ + virtual void OnTMMessageSendFailed(const DataSendError& error, + const RawMessageSptr message) = 0; +}; +} // namespace transport_manager +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_MANAGER_LISTENER diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager_listener_empty.h b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager_listener_empty.h new file mode 100644 index 000000000..5251a1c50 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/transport_manager_listener_empty.h @@ -0,0 +1,189 @@ +/** + * \file transport_manager_listener_empty.h + * \brief TransportManagerListenerEmpty class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_MANAGER_LISTENER_EMPTY_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_MANAGER_LISTENER_EMPTY_H_ + +#include "transport_manager/info.h" +#include "transport_manager/transport_manager_listener.h" +#include "transport_manager/transport_manager_impl.h" + +namespace transport_manager { + +/** + * @brief Empty implementation class for transport manager listener. + */ +class TransportManagerListenerEmpty : public TransportManagerListener { + const TransportManagerImpl *transport_manager_; + public: + + /** + * @Destructor. + */ + virtual ~TransportManagerListenerEmpty(){}; + + /** + * @brief Reaction to the event, when the list of devices is updated. + * + * @param Container that holds information about devices. + */ + virtual void OnDeviceListUpdated(const std::vector<DeviceInfo>&) { + } + + /** + * @brief Reaction to the event, when the device is found. + * + * @param device_info Variable that hold information about device. + */ + virtual void OnDeviceFound(const DeviceInfo& device_info) { + } + virtual void OnDeviceAdded(const DeviceInfo& device_info) { + } + virtual void OnDeviceRemoved(const DeviceInfo& device_info) { + } + + /** + * @brief Reaction to the event, when scanning of devices is finished. + */ + virtual void OnScanDevicesFinished() { + } + + /** + * @brief Reaction to the event, when scanning of devices is failed. + * + * @param error Error information about possible reason of scanning of devices failure. + */ + virtual void OnScanDevicesFailed(const SearchDeviceError& error) { + } + + /** + * @brief Reaction to the event, when connection is established. + * + * @param devcie_info Variable that hold information about device. + * @param connection_id connection unique identifier. + */ + virtual void OnConnectionEstablished(const DeviceInfo &device_info, + const ConnectionUID &connection_id) { + } + + /** + * @brief Reaction to the event, when connection to the device is failed. + * + * @param device_info Variable that hold information about device. + * @param error Error information about possible reason of connect failure. + */ + virtual void OnConnectionFailed(const DeviceInfo &device_info, + const ConnectError& error) { + } + + /** + * @brief Reaction to the event, when connection is closed. + * + * @param connection_id Connection unique identifier. + */ + virtual void OnConnectionClosed(ConnectionUID connection_id) { + } + + virtual void OnUnexpectedDisconnect(ConnectionUID connection_id, + const CommunicationError& error) { + } + + /** + * @brief Reaction to the event, when connection close is failed. + * + * @param connection_id Connection unique identifier. + * @param error Error information about possible reason of failure. + */ + virtual void OnConnectionClosedFailure(ConnectionUID connection_id, + const DisconnectError& error) { + } + + /** + * @brief Reaction to the event, when connection with the device is lost. + * + * @param device Handle of device. + * @param error Error information about possible reason of lost connection. + */ + virtual void OnDeviceConnectionLost(const DeviceHandle& device, + const DisconnectDeviceError& error) { + } + + /** + * @brief Reaction to the event, when Disconnect is failed. + * + * @param device Handle of device. + * @param error Error information about possible reason of Disconnect failure. + */ + virtual void OnDisconnectFailed(const DeviceHandle& device, + const DisconnectDeviceError& error) { + } + + /** + * @brief Reaction to the event, when transport manager received a massage. + * + * @param message Smart pointer to the raw massage. + * @param connection_id Connection unique identifier. + */ + virtual void OnTMMessageReceived(const RawMessageSptr message) { + } + + /** + * @brief Reaction to the event, when receiving of massage for transport manager is failed. + * + * @param connection_id connection unique identifier. + * @param error Error information about possible reason of failure. + */ + virtual void OnTMMessageReceiveFailed(ConnectionUID connection_id, + const DataReceiveError& error) { + } + + /** + * @brief Reaction to the event, when transport manager sent a massage. + */ + virtual void OnTMMessageSend(const RawMessageSptr message) { + } + + /** + * @brief Reaction to the event, when sending of massage by transport manager is failed. + * + * @param error Error information about possible reason of failure. + * @param message Smart pointer to the raw massage. + */ + virtual void OnTMMessageSendFailed(const DataSendError& error, + const RawMessageSptr message) { + } +}; +} // namespace transport_manager +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_MANAGER_LISTENER_EMPTY_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/usb/common.h b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/common.h new file mode 100644 index 000000000..31ef25a24 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/common.h @@ -0,0 +1,82 @@ +/** + * \file common.h + * \brief TM USB adapter common definitions + * + * 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_COMMON_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_COMMON_H_ + +#include <cstdint> + +#include "utils/shared_ptr.h" + +#if defined(__QNX__) || (__QNXNTO__) +#include "transport_manager/usb/qnx/usb_handler.h" +#else +#include "transport_manager/usb/libusb/usb_handler.h" +#endif + +namespace transport_manager { + +namespace transport_adapter { + +static const uint16_t kAoaVid = 0x18d1; +static const uint16_t kAoaPid1 = 0x2d00; +static const uint16_t kAoaPid2 = 0x2d01; +static const uint8_t kAoaInterfaceSubclass = 0xff; +static const int kUsbConfiguration = 1; + +typedef utils::SharedPtr<UsbHandler> UsbHandlerSptr; + +class UsbDeviceListener { + public: + virtual ~UsbDeviceListener() {} + + UsbHandlerSptr GetUsbHandler() { return usb_handler_; } + + void SetUsbHandler(UsbHandlerSptr usb_handler) { + usb_handler_ = usb_handler; + usb_handler_->usb_device_listeners_.push_back(this); + } + + virtual void OnDeviceArrived(PlatformUsbDevice* device) = 0; + virtual void OnDeviceLeft(PlatformUsbDevice* device) = 0; + + private: + UsbHandlerSptr usb_handler_; +}; + +} // namespace +} // namespace + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_COMMON_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/usb/libusb/platform_usb_device.h b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/libusb/platform_usb_device.h new file mode 100644 index 000000000..d1f485c9d --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/libusb/platform_usb_device.h @@ -0,0 +1,78 @@ +/** + * \file platform_usb_device.h + * \brief libusb PlatformUsbDevice class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_LIBUSB_PLATFORM_USB_DEVICE_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_LIBUSB_PLATFORM_USB_DEVICE_H_ + +#include <string> +#include <libusb/libusb.h> + +namespace transport_manager { + +namespace transport_adapter { + +class PlatformUsbDevice { + public: + uint8_t bus_number() const { return bus_number_; } + uint8_t address() const { return address_; } + uint16_t vendor_id() const { return vendor_id_; } + uint16_t product_id() const { return product_id_; } + std::string GetManufacturer() const; + std::string GetProductName() const; + std::string GetSerialNumber() const; + PlatformUsbDevice(uint8_t bus, uint8_t address, + const libusb_device_descriptor& device_descriptor, + libusb_device* device_libusb, + libusb_device_handle* device_handle_libusb); + libusb_device_handle* GetLibusbHandle() { return libusb_device_handle_; } + libusb_device* GetLibusbDevice() { return libusb_device_; } + + private: + std::string GetDescString(uint8_t index) const; + + private: + uint8_t bus_number_; + uint8_t address_; + uint16_t vendor_id_; + uint16_t product_id_; + libusb_device_descriptor device_descriptor_; + libusb_device_handle* libusb_device_handle_; + libusb_device* libusb_device_; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_LIBUSB_PLATFORM_USB_DEVICE_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/usb/libusb/usb_connection.h b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/libusb/usb_connection.h new file mode 100644 index 000000000..b6c050fa7 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/libusb/usb_connection.h @@ -0,0 +1,100 @@ +/** + * \file usb_connection.h + * \brief UsbConnection class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_LIBUSB_USB_CONNECTION_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_LIBUSB_USB_CONNECTION_H_ + +#include <pthread.h> + +#include "transport_manager/transport_adapter/transport_adapter_controller.h" +#include "transport_manager/transport_adapter/connection.h" +#include "transport_manager/usb/common.h" + +namespace transport_manager { +namespace transport_adapter { + +class UsbConnection : public Connection { + public: + UsbConnection(const DeviceUID& device_uid, + const ApplicationHandle& app_handle, + TransportAdapterController* controller, + const UsbHandlerSptr& usb_handler, PlatformUsbDevice* device); + + bool Init(); + + virtual ~UsbConnection(); + + protected: + virtual TransportAdapter::Error SendData(RawMessageSptr message); + virtual TransportAdapter::Error Disconnect(); + + private: + friend void InTransferCallback(struct libusb_transfer*); + friend void OutTransferCallback(struct libusb_transfer*); + bool FindEndpoints(); + void PopOutMessage(); + bool PostInTransfer(); + bool PostOutTransfer(); + void OnInTransfer(struct libusb_transfer*); + void OnOutTransfer(struct libusb_transfer*); + void Finalise(); + + const DeviceUID device_uid_; + const ApplicationHandle app_handle_; + TransportAdapterController* controller_; + UsbHandlerSptr usb_handler_; + libusb_device* libusb_device_; + libusb_device_handle* device_handle_; + uint8_t in_endpoint_; + uint16_t in_endpoint_max_packet_size_; + uint8_t out_endpoint_; + uint16_t out_endpoint_max_packet_size_; + unsigned char* in_buffer_; + libusb_transfer* in_transfer_; + libusb_transfer* out_transfer_; + + std::list<RawMessageSptr> out_messages_; + RawMessageSptr current_out_message_; + pthread_mutex_t out_messages_mutex_; + size_t bytes_sent_; + bool disconnecting_; + bool waiting_in_transfer_cancel_; + bool waiting_out_transfer_cancel_; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_LIBUSB_USB_CONNECTION_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/usb/libusb/usb_handler.h b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/libusb/usb_handler.h new file mode 100644 index 000000000..e76f49f8e --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/libusb/usb_handler.h @@ -0,0 +1,100 @@ +/** + * \file usb_handler.h + * \brief libusb USB handler class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_LIBUSB_USB_HANDLER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_LIBUSB_USB_HANDLER_H_ + +#include <pthread.h> + +#include <libusb/libusb.h> + +#include "transport_manager/transport_adapter/transport_adapter.h" +#include "transport_manager/usb/usb_control_transfer.h" +#include "transport_manager/usb/libusb/platform_usb_device.h" + +namespace transport_manager { + +namespace transport_adapter { + +class UsbHandler { + public: + UsbHandler(); + ~UsbHandler(); + void StartControlTransferSequence(UsbControlTransferSequence* sequence, + PlatformUsbDevice* device); + TransportAdapter::Error Init(); + + public: + void CloseDeviceHandle(libusb_device_handle* device_handle); + + private: + class ControlTransferSequenceState; + + void Thread(); + void DeviceArrived(libusb_device* device); + void DeviceLeft(libusb_device* device); + + void ControlTransferCallback(libusb_transfer* transfer); + void SubmitControlTransfer(ControlTransferSequenceState* sequence_state); + friend void UsbTransferSequenceCallback(libusb_transfer* transfer); + + private: + bool shutdown_requested_; + pthread_t thread_; + + friend class UsbDeviceListener; + std::list<class UsbDeviceListener*> usb_device_listeners_; + + typedef std::vector<PlatformUsbDevice*> Devices; + Devices devices_; + + typedef std::list<ControlTransferSequenceState*> TransferSequences; + TransferSequences transfer_sequences_; + std::list<libusb_device_handle*> device_handles_to_close_; + libusb_context* libusb_context_; + libusb_hotplug_callback_handle arrived_callback_handle_; + libusb_hotplug_callback_handle left_callback_handle_; + + friend void* UsbHandlerThread(void* data); + friend int ArrivedCallback(libusb_context* context, libusb_device* device, + libusb_hotplug_event event, void* data); + friend int LeftCallback(libusb_context* context, libusb_device* device, + libusb_hotplug_event event, void* data); +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_LIBUSB_USB_HANDLER_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/usb/qnx/platform_usb_device.h b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/qnx/platform_usb_device.h new file mode 100644 index 000000000..939a8fcb3 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/qnx/platform_usb_device.h @@ -0,0 +1,78 @@ +/** + * \file platform_usb_device.h + * \brief QNX PlatfromUsbDevice class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_QNX_PLATFORM_USB_DEVICE_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_QNX_PLATFORM_USB_DEVICE_H_ + +#include <string> +#include <sys/usbdi.h> + +namespace transport_manager { + +namespace transport_adapter { + +class PlatformUsbDevice { + public: + uint8_t bus_number() const { return bus_number_; } + uint8_t address() const { return address_; } + uint16_t vendor_id() const { return vendor_id_; } + uint16_t product_id() const { return product_id_; } + std::string GetManufacturer() const; + std::string GetProductName() const; + std::string GetSerialNumber() const; + PlatformUsbDevice(usbd_device_instance_t* instance, usbd_device* device, + const usbd_device_descriptor_t& device_descriptor); + const usbd_device_instance_t& GetDeviceInstance() { + return usbd_device_instance_; + } + usbd_device* GetUsbdDevice() { return usbd_device_; } + + private: + std::string GetDescString(uint8_t index) const; + + private: + uint8_t bus_number_; + uint8_t address_; + uint16_t vendor_id_; + uint16_t product_id_; + usbd_device_descriptor_t device_descriptor_; + usbd_device_instance_t usbd_device_instance_; + usbd_device* usbd_device_; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_QNX_PLATFORM_USB_DEVICE_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/usb/qnx/usb_connection.h b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/qnx/usb_connection.h new file mode 100644 index 000000000..3cd52fade --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/qnx/usb_connection.h @@ -0,0 +1,101 @@ +/** + * \file usb_connection.h + * \brief UsbConnection class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_QNX_USB_CONNECTION_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_QNX_USB_CONNECTION_H_ + +#include <pthread.h> + +#include "transport_manager/transport_adapter/transport_adapter_controller.h" +#include "transport_manager/transport_adapter/connection.h" +#include "transport_manager/usb/common.h" + +namespace transport_manager { +namespace transport_adapter { + +class UsbConnection : public Connection { + static const uint32_t kInBufferSize = 2048; + public: + UsbConnection(const DeviceUID& device_uid, + const ApplicationHandle& app_handle, + TransportAdapterController* controller, + const UsbHandlerSptr& libusb_handler, + PlatformUsbDevice* device); + + bool Init(); + + virtual ~UsbConnection(); + protected: + virtual TransportAdapter::Error SendData(RawMessageSptr message); + virtual TransportAdapter::Error Disconnect(); + private: + friend void InTransferCallback(usbd_urb* urb, usbd_pipe*, void*); + friend void OutTransferCallback(usbd_urb* urb, usbd_pipe*, void*); + bool OpenEndpoints(); + void PopOutMessage(); + bool PostInTransfer(); + bool PostOutTransfer(); + void OnInTransfer(usbd_urb* urb); + void OnOutTransfer(usbd_urb* urb); + void Finalise(); + + const DeviceUID device_uid_; + const ApplicationHandle app_handle_; + TransportAdapterController* controller_; + UsbHandlerSptr libusb_handler_; + usbd_device* usbd_device_; + + usbd_pipe* in_pipe_; + usbd_pipe* out_pipe_; + + unsigned char* in_buffer_; + void* out_buffer_; + + usbd_urb* in_urb_; + usbd_urb* out_urb_; + + std::list<RawMessageSptr> out_messages_; + RawMessageSptr current_out_message_; + pthread_mutex_t out_messages_mutex_; + size_t bytes_sent_; + bool disconnecting_; + bool pending_in_transfer_; + bool pending_out_transfer_; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_QNX_USB_CONNECTION_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/usb/qnx/usb_handler.h b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/qnx/usb_handler.h new file mode 100644 index 000000000..26b00087f --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/qnx/usb_handler.h @@ -0,0 +1,85 @@ +/** + * \file usb_handler.h + * \brief QNX USB handler class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_QNX_USB_HANDLER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_QNX_USB_HANDLER_H_ + +#include <sys/usbdi.h> + +#include "transport_manager/transport_adapter/transport_adapter.h" +#include "transport_manager/usb/usb_control_transfer.h" +#include "transport_manager/usb/qnx/platform_usb_device.h" + +namespace transport_manager { + +namespace transport_adapter { + +class UsbHandler { + public: + UsbHandler(); + ~UsbHandler(); + void StartControlTransferSequence(UsbControlTransferSequence* sequence, + PlatformUsbDevice* device); + TransportAdapter::Error Init(); + + private: + void DeviceArrived(usbd_connection* connection, + usbd_device_instance_t* instance); + void DeviceLeft(usbd_device_instance_t* instance); + + private: + friend class UsbDeviceListener; + std::list<class UsbDeviceListener*> usb_device_listeners_; + + typedef std::vector<PlatformUsbDevice*> Devices; + Devices devices_; + + struct usbd_connection* usbd_general_connection_; + struct usbd_connection* usbd_aoa_connection_; + + friend void ArrivedCallback(usbd_connection* connection, + usbd_device_instance_t* instance); + friend void LeftCallback(usbd_connection* connection, + usbd_device_instance_t* instance); + friend void ArrivedAoaCallback(usbd_connection* connection, + usbd_device_instance_t* instance); + friend void LeftAoaCallback(usbd_connection* connection, + usbd_device_instance_t* instance); +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_QNX_USB_HANDLER_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_aoa_adapter.h b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_aoa_adapter.h new file mode 100644 index 000000000..55fb028ef --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_aoa_adapter.h @@ -0,0 +1,64 @@ +/** + * \file usb_aoa_adapter.h + * \brief UsbAoaAdapter class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_USB_AOA_ADAPTER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_USB_AOA_ADAPTER_H_ + +#include "transport_manager/transport_adapter/transport_adapter_impl.h" +#include "transport_manager/usb/common.h" + +namespace transport_manager { +namespace transport_adapter { + +class UsbAoaAdapter : public TransportAdapterImpl { + public: + UsbAoaAdapter(); + virtual ~UsbAoaAdapter(); + + protected: + virtual DeviceType GetDeviceType() const; + virtual bool IsInitialised() const; + virtual TransportAdapter::Error Init(); + virtual bool ToBeAutoConnected(DeviceSptr device) const; + + private: + bool is_initialised_; + UsbHandlerSptr usb_handler_; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_USB_AOA_ADAPTER diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_connection_factory.h b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_connection_factory.h new file mode 100644 index 000000000..ed0fda91e --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_connection_factory.h @@ -0,0 +1,67 @@ +/** + * \file usb_connection_factory.h + * \brief UsbConnectionFactory class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_USB_CONNECTION_FACTORY_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_USB_CONNECTION_FACTORY_H_ + +#include "transport_manager/transport_adapter/server_connection_factory.h" +#include "transport_manager/transport_adapter/transport_adapter_controller.h" +#include "transport_manager/usb/common.h" + +namespace transport_manager { +namespace transport_adapter { + +class UsbConnectionFactory : public ServerConnectionFactory { + public: + UsbConnectionFactory(TransportAdapterController* controller); + void SetUsbHandler(const UsbHandlerSptr& usb_handler); + + protected: + virtual TransportAdapter::Error Init(); + virtual TransportAdapter::Error CreateConnection( + const DeviceUID& device_uid, const ApplicationHandle& app_handle); + virtual void Terminate(); + virtual bool IsInitialised() const; + virtual ~UsbConnectionFactory(); + + private: + TransportAdapterController* controller_; + UsbHandlerSptr usb_handler_; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_CONNECTION_FACTORY_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_control_transfer.h b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_control_transfer.h new file mode 100644 index 000000000..8c4f8a9d4 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_control_transfer.h @@ -0,0 +1,105 @@ +/** + * \file aoa_common.h + * \brief Google AOA protocol header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_CONTROL_TRANSFER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_CONTROL_TRANSFER_H_ + +namespace transport_manager { + +namespace transport_adapter { + +class UsbControlTransfer; +class UsbControlInTransfer; +class UsbControlOutTransfer; + +class UsbControlTransfer { + public: + enum TransferDirection { + IN, + OUT + }; + + enum RequestType { + VENDOR + }; + + virtual ~UsbControlTransfer() {} + virtual TransferDirection Direction() const = 0; + virtual RequestType Type() const = 0; + virtual uint8_t Request() const = 0; + virtual uint16_t Value() const = 0; + virtual uint16_t Index() const = 0; + virtual uint16_t Length() const = 0; +}; + +class UsbControlInTransfer : public UsbControlTransfer { + public: + virtual ~UsbControlInTransfer() {} + virtual TransferDirection Direction() const { return IN; } + virtual bool OnCompleted(unsigned char* data) const = 0; +}; + +class UsbControlOutTransfer : public UsbControlTransfer { + public: + virtual ~UsbControlOutTransfer() {} + virtual TransferDirection Direction() const { return OUT; } + virtual const char* Data() const = 0; +}; + +class UsbControlTransferSequence { + public: + typedef std::list<UsbControlTransfer*> Transfers; + const Transfers& transfers() const { return transfers_; } + + virtual ~UsbControlTransferSequence() { + for (Transfers::iterator it = transfers_.begin(); it != transfers_.end(); + ++it) { + delete *it; + } + } + + protected: + void AddTransfer(UsbControlTransfer* transfer) { + transfers_.push_back(transfer); + } + + private: + Transfers transfers_; +}; + +} // namespace +} // namespace + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_CONTROL_TRANSFER_H_ diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_device.h b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_device.h new file mode 100644 index 000000000..48c5867f9 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_device.h @@ -0,0 +1,74 @@ +/* + * 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. + */ + +/** + * \file usb_device.h + * \brief UsbDevice class header file. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_USB_DEVICE_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_USB_DEVICE_H_ + +#include "transport_manager/transport_adapter/device.h" + +namespace transport_manager { +namespace transport_adapter { + +class UsbDevice : public Device { + public: + UsbDevice(PlatformUsbDevice* usb_device, const std::string& name, + const DeviceUID& unique_device_id) + : Device(name, unique_device_id), usb_device_(usb_device) {} + + PlatformUsbDevice* usb_device() const { return usb_device_; } + + protected: + virtual ~UsbDevice() {} + + virtual bool IsSameAs(const Device* other_device) const { + return unique_device_id() == other_device->unique_device_id(); + } + + virtual ApplicationList GetApplicationList() const { + ApplicationList app_list; + app_list.push_back(1); + return app_list; + } + + private: + PlatformUsbDevice* usb_device_; +}; + +} // namespace transport_adapter +} // namespace transport_manager + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_USB_DEVICE diff --git a/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_device_scanner.h b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_device_scanner.h new file mode 100644 index 000000000..6cae52266 --- /dev/null +++ b/SDL_Core/src/components/transport_manager/include/transport_manager/usb/usb_device_scanner.h @@ -0,0 +1,78 @@ +/** + * \file usb_device_scanner.h + * \brief UsbDeviceScanner class header 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. + */ + +#ifndef SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_DEVICE_SCANNER_H_ +#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_DEVICE_SCANNER_H_ + +#include <list> + +#include <pthread.h> + +#include "transport_manager/transport_adapter/device_scanner.h" +#include "transport_manager/usb/common.h" + +namespace transport_manager { + +namespace transport_adapter { + +class UsbDeviceScanner : public DeviceScanner, public UsbDeviceListener { + public: + UsbDeviceScanner(class TransportAdapterController* controller); + virtual ~UsbDeviceScanner(); + + protected: + virtual TransportAdapter::Error Init(); + virtual TransportAdapter::Error Scan(); + virtual void Terminate(); + virtual bool IsInitialised() const; + virtual void OnDeviceArrived(PlatformUsbDevice* device); + virtual void OnDeviceLeft(PlatformUsbDevice* device); + + private: + void UpdateList(); + void TurnIntoAccessoryMode(PlatformUsbDevice* device); + void GoogleAccessoryFound(PlatformUsbDevice* device); + + TransportAdapterController* controller_; + + typedef std::list<PlatformUsbDevice*> Devices; + Devices devices_; + pthread_mutex_t devices_mutex_; +}; + +} // namespace +} // namespace + +#endif // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_USB_DEVICE_SCANNER |