diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2011-12-24 01:45:46 +0100 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2011-12-24 01:45:46 +0100 |
commit | 3ee2a9071f6226bdd797d0cafd61044c1f3a139e (patch) | |
tree | 7a9ab19903142181f6d04ec49fe7583142808b6a | |
parent | d3d5bd4344e0cac23eda536144c62d08429f2fca (diff) | |
download | ebus-alt-3ee2a9071f6226bdd797d0cafd61044c1f3a139e.tar.gz ebus-alt-3ee2a9071f6226bdd797d0cafd61044c1f3a139e.zip |
ebus-scala: restructered files
-rw-r--r-- | ebus-scala/.settings/org.eclipse.jdt.core.prefs | 12 | ||||
-rwxr-xr-x | ebus-scala/run.sh | 4 | ||||
-rwxr-xr-x | ebus-scala/src/main/scala/org/xapek/ebus/Ebus.scala (renamed from ebus-scala/Main.scala) | 118 | ||||
-rw-r--r-- | ebus-scala/src/main/scala/org/xapek/ebus/Main.scala | 97 |
4 files changed, 110 insertions, 121 deletions
diff --git a/ebus-scala/.settings/org.eclipse.jdt.core.prefs b/ebus-scala/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 33e19bd..0000000 --- a/ebus-scala/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,12 +0,0 @@ -#Sat Jul 02 17:29:58 CEST 2011 -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.6 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.6 diff --git a/ebus-scala/run.sh b/ebus-scala/run.sh deleted file mode 100755 index 6b137eb..0000000 --- a/ebus-scala/run.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -exec scala "$0" "$@" -#! -App.main(args) diff --git a/ebus-scala/Main.scala b/ebus-scala/src/main/scala/org/xapek/ebus/Ebus.scala index da54e71..3db87ac 100755 --- a/ebus-scala/Main.scala +++ b/ebus-scala/src/main/scala/org/xapek/ebus/Ebus.scala @@ -1,6 +1,6 @@ +package org.xapek.ebus + import Stream._ -import java.io.{InputStream, EOFException} -import java.net.{InetAddress, InetSocketAddress, Socket} import scala.xml.{XML, Elem, Node, NodeSeq} /// Ebus Bus Definition @@ -65,7 +65,7 @@ object EbusField { /* def getFields(ebusDefinition : EbusDefinition, primaryCommand: Char, secondaryCommand: Char): List[EbusField] = { val packetDef = ebusDefinition.packet(primaryCommand, secondaryCommand) for (fieldDef <- (packetDef \ "fields").head.child) { - App.println(buildField(fieldDef)) + println(buildField(fieldDef)) } null } @@ -81,7 +81,7 @@ object EbusField { /// Ebus Protocol Parser -object Ebus { +object EbusProtocol { def SYN = 0xaa def ACK_OK = 0x00 def ACK_FAIL = 0xff @@ -220,7 +220,7 @@ object EbusL2Packet { val payload = Range(0,payloadLength).map(readPayloadByte).toList val crc = readBytes(1,"CRC")(0) - val crcCalc = Ebus.crc(List(source, destination, primaryCommand, secondaryCommand, payloadLength) ++ payload) + val crcCalc = EbusProtocol.crc(List(source, destination, primaryCommand, secondaryCommand, payloadLength) ++ payload) if (crc != crcCalc) { throw new EbusL2ParseException("CRC mismatch (read) 0x%02x != 0x%02x (calc)".format(crc, crcCalc), stream.slice(0, rawLength).toList, rawLength + 1); @@ -229,7 +229,7 @@ object EbusL2Packet { // Broadcast Packet ends here if (destination == 0xfe) { val syn = readBytes(1,"SYN(broadcast)")(0) - if (syn != Ebus.SYN) { + if (syn != EbusProtocol.SYN) { throw new EbusL2ParseException("Bad SYN: 0x%02x".format(syn), stream.slice(0, rawLength).toList, rawLength); } @@ -240,8 +240,8 @@ object EbusL2Packet { val syn = readBytes(1, "SYN/NN2")(0) // Master-Master Packet ends here - if (syn == Ebus.SYN) { - if (ack != Ebus.ACK_OK) { + if (syn == EbusProtocol.SYN) { + if (ack != EbusProtocol.ACK_OK) { throw new EbusL2ParseException("Bad ACK: 0x%02x".format(ack), stream.slice(0, rawLength).toList, rawLength); } @@ -259,20 +259,20 @@ object EbusL2Packet { val payloadSlave = Range(0,payloadSlaveLength).map(readPayloadByte).toList val crcSlave = readBytes(1,"crcSlave")(0) - val crcSlaveCalc = Ebus.crc(List(payloadSlaveLength) ++ payloadSlave) + val crcSlaveCalc = EbusProtocol.crc(List(payloadSlaveLength) ++ payloadSlave) if (crcSlave != crcSlaveCalc) { throw new EbusL2ParseException("CRC mismatch (read) 0x%02x != 0x%02x (calc)".format(crcSlave, crcSlaveCalc), stream.slice(0, rawLength).toList, rawLength); } val ackSlave = readBytes(1,"ACKslave")(0) - if (ackSlave != Ebus.ACK_OK) { + if (ackSlave != EbusProtocol.ACK_OK) { throw new EbusL2ParseException("Master-Slave ACK nicht 0x00 sondern 0x%02x".format(ackSlave), stream.slice(0, rawLength).toList, rawLength); } val synSlave= readBytes(1,"SYNslave")(0) - if (synSlave != Ebus.SYN) { + if (synSlave != EbusProtocol.SYN) { throw new EbusL2ParseException("Master-Slave SYN nicht 0xaa sondern 0x%02x".format(synSlave), stream.slice(0, rawLength).toList, rawLength); } @@ -287,7 +287,7 @@ class EbusReader(var stream : Stream[Int]) { def next() : EbusL2Packet = { while (true) { // Überspringe Synchronisationszeichen - stream = stream.dropWhile(_ == Ebus.SYN) + stream = stream.dropWhile(_ == EbusProtocol.SYN) try { val packet = EbusL2Packet(stream) @@ -296,7 +296,7 @@ class EbusReader(var stream : Stream[Int]) { } catch { case exc : EbusL2ParseException => { stream = stream.drop(exc.skipbytes) - App.println("Exception: " + exc.message + "\n\tData: " + + println("Exception: " + exc.message + "\n\tData: " + exc.data.map((it) => "0x%02x".format(it)).reduceRight((s1,s2) => s1+","+s2)) } } @@ -313,95 +313,3 @@ class EbusL7MasterSlavePacket(l2Packet: EbusL2MasterSlavePacket) extends EbusL7P class EbusL7BroadcastPacket(l2Packet: EbusL2BroadcastPacket) extends EbusL7Packet(l2Packet) {}; - -/// Application -object App { - val ebusDefinition = EbusDefinition.fromFile("../ebus-xml/ebus.xml") - var source: InputStream = null - - def println(msg : Any) { - Predef.println("[ebus]\t" + msg.toString) - } - - def main(args: Array[String]): Unit = { - args.toList match { - case "-" :: List() => { - source = System.in - } - case host :: port :: List() => { - val addr = InetAddress.getByName(host) - val sockAddr = new InetSocketAddress(addr, port.toInt) - val s = new Socket() - s.connect(sockAddr) - println("Connected to %s %d".format(args(0), args(1).toInt)) - source = s.getInputStream - } - case "dump" :: host :: port :: List() => { - val addr = InetAddress.getByName(host) - val sockAddr = new InetSocketAddress(addr, port.toInt) - val s = new Socket() - s.connect(sockAddr) - System.err.println("Connected to %s %d".format(args(1), args(2).toInt)) - while (true) { - val v = s.getInputStream.read - if (v == -1) { - println("Fehler: read == -1") - System.exit(1) - } - System.out.write(v) - } - } - case _ => { - println( -"""Usage: PROGRAM <ARGUMENTS> -ARGUMENTS: -- - Parse Ebus from stdin -dump HOST PORT - Read and Dump TCP Connection -HOST PORT - Parse Ebus from TCP Connection -""") - exit(1) - } - } - - var stream: Stream[Int] = Stream.continually({ - val value = source.read - if (value == -1) - throw new EOFException("End of File reached (read returned -1)") - value - }) - - - // Synchronisiere - while (stream(0) != Ebus.SYN) { - stream = stream.drop(1) - } - - val reader = new EbusReader(stream) - (() => { - while (true) { - try { - val packet = reader.next - val sourceDevice = ebusDefinition.device(packet.l2Header.source) - val destDevice = ebusDefinition.device(packet.l2Header.destination) - val packetDef = ebusDefinition.packet(packet.l2Header.primaryCommand, - packet.l2Header.secondaryCommand) - println(sourceDevice) - println(destDevice) - println(packetDef) - //println(packet) - } catch { - case exc : EOFException => { - println("EOF") - return; - } - } - } - })() - println("exiting") - source.close - } -} - diff --git a/ebus-scala/src/main/scala/org/xapek/ebus/Main.scala b/ebus-scala/src/main/scala/org/xapek/ebus/Main.scala new file mode 100644 index 0000000..fc5022e --- /dev/null +++ b/ebus-scala/src/main/scala/org/xapek/ebus/Main.scala @@ -0,0 +1,97 @@ +package org.xapek.ebus + +import java.io.{InputStream, EOFException} +import java.net.{InetAddress, InetSocketAddress, Socket} +import Stream._ + + +object App { + val ebusDefinition = EbusDefinition.fromFile("../ebus-xml/ebus.xml") + var source: InputStream = null + + def println(msg : Any) { + Predef.println("[ebus]\t" + msg.toString) + } + + def main(args: Array[String]): Unit = { + args.toList match { + case "-" :: List() => { + source = System.in + } + case host :: port :: List() => { + val addr = InetAddress.getByName(host) + val sockAddr = new InetSocketAddress(addr, port.toInt) + val s = new Socket() + s.connect(sockAddr) + println("Connected to %s %d".format(args(0), args(1).toInt)) + source = s.getInputStream + } + case "dump" :: host :: port :: List() => { + val addr = InetAddress.getByName(host) + val sockAddr = new InetSocketAddress(addr, port.toInt) + val s = new Socket() + s.connect(sockAddr) + System.err.println("Connected to %s %d".format(args(1), args(2).toInt)) + while (true) { + val v = s.getInputStream.read + if (v == -1) { + println("Fehler: read == -1") + System.exit(1) + } + System.out.write(v) + } + } + case _ => { + println( +"""Usage: PROGRAM <ARGUMENTS> +ARGUMENTS: +- + Parse Ebus from stdin +dump HOST PORT + Read and Dump TCP Connection +HOST PORT + Parse Ebus from TCP Connection +""") + exit(1) + } + } + + var stream: Stream[Int] = Stream.continually({ + val value = source.read + if (value == -1) + throw new EOFException("End of File reached (read returned -1)") + value + }) + + + // Synchronisiere + while (stream(0) != EbusProtocol.SYN) { + stream = stream.drop(1) + } + + val reader = new EbusReader(stream) + (() => { + while (true) { + try { + val packet = reader.next + val sourceDevice = ebusDefinition.device(packet.l2Header.source) + val destDevice = ebusDefinition.device(packet.l2Header.destination) + val packetDef = ebusDefinition.packet(packet.l2Header.primaryCommand, + packet.l2Header.secondaryCommand) + println(sourceDevice) + println(destDevice) + println(packetDef) + //println(packet) + } catch { + case exc : EOFException => { + println("EOF") + return; + } + } + } + })() + println("exiting") + source.close + } +} + |