Skip to content

Commit

Permalink
add option to change nrc code and interval for sending pending nrc
Browse files Browse the repository at this point in the history
  • Loading branch information
froks committed Jun 3, 2024
1 parent 12ecd3a commit b2f04a7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
16 changes: 8 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ plugins {
apply<NexusReleasePlugin>()

group = "io.github.doip-sim-ecu"
version = "0.12.0"
version = "0.12.1"

repositories {
gradlePluginPortal()
Expand Down Expand Up @@ -113,13 +113,13 @@ publishing {
}
}

tasks.cyclonedxBom {
setIncludeConfigs(listOf("runtimeClasspath"))
setIncludeLicenseText(false)
outputName.set("sbom")
outputFormat.set("all")
includeBomSerialNumber.set(true)
}
//tasks.cyclonedxBom {
// setIncludeConfigs(listOf("runtimeClasspath"))
// setIncludeLicenseText(false)
// outputName.set("sbom")
// outputFormat.set("all")
// includeBomSerialNumber.set(true)
//}

allOpen {
annotation("helper.Open")
Expand Down
22 changes: 22 additions & 0 deletions src/main/kotlin/SimDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ public open class ResponseData<T>(
public val pendingFor: Duration?
get() = _pendingFor

public val pendingForInterval: Duration?
get() = _pendingForInterval

public val pendingForNrc: Byte?
get() = _pendingForNrc

public val pendingForCallback: () -> Unit
get() = _pendingForCallback

Expand All @@ -156,6 +162,8 @@ public open class ResponseData<T>(
private var _response: ByteArray = ByteArray(0)
private var _continueMatching: Boolean = false
private var _pendingFor: Duration? = null
private var _pendingForInterval: Duration? = null
private var _pendingForNrc: Byte? = null
private var _pendingForCallback: () -> Unit = {}
private var _hardResetEntityFor: Duration? = null

Expand Down Expand Up @@ -262,6 +270,20 @@ public open class ResponseData<T>(
_pendingForCallback = callback
}

/**
* Overrides the default interval for sending pending NRC
*/
public fun pendingForInterval(duration: Duration) {
_pendingForInterval = duration
}

/**
* Changes the default busy wait NRC (0x78) to something different
*/
public fun pendingForNrc(nrc: Byte) {
_pendingForNrc = nrc
}

/**
* Pretend to hard-reset entity by disconnecting the current, nd not being reachable for duration,
* and sending a vam after being reachable again
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/SimEcu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,20 @@ public class SimEcu(private val data: EcuData) : SimulatedEcu(data.toEcuConfig()

private fun handlePending(request: UdsMessage, responseData: ResponseData<RequestMatcher>) {
val pendingFor = responseData.pendingFor ?: return

val pendingForInterval = responseData.pendingForInterval?.inWholeMilliseconds ?: config.pendingNrcSendInterval.inWholeMilliseconds
// this code will send pending nrcs every `config.pendingNrcSendInterval`, until
// the pendingFor duration is reached. Afterward the `pendingForCallback` is invoked,
// which may again change the final response in ResponseData
val pending = byteArrayOf(0x7f, request.message[0], NrcError.RequestCorrectlyReceivedButResponseIsPending)
val pending = byteArrayOf(0x7f, request.message[0],
responseData.pendingForNrc ?: NrcError.RequestCorrectlyReceivedButResponseIsPending)
val end = System.currentTimeMillis() + pendingFor.inWholeMilliseconds
while (System.currentTimeMillis() < end) {
sendResponse(request, pending)
logger.logForRequest(responseData.caller) { "Request for $name: '${request.message.toHexString(limit = 10, limitExceededByteCount = true)}' matched '${responseData.caller}' -> Pending '${pending.toHexString(limit = 10, limitExceededByteCount = true)}'" }
if (end - System.currentTimeMillis() <= config.pendingNrcSendInterval.inWholeMilliseconds) {
if (end - System.currentTimeMillis() <= pendingForInterval) {
Thread.sleep(end - System.currentTimeMillis())
} else {
Thread.sleep(config.pendingNrcSendInterval.inWholeMilliseconds)
Thread.sleep(pendingForInterval)
}
}
try {
Expand Down

0 comments on commit b2f04a7

Please sign in to comment.