Skip to content

Commit

Permalink
Use linked list instead of set
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Orru <simone.orru@secomind.com>
  • Loading branch information
sorru94 committed Nov 27, 2023
1 parent abb93e9 commit ca19b5c
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions src/astarte_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <astarte_hwid.h>
#include <astarte_linked_list.h>
#include <astarte_pairing.h>
#include <astarte_set.h>
#include <astarte_storage.h>
#include <astarte_zlib.h>

Expand Down Expand Up @@ -79,7 +78,7 @@ static void send_introspection(astarte_device_handle_t device);
static void send_emptycache(astarte_device_handle_t device);
static void send_device_owned_properties(astarte_device_handle_t device);
static void send_purge_device_properties(
astarte_device_handle_t device, astarte_set_handle_t *set_handle);
astarte_device_handle_t device, astarte_linked_list_handle_t *list_handle);
static void on_connected(astarte_device_handle_t device, int session_present);
static void on_disconnected(astarte_device_handle_t device);
static void on_incoming(
Expand Down Expand Up @@ -1096,7 +1095,7 @@ static void send_device_owned_properties(astarte_device_handle_t device)
char *path = NULL;
uint8_t *value = NULL;

astarte_set_handle_t set_handle = astarte_set_init();
astarte_linked_list_handle_t list_handle = astarte_linked_list_init();

// Open storage
astarte_storage_handle_t storage_handle;
Expand Down Expand Up @@ -1183,12 +1182,12 @@ static void send_device_owned_properties(astarte_device_handle_t device)
path_len - 1);

// Store the property full path in a temporary set
astarte_set_element_t set_element
astarte_linked_list_item_t list_item
= { .value = property_full_path, .value_len = property_full_path_len };
astarte_err_t set_err = astarte_set_add(&set_handle, set_element);
if (set_err != ASTARTE_OK) {
astarte_err_t list_err = astarte_linked_list_append(&list_handle, list_item);
if (list_err != ASTARTE_OK) {
ESP_LOGE(TAG, "Error adding a property name to the set %s.",
astarte_err_to_name(set_err));
astarte_err_to_name(list_err));
goto end;
}
}
Expand All @@ -1215,11 +1214,11 @@ static void send_device_owned_properties(astarte_device_handle_t device)
astarte_storage_close(storage_handle);

// Send purge device properties
send_purge_device_properties(device, &set_handle);
send_purge_device_properties(device, &list_handle);

end:
// Destroy the set
astarte_set_destroy_and_release(&set_handle);
astarte_linked_list_destroy_and_release(&list_handle);

// Free all data
free(interface_name);
Expand All @@ -1228,7 +1227,7 @@ static void send_device_owned_properties(astarte_device_handle_t device)
}

static void send_purge_device_properties(
astarte_device_handle_t device, astarte_set_handle_t *set_handle)
astarte_device_handle_t device, astarte_linked_list_handle_t *list_handle)
{
// Pointer to string that will contain a list of interface names separated by the ';' char
char *properties_list = NULL;
Expand All @@ -1238,11 +1237,9 @@ static void send_purge_device_properties(
char *payload = NULL;
size_t payload_len = 0;

while (!astarte_set_is_empty(set_handle)) {
// Get a property path from the set
astarte_set_element_t property_path;
astarte_set_pop(set_handle, &property_path); // Ignore return value as it is always ok

// Create the uncompressed properties string
astarte_linked_list_item_t property_path;
while (astarte_linked_list_remove_tail(list_handle, &property_path) != ASTARTE_ERR_NOT_FOUND) {
// Append interface name to the ';' separated string
// Both lengths include a +1 for the '\0' char
// We do a simple sum since we need an extra char to store the ';'
Expand All @@ -1252,8 +1249,8 @@ static void send_purge_device_properties(
ESP_LOGE(TAG, "Out of memory %s: %d", __FILE__, __LINE__);
free(property_path.value);
// Deallocate everything from the set
while (!astarte_set_is_empty(set_handle)) {
astarte_set_pop(set_handle, &property_path); // Ignore return value, it is always ok
while (astarte_linked_list_remove_tail(list_handle, &property_path)
!= ASTARTE_ERR_NOT_FOUND) {
free(property_path.value);
}
goto end;
Expand All @@ -1272,10 +1269,10 @@ static void send_purge_device_properties(
free(property_path.value);
}

// Step 1 estimate compression result size and payload size
// Estimate compression result size and payload size
size_t compression_input_len = (properties_list) ? (properties_list_len - 1) : 0;
uLongf compressed_len = compressBound(compression_input_len);
// Step 2 allocate enough memory for the payload
// Allocate enough memory for the payload
payload_len = 4 + compressed_len;
payload = calloc(payload_len, sizeof(char));
if (payload == NULL) {
Expand Down

0 comments on commit ca19b5c

Please sign in to comment.