1 /*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "connection_handler.h"
18
19 #include <base/bind.h>
20 #include <base/logging.h>
21
22 #include <map>
23
24 #include "avrc_defs.h"
25 #include "avrcp_message_converter.h"
26 #include "packet/avrcp/avrcp_packet.h"
27 // TODO (apanicke): Remove dependency on this header once we cleanup feature
28 // handling.
29 #include "bta/include/bta_av_api.h"
30 #include "device/include/interop.h"
31 #include "osi/include/allocator.h"
32 #include "osi/include/properties.h"
33 #include "stack/include/bt_hdr.h"
34 #include "types/raw_address.h"
35
36 namespace bluetooth {
37 namespace avrcp {
38
39 ConnectionHandler* ConnectionHandler::instance_ = nullptr;
40
Get()41 ConnectionHandler* ConnectionHandler::Get() {
42 CHECK(instance_);
43
44 return instance_;
45 }
46
IsAbsoluteVolumeEnabled(const RawAddress * bdaddr)47 bool IsAbsoluteVolumeEnabled(const RawAddress* bdaddr) {
48 char volume_disabled[PROPERTY_VALUE_MAX] = {0};
49 osi_property_get("persist.bluetooth.disableabsvol", volume_disabled, "false");
50 if (strncmp(volume_disabled, "true", 4) == 0) {
51 LOG(INFO) << "Absolute volume disabled by property";
52 return false;
53 }
54 if (interop_match_addr(INTEROP_DISABLE_ABSOLUTE_VOLUME, bdaddr)) {
55 LOG(INFO) << "Absolute volume disabled by IOP table";
56 return false;
57 }
58 return true;
59 }
60
Initialize(const ConnectionCallback & callback,AvrcpInterface * avrcp,SdpInterface * sdp,VolumeInterface * vol)61 bool ConnectionHandler::Initialize(const ConnectionCallback& callback,
62 AvrcpInterface* avrcp, SdpInterface* sdp,
63 VolumeInterface* vol) {
64 CHECK(instance_ == nullptr);
65 CHECK(avrcp != nullptr);
66 CHECK(sdp != nullptr);
67
68 // TODO (apanicke): When transitioning to using this service, implement
69 // SDP Initialization for AVRCP Here.
70 instance_ = new ConnectionHandler();
71 instance_->connection_cb_ = callback;
72 instance_->avrc_ = avrcp;
73 instance_->sdp_ = sdp;
74 instance_->vol_ = vol;
75
76 // Set up the AVRCP acceptor connection
77 if (!instance_->AvrcpConnect(false, RawAddress::kAny)) {
78 instance_->CleanUp();
79 return false;
80 }
81
82 return true;
83 }
84
CleanUp()85 bool ConnectionHandler::CleanUp() {
86 CHECK(instance_ != nullptr);
87
88 // TODO (apanicke): Cleanup the SDP Entries here
89 for (auto entry = instance_->device_map_.begin();
90 entry != instance_->device_map_.end();) {
91 auto curr = entry;
92 entry++;
93 curr->second->DeviceDisconnected();
94 instance_->avrc_->Close(curr->first);
95 }
96 instance_->device_map_.clear();
97 instance_->feature_map_.clear();
98
99 instance_->weak_ptr_factory_.InvalidateWeakPtrs();
100
101 delete instance_;
102 instance_ = nullptr;
103
104 return true;
105 }
106
InitForTesting(ConnectionHandler * handler)107 void ConnectionHandler::InitForTesting(ConnectionHandler* handler) {
108 CHECK(instance_ == nullptr);
109 instance_ = handler;
110 }
111
ConnectDevice(const RawAddress & bdaddr)112 bool ConnectionHandler::ConnectDevice(const RawAddress& bdaddr) {
113 LOG(INFO) << "Attempting to connect to device " << bdaddr;
114
115 for (const auto& pair : device_map_) {
116 if (bdaddr == pair.second->GetAddress()) {
117 LOG(WARNING) << "Already connected to device with address " << bdaddr;
118 return false;
119 }
120 }
121
122 auto connection_lambda = [](ConnectionHandler* instance_,
123 const RawAddress& bdaddr, uint16_t status,
124 uint16_t version, uint16_t features) {
125 LOG(INFO) << __PRETTY_FUNCTION__
126 << " SDP Completed features=" << loghex(features);
127 if (status != AVRC_SUCCESS || !(features & BTA_AV_FEAT_RCCT)) {
128 LOG(ERROR) << "Failed to do SDP: status=" << loghex(status)
129 << " features=" << loghex(features)
130 << " supports controller: " << (features & BTA_AV_FEAT_RCCT);
131 instance_->connection_cb_.Run(std::shared_ptr<Device>());
132 }
133
134 instance_->feature_map_.emplace(bdaddr, features);
135 instance_->AvrcpConnect(true, bdaddr);
136 return;
137 };
138
139 return SdpLookup(bdaddr, base::Bind(connection_lambda, this, bdaddr), false);
140 }
141
DisconnectDevice(const RawAddress & bdaddr)142 bool ConnectionHandler::DisconnectDevice(const RawAddress& bdaddr) {
143 for (auto it = device_map_.begin(); it != device_map_.end(); it++) {
144 if (bdaddr == it->second->GetAddress()) {
145 uint8_t handle = it->first;
146 return avrc_->Close(handle) == AVRC_SUCCESS;
147 }
148 }
149
150 return false;
151 }
152
SetBipClientStatus(const RawAddress & bdaddr,bool connected)153 void ConnectionHandler::SetBipClientStatus(const RawAddress& bdaddr,
154 bool connected) {
155 for (auto it = device_map_.begin(); it != device_map_.end(); it++) {
156 if (bdaddr == it->second->GetAddress()) {
157 it->second->SetBipClientStatus(connected);
158 return;
159 }
160 }
161 }
162
GetListOfDevices() const163 std::vector<std::shared_ptr<Device>> ConnectionHandler::GetListOfDevices()
164 const {
165 std::vector<std::shared_ptr<Device>> list;
166 for (const auto& device : device_map_) {
167 list.push_back(device.second);
168 }
169 return list;
170 }
171
SdpLookup(const RawAddress & bdaddr,SdpCallback cb,bool retry)172 bool ConnectionHandler::SdpLookup(const RawAddress& bdaddr, SdpCallback cb,
173 bool retry) {
174 LOG(INFO) << __PRETTY_FUNCTION__;
175
176 tAVRC_SDP_DB_PARAMS db_params;
177 // TODO (apanicke): This needs to be replaced with smarter memory management.
178 tSDP_DISCOVERY_DB* disc_db =
179 (tSDP_DISCOVERY_DB*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
180 uint16_t attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
181 ATTR_ID_BT_PROFILE_DESC_LIST,
182 ATTR_ID_SUPPORTED_FEATURES};
183
184 db_params.db_len =
185 BT_DEFAULT_BUFFER_SIZE; // Some magic number found in the AVRCP code
186 db_params.num_attr = sizeof(attr_list) / sizeof(attr_list[0]);
187 db_params.p_db = disc_db;
188 db_params.p_attrs = attr_list;
189
190 return avrc_->FindService(UUID_SERVCLASS_AV_REMOTE_CONTROL, bdaddr,
191 &db_params,
192 base::Bind(&ConnectionHandler::SdpCb,
193 weak_ptr_factory_.GetWeakPtr(), bdaddr,
194 cb, disc_db, retry)) == AVRC_SUCCESS;
195 }
196
AvrcpConnect(bool initiator,const RawAddress & bdaddr)197 bool ConnectionHandler::AvrcpConnect(bool initiator, const RawAddress& bdaddr) {
198 LOG(INFO) << "Connect to device " << bdaddr.ToString();
199
200 tAVRC_CONN_CB open_cb;
201 if (initiator) {
202 open_cb.ctrl_cback = base::Bind(&ConnectionHandler::InitiatorControlCb,
203 weak_ptr_factory_.GetWeakPtr());
204 } else {
205 open_cb.ctrl_cback = base::Bind(&ConnectionHandler::AcceptorControlCb,
206 weak_ptr_factory_.GetWeakPtr());
207 }
208 open_cb.msg_cback =
209 base::Bind(&ConnectionHandler::MessageCb, weak_ptr_factory_.GetWeakPtr());
210 open_cb.company_id = AVRC_CO_GOOGLE;
211 open_cb.conn = initiator ? AVRC_CONN_INT
212 : AVRC_CONN_ACP; // 0 if initiator, 1 if acceptor
213 // TODO (apanicke): We shouldn't need RCCT to do absolute volume. The current
214 // AVRC_API requires it though.
215 open_cb.control = BTA_AV_FEAT_RCTG | BTA_AV_FEAT_RCCT | BTA_AV_FEAT_METADATA
216 | AVRC_CT_PASSIVE;
217
218 uint8_t handle = 0;
219 uint16_t status = avrc_->Open(&handle, &open_cb, bdaddr);
220 LOG(INFO) << __PRETTY_FUNCTION__ << ": handle=" << loghex(handle)
221 << " status= " << loghex(status);
222 return status == AVRC_SUCCESS;
223 }
224
InitiatorControlCb(uint8_t handle,uint8_t event,uint16_t result,const RawAddress * peer_addr)225 void ConnectionHandler::InitiatorControlCb(uint8_t handle, uint8_t event,
226 uint16_t result,
227 const RawAddress* peer_addr) {
228 DCHECK(!connection_cb_.is_null());
229
230 LOG(INFO) << __PRETTY_FUNCTION__ << ": handle=" << loghex(handle)
231 << " result=" << loghex(result)
232 << " addr=" << (peer_addr ? peer_addr->ToString() : "none");
233
234 switch (event) {
235 case AVRC_OPEN_IND_EVT: {
236 LOG(INFO) << __PRETTY_FUNCTION__ << ": Connection Opened Event";
237
238 const auto& feature_iter = feature_map_.find(*peer_addr);
239 if (feature_iter == feature_map_.end()) {
240 LOG(ERROR) << "Features do not exist even though SDP should have been "
241 "done first";
242 return;
243 }
244
245 bool supports_browsing = feature_iter->second & BTA_AV_FEAT_BROWSE;
246
247 if (supports_browsing) {
248 avrc_->OpenBrowse(handle, AVCT_INT);
249 }
250
251 // TODO (apanicke): Implement a system to cache SDP entries. For most
252 // devices SDP is completed after the device connects AVRCP so that
253 // information isn't very useful when trying to control our
254 // capabilities. For now always use AVRCP 1.6.
255 auto&& callback = base::Bind(&ConnectionHandler::SendMessage,
256 base::Unretained(this), handle);
257 auto&& ctrl_mtu = avrc_->GetPeerMtu(handle) - AVCT_HDR_LEN;
258 auto&& browse_mtu = avrc_->GetBrowseMtu(handle) - AVCT_HDR_LEN;
259 std::shared_ptr<Device> newDevice = std::make_shared<Device>(
260 *peer_addr, !supports_browsing, callback, ctrl_mtu, browse_mtu);
261
262 device_map_[handle] = newDevice;
263 // TODO (apanicke): Create the device with all of the interfaces it
264 // needs. Return the new device where the service will register the
265 // interfaces it needs.
266 connection_cb_.Run(newDevice);
267
268 if (feature_iter->second & BTA_AV_FEAT_ADV_CTRL) {
269 newDevice->RegisterVolumeChanged();
270 } else if (instance_->vol_ != nullptr) {
271 instance_->vol_->DeviceConnected(newDevice->GetAddress());
272 }
273
274 } break;
275
276 case AVRC_CLOSE_IND_EVT: {
277 LOG(INFO) << __PRETTY_FUNCTION__ << ": Connection Closed Event";
278
279 if (device_map_.find(handle) == device_map_.end()) {
280 LOG(WARNING)
281 << "Connection Close received from device that doesn't exist";
282 return;
283 }
284 avrc_->Close(handle);
285 feature_map_.erase(device_map_[handle]->GetAddress());
286 device_map_[handle]->DeviceDisconnected();
287 device_map_.erase(handle);
288 } break;
289
290 case AVRC_BROWSE_OPEN_IND_EVT: {
291 LOG(INFO) << __PRETTY_FUNCTION__ << ": Browse Open Event";
292 // NOTE (apanicke): We don't need to explicitly handle this message
293 // since the AVCTP Layer will still send us browsing messages
294 // regardless. It would be useful to note this though for future
295 // compatibility issues.
296 if (device_map_.find(handle) == device_map_.end()) {
297 LOG(WARNING) << "Browse Opened received from device that doesn't exist";
298 return;
299 }
300
301 auto browse_mtu = avrc_->GetBrowseMtu(handle) - AVCT_HDR_LEN;
302 device_map_[handle]->SetBrowseMtu(browse_mtu);
303 } break;
304 case AVRC_BROWSE_CLOSE_IND_EVT:
305 LOG(INFO) << __PRETTY_FUNCTION__ << ": Browse Close Event";
306 break;
307 default:
308 LOG(ERROR) << "Unknown AVRCP Control event";
309 break;
310 }
311 }
312
AcceptorControlCb(uint8_t handle,uint8_t event,uint16_t result,const RawAddress * peer_addr)313 void ConnectionHandler::AcceptorControlCb(uint8_t handle, uint8_t event,
314 uint16_t result,
315 const RawAddress* peer_addr) {
316 DCHECK(!connection_cb_.is_null());
317
318 LOG(INFO) << __PRETTY_FUNCTION__ << ": handle=" << loghex(handle)
319 << " result=" << loghex(result)
320 << " addr=" << (peer_addr ? peer_addr->ToString() : "none");
321
322 switch (event) {
323 case AVRC_OPEN_IND_EVT: {
324 LOG(INFO) << __PRETTY_FUNCTION__ << ": Connection Opened Event";
325
326 auto&& callback = base::Bind(&ConnectionHandler::SendMessage,
327 weak_ptr_factory_.GetWeakPtr(), handle);
328 auto&& ctrl_mtu = avrc_->GetPeerMtu(handle) - AVCT_HDR_LEN;
329 auto&& browse_mtu = avrc_->GetBrowseMtu(handle) - AVCT_HDR_LEN;
330 std::shared_ptr<Device> newDevice = std::make_shared<Device>(
331 *peer_addr, false, callback, ctrl_mtu, browse_mtu);
332
333 device_map_[handle] = newDevice;
334 connection_cb_.Run(newDevice);
335
336 LOG(INFO) << __PRETTY_FUNCTION__
337 << ": Performing SDP on connected device. address="
338 << peer_addr->ToString();
339 auto sdp_lambda = [](ConnectionHandler* instance_, uint8_t handle,
340 uint16_t status, uint16_t version,
341 uint16_t features) {
342 if (instance_->device_map_.find(handle) ==
343 instance_->device_map_.end()) {
344 LOG(WARNING) << __PRETTY_FUNCTION__
345 << ": No device found for handle: " << loghex(handle);
346 return;
347 }
348
349 auto device = instance_->device_map_[handle];
350 instance_->feature_map_.emplace(device->GetAddress(), features);
351
352 // TODO (apanicke): Report to the VolumeInterface that a new Device is
353 // connected that doesn't support absolute volume.
354 if (features & BTA_AV_FEAT_ADV_CTRL) {
355 device->RegisterVolumeChanged();
356 } else if (instance_->vol_ != nullptr) {
357 instance_->vol_->DeviceConnected(device->GetAddress());
358 }
359 };
360
361 SdpLookup(*peer_addr, base::Bind(sdp_lambda, this, handle), false);
362
363 avrc_->OpenBrowse(handle, AVCT_ACP);
364 AvrcpConnect(false, RawAddress::kAny);
365 } break;
366
367 case AVRC_CLOSE_IND_EVT: {
368 LOG(INFO) << __PRETTY_FUNCTION__ << ": Connection Closed Event";
369
370 if (device_map_.find(handle) == device_map_.end()) {
371 LOG(WARNING)
372 << "Connection Close received from device that doesn't exist";
373 return;
374 }
375 avrc_->Close(handle);
376 feature_map_.erase(device_map_[handle]->GetAddress());
377 device_map_[handle]->DeviceDisconnected();
378 device_map_.erase(handle);
379 } break;
380
381 case AVRC_BROWSE_OPEN_IND_EVT: {
382 LOG(INFO) << __PRETTY_FUNCTION__ << ": Browse Open Event";
383 // NOTE (apanicke): We don't need to explicitly handle this message
384 // since the AVCTP Layer will still send us browsing messages
385 // regardless. It would be useful to note this though for future
386 // compatibility issues.
387 if (device_map_.find(handle) == device_map_.end()) {
388 LOG(WARNING) << "Browse Opened received from device that doesn't exist";
389 return;
390 }
391
392 auto browse_mtu = avrc_->GetBrowseMtu(handle) - AVCT_HDR_LEN;
393 device_map_[handle]->SetBrowseMtu(browse_mtu);
394 } break;
395 case AVRC_BROWSE_CLOSE_IND_EVT:
396 LOG(INFO) << __PRETTY_FUNCTION__ << ": Browse Close Event";
397 break;
398 default:
399 LOG(ERROR) << "Unknown AVRCP Control event";
400 break;
401 }
402 }
403
MessageCb(uint8_t handle,uint8_t label,uint8_t opcode,tAVRC_MSG * p_msg)404 void ConnectionHandler::MessageCb(uint8_t handle, uint8_t label, uint8_t opcode,
405 tAVRC_MSG* p_msg) {
406 if (device_map_.find(handle) == device_map_.end()) {
407 LOG(ERROR) << "Message received for unconnected device: handle="
408 << loghex(handle);
409 return;
410 }
411
412 auto pkt = AvrcpMessageConverter::Parse(p_msg);
413
414 if (opcode == AVRC_OP_BROWSE) {
415 VLOG(4) << "Browse Message received on handle " << (unsigned int)handle;
416 device_map_[handle]->BrowseMessageReceived(label, BrowsePacket::Parse(pkt));
417 return;
418 }
419
420 VLOG(4) << "Message received on handle " << (unsigned int)handle;
421 device_map_[handle]->MessageReceived(label, Packet::Parse(pkt));
422 }
423
SdpCb(RawAddress bdaddr,SdpCallback cb,tSDP_DISCOVERY_DB * disc_db,bool retry,uint16_t status)424 void ConnectionHandler::SdpCb(RawAddress bdaddr, SdpCallback cb,
425 tSDP_DISCOVERY_DB* disc_db, bool retry,
426 uint16_t status) {
427 VLOG(1) << __PRETTY_FUNCTION__ << ": SDP lookup callback received";
428
429 if (status == SDP_CONN_FAILED and !retry) {
430 LOG(WARNING) << __PRETTY_FUNCTION__ << ": SDP Failure retry again";
431 SdpLookup(bdaddr, cb, true);
432 return;
433 } else if (status != AVRC_SUCCESS) {
434 LOG(ERROR) << __PRETTY_FUNCTION__
435 << ": SDP Failure: status = " << (unsigned int)status;
436 cb.Run(status, 0, 0);
437 osi_free(disc_db);
438 return;
439 }
440
441 // Check the peer features
442 tSDP_DISC_REC* sdp_record = nullptr;
443 uint16_t peer_features = 0;
444 uint16_t peer_avrcp_version = 0;
445
446 // TODO (apanicke): Replace this in favor of our own supported features.
447 sdp_record =
448 sdp_->FindServiceInDb(disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL, nullptr);
449 if (sdp_record != nullptr) {
450 LOG(INFO) << __PRETTY_FUNCTION__ << ": Device " << bdaddr.ToString()
451 << " supports remote control";
452 peer_features |= BTA_AV_FEAT_RCCT;
453
454 if ((sdp_->FindAttributeInRec(sdp_record, ATTR_ID_BT_PROFILE_DESC_LIST)) !=
455 NULL) {
456 /* get profile version (if failure, version parameter is not updated) */
457 sdp_->FindProfileVersionInRec(
458 sdp_record, UUID_SERVCLASS_AV_REMOTE_CONTROL, &peer_avrcp_version);
459 VLOG(1) << __PRETTY_FUNCTION__ << ": Device " << bdaddr.ToString()
460 << " peer avrcp version=" << loghex(peer_avrcp_version);
461
462 if (peer_avrcp_version >= AVRC_REV_1_3) {
463 // These are the standard features, another way to check this is to
464 // search for CAT1 on the remote device
465 VLOG(1) << __PRETTY_FUNCTION__ << ": Device " << bdaddr.ToString()
466 << " supports metadata";
467 peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
468 }
469 if (peer_avrcp_version >= AVRC_REV_1_4) {
470 /* get supported categories */
471 VLOG(1) << __PRETTY_FUNCTION__ << " Get Supported categories";
472 tSDP_DISC_ATTR* sdp_attribute =
473 sdp_->FindAttributeInRec(sdp_record, ATTR_ID_SUPPORTED_FEATURES);
474 if (sdp_attribute != NULL) {
475 VLOG(1) << __PRETTY_FUNCTION__
476 << "Get Supported categories SDP ATTRIBUTES != null";
477 uint16_t categories = sdp_attribute->attr_value.v.u16;
478 if (categories & AVRC_SUPF_CT_CAT2) {
479 VLOG(1) << __PRETTY_FUNCTION__ << ": Device " << bdaddr.ToString()
480 << " supports advanced control";
481 if (IsAbsoluteVolumeEnabled(&bdaddr)) {
482 peer_features |= (BTA_AV_FEAT_ADV_CTRL);
483 }
484 }
485 if (categories & AVRC_SUPF_CT_BROWSE) {
486 VLOG(1) << __PRETTY_FUNCTION__ << ": Device " << bdaddr.ToString()
487 << " supports browsing";
488 peer_features |= (BTA_AV_FEAT_BROWSE);
489 }
490 }
491 }
492
493 if (osi_property_get_bool(AVRC_DYNAMIC_AVRCP_ENABLE_PROPERTY, true)) {
494 avrc_->SaveControllerVersion(bdaddr, peer_avrcp_version);
495 }
496 }
497 }
498
499 sdp_record = sdp_->FindServiceInDb(disc_db, UUID_SERVCLASS_AV_REM_CTRL_TARGET,
500 nullptr);
501 if (sdp_record != nullptr) {
502 VLOG(1) << __PRETTY_FUNCTION__ << ": Device " << bdaddr.ToString()
503 << " supports remote control target";
504
505 uint16_t peer_avrcp_target_version = 0;
506 sdp_->FindProfileVersionInRec(sdp_record, UUID_SERVCLASS_AV_REMOTE_CONTROL,
507 &peer_avrcp_target_version);
508 VLOG(1) << __PRETTY_FUNCTION__ << ": Device " << bdaddr.ToString()
509 << " peer avrcp target version="
510 << loghex(peer_avrcp_target_version);
511
512 if ((sdp_->FindAttributeInRec(sdp_record, ATTR_ID_BT_PROFILE_DESC_LIST)) !=
513 NULL) {
514 if (peer_avrcp_target_version >= AVRC_REV_1_4) {
515 /* get supported categories */
516 VLOG(1) << __PRETTY_FUNCTION__ << " Get Supported categories";
517 tSDP_DISC_ATTR* sdp_attribute =
518 sdp_->FindAttributeInRec(sdp_record, ATTR_ID_SUPPORTED_FEATURES);
519 if (sdp_attribute != NULL) {
520 VLOG(1) << __PRETTY_FUNCTION__
521 << "Get Supported categories SDP ATTRIBUTES != null";
522 uint16_t categories = sdp_attribute->attr_value.v.u16;
523 if (categories & AVRC_SUPF_CT_CAT2) {
524 VLOG(1) << __PRETTY_FUNCTION__ << ": Device " << bdaddr.ToString()
525 << " supports advanced control";
526 if (IsAbsoluteVolumeEnabled(&bdaddr)) {
527 peer_features |= (BTA_AV_FEAT_ADV_CTRL);
528 }
529 }
530 }
531 }
532 }
533 }
534
535 osi_free(disc_db);
536
537 cb.Run(status, peer_avrcp_version, peer_features);
538 }
539
SendMessage(uint8_t handle,uint8_t label,bool browse,std::unique_ptr<::bluetooth::PacketBuilder> message)540 void ConnectionHandler::SendMessage(
541 uint8_t handle, uint8_t label, bool browse,
542 std::unique_ptr<::bluetooth::PacketBuilder> message) {
543 std::shared_ptr<::bluetooth::Packet> packet = VectorPacket::Make();
544 message->Serialize(packet);
545
546 uint8_t ctype = AVRC_RSP_ACCEPT;
547 if (!browse) {
548 ctype =
549 (uint8_t)(::bluetooth::Packet::Specialize<Packet>(packet)->GetCType());
550 }
551
552 DLOG(INFO) << "SendMessage to handle=" << loghex(handle);
553
554 BT_HDR* pkt = (BT_HDR*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
555
556 pkt->offset = AVCT_MSG_OFFSET;
557 // TODO (apanicke): Update this constant. Currently this is a unique event
558 // used to tell the AVRCP API layer that the data is properly formatted and
559 // doesn't need to be processed. In the future, this is the only place sending
560 // the packet so none of these layer specific fields will be used.
561 pkt->event = 0xFFFF;
562 /* Handle for AVRCP fragment */
563 uint16_t op_code = (uint16_t)(::bluetooth::Packet::Specialize<Packet>(packet)->GetOpcode());
564 if (!browse && (op_code == (uint16_t)(Opcode::VENDOR))) {
565 pkt->event = op_code;
566 }
567
568 // TODO (apanicke): This layer specific stuff can go away once we move over
569 // to the new service.
570 pkt->layer_specific = AVCT_DATA_CTRL;
571 if (browse) {
572 pkt->layer_specific = AVCT_DATA_BROWSE;
573 }
574
575 pkt->len = packet->size();
576 uint8_t* p_data = (uint8_t*)(pkt + 1) + pkt->offset;
577 for (auto it = packet->begin(); it != packet->end(); it++) {
578 *p_data++ = *it;
579 }
580
581 avrc_->MsgReq(handle, label, ctype, pkt);
582 }
583
584 } // namespace avrcp
585 } // namespace bluetooth
586