From 8b70bf1a14c61bff2329f5fefab114f3e2e8a6d4 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Mon, 29 Mar 2021 19:47:24 +0800 Subject: [PATCH] add WIP TCPInterface --- .../mesh/service/SerialInterface.kt | 4 +- .../mesh/service/StreamInterface.kt | 6 +- .../geeksville/mesh/service/TCPInterface.kt | 75 +++++++++++++++++++ 3 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/geeksville/mesh/service/TCPInterface.kt diff --git a/app/src/main/java/com/geeksville/mesh/service/SerialInterface.kt b/app/src/main/java/com/geeksville/mesh/service/SerialInterface.kt index 877ddb5ae..5912a255b 100644 --- a/app/src/main/java/com/geeksville/mesh/service/SerialInterface.kt +++ b/app/src/main/java/com/geeksville/mesh/service/SerialInterface.kt @@ -19,8 +19,8 @@ import com.hoho.android.usbserial.util.SerialInputOutputManager /** * An interface that assumes we are talking to a meshtastic device via USB serial */ -class SerialInterface(service: RadioInterfaceService, address: String) : - StreamInterface(service, address), Logging, SerialInputOutputManager.Listener { +class SerialInterface(service: RadioInterfaceService, private val address: String) : + StreamInterface(service), Logging, SerialInputOutputManager.Listener { companion object : Logging { /** diff --git a/app/src/main/java/com/geeksville/mesh/service/StreamInterface.kt b/app/src/main/java/com/geeksville/mesh/service/StreamInterface.kt index ad0f3dcf1..717805ef6 100644 --- a/app/src/main/java/com/geeksville/mesh/service/StreamInterface.kt +++ b/app/src/main/java/com/geeksville/mesh/service/StreamInterface.kt @@ -6,7 +6,7 @@ import com.geeksville.android.Logging /** * An interface that assumes we are talking to a meshtastic device over some sort of stream connection (serial or TCP probably) */ -abstract class StreamInterface(protected val service: RadioInterfaceService, val address: String) : +abstract class StreamInterface(protected val service: RadioInterfaceService) : Logging, IRadioInterface { companion object : Logging { @@ -49,6 +49,9 @@ abstract class StreamInterface(protected val service: RadioInterfaceService, val abstract fun sendBytes(p: ByteArray) + /// If subclasses need to flash at the end of a packet they can implement + open fun flushBytes() {} + override fun handleSendToRadio(p: ByteArray) { // This method is called from a continuation and it might show up late, so check for uart being null @@ -60,6 +63,7 @@ abstract class StreamInterface(protected val service: RadioInterfaceService, val sendBytes(header) sendBytes(p) + flushBytes() } diff --git a/app/src/main/java/com/geeksville/mesh/service/TCPInterface.kt b/app/src/main/java/com/geeksville/mesh/service/TCPInterface.kt new file mode 100644 index 000000000..8b8e755a9 --- /dev/null +++ b/app/src/main/java/com/geeksville/mesh/service/TCPInterface.kt @@ -0,0 +1,75 @@ +package com.geeksville.mesh.service + +import java.io.* +import java.net.InetAddress +import java.net.Socket +import kotlin.concurrent.thread + + +class TCPInterface(service: RadioInterfaceService, private val address: String) : + StreamInterface(service) { + + var socket: Socket? = null + lateinit var outStream: OutputStream + lateinit var inStream: InputStream + + init { + connect() + } + + override fun sendBytes(p: ByteArray) { + outStream.write(p) + } + + override fun flushBytes() { + outStream.flush() + } + + override fun onDeviceDisconnect(waitForStopped: Boolean) { + val s = socket + if(s != null) { + socket = null + outStream.close() + inStream.close() + s.close() + } + super.onDeviceDisconnect(waitForStopped) + } + + override fun connect() { + //here you must put your computer's IP address. + //here you must put your computer's IP address. + val addr = InetAddress.getByName(address) + + debug("TCP connecting to $address") + + //create a socket to make the connection with the server + + //create a socket to make the connection with the server + val port = 4403 + val s = Socket(addr, port) + s.tcpNoDelay = true + socket = s + outStream = BufferedOutputStream(s.getOutputStream()) + inStream = BufferedInputStream(s.getInputStream()) + + // No need to keep a reference to this thread - it will exit when we close inStream + thread(start = true, isDaemon = true, name = "TCP reader") { + try { + while(true) { + val c = inStream.read() + if(c == -1) + break; + else + readChar(c.toByte()) + } + } + catch(ex: Throwable) { + errormsg("Exception in TCP reader: $ex") + onDeviceDisconnect(false) + } + debug("Exiting TCP reader") + } + super.connect() + } +} \ No newline at end of file