From cc9c96334dfc52a9c266c1a5619455e1a8b0855a Mon Sep 17 00:00:00 2001
From: Henri Chataing <henrichataing@google.com>
Date: Thu, 7 Apr 2022 13:41:06 +0000
Subject: [PATCH] PDL: Unify the declaration scope for Packet and Struct
 declarations

Packet and Struct currently live in different scopes, which allows
packet and struct to have the same name. This impacts the rust
code generation as packets and structs are disambiguated by adding
appropriate suffixes, which is undesirable.

This change renames the few name conflicts in hci_packets.pdl that
prevent an immediate enforcement of this new rule.

Bug: 228327522
Test: Tested libbluetooth, root-canal target compilation
Change-Id: I88bbd394b56dbf6ac24ead4dcdeddcd467f71426
---
 system/blueberry/tests/gd/cert/matchers.py    |  4 +--
 system/gd/btaa/linux_generic/hci_processor.cc |  2 +-
 .../hci/facade/le_scanning_manager_facade.cc  |  4 +--
 system/gd/hci/hci_packets.pdl                 | 36 +++++++++----------
 system/gd/hci/le_scanning_manager.cc          | 10 +++---
 system/gd/hci/le_scanning_manager_test.cc     |  6 ++--
 system/gd/neighbor/inquiry.cc                 |  4 +--
 system/gd/packet/parser/doc/reference.md      | 14 ++++++++
 system/main/shim/btm.cc                       |  4 +--
 .../model/controller/link_layer_controller.cc |  8 ++---
 10 files changed, 53 insertions(+), 39 deletions(-)

diff --git a/system/blueberry/tests/gd/cert/matchers.py b/system/blueberry/tests/gd/cert/matchers.py
index 95dbe4dcfc5..df15d7d4e49 100644
--- a/system/blueberry/tests/gd/cert/matchers.py
+++ b/system/blueberry/tests/gd/cert/matchers.py
@@ -242,7 +242,7 @@ class NeighborMatchers(object):
         inquiry_view = hci_packets.InquiryResultView(hci_event)
         if inquiry_view is None:
             return False
-        results = inquiry_view.GetInquiryResults()
+        results = inquiry_view.GetResponses()
         return any((address == result.bd_addr for result in results))
 
     @staticmethod
@@ -257,7 +257,7 @@ class NeighborMatchers(object):
         inquiry_view = hci_packets.InquiryResultWithRssiView(hci_event)
         if inquiry_view is None:
             return False
-        results = inquiry_view.GetInquiryResults()
+        results = inquiry_view.GetResponses()
         return any((address == result.address for result in results))
 
     @staticmethod
diff --git a/system/gd/btaa/linux_generic/hci_processor.cc b/system/gd/btaa/linux_generic/hci_processor.cc
index 60c5969bc94..28c1e70b19a 100644
--- a/system/gd/btaa/linux_generic/hci_processor.cc
+++ b/system/gd/btaa/linux_generic/hci_processor.cc
@@ -74,7 +74,7 @@ void HciProcessor::process_special_event(
       if (!packet_view.IsValid()) {
         return;
       }
-      auto inquiry_results = packet_view.GetInquiryResults();
+      auto inquiry_results = packet_view.GetResponses();
       avg_byte_count = byte_count / inquiry_results.size();
       for (auto& inquiry_result : inquiry_results) {
         btaa_hci_packets.push_back(BtaaHciPacket(Activity::SCAN, inquiry_result.bd_addr_, avg_byte_count));
diff --git a/system/gd/hci/facade/le_scanning_manager_facade.cc b/system/gd/hci/facade/le_scanning_manager_facade.cc
index 8e583e218b2..ea0d058510f 100644
--- a/system/gd/hci/facade/le_scanning_manager_facade.cc
+++ b/system/gd/hci/facade/le_scanning_manager_facade.cc
@@ -124,8 +124,8 @@ class LeScanningManagerFacadeService : public LeScanningManagerFacade::Service,
       uint16_t periodic_advertising_interval,
       std::vector<uint8_t> advertising_data) {
     AdvertisingReportMsg advertising_report_msg;
-    std::vector<LeExtendedAdvertisingReport> advertisements;
-    LeExtendedAdvertisingReport le_extended_advertising_report;
+    std::vector<LeExtendedAdvertisingResponse> advertisements;
+    LeExtendedAdvertisingResponse le_extended_advertising_report;
     le_extended_advertising_report.address_type_ = (DirectAdvertisingAddressType)address_type;
     le_extended_advertising_report.address_ = address;
     le_extended_advertising_report.advertising_data_ = advertising_data;
diff --git a/system/gd/hci/hci_packets.pdl b/system/gd/hci/hci_packets.pdl
index 2051261fc0b..ed0a5318e30 100644
--- a/system/gd/hci/hci_packets.pdl
+++ b/system/gd/hci/hci_packets.pdl
@@ -4959,7 +4959,7 @@ packet InquiryComplete : Event (event_code = INQUIRY_COMPLETE) {
   status : ErrorCode,
 }
 
-struct InquiryResult {
+struct InquiryResponse {
   bd_addr : Address,
   page_scan_repetition_mode : PageScanRepetitionMode,
   _reserved_ : 8,
@@ -4970,8 +4970,8 @@ struct InquiryResult {
 }
 
 packet InquiryResult : Event (event_code = INQUIRY_RESULT) {
-  _count_(inquiry_results) : 8,
-  inquiry_results : InquiryResult[],
+  _count_(responses) : 8,
+  responses : InquiryResponse[],
 }
 
 enum LinkType : 8 {
@@ -5197,7 +5197,7 @@ packet FlowSpecificationComplete : Event (event_code = FLOW_SPECIFICATION_COMPLE
   access_latency : 32, // Octets/s
 }
 
-struct InquiryResultWithRssi {
+struct InquiryResponseWithRssi {
   address : Address,
   page_scan_repetition_mode : PageScanRepetitionMode,
   _reserved_ : 8,
@@ -5208,8 +5208,8 @@ struct InquiryResultWithRssi {
 }
 
 packet InquiryResultWithRssi : Event (event_code = INQUIRY_RESULT_WITH_RSSI) {
-  _count_(inquiry_results) : 8,
-  inquiry_results : InquiryResultWithRssi[],
+  _count_(responses) : 8,
+  responses : InquiryResponseWithRssi[],
 }
 
 packet ReadRemoteExtendedFeaturesComplete : Event (event_code = READ_REMOTE_EXTENDED_FEATURES_COMPLETE) {
@@ -5403,7 +5403,7 @@ enum AdvertisingEventType : 8 {
   SCAN_RESPONSE = 0x04,
 }
 
-struct LeAdvertisingReport {
+struct LeAdvertisingResponse {
   event_type : AdvertisingEventType,
   address_type : AddressType,
   address : Address,
@@ -5413,11 +5413,11 @@ struct LeAdvertisingReport {
 }
 
 packet LeAdvertisingReport : LeMetaEvent (subevent_code = ADVERTISING_REPORT) {
-  _count_(advertising_reports) : 8,
-  advertising_reports : LeAdvertisingReport[],
+  _count_(responses) : 8,
+  responses : LeAdvertisingResponse[],
 }
 
-struct LeAdvertisingReportRaw {
+struct LeAdvertisingResponseRaw {
   event_type : AdvertisingEventType,
   address_type : AddressType,
   address : Address,
@@ -5427,8 +5427,8 @@ struct LeAdvertisingReportRaw {
 }
 
 packet LeAdvertisingReportRaw : LeMetaEvent (subevent_code = ADVERTISING_REPORT) {
-  _count_(advertising_reports) : 8,
-  advertising_reports : LeAdvertisingReportRaw[],
+  _count_(responses) : 8,
+  responses : LeAdvertisingResponseRaw[],
 }
 
 packet LeConnectionUpdateComplete : LeMetaEvent (subevent_code = CONNECTION_UPDATE_COMPLETE) {
@@ -5514,7 +5514,7 @@ enum DirectAddressType : 8 {
   RANDOM_DEVICE_ADDRESS = 0x01,
 }
 
-struct LeDirectedAdvertisingReport {
+struct LeDirectedAdvertisingResponse {
   event_type : DirectAdvertisingEventType,
   address_type : DirectAdvertisingAddressType,
   address : Address,
@@ -5524,8 +5524,8 @@ struct LeDirectedAdvertisingReport {
 }
 
 packet LeDirectedAdvertisingReport : LeMetaEvent (subevent_code = DIRECTED_ADVERTISING_REPORT) {
-  _count_(advertising_reports) : 8,
-  advertising_reports : LeDirectedAdvertisingReport[],
+  _count_(responses) : 8,
+  responses : LeDirectedAdvertisingResponse[],
 }
 
 packet LePhyUpdateComplete : LeMetaEvent (subevent_code = PHY_UPDATE_COMPLETE) {
@@ -5543,7 +5543,7 @@ enum DataStatus : 2 {
   RESERVED = 0x3,
 }
 
-struct LeExtendedAdvertisingReport {
+struct LeExtendedAdvertisingResponse {
   connectable : 1,
   scannable : 1,
   directed : 1,
@@ -5566,8 +5566,8 @@ struct LeExtendedAdvertisingReport {
 }
 
 packet LeExtendedAdvertisingReport : LeMetaEvent (subevent_code = EXTENDED_ADVERTISING_REPORT) {
-  _count_(advertising_reports) : 8,
-  advertising_reports : LeExtendedAdvertisingReport[],
+  _count_(responses) : 8,
+  responses : LeExtendedAdvertisingResponse[],
 }
 
 packet LePeriodicAdvertisingSyncEstablished : LeMetaEvent (subevent_code = PERIODIC_ADVERTISING_SYNC_ESTABLISHED) {
diff --git a/system/gd/hci/le_scanning_manager.cc b/system/gd/hci/le_scanning_manager.cc
index 5a742c8578c..6ba1c428620 100644
--- a/system/gd/hci/le_scanning_manager.cc
+++ b/system/gd/hci/le_scanning_manager.cc
@@ -304,13 +304,13 @@ struct LeScanningManager::impl : public bluetooth::hci::LeAddressManagerCallback
       LOG_INFO("Dropping invalid advertising event");
       return;
     }
-    std::vector<LeAdvertisingReport> reports = event_view.GetAdvertisingReports();
+    std::vector<LeAdvertisingResponse> reports = event_view.GetResponses();
     if (reports.empty()) {
       LOG_INFO("Zero results in advertising event");
       return;
     }
 
-    for (LeAdvertisingReport report : reports) {
+    for (LeAdvertisingResponse report : reports) {
       uint16_t extended_event_type = 0;
       switch (report.event_type_) {
         case hci::AdvertisingEventType::ADV_IND:
@@ -362,7 +362,7 @@ struct LeScanningManager::impl : public bluetooth::hci::LeAddressManagerCallback
       LOG_INFO("Dropping invalid advertising event");
       return;
     }
-    std::vector<LeDirectedAdvertisingReport> reports = event_view.GetAdvertisingReports();
+    std::vector<LeDirectedAdvertisingResponse> reports = event_view.GetResponses();
     if (reports.empty()) {
       LOG_INFO("Zero results in advertising event");
       return;
@@ -377,13 +377,13 @@ struct LeScanningManager::impl : public bluetooth::hci::LeAddressManagerCallback
       LOG_INFO("Dropping invalid advertising event");
       return;
     }
-    std::vector<LeExtendedAdvertisingReport> reports = event_view.GetAdvertisingReports();
+    std::vector<LeExtendedAdvertisingResponse> reports = event_view.GetResponses();
     if (reports.empty()) {
       LOG_INFO("Zero results in advertising event");
       return;
     }
 
-    for (LeExtendedAdvertisingReport report : reports) {
+    for (LeExtendedAdvertisingResponse report : reports) {
       uint16_t event_type = report.connectable_ | (report.scannable_ << kScannableBit) |
                             (report.directed_ << kDirectedBit) | (report.scan_response_ << kScanResponseBit) |
                             (report.legacy_ << kLegacyBit) | ((uint16_t)report.data_status_ << kDataStatusBits);
diff --git a/system/gd/hci/le_scanning_manager_test.cc b/system/gd/hci/le_scanning_manager_test.cc
index f5e64492958..bcf0c0287c9 100644
--- a/system/gd/hci/le_scanning_manager_test.cc
+++ b/system/gd/hci/le_scanning_manager_test.cc
@@ -382,7 +382,7 @@ TEST_F(LeScanningManagerTest, start_scan_test) {
   ASSERT_EQ(std::future_status::ready, result);
   test_hci_layer_->IncomingEvent(LeSetScanEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
 
-  LeAdvertisingReport report{};
+  LeAdvertisingResponse report{};
   report.event_type_ = AdvertisingEventType::ADV_DIRECT_IND;
   report.address_type_ = AddressType::PUBLIC_DEVICE_ADDRESS;
   Address::FromString("12:34:56:78:9a:bc", report.address_);
@@ -409,7 +409,7 @@ TEST_F(LeAndroidHciScanningManagerTest, start_scan_test) {
   ASSERT_EQ(std::future_status::ready, result);
   test_hci_layer_->IncomingEvent(LeSetScanEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
 
-  LeAdvertisingReport report{};
+  LeAdvertisingResponse report{};
   report.event_type_ = AdvertisingEventType::ADV_DIRECT_IND;
   report.address_type_ = AddressType::PUBLIC_DEVICE_ADDRESS;
   Address::FromString("12:34:56:78:9a:bc", report.address_);
@@ -527,7 +527,7 @@ TEST_F(LeExtendedScanningManagerTest, start_scan_test) {
 
   test_hci_layer_->IncomingEvent(LeSetScanEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
 
-  LeExtendedAdvertisingReport report{};
+  LeExtendedAdvertisingResponse report{};
   report.connectable_ = 1;
   report.scannable_ = 0;
   report.address_type_ = DirectAdvertisingAddressType::PUBLIC_DEVICE_ADDRESS;
diff --git a/system/gd/neighbor/inquiry.cc b/system/gd/neighbor/inquiry.cc
index 9eceebb366e..f814fd02866 100644
--- a/system/gd/neighbor/inquiry.cc
+++ b/system/gd/neighbor/inquiry.cc
@@ -208,14 +208,14 @@ void neighbor::InquiryModule::impl::OnEvent(hci::EventView view) {
     case hci::EventCode::INQUIRY_RESULT: {
       auto packet = hci::InquiryResultView::Create(view);
       ASSERT(packet.IsValid());
-      LOG_INFO("Inquiry result size:%zd num_responses:%zu", packet.size(), packet.GetInquiryResults().size());
+      LOG_INFO("Inquiry result size:%zd num_responses:%zu", packet.size(), packet.GetResponses().size());
       inquiry_callbacks_.result(packet);
     } break;
 
     case hci::EventCode::INQUIRY_RESULT_WITH_RSSI: {
       auto packet = hci::InquiryResultWithRssiView::Create(view);
       ASSERT(packet.IsValid());
-      LOG_INFO("Inquiry result with rssi num_responses:%zu", packet.GetInquiryResults().size());
+      LOG_INFO("Inquiry result with rssi num_responses:%zu", packet.GetResponses().size());
       inquiry_callbacks_.result_with_rssi(packet);
     } break;
 
diff --git a/system/gd/packet/parser/doc/reference.md b/system/gd/packet/parser/doc/reference.md
index 1e521372d3f..6158faee574 100644
--- a/system/gd/packet/parser/doc/reference.md
+++ b/system/gd/packet/parser/doc/reference.md
@@ -54,6 +54,20 @@ packet Brew {
 }
 ```
 
+## Identifiers
+
+- Identifiers can denote a field; an enumeration tag; or a declared type.
+
+- Field identifiers declared in a [packet](#packet) (resp. [struct](#struct)) belong to the _scope_ that extends
+  to the packet (resp. struct), and all derived packets (resp. structs).
+
+- Field identifiers declared in a [group](#group) belong to the _scope_ that
+  extends to the packets declaring a [group field](#group_field) for this group.
+
+- Two fields may not be declared with the same identifier in any packet scope.
+
+- Two types may not be declared width the same identifier.
+
 ## Declarations
 
 > declaration: {#declaration}\
diff --git a/system/main/shim/btm.cc b/system/main/shim/btm.cc
index 3ffd99aad60..e28f16b5664 100644
--- a/system/main/shim/btm.cc
+++ b/system/main/shim/btm.cc
@@ -174,7 +174,7 @@ Btm::Btm(os::Handler* handler, neighbor::InquiryModule* inquiry)
 }
 
 void Btm::OnInquiryResult(bluetooth::hci::InquiryResultView view) {
-  for (auto& response : view.GetInquiryResults()) {
+  for (auto& response : view.GetResponses()) {
     btm_api_process_inquiry_result(
         ToRawAddress(response.bd_addr_),
         static_cast<uint8_t>(response.page_scan_repetition_mode_),
@@ -184,7 +184,7 @@ void Btm::OnInquiryResult(bluetooth::hci::InquiryResultView view) {
 
 void Btm::OnInquiryResultWithRssi(
     bluetooth::hci::InquiryResultWithRssiView view) {
-  for (auto& response : view.GetInquiryResults()) {
+  for (auto& response : view.GetResponses()) {
     btm_api_process_inquiry_result_with_rssi(
         ToRawAddress(response.address_),
         static_cast<uint8_t>(response.page_scan_repetition_mode_),
diff --git a/tools/rootcanal/model/controller/link_layer_controller.cc b/tools/rootcanal/model/controller/link_layer_controller.cc
index b4764cb6bdd..655dcd0a873 100644
--- a/tools/rootcanal/model/controller/link_layer_controller.cc
+++ b/tools/rootcanal/model/controller/link_layer_controller.cc
@@ -799,7 +799,7 @@ void LinkLayerController::IncomingInquiryResponsePacket(
           (bluetooth::hci::PageScanRepetitionMode)
               inquiry_response.GetPageScanRepetitionMode();
 
-      std::vector<bluetooth::hci::InquiryResult> responses;
+      std::vector<bluetooth::hci::InquiryResponse> responses;
       responses.emplace_back();
       responses.back().bd_addr_ = inquiry_response.GetSourceAddress();
       responses.back().page_scan_repetition_mode_ = page_scan_repetition_mode;
@@ -820,7 +820,7 @@ void LinkLayerController::IncomingInquiryResponsePacket(
           (bluetooth::hci::PageScanRepetitionMode)
               inquiry_response.GetPageScanRepetitionMode();
 
-      std::vector<bluetooth::hci::InquiryResultWithRssi> responses;
+      std::vector<bluetooth::hci::InquiryResponseWithRssi> responses;
       responses.emplace_back();
       responses.back().address_ = inquiry_response.GetSourceAddress();
       responses.back().page_scan_repetition_mode_ = page_scan_repetition_mode;
@@ -1825,7 +1825,7 @@ void LinkLayerController::IncomingLeScanResponsePacket(
     if (adv_type != model::packets::AdvertisementType::SCAN_RESPONSE) {
       return;
     }
-    bluetooth::hci::LeAdvertisingReportRaw report;
+    bluetooth::hci::LeAdvertisingResponseRaw report;
     report.event_type_ = bluetooth::hci::AdvertisingEventType::SCAN_RESPONSE;
     report.address_ = incoming.GetSourceAddress();
     report.address_type_ =
@@ -1845,7 +1845,7 @@ void LinkLayerController::IncomingLeScanResponsePacket(
       properties_.IsUnmasked(EventCode::LE_META_EVENT) &&
       properties_.GetLeEventSupported(
           bluetooth::hci::SubeventCode::EXTENDED_ADVERTISING_REPORT)) {
-    bluetooth::hci::LeExtendedAdvertisingReport report{};
+    bluetooth::hci::LeExtendedAdvertisingResponse report{};
     report.address_ = incoming.GetSourceAddress();
     report.address_type_ =
         static_cast<bluetooth::hci::DirectAdvertisingAddressType>(address_type);
-- 
GitLab