All Downloads are FREE. Search and download functionalities are using the official Maven repository.

library.SimulatedEcu.kt Maven / Gradle / Ivy

Go to download

This is a kotlin based domain specific language (dsl), to quickly and intuitively write custom DoIP ECU simulations.

The newest version!
package library

import NrcError
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.util.concurrent.atomic.AtomicBoolean

public open class SimulatedEcu(public val config: EcuConfig) {
    /**
     * Name of the ECU
     */
    public val name: String =
        config.name

    private val logger: Logger = LoggerFactory.getLogger(SimulatedEcu::class.java)

    private val isBusy: AtomicBoolean = AtomicBoolean(false)

    internal open fun simStarted() {
    }

    public open fun reset() {
    }

    public open fun start() {
    }

    /**
     * Handler for an incoming diagnostic message when the ECU isn't busy
     */
    public open fun handleRequest(request: UdsMessage) {
        logger.debugIf { "Handle Request message: ${request.message.toHexString(limit = 20)} for $name" }
    }

    /**
     * Handler that is called when the ecu is currently busy with another request
     */
    public open fun handleRequestIfBusy(request: UdsMessage) {
        // Busy NRC
        logger.debugIf { "ECU $name is busy, sending busy-NRC" }
        request.respond(byteArrayOf(0x7f, request.message[0], NrcError.BusyRepeatRequest))
    }

    /**
     * Called on incoming diagnostic messages for this ECU
     */
    public open fun onIncomingUdsMessage(request: UdsMessage) {
        return if (isBusy.compareAndSet(false, true)) {
            try {
                handleRequest(request)
            } finally {
                isBusy.set(false)
            }
        } else {
            handleRequestIfBusy(request)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy