RootCanal: Use EventMask

Don't send events that have been masked

Test: gd/cert/run
Tag: #testing
Change-Id: Icb8a94db570fb77c852054faa2e0e411fe2b23a8
Signed-off-by: David Duarte <licorne@google.com>
diff --git a/system/gd/cert/py_hal.py b/system/gd/cert/py_hal.py
index cba7b30..3348b6a 100644
--- a/system/gd/cert/py_hal.py
+++ b/system/gd/cert/py_hal.py
@@ -119,6 +119,8 @@
         self.hci_event_stream = EventStream(self.device.hal.StreamEvents(empty_proto.Empty()))
         self.acl_stream = EventStream(self.device.hal.StreamAcl(empty_proto.Empty()))
 
+        self.event_mask = 0x1FFF_FFFF_FFFF  # Default Event Mask (Core Vol 4 [E] 7.3.1)
+
         # We don't deal with SCO for now
 
     def close(self):
@@ -169,6 +171,11 @@
                                                            [phy_scan_params]))
         self.wait_for_complete(OpCode.LE_SET_EXTENDED_SCAN_PARAMETERS)
 
+    def unmask_event(self, *event_codes):
+        for event_code in event_codes:
+            self.event_mask |= 1 << (int(event_code) - 1)
+        self.send_hci_command(hci_packets.SetEventMaskBuilder(self.event_mask))
+
     def start_scanning(self):
         self.send_hci_command(
             hci_packets.LeSetExtendedScanEnableBuilder(hci_packets.Enable.ENABLED,
diff --git a/system/gd/hal/cert/simple_hal_test_lib.py b/system/gd/hal/cert/simple_hal_test_lib.py
index 9ccef7a..691d938 100644
--- a/system/gd/hal/cert/simple_hal_test_lib.py
+++ b/system/gd/hal/cert/simple_hal_test_lib.py
@@ -73,6 +73,7 @@
                                                              )
 
     def test_le_ad_scan_cert_advertises(self):
+        self.dut_hal.unmask_event(hci_packets.EventCode.LE_META_EVENT)
         self.dut_hal.set_random_le_address('0D:05:04:03:02:01')
 
         self.dut_hal.set_scan_parameters()
@@ -96,10 +97,12 @@
         self.dut_hal.stop_scanning()
 
     def test_le_connection_dut_advertises(self):
+        self.cert_hal.unmask_event(hci_packets.EventCode.LE_META_EVENT)
         self.cert_hal.set_random_le_address('0C:05:04:03:02:01')
         self.cert_hal.initiate_le_connection('0D:05:04:03:02:01')
 
         # DUT Advertises
+        self.dut_hal.unmask_event(hci_packets.EventCode.LE_META_EVENT)
         advertisement = self.dut_hal.create_advertisement(0, '0D:05:04:03:02:01')
         advertisement.set_data(b'Im_The_DUT')
         advertisement.set_scan_response(b'Im_The_D')
@@ -115,10 +118,12 @@
         assertThat(self.dut_hal.get_acl_stream()).emits(lambda packet: b'SomeMoreAclData' in packet.payload)
 
     def test_le_connect_list_connection_cert_advertises(self):
+        self.dut_hal.unmask_event(hci_packets.EventCode.LE_META_EVENT)
         self.dut_hal.set_random_le_address('0D:05:04:03:02:01')
         self.dut_hal.add_to_connect_list('0C:05:04:03:02:01')
         self.dut_hal.initiate_le_connection_by_connect_list('BA:D5:A4:A3:A2:A1')
 
+        self.cert_hal.unmask_event(hci_packets.EventCode.LE_META_EVENT)
         advertisement = self.cert_hal.create_advertisement(
             1,
             '0C:05:04:03:02:01',
diff --git a/system/gd/hci/cert/direct_hci_test_lib.py b/system/gd/hci/cert/direct_hci_test_lib.py
index cd6c210..c517f32 100644
--- a/system/gd/hci/cert/direct_hci_test_lib.py
+++ b/system/gd/hci/cert/direct_hci_test_lib.py
@@ -202,6 +202,7 @@
                                             SubeventCode.ENHANCED_CONNECTION_COMPLETE,
                                             SubeventCode.READ_REMOTE_FEATURES_COMPLETE)
         # Cert Connects
+        self.cert_hal.unmask_event(EventCode.LE_META_EVENT)
         self.cert_hal.send_hci_command(LeSetRandomAddressBuilder('0C:05:04:03:02:01'))
         phy_scan_params = DirectHciTestBase._create_phy_scan_params()
         self.cert_hal.send_hci_command(
@@ -242,6 +243,7 @@
                                               OwnAddressType.RANDOM_DEVICE_ADDRESS, AddressType.RANDOM_DEVICE_ADDRESS,
                                               'BA:D5:A4:A3:A2:A1', 1, [phy_scan_params]))
 
+        self.cert_hal.unmask_event(EventCode.LE_META_EVENT)
         advertisement = self.cert_hal.create_advertisement(
             1,
             '0C:05:04:03:02:01',
diff --git a/system/vendor_libs/test_vendor_lib/model/controller/dual_mode_controller.cc b/system/vendor_libs/test_vendor_lib/model/controller/dual_mode_controller.cc
index 2ed8c05..5bc541b 100644
--- a/system/vendor_libs/test_vendor_lib/model/controller/dual_mode_controller.cc
+++ b/system/vendor_libs/test_vendor_lib/model/controller/dual_mode_controller.cc
@@ -331,8 +331,10 @@
     cp.connection_handle_ = handle;
     cp.host_num_of_completed_packets_ = kNumCommandPackets;
     completed_packets.push_back(cp);
-    send_event_(bluetooth::hci::NumberOfCompletedPacketsBuilder::Create(
-        completed_packets));
+    if (properties_.IsUnmasked(EventCode::NUMBER_OF_COMPLETED_PACKETS)) {
+      send_event_(bluetooth::hci::NumberOfCompletedPacketsBuilder::Create(
+          completed_packets));
+    }
     return;
   }
 
@@ -352,8 +354,10 @@
     cp.connection_handle_ = handle;
     cp.host_num_of_completed_packets_ = kNumCommandPackets;
     completed_packets.push_back(cp);
-    send_event_(bluetooth::hci::NumberOfCompletedPacketsBuilder::Create(
-        completed_packets));
+    if (properties_.IsUnmasked(EventCode::NUMBER_OF_COMPLETED_PACKETS)) {
+      send_event_(bluetooth::hci::NumberOfCompletedPacketsBuilder::Create(
+          completed_packets));
+    }
     return;
   }
 }
diff --git a/system/vendor_libs/test_vendor_lib/model/controller/link_layer_controller.cc b/system/vendor_libs/test_vendor_lib/model/controller/link_layer_controller.cc
index f03de98..a44d073 100644
--- a/system/vendor_libs/test_vendor_lib/model/controller/link_layer_controller.cc
+++ b/system/vendor_libs/test_vendor_lib/model/controller/link_layer_controller.cc
@@ -15,6 +15,7 @@
  */
 
 #include "link_layer_controller.h"
+
 #include <hci/hci_packets.h>
 
 #include "include/le_advertisement.h"
@@ -26,6 +27,7 @@
 using bluetooth::hci::Address;
 using bluetooth::hci::AddressType;
 using bluetooth::hci::AddressWithType;
+using bluetooth::hci::EventCode;
 
 using namespace model::packets;
 using model::packets::PacketType;
@@ -158,7 +160,9 @@
     completed_packets.push_back(cp);
     auto packet = bluetooth::hci::NumberOfCompletedPacketsBuilder::Create(
         completed_packets);
-    send_event_(std::move(packet));
+    if (properties_.IsUnmasked(EventCode::NUMBER_OF_COMPLETED_PACKETS)) {
+      send_event_(std::move(packet));
+    }
   });
 
   auto acl_payload = acl_packet.GetPayload();
@@ -431,8 +435,10 @@
   auto view = model::packets::RemoteNameRequestResponseView::Create(packet);
   ASSERT(view.IsValid());
 
-  send_event_(bluetooth::hci::RemoteNameRequestCompleteBuilder::Create(
-      ErrorCode::SUCCESS, packet.GetSourceAddress(), view.GetName()));
+  if (properties_.IsUnmasked(EventCode::REMOTE_NAME_REQUEST_COMPLETE)) {
+    send_event_(bluetooth::hci::RemoteNameRequestCompleteBuilder::Create(
+        ErrorCode::SUCCESS, packet.GetSourceAddress(), view.GetName()));
+  }
 }
 
 void LinkLayerController::IncomingReadRemoteLmpFeatures(
@@ -447,9 +453,12 @@
     model::packets::LinkLayerPacketView packet) {
   auto view = model::packets::ReadRemoteLmpFeaturesResponseView::Create(packet);
   ASSERT(view.IsValid());
-  send_event_(
-      bluetooth::hci::RemoteHostSupportedFeaturesNotificationBuilder::Create(
-          packet.GetSourceAddress(), view.GetFeatures()));
+  if (properties_.IsUnmasked(
+          EventCode::REMOTE_HOST_SUPPORTED_FEATURES_NOTIFICATION)) {
+    send_event_(
+        bluetooth::hci::RemoteHostSupportedFeaturesNotificationBuilder::Create(
+            packet.GetSourceAddress(), view.GetFeatures()));
+  }
 }
 
 void LinkLayerController::IncomingReadRemoteSupportedFeatures(
@@ -472,9 +481,12 @@
              source.ToString().c_str());
     return;
   }
-  send_event_(
-      bluetooth::hci::ReadRemoteSupportedFeaturesCompleteBuilder::Create(
-          ErrorCode::SUCCESS, handle, view.GetFeatures()));
+  if (properties_.IsUnmasked(
+          EventCode::READ_REMOTE_SUPPORTED_FEATURES_COMPLETE)) {
+    send_event_(
+        bluetooth::hci::ReadRemoteSupportedFeaturesCompleteBuilder::Create(
+            ErrorCode::SUCCESS, handle, view.GetFeatures()));
+  }
 }
 
 void LinkLayerController::IncomingReadRemoteExtendedFeatures(
@@ -505,9 +517,13 @@
              source.ToString().c_str());
     return;
   }
-  send_event_(bluetooth::hci::ReadRemoteExtendedFeaturesCompleteBuilder::Create(
-      static_cast<ErrorCode>(view.GetStatus()), handle, view.GetPageNumber(),
-      view.GetMaxPageNumber(), view.GetFeatures()));
+  if (properties_.IsUnmasked(
+          EventCode::READ_REMOTE_EXTENDED_FEATURES_COMPLETE)) {
+    send_event_(
+        bluetooth::hci::ReadRemoteExtendedFeaturesCompleteBuilder::Create(
+            static_cast<ErrorCode>(view.GetStatus()), handle,
+            view.GetPageNumber(), view.GetMaxPageNumber(), view.GetFeatures()));
+  }
 }
 
 void LinkLayerController::IncomingReadRemoteVersion(
@@ -531,10 +547,13 @@
              source.ToString().c_str());
     return;
   }
-  send_event_(
-      bluetooth::hci::ReadRemoteVersionInformationCompleteBuilder::Create(
-          ErrorCode::SUCCESS, handle, view.GetLmpVersion(),
-          view.GetManufacturerName(), view.GetLmpSubversion()));
+  if (properties_.IsUnmasked(
+          EventCode::READ_REMOTE_VERSION_INFORMATION_COMPLETE)) {
+    send_event_(
+        bluetooth::hci::ReadRemoteVersionInformationCompleteBuilder::Create(
+            ErrorCode::SUCCESS, handle, view.GetLmpVersion(),
+            view.GetManufacturerName(), view.GetLmpSubversion()));
+  }
 }
 
 void LinkLayerController::IncomingReadClockOffset(
@@ -555,8 +574,10 @@
              source.ToString().c_str());
     return;
   }
-  send_event_(bluetooth::hci::ReadClockOffsetCompleteBuilder::Create(
-      ErrorCode::SUCCESS, handle, view.GetOffset()));
+  if (properties_.IsUnmasked(EventCode::READ_CLOCK_OFFSET_COMPLETE)) {
+    send_event_(bluetooth::hci::ReadClockOffsetCompleteBuilder::Create(
+        ErrorCode::SUCCESS, handle, view.GetOffset()));
+  }
 }
 
 void LinkLayerController::IncomingDisconnectPacket(
@@ -591,8 +612,10 @@
     LOG_INFO("Unknown connection @%s", peer.ToString().c_str());
     return;
   }
-  send_event_(bluetooth::hci::EncryptionChangeBuilder::Create(
-      ErrorCode::SUCCESS, handle, bluetooth::hci::EncryptionEnabled::ON));
+  if (properties_.IsUnmasked(EventCode::ENCRYPTION_CHANGE)) {
+    send_event_(bluetooth::hci::EncryptionChangeBuilder::Create(
+        ErrorCode::SUCCESS, handle, bluetooth::hci::EncryptionEnabled::ON));
+  }
 
   uint16_t count = security_manager_.ReadKey(peer);
   if (count == 0) {
@@ -619,7 +642,9 @@
   }
   auto packet = bluetooth::hci::EncryptionChangeBuilder::Create(
       ErrorCode::SUCCESS, handle, bluetooth::hci::EncryptionEnabled::ON);
-  send_event_(std::move(packet));
+  if (properties_.IsUnmasked(EventCode::ENCRYPTION_CHANGE)) {
+    send_event_(std::move(packet));
+  }
 }
 
 void LinkLayerController::IncomingInquiryPacket(
@@ -689,7 +714,9 @@
       responses.back().class_of_device_ = inquiry_response.GetClassOfDevice();
       responses.back().clock_offset_ = inquiry_response.GetClockOffset();
       auto packet = bluetooth::hci::InquiryResultBuilder::Create(responses);
-      send_event_(std::move(packet));
+      if (properties_.IsUnmasked(EventCode::INQUIRY_RESULT)) {
+        send_event_(std::move(packet));
+      }
     } break;
 
     case (model::packets::InquiryType::RSSI): {
@@ -711,7 +738,9 @@
       responses.back().rssi_ = inquiry_response.GetRssi();
       auto packet =
           bluetooth::hci::InquiryResultWithRssiBuilder::Create(responses);
-      send_event_(std::move(packet));
+      if (properties_.IsUnmasked(EventCode::INQUIRY_RESULT_WITH_RSSI)) {
+        send_event_(std::move(packet));
+      }
     } break;
 
     case (model::packets::InquiryType::EXTENDED): {
@@ -737,7 +766,9 @@
       auto packet = bluetooth::hci::EventBuilder::Create(
           bluetooth::hci::EventCode::EXTENDED_INQUIRY_RESULT,
           std::move(raw_builder_ptr));
-      send_event_(std::move(packet));
+      if (properties_.IsUnmasked(EventCode::EXTENDED_INQUIRY_RESULT)) {
+        send_event_(std::move(packet));
+      }
     } break;
     default:
       LOG_WARN("Unhandled Incoming Inquiry Response of type %d",
@@ -766,8 +797,10 @@
     security_manager_.AuthenticationRequest(incoming.GetSourceAddress(), handle,
                                             false);
     security_manager_.SetPinRequested(peer);
-    send_event_(bluetooth::hci::PinCodeRequestBuilder::Create(
-        incoming.GetSourceAddress()));
+    if (properties_.IsUnmasked(EventCode::PIN_CODE_REQUEST)) {
+      send_event_(bluetooth::hci::PinCodeRequestBuilder::Create(
+          incoming.GetSourceAddress()));
+    }
     return;
   }
 
@@ -783,7 +816,9 @@
       static_cast<bluetooth::hci::OobDataPresent>(oob_data_present),
       static_cast<bluetooth::hci::AuthenticationRequirements>(
           authentication_requirements));
-  send_event_(std::move(packet));
+  if (properties_.IsUnmasked(EventCode::IO_CAPABILITY_RESPONSE)) {
+    send_event_(std::move(packet));
+  }
 
   bool pairing_started = security_manager_.AuthenticationInProgress();
   if (!pairing_started) {
@@ -833,7 +868,9 @@
       static_cast<bluetooth::hci::OobDataPresent>(oob_data_present),
       static_cast<bluetooth::hci::AuthenticationRequirements>(
           authentication_requirements));
-  send_event_(std::move(packet));
+  if (properties_.IsUnmasked(EventCode::IO_CAPABILITY_RESPONSE)) {
+    send_event_(std::move(packet));
+  }
 
   PairingType pairing_type = security_manager_.GetSimplePairingType();
   if (pairing_type != PairingType::INVALID) {
@@ -855,8 +892,10 @@
   LOG_INFO("%s doesn't support SSP, try PIN",
            incoming.GetSourceAddress().ToString().c_str());
   security_manager_.SetPinRequested(peer);
-  send_event_(bluetooth::hci::PinCodeRequestBuilder::Create(
-      incoming.GetSourceAddress()));
+  if (properties_.IsUnmasked(EventCode::PIN_CODE_REQUEST)) {
+    send_event_(bluetooth::hci::PinCodeRequestBuilder::Create(
+        incoming.GetSourceAddress()));
+  }
 }
 
 void LinkLayerController::IncomingIsoPacket(LinkLayerPacketView incoming) {
@@ -1016,9 +1055,11 @@
   connections_.CreatePendingCis(config);
   connections_.SetRemoteCisHandle(config.cis_connection_handle_,
                                   req.GetRequesterCisHandle());
-  send_event_(bluetooth::hci::LeCisRequestBuilder::Create(
-      config.acl_connection_handle_, config.cis_connection_handle_, group_id,
-      req.GetId()));
+  if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
+    send_event_(bluetooth::hci::LeCisRequestBuilder::Create(
+        config.acl_connection_handle_, config.cis_connection_handle_, group_id,
+        req.GetId()));
+  }
 }
 
 void LinkLayerController::IncomingIsoConnectionResponsePacket(
@@ -1036,10 +1077,13 @@
   }
   ErrorCode status = static_cast<ErrorCode>(response.GetStatus());
   if (status != ErrorCode::SUCCESS) {
-    send_event_(bluetooth::hci::LeCisEstablishedBuilder::Create(
-        status, config.cis_connection_handle_, 0, 0, 0, 0,
-        bluetooth::hci::SecondaryPhyType::NO_PACKETS,
-        bluetooth::hci::SecondaryPhyType::NO_PACKETS, 0, 0, 0, 0, 0, 0, 0, 0));
+    if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
+      send_event_(bluetooth::hci::LeCisEstablishedBuilder::Create(
+          status, config.cis_connection_handle_, 0, 0, 0, 0,
+          bluetooth::hci::SecondaryPhyType::NO_PACKETS,
+          bluetooth::hci::SecondaryPhyType::NO_PACKETS, 0, 0, 0, 0, 0, 0, 0,
+          0));
+    }
     return;
   }
   connections_.SetRemoteCisHandle(config.cis_connection_handle_,
@@ -1062,12 +1106,14 @@
   uint8_t max_pdu_m_to_s = 0x40;
   uint8_t max_pdu_s_to_m = 0x40;
   uint16_t iso_interval = 0x100;
-  send_event_(bluetooth::hci::LeCisEstablishedBuilder::Create(
-      status, config.cis_connection_handle_, cig_sync_delay, cis_sync_delay,
-      latency_m_to_s, latency_s_to_m,
-      bluetooth::hci::SecondaryPhyType::NO_PACKETS,
-      bluetooth::hci::SecondaryPhyType::NO_PACKETS, nse, bn_m_to_s, bn_s_to_m,
-      ft_m_to_s, ft_s_to_m, max_pdu_m_to_s, max_pdu_s_to_m, iso_interval));
+  if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
+    send_event_(bluetooth::hci::LeCisEstablishedBuilder::Create(
+        status, config.cis_connection_handle_, cig_sync_delay, cis_sync_delay,
+        latency_m_to_s, latency_s_to_m,
+        bluetooth::hci::SecondaryPhyType::NO_PACKETS,
+        bluetooth::hci::SecondaryPhyType::NO_PACKETS, nse, bn_m_to_s, bn_s_to_m,
+        ft_m_to_s, ft_s_to_m, max_pdu_m_to_s, max_pdu_s_to_m, iso_interval));
+  }
 }
 
 void LinkLayerController::IncomingKeypressNotificationPacket(
@@ -1081,10 +1127,12 @@
              static_cast<int>(notification_type));
     return;
   }
-  send_event_(bluetooth::hci::KeypressNotificationBuilder::Create(
-      incoming.GetSourceAddress(),
-      static_cast<bluetooth::hci::KeypressNotificationType>(
-          notification_type)));
+  if (properties_.IsUnmasked(EventCode::KEYPRESS_NOTIFICATION)) {
+    send_event_(bluetooth::hci::KeypressNotificationBuilder::Create(
+        incoming.GetSourceAddress(),
+        static_cast<bluetooth::hci::KeypressNotificationType>(
+            notification_type)));
+  }
 }
 
 void LinkLayerController::IncomingLeAdvertisementPacket(
@@ -1113,7 +1161,9 @@
     raw_builder_ptr->AddOctets1(GetRssi());
     auto packet = bluetooth::hci::EventBuilder::Create(
         bluetooth::hci::EventCode::LE_META_EVENT, std::move(raw_builder_ptr));
-    send_event_(std::move(packet));
+    if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
+      send_event_(std::move(packet));
+    }
   }
 
   if (le_scan_enable_ == bluetooth::hci::OpCode::LE_SET_EXTENDED_SCAN_ENABLE) {
@@ -1154,8 +1204,11 @@
     raw_builder_ptr->AddAddress(Address::kEmpty);  // Direct_Address
     raw_builder_ptr->AddOctets1(ad.size());
     raw_builder_ptr->AddOctets(ad);
-    send_event_(bluetooth::hci::EventBuilder::Create(
-        bluetooth::hci::EventCode::LE_META_EVENT, std::move(raw_builder_ptr)));
+    if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
+      send_event_(bluetooth::hci::EventBuilder::Create(
+          bluetooth::hci::EventCode::LE_META_EVENT,
+          std::move(raw_builder_ptr)));
+    }
   }
 
   // Active scanning
@@ -1227,7 +1280,9 @@
       address.GetAddressType(), address.GetAddress(), connection_interval,
       connection_latency, supervision_timeout,
       static_cast<bluetooth::hci::ClockAccuracy>(0x00));
-  send_event_(std::move(packet));
+  if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
+    send_event_(std::move(packet));
+  }
 }
 
 void LinkLayerController::IncomingLeConnectPacket(
@@ -1312,8 +1367,10 @@
 
   // TODO: Save keys to check
 
-  send_event_(bluetooth::hci::LeLongTermKeyRequestBuilder::Create(
-      handle, le_encrypt.GetRand(), le_encrypt.GetEdiv()));
+  if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
+    send_event_(bluetooth::hci::LeLongTermKeyRequestBuilder::Create(
+        handle, le_encrypt.GetRand(), le_encrypt.GetEdiv()));
+  }
 }
 
 void LinkLayerController::IncomingLeEncryptConnectionResponse(
@@ -1339,12 +1396,16 @@
   }
 
   if (connections_.IsEncrypted(handle)) {
-    send_event_(bluetooth::hci::EncryptionKeyRefreshCompleteBuilder::Create(
-        status, handle));
+    if (properties_.IsUnmasked(EventCode::ENCRYPTION_KEY_REFRESH_COMPLETE)) {
+      send_event_(bluetooth::hci::EncryptionKeyRefreshCompleteBuilder::Create(
+          status, handle));
+    }
   } else {
     connections_.Encrypt(handle);
-    send_event_(bluetooth::hci::EncryptionChangeBuilder::Create(
-        status, handle, bluetooth::hci::EncryptionEnabled::ON));
+    if (properties_.IsUnmasked(EventCode::ENCRYPTION_CHANGE)) {
+      send_event_(bluetooth::hci::EncryptionChangeBuilder::Create(
+          status, handle, bluetooth::hci::EncryptionEnabled::ON));
+    }
   }
 }
 
@@ -1380,8 +1441,10 @@
   } else {
     status = static_cast<ErrorCode>(response.GetStatus());
   }
-  send_event_(bluetooth::hci::LeReadRemoteFeaturesCompleteBuilder::Create(
-      status, handle, response.GetFeatures()));
+  if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
+    send_event_(bluetooth::hci::LeReadRemoteFeaturesCompleteBuilder::Create(
+        status, handle, response.GetFeatures()));
+  }
 }
 
 void LinkLayerController::IncomingLeScanPacket(
@@ -1421,7 +1484,9 @@
     raw_builder_ptr->AddOctets1(GetRssi());
     auto packet = bluetooth::hci::EventBuilder::Create(
         bluetooth::hci::EventCode::LE_META_EVENT, std::move(raw_builder_ptr));
-    send_event_(std::move(packet));
+    if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
+      send_event_(std::move(packet));
+    }
   }
 
   if (le_scan_enable_ == bluetooth::hci::OpCode::LE_SET_EXTENDED_SCAN_ENABLE) {
@@ -1445,7 +1510,9 @@
     raw_builder_ptr->AddOctets(ad);
     auto packet = bluetooth::hci::EventBuilder::Create(
         bluetooth::hci::EventCode::LE_META_EVENT, std::move(raw_builder_ptr));
-    send_event_(std::move(packet));
+    if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
+      send_event_(std::move(packet));
+    }
   }
 }
 
@@ -1463,8 +1530,10 @@
   auto current_peer = incoming.GetSourceAddress();
   security_manager_.AuthenticationRequestFinished();
   ScheduleTask(milliseconds(5), [this, current_peer]() {
-    send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
-        ErrorCode::AUTHENTICATION_FAILURE, current_peer));
+    if (properties_.IsUnmasked(EventCode::SIMPLE_PAIRING_COMPLETE)) {
+      send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
+          ErrorCode::AUTHENTICATION_FAILURE, current_peer));
+    }
   });
 }
 
@@ -1510,8 +1579,10 @@
       } else {
         security_manager_.AuthenticationRequestFinished();
         ScheduleTask(milliseconds(5), [this, peer]() {
-          send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
-              ErrorCode::AUTHENTICATION_FAILURE, peer));
+          if (properties_.IsUnmasked(EventCode::SIMPLE_PAIRING_COMPLETE)) {
+            send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
+                ErrorCode::AUTHENTICATION_FAILURE, peer));
+          }
         });
       }
     }
@@ -1519,7 +1590,9 @@
     LOG_INFO("PIN pairing %s", properties_.GetAddress().ToString().c_str());
     ScheduleTask(milliseconds(5), [this, peer]() {
       security_manager_.SetPinRequested(peer);
-      send_event_(bluetooth::hci::PinCodeRequestBuilder::Create(peer));
+      if (properties_.IsUnmasked(EventCode::PIN_CODE_REQUEST)) {
+        send_event_(bluetooth::hci::PinCodeRequestBuilder::Create(peer));
+      }
     });
   }
 }
@@ -1559,8 +1632,10 @@
       } else {
         security_manager_.AuthenticationRequestFinished();
         ScheduleTask(milliseconds(5), [this, peer]() {
-          send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
-              ErrorCode::AUTHENTICATION_FAILURE, peer));
+          if (properties_.IsUnmasked(EventCode::SIMPLE_PAIRING_COMPLETE)) {
+            send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
+                ErrorCode::AUTHENTICATION_FAILURE, peer));
+          }
         });
       }
     }
@@ -1568,7 +1643,9 @@
     LOG_INFO("PIN pairing %s", properties_.GetAddress().ToString().c_str());
     ScheduleTask(milliseconds(5), [this, peer]() {
       security_manager_.SetPinRequested(peer);
-      send_event_(bluetooth::hci::PinCodeRequestBuilder::Create(peer));
+      if (properties_.IsUnmasked(EventCode::PIN_CODE_REQUEST)) {
+        send_event_(bluetooth::hci::PinCodeRequestBuilder::Create(peer));
+      }
     });
   }
 }
@@ -1594,7 +1671,9 @@
       source_address, page.GetClassOfDevice(),
       bluetooth::hci::ConnectionRequestLinkType::ACL);
 
-  send_event_(std::move(packet));
+  if (properties_.IsUnmasked(EventCode::CONNECTION_REQUEST)) {
+    send_event_(std::move(packet));
+  }
 }
 
 void LinkLayerController::IncomingPageRejectPacket(
@@ -1607,7 +1686,9 @@
       static_cast<ErrorCode>(reject.GetReason()), 0x0eff,
       incoming.GetSourceAddress(), bluetooth::hci::LinkType::ACL,
       bluetooth::hci::Enable::DISABLED);
-  send_event_(std::move(packet));
+  if (properties_.IsUnmasked(EventCode::CONNECTION_COMPLETE)) {
+    send_event_(std::move(packet));
+  }
 }
 
 void LinkLayerController::IncomingPageResponsePacket(
@@ -1624,7 +1705,9 @@
   auto packet = bluetooth::hci::ConnectionCompleteBuilder::Create(
       ErrorCode::SUCCESS, handle, incoming.GetSourceAddress(),
       bluetooth::hci::LinkType::ACL, bluetooth::hci::Enable::DISABLED);
-  send_event_(std::move(packet));
+  if (properties_.IsUnmasked(EventCode::CONNECTION_COMPLETE)) {
+    send_event_(std::move(packet));
+  }
 
   if (awaiting_authentication) {
     ScheduleTask(milliseconds(5), [this, peer, handle]() {
@@ -1716,7 +1799,9 @@
 void LinkLayerController::StartSimplePairing(const Address& address) {
   // IO Capability Exchange (See the Diagram in the Spec)
   auto packet = bluetooth::hci::IoCapabilityRequestBuilder::Create(address);
-  send_event_(std::move(packet));
+  if (properties_.IsUnmasked(EventCode::IO_CAPABILITY_REQUEST)) {
+    send_event_(std::move(packet));
+  }
 
   // Get a Key, then authenticate
   // PublicKeyExchange(address);
@@ -1730,27 +1815,39 @@
   // TODO: Public key exchange first?
   switch (pairing_type) {
     case PairingType::AUTO_CONFIRMATION:
-      send_event_(
-          bluetooth::hci::UserConfirmationRequestBuilder::Create(peer, 123456));
+      if (properties_.IsUnmasked(EventCode::USER_CONFIRMATION_REQUEST)) {
+        send_event_(bluetooth::hci::UserConfirmationRequestBuilder::Create(
+            peer, 123456));
+      }
       break;
     case PairingType::CONFIRM_Y_N:
-      send_event_(
-          bluetooth::hci::UserConfirmationRequestBuilder::Create(peer, 123456));
+      if (properties_.IsUnmasked(EventCode::USER_CONFIRMATION_REQUEST)) {
+        send_event_(bluetooth::hci::UserConfirmationRequestBuilder::Create(
+            peer, 123456));
+      }
       break;
     case PairingType::DISPLAY_PIN:
-      send_event_(
-          bluetooth::hci::UserPasskeyNotificationBuilder::Create(peer, 123456));
+      if (properties_.IsUnmasked(EventCode::USER_PASSKEY_NOTIFICATION)) {
+        send_event_(bluetooth::hci::UserPasskeyNotificationBuilder::Create(
+            peer, 123456));
+      }
       break;
     case PairingType::DISPLAY_AND_CONFIRM:
-      send_event_(
-          bluetooth::hci::UserConfirmationRequestBuilder::Create(peer, 123456));
+      if (properties_.IsUnmasked(EventCode::USER_CONFIRMATION_REQUEST)) {
+        send_event_(bluetooth::hci::UserConfirmationRequestBuilder::Create(
+            peer, 123456));
+      }
       break;
     case PairingType::INPUT_PIN:
-      send_event_(bluetooth::hci::UserPasskeyRequestBuilder::Create(peer));
+      if (properties_.IsUnmasked(EventCode::USER_PASSKEY_REQUEST)) {
+        send_event_(bluetooth::hci::UserPasskeyRequestBuilder::Create(peer));
+      }
       break;
     case PairingType::OUT_OF_BAND:
       LOG_INFO("Oob data request for %s", peer.ToString().c_str());
-      send_event_(bluetooth::hci::RemoteOobDataRequestBuilder::Create(peer));
+      if (properties_.IsUnmasked(EventCode::REMOTE_OOB_DATA_REQUEST)) {
+        send_event_(bluetooth::hci::RemoteOobDataRequestBuilder::Create(peer));
+      }
       break;
     case PairingType::PEER_HAS_OUT_OF_BAND:
       LOG_INFO("Trusting that %s has OOB data", peer.ToString().c_str());
@@ -1769,7 +1866,9 @@
   if (security_manager_.IsInitiator()) {
     auto packet = bluetooth::hci::AuthenticationCompleteBuilder::Create(
         ErrorCode::SUCCESS, handle);
-    send_event_(std::move(packet));
+    if (properties_.IsUnmasked(EventCode::AUTHENTICATION_COMPLETE)) {
+      send_event_(std::move(packet));
+    }
   }
 }
 
@@ -1803,7 +1902,9 @@
     LOG_INFO("PIN pairing %s", properties_.GetAddress().ToString().c_str());
     ScheduleTask(milliseconds(5), [this, address]() {
       security_manager_.SetPinRequested(address);
-      send_event_(bluetooth::hci::PinCodeRequestBuilder::Create(address));
+      if (properties_.IsUnmasked(EventCode::PIN_CODE_REQUEST)) {
+        send_event_(bluetooth::hci::PinCodeRequestBuilder::Create(address));
+      }
     });
   }
   return ErrorCode::SUCCESS;
@@ -1876,18 +1977,24 @@
   if (key_type == 'L') {
     // Legacy
     ScheduleTask(milliseconds(5), [this, peer, key_vec]() {
-      send_event_(bluetooth::hci::LinkKeyNotificationBuilder::Create(
-          peer, key_vec, bluetooth::hci::KeyType::AUTHENTICATED_P192));
+      if (properties_.IsUnmasked(EventCode::LINK_KEY_NOTIFICATION)) {
+        send_event_(bluetooth::hci::LinkKeyNotificationBuilder::Create(
+            peer, key_vec, bluetooth::hci::KeyType::AUTHENTICATED_P192));
+      }
     });
   } else {
     ScheduleTask(milliseconds(5), [this, peer]() {
-      send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
-          ErrorCode::SUCCESS, peer));
+      if (properties_.IsUnmasked(EventCode::SIMPLE_PAIRING_COMPLETE)) {
+        send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
+            ErrorCode::SUCCESS, peer));
+      }
     });
 
     ScheduleTask(milliseconds(5), [this, peer, key_vec]() {
-      send_event_(bluetooth::hci::LinkKeyNotificationBuilder::Create(
-          peer, key_vec, bluetooth::hci::KeyType::AUTHENTICATED_P256));
+      if (properties_.IsUnmasked(EventCode::LINK_KEY_NOTIFICATION)) {
+        send_event_(bluetooth::hci::LinkKeyNotificationBuilder::Create(
+            peer, key_vec, bluetooth::hci::KeyType::AUTHENTICATED_P256));
+      }
     });
   }
 
@@ -1904,8 +2011,10 @@
              peer.ToString().c_str(), current_peer.ToString().c_str());
     security_manager_.AuthenticationRequestFinished();
     ScheduleTask(milliseconds(5), [this, current_peer]() {
-      send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
-          ErrorCode::AUTHENTICATION_FAILURE, current_peer));
+      if (properties_.IsUnmasked(EventCode::SIMPLE_PAIRING_COMPLETE)) {
+        send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
+            ErrorCode::AUTHENTICATION_FAILURE, current_peer));
+      }
     });
     return ErrorCode::UNKNOWN_CONNECTION;
   }
@@ -1921,8 +2030,10 @@
     } else {
       security_manager_.AuthenticationRequestFinished();
       ScheduleTask(milliseconds(5), [this, peer]() {
-        send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
-            ErrorCode::AUTHENTICATION_FAILURE, peer));
+        if (properties_.IsUnmasked(EventCode::SIMPLE_PAIRING_COMPLETE)) {
+          send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
+              ErrorCode::AUTHENTICATION_FAILURE, peer));
+        }
       });
     }
   } else {
@@ -1937,8 +2048,10 @@
   auto current_peer = security_manager_.GetAuthenticationAddress();
   security_manager_.AuthenticationRequestFinished();
   ScheduleTask(milliseconds(5), [this, current_peer]() {
-    send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
-        ErrorCode::AUTHENTICATION_FAILURE, current_peer));
+    if (properties_.IsUnmasked(EventCode::SIMPLE_PAIRING_COMPLETE)) {
+      send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
+          ErrorCode::AUTHENTICATION_FAILURE, current_peer));
+    }
   });
   if (peer != current_peer) {
     return ErrorCode::UNKNOWN_CONNECTION;
@@ -1964,8 +2077,10 @@
   auto current_peer = security_manager_.GetAuthenticationAddress();
   security_manager_.AuthenticationRequestFinished();
   ScheduleTask(milliseconds(5), [this, current_peer]() {
-    send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
-        ErrorCode::AUTHENTICATION_FAILURE, current_peer));
+    if (properties_.IsUnmasked(EventCode::SIMPLE_PAIRING_COMPLETE)) {
+      send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
+          ErrorCode::AUTHENTICATION_FAILURE, current_peer));
+    }
   });
   if (peer != current_peer) {
     return ErrorCode::UNKNOWN_CONNECTION;
@@ -1990,8 +2105,10 @@
   auto current_peer = security_manager_.GetAuthenticationAddress();
   security_manager_.AuthenticationRequestFinished();
   ScheduleTask(milliseconds(5), [this, current_peer]() {
-    send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
-        ErrorCode::AUTHENTICATION_FAILURE, current_peer));
+    if (properties_.IsUnmasked(EventCode::SIMPLE_PAIRING_COMPLETE)) {
+      send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
+          ErrorCode::AUTHENTICATION_FAILURE, current_peer));
+    }
   });
   if (peer != current_peer) {
     return ErrorCode::UNKNOWN_CONNECTION;
@@ -2016,8 +2133,10 @@
   auto current_peer = security_manager_.GetAuthenticationAddress();
   security_manager_.AuthenticationRequestFinished();
   ScheduleTask(milliseconds(5), [this, current_peer]() {
-    send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
-        ErrorCode::AUTHENTICATION_FAILURE, current_peer));
+    if (properties_.IsUnmasked(EventCode::SIMPLE_PAIRING_COMPLETE)) {
+      send_event_(bluetooth::hci::SimplePairingCompleteBuilder::Create(
+          ErrorCode::AUTHENTICATION_FAILURE, current_peer));
+    }
   });
   if (peer != current_peer) {
     return ErrorCode::UNKNOWN_CONNECTION;
@@ -2058,7 +2177,9 @@
                                                       uint16_t handle) {
   security_manager_.AuthenticationRequest(address, handle, true);
   auto packet = bluetooth::hci::LinkKeyRequestBuilder::Create(address);
-  send_event_(std::move(packet));
+  if (properties_.IsUnmasked(EventCode::LINK_KEY_REQUEST)) {
+    send_event_(std::move(packet));
+  }
 }
 
 ErrorCode LinkLayerController::AuthenticationRequested(uint16_t handle) {
@@ -2084,7 +2205,9 @@
     auto packet = bluetooth::hci::EncryptionChangeBuilder::Create(
         ErrorCode::SUCCESS, handle,
         static_cast<bluetooth::hci::EncryptionEnabled>(encryption_enable));
-    send_event_(std::move(packet));
+    if (properties_.IsUnmasked(EventCode::ENCRYPTION_CHANGE)) {
+      send_event_(std::move(packet));
+    }
     return;
   }
 
@@ -2156,7 +2279,9 @@
   auto packet = bluetooth::hci::ConnectionCompleteBuilder::Create(
       ErrorCode::SUCCESS, handle, addr, bluetooth::hci::LinkType::ACL,
       bluetooth::hci::Enable::DISABLED);
-  send_event_(std::move(packet));
+  if (properties_.IsUnmasked(EventCode::CONNECTION_COMPLETE)) {
+    send_event_(std::move(packet));
+  }
 }
 
 ErrorCode LinkLayerController::RejectConnectionRequest(const Address& addr,
@@ -2184,7 +2309,9 @@
   auto packet = bluetooth::hci::ConnectionCompleteBuilder::Create(
       static_cast<ErrorCode>(reason), 0xeff, addr,
       bluetooth::hci::LinkType::ACL, bluetooth::hci::Enable::DISABLED);
-  send_event_(std::move(packet));
+  if (properties_.IsUnmasked(EventCode::CONNECTION_COMPLETE)) {
+    send_event_(std::move(packet));
+  }
 }
 
 ErrorCode LinkLayerController::CreateConnection(const Address& addr, uint16_t,
@@ -2233,7 +2360,9 @@
   // TODO: Clean up other connection state.
   auto packet = bluetooth::hci::DisconnectionCompleteBuilder::Create(
       ErrorCode::SUCCESS, handle, static_cast<ErrorCode>(reason));
-  send_event_(std::move(packet));
+  if (properties_.IsUnmasked(EventCode::DISCONNECTION_COMPLETE)) {
+    send_event_(std::move(packet));
+  }
 }
 
 ErrorCode LinkLayerController::ChangeConnectionPacketType(uint16_t handle,
@@ -2246,7 +2375,9 @@
   std::shared_ptr<bluetooth::hci::ConnectionPacketTypeChangedBuilder>
       shared_packet = std::move(packet);
   ScheduleTask(milliseconds(20), [this, shared_packet]() {
-    send_event_(std::move(shared_packet));
+    if (properties_.IsUnmasked(EventCode::CONNECTION_PACKET_TYPE_CHANGED)) {
+      send_event_(std::move(shared_packet));
+    }
   });
 
   return ErrorCode::SUCCESS;
@@ -2559,8 +2690,10 @@
     status = ErrorCode::INVALID_HCI_COMMAND_PARAMETERS;
   }
   uint16_t interval = (interval_min + interval_max) / 2;
-  send_event_(bluetooth::hci::LeConnectionUpdateCompleteBuilder::Create(
-      status, handle, interval, latency, supervision_timeout));
+  if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
+    send_event_(bluetooth::hci::LeConnectionUpdateCompleteBuilder::Create(
+        status, handle, interval, latency, supervision_timeout));
+  }
 }
 
 ErrorCode LinkLayerController::LeConnectionUpdate(
@@ -2654,10 +2787,12 @@
     uint16_t max_transport_latency_m_to_s,
     uint16_t max_transport_latency_s_to_m,
     std::vector<bluetooth::hci::CisParametersConfig> cis_config) {
-  send_event_(connections_.SetCigParameters(
-      cig_id, sdu_interval_m_to_s, sdu_interval_s_to_m, clock_accuracy, packing,
-      framing, max_transport_latency_m_to_s, max_transport_latency_s_to_m,
-      cis_config));
+  if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
+    send_event_(connections_.SetCigParameters(
+        cig_id, sdu_interval_m_to_s, sdu_interval_s_to_m, clock_accuracy,
+        packing, framing, max_transport_latency_m_to_s,
+        max_transport_latency_s_to_m, cis_config));
+  }
 }
 
 ErrorCode LinkLayerController::LeCreateCis(
@@ -2730,12 +2865,14 @@
   uint8_t max_pdu_m_to_s = 0x40;
   uint8_t max_pdu_s_to_m = 0x40;
   uint16_t iso_interval = 0x100;
-  send_event_(bluetooth::hci::LeCisEstablishedBuilder::Create(
-      ErrorCode::SUCCESS, cis_handle, cig_sync_delay, cis_sync_delay,
-      latency_m_to_s, latency_s_to_m,
-      bluetooth::hci::SecondaryPhyType::NO_PACKETS,
-      bluetooth::hci::SecondaryPhyType::NO_PACKETS, nse, bn_m_to_s, bn_s_to_m,
-      ft_m_to_s, ft_s_to_m, max_pdu_m_to_s, max_pdu_s_to_m, iso_interval));
+  if (properties_.IsUnmasked(EventCode::LE_META_EVENT)) {
+    send_event_(bluetooth::hci::LeCisEstablishedBuilder::Create(
+        ErrorCode::SUCCESS, cis_handle, cig_sync_delay, cis_sync_delay,
+        latency_m_to_s, latency_s_to_m,
+        bluetooth::hci::SecondaryPhyType::NO_PACKETS,
+        bluetooth::hci::SecondaryPhyType::NO_PACKETS, nse, bn_m_to_s, bn_s_to_m,
+        ft_m_to_s, ft_s_to_m, max_pdu_m_to_s, max_pdu_s_to_m, iso_interval));
+  }
   return ErrorCode::SUCCESS;
 }
 
@@ -2828,12 +2965,16 @@
 
   // TODO: Check keys
   if (connections_.IsEncrypted(handle)) {
-    send_event_(bluetooth::hci::EncryptionKeyRefreshCompleteBuilder::Create(
-        ErrorCode::SUCCESS, handle));
+    if (properties_.IsUnmasked(EventCode::ENCRYPTION_KEY_REFRESH_COMPLETE)) {
+      send_event_(bluetooth::hci::EncryptionKeyRefreshCompleteBuilder::Create(
+          ErrorCode::SUCCESS, handle));
+    }
   } else {
     connections_.Encrypt(handle);
-    send_event_(bluetooth::hci::EncryptionChangeBuilder::Create(
-        ErrorCode::SUCCESS, handle, bluetooth::hci::EncryptionEnabled::ON));
+    if (properties_.IsUnmasked(EventCode::ENCRYPTION_CHANGE)) {
+      send_event_(bluetooth::hci::EncryptionChangeBuilder::Create(
+          ErrorCode::SUCCESS, handle, bluetooth::hci::EncryptionEnabled::ON));
+    }
   }
   SendLeLinkLayerPacket(
       model::packets::LeEncryptConnectionResponseBuilder::Create(
@@ -3039,7 +3180,9 @@
     inquiry_timer_task_id_ = kInvalidTaskId;
     auto packet =
         bluetooth::hci::InquiryCompleteBuilder::Create(ErrorCode::SUCCESS);
-    send_event_(std::move(packet));
+    if (properties_.IsUnmasked(EventCode::INQUIRY_COMPLETE)) {
+      send_event_(std::move(packet));
+    }
   }
 }
 
diff --git a/system/vendor_libs/test_vendor_lib/model/devices/device_properties.h b/system/vendor_libs/test_vendor_lib/model/devices/device_properties.h
index 968f743..45419fb 100644
--- a/system/vendor_libs/test_vendor_lib/model/devices/device_properties.h
+++ b/system/vendor_libs/test_vendor_lib/model/devices/device_properties.h
@@ -30,6 +30,7 @@
 
 using ::bluetooth::hci::Address;
 using ::bluetooth::hci::ClassOfDevice;
+using ::bluetooth::hci::EventCode;
 
 class DeviceProperties {
  public:
@@ -229,6 +230,11 @@
 
   void SetEventMask(uint64_t mask) { event_mask_ = mask; }
 
+  bool IsUnmasked(EventCode event) const {
+    uint64_t bit = UINT64_C(1) << (static_cast<uint8_t>(event) - 1);
+    return (event_mask_ & bit) != 0;
+  }
+
   // Low-Energy functions
   const Address& GetLeAddress() const {
     return le_address_;