summaryrefslogtreecommitdiff
path: root/src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h
blob: 3e7d64e9d297c3955092f872f6e49618bb32dd67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*
 * 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_PROTOCOL_HANDLER_IMPL_H_
#define SRC_COMPONENTS_PROTOCOL_HANDLER_INCLUDE_PROTOCOL_HANDLER_PROTOCOL_HANDLER_IMPL_H_

#include <map>
#include <memory>
#include <set>
#include <stdint.h>
#include <utility>  // std::make_pair
#include <vector>
#include "utils/prioritized_queue.h"
#include "utils/message_queue.h"
#include "utils/threads/message_loop_thread.h"

#include "utils/messagemeter.h"
#include "utils/custom_string.h"
#include "utils/semantic_version.h"

#include "protocol_handler/protocol_handler.h"
#include "protocol_handler/protocol_packet.h"
#include "protocol_handler/protocol_handler_settings.h"
#include "protocol_handler/session_observer.h"
#include "protocol_handler/protocol_observer.h"
#include "protocol_handler/incoming_data_handler.h"
#include "protocol_handler/multiframe_builder.h"
#include "transport_manager/common.h"
#include "transport_manager/transport_manager.h"
#include "transport_manager/transport_manager_listener_empty.h"
#include "transport_manager/transport_adapter/transport_adapter.h"
#include "connection_handler/connection_handler.h"
#include "application_manager/policies/policy_handler_observer.h"

#ifdef TELEMETRY_MONITOR
#include "protocol_handler/telemetry_observer.h"
#include "telemetry_monitor/telemetry_observable.h"
#endif  // TELEMETRY_MONITOR

#ifdef ENABLE_SECURITY
#include "security_manager/security_manager.h"
#include "protocol_handler/handshake_handler.h"
#endif  // ENABLE_SECURITY

namespace connection_handler {
class ConnectionHandlerImpl;
}  // namespace connection_handler

/**
 *\namespace protocol_handlerHandler
 *\brief Namespace for SmartDeviceLink ProtocolHandler related functionality.
 */
namespace protocol_handler {
class ProtocolObserver;
class SessionObserver;
class HandshakeHandler;

class MessagesFromMobileAppHandler;
class MessagesToMobileAppHandler;

using transport_manager::TransportManagerListenerEmpty;

typedef std::multimap<int32_t, RawMessagePtr> MessagesOverNaviMap;
typedef std::map<std::pair<ConnectionID, uint8_t>, ProtocolFramePtr>
    StartSessionFrameMap;
typedef std::set<ProtocolObserver*> ProtocolObservers;
typedef transport_manager::ConnectionUID ConnectionID;

namespace impl {
/*
 * These dummy classes are here to locally impose strong typing on different
 * kinds of messages
 * Currently there is no type difference between incoming and outgoing messages
 * TODO(ik): replace these with globally defined message types
 * when we have them.
 */
struct RawFordMessageFromMobile : public ProtocolFramePtr {
  RawFordMessageFromMobile() {}
  explicit RawFordMessageFromMobile(const ProtocolFramePtr message)
      : ProtocolFramePtr(message) {}
  // PrioritizedQueue requires this method to decide which priority to assign
  size_t PriorityOrder() const {
    return MessagePriority::FromServiceType(
               ServiceTypeFromByte(get()->service_type())).OrderingValue();
  }
};

struct RawFordMessageToMobile : public ProtocolFramePtr {
  RawFordMessageToMobile() : is_final(false) {}
  explicit RawFordMessageToMobile(const ProtocolFramePtr message,
                                  bool final_message)
      : ProtocolFramePtr(message), is_final(final_message) {}
  // PrioritizedQueue requires this method to decide which priority to assign
  size_t PriorityOrder() const {
    return MessagePriority::FromServiceType(
               ServiceTypeFromByte(get()->service_type())).OrderingValue();
  }
  // Signals whether connection to mobile must be closed after processing this
  // message
  bool is_final;
};

// Short type names for prioritized message queues
typedef threads::MessageLoopThread<
    utils::PrioritizedQueue<RawFordMessageFromMobile> > FromMobileQueue;
typedef threads::MessageLoopThread<
    utils::PrioritizedQueue<RawFordMessageToMobile> > ToMobileQueue;

// Type to allow easy mapping between a device type and transport
// characteristics
typedef enum {
  TT_NONE = -1,
  TT_USB = 0,
  TT_BLUETOOTH = 1,
  TT_WIFI = 2
} TransportType;

struct TransportDescription {
  TransportDescription(const TransportType transport_type,
                       const bool ios_transport,
                       const bool android_transport)
      : transport_type_(transport_type)
      , ios_transport_(ios_transport)
      , android_transport_(android_transport) {}

  TransportType transport_type_;
  bool ios_transport_;
  bool android_transport_;
};

typedef std::map<std::string, TransportDescription> TransportTypes;
}  // namespace impl

/**
 * \class ProtocolHandlerImpl
 * \brief Class for handling message exchange between Transport and higher
 * layers. Receives message in form of array of bytes, parses its protocol,
 * handles according to parsing results (version number, start/end session etc
 * and if needed passes message to JSON Handler or notifies Connection Handler
 * about activities around sessions.
 */
class ProtocolHandlerImpl
    : public ProtocolHandler,
      public TransportManagerListenerEmpty,
      public policy::PolicyHandlerObserver,
      public impl::FromMobileQueue::Handler,
      public impl::ToMobileQueue::Handler
#ifdef TELEMETRY_MONITOR
      ,
      public telemetry_monitor::TelemetryObservable<PHTelemetryObserver>
#endif  // TELEMETRY_MONITOR
      {
 public:
  /**
   * @brief Constructor
   * @param settings reference to ProtocolHandlerSettingsImpl object
   * @param session_observer reference to SessionObserver to access session
   * information and controll session life cycle
   * @param transportManager Pointer to Transport layer handler for
   */
  ProtocolHandlerImpl(const ProtocolHandlerSettings& settings,
                      protocol_handler::SessionObserver& session_observer,
                      connection_handler::ConnectionHandler& connection_handler,
                      transport_manager::TransportManager& transport_manager);
  /**
   * \brief Destructor
   */
  ~ProtocolHandlerImpl();

  /**
   * \brief Adds pointer to higher layer handler for message exchange
   * \param observer Pointer to object of the class implementing
   * IProtocolObserver
   */
  void AddProtocolObserver(ProtocolObserver* observer);

  void RemoveProtocolObserver(ProtocolObserver* observer) OVERRIDE;

#ifdef ENABLE_SECURITY
  /**
   * \brief Sets pointer for SecurityManager layer for managing protection
   * routine
   * \param security_manager Pointer to SecurityManager object
   */
  void set_security_manager(
      security_manager::SecurityManager* security_manager);
#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
   */
  void SendMessageToMobileApp(const RawMessagePtr message,
                              bool final_message) OVERRIDE;

  /**
   * \brief Sends number of processed frames in case of binary nav streaming
   * \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.
   */
  void SendFramesNumber(uint32_t connection_key, int32_t number_of_frames);

#ifdef TELEMETRY_MONITOR
  /**
   * @brief Setup observer for time metric.
   *
   * @param observer - pointer to observer
   */
  void SetTelemetryObserver(PHTelemetryObserver* observer);
#endif  // TELEMETRY_MONITOR

  /*
   * Prepare and send heartbeat message to mobile
   */
  void SendHeartBeat(int32_t connection_id, uint8_t session_id);

  /**
    * \brief Sends ending session to mobile application
    * \param connection_id Identifier of connection within which
    * session exists
    * \param session_id ID of session to be ended
    */
  void SendEndSession(int32_t connection_id, uint8_t session_id);

  /**
    * \brief Sends ending session to mobile application
    * \param primary_connection_id Identifier of connection within which
    * service exists
    * \param connection_id Identifier of the actual transport for the service
    * \param session_id ID of session to be ended
    */
  void SendEndService(int32_t primary_connection_id,
                      int32_t connection_id,
                      uint8_t session_id,
                      uint8_t service_type);

  void NotifyOnFailedHandshake() OVERRIDE;

  // TODO(Ezamakhov): move Ack/Nack as interface for StartSessionHandler
  /**
   * \brief Sends acknowledgement of starting session to mobile application
   * with session number and hash code for second version of protocol
   * was started
   * \param connection_id Identifier of connection within which session
   * \param session_id ID of session to be sent to mobile application
   * \param protocol_version Version of protocol used for communication
   * \param hash_code For second version of protocol: identifier of session
   * to be sent to
   * mobile app for using when ending session
   * \param service_type Type of session: RPC or BULK Data. RPC by default
   * \param protection Protection flag
   */
  void SendStartSessionAck(ConnectionID connection_id,
                           uint8_t session_id,
                           uint8_t input_protocol_version,
                           uint32_t hash_code,
                           uint8_t service_type,
                           bool protection);

  /**
   * \brief Sends acknowledgement of starting session to mobile application
   * with session number and hash code for second version of protocol
   * was started
   * \param connection_id Identifier of connection within which session
   * \param session_id ID of session to be sent to mobile application
   * \param protocol_version Version of protocol used for communication
   * \param hash_code For second version of protocol: identifier of session
   * to be sent to
   * mobile app for using when ending session
   * \param service_type Type of session: RPC or BULK Data. RPC by default
   * \param protection Protection flag
   * \param full_version full protocol version (major.minor.patch) used by the
   *                     mobile proxy
   */
  void SendStartSessionAck(ConnectionID connection_id,
                           uint8_t session_id,
                           uint8_t protocol_version,
                           uint32_t hash_code,
                           uint8_t service_type,
                           bool protection,
                           utils::SemanticVersion& full_version);

  /**
   * \brief Sends acknowledgement of starting session to mobile application
   * with session number and hash code for second version of protocol
   * was started
   * \param connection_id Identifier of connection within which session
   * \param session_id ID of session to be sent to mobile application
   * \param protocol_version Version of protocol used for communication
   * \param hash_code For second version of protocol: identifier of session
   * to be sent to
   * mobile app for using when ending session
   * \param service_type Type of session: RPC or BULK Data. RPC by default
   * \param protection Protection flag
   * \param full_version full protocol version (major.minor.patch) used by the
   *                     mobile proxy
   * \param params Parameters added in the payload
   */
  void SendStartSessionAck(ConnectionID connection_id,
                           uint8_t session_id,
                           uint8_t protocol_version,
                           uint32_t hash_code,
                           uint8_t service_type,
                           bool protection,
                           utils::SemanticVersion& full_version,
                           BsonObject& params);

  const ProtocolHandlerSettings& get_settings() const OVERRIDE {
    return settings_;
  }

  /**
   * \brief Sends fail of starting session to mobile application
   * \param connection_id Identifier of connection within which session
   * \param session_id ID of session to be sent to mobile application
   * \param protocol_version Version of protocol used for communication
   * \param service_type Type of session: RPC or BULK Data. RPC by default
   */
  void SendStartSessionNAck(ConnectionID connection_id,
                            uint8_t session_id,
                            uint8_t protocol_version,
                            uint8_t service_type);

  /**
   * \brief Sends fail of starting session to mobile application
   * \param connection_id Identifier of connection within which session
   * \param session_id ID of session to be sent to mobile application
   * \param protocol_version Version of protocol used for communication
   * \param service_type Type of session: RPC or BULK Data. RPC by default
   * \param rejected_params List of rejected params to send in payload
   */
  void SendStartSessionNAck(ConnectionID connection_id,
                            uint8_t session_id,
                            uint8_t protocol_version,
                            uint8_t service_type,
                            std::vector<std::string>& rejectedParams);

  /**
   * \brief Sends acknowledgement of end session/service to mobile application
   * with session number for second version of protocol.
   * \param connection_id Identifier of connection
   * \param connection_handle Identifier of connection within which session
   * was started
   * \param session_id ID of session to be sent to mobile application
   * \param protocol_version Version of protocol used for communication
   * 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,
                         uint8_t session_id,
                         uint8_t protocol_version,
                         uint8_t service_type);

  /**
   * \brief Sends fail of ending session to mobile application
   * \param connection_id Identifier of connection within which
   * session exists
   * \param session_id ID of session ment to be ended
   * \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,
                          uint32_t session_id,
                          uint8_t protocol_version,
                          uint8_t service_type);
  /**
   * \brief Sends fail of ending session to mobile application (variant for
   * Protocol v5)
   * \param connection_id Identifier of connection within which
   * session exists
   * \param session_id ID of session ment to be ended
   * \param protocol_version Version of protocol used for communication
   * \param service_type Type of session: RPC or BULK Data. RPC by default
   * \param rejected_params List of rejected params to send in payload
   */
  void SendEndSessionNAck(ConnectionID connection_id,
                          uint32_t session_id,
                          uint8_t protocol_version,
                          uint8_t service_type,
                          std::vector<std::string>& rejected_params);

  SessionObserver& get_session_observer() OVERRIDE;

  /**
   * @brief Called by connection handler to notify the result of
   * OnSessionStartedCallback().
   * @param context reference to structure with started session data
   * @param rejected_params list of parameters name that are rejected.
   * Only valid when generated_session_id is 0. Note, even if
   * generated_session_id is 0, the list may be empty.
   */
  void NotifySessionStarted(const SessionContext& context,
                            std::vector<std::string>& rejected_params) OVERRIDE;

#ifdef BUILD_TESTS
  const impl::FromMobileQueue& get_from_mobile_queue() const {
    return raw_ford_messages_from_mobile_;
  }

  const impl::ToMobileQueue& get_to_mobile_queue() const {
    return raw_ford_messages_to_mobile_;
  }

  void set_tcp_config(bool tcp_enabled,
                      std::string tcp_address,
                      std::string tcp_port) {
    tcp_enabled_ = tcp_enabled;
    tcp_ip_address_ = tcp_address;
    tcp_port_ = tcp_port;
  }
#endif

 private:
  void SendEndServicePrivate(int32_t primary_connection_id,
                             int32_t connection_id,
                             uint8_t session_id,
                             uint8_t service_type);

  /*
   * Prepare and send heartbeat acknowledge message
   */
  RESULT_CODE SendHeartBeatAck(ConnectionID connection_id,
                               uint8_t session_id,
                               uint32_t message_id);

  /*
   * Prepare and send TransportUpdateEvent message
   */
  void SendTransportUpdateEvent(ConnectionID connection_id, uint8_t session_id);

  /*
   * Prepare and send RegisterSecondaryTransportAck message
   */
  RESULT_CODE SendRegisterSecondaryTransportAck(
      ConnectionID connection_id,
      ConnectionID primary_transport_connection_id,
      uint8_t session_id);

  /*
   * Prepare and send RegisterSecondaryTransportNAck message
   */
  RESULT_CODE SendRegisterSecondaryTransportNAck(
      ConnectionID connection_id,
      ConnectionID primary_transport_connection_id,
      uint8_t session_id,
      BsonObject* reason = NULL);

  /**
   * @brief Notifies about receiving message from TM.
   *
   * @param message Received message
   **/
  void OnTMMessageReceived(const RawMessagePtr message) OVERRIDE;

  /**
   * @brief Notifies about error on receiving message from TM.
   *
   * @param error Occurred error
   **/
  void OnTMMessageReceiveFailed(
      const transport_manager::DataReceiveError& error) OVERRIDE;

  /**
   * @brief Notifies about successfully sending message.
   *
   **/
  void OnTMMessageSend(const RawMessagePtr message) OVERRIDE;

  /**
   * @brief Notifies about error occurred during
   * sending message.
   *
   * @param error Describes occurred error.
   * @param message Message during sending which error occurred.
   **/
  void OnTMMessageSendFailed(const transport_manager::DataSendError& error,
                             const RawMessagePtr message) OVERRIDE;

  void OnConnectionEstablished(
      const transport_manager::DeviceInfo& device_info,
      const transport_manager::ConnectionUID connection_id) OVERRIDE;

  void OnConnectionClosed(
      const transport_manager::ConnectionUID connection_id) OVERRIDE;

  void OnUnexpectedDisconnect(
      const transport_manager::ConnectionUID connection_id,
      const transport_manager::CommunicationError& error) OVERRIDE;

  /**
   * @brief Notifies that configuration of a transport has been updated.
   *
   * @param configs pairs of key and value that represent configuration.
   */
  void OnTransportConfigUpdated(
      const transport_manager::transport_adapter::TransportConfig& configs)
      OVERRIDE;

  /**
   * @brief Notifies subscribers about message
   * received from mobile device.
   * @param message Message with already parsed header.
   */
  void NotifySubscribers(const RawMessagePtr message);

  /**
   * \brief Sends message which size permits to send it in one frame.
   * \param connection_handle Identifier of connection through which message
   * is to be sent.
   * \param session_id ID of session through which message is to be sent.
   * \param protocol_version Version of Protocol used in message.
   * \param service_type Type of session, RPC or BULK Data
   * \param data_size Size of message excluding protocol header
   * \param data Message string
   * \param is_final_message if is_final_message = true - it is last message
   * \return \saRESULT_CODE Status of operation
   */
  RESULT_CODE SendSingleFrameMessage(const ConnectionID connection_id,
                                     const uint8_t session_id,
                                     const uint32_t protocol_version,
                                     const uint8_t service_type,
                                     const size_t data_size,
                                     const uint8_t* data,
                                     const bool is_final_message);

  /**
   * \brief Sends message which size doesn't permit to send it in one frame.
   * \param connection_handle Identifier of connection through which message
   * is to be sent.
   * \param session_id ID of session through which message is to be sent.
   * \param protocol_version Version of Protocol used in message.
   * \param service_type Type of session, RPC or BULK Data
   * \param data_size Size of message excluding protocol header
   * \param data Message string
   * \param max_data_size Maximum allowed size of single frame.
   * \param is_final_message if is_final_message = true - it is last message
   * \return \saRESULT_CODE Status of operation
   */
  RESULT_CODE SendMultiFrameMessage(const ConnectionID connection_id,
                                    const uint8_t session_id,
                                    const uint8_t protocol_version,
                                    const uint8_t service_type,
                                    const size_t data_size,
                                    const uint8_t* data,
                                    const size_t max_frame_size,
                                    const bool is_final_message);

  /**
   * \brief Sends message already containing protocol header.
   * \param packet Message with protocol header
   * \return \saRESULT_CODE Status of operation
   */
  RESULT_CODE SendFrame(const ProtocolFramePtr packet);

  /**
   * \brief Handles received message.
   * \param connection_handle Identifier of connection through which message
   * is received.
   * \param packet Received message with protocol header.
   * \return \saRESULT_CODE Status of operation
   */
  RESULT_CODE HandleMessage(const ProtocolFramePtr packet);

  /**
   * \brief Handles message received in single frame.
   * \param connection_handle Identifier of connection through which message
   * is received.
   * \param packet Frame of message with protocol header.
   * \return \saRESULT_CODE Status of operation
   */
  RESULT_CODE HandleSingleFrameMessage(const ProtocolFramePtr packet);
  /**
   * \brief Handles message received in multiple frames. Collects all frames
   * of message.
   * \param connection_handle Identifier of connection through which message
   * is received.
   * \param packet Current frame of message with protocol header.
   * \return \saRESULT_CODE Status of operation
   */
  RESULT_CODE HandleMultiFrameMessage(const ProtocolFramePtr packet);

  /**
   * \brief Handles message received in single frame.
   * \param connection_handle Identifier of connection through which message
   * is received.
   * \param packet Received message with protocol header.
   * \return \saRESULT_CODE Status of operation
   */
  RESULT_CODE HandleControlMessage(const ProtocolFramePtr packet);

  RESULT_CODE HandleControlMessageEndSession(const ProtocolPacket& packet);

  RESULT_CODE HandleControlMessageEndServiceACK(const ProtocolPacket& packet);

  RESULT_CODE HandleControlMessageStartSession(const ProtocolFramePtr packet);

  RESULT_CODE HandleControlMessageRegisterSecondaryTransport(
      const ProtocolFramePtr packet);

  RESULT_CODE HandleControlMessageHeartBeat(const ProtocolPacket& packet);

  void PopValideAndExpirateMultiframes();

  // threads::MessageLoopThread<*>::Handler implementations
  // CALLED ON raw_ford_messages_from_mobile_ thread!
  void Handle(const impl::RawFordMessageFromMobile message);
  // CALLED ON raw_ford_messages_to_mobile_ thread!
  void Handle(const impl::RawFordMessageToMobile message);

#ifdef ENABLE_SECURITY
  /**
   * \brief Encryption/Decryption methodes for SecureSecvice check
   * \param packet frame of message to encrypted/decrypted
   */
  RESULT_CODE EncryptFrame(ProtocolFramePtr packet);
  RESULT_CODE DecryptFrame(ProtocolFramePtr packet);
#endif  // ENABLE_SECURITY

  bool TrackMessage(const uint32_t& connection_key);

  bool TrackMalformedMessage(const uint32_t& connection_key,
                             const size_t count);
  /**
   * @brief Function returns supported SDL Protocol Version,
   */
  uint8_t SupportedSDLProtocolVersion() const;

  const impl::TransportDescription GetTransportTypeFromConnectionType(
      const std::string& device_type) const;

  const bool ParseSecondaryTransportConfiguration(
      const ConnectionID connection_id,
      std::vector<std::string>& secondaryTransports,
      std::vector<int32_t>& audioServiceTransports,
      std::vector<int32_t>& videoServiceTransports) const;

  void GenerateSecondaryTransportsForStartSessionAck(
      const std::vector<std::string>& secondary_transport_types,
      bool device_is_ios,
      bool device_is_android,
      std::vector<std::string>& secondaryTransports) const;

  void GenerateServiceTransportsForStartSessionAck(
      bool secondary_enabled,
      const std::vector<std::string>& service_transports,
      const std::string& primary_connection_type,
      const impl::TransportType primary_transport_type,
      const std::vector<std::string>& secondary_transport_types,
      std::vector<int32_t>& serviceTransports) const;

  const std::string TransportTypeFromTransport(
      const utils::custom_string::CustomString& transport) const;

  const ProtocolHandlerSettings& settings_;

  /**
   *\brief Pointer on instance of class implementing IProtocolObserver
   *\brief (JSON Handler)
   */
  ProtocolObservers protocol_observers_;

  /**
   *\brief Pointer on instance of class implementing ISessionObserver
   *\brief (Connection Handler).
   * According to usage it can't be null
   */
  SessionObserver& session_observer_;

  /**
   *\brief Pointer on instance of Transport layer handler for message exchange.
   *\brief Instance of class implementing Connection  Handler t keep connection
   *alive
   */
  connection_handler::ConnectionHandler& connection_handler_;

  /**
   *\brief Pointer on instance of Transport layer handler for message exchange.
   */
  transport_manager::TransportManager& transport_manager_;

  /**
   *\brief Assembling support class.
   */
  MultiFrameBuilder multiframe_builder_;

  /**
   * \brief Map of messages (frames) received over mobile nave session
   * for map streaming.
   */
  MessagesOverNaviMap message_over_navi_session_;

  /**
   * \brief Untill specified otherwise, amount of message recievied
   * over streaming session to send Ack
   */
  const uint32_t kPeriodForNaviAck;

  /**
   *\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_;

  /**
   *\brief Connections that must be closed after their last messages were sent
   */
  std::vector<uint32_t> ready_to_close_connections_;

  ProtocolPacket::ProtocolHeaderValidator protocol_header_validator_;
  IncomingDataHandler incoming_data_handler_;
  // Use uint32_t as application identifier
  utils::MessageMeter<uint32_t> message_meter_;
  // Use uint32_t as connection identifier
  utils::MessageMeter<uint32_t> malformed_message_meter_;

#ifdef ENABLE_SECURITY
  security_manager::SecurityManager* security_manager_;
#endif  // ENABLE_SECURITY

  // Thread that pumps non-parsed messages coming from mobile side.
  impl::FromMobileQueue raw_ford_messages_from_mobile_;
  // Thread that pumps messages prepared to being sent to mobile side.
  impl::ToMobileQueue raw_ford_messages_to_mobile_;

  sync_primitives::Lock protocol_observers_lock_;

  sync_primitives::Lock start_session_frame_map_lock_;
  StartSessionFrameMap start_session_frame_map_;

  bool tcp_enabled_;
  std::string tcp_port_;
  std::string tcp_ip_address_;

#ifdef TELEMETRY_MONITOR
  PHTelemetryObserver* metric_observer_;
#endif  // TELEMETRY_MONITOR
};
}  // namespace protocol_handler
#endif  // SRC_COMPONENTS_PROTOCOL_HANDLER_INCLUDE_PROTOCOL_HANDLER_PROTOCOL_HANDLER_IMPL_H_