You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2020/05/06 14:03:06 UTC

[plc4x] branch feature/c-api updated: - Added pointers from system-task to connection - Added a num_running_system_tasks to the connection to keep track of the active system tasks - Made the system auto decrement the num_running_system_tasks of a connection

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch feature/c-api
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/feature/c-api by this push:
     new f3abb7f  - Added pointers from system-task to connection - Added a num_running_system_tasks to the connection to keep track of the active system tasks - Made the system auto decrement the num_running_system_tasks of a connection
f3abb7f is described below

commit f3abb7f1d5ff28cb124244fb5a9e03f0656cb7ba
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Wed May 6 16:02:14 2020 +0200

    - Added pointers from system-task to connection
    - Added a num_running_system_tasks to the connection to keep track of the active system tasks
    - Made the system auto decrement the num_running_system_tasks of a connection
---
 sandbox/plc4c/drivers/modbus/src/driver_modbus.c   |  2 +-
 sandbox/plc4c/drivers/s7/src/driver_s7.c           |  2 +-
 .../plc4c/drivers/simulated/src/driver_simulated.c | 64 ++++++++++++----------
 .../plc4c/examples/hello-world/src/hello_world.c   |  5 +-
 .../plc4c/spi/include/plc4c/spi/types_private.h    | 17 +++++-
 sandbox/plc4c/spi/src/connection.c                 |  2 +
 sandbox/plc4c/spi/src/read.c                       |  4 +-
 sandbox/plc4c/spi/src/system.c                     | 20 ++++++-
 sandbox/plc4c/spi/src/write.c                      |  4 +-
 .../plc4c/transports/dummy/src/transport_dummy.c   |  2 +-
 .../plc4c/transports/serial/src/transport_serial.c |  2 +-
 sandbox/plc4c/transports/tcp/src/transport_tcp.c   |  2 +-
 12 files changed, 84 insertions(+), 42 deletions(-)

diff --git a/sandbox/plc4c/drivers/modbus/src/driver_modbus.c b/sandbox/plc4c/drivers/modbus/src/driver_modbus.c
index bf615a3..f046d5d 100644
--- a/sandbox/plc4c/drivers/modbus/src/driver_modbus.c
+++ b/sandbox/plc4c/drivers/modbus/src/driver_modbus.c
@@ -22,7 +22,7 @@
 #include <plc4c/driver_modbus.h>
 
 plc4c_driver *plc4c_driver_modbus_create() {
-    plc4c_driver* driver = (plc4c_driver*) malloc(sizeof(plc4c_driver));
+    plc4c_driver *driver = (plc4c_driver *) malloc(sizeof(plc4c_driver));
     driver->protocol_code = "modbus";
     driver->protocol_name = "Modbus";
     driver->default_transport_code = "tcp";
diff --git a/sandbox/plc4c/drivers/s7/src/driver_s7.c b/sandbox/plc4c/drivers/s7/src/driver_s7.c
index 9a9a544..739c0e2 100644
--- a/sandbox/plc4c/drivers/s7/src/driver_s7.c
+++ b/sandbox/plc4c/drivers/s7/src/driver_s7.c
@@ -22,7 +22,7 @@
 #include <plc4c/driver_s7.h>
 
 plc4c_driver *plc4c_driver_s7_create() {
-    plc4c_driver* driver = (plc4c_driver*) malloc(sizeof(plc4c_driver));
+    plc4c_driver *driver = (plc4c_driver *) malloc(sizeof(plc4c_driver));
     driver->protocol_code = "s7";
     driver->protocol_name = "Siemens S7 (Basic)";
     driver->default_transport_code = "tcp";
diff --git a/sandbox/plc4c/drivers/simulated/src/driver_simulated.c b/sandbox/plc4c/drivers/simulated/src/driver_simulated.c
index e97f9a7..b9aacee 100644
--- a/sandbox/plc4c/drivers/simulated/src/driver_simulated.c
+++ b/sandbox/plc4c/drivers/simulated/src/driver_simulated.c
@@ -146,34 +146,36 @@ plc4c_return_code plc4c_driver_simulated_write_machine_function(plc4c_system_tas
             plc4c_list_element *cur_element = plc4c_utils_list_head(write_request->items);
             while (cur_element != NULL) {
                 plc4c_request_value_item *cur_value_item = cur_element->value;
-                plc4c_driver_simulated_item *cur_item = (plc4c_driver_simulated_item*) cur_value_item->item;
-                plc4c_data * write_data = cur_value_item->value;
+                plc4c_driver_simulated_item *cur_item = (plc4c_driver_simulated_item *) cur_value_item->item;
+                plc4c_data *write_data = cur_value_item->value;
                 plc4c_response_code response_code = -1;
                 switch (cur_item->type) {
-                  case STDOUT: {
-                    printf("");
-                    if (cur_item->data_type != write_data->data_type) {
-                      printf("--> Simulated Driver Write: Value is %s but Item type is %s", plc4c_data_type_name(write_data->data_type), plc4c_data_type_name(cur_item->data_type));
-                      response_code = PLC4C_RESPONSE_CODE_INVALID_DATATYPE;
-                      break;
+                    case STDOUT: {
+                        printf("");
+                        if (cur_item->data_type != write_data->data_type) {
+                            printf("--> Simulated Driver Write: Value is %s but Item type is %s",
+                                   plc4c_data_type_name(write_data->data_type),
+                                   plc4c_data_type_name(cur_item->data_type));
+                            response_code = PLC4C_RESPONSE_CODE_INVALID_DATATYPE;
+                            break;
+                        }
+                        printf("--> Simulated Driver Write: Value (%s) %s: ",
+                               plc4c_data_type_name(write_data->data_type),
+                               cur_item->name);
+                        plc4c_data_printf(write_data);
+                        printf("\n");
+                        response_code = PLC4C_RESPONSE_CODE_OK;
+                        break;
+                    }
+                    default: {
+                        response_code = PLC4C_RESPONSE_CODE_INVALID_ADDRESS;
+                        break;
                     }
-                    printf("--> Simulated Driver Write: Value (%s) %s: ",
-                           plc4c_data_type_name(write_data->data_type),
-                           cur_item->name);
-                    plc4c_data_printf(write_data);
-                    printf("\n");
-                    response_code = PLC4C_RESPONSE_CODE_OK;
-                    break;
-                  }
-                  default: {
-                    response_code = PLC4C_RESPONSE_CODE_INVALID_ADDRESS;
-                    break;
-                  }
                 }
 
                 // Create a response element and add that to the response ...
                 plc4c_response_item *response_item = malloc(sizeof(plc4c_response_item));
-                response_item->item = (plc4c_item*) cur_item;
+                response_item->item = (plc4c_item *) cur_item;
                 response_item->response_code = response_code;
                 plc4c_utils_list_insert_tail_value(write_response->response_items, response_item);
 
@@ -229,13 +231,13 @@ plc4c_item *plc4c_driver_simulated_parse_address(char *address_string) {
         }
         // This marks the end of the data-type part if there is a size coming in addition.
         if ((i == strlen(address_string)) || (*(address_string + i) == '[')) {
-            char* datatype_name = malloc(sizeof(char) * ((i - start_segment_index) + 1));
+            char *datatype_name = malloc(sizeof(char) * ((i - start_segment_index) + 1));
             strlcpy(datatype_name, start_segment, (i - start_segment_index) + 1);
 
             // Translate the string into a constant.
-            if(strcmp(datatype_name, "INTEGER") == 0) {
+            if (strcmp(datatype_name, "INTEGER") == 0) {
                 data_type = PLC4C_INT;
-            } else if(strcmp(datatype_name, "STRING") == 0) {
+            } else if (strcmp(datatype_name, "STRING") == 0) {
                 data_type = PLC4C_CONSTANT_STRING;
             } else {
                 return NULL;
@@ -270,41 +272,47 @@ plc4c_item *plc4c_driver_simulated_parse_address(char *address_string) {
 
 
 plc4c_return_code plc4c_driver_simulated_connect_function(plc4c_connection *connection,
-                                                    plc4c_system_task **task) {
+                                                          plc4c_system_task **task) {
     plc4c_system_task *new_task = malloc(sizeof(plc4c_system_task));
     new_task->context = connection;
     // There's nothing to do here, so no need for a state-machine.
     new_task->state_id = -1;
     new_task->state_machine_function = &plc4c_driver_simulated_connect_machine_function;
+    new_task->connection = connection;
     *task = new_task;
     return OK;
 }
 
 plc4c_return_code plc4c_driver_simulated_disconnect_function(plc4c_connection *connection,
-                                                       plc4c_system_task **task) {
+                                                             plc4c_system_task **task) {
     plc4c_system_task *new_task = malloc(sizeof(plc4c_system_task));
     new_task->context = connection;
     // There's nothing to do here, so no need for a state-machine.
     new_task->state_id = -1;
     new_task->state_machine_function = &plc4c_driver_simulated_disconnect_machine_function;
+    new_task->connection = connection;
     *task = new_task;
     return OK;
 }
 
-plc4c_return_code plc4c_driver_simulated_read_function(plc4c_system_task **task) {
+plc4c_return_code plc4c_driver_simulated_read_function(plc4c_connection *connection,
+                                                       plc4c_system_task **task) {
     plc4c_system_task *new_task = malloc(sizeof(plc4c_system_task));
     new_task->state_id = READ_INIT;
     new_task->state_machine_function = &plc4c_driver_simulated_read_machine_function;
     new_task->completed = false;
+    new_task->connection = connection;
     *task = new_task;
     return OK;
 }
 
-plc4c_return_code plc4c_driver_simulated_write_function(plc4c_system_task **task) {
+plc4c_return_code plc4c_driver_simulated_write_function(plc4c_connection *connection,
+                                                        plc4c_system_task **task) {
     plc4c_system_task *new_task = malloc(sizeof(plc4c_system_task));
     new_task->state_id = WRITE_INIT;
     new_task->state_machine_function = &plc4c_driver_simulated_write_machine_function;
     new_task->completed = false;
+    new_task->connection = connection;
     *task = new_task;
     return OK;
 }
diff --git a/sandbox/plc4c/examples/hello-world/src/hello_world.c b/sandbox/plc4c/examples/hello-world/src/hello_world.c
index 718e049..d95616b 100644
--- a/sandbox/plc4c/examples/hello-world/src/hello_world.c
+++ b/sandbox/plc4c/examples/hello-world/src/hello_world.c
@@ -212,7 +212,7 @@ int main() {
                     plc4c_response_value_item *value_item = cur_element->value;
 
                     printf("Value %s (%s):", value_item->item->name,
-                            plc4c_response_code_to_message(value_item->response_code));
+                           plc4c_response_code_to_message(value_item->response_code));
                     plc4c_data_printf(value_item->value);
                     printf("\n");
 
@@ -232,7 +232,8 @@ int main() {
                 plc4c_list *value_list = NULL;
                 plc4c_utils_list_create(&value_list);
                 char value[] = "bar";
-                plc4c_utils_list_insert_head_value(value_list,  plc4c_data_create_constant_string_data(strlen(value), value));
+                plc4c_utils_list_insert_head_value(value_list,
+                                                   plc4c_data_create_constant_string_data(strlen(value), value));
                 result = plc4c_connection_create_write_request(connection, address_list, value_list, &write_request);
 
                 // As we only used these to create the request, they can now be released again.
diff --git a/sandbox/plc4c/spi/include/plc4c/spi/types_private.h b/sandbox/plc4c/spi/include/plc4c/spi/types_private.h
index 7bf7d55..fb99b9f 100644
--- a/sandbox/plc4c/spi/include/plc4c/spi/types_private.h
+++ b/sandbox/plc4c/spi/include/plc4c/spi/types_private.h
@@ -41,14 +41,17 @@ typedef struct plc4c_system_task_t plc4c_system_task;
 
 typedef plc4c_return_code (*plc4c_system_task_state_machine_function)(plc4c_system_task *task);
 
-typedef plc4c_return_code (*plc4c_connection_connect_function)(plc4c_connection *connection, plc4c_system_task **task);
+typedef plc4c_return_code (*plc4c_connection_connect_function)(plc4c_connection *connection,
+                                                               plc4c_system_task **task);
 
 typedef plc4c_return_code (*plc4c_connection_disconnect_function)(plc4c_connection *connection,
                                                                   plc4c_system_task **task);
 
-typedef plc4c_return_code (*plc4c_connection_read_function)(plc4c_system_task **task);
+typedef plc4c_return_code (*plc4c_connection_read_function)(plc4c_connection *connection,
+                                                            plc4c_system_task **task);
 
-typedef plc4c_return_code (*plc4c_connection_write_function)(plc4c_system_task **task);
+typedef plc4c_return_code (*plc4c_connection_write_function)(plc4c_connection *connection,
+                                                             plc4c_system_task **task);
 
 typedef void (*plc4c_connect_free_read_response_function)(plc4c_read_response *response);
 
@@ -121,6 +124,11 @@ struct plc4c_connection_t {
 
     bool connected;
 
+    // Internal flag indicating the connection should be disconnected
+    bool disconnect;
+    // Number of system_tasks currently still active in the system.
+    int num_running_system_tasks;
+
     plc4c_system *system;
     plc4c_driver *driver;
     plc4c_transport *transport;
@@ -210,6 +218,9 @@ struct plc4c_system_task_t {
     plc4c_system_task_state_machine_function state_machine_function;
     void *context;
     bool completed;
+
+    // Reference to the connection that owns this task
+    plc4c_connection *connection;
 };
 
 #endif //PLC4C_SPI_TYPES_PRIVATE_H_
diff --git a/sandbox/plc4c/spi/src/connection.c b/sandbox/plc4c/spi/src/connection.c
index fc164ed..ce420d3 100644
--- a/sandbox/plc4c/spi/src/connection.c
+++ b/sandbox/plc4c/spi/src/connection.c
@@ -35,6 +35,8 @@ plc4c_return_code plc4c_connection_disconnect(plc4c_connection *connection) {
     if (result != OK) {
         return -1;
     }
+    // Increment the number of running tasks for this connection.
+    connection->num_running_system_tasks++;
     plc4c_utils_list_insert_tail_value(connection->system->task_list, new_disconnection_task);
     return OK;
 }
diff --git a/sandbox/plc4c/spi/src/read.c b/sandbox/plc4c/spi/src/read.c
index 143c4fc..abcfda6 100644
--- a/sandbox/plc4c/spi/src/read.c
+++ b/sandbox/plc4c/spi/src/read.c
@@ -25,7 +25,9 @@
 plc4c_return_code plc4c_read_request_execute(plc4c_read_request *read_request,
                                              plc4c_read_request_execution **read_request_execution) {
     plc4c_system_task *system_task;
-    read_request->connection->driver->read_function(&system_task);
+    read_request->connection->driver->read_function(read_request->connection, &system_task);
+    // Increment the number of running tasks for this connection.
+    read_request->connection->num_running_system_tasks++;
     // Add the new task to the task-list.
     plc4c_utils_list_insert_tail_value(read_request->connection->system->task_list, system_task);
 
diff --git a/sandbox/plc4c/spi/src/system.c b/sandbox/plc4c/spi/src/system.c
index c0fde0b..02fdc70 100644
--- a/sandbox/plc4c/spi/src/system.c
+++ b/sandbox/plc4c/spi/src/system.c
@@ -256,8 +256,20 @@ plc4c_return_code plc4c_system_create_connection(char *connection_string, plc4c_
     new_connection->transport_code = transport_code;
     new_connection->transport_connect_information = transport_connect_information;
     new_connection->parameters = parameters;
-    *connection = new_connection;
 
+    new_connection->connected = false;
+    new_connection->disconnect = false;
+    new_connection->num_running_system_tasks = 0;
+
+    new_connection->system = NULL;
+    new_connection->driver = NULL;
+    new_connection->transport = NULL;
+
+    new_connection->supports_reading = NULL;
+    new_connection->supports_writing = NULL;
+    new_connection->supports_subscriptions = NULL;
+
+    *connection = new_connection;
     return OK;
 }
 
@@ -344,6 +356,8 @@ plc4c_return_code plc4c_system_connect(plc4c_system *system,
     if (result != OK) {
         return -1;
     }
+    // Increment the number of running tasks for this connection.
+    new_connection->num_running_system_tasks++;
     plc4c_utils_list_insert_tail_value(system->task_list, new_connection_task);
 
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -381,7 +395,9 @@ plc4c_return_code plc4c_system_loop(plc4c_system *system) {
         // If the current task is completed at the end, remove it from the task_queue.
         if (cur_task->completed) {
             plc4c_utils_list_remove(system->task_list, cur_task_element);
-
+            if (cur_task->connection != NULL) {
+                cur_task->connection->num_running_system_tasks--;
+            }
             // TODO: clean up the memory of the cur_task_element and cur_task
         }
 
diff --git a/sandbox/plc4c/spi/src/write.c b/sandbox/plc4c/spi/src/write.c
index 9188960..19c0ec0 100644
--- a/sandbox/plc4c/spi/src/write.c
+++ b/sandbox/plc4c/spi/src/write.c
@@ -24,7 +24,9 @@
 plc4c_return_code plc4c_write_request_execute(plc4c_write_request *write_request,
                                               plc4c_write_request_execution **write_request_execution) {
     plc4c_system_task *system_task;
-    write_request->connection->driver->write_function(&system_task);
+    write_request->connection->driver->write_function(write_request->connection, &system_task);
+    // Increment the number of running tasks for this connection.
+    write_request->connection->num_running_system_tasks++;
     // Add the new task to the task-list.
     plc4c_utils_list_insert_tail_value(write_request->connection->system->task_list, system_task);
 
diff --git a/sandbox/plc4c/transports/dummy/src/transport_dummy.c b/sandbox/plc4c/transports/dummy/src/transport_dummy.c
index eab16b1..57291c7 100644
--- a/sandbox/plc4c/transports/dummy/src/transport_dummy.c
+++ b/sandbox/plc4c/transports/dummy/src/transport_dummy.c
@@ -22,7 +22,7 @@
 #include <plc4c/transport_dummy.h>
 
 plc4c_transport *plc4c_transport_dummy_create() {
-    plc4c_transport* transport = (plc4c_transport*) malloc(sizeof(plc4c_transport));
+    plc4c_transport *transport = (plc4c_transport *) malloc(sizeof(plc4c_transport));
     transport->transport_code = "dummy";
     return transport;
 }
diff --git a/sandbox/plc4c/transports/serial/src/transport_serial.c b/sandbox/plc4c/transports/serial/src/transport_serial.c
index ada4ce0..2a7ba02 100644
--- a/sandbox/plc4c/transports/serial/src/transport_serial.c
+++ b/sandbox/plc4c/transports/serial/src/transport_serial.c
@@ -22,7 +22,7 @@
 #include <plc4c/transport_serial.h>
 
 plc4c_transport *plc4c_transport_serial_create() {
-    plc4c_transport* transport = (plc4c_transport*) malloc(sizeof(plc4c_transport));
+    plc4c_transport *transport = (plc4c_transport *) malloc(sizeof(plc4c_transport));
     transport->transport_code = "serial";
     return transport;
 }
diff --git a/sandbox/plc4c/transports/tcp/src/transport_tcp.c b/sandbox/plc4c/transports/tcp/src/transport_tcp.c
index 7a51df6..2875891 100644
--- a/sandbox/plc4c/transports/tcp/src/transport_tcp.c
+++ b/sandbox/plc4c/transports/tcp/src/transport_tcp.c
@@ -22,7 +22,7 @@
 #include <plc4c/transport_tcp.h>
 
 plc4c_transport *plc4c_transport_tcp_create() {
-    plc4c_transport* transport = (plc4c_transport*) malloc(sizeof(plc4c_transport));
+    plc4c_transport *transport = (plc4c_transport *) malloc(sizeof(plc4c_transport));
     transport->transport_code = "tcp";
     return transport;
 }