-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
214 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
android/app/src/main/kotlin/io/rebble/cobble/bluetooth/classic/SocketSerialDriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package io.rebble.cobble.bluetooth.classic | ||
|
||
import io.rebble.cobble.bluetooth.BlueIO | ||
import io.rebble.cobble.bluetooth.PebbleBluetoothDevice | ||
import io.rebble.cobble.bluetooth.SingleConnectionStatus | ||
import io.rebble.cobble.bluetooth.readFully | ||
import io.rebble.cobble.datasources.IncomingPacketsListener | ||
import io.rebble.libpebblecommon.ProtocolHandler | ||
import io.rebble.libpebblecommon.packets.QemuPacket | ||
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import timber.log.Timber | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import java.net.Socket | ||
import java.nio.ByteBuffer | ||
import java.nio.ByteOrder | ||
import kotlin.coroutines.coroutineContext | ||
|
||
/** | ||
* Used for testing app via a qemu pebble | ||
*/ | ||
class SocketSerialDriver( | ||
private val protocolHandler: ProtocolHandler, | ||
private val incomingPacketsListener: IncomingPacketsListener | ||
): BlueIO { | ||
|
||
private var inputStream: InputStream? = null | ||
private var outputStream: OutputStream? = null | ||
|
||
private suspend fun readLoop() { | ||
try { | ||
val buf: ByteBuffer = ByteBuffer.allocate(8192) | ||
|
||
while (coroutineContext.isActive) { | ||
val inputStream = inputStream ?: break | ||
/* READ PACKET META */ | ||
inputStream.readFully(buf, 0, 4) | ||
|
||
val qemuPacket = QemuPacket.deserialize(buf.array().asUByteArray()) | ||
if (qemuPacket.protocol.get() != UShort.MAX_VALUE) { | ||
Timber.d("QEMU packet ${qemuPacket.protocol.get()}") | ||
} | ||
val sppPacket = qemuPacket as? QemuPacket.QemuSPP ?: continue | ||
|
||
buf.rewind() | ||
inputStream.readFully(buf, 4, sppPacket.length.get().toInt()) | ||
buf.rewind() | ||
|
||
val metBuf = ByteBuffer.wrap(buf.array()) | ||
metBuf.order(ByteOrder.BIG_ENDIAN) | ||
val length = metBuf.short | ||
val endpoint = metBuf.short | ||
if (length < 0 || length > buf.capacity()) { | ||
Timber.w("Invalid length in packet (EP ${endpoint.toUShort()}): got ${length.toUShort()}") | ||
continue | ||
} | ||
|
||
Timber.d("Got packet: EP ${ProtocolEndpoint.getByValue(endpoint.toUShort())} | Length ${length.toUShort()}") | ||
|
||
buf.rewind() | ||
val packet = ByteArray(length.toInt() + 2 * (Short.SIZE_BYTES)) | ||
buf.get(packet, 0, packet.size) | ||
incomingPacketsListener.receivedPackets.emit(packet) | ||
protocolHandler.receivePacket(packet.toUByteArray()) | ||
} | ||
} finally { | ||
Timber.e("Read loop returning") | ||
try { | ||
withContext(Dispatchers.IO) { | ||
inputStream?.close() | ||
} | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} finally { | ||
inputStream = null | ||
} | ||
|
||
try { | ||
withContext(Dispatchers.IO) { | ||
outputStream?.close() | ||
} | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} finally { | ||
outputStream = null | ||
} | ||
} | ||
} | ||
|
||
@FlowPreview | ||
override fun startSingleWatchConnection(device: PebbleBluetoothDevice): Flow<SingleConnectionStatus> = flow { | ||
val host = device.address | ||
coroutineScope { | ||
emit(SingleConnectionStatus.Connecting(device)) | ||
|
||
val serialSocket = withContext(Dispatchers.IO) { | ||
Socket(host, 12344) | ||
} | ||
|
||
delay(8000) | ||
|
||
val sendLoop = launch { | ||
protocolHandler.startPacketSendingLoop(::sendPacket) | ||
} | ||
|
||
inputStream = serialSocket.inputStream | ||
outputStream = serialSocket.outputStream | ||
|
||
readLoop() | ||
try { | ||
withContext(Dispatchers.IO) { | ||
serialSocket.close() | ||
} | ||
} catch (_: IOException) { | ||
} | ||
sendLoop.cancel() | ||
} | ||
} | ||
|
||
private suspend fun sendPacket(bytes: UByteArray): Boolean { | ||
//Timber.d("Sending packet of EP ${PebblePacket(bytes.toUByteArray()).endpoint}") | ||
val qemuPacket = QemuPacket.QemuSPP(bytes) | ||
val outputStream = outputStream ?: return false | ||
withContext(Dispatchers.IO) { | ||
outputStream.write(qemuPacket.serialize().toByteArray()) | ||
} | ||
return true | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.