Transport adapter last state is stored when application is either
authorNikita Vaganov <nvaganov@luxoft.com>
Wed, 19 Mar 2014 05:56:23 +0000 (09:56 +0400)
committerJustin Dickow <jjdickow@gmail.com>
Tue, 8 Jul 2014 22:09:34 +0000 (18:09 -0400)
connected or disconnected
Only established connections are stored

Signed-off-by: Justin Dickow <jjdickow@gmail.com>
Conflicts:
src/appMain/smartDeviceLink.ini

src/appMain/CMakeLists.txt
src/appMain/life_cycle.cc
src/appMain/smartDeviceLink.ini
src/components/config_profile/src/profile.cc
src/components/resumption/include/resumption/last_state.h
src/components/resumption/src/last_state.cc
src/components/transport_manager/include/transport_manager/transport_adapter/transport_adapter_impl.h
src/components/transport_manager/src/bluetooth/bluetooth_transport_adapter.cc
src/components/transport_manager/src/tcp/tcp_transport_adapter.cc
src/components/transport_manager/src/transport_adapter/transport_adapter_impl.cc

index e91ab4d..a326933 100644 (file)
@@ -77,6 +77,7 @@ include_directories (
   ../components/smart_objects/include/
   ../components/media_manager/include/
   ../components/policies/include/
+  ../components/resumption/include/
   ../thirdPartyLibs/MessageBroker/include
   ../thirdPartyLibs/encryption/include
   ${CMAKE_SOURCE_DIR}/src/components/
index 687cc33..ca0859d 100644 (file)
@@ -35,6 +35,7 @@
 #include "./life_cycle.h"
 #include "utils/signals.h"
 #include "config_profile/profile.h"
+#include "resumption/last_state.h"
 
 using threads::Thread;
 
@@ -245,6 +246,9 @@ bool LifeCycle::InitMessageSystem() {
 #endif  // QT_HMI
 
 void LifeCycle::StopComponents() {
+  LOG4CXX_INFO(logger_, "Destroying Last State");
+  resumption::LastState::destroy();
+
   hmi_handler_->set_message_observer(NULL);
   connection_handler_->set_connection_handler_observer(NULL);
   protocol_handler_->RemoveProtocolObserver(app_manager_);
index c7bdc5f..d228058 100644 (file)
@@ -39,6 +39,8 @@ AppRequestsTimeScale = 10
 PendingRequestsAmount = 1000
 HeartBeatTimeout = 0
 SupportedDiagModes = 0x01, 0x02, 0x03, 0x05, 0x06, 0x07, 0x09, 0x0A, 0x18, 0x19, 0x22, 0x3E
+SystemFilesPath = /tmp/fs/mp/images/ivsu_cache
+UseLastState = true
 
 [MEDIA MANAGER]
 EnableRedecoding = false
index db4a0d5..aae7290 100644 (file)
@@ -602,6 +602,16 @@ void Profile::UpdateValues() {
 
   (void) ReadIntValue(&heart_beat_timeout_, kDefaultHeartBeatTimeout,
                       kMainSection, "HeartBeatTimeout");
+
+  *value = '\0';
+  if ((0 != ini_read_value(config_file_name_.c_str(),
+                           "MAIN", "UseLastState", value))
+      && ('\0' != *value)) {
+    if (0 == strcmp("true", value)) {
+      use_last_state_ = true;
+    }
+    LOG4CXX_INFO(logger_, "Set UseLastState to " << value);
+  }
 }
 
 bool Profile::ReadValue(bool* value, const char* const pSection,
index 18469e2..4380542 100644 (file)
@@ -1,5 +1,5 @@
-/**
- * Copyright (c) 2013, Ford Motor Company
+/*
+ * Copyright (c) 2014, Ford Motor Company
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -48,27 +48,26 @@ class LastState : public utils::Singleton<LastState> {
  */
   Json::Value dictionary;
 
-  private:
-
-  /**
-   * @brief File to save Dictionary
-   */
+ private:
+/**
+ * @brief File to save Dictionary
+ */
   static const std::string filename;
-
-  /**
-   * @brief Saving dictionary to filesystem as Json
-   */
+/**
+ * @brief Saving dictionary to filesystem
+ */
   void SaveToFileSystem();
-
-  /**
-   * @brief Load dictionary from filesystem as Json
-   */
+/**
+ * @brief Load dictionary from filesystem
+ */
   void LoadFromFileSystem();
-
- /**
+/**
  * @brief Private default constructor
  */
   LastState();
+/**
+ * @brief Private destructor
+ */
   ~LastState();
 
 
index 97b13a7..ebf4ce8 100644 (file)
@@ -35,6 +35,8 @@
 
 namespace resumption {
 
+const std::string LastState::filename = "LastState.dat";
+
 void LastState::SaveToFileSystem() {
   const std::string& str = dictionary.toStyledString();
   const std::vector<uint8_t> char_vector_pdata(
@@ -61,5 +63,4 @@ LastState::~LastState() {
   SaveToFileSystem();
 }
 
-const std::string LastState::filename = "TM.dat";
 }
index 664a5c8..ac22931 100644 (file)
@@ -393,7 +393,6 @@ class TransportAdapterImpl : public TransportAdapter,
    */
   virtual bool ToBeAutoConnected(DeviceSptr device) const;
 
- private:
   /**
    * @brief Find connection that has state - ESTABLISHED.
    *
@@ -403,7 +402,9 @@ class TransportAdapterImpl : public TransportAdapter,
    * @return ConnectionSptr smart pointer to the connection.
    */
   ConnectionSptr FindEstablishedConnection(const DeviceUID& device_handle,
-                                           const ApplicationHandle& app_handle);
+                                           const ApplicationHandle& app_handle) const;
+
+ private:
   /**
    * @brief Connect to all applications discovered on device
    * @param device Pointer to device
index 917ad24..39db272 100644 (file)
@@ -86,19 +86,23 @@ void BluetoothTransportAdapter::Store() const {
     ApplicationList app_ids = bluetooth_device->GetApplicationList();
     for (ApplicationList::const_iterator j = app_ids.begin(); j != app_ids.end(); ++j) {
       ApplicationHandle app_handle = *j;
-      uint8_t rfcomm_channel;
-      bluetooth_device->GetRfcommChannel(app_handle, &rfcomm_channel);
-      Json::Value application_dictionary;
-      char rfcomm_channel_record[4];
-      sprintf(rfcomm_channel_record, "%ud", rfcomm_channel);
-      application_dictionary["rfcomm_channel"] = std::string(rfcomm_channel_record);
-      applications_dictionary.append(application_dictionary);
+      if (FindEstablishedConnection(bluetooth_device->name(), app_handle)) {
+        uint8_t rfcomm_channel;
+        bluetooth_device->GetRfcommChannel(app_handle, &rfcomm_channel);
+        Json::Value application_dictionary;
+        char rfcomm_channel_record[4];
+        sprintf(rfcomm_channel_record, "%u", rfcomm_channel);
+        application_dictionary["rfcomm_channel"] = std::string(rfcomm_channel_record);
+        applications_dictionary.append(application_dictionary);
+      }
+    }
+    if (!applications_dictionary.empty()) {
+      device_dictionary["applications"] = applications_dictionary;
+      devices_dictionary.append(device_dictionary);
     }
-    device_dictionary["applications"] = applications_dictionary;
-    devices_dictionary.append(device_dictionary);
   }
   bluetooth_adapter_dictionary["devices"] = devices_dictionary;
-  resumption::LastState::instance()->dictionary["BluetoothAdapter"] =
+  resumption::LastState::instance()->dictionary["TransportManager"]["BluetoothAdapter"] =
     bluetooth_adapter_dictionary;
   LOG4CXX_TRACE_EXIT(logger_);
 }
@@ -107,7 +111,7 @@ bool BluetoothTransportAdapter::Restore() {
   LOG4CXX_TRACE_ENTER(logger_);
   bool errors_occured = false;
   const Json::Value bluetooth_adapter_dictionary =
-    resumption::LastState::instance()->dictionary["BluetoothAdapter"];
+    resumption::LastState::instance()->dictionary["TransportManager"]["BluetoothAdapter"];
   const Json::Value devices_dictionary = bluetooth_adapter_dictionary["devices"];
   for (Json::Value::const_iterator i = devices_dictionary.begin();
     i != devices_dictionary.end(); ++i) {
index d09b78d..4aa52fb 100644 (file)
@@ -93,20 +93,24 @@ void TcpTransportAdapter::Store() const {
     ApplicationList app_ids = tcp_device->GetApplicationList();
     for (ApplicationList::const_iterator j = app_ids.begin(); j != app_ids.end(); ++j) {
       ApplicationHandle app_handle = *j;
-      int port = tcp_device->GetApplicationPort(app_handle);
-      if (port != -1) { // don't want to store incoming applications
-        Json::Value application_dictionary;
-        char port_record[12];
-        sprintf(port_record, "%d", port);
-        application_dictionary["port"] = std::string(port_record);
-        applications_dictionary.append(application_dictionary);
+      if (FindEstablishedConnection(tcp_device->name(), app_handle)) {
+        int port = tcp_device->GetApplicationPort(app_handle);
+        if (port != -1) { // don't want to store incoming applications
+          Json::Value application_dictionary;
+          char port_record[12];
+          sprintf(port_record, "%d", port);
+          application_dictionary["port"] = std::string(port_record);
+          applications_dictionary.append(application_dictionary);
+        }
       }
     }
-    device_dictionary["applications"] = applications_dictionary;
-    devices_dictionary.append(device_dictionary);
+    if (!applications_dictionary.empty()) {
+      device_dictionary["applications"] = applications_dictionary;
+      devices_dictionary.append(device_dictionary);
+    }
   }
   tcp_adapter_dictionary["devices"] = devices_dictionary;
-  resumption::LastState::instance()->dictionary["TcpAdapter"] =
+  resumption::LastState::instance()->dictionary["TransportManager"]["TcpAdapter"] =
     tcp_adapter_dictionary;
   LOG4CXX_TRACE_EXIT(logger_);
 }
@@ -115,7 +119,7 @@ bool TcpTransportAdapter::Restore() {
   LOG4CXX_TRACE_ENTER(logger_);
   bool errors_occured = false;
   const Json::Value tcp_adapter_dictionary =
-    resumption::LastState::instance()->dictionary["TcpAdapter"];
+    resumption::LastState::instance()->dictionary["TransportManager"]["TcpAdapter"];
   const Json::Value devices_dictionary = tcp_adapter_dictionary["devices"];
   for (Json::Value::const_iterator i = devices_dictionary.begin();
     i != devices_dictionary.end(); ++i) {
index 6149670..ff7dae1 100644 (file)
@@ -418,6 +418,8 @@ void TransportAdapterImpl::DisconnectDone(const DeviceUID& device_id,
     }
     pthread_mutex_unlock(&devices_mutex_);
   }
+
+  Store();
 }
 
 void TransportAdapterImpl::DataReceiveDone(const DeviceUID& device_id,
@@ -483,6 +485,8 @@ void TransportAdapterImpl::ConnectDone(const DeviceUID& device_id,
   for (TransportAdapterListenerList::iterator it = listeners_.begin();
        it != listeners_.end(); ++it)
     (*it)->OnConnectDone(this, device_id, app_handle);
+
+  Store();
 }
 
 void TransportAdapterImpl::ConnectFailed(const DeviceUID& device_id,
@@ -569,13 +573,13 @@ bool TransportAdapterImpl::ToBeAutoConnected(DeviceSptr device) const {
 }
 
 ConnectionSptr TransportAdapterImpl::FindEstablishedConnection(
-    const DeviceUID& device_id, const ApplicationHandle& app_handle) {
+    const DeviceUID& device_id, const ApplicationHandle& app_handle) const {
   ConnectionSptr connection;
   pthread_mutex_lock(&connections_mutex_);
-  ConnectionMap::iterator it =
+  ConnectionMap::const_iterator it =
       connections_.find(std::make_pair(device_id, app_handle));
   if (it != connections_.end()) {
-    ConnectionInfo& info = it->second;
+    const ConnectionInfo& info = it->second;
     if (info.state == ConnectionInfo::ESTABLISHED) connection = info.connection;
   }
   pthread_mutex_unlock(&connections_mutex_);