Skip to content

Commit 5d5add2

Browse files
lylezhu2012MaureenHelm
authored andcommitted
test: shell: GOEP: Optimize TLV-triplet setting
Add a TLV array `tlvs` to keep the pending TLV data. Update `app_param`, `auth_challenge`, and `auth_rsp` to set the one TLV information each time. When the optional argument `last` set, write the all pending TLVs to the tx buffer. Signed-off-by: Lyle Zhu <[email protected]>
1 parent c418d21 commit 5d5add2

File tree

1 file changed

+130
-19
lines changed
  • subsys/bluetooth/host/classic/shell

1 file changed

+130
-19
lines changed

subsys/bluetooth/host/classic/shell/goep.c

Lines changed: 130 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ static struct bt_goep_app goep_app;
4343
static struct bt_goep_transport_rfcomm_server rfcomm_server;
4444
static struct bt_goep_transport_l2cap_server l2cap_server;
4545

46+
#define TLV_COUNT 3
47+
#define TLV_BUFFER_SIZE 64
48+
49+
static struct bt_obex_tlv tlvs[TLV_COUNT];
50+
static uint8_t tlv_buffers[TLV_COUNT][TLV_BUFFER_SIZE];
51+
static uint8_t tlv_count;
52+
4653
static struct bt_goep_app *goep_alloc(struct bt_conn *conn)
4754
{
4855
if (goep_app.conn) {
@@ -76,10 +83,33 @@ struct bt_goep_transport_ops goep_transport_ops = {
7683
.disconnected = goep_transport_disconnected,
7784
};
7885

86+
static bool goep_parse_tlvs_cb(struct bt_obex_tlv *tlv, void *user_data)
87+
{
88+
bt_shell_print("T %02x L %d", tlv->type, tlv->data_len);
89+
bt_shell_hexdump(tlv->data, tlv->data_len);
90+
91+
return true;
92+
}
93+
7994
static bool goep_parse_headers_cb(struct bt_obex_hdr *hdr, void *user_data)
8095
{
8196
bt_shell_print("HI %02x Len %d", hdr->id, hdr->len);
82-
bt_shell_hexdump(hdr->data, hdr->len);
97+
98+
switch (hdr->id) {
99+
case BT_OBEX_HEADER_ID_APP_PARAM:
100+
case BT_OBEX_HEADER_ID_AUTH_CHALLENGE:
101+
case BT_OBEX_HEADER_ID_AUTH_RSP:
102+
int err;
103+
104+
err = bt_obex_tlv_parse(hdr->len, hdr->data, goep_parse_tlvs_cb, NULL);
105+
if (err) {
106+
bt_shell_error("Fail to parse OBEX TLV triplet");
107+
}
108+
break;
109+
default:
110+
bt_shell_hexdump(hdr->data, hdr->len);
111+
break;
112+
}
83113

84114
return true;
85115
}
@@ -655,59 +685,140 @@ static int cmd_add_header_app_param(const struct shell *sh, size_t argc, char *a
655685
{
656686
size_t len;
657687
int err;
658-
const char *hex_payload = argv[1];
659-
size_t hex_payload_size = strlen(hex_payload);
688+
uint8_t tag;
689+
bool last = false;
660690

661-
len = hex2bin(hex_payload, hex_payload_size, add_head_buffer, sizeof(add_head_buffer));
691+
if (tlv_count >= (uint8_t)ARRAY_SIZE(tlvs)) {
692+
shell_warn(sh, "No space of TLV array, add app_param and clear tlvs");
693+
goto add_header;
694+
}
695+
696+
len = hex2bin(argv[1], strlen(argv[1]), &tag, sizeof(tag));
697+
if (len < 1) {
698+
shell_error(sh, "Length should not be zero");
699+
return -ENOEXEC;
700+
}
701+
702+
len = hex2bin(argv[2], strlen(argv[2]), &tlv_buffers[tlv_count][0], TLV_BUFFER_SIZE);
662703
if (len > UINT16_MAX) {
663704
shell_error(sh, "Length exceeds max length (%x > %x)", len, UINT16_MAX);
664705
return -ENOEXEC;
665706
}
666707

667-
err = bt_obex_add_header_app_param(goep_app.tx_buf, (uint16_t)len, add_head_buffer);
708+
if ((argc > 3) && !strcmp(argv[3], "last")) {
709+
last = true;
710+
}
711+
712+
tlvs[tlv_count].type = tag;
713+
tlvs[tlv_count].data_len = len;
714+
tlvs[tlv_count].data = &tlv_buffers[tlv_count][0];
715+
716+
tlv_count++;
717+
718+
if (!last) {
719+
return 0;
720+
}
721+
722+
add_header:
723+
err = bt_obex_add_header_app_param(goep_app.tx_buf, (size_t)tlv_count, tlvs);
668724
if (err) {
669725
shell_error(sh, "Fail to add header app_param");
670726
}
727+
tlv_count = 0;
671728
return err;
672729
}
673730

674731
static int cmd_add_header_auth_challenge(const struct shell *sh, size_t argc, char *argv[])
675732
{
676733
size_t len;
677734
int err;
678-
const char *hex_payload = argv[1];
679-
size_t hex_payload_size = strlen(hex_payload);
735+
uint8_t tag;
736+
bool last = false;
680737

681-
len = hex2bin(hex_payload, hex_payload_size, add_head_buffer, sizeof(add_head_buffer));
738+
if (tlv_count >= (uint8_t)ARRAY_SIZE(tlvs)) {
739+
shell_warn(sh, "No space of TLV array, add auth_challenge and clear tlvs");
740+
goto add_header;
741+
}
742+
743+
len = hex2bin(argv[1], strlen(argv[1]), &tag, sizeof(tag));
744+
if (len < 1) {
745+
shell_error(sh, "Length should not be zero");
746+
return -ENOEXEC;
747+
}
748+
749+
len = hex2bin(argv[2], strlen(argv[2]), &tlv_buffers[tlv_count][0], TLV_BUFFER_SIZE);
682750
if (len > UINT16_MAX) {
683751
shell_error(sh, "Length exceeds max length (%x > %x)", len, UINT16_MAX);
684752
return -ENOEXEC;
685753
}
686754

687-
err = bt_obex_add_header_auth_challenge(goep_app.tx_buf, (uint16_t)len, add_head_buffer);
755+
if ((argc > 3) && !strcmp(argv[3], "last")) {
756+
last = true;
757+
}
758+
759+
tlvs[tlv_count].type = tag;
760+
tlvs[tlv_count].data_len = len;
761+
tlvs[tlv_count].data = &tlv_buffers[tlv_count][0];
762+
763+
tlv_count++;
764+
765+
if (!last) {
766+
return 0;
767+
}
768+
769+
add_header:
770+
err = bt_obex_add_header_auth_challenge(goep_app.tx_buf, (size_t)tlv_count, tlvs);
688771
if (err) {
689772
shell_error(sh, "Fail to add header auth_challenge");
690773
}
774+
tlv_count = 0;
691775
return err;
692776
}
693777

694778
static int cmd_add_header_auth_rsp(const struct shell *sh, size_t argc, char *argv[])
695779
{
696780
size_t len;
697781
int err;
698-
const char *hex_payload = argv[1];
699-
size_t hex_payload_size = strlen(hex_payload);
782+
uint8_t tag;
783+
bool last = false;
700784

701-
len = hex2bin(hex_payload, hex_payload_size, add_head_buffer, sizeof(add_head_buffer));
785+
if (tlv_count >= (uint8_t)ARRAY_SIZE(tlvs)) {
786+
shell_warn(sh, "No space of TLV array, add auth_rsp and clear tlvs");
787+
goto add_header;
788+
}
789+
790+
len = hex2bin(argv[1], strlen(argv[1]), &tag, sizeof(tag));
791+
if (len < 1) {
792+
shell_error(sh, "Length should not be zero");
793+
return -ENOEXEC;
794+
}
795+
796+
len = hex2bin(argv[2], strlen(argv[2]), &tlv_buffers[tlv_count][0], TLV_BUFFER_SIZE);
702797
if (len > UINT16_MAX) {
703798
shell_error(sh, "Length exceeds max length (%x > %x)", len, UINT16_MAX);
704799
return -ENOEXEC;
705800
}
706801

707-
err = bt_obex_add_header_auth_rsp(goep_app.tx_buf, (uint16_t)len, add_head_buffer);
802+
if ((argc > 3) && !strcmp(argv[3], "last")) {
803+
last = true;
804+
}
805+
806+
tlvs[tlv_count].type = tag;
807+
tlvs[tlv_count].data_len = len;
808+
tlvs[tlv_count].data = &tlv_buffers[tlv_count][0];
809+
810+
tlv_count++;
811+
812+
if (!last) {
813+
return 0;
814+
}
815+
816+
add_header:
817+
err = bt_obex_add_header_auth_rsp(goep_app.tx_buf, (size_t)tlv_count, tlvs);
708818
if (err) {
709819
shell_error(sh, "Fail to add header auth_rsp");
710820
}
821+
tlv_count = 0;
711822
return err;
712823
}
713824

@@ -1407,12 +1518,12 @@ SHELL_STATIC_SUBCMD_SET_CREATE(obex_add_header_cmds,
14071518
cmd_add_header_who, 2, 0),
14081519
SHELL_CMD_ARG(conn_id, NULL, "<an identifier used for OBEX connection multiplexing>",
14091520
cmd_add_header_conn_id, 2, 0),
1410-
SHELL_CMD_ARG(app_param, NULL, "<extended application request & response information>",
1411-
cmd_add_header_app_param, 2, 0),
1412-
SHELL_CMD_ARG(auth_challenge, NULL, "<authentication digest-challenge>",
1413-
cmd_add_header_auth_challenge, 2, 0),
1414-
SHELL_CMD_ARG(auth_rsp, NULL, "<authentication digest-response>", cmd_add_header_auth_rsp,
1415-
2, 0),
1521+
SHELL_CMD_ARG(app_param, NULL, "application parameter: <tag> <value> [last]",
1522+
cmd_add_header_app_param, 3, 1),
1523+
SHELL_CMD_ARG(auth_challenge, NULL, "authentication digest-challenge: <tag> <value> [last]",
1524+
cmd_add_header_auth_challenge, 3, 1),
1525+
SHELL_CMD_ARG(auth_rsp, NULL, "authentication digest-response: <tag> <value> [last]",
1526+
cmd_add_header_auth_rsp, 3, 1),
14161527
SHELL_CMD_ARG(creator_id, NULL, "<indicates the creator of an object>",
14171528
cmd_add_header_creator_id, 2, 0),
14181529
SHELL_CMD_ARG(wan_uuid, NULL, "<uniquely identifies the network client (OBEX server)>",

0 commit comments

Comments
 (0)