blob: 51322ec0b3ee7bf9f4d5e0c5d653a9e587942dd1 [file] [log] [blame]
/**
* Copyright (c) 2020-2021 Robert Bosch GmbH.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Robert Bosch GmbH - initial API and implementation
*/
package org.eclipse.app4mc.slg.ros2.generators;
import org.eclipse.app4mc.amalthea.model.InterProcessTrigger;
import org.eclipse.app4mc.slg.commons.m2t.generators.TranslationUnit;
import org.eclipse.app4mc.slg.ros2.transformers.utils.Utils;
import org.eclipse.app4mc.transformation.util.OutputBuffer;
import org.eclipse.xtend2.lib.StringConcatenation;
public class RosInterProcessTriggerUtilsTranslationUnit extends TranslationUnit {
private InterProcessTrigger ip;
private OutputBuffer outputBuffer;
public RosInterProcessTriggerUtilsTranslationUnit(final OutputBuffer outputBuffer, final InterProcessTrigger ip) {
super();
this.ip = ip;
this.outputBuffer = outputBuffer;
this.genFiles();
}
public static String getModulePathStatic() {
String _basePathStatic = "synthetic_gen";
String _moduleNameStatic = RosInterProcessTriggerUtilsTranslationUnit.getModuleNameStatic();
return _basePathStatic + "/" + _moduleNameStatic;
}
public static String getMakeFilePathStatic() {
String _modulePathStatic = RosInterProcessTriggerUtilsTranslationUnit.getModulePathStatic();
String _makeFileName = "CMakeLists.txt";
return _modulePathStatic + "/" + _makeFileName;
}
public static String getModuleNameStatic() {
return "interProcessTriggerUtils";
}
public static String getLibName() {
return "INTERPROCESSTRIGGER_UTIL";
}
public String getModuleName() {
return RosInterProcessTriggerUtilsTranslationUnit.getModuleNameStatic();
}
public String getIncFile() {
return this.getModuleName() + ".h";
}
public String getExecCall(final String param) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("call_service_");
_builder.append(param);
_builder.append("(");
_builder.append(param);
_builder.append("_client)");
return _builder.toString();
}
public boolean genFiles() {
boolean _xblockexpression = false;
{
boolean _isSrcFileEmpty = this.isSrcFileEmpty();
if (_isSrcFileEmpty) {
this.incAppend(this.toHeader().toString());
}
boolean _isSrcFileEmpty_1 = this.isSrcFileEmpty();
if (_isSrcFileEmpty_1) {
this.srcAppend(this.toCPPHead().toString());
}
this.incAppend(this.toH().toString());
_xblockexpression = this.srcAppend(this.toCPP().toString());
}
return _xblockexpression;
}
public CharSequence toCPPHead() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("#include \"interProcessTriggerUtils.h\"");
_builder.newLine();
_builder.newLine();
_builder.append("using namespace std::chrono_literals;");
_builder.newLine();
return _builder;
}
public CharSequence toHeader() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("#include <string>");
_builder.newLine();
_builder.newLine();
_builder.append("#include \"rclcpp/rclcpp.hpp\"");
_builder.newLine();
_builder.append("#include \"std_msgs/msg/string.hpp\"");
_builder.newLine();
_builder.newLine();
return _builder;
}
public CharSequence toCPP() {
StringConcatenation _builder = new StringConcatenation();
_builder.newLine();
_builder.append("void call_service_");
String _name = this.ip.getStimulus().getName();
_builder.append(_name);
_builder.append("(rclcpp::Client<");
String _name_1 = this.ip.getStimulus().getName();
_builder.append(_name_1);
_builder.append("_service::srv::");
String _name_2 = this.ip.getStimulus().getName();
String _plus = (_name_2 + "_service");
String _idlCompliantName = Utils.toIdlCompliantName(_plus);
_builder.append(_idlCompliantName);
_builder.append(">::SharedPtr& client) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("auto request = std::make_shared<");
String _name_3 = this.ip.getStimulus().getName();
_builder.append(_name_3, "\t");
_builder.append("_service::srv::");
String _name_4 = this.ip.getStimulus().getName();
String _plus_1 = (_name_4 + "_service");
String _idlCompliantName_1 = Utils.toIdlCompliantName(_plus_1);
_builder.append(_idlCompliantName_1, "\t");
_builder.append("::Request>();");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t ");
_builder.append("while (!client->wait_for_service(50ms)) {");
_builder.newLine();
_builder.append("\t ");
_builder.append("if (!rclcpp::ok()) {");
_builder.newLine();
_builder.append("\t ");
_builder.append("RCLCPP_ERROR(rclcpp::get_logger(\"rclcpp\"), \"Interrupted while waiting for the service. Exiting.\");");
_builder.newLine();
_builder.append("\t ");
_builder.append("exit;");
_builder.newLine();
_builder.append("\t ");
_builder.append("}");
_builder.newLine();
_builder.append("\t ");
_builder.append("RCLCPP_INFO(rclcpp::get_logger(\"rclcpp\"), \"service not available, waiting again...\");");
_builder.newLine();
_builder.append("\t ");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("auto result = client->async_send_request(request);\t");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder;
}
public CharSequence toH() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("#include \"");
String _name = this.ip.getStimulus().getName();
_builder.append(_name);
_builder.append("_service/srv/");
String _name_1 = this.ip.getStimulus().getName();
_builder.append(_name_1);
_builder.append("_service.hpp\"");
_builder.newLineIfNotEmpty();
_builder.append("void call_service_");
String _name_2 = this.ip.getStimulus().getName();
_builder.append(_name_2);
_builder.append("(rclcpp::Client<");
String _name_3 = this.ip.getStimulus().getName();
_builder.append(_name_3);
_builder.append("_service::srv::");
String _name_4 = this.ip.getStimulus().getName();
String _plus = (_name_4 + "_service");
String _idlCompliantName = Utils.toIdlCompliantName(_plus);
_builder.append(_idlCompliantName);
_builder.append(">::SharedPtr& client);");
_builder.newLineIfNotEmpty();
_builder.newLine();
return _builder;
}
public String getBasePath() {
return "synthetic_gen";
}
public String getModulePath() {
String _basePath = this.getBasePath();
String _plus = (_basePath + "/");
String _moduleName = this.getModuleName();
return (_plus + _moduleName);
}
@Override
public String getSrcFile() {
String _moduleName = this.getModuleName();
return (_moduleName + ".cpp");
}
public String getIncPath() {
String _modulePath = this.getModulePath();
String _plus = (_modulePath + "/_inc/");
String _incFile = this.getIncFile();
return (_plus + _incFile);
}
public String getSrcPath() {
String _modulePath = this.getModulePath();
String _plus = (_modulePath + "/_src/");
String _srcFile = this.getSrcFile();
return (_plus + _srcFile);
}
public boolean isIncFileEmpty() {
String _modulePath = this.getModulePath();
String _plus = (_modulePath + "/_inc/");
String _moduleName = this.getModuleName();
String _plus_1 = (_plus + _moduleName);
boolean _bufferExists = this.outputBuffer.bufferExists("H", _plus_1);
return (!_bufferExists);
}
public boolean isSrcFileEmpty() {
String _modulePath = this.getModulePath();
String _plus = (_modulePath + "/_src/");
String _moduleName = this.getModuleName();
String _plus_1 = (_plus + _moduleName);
boolean _bufferExists = this.outputBuffer.bufferExists("C", _plus_1);
return (!_bufferExists);
}
public boolean incAppend(final String str) {
String _modulePath = this.getModulePath();
String _plus = (_modulePath + "/_inc/");
String _moduleName = this.getModuleName();
String _plus_1 = (_plus + _moduleName);
return this.outputBuffer.appendTo("H", _plus_1, str);
}
public boolean srcAppend(final String str) {
String _modulePath = this.getModulePath();
String _plus = (_modulePath + "/_src/");
String _moduleName = this.getModuleName();
String _plus_1 = (_plus + _moduleName);
return this.outputBuffer.appendTo("C", _plus_1, str);
}
}