Skip to content

Commit

Permalink
bugfix for doip keep alive response
Browse files Browse the repository at this point in the history
minor code improvements
moved client into own package
  • Loading branch information
froks committed Apr 15, 2022
1 parent 221215d commit 79850e3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
@file:Suppress("unused")

package library
package client

import io.ktor.network.selector.*
import io.ktor.network.sockets.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import library.*
import library.DoipUdpMessageHandler.Companion.logger
import java.io.Closeable
import java.nio.ByteBuffer
Expand Down Expand Up @@ -124,6 +125,22 @@ class DoipTcpConnection(socket: Socket, private val testerAddress: Short) {
}
}

// inline fun <reified T, reified Y> callEcu(
// targetAddress: Short,
// message: Y,
// waitTimeout: Duration = 5.seconds
// ): T {
// lateinit var messageOut: ByteArray
// if (message is ByteArray) {
// messageOut = message
// }
// sendDiagnosticMessage(targetAddress, messageOut, waitTimeout = waitTimeout) {
// val returnClass = T::class
// if (returnClass == ByteArray::class) {
// }
// }
// }

fun sendDiagnosticMessage(
targetAddress: Short,
message: ByteArray,
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/library/DoipTcpMessages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package library
import io.ktor.utils.io.*
import library.DoipUdpMessageHandler.Companion.logger
import java.io.OutputStream
import kotlin.experimental.xor
import kotlin.experimental.inv

abstract class DoipTcpMessage : DoipMessage()

Expand All @@ -15,7 +15,7 @@ open class DoipTcpConnectionMessageHandler(
logger.traceIf { "# receiveTcpData" }
val protocolVersion = brc.readByte()
val inverseProtocolVersion = brc.readByte()
if (protocolVersion != (inverseProtocolVersion xor 0xFF.toByte())) {
if (protocolVersion != inverseProtocolVersion.inv()) {
throw IncorrectPatternFormat("Invalid header $protocolVersion != $inverseProtocolVersion xor 0xFF")
}
val payloadType = brc.readShort()
Expand Down Expand Up @@ -209,7 +209,7 @@ class DoipTcpAliveCheckRequest : DoipTcpMessage() {

class DoipTcpAliveCheckResponse(val sourceAddress: Short) : DoipTcpMessage() {
override val asByteArray: ByteArray
get() = doipMessage(TYPE_TCP_ALIVE_RES)
get() = doipMessage(TYPE_TCP_ALIVE_RES, *sourceAddress.toByteArray())
}

class DoipTcpDiagMessage(
Expand Down

0 comments on commit 79850e3

Please sign in to comment.