dey-examples: cloudconnector: fix some minor issues

* Remove 'MAX_RESPONSE_SIZE' define and allocate required memory in
  'device_request_listener' example.
* Create 'free_timestamp()' function in 'upload_data_points' example.
* Use some sorter variable names.
* Use '__func__' to log function names.
* Remove line feed from log messages.
* Remove not required curly braces for single line loops.

Signed-off-by: Tatiana Leon <Tatiana.Leon@digi.com>
This commit is contained in:
Tatiana Leon 2023-05-05 11:31:26 +02:00
parent 589b41eb01
commit 5f466b2af1
6 changed files with 81 additions and 71 deletions

View File

@ -24,8 +24,6 @@
#include "device_request.h"
#define MAX_RESPONSE_SIZE 256
#if !(defined UNUSED_ARGUMENT)
#define UNUSED_ARGUMENT(a) (void)(a)
#endif
@ -33,40 +31,44 @@
/*
* get_time_cb() - Data callback for 'get_time' device requests
*
* @target: Target ID of the device request (get_time).
* @transport: Communication transport used by the device request.
* @request_buffer_info: Buffer containing the device request.
* @response_buffer_info: Buffer to store the answer of the request.
* @target: Target ID of the device request (get_time).
* @transport: Communication transport used by the device request.
* @req_buf_info: Buffer containing the device request.
* @resp_buf_info: Buffer to store the answer of the request.
*
* Logs information about the received request and executes the corresponding
* command.
*/
void get_time_cb(char const *const target,
ccapi_transport_t const transport,
ccapi_buffer_info_t const *const request_buffer_info,
ccapi_buffer_info_t *const response_buffer_info)
ccapi_buffer_info_t const *const req_buf_info,
ccapi_buffer_info_t *const resp_buf_info)
{
UNUSED_ARGUMENT(request_buffer_info);
log_debug("get_time_cb(): target='%s' - transport='%d'", target, transport);
time_t t = time(NULL);
char *time_str = ctime(&t);
response_buffer_info->buffer = malloc(sizeof(char) * MAX_RESPONSE_SIZE + 1);
if (response_buffer_info->buffer == NULL) {
log_error("%s\n", "get_time_cb(): response_buffer_info malloc error");
UNUSED_ARGUMENT(req_buf_info);
log_debug("%s: target='%s' - transport='%d'", __func__, target, transport);
resp_buf_info->length = snprintf(NULL, 0, "Time: %s", time_str);
resp_buf_info->buffer = calloc(resp_buf_info->length + 1, sizeof(char));
if (resp_buf_info->buffer == NULL) {
log_error("%s: resp_buf_info calloc error", __func__);
return;
}
time_t t = time(NULL);
response_buffer_info->length = snprintf(response_buffer_info->buffer,
MAX_RESPONSE_SIZE, "Time: %s", ctime(&t));
resp_buf_info->length = sprintf(resp_buf_info->buffer, "Time: %s", time_str);
return;
}
/*
* get_time_status_cb() - Status callback for 'get_time' device requests
*
* @target: Target ID of the device request (get_time)
* @transport: Communication transport used by the device request.
* @response_buffer_info: Buffer containing the response data.
* @receive_error: The error status of the receive process.
* @target: Target ID of the device request (get_time)
* @transport: Communication transport used by the device request.
* @resp_buf_info: Buffer containing the response data.
* @receive_error: The error status of the receive process.
*
* This callback is executed when the response process has finished. It doesn't
* matter if everything worked or there was an error during the process.
@ -75,14 +77,13 @@ void get_time_cb(char const *const target,
*/
void get_time_status_cb(char const *const target,
ccapi_transport_t const transport,
ccapi_buffer_info_t *const response_buffer_info,
ccapi_buffer_info_t *const resp_buf_info,
ccapi_receive_error_t receive_error)
{
log_debug(
"get_time_status_cb(): target='%s' - transport='%d' - error='%d'",
target, transport, receive_error);
log_debug("%s: target='%s' - transport='%d' - error='%d'",
__func__, target, transport, receive_error);
/* Free the response buffer */
if (response_buffer_info != NULL)
free(response_buffer_info->buffer);
if (resp_buf_info != NULL)
free(resp_buf_info->buffer);
}

View File

@ -25,11 +25,11 @@
#define TARGET_GET_TIME "get_time"
void get_time_cb(char const *const target, ccapi_transport_t const transport,
ccapi_buffer_info_t const *const request_buffer_info,
ccapi_buffer_info_t *const response_buffer_info);
ccapi_buffer_info_t const *const req_buf_info,
ccapi_buffer_info_t *const resp_buf_info);
void get_time_status_cb(char const *const target,
ccapi_transport_t const transport,
ccapi_buffer_info_t *const response_buffer_info,
ccapi_buffer_info_t *const resp_buf_info,
ccapi_receive_error_t receive_error);
#endif /* DEVICE_REQUEST_H_ */

View File

@ -27,7 +27,8 @@
static void sigint_handler(int signum)
{
log_debug("sigint_handler(): received signal %d to close Cloud connection.\n", signum);
log_debug("%s: received signal %d to close Cloud connection.",
__func__, signum);
exit(0);
}
@ -81,20 +82,20 @@ int main(void)
init_error = init_cloud_connection(NULL);
if (init_error != CC_INIT_ERROR_NONE) {
log_error("Cannot initialize cloud connection, error %d\n", init_error);
log_error("Cannot initialize cloud connection, error %d", init_error);
return EXIT_FAILURE;
}
start_error = start_cloud_connection();
if (start_error != CC_START_ERROR_NONE) {
log_error("Cannot start cloud connection, error %d\n", start_error);
log_error("Cannot start cloud connection, error %d", start_error);
return EXIT_FAILURE;
}
receive_error = ccapi_receive_add_target(TARGET_GET_TIME, get_time_cb,
get_time_status_cb, 0);
if (receive_error != CCAPI_RECEIVE_ERROR_NONE) {
log_error("Cannot register target '%s', error %d\n", TARGET_GET_TIME,
log_error("Cannot register target '%s', error %d", TARGET_GET_TIME,
receive_error);
return EXIT_FAILURE;
}

View File

@ -37,7 +37,7 @@ static int get_incremental(void)
else
incremental++;
log_debug("Incremental = %d\n", incremental);
log_debug("Incremental = %d", incremental);
return incremental;
}
@ -75,14 +75,31 @@ static ccapi_timestamp_t *get_timestamp(void)
return timestamp;
}
/*
* free_timestamp() - Free given timestamp structure
*
* @timestamp: The timestamp structure to release.
*/
static void free_timestamp(ccapi_timestamp_t *timestamp)
{
if (timestamp == NULL)
return;
if (timestamp->iso8601 != NULL) {
free((char *) timestamp->iso8601);
timestamp->iso8601 = NULL;
}
free(timestamp);
timestamp = NULL;
}
ccapi_dp_error_t init_data_stream(ccapi_dp_collection_handle_t *dp_collection)
{
ccapi_dp_collection_handle_t collection;
ccapi_dp_error_t dp_error;
ccapi_dp_error_t dp_error = ccapi_dp_create_collection(&collection);
dp_error = ccapi_dp_create_collection(&collection);
if (dp_error != CCAPI_DP_ERROR_NONE) {
log_error("ccapi_dp_create_collection() error %d\n", dp_error);
log_error("%s: error %d", __func__, dp_error);
return dp_error;
} else {
*dp_collection = collection;
@ -91,9 +108,9 @@ ccapi_dp_error_t init_data_stream(ccapi_dp_collection_handle_t *dp_collection)
dp_error = ccapi_dp_add_data_stream_to_collection_extra(collection,
STREAM_NAME, "int32 ts_iso", "counts", NULL);
if (dp_error != CCAPI_DP_ERROR_NONE) {
log_error("ccapi_dp_add_data_stream_to_collection_extra() error %d\n",
dp_error);
free(collection);
log_error("%s: error %d", __func__, dp_error);
ccapi_dp_destroy_collection(collection);
*dp_collection = NULL;
}
return dp_error;
@ -101,23 +118,14 @@ ccapi_dp_error_t init_data_stream(ccapi_dp_collection_handle_t *dp_collection)
ccapi_dp_error_t add_data_point(ccapi_dp_collection_handle_t dp_collection)
{
ccapi_dp_error_t dp_error;
ccapi_timestamp_t *timestamp = get_timestamp();
ccapi_dp_error_t dp_error = ccapi_dp_add(dp_collection, STREAM_NAME,
get_incremental(), timestamp);
dp_error = ccapi_dp_add(dp_collection, STREAM_NAME, get_incremental(), timestamp);
if (dp_error != CCAPI_DP_ERROR_NONE) {
log_error("ccapi_dp_add() failed with error: %d\n", dp_error);
}
if (dp_error != CCAPI_DP_ERROR_NONE)
log_error("%s: failed with error: %d", __func__, dp_error);
if (timestamp != NULL) {
if (timestamp->iso8601 != NULL) {
free((char *) timestamp->iso8601);
timestamp->iso8601 = NULL;
}
free(timestamp);
timestamp = NULL;
}
free_timestamp(timestamp);
return dp_error;
}
@ -126,18 +134,17 @@ ccapi_dp_error_t send_data_stream(ccapi_dp_collection_handle_t dp_collection)
{
ccapi_dp_error_t dp_error;
log_debug("%s", "Sending Data Stream with new incremental value\n");
log_debug("%s", "Sending Data Stream with new incremental value");
dp_error = ccapi_dp_send_collection(CCAPI_TRANSPORT_TCP, dp_collection);
if (dp_error != CCAPI_DP_ERROR_NONE) {
log_error("ccapi_dp_send_collection() error %d\n", dp_error);
}
if (dp_error != CCAPI_DP_ERROR_NONE)
log_error("%s: error %d", __func__, dp_error);
return dp_error;
}
ccapi_dp_error_t destroy_data_stream(ccapi_dp_collection_handle_t dp_collection)
{
log_debug("%s", "Destroying Data Stream\n");
log_debug("%s", "Destroying Data Stream");
return ccapi_dp_destroy_collection(dp_collection);
}

View File

@ -32,16 +32,16 @@ static ccapi_dp_collection_handle_t dp_collection;
static void sigint_handler(int signum)
{
log_debug("sigint_handler(): received signal %d to close Cloud connection.\n", signum);
log_debug("%s: received signal %d to close Cloud connection.",
__func__, signum);
exit(0);
}
static void graceful_shutdown(void)
{
if (running == 1) {
if (running == 1)
destroy_data_stream(dp_collection);
}
running = 0;
stop_cloud_connection();
@ -76,19 +76,19 @@ int main(void)
init_error = init_cloud_connection(NULL);
if (init_error != CC_INIT_ERROR_NONE) {
log_error("Cannot initialize cloud connection, error %d\n", init_error);
log_error("Cannot initialize cloud connection, error %d", init_error);
return EXIT_FAILURE;
}
start_error = start_cloud_connection();
if (start_error != CC_START_ERROR_NONE) {
log_error("Cannot start cloud connection, error %d\n", start_error);
log_error("Cannot start cloud connection, error %d", start_error);
return EXIT_FAILURE;
}
dp_error = init_data_stream(&dp_collection);
if (dp_error != CCAPI_DP_ERROR_NONE) {
log_error("Cannot initialize data stream, error %d\n", start_error);
log_error("Cannot initialize data stream, error %d", start_error);
return EXIT_FAILURE;
}
@ -100,7 +100,7 @@ int main(void)
dp_error = add_data_point(dp_collection);
if (dp_error != CCAPI_DP_ERROR_NONE) {
log_error("Cannot add data point, error %d\n", start_error);
log_error("Cannot add data point, error %d", start_error);
i--;
}
@ -110,7 +110,7 @@ int main(void)
/* Send the block of collected data points */
dp_error = send_data_stream(dp_collection);
if (dp_error != CCAPI_DP_ERROR_NONE)
log_error("Cannot send data stream, error %d\n", start_error);
log_error("Cannot send data stream, error %d", start_error);
}
return EXIT_SUCCESS;

View File

@ -29,7 +29,8 @@
static void sigint_handler(int signum)
{
log_debug("sigint_handler(): received signal %d to close Cloud connection.\n", signum);
log_debug("%s: received signal %d to close Cloud connection.",
__func__, signum);
exit(0);
}
@ -68,19 +69,19 @@ int main(int argc, char *argv[])
init_error = init_cloud_connection(NULL);
if (init_error != CC_INIT_ERROR_NONE) {
log_error("Cannot initialize cloud connection, error %d\n", init_error);
log_error("Cannot initialize cloud connection, error %d", init_error);
return EXIT_FAILURE;
}
start_error = start_cloud_connection();
if (start_error != CC_START_ERROR_NONE) {
log_error("Cannot start cloud connection, error %d\n", start_error);
log_error("Cannot start cloud connection, error %d", start_error);
return EXIT_FAILURE;
}
send_error = ccapi_dp_binary_send_file(CCAPI_TRANSPORT_TCP, UPLOAD_FILE, STREAM_NAME);
if (send_error != CCAPI_DP_B_ERROR_NONE) {
log_error("ccapi_dp_binary_send_file() failed, error %d\n", send_error);
log_error("%s failed, error %d", __func__, send_error);
return EXIT_FAILURE;
}