summaryrefslogtreecommitdiff
path: root/ebus-scala/Main.scala
diff options
context:
space:
mode:
Diffstat (limited to 'ebus-scala/Main.scala')
-rw-r--r--ebus-scala/Main.scala40
1 files changed, 15 insertions, 25 deletions
diff --git a/ebus-scala/Main.scala b/ebus-scala/Main.scala
index 070240f..be8daca 100644
--- a/ebus-scala/Main.scala
+++ b/ebus-scala/Main.scala
@@ -144,16 +144,13 @@ object EbusL2Packet {
def apply(stream: Stream[Int]): Option[Any] = {
var mystream = stream
var rawLength = 0
- def readBytes(n:Int) : List[Int] = {
+ def readBytes(n:Int, msg : String = "") : List[Int] = {
val d = mystream.slice(rawLength,rawLength+n).toList
rawLength = rawLength + n
- println("RAW: " + d.map((it) => "0x%02x".format(it)).foldLeft(""){(s1,s2) => s1+","+s2})
+ println("RAW %15s: ".format(msg) + d.map((it) => "0x%02x".format(it)).foldLeft(""){(s1,s2) => s1+","+s2})
return d
}
- val source :: destination :: primaryCommand :: secondaryCommand :: payloadLength :: _ = readBytes(5)
-
- println("source=0x%02x destination=0x%02x primaryCommand=0x%02x secondaryCommand=0x%02x payloadLength=0x%02x".format(
- source, destination, primaryCommand, secondaryCommand, payloadLength))
+ val source :: destination :: primaryCommand :: secondaryCommand :: payloadLength :: _ = readBytes(5, "QQ|ZZ|PB|SB|NN")
// Maximale payload Grösse = 16 Byte
if (payloadLength > 16) {
@@ -163,16 +160,14 @@ object EbusL2Packet {
// Unklar
println("Überspringe payloadLength mit Länge = 0")
return Some(new FakePacket(rawLength + 1));
- } else {
- println("Payload = " + payloadLength);
}
// "eBUS Spezifikation Physikalische Schicht – OSI 1
// Verbindungsschicht – OSI 2 V.1.3.1" page 8
val payload = Range(0,payloadLength).map({ i =>
- readBytes(1)(0) match {
+ readBytes(1, "payload " + i)(0) match {
case 0xa9 => {
- readBytes(1)(0) match {
+ readBytes(1, "payload esc "+i)(0) match {
case 0x00 => 0xa9
case 0x01 => 0xaa
case code : Int => {
@@ -185,9 +180,8 @@ object EbusL2Packet {
case value : Int => value
}
}).toList
- println("Payload: " + payload.map((it) => "0x%02x".format(it)).foldLeft(""){(s1,s2) => s1+","+s2})
-
- val crc = readBytes(1)(0)
+
+ val crc = readBytes(1,"CRC")(0)
val crcCalc = CRC(List(source, destination, primaryCommand, secondaryCommand, payloadLength) ++ payload)
if (crc != crcCalc) {
println("CRC mismatch (read) 0x%02x != 0x%02x (calc)".format(crc, crcCalc))
@@ -196,7 +190,7 @@ object EbusL2Packet {
// Broadcast Packet ends here
if (destination == 0xfe) {
- val syn = readBytes(1)(0)
+ val syn = readBytes(1,"SYN(broadcast)")(0)
if (syn != 0xaa) {
App.println("bad SYN 0x%02x".format(syn))
return None
@@ -206,10 +200,9 @@ object EbusL2Packet {
payloadLength, payload, crc, rawLength))
}
- val ack = readBytes(1)(0)
- println("ACK=0x%02x".format(ack))
+ val ack = readBytes(1,"ACK")(0)
- val syn = readBytes(1)(0)
+ val syn = readBytes(1, "SYN/NN2")(0)
// Master-Master Packet ends here
if (syn == 0xaa) {
if (ack != 0x00) {
@@ -222,16 +215,15 @@ object EbusL2Packet {
// otherwise this is a Master-Slave Telegramm
// and syn is payloadSlave length
val payloadSlaveLength = syn
- println("payloadSlaveLength = 0x%02x".format(payloadSlaveLength))
if (payloadSlaveLength > 16) {
println("payloadSlaveLength > 16: 0x%02x".format(payloadSlaveLength))
return None
}
val payloadSlave = Range(0,payloadSlaveLength).map({ i =>
- readBytes(1)(0) match {
+ readBytes(1, "payloadSlave "+i)(0) match {
case 0xa9 => {
- readBytes(1)(0) match {
+ readBytes(1,"payloadSlave(esc) "+i)(0) match {
case 0x00 => 0xa9
case 0x01 => 0xaa
case code : Int => {
@@ -244,23 +236,21 @@ object EbusL2Packet {
case value : Int => value
}
}).toList
- println("PayloadSlave: " + payloadSlave.map((it) => "0x%02x".format(it)).foldLeft(""){(s1,s2) => s1+","+s2})
- val crcSlave = readBytes(1)(0)
+ val crcSlave = readBytes(1,"crcSlave")(0)
val crcSlaveCalc = CRC(List(payloadSlaveLength) ++ payloadSlave)
if (crcSlave != crcSlaveCalc) {
println("CRC mismatch (read) 0x%02x != 0x%02x (calc)".format(crcSlave, crcSlaveCalc))
return Some(new FakePacket(rawLength + 2)) // + ACK + SYN
}
- val ackSlave = readBytes(1)(0)
- App.println("ACKslave = 0x%02x".format(ackSlave))
+ val ackSlave = readBytes(1,"ACKslave")(0)
if (ackSlave != 0x00) {
App.println("Master-Slave ACK nicht 0x00 sondern 0x%02x".format(ackSlave))
return None
}
- val synSlave= readBytes(1)(0)
+ val synSlave= readBytes(1,"SYNslave")(0)
if (synSlave != 0xaa) {
App.println("Master-Slave SYN nicht 0xaa sondern 0x%02x".format(synSlave))
return None