summaryrefslogtreecommitdiff
path: root/SDL_Core/src/components/application_manager/include/application_manager/application_manager_impl.h
blob: fdb6894c9cb76e3d64903ee11508b84b940b74bf (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
/**
 * 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_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_H_
#define SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_H_

#include <cstdint>
#include <vector>
#include <map>
#include <set>
#include "application_manager/hmi_command_factory.h"
#include "application_manager/application_manager.h"
#include "application_manager/hmi_capabilities.h"
#include "application_manager/message.h"
#include "application_manager/policies_manager/policies_manager.h"
#include "application_manager/request_controller.h"
#include "application_manager/resume_ctrl.h"
#include "protocol_handler/protocol_observer.h"
#include "hmi_message_handler/hmi_message_observer.h"

#include "media_manager/media_manager_impl.h"

#include "connection_handler/connection_handler_observer.h"
#include "connection_handler/device.h"

#include "formatters/CSmartFactory.hpp"

#include "interfaces/HMI_API.h"
#include "interfaces/HMI_API_schema.h"
#include "interfaces/MOBILE_API_schema.h"

#include "interfaces/v4_protocol_v1_2_no_extra.h"
#include "interfaces/v4_protocol_v1_2_no_extra_schema.h"

#include "protocol_handler/service_type.h"

#include "utils/macro.h"
#include "utils/logger.h"
#include "utils/shared_ptr.h"
#include "utils/message_queue.h"
#include "utils/prioritized_queue.h"
#include "utils/threads/thread.h"
#include "utils/threads/message_loop_thread.h"
#include "utils/lock.h"
#include "utils/singleton.h"

namespace NsSmartDeviceLink {
namespace NsSmartObjects {
class SmartObject;
}
}

namespace smart_objects = NsSmartDeviceLink::NsSmartObjects;

namespace threads {
class Thread;
}
class CommandNotificationImpl;

namespace application_manager {
namespace mobile_api = mobile_apis;

class ApplicationManagerImpl;

namespace impl {
using namespace threads;

/*
 * 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
 * And due to ApplicationManagerImpl works as message router it has to distinguish
 * messages passed from it's different connection points
 * TODO(ik): replace these with globally defined message types
 * when we have them.
 */
struct MessageFromMobile: public utils::SharedPtr<Message> {
  explicit MessageFromMobile(const utils::SharedPtr<Message>& message):
    utils::SharedPtr<Message>(message) {}
  // PrioritizedQueue requres this method to decide which priority to assign
  size_t PriorityOrder() const { return (*this)->Priority().OrderingValue(); }
};

struct MessageToMobile: public utils::SharedPtr<Message> {
  explicit MessageToMobile(const utils::SharedPtr<Message>& message,
                           bool final_message):
    utils::SharedPtr<Message>(message), is_final(final_message) {}
  // PrioritizedQueue requres this method to decide which priority to assign
  size_t PriorityOrder() const { return (*this)->Priority().OrderingValue(); }
  // Signals if connection to mobile must be closed after sending this message
  bool is_final;
 };

struct MessageFromHmi: public utils::SharedPtr<Message> {
  explicit MessageFromHmi(const utils::SharedPtr<Message>& message):
    utils::SharedPtr<Message>(message) {}
  // PrioritizedQueue requres this method to decide which priority to assign
  size_t PriorityOrder() const { return (*this)->Priority().OrderingValue(); }
};

struct MessageToHmi: public utils::SharedPtr<Message> {
  explicit MessageToHmi(const utils::SharedPtr<Message>& message):
    utils::SharedPtr<Message>(message) {}
  // PrioritizedQueue requres this method to decide which priority to assign
  size_t PriorityOrder() const { return (*this)->Priority().OrderingValue(); }
};

// Short type names for proiritized message queues
typedef threads::MessageLoopThread<
    utils::PrioritizedQueue<MessageFromMobile> > FromMobileQueue;
typedef threads::MessageLoopThread<
    utils::PrioritizedQueue<MessageToMobile> > ToMobileQueue;
typedef threads::MessageLoopThread<
    utils::PrioritizedQueue<MessageFromHmi> > FromHmiQueue;
typedef threads::MessageLoopThread<
    utils::PrioritizedQueue<MessageToHmi> > ToHmiQueue;
}

class ApplicationManagerImpl : public ApplicationManager,
  public hmi_message_handler::HMIMessageObserver,
  public protocol_handler::ProtocolObserver,
  public connection_handler::ConnectionHandlerObserver,
  public impl::FromMobileQueue::Handler,
  public impl::ToMobileQueue::Handler,
  public impl::FromHmiQueue::Handler,
  public impl::ToHmiQueue::Handler,
  public utils::Singleton<ApplicationManagerImpl> {
  friend class ResumeCtrl;
  public:
    ~ApplicationManagerImpl();

    /**
     * @brief Stop work.
     *
     * @return TRUE on success otherwise FALSE.
     **/
    virtual bool Stop();

    /////////////////////////////////////////////////////

    ApplicationSharedPtr application(int32_t app_id) const;
    inline const std::set<ApplicationSharedPtr>& applications() const;
    ApplicationSharedPtr active_application() const;
    std::vector<ApplicationSharedPtr> applications_by_button(uint32_t button);
    std::vector<ApplicationSharedPtr> applications_by_ivi(uint32_t vehicle_info);
    std::vector<ApplicationSharedPtr> applications_with_navi();

    /////////////////////////////////////////////////////

    HMICapabilities& hmi_capabilities();

    ApplicationSharedPtr RegisterApplication(
      const utils::SharedPtr<smart_objects::SmartObject>& request_for_registration);
    /*
     * @brief Closes application by id
     *
     * @param app_id Application id
     * @param is_resuming describes - is this unregister
     *        is normal or need to be resumed
     */
    void UnregisterApplication(const uint32_t& app_id, bool is_resuming = false);

    /*
     * @brief Sets unregister reason for closing all registered applications
     * duringHU switching off
     *
     * @param reason Describes the reason for HU switching off
     */
    void SetUnregisterAllApplicationsReason(
        mobile_api::AppInterfaceUnregisteredReason::eType reason);

    /*
     * @brief Closes all registered applications
     */
    void UnregisterAllApplications();

    bool RemoveAppDataFromHMI(ApplicationSharedPtr app);
    bool LoadAppDataToHMI(ApplicationSharedPtr app);
    bool ActivateApplication(ApplicationSharedPtr app);
    /**
     * @brief Put application in Limited HMI Level if possible,
     *        otherwise put applicatuion other HMI level.
     *        do not send any notifications to mobile
     * @param app, application, that need to be puted in Limeted
     * @return seted HMI Level
     */
    mobile_api::HMILevel::eType PutApplicationInLimited(ApplicationSharedPtr app);

    /**
     * @brief Put application in FULL HMI Level if possible,
     *        otherwise put applicatuion other HMI level.
     *        do not send any notifications to mobile
     * @param app, application, that need to be puted in FULL
     * @return seted HMI Level
     */
    mobile_api::HMILevel::eType PutApplicationInFull(ApplicationSharedPtr app);

    void DeactivateApplication(ApplicationSharedPtr app);
    void ConnectToDevice(uint32_t id);
    void OnHMIStartedCooperation();

    /*
     * @brief Returns unique correlation ID for HMI request
     *
     * @return Unique correlation ID
     */
    uint32_t GetNextHMICorrelationID();

    /* @brief Starts audio passthru process
     *
     * @return true on success, false if passthru is already in process
     */
    bool begin_audio_pass_thru();

    /*
     * @brief Finishes already started audio passthru process
     *
     * @return true on success, false if passthru is not active
     */
    bool end_audio_pass_thru();

    /*
     * @brief Retrieves driver distraction state
     *
     * @return Current state of the distraction state
     */
    inline bool driver_distraction() const;

    /*
     * @brief Sets state for driver distraction
     *
     * @param state New state to be set
     */
    void set_driver_distraction(bool is_distracting);

    /*
     * @brief Retrieves if VR session has started
     *
     * @return Current VR session state (started, stopped)
     */
    inline bool vr_session_started() const;

    /*
     * @brief Sets VR session state
     *
     * @param state Current HMI VR session state
     */
    void set_vr_session_started(const bool& state);

    /*
     * @brief Retrieves SDL access to all mobile apps
     *
     * @return Currently active state of the access
     */
    inline bool all_apps_allowed() const;

    /*
     * @brief Sets SDL access to all mobile apps
     *
     * @param allowed SDL access to all mobile apps
     */
    void set_all_apps_allowed(const bool& allowed);

    /*
     * @brief Starts audio pass thru thread
     *
     * @param session_key     Session key of connection for Mobile side
     * @param correlation_id  Correlation id for response for Mobile side
     * @param max_duration    Max duration of audio recording in milliseconds
     * @param sampling_rate   Value for rate(8, 16, 22, 44 kHz)
     * @param bits_per_sample The quality the audio is recorded.
     * @param audio_type      Type of audio data
     */
    void StartAudioPassThruThread(int32_t session_key, int32_t correlation_id,
                                  int32_t max_duration, int32_t sampling_rate,
                                  int32_t bits_per_sample, int32_t audio_type);

    /*
     * @brief Terminates audio pass thru thread
     * @param application_key Id of application for which
     * audio pass thru should be stopped
     */
    void StopAudioPassThru(int32_t application_key);

    void SendAudioPassThroughNotification(uint32_t session_key,
                                          std::vector<uint8_t> binaryData);

    std::string GetDeviceName(connection_handler::DeviceHandle handle);

    /////////////////////////////////////////////////////

    void set_hmi_message_handler(hmi_message_handler::HMIMessageHandler* handler);
    void set_connection_handler(connection_handler::ConnectionHandler* handler);
    virtual void set_policy_manager(policies::PolicyManager* policy_manager);
    void set_protocol_handler(protocol_handler::ProtocolHandler* handler);

    ///////////////////////////////////////////////////////

    void StartDevicesDiscovery();

    // Put message to the queue to be sent to mobile.
    // if |final_message| parameter is set connection to mobile will be closed
    // after processing this message
    void SendMessageToMobile(
      const utils::SharedPtr<smart_objects::SmartObject>& message, bool final_message = false);
    bool ManageMobileCommand(
      const utils::SharedPtr<smart_objects::SmartObject>& message);
    void SendMessageToHMI(
      const utils::SharedPtr<smart_objects::SmartObject>& message);
    bool ManageHMICommand(
      const utils::SharedPtr<smart_objects::SmartObject>& message);

    /////////////////////////////////////////////////////////
    /*
     * @brief Overriden ProtocolObserver method
     */
    virtual void OnMessageReceived(const protocol_handler::
                                   RawMessagePtr& message);

    /*
     * @brief Overriden ProtocolObserver method
     */
    virtual void OnMobileMessageSent(const protocol_handler::
                                     RawMessagePtr& message);

    void OnMessageReceived(
      hmi_message_handler::MessageSharedPointer message);
    void OnErrorSending(hmi_message_handler::MessageSharedPointer message);

    void OnDeviceListUpdated(const connection_handler::DeviceList& device_list);
    void RemoveDevice(const connection_handler::DeviceHandle& device_handle);
    bool OnServiceStartedCallback(const connection_handler::DeviceHandle& device_handle,
                                  const int32_t& session_key,
                                  const protocol_handler::ServiceType& type);
    void OnServiceEndedCallback(const int32_t& session_key,
                                const protocol_handler::ServiceType& type);

    /**
     * @ Add notification to collection
     *
     * @param ptr Reference to shared pointer that point on hmi notification
     */
    void addNotification(const CommandSharedPtr& ptr);

    /**
     * @ Add notification to collection
     *
     * @param ptr Reference to shared pointer that point on hmi notification
     */
    void removeNotification(const CommandSharedPtr& ptr);

    /**
     * @ Updates request timeout
     *
     * @param connection_key Connection key of application
     * @param mobile_correlation_id Correlation ID of the mobile request
     * @param new_timeout_value New timeout to be set
     */
    void updateRequestTimeout(uint32_t connection_key,
                              uint32_t mobile_correlation_id,
                              uint32_t new_timeout_value);

    /*
     * @brief Retrieves application id associated whith correlation id
     *
     * @param correlation_id Correlation ID of the HMI request
     *
     * @return application id associated whith correlation id
     */
    const uint32_t application_id(const int32_t correlation_id);

    /*
     * @brief Sets application id correlation id
     *
     * @param correlation_id Correlation ID of the HMI request
     * @param app_id Application ID
     */
    void set_application_id(const int32_t correlation_id,
                            const uint32_t app_id);

    /*
     * @brief Change AudioStreamingState for all application according to
     * system audio-mixing capabilities (NOT_AUDIBLE/ATTENUATED) and
     * send notification for this changes
     */
    void Mute();

    /*
     * @brief Change AudioStreamingState for all application to AUDIBLE and
     * send notification for this changes
     */
    void Unmute();

    /*
     * @brief Checks HMI level and returns true if audio streaming is allowed
     */
    bool IsAudioStreamingAllowed(uint32_t connection_key) const;

    /*
     * @brief Checks HMI level and returns true if video streaming is allowed
     */
    bool IsVideoStreamingAllowed(uint32_t connection_key) const;

    /**
      * Getter for resume_controller
      * @return Resume Controller
      */
    ResumeCtrl& resume_controller() { return resume_ctrl_; }

    /*
     * @brief Save binary data to specified directory
     *
     * @param application name
     * @param binary file name
     * @param binary data
     * @param path for saving data
     * @param offset for saving data to existing file with offset. If offset is 0 - create new file ( overrite existing )
     *
     * @return SUCCESS if file was saved, other code otherwise
     */
    mobile_apis::Result::eType SaveBinary(
                                const std::string& app_name,
                                const std::vector<uint8_t>& binary_data,
                                const std::string& save_path,
                                const uint32_t offset = 0);

  private:
    ApplicationManagerImpl();
    bool InitThread(threads::Thread* thread);
    hmi_apis::HMI_API& hmi_so_factory();
    mobile_apis::MOBILE_API& mobile_so_factory();

    void CreateHMIMatrix(HMIMatrix* matrix);
    void CreatePoliciesManager(PoliciesManager* managaer);

    /**
     * \brief Performs check using PoliciesManager of availability
     * of the message for the application. If error occured it is sent
     * as response to initiator of request.
     * \param message Message received for application
     * \param application Application that recieved message to be checked by policies
     * \return bool Indicates whether message is allowed for application
     */
    bool CheckPolicies(smart_objects::SmartObject* message,
                       ApplicationSharedPtr app);

    /**
     * \brief Using HMIMatrix checks which messages sent to HMI are of higher priority
     * and acts accordingly (closes message with lower priority,
     * rejects message in case message with higher priority is operating on HMI).
     * If error occured it is sent as response to initiator of request.
     * \param message Message received for application
     * \return bool Indicates whether message is allowed for application
     */
    bool CheckHMIMatrix(smart_objects::SmartObject* message);

    bool ConvertMessageToSO(const Message& message,
                            smart_objects::SmartObject& output);
    bool ConvertSOtoMessage(const smart_objects::SmartObject& message,
                            Message& output);
    utils::SharedPtr<Message> ConvertRawMsgToMessage(
      const protocol_handler::RawMessagePtr& message);

    void ProcessMessageFromMobile(const utils::SharedPtr<Message>& message);
    void ProcessMessageFromHMI(const utils::SharedPtr<Message>& message);

    // threads::MessageLoopThread<*>::Handler implementations
    /*
     * @brief Handles for threads pumping different types
     * of messages. Beware, each is called on different thread!
     */
    // CALLED ON messages_from_mobile_ thread!
    virtual void Handle(const impl::MessageFromMobile& message) OVERRIDE;

    // CALLED ON messages_to_mobile_ thread!
    virtual void Handle(const impl::MessageToMobile& message) OVERRIDE;

    // CALLED ON messages_from_hmi_ thread!
    virtual void Handle(const impl::MessageFromHmi& message) OVERRIDE;

    // CALLED ON messages_to_hmi_ thread!
    virtual void Handle(const impl::MessageToHmi& message) OVERRIDE;

  private:


    // members
    ResumeCtrl resume_ctrl_;

    /**
     * @brief Resume controler is responcible for save and load information
     * about persistent application data on disk, and save session ID for resuming
     * application in case INGITION_OFF or MASTER_RESSET
     */

    /**
     * @brief Map of connection keys and associated applications
     */
    std::map<int32_t, ApplicationSharedPtr> applications_;

    /**
     * @brief List of applications
     */
    std::set<ApplicationSharedPtr> application_list_;

    /**
     * @brief Set of HMI notifications with timeout.
     */
    std::list<CommandSharedPtr> notification_list_;

    /**
     * @brief Map of correlation id  and associated application id.
     */
    std::map<const int32_t, const uint32_t> appID_list_;

    bool audio_pass_thru_active_;
    sync_primitives::Lock audio_pass_thru_lock_;
    bool is_distracting_driver_;
    bool is_vr_session_strated_;
    bool hmi_cooperating_;
    bool is_all_apps_allowed_;
    media_manager::MediaManager* media_manager_;

    hmi_message_handler::HMIMessageHandler* hmi_handler_;
    connection_handler::ConnectionHandler*  connection_handler_;
    protocol_handler::ProtocolHandler*      protocol_handler_;
    policies::PolicyManager*                policy_manager_;
    request_controller::RequestController   request_ctrl_;
    HMICapabilities                         hmi_capabilities_;

    // TODO(YS): Remove old implementation
    policies_manager::PoliciesManager policies_manager_;

    hmi_apis::HMI_API*                      hmi_so_factory_;
    mobile_apis::MOBILE_API*                mobile_so_factory_;

    static log4cxx::LoggerPtr logger_;

    static uint32_t corelation_id_;
    static const uint32_t max_corelation_id_;

    // The reason of HU shutdown
    mobile_api::AppInterfaceUnregisteredReason::eType unregister_reason_;

    // Construct message threads when everything is already created

    // Thread that pumps messages coming from mobile side.
    impl::FromMobileQueue messages_from_mobile_;
    // Thread that pumps messages being passed to mobile side.
    impl::ToMobileQueue messages_to_mobile_;
    // Thread that pumps messages coming from HMI.
    impl::FromHmiQueue messages_from_hmi_;
    // Thread that pumps messages being passed to HMI.
    impl::ToHmiQueue messages_to_hmi_;

    // Lock for applications list
    sync_primitives::Lock applications_list_lock_;

    DISALLOW_COPY_AND_ASSIGN(ApplicationManagerImpl);

    FRIEND_BASE_SINGLETON_CLASS(ApplicationManagerImpl);
};

const std::set<ApplicationSharedPtr>& ApplicationManagerImpl::applications() const {
  return application_list_;
}

bool ApplicationManagerImpl::vr_session_started() const {
  return is_vr_session_strated_;
}

bool ApplicationManagerImpl::driver_distraction() const {
  return is_distracting_driver_;
}

inline bool ApplicationManagerImpl::all_apps_allowed() const {
  return is_all_apps_allowed_;
}
}  // namespace application_manager

#endif  // SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_H_