Skip to content

Commit

Permalink
Merge pull request #577 from NordicSemiconductor/bugfix/testing
Browse files Browse the repository at this point in the history
Test app iimprovements
  • Loading branch information
philips77 authored Aug 28, 2024
2 parents 50cfcb5 + 25e6ac3 commit 883c154
Show file tree
Hide file tree
Showing 27 changed files with 186 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ class ClientConnection @Inject constructor(
* Two requests [BleManager.setWriteCallback], [BleManager.readCharacteristic] have been added and
* when both requests are enqueued successfully, reliable write process will start automatically.
*/
fun testReliableWrite(reliableRequest: ByteArray) {
suspend fun testReliableWrite(reliableRequest: ByteArray) {
beginReliableWrite()
.add(writeCharacteristic(reliableCharacteristics, reliableRequest, WRITE_TYPE_DEFAULT))
.add(readCharacteristic(readCharacteristics))
.enqueue()
.suspend()
}

/**
Expand Down Expand Up @@ -125,30 +125,29 @@ class ClientConnection @Inject constructor(

/**
* Wait for Notification [BleManager.waitForNotification]. It waits until the notification is sent
* from the remote device. Once notification is received, then [WaitForReadRequest.then] it will
* disable notification [BleManager.disableNotifications] request for the given characteristics.
* from the remote device.
*/
fun testWaitForNotification(): WaitForValueChangedRequest {
return waitForNotification(characteristic)
.then { disableNotifications(characteristic)}
}

/**
* Sends the read request to the given characteristic [BleManager.readCharacteristic].
*/
fun testReadCharacteristics(): ReadRequest {
fun testReadCharacteristics(): ReadRequest {
return readCharacteristic(readCharacteristics)
}

/**
* It initiates the atomic request queue [BleManager.beginAtomicRequestQueue] and it will execute the requests in the queue in the order.
* It initiates the atomic request queue [BleManager.beginAtomicRequestQueue] and it will execute
* the requests in the queue in the order.
* The method has two requests and they will execute together. The method is particularly useful
* when the user wants to execute multiple requests simultaneously and ensure they are executed together.
*/
fun testBeginAtomicRequestQueue(request: ByteArray): RequestQueue {
return beginAtomicRequestQueue()
.add(writeCharacteristic(characteristic, request, WRITE_TYPE_DEFAULT))
.add(readCharacteristic(readCharacteristics))
.before { writeCharacteristic(characteristic, request, WRITE_TYPE_DEFAULT) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ class ScanningManager @Inject constructor(
suspend fun scanningForServer(): BluetoothDevice = suspendCancellableCoroutine { continuation ->

val callback = object : ScanCallback() {
var found = false

override fun onScanResult(callbackType: Int, result: ScanResult?) {
if (found) return
result
?.let { continuation.resume(it.device) {} }
?.let {
found = true
continuation.resume(it.device) {}
}
.also { bluetoothLeScanner.stopScan(this) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class ClientTaskPerformer @Inject constructor(
tasks.forEach {
try {
it.start()
_testCases.value = _testCases.value + listOf(TestCase(it.taskName(), true))
_testCases.value += listOf(TestCase(it.taskName(), true))
} catch (e: Exception) {
_testCases.value = _testCases.value + listOf(TestCase(it.taskName(), false))
_testCases.value += listOf(TestCase(it.taskName(), false))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import no.nordicsemi.andorid.ble.test.spec.Callbacks.ATOMIC_REQUEST_QUEUE
import no.nordicsemi.andorid.ble.test.spec.Requests.atomicRequestQueue
import no.nordicsemi.android.ble.ktx.suspend

/**
* Begins an atomic request queue.
*/
class TestBeginAtomicRequestQueue(
private val clientConnection: ClientConnection
) : TaskManager {

// Start the task
override suspend fun start() {
clientConnection.testBeginAtomicRequestQueue(atomicRequestQueue)
clientConnection
.testBeginAtomicRequestQueue(atomicRequestQueue)
.suspend()
}

// Return task name
override fun taskName(): String {
return ATOMIC_REQUEST_QUEUE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ import no.nordicsemi.andorid.ble.test.spec.Callbacks.ENABLE_INDICATION
import no.nordicsemi.andorid.ble.test.spec.FlagBasedPacketMerger
import no.nordicsemi.android.ble.ktx.suspend

/**
* Waits until an indication is received and [FlagBasedPacketMerger]
* efficiently merges and processes the data received from the remote device.
*/
class TestIndication(
private val clientConnection: ClientConnection
) : TaskManager {
/**
* Enables Indication and waits until indication response is received and [FlagBasedPacketMerger] to
* efficiently merge and process the data received from the remote device.
*/

override suspend fun start() {
clientConnection.testEnableIndication().suspend()
clientConnection.testWaitForIndication()
clientConnection
.testWaitForIndication()
.merge(FlagBasedPacketMerger())
.trigger(
clientConnection.testEnableIndication()
)
.suspend()
}

// Return task name
override fun taskName(): String {
val indications = listOf(
ENABLE_INDICATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ import no.nordicsemi.andorid.ble.test.spec.Callbacks
import no.nordicsemi.andorid.ble.test.spec.HeaderBasedPacketMerger
import no.nordicsemi.android.ble.ktx.suspend

/**
* Waits until a notification is received and [HeaderBasedPacketMerger]
* efficiently merges and processes the data received from the remote device.
*/
class TestNotification(
private val clientConnection: ClientConnection
) : TaskManager {
/**
* Enable Notification and waits until notification response is received and [HeaderBasedPacketMerger] to
* efficiently merge and process the data received from the remote device.
*/

override suspend fun start() {
clientConnection.testEnableNotification().suspend()
clientConnection.testWaitForNotification()
clientConnection
.testWaitForNotification()
.merge(HeaderBasedPacketMerger())
.trigger(
clientConnection.testEnableNotification()
)
.suspend()
}

// Return task name
override fun taskName(): String {
val indications = listOf(
Callbacks.ENABLE_NOTIFICATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import no.nordicsemi.andorid.ble.test.server.tasks.TaskManager
import no.nordicsemi.andorid.ble.test.spec.Callbacks.READ_CHA
import no.nordicsemi.android.ble.ktx.suspend

/**
* Reads the characteristics from the remote device.
*/
class TestReadCharacteristics(
private val clientConnection: ClientConnection
) : TaskManager {
// Start the task

override suspend fun start() {
clientConnection.testReadCharacteristics().suspend()
clientConnection
.testReadCharacteristics()
.suspend()
}

// Return task name
override fun taskName(): String {
return READ_CHA
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import no.nordicsemi.andorid.ble.test.server.tasks.TaskManager
import no.nordicsemi.andorid.ble.test.spec.Callbacks.RELIABLE_WRITE
import no.nordicsemi.andorid.ble.test.spec.Requests

/**
* Writes and reads a characteristic value to the remote device using reliable write.
*/
class TestReliableWrite(
private val clientConnection: ClientConnection
) : TaskManager {
// Start the task

override suspend fun start() {
clientConnection.testReliableWrite(Requests.reliableRequest)
clientConnection
.testReliableWrite(Requests.reliableRequest)
}

// Return task name
override fun taskName(): String {
return RELIABLE_WRITE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import no.nordicsemi.andorid.ble.test.spec.Callbacks.WRITE_CHARACTERISTICS
import no.nordicsemi.andorid.ble.test.spec.Requests.writeRequest
import no.nordicsemi.android.ble.ktx.suspend

/**
* Writes the characteristics to the remote device.
*/
class TestWrite(
private val clientConnection: ClientConnection
) : TaskManager {
// Start the task

override suspend fun start() {
clientConnection.testWrite(writeRequest)
clientConnection
.testWrite(writeRequest)
.suspend()
}

// Return task name
override fun taskName(): String {
return WRITE_CHARACTERISTICS
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import no.nordicsemi.andorid.ble.test.spec.Requests.splitterRequest
import no.nordicsemi.android.ble.WriteRequest
import no.nordicsemi.android.ble.ktx.suspend

/**
* Writes the request data to the given characteristics. It utilizes the [WriteRequest.split] callback
* to chunk the data into multiple packets, if the data cannot be sent in a single write operation.
*/
class TestWriteWithDefaultSplitter(
private val clientConnection: ClientConnection
) : TaskManager {

/**
* Writes the request data to the given characteristics. It utilizes the [WriteRequest.split] callback
* to chunk the data into multiple packets, if the data cannot be sent in a single write operation.
*/
override suspend fun start() {
val requestToSend = clientConnection.checkSizeOfRequest(splitterRequest)
clientConnection.testWrite(requestToSend)
clientConnection
.testWrite(requestToSend)
.split()
.suspend()
}

// Return task name
override fun taskName(): String {
return DEFAULT_MTU_SPLITTER
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ import no.nordicsemi.andorid.ble.test.spec.Requests.splitterRequest
import no.nordicsemi.android.ble.WriteRequest
import no.nordicsemi.android.ble.ktx.suspend

/**
* Writes the request data to the given characteristics.
* It utilizes the [WriteRequest.split] callback with [FlagBasedPacketSplitter]
* to chunk the data into multiple packets, if the data cannot be sent in a single write operation.
*/
class TestWriteWithFlagBasedSplitter(
private val clientConnection: ClientConnection
) : TaskManager {
/**
* Writes the request data to the given characteristics. It utilizes the [WriteRequest.split] callback with [FlagBasedPacketSplitter]
* to chunk the data into multiple packets, if the data cannot be sent in a single write operation.
*/

override suspend fun start() {
clientConnection.testWrite(splitterRequest)
clientConnection
.testWrite(splitterRequest)
.split(FlagBasedPacketSplitter())
.suspend()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ import no.nordicsemi.android.ble.WriteRequest
import no.nordicsemi.android.ble.callback.WriteProgressCallback
import no.nordicsemi.android.ble.ktx.suspend

/**
* Writes the request data to the given characteristics. It utilizes the [WriteRequest.split] callback with [HeaderBasedPacketSplitter]
* to chunk the data into multiple packets, if the data cannot be sent in a single write operation. The [WriteProgressCallback] is used to observe the
* packet on each time a packet has been sent.
*/
class TestWriteWithHeaderBasedSplitter(
private val clientConnection: ClientConnection
) : TaskManager {
private val TAG = "WriteProgressCallback"

/**
* Writes the request data to the given characteristics. It utilizes the [WriteRequest.split] callback with [HeaderBasedPacketSplitter]
* to chunk the data into multiple packets, if the data cannot be sent in a single write operation. The [WriteProgressCallback] is used to observe the
* packet on each time a packet has been sent.
*/
override suspend fun start() {
clientConnection.testWrite(splitterRequest)
.split(
HeaderBasedPacketSplitter()
) { _, data, index ->
clientConnection
.testWrite(splitterRequest)
.split(HeaderBasedPacketSplitter()) { _, data, index ->
Log.i(
TAG,
"onPacketSent: Packet size ${data?.size} and index $index "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ class ClientViewModel @Inject constructor(
.apply {
connectDevice(device)
// Start testing tasks after client connection
clientTaskPerformer.startTasks()
clientTaskPerformer.testCases
.onEach {
it.forEach { tc -> updateTestList(tc) }
}
.launchIn(viewModelScope)
clientTaskPerformer.startTasks()
}
}
}
Expand Down
Loading

0 comments on commit 883c154

Please sign in to comment.