This commit is contained in:
Gilles Boccon-Gibod
2024-09-24 17:15:53 -07:00
parent 55f99e6887
commit c91695c23a
6 changed files with 150 additions and 115 deletions

View File

@@ -17,86 +17,113 @@ package com.github.google.bumble.btbench;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.renderscript.RSInvalidStateException;
import android.util.Log;
import com.google.android.mobly.snippet.Snippet;
import com.google.android.mobly.snippet.rpc.Rpc;
import androidx.test.core.app.ApplicationProvider;
import org.json.JSONException;
import org.json.JSONObject;
public class AutomationSnippet implements Snippet {
private static final String TAG = "btbench.snippet";
private final BluetoothAdapter mBluetoothAdapter;
private AppViewModel rfcommServerModel;
private RfcommServer rfcommServer;
private AppViewModel l2capServerModel;
private L2capServer l2capServer;
private final Context mContext;
public AutomationSnippet() {
Context context = ApplicationProvider.getApplicationContext();
BluetoothManager bluetoothManager = context.getSystemService(BluetoothManager.class);
mContext = ApplicationProvider.getApplicationContext();
BluetoothManager bluetoothManager = mContext.getSystemService(BluetoothManager.class);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
throw new RuntimeException("bluetooth not supported");
}
}
private static JSONObject throughputStats(AppViewModel model) throws JSONException {
private void runScenario(AppViewModel model, String mode, String scenario) {
Mode runner;
switch (mode) {
case "rfcomm-client":
runner = new RfcommClient(model, mBluetoothAdapter, (PacketIO packetIO) -> createIoClient(model, scenario, packetIO));
break;
case "rfcomm-server":
runner = new RfcommServer(model, mBluetoothAdapter, (PacketIO packetIO) -> createIoClient(model, scenario, packetIO));
break;
case "l2cap-client":
runner = new L2capClient(model, mBluetoothAdapter, mContext, (PacketIO packetIO) -> createIoClient(model, scenario, packetIO));
break;
case "l2cap-server":
runner = new L2capServer(model, mBluetoothAdapter, (PacketIO packetIO) -> createIoClient(model, scenario, packetIO));
break;
default:
return;
}
runner.run(true);
}
private IoClient createIoClient(AppViewModel model, String scenario, PacketIO packetIO) {
switch (scenario) {
case "send":
return new Sender(model, packetIO);
case "receive":
return new Receiver(model, packetIO);
case "ping":
return new Pinger(model, packetIO);
case "pong":
return new Ponger(model, packetIO);
default:
return null;
}
}
private static JSONObject resultFromModel(AppViewModel model) throws JSONException {
JSONObject result = new JSONObject();
JSONObject stats = new JSONObject();
result.put("stats", stats);
JSONObject throughputStats = new JSONObject();
stats.put("throughput", throughputStats);
throughputStats.put("average", model.getThroughput());
JSONObject rttStats = new JSONObject();
stats.put("rtt", rttStats);
rttStats.put("compound", model.getStats());
return result;
}
@Rpc(description = "Run an RFComm client throughput test")
public JSONObject runRfcommClient(String peerBluetoothAddress, int packetCount, int packetSize) throws JSONException {
assert(mBluetoothAdapter != null);
@Rpc(description = "Run a scenario in RFComm Client mode")
public JSONObject runRfcommClient(String scenario, String peerBluetoothAddress, int packetCount, int packetSize, int packetInterval) throws JSONException {
assert (mBluetoothAdapter != null);
AppViewModel model = new AppViewModel();
model.setPeerBluetoothAddress(peerBluetoothAddress);
model.setSenderPacketCount(packetCount);
model.setSenderPacketSize(packetSize);
model.setSenderPacketInterval(packetInterval);
//RfcommClient rfCommClient = new RfcommClient(model, mBluetoothAdapter);
//rfCommClient.run(true);
return throughputStats(model);
runScenario(model, "rfcomm-client", scenario);
return resultFromModel(model);
}
@Rpc(description = "Run an L2CAP client throughput test")
public JSONObject runL2capClient(String peerBluetoothAddress, int psm, boolean use_2m_phy, int packetCount, int packetSize) throws JSONException {
assert(mBluetoothAdapter != null);
@Rpc(description = "Run a scenario in L2CAP Client mode")
public JSONObject runL2capClient(String scenario, String peerBluetoothAddress, int psm, boolean use_2m_phy, int packetCount, int packetSize, int packetInterval) throws JSONException {
assert (mBluetoothAdapter != null);
AppViewModel model = new AppViewModel();
model.setPeerBluetoothAddress(peerBluetoothAddress);
model.setL2capPsm(psm);
model.setUse2mPhy(use_2m_phy);
model.setSenderPacketCount(packetCount);
model.setSenderPacketSize(packetSize);
model.setSenderPacketInterval(packetInterval);
Context context = ApplicationProvider.getApplicationContext();
//L2capClient l2capClient = new L2capClient(model, mBluetoothAdapter, context);
//l2capClient.run(true);
return throughputStats(model);
}
@Rpc(description = "Run an RFComm server")
public JSONObject runRfcommServer() throws JSONException {
assert(mBluetoothAdapter != null);
if (rfcommServerModel != null) {
rfcommServerModel.abort();
rfcommServerModel = null;
rfcommServer = null;
}
rfcommServerModel = new AppViewModel();
//rfcommServer = new RfcommServer(rfcommServerModel, mBluetoothAdapter);
//rfcommServer.run(true);
return new JSONObject();
runScenario(model, "l2cap-client", scenario);
return resultFromModel(model);
}
@Override

View File

@@ -168,12 +168,25 @@ class MainActivity : ComponentActivity() {
appViewModel.senderPacketInterval = packetInterval
}
appViewModel.updateSenderPacketSizeSlider()
intent.getStringExtra("scenario")?.let {
when (it) {
"send" -> appViewModel.scenario = SEND_SCENARIO
"receive" -> appViewModel.scenario = RECEIVE_SCENARIO
"ping" -> appViewModel.scenario = PING_SCENARIO
"pong" -> appViewModel.scenario = PONG_SCENARIO
}
}
intent.getStringExtra("mode")?.let {
when (it) {
"rfcomm-client" -> appViewModel.mode = RFCOMM_CLIENT_MODE
"rfcomm-server" -> appViewModel.mode = RFCOMM_SERVER_MODE
"l2cap-client" -> appViewModel.mode = L2CAP_CLIENT_MODE
"l2cap-server" -> appViewModel.mode = L2CAP_SERVER_MODE
}
}
intent.getStringExtra("autostart")?.let {
when (it) {
"rfcomm-client" -> runRfcommClient()
"rfcomm-server" -> runRfcommServer()
"l2cap-client" -> runL2capClient()
"l2cap-server" -> runL2capServer()
"run-scenario" -> runScenario()
"scan-start" -> runScan(true)
"stop-start" -> runScan(false)
}
@@ -200,6 +213,11 @@ class MainActivity : ComponentActivity() {
runner.run(false)
}
private fun runScan(startScan: Boolean) {
val scan = bluetoothAdapter?.let { Scan(it) }
scan?.run(startScan)
}
private fun createIoClient(packetIo: PacketIO): IoClient {
return when (appViewModel.scenario) {
SEND_SCENARIO -> Sender(appViewModel, packetIo)
@@ -210,30 +228,6 @@ class MainActivity : ComponentActivity() {
}
}
private fun runRfcommClient() {
// val rfcommClient = bluetoothAdapter?.let { RfcommClient(appViewModel, it) }
// rfcommClient?.run()
}
private fun runRfcommServer() {
// val rfcommServer = bluetoothAdapter?.let { RfcommServer(appViewModel, it) }
// rfcommServer?.run()
}
private fun runL2capClient() {
// val l2capClient = bluetoothAdapter?.let { L2capClient(appViewModel, it, baseContext) }
// l2capClient?.run()
}
private fun runL2capServer() {
// val l2capServer = bluetoothAdapter?.let { L2capServer(appViewModel, it) }
// l2capServer?.run()
}
private fun runScan(startScan: Boolean) {
val scan = bluetoothAdapter?.let { Scan(it) }
scan?.run(startScan)
}
@SuppressLint("MissingPermission")
fun becomeDiscoverable() {

View File

@@ -64,6 +64,7 @@ class SocketClient(
try {
ioClient.run()
socket.close()
} catch (error: IOException) {
Log.info("run ended abruptly")
}

View File

@@ -23,6 +23,7 @@ public class HciProxy {
HciHal hciHal = HciHal.create(new HciHalCallback() {
@Override
public void onPacket(HciPacket.Type type, byte[] packet) {
Log.d(TAG, String.format("CONTROLLER->HOST: type=%s, size=%d", type, packet.length));
mServer.sendPacket(type, packet);
switch (type) {
@@ -83,7 +84,7 @@ public class HciProxy {
@Override
public void onPacket(HciPacket.Type type, byte[] packet) {
Log.d(TAG, String.format("onPacket: type=%s, size=%d", type, packet.length));
Log.d(TAG, String.format("HOST->CONTROLLER: type=%s, size=%d", type, packet.length));
hciHal.sendPacket(type, packet);
switch (type) {