From bd4fa3907bcbc9d6e4a0b1e46d71915d3afd16c9 Mon Sep 17 00:00:00 2001
From: Thomas Boehm <boehm@fortiss.org>
Date: Wed, 13 Jul 2016 12:38:10 +0000
Subject: [PATCH] Fixed generated files and refactored some methods.

---
 .../generator/executable/BusExecutable.java   | 138 ++++++++----------
 .../generator/executable/CoreExecutable.java  |   3 +-
 2 files changed, 61 insertions(+), 80 deletions(-)

diff --git a/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/BusExecutable.java b/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/BusExecutable.java
index 95432e86..20eec60c 100644
--- a/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/BusExecutable.java
+++ b/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/BusExecutable.java
@@ -22,8 +22,6 @@ import static org.fortiss.af3.platform.raspberry.utils.RaspberryUtils.getEcuForC
 import static org.fortiss.af3.platform.raspberry.utils.RaspberryUtils.getTargetComponent;
 import static org.fortiss.tooling.kernel.utils.KernelModelElementUtils.findElementById;
 
-import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -38,6 +36,7 @@ import org.fortiss.af3.generator.common.model.source.SourcePackage;
 import org.fortiss.af3.platform.language.executable.TransmissionUnitExecutableBase;
 import org.fortiss.af3.platform.model.ExecutionUnit;
 import org.fortiss.af3.platform.model.TransmissionUnit;
+import org.fortiss.af3.platform.model.impl.PlatformConnectorUnitImpl;
 import org.fortiss.af3.platform.raspberry.generated.files.RaspberryFileGeneration;
 import org.fortiss.af3.platform.raspberry.model.annotation.PeripheralAddress;
 import org.fortiss.af3.platform.raspberry.model.annotation.PinNumber;
@@ -77,6 +76,19 @@ public class BusExecutable extends TransmissionUnitExecutableBase<TransmissionUn
 	 * message.
 	 */
 	public void findBusSignals(int deploymentID, Component topComponent, SourcePackage pack) {
+		createProtoFile(deploymentID, topComponent, pack);
+
+		createHeaderFile(pack, "sensors");
+		createHeaderFile(pack, "actuators");
+
+		createFilesPeripheralDetails(pack);
+	}
+
+	/**
+	 * Finds Information about Signals that are transmitted over the Bus and form fields of the
+	 * message.
+	 */
+	private void createProtoFile(int deploymentID, Component topComponent, SourcePackage pack) {
 		String messageString = "message msg { \n";
 		int counter = 0;
 		Deployment currentDeployment =
@@ -109,8 +121,8 @@ public class BusExecutable extends TransmissionUnitExecutableBase<TransmissionUn
 									}
 									messageString +=
 											"\t optional " + prtType + " " +
-													((OutputPort)port).getName() + "= " + counter +
-													";\n";
+													((OutputPort)port).getName().toLowerCase() +
+													"= " + counter + ";\n";
 								}
 							}
 						}
@@ -120,9 +132,6 @@ public class BusExecutable extends TransmissionUnitExecutableBase<TransmissionUn
 		}
 		messageString += "}";
 		createFile(pack, messageString, "message.proto");
-		createSensorHeaderFile(pack);
-		createActuatorHeaderFile(pack);
-		createFilesPeripheralDetails(pack);
 	}
 
 	/** Determine Sensors, Actuators their IDs and PinNumbers. */
@@ -132,102 +141,73 @@ public class BusExecutable extends TransmissionUnitExecutableBase<TransmissionUn
 		String actuatorList = "#include \"actuators.h\" \n\n";
 		actuatorList += "actuators act[] = \n{\n";
 
-		List<SensorInImpl> sens = new ArrayList<SensorInImpl>();
-		List<ActuatorOutImpl> acts = new ArrayList<ActuatorOutImpl>();
+		String componentsList = "#ifndef __COMPONENTS_H\n";
+		componentsList += "#define __COMPONENTS_H\n\n";
+
 		EList<IHierarchicElement> el = modelElement.getContainer().getContainedElements();
 		for(IHierarchicElement ce : el) {
 			EList<IConnector> conList = ce.getConnectors();
 			for(IConnector conn : conList) {
 				if(conn instanceof SensorInImpl) {
-					sens.add((SensorInImpl)conn);
+					sensorList += addSensorSpecsToList((SensorInImpl)conn);
+					componentsList +=
+							componentDefine("SensorIn", ((PlatformConnectorUnitImpl)conn).getId());
 				}
 				if(conn instanceof ActuatorOutImpl) {
-					acts.add((ActuatorOutImpl)conn);
+					actuatorList += addSensorSpecsToList((ActuatorOutImpl)conn);
+					componentsList +=
+							componentDefine("ActuatorOut",
+									((PlatformConnectorUnitImpl)conn).getId());
 				}
 			}
 		}
-		sensorList = addSensorSpecsToList(sensorList, sens);
-		actuatorList = addActuatorSpecsToList(actuatorList, acts);
-		sensorList += "\n };";
-		actuatorList += "\n };";
+
+		sensorList = fixEnding(sensorList);
+		actuatorList = fixEnding(actuatorList);
+
+		componentsList += "\n#endif";
 
 		createFile(pack, sensorList, "sensors.c");
 		createFile(pack, actuatorList, "actuators.c");
+		createFile(pack, componentsList, "components.h");
+	}
+
+	private String fixEnding(String s) {
+		return s.substring(0, s.length() - ",\n".length()) + "\n};";
 
 	}
 
-	/** Add specification of peripheral to the respective list of Sensors. */
-	private String addSensorSpecsToList(String sensorList, List<SensorInImpl> sens) {
-		for(SensorInImpl sen : sens) {
-			int Id = sen.getId();
-			HashMap<String, Integer> hmap = new HashMap<String, Integer>();
-			hmap.put("Id", Id);
-			EList<IModelElementSpecification> specs = sen.getSpecifications();
-			for(IModelElementSpecification spec : specs) {
-				if(spec instanceof PinNumber) {
-					int pinNr = ((PinNumber)spec).getPinNumber();
-					hmap.put("pinNr", pinNr);
-				}
-				if(spec instanceof PeripheralAddress) {
-					int address = ((PeripheralAddress)spec).getPeripheralAddress();
-					hmap.put("address", address);
-				}
-			}
-			sensorList +=
-					" {" + hmap.get("Id") + ", " + hmap.get("pinNr") + ", " + hmap.get("address") +
-							"} ";
-			if(sen.getId() != sens.get(sens.size() - 1).getId())
-				sensorList += ", \n";
-		}
-		return sensorList;
+	private String componentDefine(String s, int value) {
+		return "#define " + s + "_ID_" + value + " " + value + "\n";
 	}
 
 	/** Add specification of peripheral to the respective list of Sensors. */
-	private String addActuatorSpecsToList(String actuatorList, List<ActuatorOutImpl> acts) {
-		for(ActuatorOutImpl act : acts) {
-			int Id = act.getId();
-			HashMap<String, Integer> hmap = new HashMap<String, Integer>();
-			hmap.put("Id", Id);
-			EList<IModelElementSpecification> specs = act.getSpecifications();
-			for(IModelElementSpecification spec : specs) {
-				if(spec instanceof PinNumber) {
-					int pinNr = ((PinNumber)spec).getPinNumber();
-					hmap.put("pinNr", pinNr);
-				}
-				if(spec instanceof PeripheralAddress) {
-					int address = ((PeripheralAddress)spec).getPeripheralAddress();
-					hmap.put("address", address);
-				}
+	private String addSensorSpecsToList(PlatformConnectorUnitImpl conn) {
+		int id = conn.getId();
+		int pinNr = 0;
+		int address = 0;
+		EList<IModelElementSpecification> specs = conn.getSpecifications();
+		for(IModelElementSpecification spec : specs) {
+			if(spec instanceof PinNumber) {
+				pinNr = ((PinNumber)spec).getPinNumber();
+			}
+			if(spec instanceof PeripheralAddress) {
+				address = ((PeripheralAddress)spec).getPeripheralAddress();
 			}
-			actuatorList +=
-					" {" + hmap.get("Id") + ", " + hmap.get("pinNr") + ", " + hmap.get("address") +
-							"} ";
-			if(act.getId() != acts.get(acts.size() - 1).getId())
-				actuatorList += ", \n";
 		}
-		return actuatorList;
-	}
-
-	/** Create Header Files defining structure for sensor. */
-	private void createSensorHeaderFile(SourcePackage pack) {
-		String content = "#indef __SENSORS_H \n";
-		content += "#define __SENSORS.H \n\n";
-		content += "struct \n { \n";
-		content += "   int Id; \n   int pinNr; \n   int address; \n } sensors; \n ";
-		content += "extern sensors sen[];\n \n";
-		content += " #endif \n \n";
-		createFile(pack, content, "sensors.h");
+		return "\t{" + id + ", " + pinNr + ", " + address + "},\n";
 	}
 
 	/** Create Header Files defining structure for actuator. */
-	private void createActuatorHeaderFile(SourcePackage pack) {
-		String content = "#indef __ACTUATORS_H \n";
-		content += "#define __ACTUATORS.H \n\n";
-		content += "struct \n { \n";
-		content += "   int Id; \n   int pinNr; \n   int address; \n } actuators; \n ";
-		content += "extern actuators act[];\n \n";
-		content += " #endif \n \n";
-		createFile(pack, content, "actuators.h");
+	private void createHeaderFile(SourcePackage pack, String type) {
+		String content = "#ifndef __" + type.toUpperCase() + "_H \n";
+		content += "#define __" + type.toUpperCase() + "_H \n\n";
+		content += "struct " + type + "\n{ \n";
+		content += "\tint Id;\n\tint pinNr;\n\tint address; \n};\n\n";
+		content += "typedef struct " + type + " " + type + ";\n\n";
+		content += "extern " + type + " " + type.substring(0, 3) + "[];\n\n";
+		content += "#endif \n \n";
+		createFile(pack, content, type + ".h");
 	}
 
 	/** Create file with a given Name and given text. */
diff --git a/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/CoreExecutable.java b/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/CoreExecutable.java
index 72829892..2080b2fe 100644
--- a/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/CoreExecutable.java
+++ b/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/CoreExecutable.java
@@ -226,6 +226,7 @@ public class CoreExecutable extends ExecutionUnitExecutableBase<Core, CSourcePac
 		// String dirName = ((GenericPlatformUnit)modelElement.eContainer()).getName();
 		system.getImports().add(createInclude("message.pb-c", false));
 		system.getImports().add(createInclude("pi", false));
+		system.getImports().add(createInclude("components", false));
 		system.getImports().add(createInclude("stdio", true));
 		system.getImports().add(createInclude("unistd", true));
 		// addStandardHeaderInclude(system, dirName + "/message");
@@ -284,7 +285,7 @@ public class CoreExecutable extends ExecutionUnitExecutableBase<Core, CSourcePac
 		boolean writeMsgFlag = modifyBusMsg(deployedPorts);
 		if(writeMsgFlag == true) {
 			body.add(assignment(definedConst("Msg msg")));
-			body.add(assignment(definedConst("msg_init(&msg)")));
+			body.add(assignment(definedConst("msg__init(&msg)")));
 		}
 		// Search for local channels, i.e. channels that begin and end at components deployed to
 		// this generic execution unit.
-- 
GitLab