summaryrefslogtreecommitdiff
path: root/src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_impl.h
blob: 078f93b32feba8bbd7467ff0d6e46d64f53b2010 (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
/*
 * Copyright (c) 2016, 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 <map>
#include <memory>
#include <string>

#include "utils/lock.h"
#include "utils/rwlock.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"
#include "transport_manager/transport_manager_settings.h"
#include "resumption/last_state.h"

#ifdef TELEMETRY_MONITOR
#include "transport_manager/telemetry_observer.h"
#endif  // TELEMETRY_MONITOR

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,
                       resumption::LastState& last_state,
                       const TransportManagerSettings& settings);

  /**
   * @brief Destructor.
   **/
  virtual ~TransportAdapterImpl();

  /**
   * @brief Check initialization.
   *
   * @return true if initialized.
   * @return false if not initialized.
   */
  bool IsInitialised() const OVERRIDE;

 public:
  /**
   * @brief Initiate device adapter.
   *
   * Called from transport manager to start device adapter.
   *
   * @return Error information about possible reason of failure.
   **/
  TransportAdapter::Error Init() OVERRIDE;

  /**
   * @brief Stops device adapter
   * Called from transport manager to stop device adapter
   */
  void Terminate() OVERRIDE;

  /**
   * @brief Add listener to the container(list) of device adapter listeners.
   *
   * @param listener Pointer to the device adapter listener.
   */
  void AddListener(TransportAdapterListener* listener) OVERRIDE;

  /**
   * @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.
   **/
  TransportAdapter::Error SearchDevices() OVERRIDE;

  /**
   * @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 failure.
   **/
  TransportAdapter::Error Connect(const DeviceUID& device_handle,
                                  const ApplicationHandle& app_handle) OVERRIDE;

  /**
   * @brief Connect to all applications discovered on device
   * @param device_handle Handle of device
   * @return Error information about connecting applications on device
   */
  TransportAdapter::Error ConnectDevice(
      const DeviceUID& device_handle) OVERRIDE;

  /**
   * @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.
   **/
  TransportAdapter::Error Disconnect(
      const DeviceUID& device_handle,
      const ApplicationHandle& app_handle) OVERRIDE;

  /**
   * @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.
   **/
  TransportAdapter::Error DisconnectDevice(
      const DeviceUID& device_handle) OVERRIDE;

  /**
   * @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
   **/
  TransportAdapter::Error SendData(
      const DeviceUID& device_handle,
      const ApplicationHandle& app_handle,
      const ::protocol_handler::RawMessagePtr data) OVERRIDE;

  /**
   * @brief Start client listener.
   *
   * @return Error information about possible reason of failure.
   */
  TransportAdapter::Error StartClientListening() OVERRIDE;

  /**
   * @brief Stop client listener.
   *
   * @return Error information about possible reason of failure.
   */
  TransportAdapter::Error StopClientListening() OVERRIDE;

  /**
   * @brief Notify that device scanner is available.
   *
   * @return True - available, false - not available.
   */
  bool IsSearchDevicesSupported() const OVERRIDE;

  /**
   * @brief Notify that server connection factory is available.
   *
   * @return True - available, false - not available.
   */
  bool IsServerOriginatedConnectSupported() const OVERRIDE;

  /**
   * @brief Notify that listener of client connection is available.
   *
   * @return True - available, false - not available.
   */
  bool IsClientOriginatedConnectSupported() const OVERRIDE;

  /**
   * @brief Create container(vector) of device unique identifiers.
   *
   * @return Container(vector) of device unique identifiers.
   */
  DeviceList GetDeviceList() const OVERRIDE;

  /**
   * @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.
   */
  ApplicationList GetApplicationList(
      const DeviceUID& device_handle) const OVERRIDE;

  /**
   * @brief Find device in the internal container(map).
   *
   * @param device_handle Device unique identifier.
   *
   * @return Smart pointer to device.
   */
  DeviceSptr FindDevice(const DeviceUID& device_handle) const OVERRIDE;

  /**
   * @brief Search for device in container of devices, if it is not there - adds
   *it.
   *
   * @param devices Container(vector) of smart pointers to devices.
   */
  void SearchDeviceDone(const DeviceVector& devices) OVERRIDE;

  void ApplicationListUpdated(const DeviceUID& device_handle) OVERRIDE;

  void FindNewApplicationsRequest() OVERRIDE;

  /**
   * @brief Launch OnSearchDeviceFailed event in device adapter listener.
   *
   * @param error Error class that contains details of this error situation.
   */
  void SearchDeviceFailed(const SearchDeviceError& error) OVERRIDE;

  /**
   * @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.
   */
  DeviceSptr AddDevice(DeviceSptr device) OVERRIDE;

  /**
   * @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.
   */
  void ConnectionCreated(ConnectionSPtr connection,
                         const DeviceUID& device_handle,
                         const ApplicationHandle& app_handle) OVERRIDE;

  /**
   * @brief Set state of specified connection - FINALISING.
   *
   * @param device_handle Device unique identifier.
   * @param app_handle Handle of application.
   */
  void ConnectionFinished(const DeviceUID& device_handle,
                          const ApplicationHandle& app_handle) OVERRIDE;

  /**
   * @brief Set specified connection state to FINALISING 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.
   */
  void ConnectionAborted(const DeviceUID& device_handle,
                         const ApplicationHandle& app_handle,
                         const CommunicationError& error) OVERRIDE;

  /**
   * @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.
   */
  void ConnectDone(const DeviceUID& device_handle,
                   const ApplicationHandle& app_handle) OVERRIDE;

  /**
   * @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.
   */
  void ConnectFailed(const DeviceUID& device_handle,
                     const ApplicationHandle& app_handle,
                     const ConnectError& error) OVERRIDE;

  /**
   * @brief Remove marked as FINALISING connection from accounting.
   *
   * @param device_handle Device unique identifier.
   * @param app_handle Handle of application.
   */
  void RemoveFinalizedConnection(const DeviceUID& device_handle,
                                 const ApplicationHandle& app_handle) OVERRIDE;

  /**
   * @brief Remove specified device and all its connections
   * @param device_handle Device unique identifier.
   * @param error Error class that contains details of this error situation.
   */
  void DeviceDisconnected(const DeviceUID& device_handle,
                          const DisconnectDeviceError& error) OVERRIDE;

  /**
   * @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.
   */
  void DisconnectDone(const DeviceUID& device_handle,
                      const ApplicationHandle& app_handle) OVERRIDE;

  /**
   * @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.
   */
  void DataReceiveDone(const DeviceUID& device_handle,
                       const ApplicationHandle& app_handle,
                       ::protocol_handler::RawMessagePtr message) OVERRIDE;

  /**
   * @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.
   */
  void DataReceiveFailed(const DeviceUID& device_handle,
                         const ApplicationHandle& app_handle,
                         const DataReceiveError& error) OVERRIDE;

  /**
   * @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.
   */
  void DataSendDone(const DeviceUID& device_handle,
                    const ApplicationHandle& app_handle,
                    ::protocol_handler::RawMessagePtr message) OVERRIDE;

  /**
   * @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.
   */
  void DataSendFailed(const DeviceUID& device_handle,
                      const ApplicationHandle& app_handle,
                      ::protocol_handler::RawMessagePtr message,
                      const DataSendError& error) OVERRIDE;

  /**
   * @brief Notification that transport's configuration is updated
   *
   * @param new_config The new configuration of the transport
   */
  void TransportConfigUpdated(const TransportConfig& new_config) OVERRIDE;

  /**
   * @brief DoTransportSwitch notifies listeners of transport adapter events
   * that transport switching is requested by system
   */
  void DoTransportSwitch() const OVERRIDE;

  /**
   * @brief DeviceSwitched Notifies system on successful transport switch for
   * particular device
   * @param device_handle Device handle of switched device
   */
  void DeviceSwitched(const DeviceUID& device_handle) OVERRIDE;

  /**
   * @brief GetSwitchableDevices Provides list of devices able to switch their
   * transport (e.g. iAP2 Bluetooth to iAP2 USB).
   * @return
   */
  SwitchableDevices GetSwitchableDevices() const OVERRIDE;

  /**
   * @brief Returns the transport's configuration information
   */
  virtual TransportConfig GetTransportConfiguration() const OVERRIDE {
    // default is empty
    return TransportConfig();
  }

  /**
   * @brief Return name of device.
   *
   * @param device_id Device unique identifier.
   *
   * @return String with name.
   */
  std::string DeviceName(const DeviceUID& device_id) const OVERRIDE;

  /**
   * @brief StopDevice looks for specific device in devices list and calls
   * Stop() interface of that device
   * @param device_id unique device identifier that has to be stopped.
   */
  void StopDevice(const DeviceUID& device_id) const OVERRIDE;

  /**
   * @brief Allows to obtain connection type used by device.
   * @return connection type.
   */
  std::string GetConnectionType() const OVERRIDE;

  /**
   * @brief RunAppOnDevice allows run specific application on the certain
   *device.
   *
   * @param device_handle device identifier to run application on.
   *
   * @param bundle_id application id also known as bundle id on some devices to
   *run.
   */
  void RunAppOnDevice(const DeviceUID& device_uid,
                      const std::string& bundle_id) OVERRIDE;

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

  /**
   * @brief Return Time metric observer
   *
   * @param return pointer to Time metric observer
   */
  TMTelemetryObserver* GetTelemetryObserver() OVERRIDE;
#endif  // TELEMETRY_MONITOR

 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;

  /**
   * @brief Returns true if \a device is to be disconnected automatically when
   * all applications will be closed
   */
  virtual bool ToBeAutoDisconnected(DeviceSptr device) const;

  /**
   * @brief Find connection that has state - ESTABLISHED.
   *
   * @param device_handle Device unique identifier.
   * @param app_handle Handle of application.
   *
   * @return pointer to the connection.
   */
  virtual ConnectionSPtr FindEstablishedConnection(
      const DeviceUID& device_handle,
      const ApplicationHandle& app_handle) const;

  const TransportManagerSettings& get_settings() const {
    return settings_;
  }

 private:
  /**
   * @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 Remove specified device
   * @param device_handle Device unique identifier.
   */
  void RemoveDevice(const DeviceUID& device_handle);

  /**
   * Checks whether application is single active on device
   * @param device_uid
   * @param app_uid
   * @return true if this application is the single application on device
   */
  bool IsSingleApplication(const DeviceUID& device_uid,
                           const ApplicationHandle& app_uid);

  /**
   * @brief Listener for device adapter notifications.
   **/
  TransportAdapterListenerList listeners_;

  /**
   * @brief Flag variable that notify initialized device adapter or not.
   */
  bool initialised_;

  /**
   * @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 sync_primitives::Lock devices_mutex_;

  /**
   * @brief Container(map) of connections.
   **/
  ConnectionMap connections_;

  /**
   * @brief Mutex restricting access to connections map.
   **/
  mutable sync_primitives::RWLock connections_lock_;

 protected:
#ifdef TELEMETRY_MONITOR
  /**
   * @brief Pointer to time metric observer
   */
  TMTelemetryObserver* metric_observer_;
#endif  // TELEMETRY_MONITOR

  resumption::LastState& last_state() const {
    return last_state_;
  }

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

  resumption::LastState& last_state_;
  const TransportManagerSettings& settings_;
};

}  // namespace transport_adapter
}  // namespace transport_manager

#endif  // SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_ADAPTER_TRANSPORT_ADAPTER_IMPL_H_