Skip to content

Instantly share code, notes, and snippets.

@Malinskiy
Created April 28, 2020 09:48
Show Gist options
  • Save Malinskiy/56555c4f29ca8f4c6626780fe3dc2d0d to your computer and use it in GitHub Desktop.
Save Malinskiy/56555c4f29ca8f4c6626780fe3dc2d0d to your computer and use it in GitHub Desktop.
abstract class Request(val target: Target = HostTarget) {
/**
* Some requests require a device serial to be passed to the request itself by means of <host-prefix>
* @see https://android.googlesource.com/platform/system/core/+/refs/heads/master/adb/SERVICES.TXT
*/
abstract fun serialize(): ByteArray
open suspend fun handshake(readChannel: AndroidReadChannel, writeChannel: AndroidWriteChannel) {
val request = serialize()
writeChannel.write(request)
val response = readChannel.read()
if (!response.okay) {
log.warn { "adb server rejected command ${String(request, Const.DEFAULT_TRANSPORT_ENCODING)}" }
throw RequestRejectedException(response.message ?: "no message received")
}
}
protected fun createBaseRequest(request: String): ByteArray {
val fullRequest = target.serialize() + request
return String.format("%04X%s", fullRequest.length, fullRequest)
.toByteArray(Const.DEFAULT_TRANSPORT_ENCODING)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment