Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

Bluetooth SDK

stan4233_475378
Employee
10 replies posted 10 sign-ins 5 replies posted
Employee

Hello,

I was trying the AnyCloud BLE Environmental Sensing Service example on a PSoC 6 Wi-Fi BT Prototyping Kit and noticed some errors.

The code periodically reads a thermistor and prints it on the console. If one runs the Cypress CySmart Android app, browses to the Gatt service and activates the "Notify" option, the app gets notified by the PSoC board of the temperature reading.

The problem is: turning on Notify will cause the BLE to abruptly disconnect within a short period (about 20 sec). The reason is the handler for write failed to send a response to the phone.

In app_bt_gatt_handler.c ( https://github.com/Infineon/mtb-example-anycloud-ble-ess/blob/master/app_bt_gatt_handler.c),

Function app_gatts_attr_req_handler():

        case GATT_REQ_WRITE:
        case GATT_CMD_WRITE:
             gatt_status = app_gatt_attr_write_handler(p_attr_req->opcode,
                                                       &p_attr_req->data.write_req,
                                                       p_attr_req->len_requested );
                                                       
             if (p_attr_req->opcode == GATT_REQ_WRITE)
             {
                if(gatt_status == WICED_BT_GATT_INVALID_ATTR_LEN)
                {
                    wiced_bt_gatt_server_send_error_rsp(p_attr_req->conn_id,
                                                        p_attr_req->opcode,
                                                        p_attr_req->data.read_req.handle,
                                                        WICED_BT_GATT_INVALID_ATTR_LEN);
                }
                if(gatt_status == WICED_BT_GATT_INVALID_HANDLE)
                {
                    wiced_bt_gatt_server_send_error_rsp(p_attr_req->conn_id,
                                                        p_attr_req->opcode,
                                                        p_attr_req->data.read_req.handle,
                                                        WICED_BT_GATT_INVALID_HANDLE);
                }
             }

When the call to app_gatt_attr_write_handler() returns with WICED_BT_GATT_SUCCESS, there needs to be a call to wiced_bt_gatt_server_send_write_rsp() to send the response. This is missing in the code.

This line is also dubious: if (p_attr_req->opcode == GATT_REQ_WRITE). What about the GATT_CMD_WRITE case?

Furthermore, on closer examination, even the parameter passed to wiced_bt_gatt_server_send_error_rsp() appears to be wrong: it passes p_attr_req->data.read_req.handle for a WRITE case. It should have used p_attr_req->data.write_req.handle.

Besides, the 2 separate calls to wiced_bt_gatt_server_send_error_rsp() can be reduced to 1 call: wiced_bt_gatt_server_send_error_rsp(..., gatt_status).

The error handling code for READ has similar mistakes:

        case GATT_REQ_READ:
        case GATT_REQ_READ_BLOB:
             gatt_status = app_gatt_attr_read_handler(p_attr_req->conn_id,
                                                      p_attr_req->opcode,
                                                     &p_attr_req->data.read_req,
                                                      p_attr_req->len_requested);
             if ( (p_attr_req->opcode == GATT_REQ_READ) ||
                  (p_attr_req->opcode == GATT_REQ_READ_BLOB) )
             {
                if(gatt_status == WICED_BT_GATT_SUCCESS)
                {
                    wiced_bt_gatt_server_send_write_rsp(p_attr_req->conn_id,
                                                        p_attr_req->opcode,
                                                        p_attr_req->data.write_req.handle);
                }
                else
                {
                    wiced_bt_gatt_server_send_error_rsp(p_attr_req->conn_id,
                                                        p_attr_req->opcode,
                                                        p_attr_req->data.write_req.handle,
                                                        gatt_status);
                }
             }


The code invokes wiced_bt_gatt_server_send_write_rsp() and passes p_attr_req->data.write_req.handle for a READ case. It should have used wiced_bt_gatt_server_send_read_handle_rsp() and used p_attr_req->data.read_req.handle.

These 2 lines also appear redundant. The switch case already assures that opcode has those values.
if ( (p_attr_req->opcode == GATT_REQ_READ) ||
(p_attr_req->opcode == GATT_REQ_READ_BLOB) )

I hope the code can be improved so others won't have to contend with the abrupt disconnection.

Best Regards,
SK

0 Likes
1 Solution
BragadeeshV
Moderator
Moderator First question asked 1000 replies posted 750 replies posted
Moderator

Hi @stan4233_475378 ,

The environmental sensing example uses an earlier version of the BTSTACK where the read response and write response was sent automatically. In the latest stack the responses has to be provided by the application and this example is not updated as per the latest BTSTACK. You can refer to the Battery server example that handles the different events correctly as per the latest stack.

https://github.com/Infineon/mtb-example-anycloud-ble-battery-server

Thank you for pointing out this issue to us. We will work on updating this project.

Regards,
Bragadeesh

View solution in original post

0 Likes
1 Reply
BragadeeshV
Moderator
Moderator First question asked 1000 replies posted 750 replies posted
Moderator

Hi @stan4233_475378 ,

The environmental sensing example uses an earlier version of the BTSTACK where the read response and write response was sent automatically. In the latest stack the responses has to be provided by the application and this example is not updated as per the latest BTSTACK. You can refer to the Battery server example that handles the different events correctly as per the latest stack.

https://github.com/Infineon/mtb-example-anycloud-ble-battery-server

Thank you for pointing out this issue to us. We will work on updating this project.

Regards,
Bragadeesh
0 Likes