Skip to content

Commit

Permalink
Checks on NULL parameters
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 Dec 12, 2024
1 parent 3b6a857 commit 678636b
Showing 1 changed file with 81 additions and 13 deletions.
94 changes: 81 additions & 13 deletions lib/astarte_device_sdk/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ ASTARTE_LOG_MODULE_REGISTER(astarte_device, CONFIG_ASTARTE_DEVICE_SDK_DEVICE_LOG
* Static functions declaration *
***********************************************/

/**
* @brief Initialize the device introspection.
*
* @param[in] device Handle to the device instance.
* @param[in] interfaces A list of pointers to interfaces.
* @param[in] interfaces_size Number of interfaces in the list.
* @return ASTARTE_RESULT_OK on success, an error code otherwise.
*/
static astarte_result_t initialize_introspection(
astarte_device_handle_t device, const astarte_interface_t **interfaces, size_t interfaces_size);
/**
* @brief Initialize MQTT topics.
*
* @param[in] device Handle to the device instance.
* @return ASTARTE_RESULT_OK if publish has been successful, an error code otherwise.
* @return ASTARTE_RESULT_OK on success, an error code otherwise.
*/
static astarte_result_t initialize_mqtt_topics(astarte_device_handle_t device);

Expand Down Expand Up @@ -83,8 +93,15 @@ static astarte_result_t refresh_client_cert_handler(astarte_mqtt_t *astarte_mqtt
astarte_result_t astarte_device_new(astarte_device_config_t *cfg, astarte_device_handle_t *device)
{
astarte_result_t ares = ASTARTE_RESULT_OK;
astarte_device_handle_t handle = NULL;

if (!cfg || !device) {
ASTARTE_LOG_ERR("Received NULL reference for configuration or device handle");
ares = ASTARTE_RESULT_INVALID_PARAM;
goto failure;
}

astarte_device_handle_t handle = calloc(1, sizeof(struct astarte_device));
handle = calloc(1, sizeof(struct astarte_device));
if (!handle) {
ASTARTE_LOG_ERR("Out of memory %s: %d", __FILE__, __LINE__);
ares = ASTARTE_RESULT_OUT_OF_MEMORY;
Expand Down Expand Up @@ -115,20 +132,10 @@ astarte_result_t astarte_device_new(astarte_device_config_t *cfg, astarte_device
handle->connection_state = DEVICE_DISCONNECTED;

ASTARTE_LOG_DBG("Initializing introspection");
ares = introspection_init(&handle->introspection);
ares = initialize_introspection(handle, cfg->interfaces, cfg->interfaces_size);
if (ares != ASTARTE_RESULT_OK) {
ASTARTE_LOG_ERR("Introspection initialization failure %s.", astarte_result_to_name(ares));
goto failure;
}
if (cfg->interfaces) {
for (size_t i = 0; i < cfg->interfaces_size; i++) {
ares = introspection_add(&handle->introspection, cfg->interfaces[i]);
if (ares != ASTARTE_RESULT_OK) {
ASTARTE_LOG_ERR("Introspection add failure %s.", astarte_result_to_name(ares));
goto failure;
}
}
}

ares = initialize_mqtt_topics(handle);
if (ares != ASTARTE_RESULT_OK) {
Expand Down Expand Up @@ -192,6 +199,11 @@ astarte_result_t astarte_device_new(astarte_device_config_t *cfg, astarte_device
astarte_result_t astarte_device_destroy(astarte_device_handle_t device)
{
astarte_result_t ares = ASTARTE_RESULT_OK;

if (!device) {
return ASTARTE_RESULT_OK;
}

if (device->connection_state != DEVICE_DISCONNECTED) {
ares = astarte_device_disconnect(device);
if (ares != ASTARTE_RESULT_OK) {
Expand All @@ -215,28 +227,48 @@ astarte_result_t astarte_device_destroy(astarte_device_handle_t device)
astarte_result_t astarte_device_add_interface(
astarte_device_handle_t device, const astarte_interface_t *interface)
{
if (!device || !interface) {
ASTARTE_LOG_ERR("Received NULL reference for device handle or interface");
return ASTARTE_RESULT_INVALID_PARAM;
}
return introspection_update(&device->introspection, interface);
}

astarte_result_t astarte_device_connect(astarte_device_handle_t device)
{
if (!device) {
ASTARTE_LOG_ERR("Received NULL reference for device handle");
return ASTARTE_RESULT_INVALID_PARAM;
}
return astarte_device_connection_connect(device);
}

astarte_result_t astarte_device_disconnect(astarte_device_handle_t device)
{
if (!device) {
ASTARTE_LOG_ERR("Received NULL reference for device handle");
return ASTARTE_RESULT_INVALID_PARAM;
}
return astarte_device_connection_disconnect(device);
}

astarte_result_t astarte_device_poll(astarte_device_handle_t device)
{
if (!device) {
ASTARTE_LOG_ERR("Received NULL reference for device handle");
return ASTARTE_RESULT_INVALID_PARAM;
}
return astarte_device_connection_poll(device);
}

astarte_result_t astarte_device_stream_individual(astarte_device_handle_t device,
const char *interface_name, const char *path, astarte_individual_t individual,
const int64_t *timestamp)
{
if (!device || !interface_name || !path) {
ASTARTE_LOG_ERR("Received a NULL reference for a required input parameter.");
return ASTARTE_RESULT_INVALID_PARAM;
}
if (device->connection_state != DEVICE_CONNECTED) {
ASTARTE_LOG_ERR("Called stream individual function when the device is not connected.");
return ASTARTE_RESULT_DEVICE_NOT_READY;
Expand All @@ -249,6 +281,10 @@ astarte_result_t astarte_device_stream_aggregated(astarte_device_handle_t device
const char *interface_name, const char *path, astarte_object_entry_t *entries,
size_t entries_len, const int64_t *timestamp)
{
if (!device || !interface_name || !path || !entries) {
ASTARTE_LOG_ERR("Received a NULL reference for a required input parameter.");
return ASTARTE_RESULT_INVALID_PARAM;
}
if (device->connection_state != DEVICE_CONNECTED) {
ASTARTE_LOG_ERR("Called stream aggregated function when the device is not connected.");
return ASTARTE_RESULT_DEVICE_NOT_READY;
Expand All @@ -261,6 +297,10 @@ astarte_result_t astarte_device_stream_aggregated(astarte_device_handle_t device
astarte_result_t astarte_device_set_property(astarte_device_handle_t device,
const char *interface_name, const char *path, astarte_individual_t individual)
{
if (!device || !interface_name || !path) {
ASTARTE_LOG_ERR("Received a NULL reference for a required input parameter.");
return ASTARTE_RESULT_INVALID_PARAM;
}
if (device->connection_state != DEVICE_CONNECTED) {
ASTARTE_LOG_ERR("Called set property function when the device is not connected.");
return ASTARTE_RESULT_DEVICE_NOT_READY;
Expand All @@ -272,6 +312,10 @@ astarte_result_t astarte_device_set_property(astarte_device_handle_t device,
astarte_result_t astarte_device_unset_property(
astarte_device_handle_t device, const char *interface_name, const char *path)
{
if (!device || !interface_name || !path) {
ASTARTE_LOG_ERR("Received a NULL reference for a required input parameter.");
return ASTARTE_RESULT_INVALID_PARAM;
}
if (device->connection_state != DEVICE_CONNECTED) {
ASTARTE_LOG_ERR("Called unset property function when the device is not connected.");
return ASTARTE_RESULT_DEVICE_NOT_READY;
Expand All @@ -285,6 +329,10 @@ astarte_result_t astarte_device_get_property(astarte_device_handle_t device,
const char *interface_name, const char *path, astarte_device_property_loader_cbk_t loader_cbk,
void *user_data)
{
if (!device || !interface_name || !path || !loader_cbk) {
ASTARTE_LOG_ERR("Received a NULL reference for a required input parameter.");
return ASTARTE_RESULT_INVALID_PARAM;
}
astarte_result_t ares = ASTARTE_RESULT_OK;
astarte_individual_t individual = { 0 };
uint32_t out_major = 0U;
Expand Down Expand Up @@ -312,6 +360,26 @@ astarte_result_t astarte_device_get_property(astarte_device_handle_t device,
* Static functions definitions *
***********************************************/

static astarte_result_t initialize_introspection(
astarte_device_handle_t device, const astarte_interface_t **interfaces, size_t interfaces_size)
{
astarte_result_t ares = introspection_init(&device->introspection);
if (ares != ASTARTE_RESULT_OK) {
ASTARTE_LOG_ERR("Introspection initialization failure %s.", astarte_result_to_name(ares));
return ares;
}
if (interfaces) {
for (size_t i = 0; i < interfaces_size; i++) {
ares = introspection_add(&device->introspection, interfaces[i]);
if (ares != ASTARTE_RESULT_OK) {
ASTARTE_LOG_ERR("Introspection add failure %s.", astarte_result_to_name(ares));
return ares;
}
}
}
return ASTARTE_RESULT_OK;
}

static astarte_result_t initialize_mqtt_topics(astarte_device_handle_t device)
{
int snprintf_rc = snprintf(
Expand Down

0 comments on commit 678636b

Please sign in to comment.