Skip to content

Commit

Permalink
Merge pull request #102 from fsaris/dev
Browse files Browse the repository at this point in the history
Prevent nullprt errors and remove deprecated color_mode from config message
  • Loading branch information
fsaris authored Oct 5, 2024
2 parents 2c39ca3 + 8900d8f commit cc49b06
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
15 changes: 13 additions & 2 deletions components/awox_mesh/awox_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ FoundDevice *AwoxMesh::add_to_found_devices(const esp32_ble_tracker::ESPBTDevice

bool AwoxMesh::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
if (device.address_str().rfind(this->address_prefix, 0) != 0) {
ESP_LOGD(TAG, "Skipped device %s - %s. RSSI: %d, address_prefix mismatch", device.get_name().c_str(),
device.address_str().c_str(), device.get_rssi());
return false;
}

if (!this->mac_addresses_allowed(device.address_uint64())) {
ESP_LOGD(TAG, "Skipped device %s - %s. RSSI: %d, not in mac_addresses_allowed", device.get_name().c_str(),
device.address_str().c_str(), device.get_rssi());
return false;
}

Expand Down Expand Up @@ -174,6 +178,10 @@ void AwoxMesh::loop() {
this->send_group_discovery(group);
}
}

if (now - this->last_found_device_cleanup < 20000) {
this->set_rssi_for_devices_that_are_not_available();
}
}

void AwoxMesh::disconnect_connections_with_overlapping_mesh_ids() {
Expand Down Expand Up @@ -274,9 +282,12 @@ FoundDevice *AwoxMesh::next_to_connect() {
}

void AwoxMesh::set_rssi_for_devices_that_are_not_available() {
const uint32_t now = esphome::millis();
this->last_found_device_cleanup = esphome::millis();
for (auto *found_device : this->found_devices_) {
if (now - found_device->last_detected > 20000) {
if (found_device->rssi > RSSI_NOT_AVAILABLE &&
this->last_found_device_cleanup - found_device->last_detected > 20000) {
ESP_LOGD(TAG, "Clear RSSI for %s [%d] not found the last 20 seconds", found_device->device.address_str().c_str(),
found_device->mesh_id);
found_device->rssi = RSSI_NOT_AVAILABLE;
}
}
Expand Down
2 changes: 2 additions & 0 deletions components/awox_mesh/awox_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class AwoxMesh : public esp32_ble_tracker::ESPBTDeviceListener, public Component

uint32_t last_connection_attempt = 0;

uint32_t last_found_device_cleanup = 0;

int minimum_rssi = -90;

std::string mesh_name = "";
Expand Down
2 changes: 0 additions & 2 deletions components/awox_mesh/awox_mesh_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,6 @@ void AwoxMeshMqtt::send_discovery(Device *device) {
root[MQTT_AVAILABILITY_MODE] = "all";

// Features
root[MQTT_COLOR_MODE] = true;

if (device->device_info->has_feature(FEATURE_WHITE_BRIGHTNESS) ||
device->device_info->has_feature(FEATURE_COLOR_BRIGHTNESS)) {
root["brightness"] = true;
Expand Down
30 changes: 29 additions & 1 deletion components/awox_mesh/mesh_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static int get_product_code(unsigned char part1, unsigned char part2) { return i

void MeshConnection::connect_to(FoundDevice *found_device) {
this->set_address(found_device->device.address_uint64());
this->set_state(esp32_ble_tracker::ClientState::IDLE);
this->set_state(esp32_ble_tracker::ClientState::SEARCHING);
this->found_device = found_device;
this->found_device->connected = true;

Expand Down Expand Up @@ -216,6 +216,27 @@ void MeshConnection::setup_connection() {
this->pair_char = this->get_characteristic(esp32_ble_tracker::ESPBTUUID::from_raw(uuid_info_service),
esp32_ble_tracker::ESPBTUUID::from_raw(uuid_pair_char));

if (this->notification_char == nullptr) {
ESP_LOGW(TAG, "[%d] [%s] No BLE Notification character found", this->get_conn_id(), this->address_str_.c_str());
this->disconnect();
this->set_address(0);
return;
}

if (this->command_char == nullptr) {
ESP_LOGW(TAG, "[%d] [%s] No BLE Command character found", this->get_conn_id(), this->address_str_.c_str());
this->disconnect();
this->set_address(0);
return;
}

if (this->pair_char == nullptr) {
ESP_LOGW(TAG, "[%d] [%s] No BLE Pair character found", this->get_conn_id(), this->address_str_.c_str());
this->disconnect();
this->set_address(0);
return;
}

unsigned char key[8];
esp_fill_random(key, 8);
this->random_key = std::string((char *) key).substr(0, 8);
Expand All @@ -229,6 +250,9 @@ void MeshConnection::setup_connection() {
if (status != ESP_OK) {
ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_read_char failed, error=%d", this->get_conn_id(), this->address_str_.c_str(),
status);
this->disconnect();
this->set_address(0);
return;
}

ESP_LOGD(TAG, "Listen for notifications");
Expand All @@ -237,7 +261,11 @@ void MeshConnection::setup_connection() {
if (status) {
ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_register_for_notify failed, status=%d", this->get_conn_id(),
this->address_str_.c_str(), status);
this->disconnect();
this->set_address(0);
return;
}

ESP_LOGD(TAG, "Enable notifications");
uint16_t notify_en = 1;
this->notification_char->write_value((uint8_t *) &notify_en, sizeof(notify_en));
Expand Down
23 changes: 13 additions & 10 deletions components/awox_mesh/mesh_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,48 +103,51 @@ class MeshConnection : public esp32_ble_client::BLEClientBase {

virtual void set_state(esp32_ble_tracker::ClientState st) override {
this->state_ = st;
std::string state = "";
switch (st) {
case esp32_ble_tracker::ClientState::INIT:

ESP_LOGI("awox.connection", "INIT");
state = "INIT";
break;
case esp32_ble_tracker::ClientState::DISCONNECTING:

ESP_LOGI("awox.connection", "DISCONNECTING");
state = "DISCONNECTING";
break;
case esp32_ble_tracker::ClientState::IDLE:

ESP_LOGI("awox.connection", "IDLE");
state = "IDLE";
break;
case esp32_ble_tracker::ClientState::SEARCHING:

ESP_LOGI("awox.connection", "SEARCHING");
state = "SEARCHING";
break;
case esp32_ble_tracker::ClientState::DISCOVERED:

ESP_LOGI("awox.connection", "DISCOVERED");
state = "DISCOVERED";
break;
case esp32_ble_tracker::ClientState::READY_TO_CONNECT:

ESP_LOGI("awox.connection", "READY_TO_CONNECT");
state = "READY_TO_CONNECT";
break;
case esp32_ble_tracker::ClientState::CONNECTING:

ESP_LOGI("awox.connection", "CONNECTING");
state = "CONNECTING";
break;
case esp32_ble_tracker::ClientState::CONNECTED:

ESP_LOGI("awox.connection", "CONNECTED");
state = "CONNECTED";
break;
case esp32_ble_tracker::ClientState::ESTABLISHED:

ESP_LOGI("awox.connection", "ESTABLISHED");
state = "ESTABLISHED";
break;

default:
ESP_LOGI("awox.connection", "Unknown state");
state = "Unknown state";
break;
}

ESP_LOGI("awox.connection", "[%d] set_state %s", this->connection_index_, state.c_str());
}

public:
Expand Down

0 comments on commit cc49b06

Please sign in to comment.