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:
parent
589b41eb01
commit
5f466b2af1
|
|
@ -24,8 +24,6 @@
|
|||
|
||||
#include "device_request.h"
|
||||
|
||||
#define MAX_RESPONSE_SIZE 256
|
||||
|
||||
#if !(defined UNUSED_ARGUMENT)
|
||||
#define UNUSED_ARGUMENT(a) (void)(a)
|
||||
#endif
|
||||
|
|
@ -35,29 +33,33 @@
|
|||
*
|
||||
* @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.
|
||||
* @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;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -65,7 +67,7 @@ void get_time_cb(char const *const target,
|
|||
*
|
||||
* @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.
|
||||
* @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
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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_ */
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,42 +75,16 @@ static ccapi_timestamp_t *get_timestamp(void)
|
|||
return timestamp;
|
||||
}
|
||||
|
||||
ccapi_dp_error_t init_data_stream(ccapi_dp_collection_handle_t *dp_collection)
|
||||
/*
|
||||
* free_timestamp() - Free given timestamp structure
|
||||
*
|
||||
* @timestamp: The timestamp structure to release.
|
||||
*/
|
||||
static void free_timestamp(ccapi_timestamp_t *timestamp)
|
||||
{
|
||||
ccapi_dp_collection_handle_t collection;
|
||||
ccapi_dp_error_t dp_error;
|
||||
if (timestamp == NULL)
|
||||
return;
|
||||
|
||||
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);
|
||||
return dp_error;
|
||||
} else {
|
||||
*dp_collection = 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);
|
||||
}
|
||||
|
||||
return dp_error;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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 (timestamp != NULL) {
|
||||
if (timestamp->iso8601 != NULL) {
|
||||
free((char *) timestamp->iso8601);
|
||||
timestamp->iso8601 = NULL;
|
||||
|
|
@ -119,6 +93,40 @@ ccapi_dp_error_t add_data_point(ccapi_dp_collection_handle_t dp_collection)
|
|||
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_create_collection(&collection);
|
||||
|
||||
if (dp_error != CCAPI_DP_ERROR_NONE) {
|
||||
log_error("%s: error %d", __func__, dp_error);
|
||||
return dp_error;
|
||||
} else {
|
||||
*dp_collection = 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("%s: error %d", __func__, dp_error);
|
||||
ccapi_dp_destroy_collection(collection);
|
||||
*dp_collection = NULL;
|
||||
}
|
||||
|
||||
return dp_error;
|
||||
}
|
||||
|
||||
ccapi_dp_error_t add_data_point(ccapi_dp_collection_handle_t dp_collection)
|
||||
{
|
||||
ccapi_timestamp_t *timestamp = get_timestamp();
|
||||
ccapi_dp_error_t dp_error = ccapi_dp_add(dp_collection, STREAM_NAME,
|
||||
get_incremental(), timestamp);
|
||||
|
||||
if (dp_error != CCAPI_DP_ERROR_NONE)
|
||||
log_error("%s: failed with error: %d", __func__, dp_error);
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue