liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include "get_folder_items.h"
 
namespace bluetooth {
namespace avrcp {
 
std::unique_ptr<GetFolderItemsResponseBuilder>
GetFolderItemsResponseBuilder::MakePlayerListBuilder(Status status,
                                                     uint16_t uid_counter,
                                                     size_t mtu) {
  std::unique_ptr<GetFolderItemsResponseBuilder> builder(
      new GetFolderItemsResponseBuilder(Scope::MEDIA_PLAYER_LIST, status,
                                        uid_counter, mtu));
 
  return builder;
}
 
std::unique_ptr<GetFolderItemsResponseBuilder>
GetFolderItemsResponseBuilder::MakeVFSBuilder(Status status,
                                              uint16_t uid_counter,
                                              size_t mtu) {
  std::unique_ptr<GetFolderItemsResponseBuilder> builder(
      new GetFolderItemsResponseBuilder(Scope::VFS, status, uid_counter, mtu));
 
  return builder;
}
 
std::unique_ptr<GetFolderItemsResponseBuilder>
GetFolderItemsResponseBuilder::MakeNowPlayingBuilder(Status status,
                                                     uint16_t uid_counter,
                                                     size_t mtu) {
  std::unique_ptr<GetFolderItemsResponseBuilder> builder(
      new GetFolderItemsResponseBuilder(Scope::NOW_PLAYING, status, uid_counter,
                                        mtu));
 
  return builder;
}
 
size_t GetFolderItemsResponseBuilder::size() const {
  size_t len = BrowsePacket::kMinSize();
  len += 1;  // Status
 
  // There is nothing other than the status in the packet if the status isn't
  // NO_ERROR
  if (status_ != Status::NO_ERROR || items_.size() == 0) return len;
 
  len += 2;  // UID Counter
  len += 2;  // Number of Items;
  for (const auto& item : items_) {
    len += item.size();
  }
 
  return len;
}
 
bool GetFolderItemsResponseBuilder::Serialize(
    const std::shared_ptr<::bluetooth::Packet>& pkt) {
  ReserveSpace(pkt, size());
 
  BrowsePacketBuilder::PushHeader(pkt, size() - BrowsePacket::kMinSize());
 
  if (status_ == Status::NO_ERROR && items_.size() == 0) {
    // Return range out of bounds if there are zero items in the folder
    status_ = Status::RANGE_OUT_OF_BOUNDS;
  }
 
  AddPayloadOctets1(pkt, (uint8_t)status_);  // Status
  if (status_ != Status::NO_ERROR) return true;
 
  AddPayloadOctets2(pkt, base::ByteSwap(uid_counter_));
  uint16_t num_items = items_.size();
  AddPayloadOctets2(pkt, base::ByteSwap(num_items));
 
  for (const auto& item : items_) {
    PushMediaListItem(pkt, item);
  }
 
  return true;
}
 
bool GetFolderItemsResponseBuilder::AddMediaPlayer(MediaPlayerItem item) {
  CHECK(scope_ == Scope::MEDIA_PLAYER_LIST);
 
  if (size() + item.size() > mtu_) return false;
 
  items_.push_back(MediaListItem(item));
  return true;
}
 
bool GetFolderItemsResponseBuilder::AddSong(MediaElementItem item) {
  CHECK(scope_ == Scope::VFS || scope_ == Scope::NOW_PLAYING);
 
  if (size() + item.size() > mtu_) return false;
 
  items_.push_back(MediaListItem(item));
  return true;
}
 
bool GetFolderItemsResponseBuilder::AddFolder(FolderItem item) {
  CHECK(scope_ == Scope::VFS);
 
  if (size() + item.size() > mtu_) return false;
 
  items_.push_back(MediaListItem(item));
  return true;
}
 
void GetFolderItemsResponseBuilder::PushMediaListItem(
    const std::shared_ptr<::bluetooth::Packet>& pkt,
    const MediaListItem& item) {
  switch (item.type_) {
    case MediaListItem::PLAYER:
      PushMediaPlayerItem(pkt, item.player_);
      break;
    case MediaListItem::FOLDER:
      PushFolderItem(pkt, item.folder_);
      break;
    case MediaListItem::SONG:
      PushMediaElementItem(pkt, item.song_);
      break;
  }
}
 
void GetFolderItemsResponseBuilder::PushMediaPlayerItem(
    const std::shared_ptr<::bluetooth::Packet>& pkt,
    const MediaPlayerItem& item) {
  AddPayloadOctets1(pkt, 0x01);  // Media Player Item
  uint16_t item_len = item.size() - 3;
  AddPayloadOctets2(pkt, base::ByteSwap(item_len));  // Item length
  AddPayloadOctets2(pkt, base::ByteSwap(item.id_));  // Player ID
  AddPayloadOctets1(pkt, 0x01);                      // Player Type
  AddPayloadOctets4(pkt, 0x00000000);                // Player Subtype
  AddPayloadOctets1(
      pkt, 0x02);  // Player Play Status // TODO: Add this as a passed field
 
  // Features
  AddPayloadOctets1(pkt, 0x00);
  AddPayloadOctets1(pkt, 0x00);
  AddPayloadOctets1(pkt, 0x00);
  AddPayloadOctets1(pkt, 0x00);
  AddPayloadOctets1(pkt, 0x00);
  AddPayloadOctets1(pkt, 0xb7);
  AddPayloadOctets1(pkt, 0x01);
  if (item.browsable_) {
    AddPayloadOctets1(pkt, 0x0C);
    AddPayloadOctets1(pkt, 0x0a);
  } else {
    AddPayloadOctets1(pkt, 0x04);
    AddPayloadOctets1(pkt, 0x00);
  }
  AddPayloadOctets1(pkt, 0x00);
  AddPayloadOctets1(pkt, 0x00);
  AddPayloadOctets1(pkt, 0x00);
  AddPayloadOctets1(pkt, 0x00);
  AddPayloadOctets1(pkt, 0x00);
  AddPayloadOctets1(pkt, 0x00);
  AddPayloadOctets1(pkt, 0x00);
 
  AddPayloadOctets2(pkt, base::ByteSwap((uint16_t)0x006a));
  uint16_t name_len = item.name_.size();
  AddPayloadOctets2(pkt, base::ByteSwap(name_len));
 
  for (const uint8_t& byte : item.name_) {
    AddPayloadOctets1(pkt, byte);
  }
}
 
void GetFolderItemsResponseBuilder::PushFolderItem(
    const std::shared_ptr<::bluetooth::Packet>& pkt, const FolderItem& item) {
  AddPayloadOctets1(pkt, 0x02);  // Folder Item
  uint16_t item_len = item.size() - 3;
  AddPayloadOctets2(pkt, base::ByteSwap(item_len));
  AddPayloadOctets8(pkt, base::ByteSwap(item.uid_));
  AddPayloadOctets1(pkt, item.folder_type_);
  AddPayloadOctets1(pkt, item.is_playable_ ? 0x01 : 0x00);
  AddPayloadOctets2(pkt,
                    base::ByteSwap((uint16_t)0x006a));  // UTF-8 Character Set
  uint16_t name_len = item.name_.size();
  AddPayloadOctets2(pkt, base::ByteSwap(name_len));
  for (const uint8_t& byte : item.name_) {
    AddPayloadOctets1(pkt, byte);
  }
}
 
void GetFolderItemsResponseBuilder::PushMediaElementItem(
    const std::shared_ptr<::bluetooth::Packet>& pkt,
    const MediaElementItem& item) {
  AddPayloadOctets1(pkt, 0x03);  // Media Element Item
  uint16_t item_len = item.size() - 3;
  AddPayloadOctets2(pkt, base::ByteSwap(item_len));
  AddPayloadOctets8(pkt, base::ByteSwap(item.uid_));
  AddPayloadOctets1(pkt, 0x00);  // Media Type Audio
  AddPayloadOctets2(pkt,
                    base::ByteSwap((uint16_t)0x006a));  // UTF-8 Character Set
  uint16_t name_len = item.name_.size();
  AddPayloadOctets2(pkt, base::ByteSwap(name_len));
  for (const uint8_t& byte : item.name_) {
    AddPayloadOctets1(pkt, byte);
  }
 
  AddPayloadOctets1(pkt, (uint8_t)item.attributes_.size());
  for (const auto& entry : item.attributes_) {
    AddPayloadOctets4(pkt, base::ByteSwap((uint32_t)entry.attribute()));
    AddPayloadOctets2(pkt,
                      base::ByteSwap((uint16_t)0x006a));  // UTF-8 Character Set
 
    std::string attr_val = entry.value();
    uint16_t attr_len = attr_val.size();
 
    AddPayloadOctets2(pkt, base::ByteSwap(attr_len));
    for (const uint8_t& byte : attr_val) {
      AddPayloadOctets1(pkt, byte);
    }
  }
}
 
Scope GetFolderItemsRequest::GetScope() const {
  auto it = begin() + BrowsePacket::kMinSize();
  return static_cast<Scope>(*it);
}
 
uint32_t GetFolderItemsRequest::GetStartItem() const {
  auto it = begin() + BrowsePacket::kMinSize() + static_cast<size_t>(1);
  return it.extractBE<uint32_t>();
}
 
uint32_t GetFolderItemsRequest::GetEndItem() const {
  auto it = begin() + BrowsePacket::kMinSize() + static_cast<size_t>(5);
  return it.extractBE<uint32_t>();
}
 
uint8_t GetFolderItemsRequest::GetNumAttributes() const {
  auto it = begin() + BrowsePacket::kMinSize() + static_cast<size_t>(9);
  return *it;
}
 
std::vector<Attribute> GetFolderItemsRequest::GetAttributesRequested() const {
  auto it = begin() + BrowsePacket::kMinSize() + static_cast<size_t>(9);
 
  size_t number_of_attributes = it.extract<uint8_t>();
  std::vector<Attribute> attribute_list;
 
  // No attributes requested
  if (number_of_attributes == 0xFF) return attribute_list;
 
  // TODO: If the number of attributes equals 0, then all attributes are
  // requested right now thats handled in the service itself, but it'd be nice
  // to have this function return a vector with all the attributes
 
  for (size_t i = 0; i < number_of_attributes; i++) {
    attribute_list.push_back((Attribute)it.extractBE<uint32_t>());
  }
 
  return attribute_list;
}
 
bool GetFolderItemsRequest::IsValid() const {
  if (!BrowsePacket::IsValid()) return false;
  // The minimum size required to be valid
  if (size() < kMinSize()) return false;
 
  auto attr_count = GetNumAttributes();
 
  // No items requested
  if (attr_count == 0xFF) return true;
 
  auto attr_start = begin() + kMinSize();
 
  // Casting the int returned from end - attr_start should be fine. If an
  // overflow occurs we can definitly say the packet is invalid
  return (attr_count * sizeof(Attribute)) == (size_t)(end() - attr_start);
}
 
std::string GetFolderItemsRequest::ToString() const {
  std::stringstream ss;
  ss << "GetFolderItemsRequestPacket: " << std::endl;
  ss << "  └ PDU = " << GetPdu() << std::endl;
  ss << "  └ Length = " << GetLength() << std::endl;
  ss << "  └ Scope = " << GetScope() << std::endl;
  ss << "  └ Start Item = " << loghex(GetStartItem()) << std::endl;
  ss << "  └ End Item = " << loghex(GetEndItem()) << std::endl;
  ss << "  └ Attribute Count = " << loghex(GetNumAttributes()) << std::endl;
 
  ss << std::endl;
 
  return ss.str();
}
 
std::unique_ptr<GetFolderItemsRequestBuilder>
GetFolderItemsRequestBuilder::MakeBuilder(
    Scope scope, uint32_t start_item, uint32_t end_item,
    const std::set<Attribute>& requested_attrs) {
  std::unique_ptr<GetFolderItemsRequestBuilder> builder(
      new GetFolderItemsRequestBuilder(scope, start_item, end_item,
                                       requested_attrs));
 
  return builder;
}
 
size_t GetFolderItemsRequestBuilder::size() const {
  size_t len = GetFolderItemsRequest::kMinSize();
  len += requested_attrs_.size() * sizeof(Attribute);
  return len;
}
 
bool GetFolderItemsRequestBuilder::Serialize(
    const std::shared_ptr<::bluetooth::Packet>& pkt) {
  ReserveSpace(pkt, size());
 
  BrowsePacketBuilder::PushHeader(pkt, size() - BrowsePacket::kMinSize());
 
  AddPayloadOctets1(pkt, static_cast<uint8_t>(scope_));
  AddPayloadOctets4(pkt, base::ByteSwap(start_item_));
  AddPayloadOctets4(pkt, base::ByteSwap(end_item_));
 
  if (requested_attrs_.size() == 0) {
    // 0xFF is the value to signify that there are no attributes requested.
    AddPayloadOctets1(pkt, 0xFF);
    return true;
  }
 
  AddPayloadOctets1(pkt, requested_attrs_.size());
  for (const auto& attr : requested_attrs_) {
    AddPayloadOctets4(pkt, base::ByteSwap(static_cast<uint32_t>(attr)));
  }
  return true;
}
 
}  // namespace avrcp
}  // namespace bluetooth