Skip to content

Commit 34bd7dc

Browse files
lylezhu2012MaureenHelm
authored andcommitted
Bluetooth: GOEP: Support the empty Name header case
According to the OBEX Version 1.5, the Name header could be a empty string. Update function `bt_obex_add_header_name` to support the case that the length of name could be 0. Update function `bt_obex_get_header_name` to support the case that the name header is found but the name length is 0. Signed-off-by: Lyle Zhu <[email protected]>
1 parent 5d5add2 commit 34bd7dc

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

subsys/bluetooth/host/classic/obex.c

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,7 +2007,14 @@ int bt_obex_add_header_name(struct net_buf *buf, uint16_t len, const uint8_t *na
20072007
{
20082008
size_t total;
20092009

2010-
if (!buf || !name || !len) {
2010+
/*
2011+
* OBEX Version 1.5, section 2.2.2 Name
2012+
* The `name` could be a NULL, so the `len` of the name could 0.
2013+
* In some cases an empty Name header is used to specify a particular behavior;
2014+
* see the GET and SETPATH Operations. An empty Name header is defined as a Name
2015+
* header of length 3 (one byte opcode + two byte length).
2016+
*/
2017+
if (!buf || (len && !name)) {
20112018
LOG_WRN("Invalid parameter");
20122019
return -EINVAL;
20132020
}
@@ -2017,14 +2024,16 @@ int bt_obex_add_header_name(struct net_buf *buf, uint16_t len, const uint8_t *na
20172024
return -ENOMEM;
20182025
}
20192026

2020-
if (!bt_obex_string_is_valid(BT_OBEX_HEADER_ID_NAME, len, name)) {
2027+
if (len && !bt_obex_string_is_valid(BT_OBEX_HEADER_ID_NAME, len, name)) {
20212028
LOG_WRN("Invalid string");
20222029
return -EINVAL;
20232030
}
20242031

20252032
net_buf_add_u8(buf, BT_OBEX_HEADER_ID_NAME);
20262033
net_buf_add_be16(buf, (uint16_t)total);
2027-
net_buf_add_mem(buf, name, len);
2034+
if (len) {
2035+
net_buf_add_mem(buf, name, len);
2036+
}
20282037
return 0;
20292038
}
20302039

@@ -2662,6 +2671,7 @@ int bt_obex_header_parse(struct net_buf *buf,
26622671

26632672
struct bt_obex_find_header_data {
26642673
struct bt_obex_hdr hdr;
2674+
bool found;
26652675
};
26662676

26672677
static bool bt_obex_find_header_cb(struct bt_obex_hdr *hdr, void *user_data)
@@ -2691,6 +2701,7 @@ int bt_obex_get_header_count(struct net_buf *buf, uint32_t *count)
26912701
data.hdr.id = BT_OBEX_HEADER_ID_COUNT;
26922702
data.hdr.len = 0;
26932703
data.hdr.data = NULL;
2704+
data.found = false;
26942705

26952706
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
26962707
if (err) {
@@ -2718,13 +2729,14 @@ int bt_obex_get_header_name(struct net_buf *buf, uint16_t *len, const uint8_t **
27182729
data.hdr.id = BT_OBEX_HEADER_ID_NAME;
27192730
data.hdr.len = 0;
27202731
data.hdr.data = NULL;
2732+
data.found = false;
27212733

27222734
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
27232735
if (err) {
27242736
return err;
27252737
}
27262738

2727-
if ((data.hdr.len == 0) || !data.hdr.data) {
2739+
if (!data.found) {
27282740
return -ENODATA;
27292741
}
27302742

@@ -2746,6 +2758,7 @@ int bt_obex_get_header_type(struct net_buf *buf, uint16_t *len, const uint8_t **
27462758
data.hdr.id = BT_OBEX_HEADER_ID_TYPE;
27472759
data.hdr.len = 0;
27482760
data.hdr.data = NULL;
2761+
data.found = false;
27492762

27502763
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
27512764
if (err) {
@@ -2774,6 +2787,7 @@ int bt_obex_get_header_len(struct net_buf *buf, uint32_t *len)
27742787
data.hdr.id = BT_OBEX_HEADER_ID_LEN;
27752788
data.hdr.len = 0;
27762789
data.hdr.data = NULL;
2790+
data.found = false;
27772791

27782792
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
27792793
if (err) {
@@ -2801,6 +2815,7 @@ int bt_obex_get_header_time_iso_8601(struct net_buf *buf, uint16_t *len, const u
28012815
data.hdr.id = BT_OBEX_HEADER_ID_TIME_ISO_8601;
28022816
data.hdr.len = 0;
28032817
data.hdr.data = NULL;
2818+
data.found = false;
28042819

28052820
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
28062821
if (err) {
@@ -2829,6 +2844,7 @@ int bt_obex_get_header_time(struct net_buf *buf, uint32_t *t)
28292844
data.hdr.id = BT_OBEX_HEADER_ID_TIME;
28302845
data.hdr.len = 0;
28312846
data.hdr.data = NULL;
2847+
data.found = false;
28322848

28332849
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
28342850
if (err) {
@@ -2856,6 +2872,7 @@ int bt_obex_get_header_description(struct net_buf *buf, uint16_t *len, const uin
28562872
data.hdr.id = BT_OBEX_HEADER_ID_DES;
28572873
data.hdr.len = 0;
28582874
data.hdr.data = NULL;
2875+
data.found = false;
28592876

28602877
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
28612878
if (err) {
@@ -2884,6 +2901,7 @@ int bt_obex_get_header_target(struct net_buf *buf, uint16_t *len, const uint8_t
28842901
data.hdr.id = BT_OBEX_HEADER_ID_TARGET;
28852902
data.hdr.len = 0;
28862903
data.hdr.data = NULL;
2904+
data.found = false;
28872905

28882906
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
28892907
if (err) {
@@ -2912,6 +2930,7 @@ int bt_obex_get_header_http(struct net_buf *buf, uint16_t *len, const uint8_t **
29122930
data.hdr.id = BT_OBEX_HEADER_ID_HTTP;
29132931
data.hdr.len = 0;
29142932
data.hdr.data = NULL;
2933+
data.found = false;
29152934

29162935
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
29172936
if (err) {
@@ -2940,6 +2959,7 @@ int bt_obex_get_header_body(struct net_buf *buf, uint16_t *len, const uint8_t **
29402959
data.hdr.id = BT_OBEX_HEADER_ID_BODY;
29412960
data.hdr.len = 0;
29422961
data.hdr.data = NULL;
2962+
data.found = false;
29432963

29442964
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
29452965
if (err) {
@@ -2968,6 +2988,7 @@ int bt_obex_get_header_end_body(struct net_buf *buf, uint16_t *len, const uint8_
29682988
data.hdr.id = BT_OBEX_HEADER_ID_END_BODY;
29692989
data.hdr.len = 0;
29702990
data.hdr.data = NULL;
2991+
data.found = false;
29712992

29722993
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
29732994
if (err) {
@@ -2996,6 +3017,7 @@ int bt_obex_get_header_who(struct net_buf *buf, uint16_t *len, const uint8_t **w
29963017
data.hdr.id = BT_OBEX_HEADER_ID_WHO;
29973018
data.hdr.len = 0;
29983019
data.hdr.data = NULL;
3020+
data.found = false;
29993021

30003022
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
30013023
if (err) {
@@ -3024,6 +3046,7 @@ int bt_obex_get_header_conn_id(struct net_buf *buf, uint32_t *conn_id)
30243046
data.hdr.id = BT_OBEX_HEADER_ID_CONN_ID;
30253047
data.hdr.len = 0;
30263048
data.hdr.data = NULL;
3049+
data.found = false;
30273050

30283051
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
30293052
if (err) {
@@ -3079,6 +3102,7 @@ int bt_obex_get_header_app_param(struct net_buf *buf, uint16_t *len, const uint8
30793102
data.hdr.id = BT_OBEX_HEADER_ID_APP_PARAM;
30803103
data.hdr.len = 0;
30813104
data.hdr.data = NULL;
3105+
data.found = false;
30823106

30833107
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
30843108
if (err) {
@@ -3107,6 +3131,7 @@ int bt_obex_get_header_auth_challenge(struct net_buf *buf, uint16_t *len, const
31073131
data.hdr.id = BT_OBEX_HEADER_ID_AUTH_CHALLENGE;
31083132
data.hdr.len = 0;
31093133
data.hdr.data = NULL;
3134+
data.found = false;
31103135

31113136
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
31123137
if (err) {
@@ -3135,6 +3160,7 @@ int bt_obex_get_header_auth_rsp(struct net_buf *buf, uint16_t *len, const uint8_
31353160
data.hdr.id = BT_OBEX_HEADER_ID_AUTH_RSP;
31363161
data.hdr.len = 0;
31373162
data.hdr.data = NULL;
3163+
data.found = false;
31383164

31393165
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
31403166
if (err) {
@@ -3163,6 +3189,7 @@ int bt_obex_get_header_creator_id(struct net_buf *buf, uint32_t *creator_id)
31633189
data.hdr.id = BT_OBEX_HEADER_ID_CREATE_ID;
31643190
data.hdr.len = 0;
31653191
data.hdr.data = NULL;
3192+
data.found = false;
31663193

31673194
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
31683195
if (err) {
@@ -3190,6 +3217,7 @@ int bt_obex_get_header_wan_uuid(struct net_buf *buf, uint16_t *len, const uint8_
31903217
data.hdr.id = BT_OBEX_HEADER_ID_WAN_UUID;
31913218
data.hdr.len = 0;
31923219
data.hdr.data = NULL;
3220+
data.found = false;
31933221

31943222
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
31953223
if (err) {
@@ -3218,6 +3246,7 @@ int bt_obex_get_header_obj_class(struct net_buf *buf, uint16_t *len, const uint8
32183246
data.hdr.id = BT_OBEX_HEADER_ID_OBJECT_CLASS;
32193247
data.hdr.len = 0;
32203248
data.hdr.data = NULL;
3249+
data.found = false;
32213250

32223251
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
32233252
if (err) {
@@ -3247,6 +3276,7 @@ int bt_obex_get_header_session_param(struct net_buf *buf, uint16_t *len,
32473276
data.hdr.id = BT_OBEX_HEADER_ID_SESSION_PARAM;
32483277
data.hdr.len = 0;
32493278
data.hdr.data = NULL;
3279+
data.found = false;
32503280

32513281
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
32523282
if (err) {
@@ -3275,6 +3305,7 @@ int bt_obex_get_header_session_seq_number(struct net_buf *buf, uint32_t *session
32753305
data.hdr.id = BT_OBEX_HEADER_ID_SESSION_SEQ_NUM;
32763306
data.hdr.len = 0;
32773307
data.hdr.data = NULL;
3308+
data.found = false;
32783309

32793310
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
32803311
if (err) {
@@ -3302,6 +3333,7 @@ int bt_obex_get_header_action_id(struct net_buf *buf, uint32_t *action_id)
33023333
data.hdr.id = BT_OBEX_HEADER_ID_ACTION_ID;
33033334
data.hdr.len = 0;
33043335
data.hdr.data = NULL;
3336+
data.found = false;
33053337

33063338
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
33073339
if (err) {
@@ -3329,6 +3361,7 @@ int bt_obex_get_header_dest_name(struct net_buf *buf, uint16_t *len, const uint8
33293361
data.hdr.id = BT_OBEX_HEADER_ID_DEST_NAME;
33303362
data.hdr.len = 0;
33313363
data.hdr.data = NULL;
3364+
data.found = false;
33323365

33333366
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
33343367
if (err) {
@@ -3357,6 +3390,7 @@ int bt_obex_get_header_perm(struct net_buf *buf, uint32_t *perm)
33573390
data.hdr.id = BT_OBEX_HEADER_ID_PERM;
33583391
data.hdr.len = 0;
33593392
data.hdr.data = NULL;
3393+
data.found = false;
33603394

33613395
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
33623396
if (err) {
@@ -3384,6 +3418,7 @@ int bt_obex_get_header_srm(struct net_buf *buf, uint8_t *srm)
33843418
data.hdr.id = BT_OBEX_HEADER_ID_SRM;
33853419
data.hdr.len = 0;
33863420
data.hdr.data = NULL;
3421+
data.found = false;
33873422

33883423
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
33893424
if (err) {
@@ -3411,6 +3446,7 @@ int bt_obex_get_header_srm_param(struct net_buf *buf, uint8_t *srm_param)
34113446
data.hdr.id = BT_OBEX_HEADER_ID_SRM_PARAM;
34123447
data.hdr.len = 0;
34133448
data.hdr.data = NULL;
3449+
data.found = false;
34143450

34153451
err = bt_obex_header_parse(buf, bt_obex_find_header_cb, &data);
34163452
if (err) {

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,21 @@ static int cmd_add_header_name(const struct shell *sh, size_t argc, char *argv[]
463463
{
464464
size_t len;
465465
int err;
466-
const char *hex_payload = argv[1];
467-
size_t hex_payload_size = strlen(hex_payload);
468-
469-
len = hex2bin(hex_payload, hex_payload_size, add_head_buffer, sizeof(add_head_buffer));
470-
if (len > UINT16_MAX) {
471-
shell_error(sh, "Length exceeds max length (%x > %x)", len, UINT16_MAX);
472-
return -ENOEXEC;
466+
const char *hex_payload;
467+
size_t hex_payload_size;
468+
469+
if (argc > 1) {
470+
hex_payload = argv[1];
471+
hex_payload_size = strlen(hex_payload);
472+
473+
len = hex2bin(hex_payload, hex_payload_size, add_head_buffer,
474+
sizeof(add_head_buffer));
475+
if (len > UINT16_MAX) {
476+
shell_error(sh, "Length exceeds max length (%x > %x)", len, UINT16_MAX);
477+
return -ENOEXEC;
478+
}
479+
} else {
480+
len = 0;
473481
}
474482

475483
err = bt_obex_add_header_name(goep_app.tx_buf, (uint16_t)len, add_head_buffer);
@@ -1495,8 +1503,8 @@ static int cmd_goep_server_action(const struct shell *sh, size_t argc, char *arg
14951503
SHELL_STATIC_SUBCMD_SET_CREATE(obex_add_header_cmds,
14961504
SHELL_CMD_ARG(count, NULL, "<number of objects (used by Connect)>", cmd_add_header_count, 2,
14971505
0),
1498-
SHELL_CMD_ARG(name, NULL, "<name of the object (often a file name)>", cmd_add_header_name,
1499-
2, 0),
1506+
SHELL_CMD_ARG(name, NULL, "[name of the object (often a file name)]", cmd_add_header_name,
1507+
1, 1),
15001508
SHELL_CMD_ARG(type, NULL,
15011509
"<type of object - e.g. text, html, binary, manufacturer specific>",
15021510
cmd_add_header_type, 2, 0),

0 commit comments

Comments
 (0)