perf: updated error checks

This commit is contained in:
aZholtikov committed 2026-10-04 13:00:19 +03:00
1 parent a39d9d40cf
commit 15a9db7342
3 files changed
+48 -34

No files matched your search

+5 -8
View File
@@ -1,9 +1,5 @@
# ESP32 ESP-IDF component for Syslog UDP client # ESP32 ESP-IDF component for Syslog UDP client
## SAST Tools
[PVS-Studio](https://pvs-studio.com/pvs-studio/?utm_source=website&utm_medium=github&utm_campaign=open_source) — static analyzer for C, C++, C#, and Java code.
## Features ## Features
1. Support for all standard syslog facility codes (0-23) per RFC 5424. 1. Support for all standard syslog facility codes (0-23) per RFC 5424.
@@ -51,11 +47,11 @@ Messages are built using the format defined in [RFC 5424](https://datatracker.ie
#define SYSLOG_IP "8.8.8.8" #define SYSLOG_IP "8.8.8.8"
esp_timer_handle_t wifi_reconnect_timer = {0}; esp_timer_handle_t wifi_reconnect_timer = NULL;
uint8_t wifi_reconnect_retry_num = 0; uint8_t wifi_reconnect_retry_num = 0;
EventGroupHandle_t event_group_handle = {0}; EventGroupHandle_t event_group_handle = NULL;
bool flag = false; volatile bool flag = false;
void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
@@ -75,8 +71,8 @@ void app_main(void)
esp_wifi_set_config(WIFI_IF_STA, &wifi_config); esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL); esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL);
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, NULL); esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, NULL);
esp_wifi_start();
event_group_handle = xEventGroupCreate(); event_group_handle = xEventGroupCreate();
esp_wifi_start();
xEventGroupWaitBits(event_group_handle, WIFI_CONNECT_SUCCESS, pdTRUE, pdTRUE, portMAX_DELAY); xEventGroupWaitBits(event_group_handle, WIFI_CONNECT_SUCCESS, pdTRUE, pdTRUE, portMAX_DELAY);
zh_syslog_init_config_t config = ZH_SYSLOG_INIT_CONFIG_DEFAULT(); zh_syslog_init_config_t config = ZH_SYSLOG_INIT_CONFIG_DEFAULT();
strcpy(config.syslog_ip, SYSLOG_IP); strcpy(config.syslog_ip, SYSLOG_IP);
@@ -121,6 +117,7 @@ void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id
{ {
esp_timer_stop(wifi_reconnect_timer); esp_timer_stop(wifi_reconnect_timer);
esp_timer_delete(wifi_reconnect_timer); esp_timer_delete(wifi_reconnect_timer);
wifi_reconnect_timer = NULL;
} }
esp_timer_create_args_t wifi_reconnect_timer_args = { esp_timer_create_args_t wifi_reconnect_timer_args = {
.callback = (void *)esp_wifi_connect, .callback = (void *)esp_wifi_connect,
+12 -13
View File
@@ -10,8 +10,8 @@
* - Support for all standard syslog facility codes (0-23) * - Support for all standard syslog facility codes (0-23)
* - Support for all standard syslog severity levels (0-7) * - Support for all standard syslog severity levels (0-7)
* - RFC 5424 format message generation * - RFC 5424 format message generation
* - Thread-safe initialization and deinitialization * - Short messages are formatted into a stack buffer, longer ones fall back
* - MALLOC_CAP_8BIT memory allocation for DMA compatibility * to a heap allocation with the exact required size
* *
* @note Connection to the network must be established before calling zh_syslog_init(). * @note Connection to the network must be established before calling zh_syslog_init().
* @warning This library does not guarantee message delivery (UDP is connectionless). * @warning This library does not guarantee message delivery (UDP is connectionless).
@@ -19,17 +19,15 @@
#pragma once #pragma once
#include "stdio.h"
#include "string.h"
#include "stdint.h"
#include "stdbool.h" #include "stdbool.h"
#include "esp_heap_caps.h"
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
#include "lwip/err.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
/**
* @brief Default syslog client initialization configuration.
*/
#define ZH_SYSLOG_INIT_CONFIG_DEFAULT() \ #define ZH_SYSLOG_INIT_CONFIG_DEFAULT() \
{ \ { \
.syslog_ip = "192.168.0.1", \ .syslog_ip = "192.168.0.1", \
@@ -116,10 +114,11 @@ extern "C"
* Creates a UDP socket and configures connection parameters * Creates a UDP socket and configures connection parameters
* for the remote syslog server. * for the remote syslog server.
* *
* @param[in] config Pointer to syslog client initialization configuration structure. Can point to a temporary variable (must not be NULL) * @param[in] config Pointer to syslog client initialization configuration structure (must not be NULL)
* *
* @return ESP_OK on success * @return ESP_OK on success
* @return ESP_ERR_INVALID_ARG if config is NULL * @return ESP_ERR_INVALID_ARG if any parameter is NULL, port is out of range, or IP is malformed
* @return ESP_ERR_INVALID_STATE if client is already initialized
* @return ESP_FAIL if socket creation fails * @return ESP_FAIL if socket creation fails
* *
* @note Connection to the network must be established before calling this function. * @note Connection to the network must be established before calling this function.
@@ -147,12 +146,12 @@ extern "C"
* *
* @param[in] facility Syslog facility code (0-23) * @param[in] facility Syslog facility code (0-23)
* @param[in] severity Syslog severity code (0-7) * @param[in] severity Syslog severity code (0-7)
* @param[in] hostname Pointer to device hostname that generated the message. Only alphanumeric characters without spaces (must not be NULL) * @param[in] hostname Pointer to device hostname that generated the message (must not be NULL)
* @param[in] app_name Pointer to application name that generated the message. Only alphanumeric characters without spaces (must not be NULL) * @param[in] app_name Pointer to application name that generated the message (must not be NULL)
* @param[in] message Pointer to the message payload to send (must not be NULL) * @param[in] message Pointer to the message payload to send (must not be NULL)
* *
* @return ESP_OK on success * @return ESP_OK on success
* @return ESP_ERR_INVALID_ARG if hostname, app_name, or message is NULL * @return ESP_ERR_INVALID_ARG if hostname, app_name, or message is NULL, or facility/severity is out of range
* @return ESP_ERR_INVALID_STATE if zh_syslog_init() was not called previously * @return ESP_ERR_INVALID_STATE if zh_syslog_init() was not called previously
* @return ESP_ERR_NO_MEM if memory allocation fails * @return ESP_ERR_NO_MEM if memory allocation fails
* @return ESP_FAIL if UDP send operation fails * @return ESP_FAIL if UDP send operation fails
+31 -13
View File
@@ -13,19 +13,22 @@
static const char *TAG = "zh_syslog"; static const char *TAG = "zh_syslog";
static zh_syslog_init_config_t _init_config = {0}; /*!< Syslog client initialization configuration (internal copy) */ static struct sockaddr_in _syslog_server = {0};
static struct sockaddr_in _syslog_server = {0}; /*!< Pre-configured syslog server address */ volatile static int _socket = -1;
volatile static int _socket = 0; /*!< UDP socket file descriptor */ volatile static bool _is_initialized = false;
volatile static bool _is_initialized = false; /*!< Initialization status flag */
esp_err_t zh_syslog_init(const zh_syslog_init_config_t *config) esp_err_t zh_syslog_init(const zh_syslog_init_config_t *config)
{ {
ESP_LOGI(TAG, "Syslog client initialization begin."); ESP_LOGI(TAG, "Syslog client initialization begin.");
ZH_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "Syslog client initialization fail. Invalid argument."); ZH_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "Syslog client initialization fail. Invalid argument.");
_init_config = *config; ZH_ERROR_CHECK(_is_initialized == false, ESP_ERR_INVALID_STATE, NULL, "Syslog client initialization fail. Syslog client already initialized.");
_syslog_server.sin_addr.s_addr = inet_addr(_init_config.syslog_ip); ZH_ERROR_CHECK(config->syslog_port != 0 && config->syslog_port <= 65535, ESP_ERR_INVALID_ARG, NULL, "Syslog client initialization fail. Invalid argument.");
ZH_ERROR_CHECK(memchr(config->syslog_ip, '\0', sizeof(config->syslog_ip)) != NULL, ESP_ERR_INVALID_ARG, NULL, "Syslog client initialization fail. Invalid IP.");
struct in_addr addr;
ZH_ERROR_CHECK(inet_pton(AF_INET, config->syslog_ip, &addr) == 1, ESP_ERR_INVALID_ARG, NULL, "Syslog client initialization fail. Invalid argument.");
_syslog_server.sin_addr = addr;
_syslog_server.sin_family = AF_INET; _syslog_server.sin_family = AF_INET;
_syslog_server.sin_port = htons(_init_config.syslog_port); _syslog_server.sin_port = htons((uint16_t)config->syslog_port);
_socket = socket(AF_INET, SOCK_DGRAM, 0); _socket = socket(AF_INET, SOCK_DGRAM, 0);
ZH_ERROR_CHECK(_socket >= 0, ESP_FAIL, NULL, "Syslog client initialization fail. Create socket error."); ZH_ERROR_CHECK(_socket >= 0, ESP_FAIL, NULL, "Syslog client initialization fail. Create socket error.");
_is_initialized = true; _is_initialized = true;
@@ -37,24 +40,39 @@ esp_err_t zh_syslog_deinit(void)
{ {
ESP_LOGI(TAG, "Syslog client deinitialization begin."); ESP_LOGI(TAG, "Syslog client deinitialization begin.");
ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "Syslog client deinitialization fail. Syslog client not initialized."); ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "Syslog client deinitialization fail. Syslog client not initialized.");
shutdown(_socket, 0);
close(_socket); close(_socket);
_socket = -1;
_is_initialized = false; _is_initialized = false;
ESP_LOGI(TAG, "Syslog client deinitialization success."); ESP_LOGI(TAG, "Syslog client deinitialization success.");
return ESP_OK; return ESP_OK;
} }
esp_err_t zh_syslog_send(const zh_syslog_facility_code_t facility, const zh_syslog_severity_code_t severity, const char *hostname, const char *app_name, const char *message) // -V2008 esp_err_t zh_syslog_send(const zh_syslog_facility_code_t facility, const zh_syslog_severity_code_t severity, const char *hostname, const char *app_name, const char *message)
{ {
ESP_LOGI(TAG, "Syslog client send begin."); ESP_LOGI(TAG, "Syslog client send begin.");
ZH_ERROR_CHECK(hostname != NULL && app_name != NULL && message != NULL, ESP_ERR_INVALID_ARG, NULL, "Syslog client send fail. Invalid argument."); ZH_ERROR_CHECK(hostname != NULL && app_name != NULL && message != NULL, ESP_ERR_INVALID_ARG, NULL, "Syslog client send fail. Invalid argument.");
ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "Syslog client send fail. Syslog client not initialized."); ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "Syslog client send fail. Syslog client not initialized.");
char *payload = heap_caps_calloc(1, 1024, MALLOC_CAP_8BIT); ZH_ERROR_CHECK(facility >= ZH_KERN && facility <= ZH_LOCAL7 && severity >= ZH_EMERG && severity <= ZH_DEBUG, ESP_ERR_INVALID_ARG, NULL, "Syslog client send fail. Invalid argument.");
const int pri = ((uint8_t)facility * 8) + (uint8_t)severity;
char stack_buf[256];
int len = snprintf(stack_buf, sizeof(stack_buf), "<%d>1 - %s %s - - - %s", pri, hostname, app_name, message);
ZH_ERROR_CHECK(len >= 0, ESP_FAIL, NULL, "Syslog client send fail. Format error.");
char *payload = stack_buf;
bool heap_allocated = false;
if (len >= sizeof(stack_buf))
{
payload = heap_caps_calloc(1, len + 1, MALLOC_CAP_8BIT);
ZH_ERROR_CHECK(payload != NULL, ESP_ERR_NO_MEM, NULL, "Syslog client send fail. Memory allocation fail or no free memory in the heap."); ZH_ERROR_CHECK(payload != NULL, ESP_ERR_NO_MEM, NULL, "Syslog client send fail. Memory allocation fail or no free memory in the heap.");
sprintf(payload, "<%d>1 - %s %s - - - %s", ((uint8_t)facility * 8) + (uint8_t)severity, hostname, app_name, message); int written = snprintf(payload, len + 1, "<%d>1 - %s %s - - - %s", pri, hostname, app_name, message);
int temp = sendto(_socket, payload, strlen(payload), 0, (struct sockaddr *)&_syslog_server, sizeof(_syslog_server)); // -V641 ZH_ERROR_CHECK(written == len, ESP_FAIL, heap_caps_free(payload), "Syslog client send fail. Format error.");
ZH_ERROR_CHECK(temp >= 0, ESP_FAIL, heap_caps_free(payload), "Syslog client send fail. Socket error."); heap_allocated = true;
}
int sent = sendto(_socket, payload, len, 0, (struct sockaddr *)&_syslog_server, sizeof(_syslog_server));
if (heap_allocated == true)
{
heap_caps_free(payload); heap_caps_free(payload);
}
ZH_ERROR_CHECK(sent == len, ESP_FAIL, NULL, "Syslog client send fail. Socket error.");
ESP_LOGI(TAG, "Syslog client send success."); ESP_LOGI(TAG, "Syslog client send success.");
return ESP_OK; return ESP_OK;
} }