Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example using with BLE #136

Open
beriberikix opened this issue Jul 15, 2022 · 2 comments
Open

Example using with BLE #136

beriberikix opened this issue Jul 15, 2022 · 2 comments

Comments

@beriberikix
Copy link

Hello! Just discovered this implementation, very cool. I've had the idea to use CoAP over BLE for some time and was wondering if you had any examples of that, perhaps with kable?

@twyatt
Copy link
Member

twyatt commented Jul 25, 2022

I don't have a full fledged example. From the BLE side, SensorTag sample app shows how to use Kable in a multiplatform way. Since the SensorTag doesn't use CoAP at all (that I'm aware of) there wasn't a need to utilize KoAP for that sample.

I'll share a bit about how we're using KoAP w/ Kable, it won't be an end-to-end sample, but might be enough to get you started? Although, it all depends on how the peripheral (firmware) side implements CoAP as to how you should probably go about it on the central (e.g. phone) side.

CoAP feels very much like HTTP (request then response) style protocol, which differs from BLE a bit (which has a collection of services/characteristics/descriptors that can be written/read/observed).

We bridged the paradigms by having a dedicated "send" characteristic (which we use purely for writing) and a dedicated "response" characteristic (which we "observe" via BLE notifications).

As far as how KoAP builds on Kable for that:

You can have a write function, similar to:

suspend fun send(request: Message.Tcp) {
    peripheral.write(sendCharacteristic, request.encode())
}

Then, for processing responses:

val responses = peripheral.observe(responseCharacteristic)
    .map { bytes -> bytes.decode<Tcp>() }

And, to bring it together for a request+response function:

// note: every CoAP request should have a unique token
suspend fun sendForResponse(request: Message.Tcp): Message.Tcp {
    val response = responses
        .onSubscription { send(request) }
        .first { response.token == request.token }
    check(response.code is Message.Code.Response) { "Received invalid response code: ${response.code}" }
    return response
}

You could additionally have helper functions to emulate an API with a HTTP feel:

suspend fun get(model: YourRequestModel): YourResponseModel {
    // e.g. "POST /example/path"
    val request = Message.Tcp(
        code = POST,
        token = generateRandomToken(),
        options = listOf(
            UriPath("example"), UriPath("path"),
            Accept(CBOR),
        ),
        // e.g. use kotlinx.serialization to encode as CBOR, but other formats should work if your firmware supports it
        payload = Cbor.encodeToByteArray(YourRequestModel.serializer(), model),
    )
    val response = sendForResponse(request)
    // todo: handle `Message.Tcp.option`s that are applicable
    return Cbor.decodeFromByteArray(YourResponseModel.serializer(), response.payload)
}

Note, the above code was written by hand so there could be many compilation or logical errors. Use as psuedo code. 🤷

Best of luck!

@beriberikix
Copy link
Author

Thank you writing this up! Hope to play around with this over the weekend.

As a side note, did y'all review the IETF draft https://datatracker.ietf.org/doc/draft-amsuess-core-coap-over-gatt/? It went stale but looks like the author is trying to pick it up again. It follows a similar approach to what we did at a presence company.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants