shared/att: Auto retry if request fails
Luiz Augusto von Dentz
maintainerPATCH 1/11 shared/att: Auto retry if request fails
From: Luiz Augusto von Dentz <[email protected]>
This adds auto retry logic to requests when a security error happen, it will first attempt to elevate the security to match the requirement and then retry sending the request once again.
--- src/shared/att.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 4 deletions(-)
src/shared/att.c +54 -4
@@ -576,6 +576,55 @@ static bool disconnect_cb(struct io *io, void *user_data)return false; } +static bool change_security (struct bt_att *att,uint8_t ecode) +{ +int security; + + security =bt_att_get_security (att); +if (ecode ==BT_ATT_ERROR_INSUFFICIENT_ENCRYPTION && + security <BT_ATT_SECURITY_MEDIUM ) + security =BT_ATT_SECURITY_MEDIUM ; +else if (ecode ==BT_ATT_ERROR_AUTHENTICATION && + security <BT_ATT_SECURITY_HIGH ) + security =BT_ATT_SECURITY_HIGH ; +else +return false; + +return bt_att_set_security (att, security); +} + +static bool handle_error_rsp (struct bt_att *att,uint8_t *pdu, +ssize_t pdu_len,uint8_t *opcode) +{ +const struct bt_att_pdu_error_rsp *rsp; +struct att_send_op *op = att->pending_req; + +if (pdu_len !=sizeof (*rsp)) { + *opcode =0 ; +return false; + } + + rsp = (void *) pdu; + + *opcode = rsp->opcode; + +/* Only try to change security on L2CAP */ +if (!att->io_on_l2cap) +return false; + +/* Attempt to change security */ +if (!change_security (att, rsp->ecode)) +return false; + +util_debug (att->debug_callback, att->debug_data, +"Retrying operation %p" , op); + + att->pending_req =NULL ; + +/* Push operation back to request queue */ +return queue_push_head (att->req_queue, op); +} +static void handle_rsp (struct bt_att *att,uint8_t opcode,uint8_t *pdu,ssize_t pdu_len) {
@@ -601,10 +650,11 @@ static void handle_rsp(struct bt_att *att, uint8_t opcode, uint8_t *pdu,* the request is malformed, end the current request with failure. */ if (opcode ==BT_ATT_OP_ERROR_RSP ) { -if (pdu_len !=4 ) - goto fail; - - req_opcode = pdu[0 ]; +/* Return if error response cause a retry */ +if (handle_error_rsp (att, pdu, pdu_len, &req_opcode)) { +wakeup_writer (att); +return ; + } }else if (!(req_opcode =get_req_opcode (opcode))) goto fail;
Signature · 1 line
2.1.0
Luiz Augusto von Dentz
maintainerPATCH 2/11 shared/att: Add BT_ATT_SECURITY_AUTO
From: Luiz Augusto von Dentz <[email protected]>
This security level enables auto elevation (on by default), this would be useful for unit tests since it can now control this behavior (default for non L2CAP transport is BT_ATT_SECURITY_LOW).
--- src/shared/att-types.h | 2 +- src/shared/att.c | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-)
src/shared/att-types.h +1 -1
@@ -27,7 +27,7 @@#define __packed __attribute__((packed))#endif -#define BT_ATT_SECURITY_NONE 0 +#define BT_ATT_SECURITY_AUTO 0#define BT_ATT_SECURITY_LOW 1#define BT_ATT_SECURITY_MEDIUM 2#define BT_ATT_SECURITY_HIGH 3
src/shared/att.c +5 -5
@@ -581,6 +581,9 @@ static bool change_security(struct bt_att *att, uint8_t ecode)int security; security =bt_att_get_security (att); +if (security !=BT_ATT_SECURITY_AUTO ) +return false; +if (ecode ==BT_ATT_ERROR_INSUFFICIENT_ENCRYPTION && security <BT_ATT_SECURITY_MEDIUM ) security =BT_ATT_SECURITY_MEDIUM ;
@@ -608,10 +611,6 @@ static bool handle_error_rsp(struct bt_att *att, uint8_t *pdu, *opcode = rsp->opcode; -/* Only try to change security on L2CAP */ -if (!att->io_on_l2cap) -return false; -/* Attempt to change security */ if (!change_security (att, rsp->ecode))return false;
@@ -1416,7 +1415,8 @@ bool bt_att_set_security(struct bt_att *att, int level) {struct bt_security sec; -if (!att || level <BT_ATT_SECURITY_LOW || level >BT_ATT_SECURITY_HIGH ) +if (!att || level <BT_ATT_SECURITY_AUTO || + level >BT_ATT_SECURITY_HIGH )return false;if (!att->io_on_l2cap) {
Signature · 1 line
2.1.0
Luiz Augusto von Dentz
maintainerPATCH 3/11 unit/test-gatt: Use auto security
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
If the test don't expect any errors to happen use set security to auto so bt_att will take care of elevating the security whenever needed.
--- unit/test-gatt.c | 4 ++++ 1 file changed, 4 insertions(+)
unit/test-gatt.c +4 -0
@@ -630,6 +630,10 @@ static void client_ready_cb(bool success, uint8_t att_ecode, void *user_data)if (context->data->step) {const struct test_step *step = context->data->step; +/* Auto elevate security for test that don't expect error */ +if (!step->expected_att_ecode) +bt_att_set_security (context->att,BT_ATT_SECURITY_AUTO ); + step->func (context);return ; }
Signature · 1 line
2.1.0
Luiz Augusto von Dentz
maintainerPATCH 4/11 unit/test-gatt: Add /TP/GAR/CL/BI-04-C/auto test
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
/TP/GAR/CL/BI-04-C/auto is similar to TP/GAR/CL/BI-04-C but automatically elevates the security level and retry.
--- unit/test-gatt.c | 8 ++++++++ 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
@@ -2691,6 +2691,14 @@ int main(int argc, char *argv[])raw_pdu (0x0a ,0x03 ,0x00 ), raw_pdu(0x01, 0x0a, 0x03, 0x00, 0x05)); +define_test_client ("/TP/GAR/CL/BI-04-C/auto" , test_client, service_db_1, + &test_read_1, +SERVICE_DATA_1_PDUS , +raw_pdu (0x0a ,0x03 ,0x00 ), +raw_pdu (0x01 ,0x0a ,0x03 ,0x00 ,0x05 ), +raw_pdu (0x0a ,0x03 ,0x00 ), +raw_pdu (0x0b ,0x01 ,0x02 ,0x03 )); + define_test_client("/TP/GAR/CL/BI-05-C", test_client, service_db_1, &test_read_6, SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
Luiz Augusto von Dentz
maintainerPATCH 5/11 unit/test-gatt: Add /TP/GAR/CL/BI-16-C/auto test
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
/TP/GAR/CL/BI-16-C/auto is similar to /TP/GAR/CL/BI-16-C but automatically elevates the security level and retry.
--- unit/test-gatt.c | 8 ++++++++ 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
@@ -3041,6 +3041,14 @@ int main(int argc, char *argv[])raw_pdu (0x0c ,0x03 ,0x00 ,0x00 ,0x00 ), raw_pdu(0x01, 0x0c, 0x03, 0x00, 0x05)); +define_test_client ("/TP/GAR/CL/BI-16-C/auto" , test_client, service_db_1, + &test_long_read_1, +SERVICE_DATA_1_PDUS , +raw_pdu (0x0c ,0x03 ,0x00 ,0x00 ,0x00 ), +raw_pdu (0x01 ,0x0c ,0x03 ,0x00 ,0x05 ), +raw_pdu (0x0c ,0x03 ,0x00 ,0x00 ,0x00 ), +raw_pdu (0x0d ,0x01 ,0x02 ,0x03 )); + define_test_client("/TP/GAR/CL/BI-17-C", test_client, service_db_1, &test_long_read_8, SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
Luiz Augusto von Dentz
maintainerPATCH 6/11 unit/test-gatt: Add /TP/GAR/CL/BI-21-C/auto test
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
/TP/GAR/CL/BI-21-C/auto is similar to /TP/GAR/CL/BI-21-C but automatically elevates the security level and retry.
--- unit/test-gatt.c | 8 ++++++++ 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
@@ -3079,6 +3079,14 @@ int main(int argc, char *argv[])raw_pdu (0x0e ,0x03 ,0x00 ,0x07 ,0x00 ), raw_pdu(0x01, 0x0e, 0x03, 0x00, 0x05)); +define_test_client ("/TP/GAR/CL/BI-21-C/auto" , test_client, service_db_1, + &test_multiple_read_1, +SERVICE_DATA_1_PDUS , +raw_pdu (0x0e ,0x03 ,0x00 ,0x07 ,0x00 ), +raw_pdu (0x01 ,0x0e ,0x03 ,0x00 ,0x05 ), +raw_pdu (0x0e ,0x03 ,0x00 ,0x07 ,0x00 ), +raw_pdu (0x0f ,0x01 ,0x02 ,0x03 )); + define_test_client("/TP/GAR/CL/BI-22-C", test_client, service_db_1, &test_multiple_read_6, SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
Luiz Augusto von Dentz
maintainerPATCH 7/11 unit/test-gatt: Add /TP/GAR/CL/BI-26-C/auto test
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
/TP/GAR/CL/BI-26-C/auto is similar to /TP/GAR/CL/BI-26-C but automatically elevates the security level and retry.
--- unit/test-gatt.c | 8 ++++++++ 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
@@ -3282,6 +3282,14 @@ int main(int argc, char *argv[])raw_pdu (0x0a ,0x04 ,0x00 ), raw_pdu(0x01, 0x0a, 0x04, 0x00, 0x05)); +define_test_client ("/TP/GAR/CL/BI-26-C/auto" , test_client, service_db_1, + &test_read_7, +SERVICE_DATA_1_PDUS , +raw_pdu (0x0a ,0x04 ,0x00 ), +raw_pdu (0x01 ,0x0a ,0x04 ,0x00 ,0x05 ), +raw_pdu (0x0a ,0x04 ,0x00 ), +raw_pdu (0x0b ,0x01 ,0x02 ,0x03 )); + define_test_client("/TP/GAR/CL/BI-27-C", test_client, service_db_1, &test_read_11, SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
Luiz Augusto von Dentz
maintainerPATCH 8/11 unit/test-gatt: Add /TP/GAR/CL/BI-32-C/auto test
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
/TP/GAR/CL/BI-32-C/auto is similar to /TP/GAR/CL/BI-32-C but automatically elevates the security level and retry.
--- unit/test-gatt.c | 8 ++++++++ 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
@@ -3442,6 +3442,14 @@ int main(int argc, char *argv[])raw_pdu (0x0c ,0x04 ,0x00 ,0x00 ,0x00 ), raw_pdu(0x01, 0x0c, 0x04, 0x00, 0x05)); +define_test_client ("/TP/GAR/CL/BI-32-C/auto" , test_client, service_db_1, + &test_long_read_9, +SERVICE_DATA_1_PDUS , +raw_pdu (0x0c ,0x04 ,0x00 ,0x00 ,0x00 ), +raw_pdu (0x01 ,0x0c ,0x04 ,0x00 ,0x05 ), +raw_pdu (0x0c ,0x04 ,0x00 ,0x00 ,0x00 ), +raw_pdu (0x0d ,0x01 ,0x02 ,0x03 )); + define_test_client("/TP/GAR/CL/BI-33-C", test_client, service_db_1, &test_long_read_15, SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
Luiz Augusto von Dentz
maintainerPATCH 9/11 unit/test-gatt: Add /TP/GAW/CL/BI-05-C/auto test
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
/TP/GAW/CL/BI-05-C/auto is similar to /TP/GAW/CL/BI-05-C but automatically elevates the security level and retry.
--- unit/test-gatt.c | 8 ++++++++ 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
@@ -3515,6 +3515,14 @@ int main(int argc, char *argv[])raw_pdu (0x12 ,0x07 ,0x00 ,0x01 ,0x02 ,0x03 ), raw_pdu(0x01, 0x12, 0x07, 0x00, 0x05)); +define_test_client ("/TP/GAW/CL/BI-05-C/auto" , test_client, service_db_1, + &test_write_1, +SERVICE_DATA_1_PDUS , +raw_pdu (0x12 ,0x07 ,0x00 ,0x01 ,0x02 ,0x03 ), +raw_pdu (0x01 ,0x12 ,0x07 ,0x00 ,0x05 ), +raw_pdu (0x12 ,0x07 ,0x00 ,0x01 ,0x02 ,0x03 ), +raw_pdu (0x13 )); + define_test_client("/TP/GAW/CL/BI-06-C", test_client, service_db_1, &test_write_6, SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
Luiz Augusto von Dentz
maintainerPATCH 10/11 unit/test-gatt: Add /TP/GAW/CL/BI-18-C/auto test
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
/TP/GAW/CL/BI-18-C/auto is similar to /TP/GAW/CL/BI-18-C but automatically elevates the security level and retry.
--- unit/test-gatt.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
unit/test-gatt.c +10 -0
@@ -3943,6 +3943,16 @@ int main(int argc, char *argv[])raw_pdu (0x18 ,0x00 ), raw_pdu(0x19)); +define_test_client ("/TP/GAW/CL/BI-18-C/auto" , test_client, service_db_1, + &test_reliable_write_1, +SERVICE_DATA_1_PDUS , +raw_pdu (0x16 ,0x07 ,0x00 ,0x00 ,0x00 ,0x01 ,0x02 ,0x03 ), +raw_pdu (0x01 ,0x16 ,0x07 ,0x00 ,0x05 ), +raw_pdu (0x16 ,0x07 ,0x00 ,0x00 ,0x00 ,0x01 ,0x02 ,0x03 ), +raw_pdu (0x17 ,0x07 ,0x00 ,0x00 ,0x00 ,0x01 ,0x02 ,0x03 ), +raw_pdu (0x18 ,0x01 ), +raw_pdu (0x19 )); + define_test_client("/TP/GAW/CL/BI-19-C", test_client, service_db_1, &test_reliable_write_6, SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
Luiz Augusto von Dentz
maintainerPATCH 11/11 unit/test-gatt: Add /TP/GAW/CL/BI-23-C/auto test
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
/TP/GAW/CL/BI-23-C/auto is similar to /TP/GAW/CL/BI-23-C but automatically elevates the security level and retry.
--- unit/test-gatt.c | 8 ++++++++ 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
@@ -4059,6 +4059,14 @@ int main(int argc, char *argv[])raw_pdu (0x12 ,0x08 ,0x00 ,0x01 ,0x02 ,0x03 ), raw_pdu(0x01, 0x12, 0x08, 0x00, 0x05)); +define_test_client ("/TP/GAW/CL/BI-23-C/auto" , test_client, service_db_1, + &test_write_7, +SERVICE_DATA_1_PDUS , +raw_pdu (0x12 ,0x08 ,0x00 ,0x01 ,0x02 ,0x03 ), +raw_pdu (0x01 ,0x12 ,0x08 ,0x00 ,0x05 ), +raw_pdu (0x12 ,0x08 ,0x00 ,0x01 ,0x02 ,0x03 ), +raw_pdu (0x13 )); + define_test_client("/TP/GAW/CL/BI-24-C", test_client, service_db_1, &test_write_12, SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
Hi,
Quoted 86 lines · Luiz Augusto von Dentz
From: Luiz Augusto von Dentz <[email protected]>
This adds auto retry logic to requests when a security error happen, it will first attempt to elevate the security to match the requirement and then retry sending the request once again.
--- src/shared/att.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 4 deletions(-)src/shared/att.c +54
-5@@ -576,6 +576,55 @@ static bool disconnect_cb(struct io *io, void *user_data)return false; } +static bool change_security (struct bt_att *att,uint8_t ecode) +{ +int security; + + security =bt_att_get_security (att); +if (ecode ==BT_ATT_ERROR_INSUFFICIENT_ENCRYPTION && + security <BT_ATT_SECURITY_MEDIUM ) + security =BT_ATT_SECURITY_MEDIUM ; +else if (ecode ==BT_ATT_ERROR_AUTHENTICATION && + security <BT_ATT_SECURITY_HIGH ) + security =BT_ATT_SECURITY_HIGH ; +else +return false; + +return bt_att_set_security (att, security); +} + +static bool handle_error_rsp (struct bt_att *att,uint8_t *pdu, +ssize_t pdu_len,uint8_t *opcode) +{ +const struct bt_att_pdu_error_rsp *rsp; +struct att_send_op *op = att->pending_req; + +if (pdu_len !=sizeof (*rsp)) { + *opcode =0 ; +return false; + } + + rsp = (void *) pdu; + + *opcode = rsp->opcode; + +/* Only try to change security on L2CAP */ +if (!att->io_on_l2cap) +return false; + +/* Attempt to change security */ +if (!change_security (att, rsp->ecode)) +return false; + +util_debug (att->debug_callback, att->debug_data, +"Retrying operation %p" , op); + + att->pending_req =NULL ; + +/* Push operation back to request queue */ +return queue_push_head (att->req_queue, op); +} +static void handle_rsp (struct bt_att *att,uint8_t opcode,uint8_t *pdu,ssize_t pdu_len) {@@ -601,10 +650,11 @@ static void handle_rsp(struct bt_att *att, uint8_t opcode, uint8_t *pdu,* the request is malformed, end the current request with failure. */ if (opcode ==BT_ATT_OP_ERROR_RSP ) { -if (pdu_len !=4 ) - goto fail; - - req_opcode = pdu[0 ]; +/* Return if error response cause a retry */ +if (handle_error_rsp (att, pdu, pdu_len, &req_opcode)) { +wakeup_writer (att); +return ; + } }else if (!(req_opcode =get_req_opcode (opcode))) goto fail; --
Quoted 1 line
2.1.0
Applied.
Signature · 1 line
Luiz Augusto von Dentz