BLE variable attribute length

ndjurov
Posts: 10
Joined: Wed Mar 25, 2020 11:34 pm

BLE variable attribute length

Postby ndjurov » Thu Nov 05, 2020 12:10 am

I developed an ESP32 application that uses TCP stack to receive commands from a remote host application, processes them and sends the response back. Each command has different number of parameters, so when receiving a command, the application first reads only one byte that gives a command ID and based on the ID it then reads the remaining number of bytes, which is, like I said, variable, based on the command ID. The command is processed and response is sent back to the host. Response size also depends on the command ID. All commands + parameters are less than 10 bytes and all responses are less than 100 bytes. All this works just fine.
However, I'm now trying to learn how to do the same using BLE. Do I understand correctly that it is not possible to send these variable length packets? If not, can anybody suggest a BLE way of doing this?
Thanks a lot.

chegewara
Posts: 1383
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE variable attribute length

Postby chegewara » Thu Nov 05, 2020 1:18 am

Sure, you can send exactly the same packets and handle it exactly the same way, with very small amount of extra code to pass data from BLE instead of TCP.

ndjurov
Posts: 10
Joined: Wed Mar 25, 2020 11:34 pm

Re: BLE variable attribute length

Postby ndjurov » Fri Nov 06, 2020 10:43 pm

Thank you very much for your reply Che.
Is there any code example that you can direct me to? At the moment I'm thinking of using something like ble_spp_server example, but I'm not sure if I'm on the right path. Also, I certainly don't see this approach as something even remotely close to the use of TCP sockets. So it must be that I'm missing something obvious.
I would really appreciate your further comment on this.

chegewara
Posts: 1383
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE variable attribute length

Postby chegewara » Sun Nov 08, 2020 8:30 pm

Maybe i didnt understand your needs, just focused on part of problem.

With wifi and TCP you have packet with sender and receiver addresses and ports, which is required by TCP packet. When using ble you have connection 1:1 so no need to have those and i focused on data only.
When you have suck code in TCP:

Code: Select all

len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
in ble your "socket" will be characteristic and rx_buffer its value.

I am assuming you have parser in your code that knows command ID, handle data and sends back response. The only difference may be you want to send to another characteristic, but thats not required, you can send back to the same characeristic as well.

ndjurov
Posts: 10
Joined: Wed Mar 25, 2020 11:34 pm

Re: BLE variable attribute length

Postby ndjurov » Mon Nov 09, 2020 3:56 am

Thanks a lot Chegewara,
I guess I created somewhat wrong picture about BLE (completely new field to me). My incorrect understanding was that if I want to get something from the peripheral I have to read that something. Actually, if hope I now understand correctly, just like you said, that a command should be Written to the peripheral (like socket) and the peripheral should write back to the client. Nobody reads anything other than the message that was sent to it.
One only remaining question is whether to write with or without response. Writing with response makes the communication synchronous and simpler to handle but things get complicated if the response never comes. Some examples that I've seen so far suggest that client should use write without response and that peripheral should use indication to reply to the client. Do you think this is the best way to handle this type of communication?
Cheers.

chegewara
Posts: 1383
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE variable attribute length

Postby chegewara » Mon Nov 09, 2020 8:51 pm

Maybe my explanation was not complete:
- BLE peripheral can be either server or client (usually its server),
- assuming that peripheral is a server:
-- you can read or write characteristics value,
-- you can write to characteristic a value and read completely different value, depends on peripheral code,
-- peripheral can send indication/notification, you can consider it is a broadcasting/multicasting,

If you write with no response is like send and forget, similar to UDP, with response is similar to TCP.

What you are trying to do is possible i think, just you need to understand how BLE works. You also have to know that BLE has limited bandwidth.

ndjurov
Posts: 10
Joined: Wed Mar 25, 2020 11:34 pm

Re: BLE variable attribute length

Postby ndjurov » Tue Nov 10, 2020 2:53 am

Great Che, thank you very much. This was really very helpful.
Just one more question - A client sends an open Write (no response) cmd which starts an event handler on the server side. Would it be a problem if the server does a quick processing and immediately sends back a gatt notification to a client from within this event handler?

chegewara
Posts: 1383
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE variable attribute length

Postby chegewara » Tue Nov 10, 2020 10:20 pm

Well, i cant say i know for sure it is possible. I saw some issues when you try to do similar things from event handler, but you do it in few other ways, for example from another task, which can be woken with task notification etc.

Who is online

Users browsing this forum: Bing [Bot] and 42 guests