Skip to content

Commit

Permalink
adding codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
tanneberger committed Feb 14, 2025
1 parent 9a95183 commit 828989b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 13 deletions.
8 changes: 3 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.10)
project(reactor-uc LANGUAGES C)

# Command line options for the build
set(BUILD_TESTS ON CACHE BOOL "Build all tests")
set(BUILD_LF_TESTS ON CACHE BOOL "Build lf tests")
set(BUILD_UNIT_TESTS ON CACHE BOOL "Build unit tests")
set(BUILD_TESTS OFF CACHE BOOL "Build all tests")
set(BUILD_LF_TESTS OFF CACHE BOOL "Build lf tests")
set(BUILD_UNIT_TESTS OFF CACHE BOOL "Build unit tests")
set(TEST_COVERAGE OFF CACHE BOOL "Compute test coverage")
set(ASAN OFF CACHE BOOL "Compile with AddressSanitizer")
set(PLATFORM "POSIX" CACHE STRING "Platform to target")
Expand Down Expand Up @@ -80,8 +80,6 @@ endif()
# Add compile definitions for platform and network channel specifics.
target_compile_definitions(reactor-uc PUBLIC "PLATFORM_${PLATFORM}")

target_compile_definitions(reactor-uc PUBLIC "NETWORK_CHANNEL_TCP_POSIX") #TODO: Remove ME

# Add compile definition for scheduler used
target_compile_definitions(reactor-uc PRIVATE "SCHEDULER_${SCHEDULER}")

Expand Down
4 changes: 2 additions & 2 deletions include/reactor-uc/serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#endif

struct MessageFraming {
uint32_t header;
uint32_t message_size;
uint16_t preamble;
uint16_t protocol_version;
uint32_t message_size;
uint16_t crypto_id;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ enum AttrParamType {
new AttrParamSpec("name", AttrParamType.STRING, true),
new AttrParamSpec("uart_device", AttrParamType.INT, true),
new AttrParamSpec("baud_rate", AttrParamType.INT, true),
new AttrParamSpec("data_bits", AttrParamType.STRING, true),
new AttrParamSpec("data_bits", AttrParamType.INT, true),
new AttrParamSpec("parity", AttrParamType.STRING, true),
new AttrParamSpec("stop_bits", AttrParamType.STRING, true),
new AttrParamSpec("stop_bits", AttrParamType.INT, true),
new AttrParamSpec("async", AttrParamType.BOOLEAN, true))));

ATTRIBUTE_SPECS_BY_NAME.put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ class UcConnectionGenerator(
""" |typedef struct {
| FederatedConnectionBundle super;
${" | "..bundle.networkChannel.codeType} channel;
${" | "..bundle.encryptionLayer.codeType} encryption_channel;
${" | "..bundle.groupedConnections.joinWithLn { generateFederatedConnectionInstance(it) }}
| LF_FEDERATED_CONNECTION_BUNDLE_BOOKKEEPING_INSTANCES(${bundle.numInputs(currentFederate!!)}, ${
bundle.numOutputs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,25 @@ class UcFederatedConnectionBundle(
val networkChannel: UcNetworkChannel =
UcNetworkChannel.createNetworkEndpointsAndChannelForBundle(this)

val encryptionLayer: UcEncryptionLayer = UcEncryptionLayer.createEncryptionLayerForBundle(this)

fun numOutputs(federate: UcFederate) = groupedConnections.count { it.srcFed == federate }

fun numInputs(federate: UcFederate) = groupedConnections.count { it.destFed == federate }

fun generateNetworkChannelCtor(federate: UcFederate): String =
if (federate == src) {
networkChannel.generateChannelCtorSrc()
"""
${networkChannel.generateChannelCtorSrc()}
${encryptionLayer.generateChannelCtorSrc()}
"""
.trimMargin()
} else {
networkChannel.generateChannelCtorDest()
"""
${networkChannel.generateChannelCtorDest()}
${encryptionLayer.generateChannelCtorDest()}
"""
.trimMargin()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.lflang.generator.uc

import org.lflang.AttributeUtils.getLinkAttribute
import org.lflang.lf.Attribute

enum class EncryptionLayerType {
NoEncryption
}

abstract class UcEncryptionLayer(val type: EncryptionLayerType) {
/** Generate code calling the constructor of the source endpoint */
abstract fun generateChannelCtorSrc(): String

/** Generate code calling the constructor of the destination endpoint */
abstract fun generateChannelCtorDest(): String

abstract val codeType: String

companion object {
fun createEncryptionLayerForBundle(bundle: UcFederatedConnectionBundle): UcEncryptionLayer {
val attr: Attribute? = getLinkAttribute(bundle.groupedConnections.first().lfConn)

if (attr == null) {
return UcNoEncryptionLayer()
} else {
val encryption = attr.getParamString("encryption")

when (encryption) {
"no_encryption" -> return UcNoEncryptionLayer()
}

return UcNoEncryptionLayer()
}
}
}
}

class UcNoEncryptionLayer : UcEncryptionLayer(EncryptionLayerType.NoEncryption) {
override fun generateChannelCtorSrc(): String =
"NoEncryptionLayer_ctor(&self->encryption_layer, (NetworkChannel*)self->channel);"

override fun generateChannelCtorDest(): String =
"NoEncryptionLayer_ctor(&self->encryption_layer, (NetworkChannel*)self->channel);"

override val codeType: String
get() = "NoEncryptionLayer"
}
4 changes: 2 additions & 2 deletions src/serialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int serialize_payload_default(const void *user_struct, size_t user_struct_size,

int generate_message_framing(unsigned char *buffer, size_t message_size, EncryptionIdentifier encryption_identifier) {
MessageFraming *frame = (MessageFraming *)buffer;
frame->header = 0xBEEF;
frame->preamble = 0xBEEF;
frame->protocol_version = 0x0;
frame->message_size = message_size;
frame->crypto_id = encryption_identifier;
Expand All @@ -59,7 +59,7 @@ int generate_message_framing(unsigned char *buffer, size_t message_size, Encrypt
lf_ret_t validate_message_framing(unsigned char *buffer, EncryptionIdentifier expected_encryption_id) {
MessageFraming *frame = (MessageFraming *)buffer;

if (frame->header != 0xBEEF || frame->protocol_version != 0x0 || frame->crypto_id != expected_encryption_id) {
if (frame->preamble != 0xBEEF || frame->protocol_version != 0x0 || frame->crypto_id != expected_encryption_id) {
return LF_ERR;
}

Expand Down

0 comments on commit 828989b

Please sign in to comment.