From 17d1aeda18046f2b7225075d121ed54605a5adb9 Mon Sep 17 00:00:00 2001 From: Sreeharsha Ramanavarapu Date: Fri, 8 Jan 2021 09:59:17 +0530 Subject: dlt_client_main_loop running in an infinite loop restricts graceful exit of DLT Client code (#284) Issue: ------ dlt_client_main_loop currently uses an infinite loop ("while (1)"). This creates problems when a DLT client is running in a thread as part of a larger application. Graceful exit (for example: during object destruction) is not possible because a thread is running dlt_client_main_loop in the background. It is also not possible to exit the client, if we want it to fetch only a pre-decided number of messages. Solution: --------- Allow user to define a callback function to check whether the next message should be fetched. dlt-test-client.c has a new option to fetch a pre-decided number of messages, after which the client will exit. Signed-off-by: Sreeharsha Ramanavarapu --- src/lib/dlt_client.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/lib/dlt_client.c') diff --git a/src/lib/dlt_client.c b/src/lib/dlt_client.c index 0702f5f..b81d61b 100644 --- a/src/lib/dlt_client.c +++ b/src/lib/dlt_client.c @@ -97,12 +97,18 @@ #include "dlt_client_cfg.h" static int (*message_callback_function)(DltMessage *message, void *data) = NULL; +static bool (*fetch_next_message_callback_function)(void *data) = NULL; void dlt_client_register_message_callback(int (*registerd_callback)(DltMessage *message, void *data)) { message_callback_function = registerd_callback; } +void dlt_client_register_fetch_next_message_callback(bool (*registerd_callback)(void *data)) +{ + fetch_next_message_callback_function = registerd_callback; +} + DltReturnValue dlt_client_init_port(DltClient *client, int port, int verbose) { if (verbose && (port != DLT_DAEMON_TCP_PORT)) @@ -397,7 +403,8 @@ DltReturnValue dlt_client_main_loop(DltClient *client, void *data, int verbose) if (dlt_message_init(&msg, verbose) == DLT_RETURN_ERROR) return DLT_RETURN_ERROR; - while (1) { + bool fetch_next_message = true; + while (fetch_next_message) { /* wait for data from socket or serial connection */ ret = dlt_receiver_receive(&(client->receiver)); @@ -440,6 +447,8 @@ DltReturnValue dlt_client_main_loop(DltClient *client, void *data, int verbose) dlt_message_free(&msg, verbose); return DLT_RETURN_ERROR; } + if (fetch_next_message_callback_function) + fetch_next_message = (*fetch_next_message_callback_function)(data); } if (dlt_message_free(&msg, verbose) == DLT_RETURN_ERROR) -- cgit v1.2.1