SpawnEntityServiceHandler.cpp 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "SpawnEntityServiceHandler.h"
  9. #include <AzFramework/Physics/ShapeConfiguration.h>
  10. #include <ROS2/ROS2Bus.h>
  11. #include <ROS2/Utilities/ROS2Conversions.h>
  12. #include <SimulationInterfaces/SimulationEntityManagerRequestBus.h>
  13. namespace ROS2SimulationInterfaces
  14. {
  15. AZStd::unordered_set<SimulationFeatureType> SpawnEntityServiceHandler::GetProvidedFeatures()
  16. {
  17. return AZStd::unordered_set<SimulationFeatureType>{ SimulationFeatures::SPAWNING };
  18. }
  19. AZStd::optional<SpawnEntityServiceHandler::Response> SpawnEntityServiceHandler::HandleServiceRequest(
  20. const std::shared_ptr<rmw_request_id_t> header, const Request& request)
  21. {
  22. const AZStd::string_view name{ request.name.c_str(), request.name.size() };
  23. const AZStd::string_view uri{ request.uri.c_str(), request.uri.size() };
  24. const AZStd::string_view entityNamespace{ request.entity_namespace.c_str(), request.entity_namespace.size() };
  25. // Validate entity name
  26. if (!name.empty() && !ValidateEntityName(name))
  27. {
  28. Response response;
  29. response.result.result = simulation_interfaces::srv::SpawnEntity::Response::NAME_INVALID;
  30. response.result.error_message = "Invalid entity name. Entity names can only contain alphanumeric characters and underscores.";
  31. SendResponse(response);
  32. return AZStd::nullopt;
  33. }
  34. // Validate frame name
  35. if (!entityNamespace.empty() && !ValidateFrameName(entityNamespace))
  36. {
  37. Response response;
  38. response.result.result = simulation_interfaces::srv::SpawnEntity::Response::NAMESPACE_INVALID;
  39. response.result.error_message =
  40. "Invalid entity namespace. Entity namespaces can only contain alphanumeric characters and forward slashes.";
  41. SendResponse(response);
  42. return AZStd::nullopt;
  43. }
  44. const AZ::Transform initialPose = ROS2::ROS2Conversions::FromROS2Pose(request.initial_pose.pose);
  45. SimulationInterfaces::SimulationEntityManagerRequestBus::Broadcast(
  46. &SimulationInterfaces::SimulationEntityManagerRequests::SpawnEntity,
  47. name,
  48. uri,
  49. entityNamespace,
  50. initialPose,
  51. request.allow_renaming,
  52. [this](const AZ::Outcome<AZStd::string, SimulationInterfaces::FailedResult>& outcome)
  53. {
  54. Response response;
  55. if (outcome.IsSuccess())
  56. {
  57. response.result.result = simulation_interfaces::msg::Result::RESULT_OK;
  58. response.entity_name = outcome.GetValue().c_str();
  59. }
  60. else
  61. {
  62. const auto& failedResult = outcome.GetError();
  63. response.result.result = aznumeric_cast<uint8_t>(failedResult.m_errorCode);
  64. response.result.error_message = failedResult.m_errorString.c_str();
  65. }
  66. SendResponse(response);
  67. });
  68. return AZStd::nullopt;
  69. }
  70. bool SpawnEntityServiceHandler::ValidateEntityName(const AZStd::string& entityName)
  71. {
  72. const AZStd::regex entityRegex{ R"(^[a-zA-Z0-9_]+$)" }; // Entity names can only contain alphanumeric characters and underscores
  73. return AZStd::regex_match(entityName, entityRegex);
  74. }
  75. bool SpawnEntityServiceHandler::ValidateFrameName(const AZStd::string& frameName)
  76. {
  77. const AZStd::regex frameRegex{
  78. R"(^[a-zA-Z0-9_/]+$)"
  79. }; // Entity names can only contain alphanumeric characters and underscores and forward slashes
  80. return AZStd::regex_match(frameName, frameRegex);
  81. }
  82. } // namespace ROS2SimulationInterfaces