Skip to content

Commit

Permalink
Full interfaces implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Orru <[email protected]>
  • Loading branch information
sorru94 committed Jan 19, 2024
1 parent 88b6569 commit 8dda144
Show file tree
Hide file tree
Showing 14 changed files with 683 additions and 48 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ idf_component_register(
"./src/astarte_credentials.c"
"./src/astarte_device.c"
"./src/astarte_err_to_name.c"
"./src/astarte_mapping.c"
"./src/astarte_hwid.c"
"./src/astarte_linked_list.c"
"./src/astarte_pairing.c"
Expand Down
11 changes: 8 additions & 3 deletions examples/aggregates/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
# SPDX-License-Identifier: LGPL-2.1-or-later OR Apache-2.0
#

idf_component_register(SRCS "app_main.c" "./src/wifi_cfg.c" "./src/example_task.c"
INCLUDE_DIRS "./include"
REQUIRES astarte-device-sdk-esp32 nvs_flash esp_event esp_wifi)
idf_component_register(
SRCS
"app_main.c"
"./src/astarte_interface_gen.c"
"./src/wifi_cfg.c"
"./src/example_task.c"
INCLUDE_DIRS "./include"
REQUIRES astarte-device-sdk-esp32 nvs_flash esp_event esp_wifi)
23 changes: 23 additions & 0 deletions examples/aggregates/main/include/astarte_interface_gen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* (C) Copyright 2023, SECO Mind Srl
*
* SPDX-License-Identifier: LGPL-2.1-or-later OR Apache-2.0
*/

/**
* @file astarte_interface_gen.h
* @brief Contains automatically generated interfaces.
*
* @details The generated structures contain all information regarding each interface.
* and are automatically generated from the json interfaces definitions.
*/

#ifndef _ASTARTE_INTERFACE_GEN_H_
#define _ASTARTE_INTERFACE_GEN_H_

#include "astarte_interface.h"

extern const astarte_interface_t astarte_interface_org_astarteplatform_esp32_examples_DeviceAggregate;
extern const astarte_interface_t astarte_interface_org_astarteplatform_esp32_examples_ServerAggregate;

#endif /* _ASTARTE_INTERFACE_GEN_H_ */
80 changes: 80 additions & 0 deletions examples/aggregates/main/src/astarte_interface_gen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* (C) Copyright 2023, SECO Mind Srl
*
* SPDX-License-Identifier: LGPL-2.1-or-later OR Apache-2.0
*/

/**
* @file astarte_interface_gen.c
* @brief Contains automatically generated interfaces.
*/

#include "astarte_interface_gen.h"

static const astarte_mapping_t astarte_mappings_org_astarteplatform_esp32_examples_DeviceAggregate[4] = {
{
.endpoint = "/%{sensor_id}/double_endpoint",
.type = TYPE_DOUBLE,
.reliability = RELIABILITY_UNRELIABLE,
.explicit_timestamp = true,
.allow_unset = false,
},
{
.endpoint = "/%{sensor_id}/integer_endpoint",
.type = TYPE_INTEGER,
.reliability = RELIABILITY_UNRELIABLE,
.explicit_timestamp = true,
.allow_unset = false,
},
{
.endpoint = "/%{sensor_id}/boolean_endpoint",
.type = TYPE_BOOLEAN,
.reliability = RELIABILITY_UNRELIABLE,
.explicit_timestamp = true,
.allow_unset = false,
},
{
.endpoint = "/%{sensor_id}/doublearray_endpoint",
.type = TYPE_DOUBLEARRAY,
.reliability = RELIABILITY_UNRELIABLE,
.explicit_timestamp = true,
.allow_unset = false,
},
};

const astarte_interface_t astarte_interface_org_astarteplatform_esp32_examples_DeviceAggregate = {
.name = "org.astarteplatform.esp32.examples.DeviceAggregate",
.major_version = 0,
.minor_version = 1,
.type = TYPE_DATASTREAM,
.ownership = OWNERSHIP_DEVICE,
.aggregation = AGGREGATION_OBJECT,
.mappings = astarte_mappings_org_astarteplatform_esp32_examples_DeviceAggregate,
};

static const astarte_mapping_t astarte_mappings_org_astarteplatform_esp32_examples_ServerAggregate[2] = {
{
.endpoint = "/%{sensor_id}/booleanarray_endpoint",
.type = TYPE_BOOLEANARRAY,
.reliability = RELIABILITY_UNRELIABLE,
.explicit_timestamp = true,
.allow_unset = false,
},
{
.endpoint = "/%{sensor_id}/longinteger_endpoint",
.type = TYPE_LONGINTEGER,
.reliability = RELIABILITY_UNRELIABLE,
.explicit_timestamp = true,
.allow_unset = false,
},
};

const astarte_interface_t astarte_interface_org_astarteplatform_esp32_examples_ServerAggregate = {
.name = "org.astarteplatform.esp32.examples.ServerAggregate",
.major_version = 0,
.minor_version = 1,
.type = TYPE_DATASTREAM,
.ownership = OWNERSHIP_SERVER,
.aggregation = AGGREGATION_OBJECT,
.mappings = astarte_mappings_org_astarteplatform_esp32_examples_ServerAggregate,
};
32 changes: 11 additions & 21 deletions examples/aggregates/main/src/example_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "astarte_credentials.h"
#include "astarte_device.h"
#include "astarte_interface.h"
#include "astarte_interface_gen.h"

/************************************************
* Constants and defines
Expand All @@ -44,22 +45,6 @@ static const char cred_sec[] = CONFIG_CREDENTIALS_SECRET;
static const char hwid[] = CONFIG_DEVICE_ID;
static const char realm[] = CONFIG_ASTARTE_REALM;

const static astarte_interface_t device_datastream_interface = {
.name = "org.astarteplatform.esp32.examples.DeviceAggregate",
.major_version = 0,
.minor_version = 1,
.ownership = OWNERSHIP_DEVICE,
.type = TYPE_DATASTREAM,
};

const static astarte_interface_t server_datastream_interface = {
.name = "org.astarteplatform.esp32.examples.ServerAggregate",
.major_version = 0,
.minor_version = 1,
.ownership = OWNERSHIP_SERVER,
.type = TYPE_DATASTREAM,
};

/************************************************
* Structs declarations
***********************************************/
Expand Down Expand Up @@ -134,8 +119,10 @@ void astarte_example_task(void *ctx)
return;
}

astarte_device_add_interface(device, &device_datastream_interface);
astarte_device_add_interface(device, &server_datastream_interface);
astarte_device_add_interface(
device, &astarte_interface_org_astarteplatform_esp32_examples_DeviceAggregate);
astarte_device_add_interface(
device, &astarte_interface_org_astarteplatform_esp32_examples_ServerAggregate);
if (astarte_device_start(device) != ASTARTE_OK) {
ESP_LOGE(TAG, "Failed to start astarte device");
return;
Expand Down Expand Up @@ -163,7 +150,9 @@ static void astarte_data_events_handler(astarte_device_data_event_t *event)
uint8_t parsing_error = 0;
struct rx_aggregate rx_data;

if ((strcmp(event->interface_name, server_datastream_interface.name) == 0)
if ((strcmp(event->interface_name,
astarte_interface_org_astarteplatform_esp32_examples_ServerAggregate.name)
== 0)
&& (strcmp(event->path, "/11") == 0)) {

parsing_error = parse_received_bson(event->bson_element, &rx_data);
Expand Down Expand Up @@ -199,8 +188,9 @@ static void astarte_data_events_handler(astarte_device_data_event_t *event)
astarte_bson_serializer_append_end_of_document(aggregate_bson);

const void *doc = astarte_bson_serializer_get_document(aggregate_bson, NULL);
astarte_err_t res = astarte_device_stream_aggregate(
event->device, device_datastream_interface.name, "/24", doc, 0);
astarte_err_t res = astarte_device_stream_aggregate(event->device,
astarte_interface_org_astarteplatform_esp32_examples_DeviceAggregate.name, "/24", doc,
0);
if (res != ASTARTE_OK) {
ESP_LOGE(TAG, "Error streaming the aggregate");
}
Expand Down
45 changes: 21 additions & 24 deletions include/astarte_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,40 @@

/**
* @file astarte_interface.h
* @brief Astarte interface functions.
* @brief Generic specifications for Astarte interface.
*/

#ifndef _ASTARTE_INTERFACE_H_
#define _ASTARTE_INTERFACE_H_

/**
* @brief interface ownership
*
* This enum represents the possible ownerhips of an Astarte interface
*/
#include "astarte_mapping.h"

typedef enum
{
OWNERSHIP_DEVICE = 1, /**< Device owned interface */
OWNERSHIP_SERVER, /**< Server owned interface*/
OWNERSHIP_DEVICE = 1,
OWNERSHIP_SERVER,
} astarte_interface_ownership_t;

typedef enum
{
TYPE_DATASTREAM = 1, /**< Datastream interface */
TYPE_PROPERTIES, /**< Properties interface */
TYPE_DATASTREAM = 1,
TYPE_PROPERTIES,
} astarte_interface_type_t;

/**
* @brief Astarte interface definition
*
* This struct represents a subset of the information contained in an Astarte interface, and can be
* used to specify some details about a specific interface.
*
*/
typedef struct
typedef enum
{
const char *name; /**< Interface name */
int major_version; /**< Major version */
int minor_version; /**< Minor version */
astarte_interface_ownership_t ownership; /**< Ownership, see #astarte_interface_ownership_t */
astarte_interface_type_t type; /**< Type, see #astarte_interface_type_t */
AGGREGATION_INDIVIDUAL = 1,
AGGREGATION_OBJECT,
} astarte_interface_aggregation_t;

typedef struct {
const char *name;
int major_version;
int minor_version;
astarte_interface_type_t type;
astarte_interface_ownership_t ownership;
astarte_interface_aggregation_t aggregation;
const astarte_mapping_t *mappings;
} astarte_interface_t;

#endif
#endif /* _ASTARTE_INTERFACE_H_ */
51 changes: 51 additions & 0 deletions include/astarte_mapping.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* (C) Copyright 2018-2023, SECO Mind Srl
*
* SPDX-License-Identifier: LGPL-2.1-or-later OR Apache-2.0
*/

/**
* @file astarte_mapping.h
* @brief Generic specifications for Astarte interface mappings.
*/

#ifndef _ASTARTE_MAPPING_H_
#define _ASTARTE_MAPPING_H_

#include <stdbool.h>
#include "astarte.h"

typedef enum {
TYPE_INTEGER = 1,
TYPE_LONGINTEGER,
TYPE_DOUBLE,
TYPE_STRING,
TYPE_BINARYBLOB,
TYPE_BOOLEAN,
TYPE_DATETIME,
TYPE_INTEGERARRAY,
TYPE_LONGINTEGERARRAY,
TYPE_DOUBLEARRAY,
TYPE_STRINGARRAY,
TYPE_BINARYBLOBARRAY,
TYPE_BOOLEANARRAY,
TYPE_DATETIMEARRAY,
} astarte_mapping_type_t;

typedef enum {
RELIABILITY_UNRELIABLE = 0,
RELIABILITY_GUARANTEED = 1,
RELIABILITY_UNIQUE = 2,
} astarte_mapping_reliability_t;

typedef struct {
const char *endpoint;
astarte_mapping_type_t type;
astarte_mapping_reliability_t reliability;
bool explicit_timestamp;
bool allow_unset;
} astarte_mapping_t;

astarte_err_t validate_path(astarte_mapping_t mapping, const char *path, bool *result);

#endif /* _ASTARTE_MAPPING_H_ */
Loading

0 comments on commit 8dda144

Please sign in to comment.