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/04 17:35:35 UTC

[plc4x] branch feature/c-api updated: - Got a first working version of the read-functionality working.

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 aad001e  - Got a first working version of the read-functionality working.
aad001e is described below

commit aad001eb11a5ab78717d6fa81ba77795676e1310
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Mon May 4 19:35:28 2020 +0200

    - Got a first working version of the read-functionality working.
---
 sandbox/plc4c/{spi => api}/include/plc4c/utils/list.h  |  0
 sandbox/plc4c/{spi => api}/include/plc4c/utils/queue.h |  0
 sandbox/plc4c/drivers/simulated/src/driver_simulated.c | 18 +++++++++++++++---
 sandbox/plc4c/examples/hello-world/src/hello_world.c   | 14 ++++++++++++++
 sandbox/plc4c/spi/include/plc4c/spi/types_private.h    | 18 +++++++++---------
 sandbox/plc4c/spi/src/connection.c                     | 17 +++++++++--------
 6 files changed, 47 insertions(+), 20 deletions(-)

diff --git a/sandbox/plc4c/spi/include/plc4c/utils/list.h b/sandbox/plc4c/api/include/plc4c/utils/list.h
similarity index 100%
rename from sandbox/plc4c/spi/include/plc4c/utils/list.h
rename to sandbox/plc4c/api/include/plc4c/utils/list.h
diff --git a/sandbox/plc4c/spi/include/plc4c/utils/queue.h b/sandbox/plc4c/api/include/plc4c/utils/queue.h
similarity index 100%
rename from sandbox/plc4c/spi/include/plc4c/utils/queue.h
rename to sandbox/plc4c/api/include/plc4c/utils/queue.h
diff --git a/sandbox/plc4c/drivers/simulated/src/driver_simulated.c b/sandbox/plc4c/drivers/simulated/src/driver_simulated.c
index 7a3d63f..e3c2f9b 100644
--- a/sandbox/plc4c/drivers/simulated/src/driver_simulated.c
+++ b/sandbox/plc4c/drivers/simulated/src/driver_simulated.c
@@ -58,7 +58,7 @@ return_code plc4c_driver_simulated_connect_machine_function(plc4c_system_task *t
     connection->connected = true;
     task->completed = true;
     return OK;
-}
+ }
 
 return_code plc4c_driver_simulated_disconnect_machine_function(plc4c_system_task *task) {
     plc4c_connection *connection = task->context;
@@ -82,15 +82,27 @@ return_code plc4c_driver_simulated_read_machine_function(plc4c_system_task *task
     plc4c_read_request *read_request = read_request_execution->read_request;
     switch (task->state_id) {
         case READ_INIT: {
+            // Create a response.
+            plc4c_read_response *read_response = malloc(sizeof(plc4c_read_response));
+            read_response->read_request = read_request;
+            plc4c_utils_list_create(&(read_response->items));
+
             // Process every field in the request.
             plc4c_list_element *cur_element = plc4c_utils_list_head(read_request->items);
             while (cur_element != NULL) {
                 plc4c_driver_simulated_item *cur_item = cur_element->value;
+
+                // Create a new random value.
+                plc4c_value_item *value_item = malloc(sizeof(plc4c_value_item));
+                value_item->item = (plc4c_item *) cur_item;
+                // TODO: I know this is wrong ... just don't know how to do it right ...
+                value_item->value = arc4random();
+
+                // Add the value to the response.
+                plc4c_utils_list_insert_tail_value(read_response->items, value_item);
                 cur_element = cur_element->next;
             }
 
-            // Create a response.
-            plc4c_read_response *read_response = malloc(sizeof(plc4c_read_response));
             read_request_execution->read_response = read_response;
             task->state_id = READ_FINISHED;
             task->completed = true;
diff --git a/sandbox/plc4c/examples/hello-world/src/hello_world.c b/sandbox/plc4c/examples/hello-world/src/hello_world.c
index ed536c6..06867c2 100644
--- a/sandbox/plc4c/examples/hello-world/src/hello_world.c
+++ b/sandbox/plc4c/examples/hello-world/src/hello_world.c
@@ -20,6 +20,8 @@
 #include <plc4c/plc4c.h>
 #include <plc4c/driver_simulated.h>
 #include <plc4c/transport_dummy.h>
+#include <plc4c/utils/list.h>
+#include "../../../spi/include/plc4c/spi/types_private.h"
 
 
 int numOpenConnections = 0;
@@ -172,6 +174,18 @@ int main() {
             case READ_RESPONSE_RECEIVED: {
                 // Get the response for the given read-request.
                 plc4c_read_response *response = plc4c_read_request_get_response(read_request_execution);
+                if(response == NULL) {
+                    printf("FAILED (No Response)\n");
+                    return -1;
+                }
+                plc4c_list_element *cur_element = plc4c_utils_list_head(response->items);
+                while (cur_element != NULL) {
+                    plc4c_value_item *value_item = cur_element->value;
+
+                    printf("Value %d\n", (int) value_item->value);
+
+                    cur_element = cur_element->next;
+                }
 
                 // TODO: Do something sensible ...
 
diff --git a/sandbox/plc4c/spi/include/plc4c/spi/types_private.h b/sandbox/plc4c/spi/include/plc4c/spi/types_private.h
index 14e53cb..6bc835e 100644
--- a/sandbox/plc4c/spi/include/plc4c/spi/types_private.h
+++ b/sandbox/plc4c/spi/include/plc4c/spi/types_private.h
@@ -28,7 +28,7 @@ typedef struct plc4c_item_t plc4c_item;
 typedef struct plc4c_driver_list_item_t plc4c_driver_list_item;
 typedef struct plc4c_transport_list_item_t plc4c_transport_list_item;
 typedef struct plc4c_connection_list_item_t plc4c_connection_list_item;
-typedef struct plc4c_write_item_t plc4c_write_item;
+typedef struct plc4c_value_item_t plc4c_value_item;
 
 typedef plc4c_item *(*plc4c_connection_parse_address_item)(char *address_string);
 
@@ -123,20 +123,19 @@ struct plc4c_connection_list_item_t {
 };
 
 
+struct plc4c_value_item_t {
+    plc4c_item *item;
+    void *value;
+};
+
 struct plc4c_read_request_t {
     plc4c_connection *connection;
     plc4c_list *items;
 };
 
-struct plc4c_write_item_t {
-    plc4c_item *item;
-    void *value;
-};
-
 struct plc4c_write_request_t {
     plc4c_connection *connection;
-    int num_items;
-    plc4c_write_item *items;
+    plc4c_list *items;
 };
 
 struct plc4c_read_request_execution_t {
@@ -146,7 +145,8 @@ struct plc4c_read_request_execution_t {
 };
 
 struct plc4c_read_response_t {
-    // TODO: Implement
+    plc4c_read_request *read_request;
+    plc4c_list *items;
 };
 
 struct plc4c_system_task_t {
diff --git a/sandbox/plc4c/spi/src/connection.c b/sandbox/plc4c/spi/src/connection.c
index edcb7fa..0a93b3e 100644
--- a/sandbox/plc4c/spi/src/connection.c
+++ b/sandbox/plc4c/spi/src/connection.c
@@ -69,19 +69,20 @@ bool plc4c_connection_supports_writing(plc4c_connection *connection) {
 
 return_code plc4c_connection_create_write_request(plc4c_connection *connection, int num_items, char* addresses[], void* values[], plc4c_write_request** write_request) {
     plc4c_write_request* new_write_request = (plc4c_write_request*) malloc(sizeof(plc4c_write_request));
-    new_write_request->num_items = num_items;
-    new_write_request->items = malloc(num_items * sizeof(plc4c_write_item*));
+    plc4c_utils_list_create(&(new_write_request->items));
     for(int i = 0; i < num_items; i++) {
         char* address = addresses[i];
-        plc4c_item* addressItem = connection->driver->parse_address_function(address);
+        // Parse an address string and get a driver-dependent data-structure representing the address back.
+        plc4c_item* address_item = connection->driver->parse_address_function(address);
 
-        plc4c_write_item* write_item = malloc(sizeof(plc4c_write_item));
-        write_item->item = addressItem;
-        write_item->value = values[i];
+        // Create a new value item, binding an address item to a value.
+        plc4c_value_item* value_item = malloc(sizeof(plc4c_value_item));
+        value_item->item = address_item;
+        value_item->value = values[i];
 
-        new_write_request->items = write_item;
+        // Add the new item ot the list of items.
+        plc4c_utils_list_insert_tail_value(new_write_request->items, value_item);
     }
     write_request = &new_write_request;
-
     return OK;
 }