try add support mac

This commit is contained in:
Maxim Slipenko 2023-12-02 16:12:58 +03:00
parent 029df3d807
commit c8d2c86908
3 changed files with 36 additions and 14 deletions

View File

@ -67,11 +67,13 @@ CONFIG_SCHEMA = cv.Schema(
).extend(cv.COMPONENT_SCHEMA)
CONF_IRK = "irk"
CONF_MAC = "mac"
NIMBLE_DEVICE_LISTENER_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_NIMBLE_ID): cv.use_id(NimbleTracker),
cv.Optional(CONF_IRK): cv.string,
cv.Optional(CONF_MAC): cv.string,
},
cv.has_exactly_one_key(CONF_IRK),
)
@ -108,3 +110,5 @@ async def register_ble_device(var, config):
async def device_listener_to_code(var, config):
if CONF_IRK in config:
cg.add(var.set_irk(config[CONF_IRK]))
if CONF_MAC in config:
cg.add(var.set_irk(config[CONF_MAC]))

View File

@ -19,24 +19,39 @@ namespace esphome
}
}
void NimbleDeviceListener::set_address(std::string address) {
this->match_by_ = MATCH_BY_ADDRESS;
this->address_ = address;
}
bool NimbleDeviceListener::parse_event(NimbleTrackerEvent *tracker_event)
{
if (tracker_event->getAddressType() != BLE_ADDR_RANDOM)
{
return false;
}
if (this->match_by_ == MATCH_BY_IRK) {
if (tracker_event->getAddressType() != BLE_ADDR_RANDOM)
{
return false;
}
auto address = tracker_event->getAddress();
auto naddress = address.getNative();
auto address = tracker_event->getAddress();
auto naddress = address.getNative();
if (ble_ll_resolv_rpa(naddress, this->irk_))
{
ESP_LOGD(TAG, "Found device %s", tracker_event->toString().c_str());
return this->update_state(tracker_event);
}
else
{
return false;
if (ble_ll_resolv_rpa(naddress, this->irk_))
{
ESP_LOGD(TAG, "Found device %s", tracker_event->toString().c_str());
return this->update_state(tracker_event);
}
else
{
return false;
}
} else if (this->match_by_ == MATCH_BY_ADDRESS) {
auto address = tracker_event->getAddress();
if (this.address_ == address) {
return this->update_state(tracker_event);
} else {
return false;
}
}
};
} // namespace nimble_tracker

View File

@ -14,6 +14,7 @@ namespace esphome
public:
bool parse_event(NimbleTrackerEvent *tracker_event);
void set_irk(std::string irk_hex);
void set_address(std::stding address);
protected:
virtual bool update_state(NimbleTrackerEvent *tracker_event) = 0;
@ -21,10 +22,12 @@ namespace esphome
enum MatchType
{
MATCH_BY_IRK,
MATCH_BY_ADDRESS;
};
MatchType match_by_;
uint8_t *irk_;
std::string address_;
};
} // namespace nimble_tracker