summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAKalinich-Luxoft <AKalinich@luxoft.com>2017-09-20 13:31:22 +0300
committerValerii <vmalkov@luxoft.com>2018-01-26 14:15:35 +0200
commit7cf11e4afe793486d4dd85240b87a1b1c4bf1320 (patch)
tree3624733444f8c7fdeb8fbd04e7accd1324ee7bda
parent431522fbc1e14763dc2db03f4c665f3e315400cc (diff)
downloadsdl_core-7cf11e4afe793486d4dd85240b87a1b1c4bf1320.tar.gz
Add smart object check syntax functions to Command class
This functions will be used to validate incoming messages in notifications and requests so they should be accessible from any command. Also added function for checking HMI interface availability.
-rw-r--r--src/components/application_manager/include/application_manager/commands/command_impl.h31
-rw-r--r--src/components/application_manager/include/application_manager/commands/command_request_impl.h16
-rw-r--r--src/components/application_manager/src/commands/command_impl.cc87
-rw-r--r--src/components/application_manager/src/commands/command_request_impl.cc26
4 files changed, 133 insertions, 27 deletions
diff --git a/src/components/application_manager/include/application_manager/commands/command_impl.h b/src/components/application_manager/include/application_manager/commands/command_impl.h
index 5fee7500ce..1ad6a6f2e6 100644
--- a/src/components/application_manager/include/application_manager/commands/command_impl.h
+++ b/src/components/application_manager/include/application_manager/commands/command_impl.h
@@ -135,6 +135,14 @@ class CommandImpl : public Command {
*/
void SetAllowedToTerminate(const bool allowed) OVERRIDE;
+ /**
+ * @brief Check syntax of string
+ * @param str - string that need to be checked
+ * @param allow_empty_string if true methods allow empty sting
+ * @return true if syntax is correct otherwise return false
+ */
+ bool CheckSyntax(const std::string& str, bool allow_empty_line = false) const;
+
// members
static const int32_t hmi_protocol_type_;
static const int32_t mobile_protocol_type_;
@@ -169,6 +177,29 @@ class CommandImpl : public Command {
*/
void ReplaceHMIByMobileAppId(smart_objects::SmartObject& message);
+ /**
+ * @brief Validate string syntax in smart object
+ * @param obj Smart object to check
+ * @return true if syntax if all object strings is correct otherwise false
+ */
+ bool ValidateSmartObjectStrings(const smart_objects::SmartObject& obj) const;
+
+ /**
+ * @brief Validate string syntax in smart map
+ * @param obj Smart map to check
+ * @param key Map iterator
+ * @return true if syntax if all map strings is correct otherwise false
+ */
+ bool ValidateSmartMapStrings(const smart_objects::SmartObject& obj,
+ std::set<std::string>::const_iterator key) const;
+
+ /**
+ * @brief Check string syntax
+ * @param obj Smart string object
+ * @return true if syntax of string is correct otherwise false
+ */
+ bool ValidateSmartString(const smart_objects::SmartObject& obj) const;
+
MessageSharedPtr message_;
uint32_t default_timeout_;
bool allowed_to_terminate_;
diff --git a/src/components/application_manager/include/application_manager/commands/command_request_impl.h b/src/components/application_manager/include/application_manager/commands/command_request_impl.h
index 74a884a223..64fcb9dd2b 100644
--- a/src/components/application_manager/include/application_manager/commands/command_request_impl.h
+++ b/src/components/application_manager/include/application_manager/commands/command_request_impl.h
@@ -162,14 +162,6 @@ class CommandRequestImpl : public CommandImpl,
const smart_objects::SmartObject* response_params = NULL);
/**
- * @brief Check syntax of string from mobile
- * @param str - string that need to be checked
- * @param allow_empty_string if true methods allow empty sting
- * @return true if success otherwise return false
- */
- bool CheckSyntax(const std::string& str, bool allow_empty_line = false);
-
- /*
* @brief Sends HMI request
*
* @param function_id HMI request ID
@@ -201,6 +193,14 @@ class CommandRequestImpl : public CommandImpl,
protected:
/**
+ * @brief Checks is specified HMI interface available
+ * @param interface to check
+ * @return true if HMI interface available false otherwise
+ */
+ bool CheckHMIInterfaceAvailability(
+ const HmiInterfaces::InterfaceID interface) const;
+
+ /**
* @brief Checks message permissions and parameters according to policy table
* permissions
*/
diff --git a/src/components/application_manager/src/commands/command_impl.cc b/src/components/application_manager/src/commands/command_impl.cc
index b928184148..e9e37115d4 100644
--- a/src/components/application_manager/src/commands/command_impl.cc
+++ b/src/components/application_manager/src/commands/command_impl.cc
@@ -198,5 +198,92 @@ DEPRECATED void CommandImpl::ReplaceHMIByMobileAppId(
}
}
+bool CommandImpl::CheckSyntax(const std::string& str,
+ bool allow_empty_line) const {
+ if (std::string::npos != str.find_first_of("\t\n")) {
+ LOG4CXX_ERROR(logger_, "CheckSyntax failed! :" << str);
+ return false;
+ }
+ if (std::string::npos != str.find("\\n") ||
+ std::string::npos != str.find("\\t")) {
+ LOG4CXX_ERROR(logger_, "CheckSyntax failed! :" << str);
+ return false;
+ }
+ if (!allow_empty_line) {
+ if ((std::string::npos == str.find_first_not_of(' '))) {
+ return false;
+ }
+ }
+ return true;
+}
+
+bool CommandImpl::ValidateSmartString(
+ const smart_objects::SmartObject& obj) const {
+ const char* str = obj.asCharArray();
+ const bool is_str_empty = str && !str[0];
+ return is_str_empty || CheckSyntax(str);
+}
+
+bool CommandImpl::ValidateSmartMapStrings(
+ const smart_objects::SmartObject& obj,
+ std::set<std::string>::const_iterator key) const {
+ using NsSmartDeviceLink::NsSmartObjects::SmartType_Map;
+ using NsSmartDeviceLink::NsSmartObjects::SmartType_Array;
+ using NsSmartDeviceLink::NsSmartObjects::SmartType_String;
+
+ const smart_objects::SmartObject& value = obj[*key];
+ switch (value.getType()) {
+ case SmartType_Array:
+ case SmartType_Map: {
+ return ValidateSmartObjectStrings(value);
+ }
+ case SmartType_String: {
+ return ValidateSmartString(value);
+ }
+ default:
+ // Catch SmartType_Integer or SmartType_Double
+ break;
+ }
+ return true;
+}
+
+bool CommandImpl::ValidateSmartObjectStrings(
+ const smart_objects::SmartObject& obj) const {
+ using NsSmartDeviceLink::NsSmartObjects::SmartType_Map;
+ using NsSmartDeviceLink::NsSmartObjects::SmartType_Array;
+ using NsSmartDeviceLink::NsSmartObjects::SmartType_String;
+
+ switch (obj.getType()) {
+ case SmartType_Array: {
+ for (uint32_t i = 0; i < obj.length(); ++i) {
+ if (!ValidateSmartObjectStrings(obj[i])) {
+ LOG4CXX_WARN(logger_, "Invalid array item value at index: " << i);
+ return false;
+ }
+ }
+ break;
+ }
+ case SmartType_Map: {
+ const std::set<std::string> keys = obj.enumerate();
+ for (std::set<std::string>::const_iterator key = keys.begin();
+ key != keys.end();
+ key++) {
+ if (!ValidateSmartMapStrings(obj, key)) {
+ LOG4CXX_WARN(logger_, "Invalid map value at key: " << *key);
+ return false;
+ }
+ }
+ break;
+ }
+ case SmartType_String: {
+ return ValidateSmartString(obj);
+ }
+ default:
+ // Catch SmartType_Integer or SmartType_Double
+ break;
+ }
+ return true;
+}
+
} // namespace commands
} // namespace application_manager
diff --git a/src/components/application_manager/src/commands/command_request_impl.cc b/src/components/application_manager/src/commands/command_request_impl.cc
index 09aa4169e5..a2e4e44ec3 100644
--- a/src/components/application_manager/src/commands/command_request_impl.cc
+++ b/src/components/application_manager/src/commands/command_request_impl.cc
@@ -306,25 +306,6 @@ void CommandRequestImpl::SendResponse(
application_manager_.ManageMobileCommand(result, ORIGIN_SDL);
}
-bool CommandRequestImpl::CheckSyntax(const std::string& str,
- bool allow_empty_line) {
- if (std::string::npos != str.find_first_of("\t\n")) {
- LOG4CXX_ERROR(logger_, "CheckSyntax failed! :" << str);
- return false;
- }
- if (std::string::npos != str.find("\\n") ||
- std::string::npos != str.find("\\t")) {
- LOG4CXX_ERROR(logger_, "CheckSyntax failed! :" << str);
- return false;
- }
- if (!allow_empty_line) {
- if ((std::string::npos == str.find_first_not_of(' '))) {
- return false;
- }
- }
- return true;
-}
-
smart_objects::SmartObject CreateUnsupportedResourceResponse(
const hmi_apis::FunctionID::eType function_id,
const uint32_t hmi_correlation_id,
@@ -578,6 +559,13 @@ mobile_apis::Result::eType CommandRequestImpl::GetMobileResultCode(
return mobile_result;
}
+bool CommandRequestImpl::CheckHMIInterfaceAvailability(
+ const HmiInterfaces::InterfaceID interface) const {
+ const HmiInterfaces::InterfaceState interface_state =
+ application_manager_.hmi_interfaces().GetInterfaceState(interface);
+ return HmiInterfaces::InterfaceState::STATE_NOT_AVAILABLE != interface_state;
+}
+
bool CommandRequestImpl::CheckAllowedParameters() {
LOG4CXX_AUTO_TRACE(logger_);