mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 06:33:51 +00:00 
			
		
		
		
	[esp32_ble_server] Refactor property setters to reduce code duplication (#11071)
This commit is contained in:
		| @@ -147,47 +147,27 @@ bool BLECharacteristic::is_failed() { | ||||
|   return this->state_ == FAILED; | ||||
| } | ||||
|  | ||||
| void BLECharacteristic::set_broadcast_property(bool value) { | ||||
| void BLECharacteristic::set_property_bit_(esp_gatt_char_prop_t bit, bool value) { | ||||
|   if (value) { | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ | ESP_GATT_CHAR_PROP_BIT_BROADCAST); | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ | bit); | ||||
|   } else { | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ & ~ESP_GATT_CHAR_PROP_BIT_BROADCAST); | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ & ~bit); | ||||
|   } | ||||
| } | ||||
|  | ||||
| void BLECharacteristic::set_broadcast_property(bool value) { | ||||
|   this->set_property_bit_(ESP_GATT_CHAR_PROP_BIT_BROADCAST, value); | ||||
| } | ||||
| void BLECharacteristic::set_indicate_property(bool value) { | ||||
|   if (value) { | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ | ESP_GATT_CHAR_PROP_BIT_INDICATE); | ||||
|   } else { | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ & ~ESP_GATT_CHAR_PROP_BIT_INDICATE); | ||||
|   } | ||||
|   this->set_property_bit_(ESP_GATT_CHAR_PROP_BIT_INDICATE, value); | ||||
| } | ||||
| void BLECharacteristic::set_notify_property(bool value) { | ||||
|   if (value) { | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ | ESP_GATT_CHAR_PROP_BIT_NOTIFY); | ||||
|   } else { | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ & ~ESP_GATT_CHAR_PROP_BIT_NOTIFY); | ||||
|   } | ||||
| } | ||||
| void BLECharacteristic::set_read_property(bool value) { | ||||
|   if (value) { | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ | ESP_GATT_CHAR_PROP_BIT_READ); | ||||
|   } else { | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ & ~ESP_GATT_CHAR_PROP_BIT_READ); | ||||
|   } | ||||
| } | ||||
| void BLECharacteristic::set_write_property(bool value) { | ||||
|   if (value) { | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ | ESP_GATT_CHAR_PROP_BIT_WRITE); | ||||
|   } else { | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ & ~ESP_GATT_CHAR_PROP_BIT_WRITE); | ||||
|   } | ||||
|   this->set_property_bit_(ESP_GATT_CHAR_PROP_BIT_NOTIFY, value); | ||||
| } | ||||
| void BLECharacteristic::set_read_property(bool value) { this->set_property_bit_(ESP_GATT_CHAR_PROP_BIT_READ, value); } | ||||
| void BLECharacteristic::set_write_property(bool value) { this->set_property_bit_(ESP_GATT_CHAR_PROP_BIT_WRITE, value); } | ||||
| void BLECharacteristic::set_write_no_response_property(bool value) { | ||||
|   if (value) { | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ | ESP_GATT_CHAR_PROP_BIT_WRITE_NR); | ||||
|   } else { | ||||
|     this->properties_ = (esp_gatt_char_prop_t) (this->properties_ & ~ESP_GATT_CHAR_PROP_BIT_WRITE_NR); | ||||
|   } | ||||
|   this->set_property_bit_(ESP_GATT_CHAR_PROP_BIT_WRITE_NR, value); | ||||
| } | ||||
|  | ||||
| void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, | ||||
|   | ||||
| @@ -97,6 +97,8 @@ class BLECharacteristic { | ||||
|   void remove_client_from_notify_list_(uint16_t conn_id); | ||||
|   ClientNotificationEntry *find_client_in_notify_list_(uint16_t conn_id); | ||||
|  | ||||
|   void set_property_bit_(esp_gatt_char_prop_t bit, bool value); | ||||
|  | ||||
|   std::unique_ptr<std::function<void(std::span<const uint8_t>, uint16_t)>> on_write_callback_; | ||||
|   std::unique_ptr<std::function<void(uint16_t)>> on_read_callback_; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user