add WIP TCPInterface

pull/276/head^2
Kevin Hester 2021-03-29 19:47:24 +08:00
rodzic 1ebc710006
commit 8b70bf1a14
3 zmienionych plików z 82 dodań i 3 usunięć

Wyświetl plik

@ -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 {
/**

Wyświetl plik

@ -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()
}

Wyświetl plik

@ -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()
}
}