mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2026-04-07 05:37:54 +03:00
style: apply clang-format and enforce left pointer alignment
This commit is contained in:
@@ -14,37 +14,33 @@ bool sys_net_config_callback(pb_istream_t* istream, pb_ostream_t* ostream, const
|
||||
WifiList** managerPtr = static_cast<WifiList**>(field->pData);
|
||||
WifiList* manager = *managerPtr;
|
||||
|
||||
if (istream != NULL && (field->tag == sys_net_config_credentials_tag || field->tag == sys_status_wifi_scan_result_tag)) {
|
||||
if (manager == nullptr) {
|
||||
if(istream != NULL && (field->tag == sys_net_config_credentials_tag || field->tag == sys_status_wifi_scan_result_tag)) {
|
||||
if(manager == nullptr) {
|
||||
ESP_LOGE(TAG_CRED_MANAGER, "Invalid pointer to wifi list manager");
|
||||
return false;
|
||||
}
|
||||
ESP_LOGV(TAG_CRED_MANAGER, "Decoding credentials");
|
||||
sys_net_wifi_entry entry = sys_net_wifi_entry_init_default;
|
||||
if (!pb_decode(istream, &sys_net_wifi_entry_msg, &entry)) return false;
|
||||
if(!pb_decode(istream, &sys_net_wifi_entry_msg, &entry)) return false;
|
||||
printf("\nFound ssid %s, password %s\n", entry.ssid, entry.password);
|
||||
try {
|
||||
manager->AddUpdate(entry); // Add to the manager
|
||||
} catch (const std::exception& e) {
|
||||
} catch(const std::exception& e) {
|
||||
ESP_LOGE(TAG_CRED_MANAGER, "decode exception: %s", e.what());
|
||||
return false;
|
||||
}
|
||||
ESP_LOGV(TAG_CRED_MANAGER, "Credentials decoding completed");
|
||||
} else if (ostream != NULL && (field->tag == sys_net_config_credentials_tag || field->tag == sys_status_wifi_scan_result_tag)) {
|
||||
if (manager == nullptr) {
|
||||
} else if(ostream != NULL && (field->tag == sys_net_config_credentials_tag || field->tag == sys_status_wifi_scan_result_tag)) {
|
||||
if(manager == nullptr) {
|
||||
ESP_LOGV(TAG_CRED_MANAGER, "No wifi entries manager instance. nothing to encode");
|
||||
return true;
|
||||
}
|
||||
ESP_LOGV(TAG_CRED_MANAGER, "Encoding %d access points", manager->GetCount());
|
||||
|
||||
for (int i = 0; i < manager->GetCount(); i++) {
|
||||
for(int i = 0; i < manager->GetCount(); i++) {
|
||||
ESP_LOGV(TAG_CRED_MANAGER, "Encoding credential #%d: SSID: %s, PASS: %s", i, manager->GetIndex(i)->ssid, manager->GetIndex(i)->password);
|
||||
if (!pb_encode_tag_for_field(ostream, field)) {
|
||||
return false;
|
||||
}
|
||||
if (!pb_encode_submessage(ostream, &sys_net_wifi_entry_msg, manager->GetIndex(i))) {
|
||||
return false;
|
||||
}
|
||||
if(!pb_encode_tag_for_field(ostream, field)) { return false; }
|
||||
if(!pb_encode_submessage(ostream, &sys_net_wifi_entry_msg, manager->GetIndex(i))) { return false; }
|
||||
}
|
||||
ESP_LOGV(TAG_CRED_MANAGER, "Credentials encoding completed");
|
||||
}
|
||||
@@ -52,7 +48,7 @@ bool sys_net_config_callback(pb_istream_t* istream, pb_ostream_t* ostream, const
|
||||
return true;
|
||||
}
|
||||
std::string WifiList::GetBSSID(const wifi_event_sta_connected_t* evt) {
|
||||
char buffer[18]={};
|
||||
char buffer[18] = {};
|
||||
FormatBSSID(buffer, sizeof(buffer), evt->bssid);
|
||||
ESP_LOGD(TAG_CRED_MANAGER, "Formatted BSSID: %s", buffer);
|
||||
return std::string(buffer);
|
||||
@@ -63,11 +59,11 @@ bool WifiList::OffsetTimeStamp(google_protobuf_Timestamp* ts) {
|
||||
gettimeofday((struct timeval*)&tts, NULL);
|
||||
gts.nanos = tts.tv_usec * 1000;
|
||||
gts.seconds = tts.tv_sec;
|
||||
if (tts.tv_sec < 1704143717) {
|
||||
if(tts.tv_sec < 1704143717) {
|
||||
ESP_LOGE(TAG_CRED_MANAGER, "Error updating time stamp. Clock doesn't seem right");
|
||||
return false;
|
||||
}
|
||||
if (ts && ts->seconds < 1704143717) {
|
||||
if(ts && ts->seconds < 1704143717) {
|
||||
ESP_LOGV(TAG_CRED_MANAGER, "Updating time stamp based on new clock value");
|
||||
ts->seconds = gts.seconds - ts->seconds;
|
||||
ts->nanos = gts.nanos - ts->nanos;
|
||||
@@ -84,7 +80,7 @@ bool WifiList::UpdateTimeStamp(google_protobuf_Timestamp* ts, bool& has_flag_val
|
||||
gettimeofday((struct timeval*)&tts, NULL);
|
||||
gts.nanos = tts.tv_usec * 1000;
|
||||
gts.seconds = tts.tv_sec;
|
||||
if (!has_flag_val || gts.nanos != ts->nanos || gts.seconds != ts->seconds) {
|
||||
if(!has_flag_val || gts.nanos != ts->nanos || gts.seconds != ts->seconds) {
|
||||
ts->seconds = gts.seconds;
|
||||
ts->nanos = gts.nanos;
|
||||
has_flag_val = true;
|
||||
@@ -94,22 +90,16 @@ bool WifiList::UpdateTimeStamp(google_protobuf_Timestamp* ts, bool& has_flag_val
|
||||
}
|
||||
|
||||
bool WifiList::isEmpty(const char* str, size_t len) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
if (str[i] != '\0') {
|
||||
return false;
|
||||
}
|
||||
for(size_t i = 0; i < len; ++i) {
|
||||
if(str[i] != '\0') { return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiList::Update(const wifi_ap_record_t* ap, bool connected) {
|
||||
if (!Lock()) {
|
||||
throw std::runtime_error("Lock failed");
|
||||
}
|
||||
if(!Lock()) { throw std::runtime_error("Lock failed"); }
|
||||
auto existing = Get(ap);
|
||||
if (!existing) {
|
||||
return false;
|
||||
}
|
||||
if(!existing) { return false; }
|
||||
auto updated = ToSTAEntry(ap);
|
||||
updated.connected = connected;
|
||||
bool changed = Update(*existing, updated);
|
||||
@@ -121,39 +111,39 @@ bool WifiList::Update(sys_net_wifi_entry& existingEntry, sys_net_wifi_entry& upd
|
||||
|
||||
// Check if any relevant fields have changed
|
||||
bool hasChanged = false;
|
||||
if (!isEmpty(updated.ssid, sizeof(updated.ssid)) && memcmp(existingEntry.ssid, updated.ssid, sizeof(existingEntry.ssid)) != 0) {
|
||||
if(!isEmpty(updated.ssid, sizeof(updated.ssid)) && memcmp(existingEntry.ssid, updated.ssid, sizeof(existingEntry.ssid)) != 0) {
|
||||
memcpy(existingEntry.ssid, updated.ssid, sizeof(existingEntry.ssid));
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
// Check and copy BSSID if the compared BSSID is not empty
|
||||
if (!isEmpty(updated.bssid, sizeof(updated.bssid)) && strcmp(updated.bssid, "00:00:00:00:00:00") != 0 &&
|
||||
if(!isEmpty(updated.bssid, sizeof(updated.bssid)) && strcmp(updated.bssid, "00:00:00:00:00:00") != 0 &&
|
||||
memcmp(existingEntry.bssid, updated.bssid, sizeof(existingEntry.bssid)) != 0) {
|
||||
memcpy(existingEntry.bssid, updated.bssid, sizeof(existingEntry.bssid));
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
// Check and copy password if the compared password is not empty
|
||||
if (!isEmpty(updated.password, sizeof(updated.password)) &&
|
||||
if(!isEmpty(updated.password, sizeof(updated.password)) &&
|
||||
memcmp(existingEntry.password, updated.password, sizeof(existingEntry.password)) != 0) {
|
||||
memcpy(existingEntry.password, updated.password, sizeof(existingEntry.password));
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (existingEntry.channel != updated.channel && updated.channel > 0) {
|
||||
if(existingEntry.channel != updated.channel && updated.channel > 0) {
|
||||
existingEntry.channel = updated.channel;
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (existingEntry.auth_type != updated.auth_type && updated.auth_type != sys_net_auth_types_AUTH_UNKNOWN) {
|
||||
if(existingEntry.auth_type != updated.auth_type && updated.auth_type != sys_net_auth_types_AUTH_UNKNOWN) {
|
||||
existingEntry.auth_type = updated.auth_type;
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (areRadioTypesDifferent(existingEntry.radio_type, existingEntry.radio_type_count, updated.radio_type, updated.radio_type_count) &&
|
||||
if(areRadioTypesDifferent(existingEntry.radio_type, existingEntry.radio_type_count, updated.radio_type, updated.radio_type_count) &&
|
||||
updated.radio_type_count > 0 && updated.radio_type[0] != sys_net_radio_types_UNKNOWN) {
|
||||
|
||||
if (existingEntry.radio_type != nullptr) {
|
||||
if(existingEntry.radio_type != nullptr) {
|
||||
// Free the old radio_type array if it exists
|
||||
delete[] existingEntry.radio_type;
|
||||
}
|
||||
@@ -164,33 +154,33 @@ bool WifiList::Update(sys_net_wifi_entry& existingEntry, sys_net_wifi_entry& upd
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (updated.has_last_try) {
|
||||
if (memcmp(&existingEntry.last_try, &updated.last_try, sizeof(existingEntry.last_try)) != 0) {
|
||||
if(updated.has_last_try) {
|
||||
if(memcmp(&existingEntry.last_try, &updated.last_try, sizeof(existingEntry.last_try)) != 0) {
|
||||
memcpy(&existingEntry.last_try, &updated.last_try, sizeof(existingEntry.last_try));
|
||||
hasChanged = true;
|
||||
}
|
||||
}
|
||||
if (updated.has_last_seen) {
|
||||
if (memcmp(&existingEntry.last_seen, &updated.last_seen, sizeof(existingEntry.last_seen)) != 0) {
|
||||
if(updated.has_last_seen) {
|
||||
if(memcmp(&existingEntry.last_seen, &updated.last_seen, sizeof(existingEntry.last_seen)) != 0) {
|
||||
memcpy(&existingEntry.last_seen, &updated.last_seen, sizeof(existingEntry.last_seen));
|
||||
hasChanged = true;
|
||||
}
|
||||
}
|
||||
if (existingEntry.has_last_seen != updated.has_last_seen && updated.has_last_seen) {
|
||||
if(existingEntry.has_last_seen != updated.has_last_seen && updated.has_last_seen) {
|
||||
existingEntry.has_last_seen = updated.has_last_seen;
|
||||
hasChanged = true;
|
||||
}
|
||||
if (existingEntry.has_last_try != updated.has_last_try && updated.has_last_try) {
|
||||
if(existingEntry.has_last_try != updated.has_last_try && updated.has_last_try) {
|
||||
existingEntry.has_last_try = updated.has_last_try;
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (existingEntry.connected != updated.connected && updated.connected) {
|
||||
if(existingEntry.connected != updated.connected && updated.connected) {
|
||||
existingEntry.connected = updated.connected;
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (existingEntry.rssi != updated.rssi && updated.rssi != 0) {
|
||||
if(existingEntry.rssi != updated.rssi && updated.rssi != 0) {
|
||||
existingEntry.rssi = updated.rssi;
|
||||
hasChanged = true;
|
||||
}
|
||||
@@ -201,8 +191,8 @@ bool WifiList::Update(sys_net_wifi_entry& existingEntry, sys_net_wifi_entry& upd
|
||||
std::string WifiList::formatRadioTypes(const sys_net_radio_types* radioTypes, pb_size_t count) {
|
||||
std::string result;
|
||||
|
||||
for (pb_size_t i = 0; i < count; ++i) {
|
||||
switch (radioTypes[i]) {
|
||||
for(pb_size_t i = 0; i < count; ++i) {
|
||||
switch(radioTypes[i]) {
|
||||
case sys_net_radio_types_PHY_11B:
|
||||
result += "B";
|
||||
break;
|
||||
@@ -229,25 +219,21 @@ std::string WifiList::formatRadioTypes(const sys_net_radio_types* radioTypes, pb
|
||||
result += "U";
|
||||
break;
|
||||
}
|
||||
if (i < count - 1) {
|
||||
result += ",";
|
||||
}
|
||||
if(i < count - 1) { result += ","; }
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool WifiList::Update(const wifi_sta_config_t* sta, bool connected) {
|
||||
if (!sta) {
|
||||
if(!sta) {
|
||||
return false; // Invalid input
|
||||
}
|
||||
if (!Lock()) {
|
||||
throw std::runtime_error("Lock failed");
|
||||
}
|
||||
if(!Lock()) { throw std::runtime_error("Lock failed"); }
|
||||
sys_net_wifi_entry* existingEntry = Get(sta);
|
||||
|
||||
// If the entry does not exist, nothing to update
|
||||
if (!existingEntry) {
|
||||
if(!existingEntry) {
|
||||
Unlock();
|
||||
return false;
|
||||
}
|
||||
@@ -255,28 +241,28 @@ bool WifiList::Update(const wifi_sta_config_t* sta, bool connected) {
|
||||
// Check if any relevant fields have changed
|
||||
bool hasChanged = false;
|
||||
|
||||
if (strlen(updated.ssid) > 0 && memcmp(existingEntry->ssid, updated.ssid, sizeof(existingEntry->ssid)) != 0) {
|
||||
if(strlen(updated.ssid) > 0 && memcmp(existingEntry->ssid, updated.ssid, sizeof(existingEntry->ssid)) != 0) {
|
||||
memcpy(existingEntry->ssid, updated.ssid, sizeof(existingEntry->ssid));
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (strlen(updated.bssid) > 0 && strcmp(updated.bssid, "00:00:00:00:00:00") != 0 &&
|
||||
if(strlen(updated.bssid) > 0 && strcmp(updated.bssid, "00:00:00:00:00:00") != 0 &&
|
||||
memcmp(existingEntry->bssid, updated.bssid, sizeof(existingEntry->bssid)) != 0) {
|
||||
memcpy(existingEntry->bssid, updated.bssid, sizeof(existingEntry->bssid));
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (existingEntry->channel != updated.channel) {
|
||||
if(existingEntry->channel != updated.channel) {
|
||||
existingEntry->channel = updated.channel;
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (existingEntry->auth_type != updated.auth_type && updated.auth_type != sys_net_auth_types_AUTH_UNKNOWN) {
|
||||
if(existingEntry->auth_type != updated.auth_type && updated.auth_type != sys_net_auth_types_AUTH_UNKNOWN) {
|
||||
existingEntry->auth_type = updated.auth_type;
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (areRadioTypesDifferent(existingEntry->radio_type, existingEntry->radio_type_count, updated.radio_type, updated.radio_type_count) &&
|
||||
if(areRadioTypesDifferent(existingEntry->radio_type, existingEntry->radio_type_count, updated.radio_type, updated.radio_type_count) &&
|
||||
updated.radio_type_count > 0 && updated.radio_type[0] != sys_net_radio_types_UNKNOWN) {
|
||||
// Free the old radio_type array if it exists
|
||||
delete[] existingEntry->radio_type;
|
||||
@@ -289,43 +275,43 @@ bool WifiList::Update(const wifi_sta_config_t* sta, bool connected) {
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (updated.has_last_try) {
|
||||
if (memcmp(&existingEntry->last_try, &updated.last_try, sizeof(existingEntry->last_try)) != 0) {
|
||||
if(updated.has_last_try) {
|
||||
if(memcmp(&existingEntry->last_try, &updated.last_try, sizeof(existingEntry->last_try)) != 0) {
|
||||
memcpy(&existingEntry->last_try, &updated.last_try, sizeof(existingEntry->last_try));
|
||||
hasChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (updated.has_last_seen) {
|
||||
if (memcmp(&existingEntry->last_seen, &updated.last_seen, sizeof(existingEntry->last_seen)) != 0) {
|
||||
if(updated.has_last_seen) {
|
||||
if(memcmp(&existingEntry->last_seen, &updated.last_seen, sizeof(existingEntry->last_seen)) != 0) {
|
||||
memcpy(&existingEntry->last_seen, &updated.last_seen, sizeof(existingEntry->last_seen));
|
||||
hasChanged = true;
|
||||
}
|
||||
}
|
||||
if (existingEntry->has_last_try != updated.has_last_try) {
|
||||
if(existingEntry->has_last_try != updated.has_last_try) {
|
||||
existingEntry->has_last_try = updated.has_last_try;
|
||||
hasChanged = true;
|
||||
}
|
||||
if (existingEntry->has_last_seen != updated.has_last_seen) {
|
||||
if(existingEntry->has_last_seen != updated.has_last_seen) {
|
||||
existingEntry->has_last_seen = updated.has_last_seen;
|
||||
hasChanged = true;
|
||||
}
|
||||
if (existingEntry->connected != (connected | updated.connected)) {
|
||||
if(existingEntry->connected != (connected | updated.connected)) {
|
||||
existingEntry->connected = connected | updated.connected;
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
if (strlen(updated.password) == 0 && strlen(existingEntry->password) > 0) {
|
||||
if(strlen(updated.password) == 0 && strlen(existingEntry->password) > 0) {
|
||||
ESP_LOGW(TAG_CRED_MANAGER, "Updated password is empty, while existing password is %s for %s. Ignoring.", existingEntry->password,
|
||||
existingEntry->ssid);
|
||||
} else {
|
||||
if (memcmp(existingEntry->password, updated.password, sizeof(existingEntry->password)) != 0) {
|
||||
if(memcmp(existingEntry->password, updated.password, sizeof(existingEntry->password)) != 0) {
|
||||
memcpy(existingEntry->password, updated.password, sizeof(existingEntry->password));
|
||||
hasChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (existingEntry->rssi != updated.rssi && updated.rssi != 0) {
|
||||
if(existingEntry->rssi != updated.rssi && updated.rssi != 0) {
|
||||
existingEntry->rssi = updated.rssi;
|
||||
hasChanged = true;
|
||||
}
|
||||
@@ -334,15 +320,11 @@ bool WifiList::Update(const wifi_sta_config_t* sta, bool connected) {
|
||||
return hasChanged;
|
||||
}
|
||||
sys_net_wifi_entry WifiList::ToSTAEntry(const sys_net_wifi_entry* sta) {
|
||||
if (!sta) {
|
||||
throw std::runtime_error("Null STA entry provided");
|
||||
}
|
||||
if(!sta) { throw std::runtime_error("Null STA entry provided"); }
|
||||
sys_net_wifi_entry result = *sta;
|
||||
if (result.radio_type_count > 0) {
|
||||
if(result.radio_type_count > 0) {
|
||||
std::unique_ptr<sys_net_radio_types[]> newRadioTypes(new sys_net_radio_types[result.radio_type_count]);
|
||||
if (!newRadioTypes) {
|
||||
throw std::runtime_error("Failed to allocate memory for radio types");
|
||||
}
|
||||
if(!newRadioTypes) { throw std::runtime_error("Failed to allocate memory for radio types"); }
|
||||
memcpy(newRadioTypes.get(), sta->radio_type, sizeof(sys_net_radio_types) * result.radio_type_count);
|
||||
result.radio_type = newRadioTypes.release();
|
||||
} else {
|
||||
@@ -358,20 +340,18 @@ sys_net_wifi_entry WifiList::ToSTAEntry(const wifi_sta_config_t* sta, sys_net_au
|
||||
sys_net_wifi_entry WifiList::ToSTAEntry(
|
||||
const wifi_sta_config_t* sta, const std::list<sys_net_radio_types>& radio_types, sys_net_auth_types auth_type) {
|
||||
sys_net_wifi_entry item = sys_net_wifi_entry_init_default;
|
||||
ESP_LOGD(TAG_CRED_MANAGER,"%s (sta_config)","toSTAEntry");
|
||||
ESP_LOGD(TAG_CRED_MANAGER, "%s (sta_config)", "toSTAEntry");
|
||||
auto result = ToSTAEntry(sta, item, radio_types);
|
||||
ESP_LOGV(TAG_CRED_MANAGER, "ToSTAEntry: SSID: %s, PASS: %s", result.ssid, result.password);
|
||||
return result;
|
||||
}
|
||||
sys_net_wifi_entry& WifiList::ToSTAEntry(const wifi_ap_record_t* ap, sys_net_wifi_entry& item) {
|
||||
if (ap) {
|
||||
if(ap) {
|
||||
auto radioTypes = GetRadioTypes(ap);
|
||||
item.radio_type_count=radioTypes.size();
|
||||
item.radio_type_count = radioTypes.size();
|
||||
item.radio_type = new sys_net_radio_types[item.radio_type_count];
|
||||
int i = 0;
|
||||
for (const auto& type : radioTypes) {
|
||||
item.radio_type[i++] = type;
|
||||
}
|
||||
for(const auto& type : radioTypes) { item.radio_type[i++] = type; }
|
||||
item.auth_type = GetAuthType(ap);
|
||||
FormatBSSID(ap, item);
|
||||
item.channel = ap->primary;
|
||||
@@ -386,15 +366,15 @@ sys_net_wifi_entry WifiList::ToSTAEntry(const wifi_ap_record_t* ap) {
|
||||
}
|
||||
sys_net_wifi_entry& WifiList::ToSTAEntry(
|
||||
const wifi_sta_config_t* sta, sys_net_wifi_entry& item, const std::list<sys_net_radio_types>& radio_types, sys_net_auth_types auth_type) {
|
||||
if (!sta) {
|
||||
if(!sta) {
|
||||
ESP_LOGE(TAG_CRED_MANAGER, "Invalid access point entry");
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
std::string ssid = GetSSID(sta); // Convert SSID to std::string
|
||||
std::string password = GetPassword(sta); // Convert password to std::string
|
||||
|
||||
if (ssid.empty()) {
|
||||
if(ssid.empty()) {
|
||||
ESP_LOGE(TAG_CRED_MANAGER, "Invalid access point ssid");
|
||||
return item;
|
||||
}
|
||||
@@ -402,7 +382,7 @@ sys_net_wifi_entry& WifiList::ToSTAEntry(
|
||||
memset(item.password, 0x00, sizeof(item.password));
|
||||
strncpy(item.ssid, ssid.c_str(), sizeof(item.ssid)); // Copy SSID
|
||||
strncpy(item.password, password.c_str(), sizeof(item.password)); // Copy password
|
||||
if (LOG_LOCAL_LEVEL > ESP_LOG_DEBUG) {
|
||||
if(LOG_LOCAL_LEVEL > ESP_LOG_DEBUG) {
|
||||
WifiList::FormatBSSID(item.bssid, sizeof(item.bssid), sta->bssid); // Format BSSID
|
||||
}
|
||||
item.channel = sta->channel;
|
||||
@@ -410,16 +390,14 @@ sys_net_wifi_entry& WifiList::ToSTAEntry(
|
||||
item.auth_type = auth_type;
|
||||
|
||||
// Handle the radio_type array
|
||||
if (item.radio_type != nullptr) {
|
||||
if(item.radio_type != nullptr) {
|
||||
delete[] item.radio_type; // Free existing array if any
|
||||
item.radio_type_count = 0;
|
||||
}
|
||||
item.radio_type_count = radio_types.size();
|
||||
item.radio_type = new sys_net_radio_types[item.radio_type_count];
|
||||
int i = 0;
|
||||
for (const auto& type : radio_types) {
|
||||
item.radio_type[i++] = type;
|
||||
}
|
||||
for(const auto& type : radio_types) { item.radio_type[i++] = type; }
|
||||
|
||||
ESP_LOGV(TAG_CRED_MANAGER, "ToSTAEntry wifi : %s, password: %s", item.ssid, item.password);
|
||||
return item;
|
||||
@@ -427,7 +405,7 @@ sys_net_wifi_entry& WifiList::ToSTAEntry(
|
||||
bool WifiList::RemoveCredential(const wifi_sta_config_t* sta) { return RemoveCredential(GetSSID(sta)); }
|
||||
bool WifiList::RemoveCredential(const std::string& ssid) {
|
||||
auto it = credentials_.find(ssid);
|
||||
if (it != credentials_.end()) {
|
||||
if(it != credentials_.end()) {
|
||||
// Release any dynamically allocated fields in the structure
|
||||
Release(&it->second);
|
||||
// Erase the entry from the map
|
||||
@@ -438,51 +416,39 @@ bool WifiList::RemoveCredential(const std::string& ssid) {
|
||||
}
|
||||
|
||||
void WifiList::Clear() {
|
||||
if (Lock()) {
|
||||
for (auto& e : credentials_) {
|
||||
Release( &e.second);
|
||||
}
|
||||
if(Lock()) {
|
||||
for(auto& e : credentials_) { Release(&e.second); }
|
||||
credentials_.clear();
|
||||
Unlock();
|
||||
}
|
||||
}
|
||||
bool WifiList::ResetRSSI() {
|
||||
if (!Lock()) {
|
||||
if(!Lock()) {
|
||||
ESP_LOGE(TAG_CRED_MANAGER, "Unable to lock structure %s", name_.c_str());
|
||||
return false;
|
||||
}
|
||||
for (auto& e : credentials_) {
|
||||
e.second.rssi = 0;
|
||||
}
|
||||
for(auto& e : credentials_) { e.second.rssi = 0; }
|
||||
Unlock();
|
||||
return true;
|
||||
}
|
||||
bool WifiList::ResetConnected() {
|
||||
if (!Lock()) {
|
||||
throw std::runtime_error("Lock failed");
|
||||
}
|
||||
for (auto& e : credentials_) {
|
||||
e.second.connected = false;
|
||||
}
|
||||
if(!Lock()) { throw std::runtime_error("Lock failed"); }
|
||||
for(auto& e : credentials_) { e.second.connected = false; }
|
||||
Unlock();
|
||||
return true;
|
||||
}
|
||||
sys_net_wifi_entry* WifiList::Get(const std::string& ssid) {
|
||||
auto it = credentials_.find(ssid);
|
||||
if (it != credentials_.end()) {
|
||||
return &(it->second);
|
||||
}
|
||||
if(it != credentials_.end()) { return &(it->second); }
|
||||
return nullptr;
|
||||
}
|
||||
const sys_net_wifi_entry* WifiList::GetConnected() {
|
||||
if (!Lock()) {
|
||||
if(!Lock()) {
|
||||
ESP_LOGE(TAG_CRED_MANAGER, "Unable to lock structure %s", name_.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
for (auto& e : credentials_) {
|
||||
if (e.second.connected) {
|
||||
return &e.second;
|
||||
}
|
||||
for(auto& e : credentials_) {
|
||||
if(e.second.connected) { return &e.second; }
|
||||
}
|
||||
Unlock();
|
||||
return nullptr;
|
||||
@@ -491,13 +457,11 @@ bool WifiList::SetConnected(const wifi_event_sta_connected_t* evt, bool connecte
|
||||
auto ssid = GetSSID(evt);
|
||||
auto bssid = GetBSSID(evt);
|
||||
auto existing = Get(ssid);
|
||||
if (existing) {
|
||||
if (bssid != existing->bssid || existing->connected != connected || existing->auth_type != GetAuthType(evt->authmode) ||
|
||||
if(existing) {
|
||||
if(bssid != existing->bssid || existing->connected != connected || existing->auth_type != GetAuthType(evt->authmode) ||
|
||||
existing->channel != evt->channel) {
|
||||
ResetConnected();
|
||||
if (!Lock()) {
|
||||
throw std::runtime_error("Lock failed");
|
||||
}
|
||||
if(!Lock()) { throw std::runtime_error("Lock failed"); }
|
||||
strncpy(existing->bssid, bssid.c_str(), sizeof(existing->bssid));
|
||||
existing->connected = connected;
|
||||
existing->auth_type = GetAuthType(evt->authmode);
|
||||
@@ -512,21 +476,17 @@ bool WifiList::SetConnected(const wifi_event_sta_connected_t* evt, bool connecte
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
sys_net_wifi_entry& WifiList::AddUpdate(const wifi_sta_config_t* sta, sys_net_auth_types auth_type) {
|
||||
return AddUpdate(sta, GetRadioTypes(nullptr), auth_type);
|
||||
}
|
||||
sys_net_wifi_entry& WifiList::AddUpdate(
|
||||
const wifi_sta_config_t* sta, const std::list<sys_net_radio_types>& radio_types, sys_net_auth_types auth_type) {
|
||||
auto ssid = GetSSID(sta);
|
||||
|
||||
if (!Exists(sta)) {
|
||||
|
||||
if(!Exists(sta)) {
|
||||
auto entry = ToSTAEntry(sta, radio_types, auth_type);
|
||||
ESP_LOGD(TAG_CRED_MANAGER, "Added new entry %s to list %s", ssid.c_str(), name_.c_str());
|
||||
if (!Lock()) {
|
||||
throw std::runtime_error("Lock failed");
|
||||
}
|
||||
if(!Lock()) { throw std::runtime_error("Lock failed"); }
|
||||
credentials_[ssid] = entry;
|
||||
Unlock();
|
||||
} else {
|
||||
@@ -537,17 +497,13 @@ sys_net_wifi_entry& WifiList::AddUpdate(
|
||||
}
|
||||
bool WifiList::UpdateFromClock() {
|
||||
bool changed = false;
|
||||
if (Lock()) {
|
||||
if(Lock()) {
|
||||
|
||||
for (auto iter = credentials_.begin(); iter != credentials_.end(); ++iter) {
|
||||
for(auto iter = credentials_.begin(); iter != credentials_.end(); ++iter) {
|
||||
bool entrychanged = false;
|
||||
if (iter->second.has_last_seen) {
|
||||
entrychanged |= OffsetTimeStamp(&iter->second.last_seen);
|
||||
}
|
||||
if (iter->second.has_last_try) {
|
||||
entrychanged |= OffsetTimeStamp(&iter->second.last_try);
|
||||
}
|
||||
if (entrychanged) {
|
||||
if(iter->second.has_last_seen) { entrychanged |= OffsetTimeStamp(&iter->second.last_seen); }
|
||||
if(iter->second.has_last_try) { entrychanged |= OffsetTimeStamp(&iter->second.last_try); }
|
||||
if(entrychanged) {
|
||||
ESP_LOGD(TAG_CRED_MANAGER, "Updated from clock");
|
||||
PrintWifiSTAEntry(iter->second);
|
||||
}
|
||||
@@ -559,7 +515,7 @@ bool WifiList::UpdateFromClock() {
|
||||
return changed;
|
||||
}
|
||||
void WifiList::PrintTimeStamp(const google_protobuf_Timestamp* timestamp) {
|
||||
if (timestamp == NULL) {
|
||||
if(timestamp == NULL) {
|
||||
printf("Timestamp is NULL\n");
|
||||
return;
|
||||
}
|
||||
@@ -567,14 +523,12 @@ void WifiList::PrintTimeStamp(const google_protobuf_Timestamp* timestamp) {
|
||||
char buffer[80];
|
||||
|
||||
// Check for special case of time == 0
|
||||
if (timestamp->seconds == 0) {
|
||||
if (timestamp->nanos != 0) {
|
||||
printf("nanos not empty!");
|
||||
}
|
||||
if(timestamp->seconds == 0) {
|
||||
if(timestamp->nanos != 0) { printf("nanos not empty!"); }
|
||||
snprintf(buffer, sizeof(buffer), "%-26s", "N/A");
|
||||
}
|
||||
// Check for timestamps less than 1704143717 (2024-01-01)
|
||||
else if (timestamp->seconds > 0 && timestamp->seconds < 1704143717) {
|
||||
else if(timestamp->seconds > 0 && timestamp->seconds < 1704143717) {
|
||||
// Convert seconds to time_t for use with localtime
|
||||
time_t rawtime = (time_t)timestamp->seconds;
|
||||
struct tm* timeinfo = localtime(&rawtime);
|
||||
@@ -591,9 +545,7 @@ void WifiList::PrintTimeStamp(const google_protobuf_Timestamp* timestamp) {
|
||||
printf("%-26s", buffer);
|
||||
}
|
||||
bool WifiList::UpdateLastTry(const std::string ssid) {
|
||||
if (!Lock()) {
|
||||
throw std::runtime_error("Lock failed");
|
||||
}
|
||||
if(!Lock()) { throw std::runtime_error("Lock failed"); }
|
||||
auto entry = Get(ssid);
|
||||
ESP_RETURN_ON_FALSE(entry != nullptr, false, TAG_CRED_MANAGER, "Unknown ssid %s", ssid.c_str());
|
||||
ESP_RETURN_ON_FALSE(entry->ssid != nullptr, false, TAG_CRED_MANAGER, "Invalid pointer!");
|
||||
@@ -612,14 +564,10 @@ bool WifiList::UpdateLastTry(const wifi_ap_record_t* ap) { return UpdateLastTry(
|
||||
bool WifiList::UpdateLastSeen(const wifi_sta_config_t* sta) { return UpdateLastSeen(GetSSID(sta)); }
|
||||
bool WifiList::UpdateLastSeen(const wifi_ap_record_t* ap) { return UpdateLastSeen(GetSSID(ap)); }
|
||||
bool WifiList::UpdateLastSeen(const std::string ssid) {
|
||||
if (!Lock()) {
|
||||
throw std::runtime_error("Lock failed");
|
||||
}
|
||||
if(!Lock()) { throw std::runtime_error("Lock failed"); }
|
||||
auto entry = Get(ssid);
|
||||
bool changed = false;
|
||||
if (entry != nullptr) {
|
||||
changed = UpdateLastSeen(entry);
|
||||
}
|
||||
if(entry != nullptr) { changed = UpdateLastSeen(entry); }
|
||||
Unlock();
|
||||
return changed;
|
||||
}
|
||||
@@ -630,11 +578,9 @@ bool WifiList::UpdateLastSeen(sys_net_wifi_entry* entry) {
|
||||
}
|
||||
sys_net_wifi_entry& WifiList::AddUpdate(const wifi_ap_record_t* scan_rec) {
|
||||
auto ssid = GetSSID(scan_rec);
|
||||
if (!Exists(scan_rec)) {
|
||||
if(!Exists(scan_rec)) {
|
||||
ESP_LOGV(TAG_CRED_MANAGER, "Added new entry %s to list %s", ssid.c_str(), name_.c_str());
|
||||
if (!Lock()) {
|
||||
throw std::runtime_error("Lock failed");
|
||||
}
|
||||
if(!Lock()) { throw std::runtime_error("Lock failed"); }
|
||||
credentials_[ssid] = ToSTAEntry(scan_rec);
|
||||
Unlock();
|
||||
} else {
|
||||
@@ -643,17 +589,13 @@ sys_net_wifi_entry& WifiList::AddUpdate(const wifi_ap_record_t* scan_rec) {
|
||||
return credentials_[ssid];
|
||||
}
|
||||
sys_net_wifi_entry& WifiList::AddUpdate(const char* ssid, const char* password) {
|
||||
if (ssid == nullptr || password == nullptr) {
|
||||
throw std::invalid_argument("SSID and password cannot be null");
|
||||
}
|
||||
if(ssid == nullptr || password == nullptr) { throw std::invalid_argument("SSID and password cannot be null"); }
|
||||
// Ensure that the SSID and password are not too long
|
||||
if (std::strlen(ssid) >= sizeof(sys_net_wifi_entry::ssid) || std::strlen(password) >= sizeof(sys_net_wifi_entry::password)) {
|
||||
if(std::strlen(ssid) >= sizeof(sys_net_wifi_entry::ssid) || std::strlen(password) >= sizeof(sys_net_wifi_entry::password)) {
|
||||
throw std::length_error("SSID or password is too long");
|
||||
}
|
||||
if (!Exists(ssid)) {
|
||||
if (!Lock()) {
|
||||
throw std::runtime_error("Lock failed");
|
||||
}
|
||||
if(!Exists(ssid)) {
|
||||
if(!Lock()) { throw std::runtime_error("Lock failed"); }
|
||||
sys_net_wifi_entry newEntry = sys_net_wifi_entry_init_default;
|
||||
// Copy the SSID and password into the new entry, ensuring null termination
|
||||
std::strncpy(newEntry.ssid, ssid, sizeof(newEntry.ssid) - 1);
|
||||
@@ -665,7 +607,7 @@ sys_net_wifi_entry& WifiList::AddUpdate(const char* ssid, const char* password)
|
||||
Unlock();
|
||||
} else {
|
||||
auto existing = Get(ssid);
|
||||
if (strncmp(existing->password, password, sizeof(existing->password)) != 0) {
|
||||
if(strncmp(existing->password, password, sizeof(existing->password)) != 0) {
|
||||
strncpy(existing->password, password, sizeof(existing->password));
|
||||
existing->password[sizeof(existing->password) - 1] = '\0';
|
||||
}
|
||||
@@ -673,16 +615,12 @@ sys_net_wifi_entry& WifiList::AddUpdate(const char* ssid, const char* password)
|
||||
return credentials_[ssid];
|
||||
}
|
||||
sys_net_wifi_entry& WifiList::AddUpdate(const sys_net_wifi_entry* sta, const char* password) {
|
||||
if (sta == nullptr) {
|
||||
throw std::invalid_argument("Entry pointer cannot be null");
|
||||
}
|
||||
if(sta == nullptr) { throw std::invalid_argument("Entry pointer cannot be null"); }
|
||||
auto converted = ToSTAEntry(sta);
|
||||
strncpy(converted.password, password, sizeof(converted.password));
|
||||
if (!Exists(sta->ssid)) {
|
||||
strncpy(converted.password, password, sizeof(converted.password));
|
||||
if(!Exists(sta->ssid)) {
|
||||
ESP_LOGD(TAG_CRED_MANAGER, "Added new entry %s to list %s", sta->ssid, name_.c_str());
|
||||
if (!Lock()) {
|
||||
throw std::runtime_error("Lock failed");
|
||||
}
|
||||
if(!Lock()) { throw std::runtime_error("Lock failed"); }
|
||||
credentials_[sta->ssid] = converted;
|
||||
Unlock();
|
||||
} else {
|
||||
@@ -695,8 +633,8 @@ sys_net_wifi_entry& WifiList::AddUpdate(const sys_net_wifi_entry* sta, const cha
|
||||
}
|
||||
void WifiList::PrintString(const char* pData, size_t length, const char* format) {
|
||||
std::string buffer;
|
||||
for (size_t i = 0; i < length && pData[i]; i++) {
|
||||
if (isprint((char)pData[i])) {
|
||||
for(size_t i = 0; i < length && pData[i]; i++) {
|
||||
if(isprint((char)pData[i])) {
|
||||
buffer += (char)pData[i]; // Print as a character
|
||||
} else {
|
||||
buffer += '?';
|
||||
@@ -723,12 +661,12 @@ void WifiList::PrintWifiSTAEntry(const sys_net_wifi_entry& entry) {
|
||||
printf("%3u ", static_cast<unsigned>(entry.channel));
|
||||
printf("%-14s", sys_net_auth_types_name(entry.auth_type));
|
||||
printf("%-9s", formatRadioTypes(entry.radio_type, entry.radio_type_count).c_str());
|
||||
if (entry.has_last_try) {
|
||||
if(entry.has_last_try) {
|
||||
PrintTimeStamp(&entry.last_try);
|
||||
} else {
|
||||
PrintTimeStamp(>s);
|
||||
}
|
||||
if (entry.has_last_seen) {
|
||||
if(entry.has_last_seen) {
|
||||
PrintTimeStamp(&entry.last_seen);
|
||||
} else {
|
||||
PrintTimeStamp(>s);
|
||||
@@ -736,23 +674,17 @@ void WifiList::PrintWifiSTAEntry(const sys_net_wifi_entry& entry) {
|
||||
printf("\n");
|
||||
}
|
||||
sys_net_wifi_entry* WifiList::GetIndex(size_t index) {
|
||||
if (index >= credentials_.size()) {
|
||||
return nullptr;
|
||||
}
|
||||
if(index >= credentials_.size()) { return nullptr; }
|
||||
auto it = credentials_.begin();
|
||||
std::advance(it, index);
|
||||
return &(it->second);
|
||||
}
|
||||
sys_net_wifi_entry& WifiList::GetStrongestSTA() {
|
||||
if (credentials_.empty()) {
|
||||
throw std::runtime_error("No credentials available");
|
||||
}
|
||||
if(credentials_.empty()) { throw std::runtime_error("No credentials available"); }
|
||||
|
||||
auto strongestIter = credentials_.begin();
|
||||
for (auto iter = credentials_.begin(); iter != credentials_.end(); ++iter) {
|
||||
if (iter->second.rssi > strongestIter->second.rssi) {
|
||||
strongestIter = iter;
|
||||
}
|
||||
for(auto iter = credentials_.begin(); iter != credentials_.end(); ++iter) {
|
||||
if(iter->second.rssi > strongestIter->second.rssi) { strongestIter = iter; }
|
||||
}
|
||||
|
||||
return strongestIter->second;
|
||||
@@ -760,38 +692,24 @@ sys_net_wifi_entry& WifiList::GetStrongestSTA() {
|
||||
|
||||
std::list<sys_net_radio_types> WifiList::GetRadioTypes(const wifi_ap_record_t* sta) {
|
||||
std::list<sys_net_radio_types> result;
|
||||
if (sta == nullptr) {
|
||||
if(sta == nullptr) {
|
||||
result.push_back(sys_net_radio_types_UNKNOWN);
|
||||
} else {
|
||||
|
||||
// Check each bit field and return the corresponding enum value
|
||||
if (sta->phy_11b) {
|
||||
result.push_back(sys_net_radio_types_PHY_11B);
|
||||
}
|
||||
if (sta->phy_11g) {
|
||||
result.push_back(sys_net_radio_types_PHY_11G);
|
||||
}
|
||||
if (sta->phy_11n) {
|
||||
result.push_back(sys_net_radio_types_PHY_11N);
|
||||
}
|
||||
if (sta->phy_lr) {
|
||||
result.push_back(sys_net_radio_types_LR);
|
||||
}
|
||||
if (sta->wps) {
|
||||
result.push_back(sys_net_radio_types_WPS);
|
||||
}
|
||||
if (sta->ftm_responder) {
|
||||
result.push_back(sys_net_radio_types_FTM_RESPONDER);
|
||||
}
|
||||
if (sta->ftm_initiator) {
|
||||
result.push_back(sys_net_radio_types_FTM_INITIATOR);
|
||||
}
|
||||
if(sta->phy_11b) { result.push_back(sys_net_radio_types_PHY_11B); }
|
||||
if(sta->phy_11g) { result.push_back(sys_net_radio_types_PHY_11G); }
|
||||
if(sta->phy_11n) { result.push_back(sys_net_radio_types_PHY_11N); }
|
||||
if(sta->phy_lr) { result.push_back(sys_net_radio_types_LR); }
|
||||
if(sta->wps) { result.push_back(sys_net_radio_types_WPS); }
|
||||
if(sta->ftm_responder) { result.push_back(sys_net_radio_types_FTM_RESPONDER); }
|
||||
if(sta->ftm_initiator) { result.push_back(sys_net_radio_types_FTM_INITIATOR); }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
wifi_auth_mode_t WifiList::GetESPAuthMode(sys_net_auth_types auth_type) {
|
||||
switch (auth_type) {
|
||||
switch(auth_type) {
|
||||
case sys_net_auth_types_OPEN:
|
||||
return WIFI_AUTH_OPEN;
|
||||
case sys_net_auth_types_WEP:
|
||||
@@ -814,12 +732,10 @@ wifi_auth_mode_t WifiList::GetESPAuthMode(sys_net_auth_types auth_type) {
|
||||
return WIFI_AUTH_OPEN; // Default case
|
||||
}
|
||||
}
|
||||
sys_net_auth_types WifiList::GetAuthType(const wifi_ap_record_t* ap) {
|
||||
return ap ? GetAuthType(ap->authmode) : sys_net_auth_types_AUTH_UNKNOWN;
|
||||
}
|
||||
sys_net_auth_types WifiList::GetAuthType(const wifi_ap_record_t* ap) { return ap ? GetAuthType(ap->authmode) : sys_net_auth_types_AUTH_UNKNOWN; }
|
||||
sys_net_auth_types WifiList::GetAuthType(const wifi_auth_mode_t mode) {
|
||||
|
||||
switch (mode) {
|
||||
switch(mode) {
|
||||
case WIFI_AUTH_OPEN:
|
||||
return sys_net_auth_types_OPEN;
|
||||
case WIFI_AUTH_WEP:
|
||||
|
||||
Reference in New Issue
Block a user