Created
April 28, 2020 09:48
-
-
Save Malinskiy/56555c4f29ca8f4c6626780fe3dc2d0d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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