OSDN Git Service

Add respond Reject with Invalid parameter for Unknown Capability ID
authortedwang <tedwang@google.com>
Wed, 9 May 2018 06:28:04 +0000 (14:28 +0800)
committerHansong Zhang <hsz@google.com>
Tue, 15 May 2018 20:23:29 +0000 (13:23 -0700)
Add handle Get Capabilities with Unknown Capability ID and unit test
for this.

Bug: 79269978
Test: Run host native test net_test_avrcp
Change-Id: I2cb606bcb6462190354bb471577d9383e43649c1
(cherry picked from commit 5e872794641b89b63176befdb04865d5acf14aa3)

packet/tests/avrcp/avrcp_test_packets.h
profile/avrcp/device.cc
profile/avrcp/device.h
profile/avrcp/tests/avrcp_device_test.cc

index 3160ad5..41177ac 100644 (file)
@@ -25,6 +25,14 @@ namespace {
 std::vector<uint8_t> get_capabilities_request = {
     0x01, 0x48, 0x00, 0x00, 0x19, 0x58, 0x10, 0x00, 0x00, 0x01, 0x03};
 
+// AVRCP Get Capabilities Request packet with Company ID
+std::vector<uint8_t> get_capabilities_request_company_id = {
+    0x01, 0x48, 0x00, 0x00, 0x19, 0x58, 0x10, 0x00, 0x00, 0x01, 0x02};
+
+// AVRCP Get Capabilities Request packet with Unknown
+std::vector<uint8_t> get_capabilities_request_unknown = {
+    0x01, 0x48, 0x00, 0x00, 0x19, 0x58, 0x10, 0x00, 0x00, 0x01, 0x7f};
+
 // AVRCP Get Capabilities Response to Company ID request
 std::vector<uint8_t> get_capabilities_response_company_id = {
     0x0c, 0x48, 0x00, 0x00, 0x19, 0x58, 0x10, 0x00, 0x00,
index 5fb1dcd..9cf7f21 100644 (file)
@@ -91,30 +91,8 @@ void Device::VendorPacketHandler(uint8_t label,
 
   switch (pkt->GetCommandPdu()) {
     case CommandPdu::GET_CAPABILITIES: {
-      auto capability_request_pkt =
-          Packet::Specialize<GetCapabilitiesRequest>(pkt);
-      if (capability_request_pkt->GetCapabilityRequested() ==
-          Capability::COMPANY_ID) {
-        auto response =
-            GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x001958);
-        response->AddCompanyId(0x002345);
-        send_message_cb_.Run(label, false, std::move(response));
-      } else {
-        auto response =
-            GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
-                Event::PLAYBACK_STATUS_CHANGED);
-        response->AddEvent(Event::TRACK_CHANGED);
-        response->AddEvent(Event::PLAYBACK_POS_CHANGED);
-
-        if (!avrcp13_compatibility_) {
-          response->AddEvent(Event::AVAILABLE_PLAYERS_CHANGED);
-          response->AddEvent(Event::ADDRESSED_PLAYER_CHANGED);
-          response->AddEvent(Event::UIDS_CHANGED);
-          response->AddEvent(Event::NOW_PLAYING_CONTENT_CHANGED);
-        }
-
-        send_message(label, false, std::move(response));
-      }
+      HandleGetCapabilities(label,
+                            Packet::Specialize<GetCapabilitiesRequest>(pkt));
     } break;
 
     case CommandPdu::REGISTER_NOTIFICATION: {
@@ -157,6 +135,46 @@ void Device::VendorPacketHandler(uint8_t label,
   }
 }
 
+void Device::HandleGetCapabilities(
+    uint8_t label, const std::shared_ptr<GetCapabilitiesRequest>& pkt) {
+  DEVICE_VLOG(4) << __func__
+                 << ": capability=" << pkt->GetCapabilityRequested();
+
+  switch (pkt->GetCapabilityRequested()) {
+    case Capability::COMPANY_ID: {
+      auto response =
+          GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x001958);
+      response->AddCompanyId(0x002345);
+      send_message_cb_.Run(label, false, std::move(response));
+    } break;
+
+    case Capability::EVENTS_SUPPORTED: {
+      auto response =
+          GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
+              Event::PLAYBACK_STATUS_CHANGED);
+      response->AddEvent(Event::TRACK_CHANGED);
+      response->AddEvent(Event::PLAYBACK_POS_CHANGED);
+
+      if (!avrcp13_compatibility_) {
+        response->AddEvent(Event::AVAILABLE_PLAYERS_CHANGED);
+        response->AddEvent(Event::ADDRESSED_PLAYER_CHANGED);
+        response->AddEvent(Event::UIDS_CHANGED);
+        response->AddEvent(Event::NOW_PLAYING_CONTENT_CHANGED);
+      }
+
+      send_message(label, false, std::move(response));
+    } break;
+
+    default: {
+      DEVICE_LOG(WARNING) << "Unhandled Capability: "
+                          << pkt->GetCapabilityRequested();
+      auto response = RejectBuilder::MakeBuilder(CommandPdu::GET_CAPABILITIES,
+                                                 Status::INVALID_PARAMETER);
+      send_message(label, false, std::move(response));
+    } break;
+  }
+}
+
 void Device::HandleNotification(
     uint8_t label, const std::shared_ptr<RegisterNotificationRequest>& pkt) {
   DEVICE_VLOG(4) << __func__ << ": event=" << pkt->GetEventRegistered();
index fefee88..7fcb3a5 100644 (file)
@@ -114,6 +114,10 @@ class Device {
       uint8_t label, bool interim, std::string curr_song_id,
       std::vector<SongInfo> song_list);
 
+  // GET CAPABILITY
+  virtual void HandleGetCapabilities(
+      uint8_t label, const std::shared_ptr<GetCapabilitiesRequest>& pkt);
+
   // REGISTER NOTIFICATION
   virtual void HandleNotification(
       uint8_t label, const std::shared_ptr<RegisterNotificationRequest>& pkt);
index 6e3bb17..c0c31ac 100644 (file)
@@ -938,5 +938,53 @@ TEST_F(AvrcpDeviceTest, mediaKeyInactiveDeviceTest) {
   SendMessage(1, play_released_pkt);
 }
 
+TEST_F(AvrcpDeviceTest, getCapabilitiesTest) {
+  MockMediaInterface interface;
+  NiceMock<MockA2dpInterface> a2dp_interface;
+
+  test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr);
+
+  // GetCapabilities with CapabilityID COMPANY_ID
+  auto request_company_id_response =
+      GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x001958);
+  request_company_id_response->AddCompanyId(0x002345);
+  EXPECT_CALL(
+      response_cb,
+      Call(1, false, matchPacket(std::move(request_company_id_response))))
+      .Times(1);
+
+  auto request_company_id =
+      TestAvrcpPacket::Make(get_capabilities_request_company_id);
+  SendMessage(1, request_company_id);
+
+  // GetCapabilities with CapabilityID EVENTS_SUPPORTED
+  auto request_events_supported_response =
+      GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
+          Event::PLAYBACK_STATUS_CHANGED);
+  request_events_supported_response->AddEvent(Event::TRACK_CHANGED);
+  request_events_supported_response->AddEvent(Event::PLAYBACK_POS_CHANGED);
+
+  EXPECT_CALL(
+      response_cb,
+      Call(2, false, matchPacket(std::move(request_events_supported_response))))
+      .Times(1);
+
+  auto request_events_supported =
+      TestAvrcpPacket::Make(get_capabilities_request);
+  SendMessage(2, request_events_supported);
+
+  // GetCapabilities with CapabilityID UNKNOWN
+  auto request_unknown_response = RejectBuilder::MakeBuilder(
+      CommandPdu::GET_CAPABILITIES, Status::INVALID_PARAMETER);
+
+  EXPECT_CALL(response_cb,
+              Call(3, false, matchPacket(std::move(request_unknown_response))))
+      .Times(1);
+
+  auto request_unknown =
+      TestAvrcpPacket::Make(get_capabilities_request_unknown);
+  SendMessage(3, request_unknown);
+}
+
 }  // namespace avrcp
-}  // namespace bluetooth
\ No newline at end of file
+}  // namespace bluetooth