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/transport_adapter | |
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/transport_adapter')
12 files changed, 2302 insertions, 0 deletions
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 |