diff options
Diffstat (limited to 'src/components/protocol_handler/include/protocol_handler')
3 files changed, 188 insertions, 99 deletions
diff --git a/src/components/protocol_handler/include/protocol_handler/incoming_data_handler.h b/src/components/protocol_handler/include/protocol_handler/incoming_data_handler.h new file mode 100644 index 0000000000..dc753e57d1 --- /dev/null +++ b/src/components/protocol_handler/include/protocol_handler/incoming_data_handler.h @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2014, 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_PROTOCOL_HANDLER_INCLUDE_PROTOCOL_HANDLER_INCOMING_DATA_HANDLER_H_ +#define SRC_COMPONENTS_PROTOCOL_HANDLER_INCLUDE_PROTOCOL_HANDLER_INCOMING_DATA_HANDLER_H_ + +#include <list> +#include <map> +#include <vector> +#include "utils/macro.h" +#include "protocol_packet.h" +#include "transport_manager/common.h" + +namespace protocol_handler { + +/** + * \class IncomingDataHandler + * \brief Class for contecat TM messages to ford frames and validate ford header data + * IncomingDataHandler methods are reentrant and not thread-safe + */ +class IncomingDataHandler { + public: + IncomingDataHandler(); + /** + * @brief Setting additional validator for checking malformed packets + * \param validator pointer + */ + void set_validator(const ProtocolPacket::ProtocolHeaderValidator* const validator); + /** + * @brief Contecat TM messages to ford frames and validate ford header data + * \param TM messages for converting to frames + * \param result of convertion + * - RESULT_FAIL - packet serialization or validation error occurs + * - RESULT_OK - no error ocures + * \return list of complete, correct packets + */ + std::list<ProtocolFramePtr> ProcessData(const RawMessage& tm_message, RESULT_CODE* result); + /** + * @brief Add connection for data handling and verification + */ + void AddConnection( + const transport_manager::ConnectionUID connection_id); + /** + * @brief Remove connection and all unhandled data + */ + void RemoveConnection( + const transport_manager::ConnectionUID connection_id); + + private: + /** + * @brief Returns size of frame to be formed from raw bytes. + */ + static uint32_t GetPacketSize(const ProtocolPacket::ProtocolHeader& header); + /** + * @brief Try to create frame from incoming data + * \param incommung_data raw stream + * \param out_frames list for read frames + * + * \return operation RESULT_CODE + * - RESULT_DEFERRED - waiting for more data + * - RESULT_OK - one or more frames successfully created + * - RESULT_FAIL - packet serialization or validation error occurs + */ + RESULT_CODE CreateFrame(std::vector<uint8_t>& incoming_data, + std::list<ProtocolFramePtr>& out_frames, + const transport_manager::ConnectionUID connection_id); + + typedef std::map<transport_manager::ConnectionUID, std::vector<uint8_t> > ConnectionsDataMap; + ConnectionsDataMap connections_data_; + ProtocolPacket::ProtocolHeader header_; + const ProtocolPacket::ProtocolHeaderValidator * validator_; + DISALLOW_COPY_AND_ASSIGN(IncomingDataHandler); +}; +} // namespace protocol_handler +#endif // SRC_COMPONENTS_PROTOCOL_HANDLER_INCLUDE_PROTOCOL_HANDLER_INCOMING_DATA_HANDLER_H_ diff --git a/src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h b/src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h index 3732e23a4c..130987d391 100644 --- a/src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h +++ b/src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h @@ -36,15 +36,18 @@ #include <map> #include <memory> #include <set> +#include <list> #include "utils/prioritized_queue.h" #include "utils/message_queue.h" #include "utils/threads/message_loop_thread.h" #include "utils/shared_ptr.h" +#include "utils/messagemeter.h" #include "protocol_handler/protocol_handler.h" #include "protocol_handler/protocol_packet.h" #include "protocol_handler/session_observer.h" #include "protocol_handler/protocol_observer.h" +#include "protocol_handler/incoming_data_handler.h" #include "transport_manager/common.h" #include "transport_manager/transport_manager.h" #include "transport_manager/transport_manager_listener_empty.h" @@ -69,11 +72,6 @@ class MessagesToMobileAppHandler; using transport_manager::TransportManagerListenerEmpty; -/** - * @brief Type definition for variable that hold shared pointer to raw message. - */ -typedef utils::SharedPtr<protocol_handler::ProtocolPacket> ProtocolFramePtr; - typedef std::multimap<int32_t, RawMessagePtr> MessagesOverNaviMap; typedef std::set<ProtocolObserver*> ProtocolObservers; typedef transport_manager::ConnectionUID ConnectionID; @@ -131,10 +129,14 @@ class ProtocolHandlerImpl /** * \brief Constructor * \param transportManager Pointer to Transport layer handler for + * \param message_frequency_time used as time for flood filtering + * \param message_frequency_count used as maximum value of messages + * per message_frequency_time period * message exchange. */ explicit ProtocolHandlerImpl( - transport_manager::TransportManager *transport_manager_param); + transport_manager::TransportManager *transport_manager_param, + size_t message_frequency_time, size_t message_frequency_count); /** * \brief Destructor @@ -171,6 +173,11 @@ class ProtocolHandlerImpl #endif // ENABLE_SECURITY /** + * \brief Stop all handling activity + */ + void Stop(); + + /** * \brief Method for sending message to Mobile Application * \param message Message with params to be sent to Mobile App */ @@ -179,7 +186,7 @@ class ProtocolHandlerImpl /** * \brief Sends number of processed frames in case of binary nav streaming - * \param connection_key Id of connection over which message is to be sent + * \param connection_key Unique key used by other components as session identifier * \param number_of_frames Number of frames processed by * streaming server and displayed to user. */ @@ -251,7 +258,7 @@ class ProtocolHandlerImpl * mobile app for using when ending session. * \param service_type Type of session: RPC or BULK Data. RPC by default */ - void SendEndSessionAck(ConnectionID connection_id , + void SendEndSessionAck(ConnectionID connection_id, uint8_t session_id, uint8_t protocol_version, uint8_t service_type); @@ -264,7 +271,7 @@ class ProtocolHandlerImpl * \param protocol_version Version of protocol used for communication * \param service_type Type of session: RPC or BULK Data. RPC by default */ - void SendEndSessionNAck(ConnectionID connection_id , + void SendEndSessionNAck(ConnectionID connection_id, uint32_t session_id, uint8_t protocol_version, uint8_t service_type); @@ -336,11 +343,11 @@ class ProtocolHandlerImpl * \param is_final_message if is_final_message = true - it is last message * \return \saRESULT_CODE Status of operation */ - RESULT_CODE SendSingleFrameMessage(ConnectionID connection_id, + RESULT_CODE SendSingleFrameMessage(const ConnectionID connection_id, const uint8_t session_id, - uint32_t protocol_version, + const uint32_t protocol_version, const uint8_t service_type, - size_t data_size, + const size_t data_size, const uint8_t *data, const bool is_final_message); @@ -357,9 +364,9 @@ class ProtocolHandlerImpl * \param is_final_message if is_final_message = true - it is last message * \return \saRESULT_CODE Status of operation */ - RESULT_CODE SendMultiFrameMessage(ConnectionID connection_id, + RESULT_CODE SendMultiFrameMessage(const ConnectionID connection_id, const uint8_t session_id, - uint32_t protocol_version, + const uint8_t protocol_version, const uint8_t service_type, const size_t data_size, const uint8_t *data, @@ -381,7 +388,7 @@ class ProtocolHandlerImpl * \return \saRESULT_CODE Status of operation */ RESULT_CODE HandleMessage( - ConnectionID connection_id , + ConnectionID connection_id, const ProtocolFramePtr packet); /** @@ -392,7 +399,7 @@ class ProtocolHandlerImpl * \return \saRESULT_CODE Status of operation */ RESULT_CODE HandleSingleFrameMessage( - ConnectionID connection_id , + ConnectionID connection_id, const ProtocolFramePtr packet); /** * \brief Handles message received in multiple frames. Collects all frames @@ -403,7 +410,7 @@ class ProtocolHandlerImpl * \return \saRESULT_CODE Status of operation */ RESULT_CODE HandleMultiFrameMessage( - ConnectionID connection_id , + ConnectionID connection_id, const ProtocolFramePtr packet); /** @@ -414,28 +421,21 @@ class ProtocolHandlerImpl * \return \saRESULT_CODE Status of operation */ RESULT_CODE HandleControlMessage( - ConnectionID connection_id , + ConnectionID connection_id, const ProtocolFramePtr packet); RESULT_CODE HandleControlMessageEndSession( - ConnectionID connection_id , + ConnectionID connection_id, const ProtocolPacket &packet); RESULT_CODE HandleControlMessageStartSession( - ConnectionID connection_id , + ConnectionID connection_id, const ProtocolPacket &packet); RESULT_CODE HandleControlMessageHeartBeat( - ConnectionID connection_id , + ConnectionID connection_id, const ProtocolPacket &packet); - /** - * \brief Sends Mobile Navi Ack message - */ - RESULT_CODE SendMobileNaviAck( - ConnectionID connection_id , - int32_t connection_key); - // threads::MessageLoopThread<*>::Handler implementations // CALLED ON raw_ford_messages_from_mobile_ thread! void Handle(const impl::RawFordMessageFromMobile message); @@ -450,6 +450,9 @@ class ProtocolHandlerImpl RESULT_CODE EncryptFrame(ProtocolFramePtr packet); RESULT_CODE DecryptFrame(ProtocolFramePtr packet); #endif // ENABLE_SECURITY + + bool TrackMessage(const uint32_t& connection_key); + private: /** *\brief Pointer on instance of class implementing IProtocolObserver @@ -474,7 +477,7 @@ class ProtocolHandlerImpl std::map<int32_t, ProtocolFramePtr> incomplete_multi_frame_messages_; /** - * \brief Map of messages (frames) recieved over mobile nave session + * \brief Map of messages (frames) received over mobile nave session * for map streaming. */ MessagesOverNaviMap message_over_navi_session_; @@ -487,10 +490,16 @@ class ProtocolHandlerImpl /** *\brief Counter of messages sent in each session. + * Used ad unique message identifier */ std::map<uint8_t, uint32_t> message_counters_; /** + *\brief Counter of messages sent in each session. + */ + std::map<ConnectionID, uint32_t> malformed_message_counters_; + + /** *\brief map for session last message. */ std::map<uint8_t, uint32_t> sessions_last_message_id_; @@ -500,9 +509,11 @@ class ProtocolHandlerImpl */ std::list<uint32_t> ready_to_close_connections_; - - class IncomingDataHandler; - std::auto_ptr<IncomingDataHandler> incoming_data_handler_; + ProtocolPacket::ProtocolHeaderValidator protocol_header_validator_; + IncomingDataHandler incoming_data_handler_; + // Use uint32_t as application identifier + utils::MessageMeter<uint32_t> message_meter_; + size_t message_max_frequency_; #ifdef ENABLE_SECURITY security_manager::SecurityManager *security_manager_; @@ -520,5 +531,4 @@ class ProtocolHandlerImpl #endif // TIME_TESTER }; } // namespace protocol_handler - #endif // SRC_COMPONENTS_PROTOCOL_HANDLER_INCLUDE_PROTOCOL_HANDLER_PROTOCOL_HANDLER_IMPL_H_ diff --git a/src/components/protocol_handler/include/protocol_handler/protocol_packet.h b/src/components/protocol_handler/include/protocol_handler/protocol_packet.h index 2e3d39fd47..0b4b253a84 100644 --- a/src/components/protocol_handler/include/protocol_handler/protocol_packet.h +++ b/src/components/protocol_handler/include/protocol_handler/protocol_packet.h @@ -35,67 +35,45 @@ #include "utils/macro.h" #include "protocol/common.h" -#include "protocol/common.h" +#include "transport_manager/common.h" /** *\namespace protocol_handlerHandler *\brief Namespace for SmartDeviceLink ProtocolHandler related functionality. */ namespace protocol_handler { + +typedef transport_manager::ConnectionUID ConnectionID; /** * \class ProtocolPacket * \brief Class for forming/parsing protocol headers of the message and * handling multiple frames of the message. */ class ProtocolPacket { - private: + public: /** * \struct ProtocolData * \brief Used for storing message and its size. */ struct ProtocolData { - ProtocolData() - : data(0), totalDataBytes(0x00) { - } + ProtocolData(); + ~ProtocolData(); uint8_t *data; uint32_t totalDataBytes; }; /** - * \struct ProtocolHeader + * \class ProtocolHeader * \brief Used for storing protocol header of a message. */ - struct ProtocolHeader { - /** - * \brief Constructor - */ - ProtocolHeader() - : version(0x00), - protection_flag(PROTECTION_OFF), - frameType(0x00), - serviceType(0x00), - frameData(0x00), - sessionId(0x00), - dataSize(0x00), - messageId(0x00) { - } - /** - * \brief Constructor - */ + class ProtocolHeader { + public: + ProtocolHeader(); ProtocolHeader(uint8_t version, bool protection, uint8_t frameType, uint8_t serviceType, uint8_t frameData, uint8_t sessionID, - uint32_t dataSize, uint32_t messageID) - : version(version), - protection_flag(protection), - frameType(frameType), - serviceType(serviceType), - frameData(frameData), - sessionId(sessionID), - dataSize(dataSize), - messageId(messageID) { - } + uint32_t dataSize, uint32_t messageID); uint8_t version; bool protection_flag; uint8_t frameType; @@ -104,9 +82,28 @@ class ProtocolPacket { uint8_t sessionId; uint32_t dataSize; uint32_t messageId; + void deserialize(const uint8_t *message, const size_t messageSize); + }; + /** + * \class ProtocolHeaderValidator + * \brief Used for ProtocolHeader validation + */ + class ProtocolHeaderValidator { + public: + ProtocolHeaderValidator(); + /** + * \brief Setter/getter maximum payload size of packets + */ + void set_max_payload_size(const size_t max_payload_size); + size_t max_payload_size() const; + /** + * \brief Check ProtocolHeader according to protocol requiements + */ + RESULT_CODE validate(const ProtocolHeader& header) const; + private: + size_t max_payload_size_; }; - public: /** * \brief Default constructor */ @@ -115,14 +112,9 @@ class ProtocolPacket { /** * \brief Constructor * - * \param connectionKey Identifier of connection within wich message - * is transferred * \param connection_id - Connection Identifier - * \param data Message string - * \param dataSize Message size */ - ProtocolPacket(uint8_t connection_id, uint8_t *data, - uint32_t dataSize); + explicit ProtocolPacket(ConnectionID connection_id); /** * \brief Constructor @@ -137,18 +129,12 @@ class ProtocolPacket { * \param dataSize Size of message string * \param messageID ID of message or hash code - only for second protocol * \param data Message string if provided - * \param packet_id - ID for multiframe messages */ - ProtocolPacket(uint8_t connection_id, + ProtocolPacket(ConnectionID connection_id, uint8_t version, bool protection, uint8_t frameType, uint8_t serviceType, uint8_t frameData, uint8_t sessionId, uint32_t dataSize, - uint32_t messageID, const uint8_t *data = 0, - uint32_t packet_id = 0); - /** - * \brief Destructor - */ - ~ProtocolPacket(); + uint32_t messageID, const uint8_t *data = 0); /*Serialization*/ /** @@ -171,14 +157,10 @@ class ProtocolPacket { */ size_t packet_size() const; - /** - * \brief Getter of message ID - * \return uint32_t message ID - */ - uint32_t packet_id() const; - /*End of Serialization*/ + bool operator==(const protocol_handler::ProtocolPacket& other) const; + /*Deserialization*/ /** @@ -189,7 +171,7 @@ class ProtocolPacket { * \return \saRESULT_CODE Status of serialization */ RESULT_CODE deserializePacket(const uint8_t *message, - uint32_t messageSize); + const size_t messageSize); /** * \brief Getter of protocol version. @@ -285,22 +267,16 @@ class ProtocolPacket { uint32_t payload_size_; /** - *\brief Offset for multiframe messages - */ - uint32_t data_offset_; - - /** - *\brief ID for multiframe messages - */ - uint32_t packet_id_; - - /** * \brief Connection Identifier * Obtained from connection_handler */ - uint8_t connection_id_; + ConnectionID connection_id_; DISALLOW_COPY_AND_ASSIGN(ProtocolPacket); }; } // namespace protocol_handler +/** + * @brief Type definition for variable that hold shared pointer to protocolol packet + */ +typedef utils::SharedPtr<protocol_handler::ProtocolPacket> ProtocolFramePtr; #endif // SRC_COMPONENTS_PROTOCOL_HANDLER_INCLUDE_PROTOCOL_HANDLER_PROTOCOL_PACKET_H_ |