Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various Changes #784

Draft
wants to merge 16 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion code-generation/language-c/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
<extraArtifact>org.apache.plc4x.plugins:plc4x-maven-plugin:${plc4x-code-generation.version}</extraArtifact>
<extraArtifact>org.apache.plc4x.plugins:plc4x-code-generation-protocol-base:${plc4x-code-generation.version}</extraArtifact>
<extraArtifact>org.apache.plc4x.plugins:plc4x-code-generation-types-base:${plc4x-code-generation.version}</extraArtifact>
<extraArtifact>org.apache.plc4x.plugins:plc4x-code-generation-types-base:${plc4x-code-generation.version}</extraArtifact>
</extraArtifacts>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ plc4c_return_code ${helper.getCTypeName(type.name)}_parse(plc4x_spi_context ctx,
return new PlcStruct(${field.name});
<#break-->
<#case "STRING">
<#if (parserArguments?filter(parserArgument->parserArgument.name=="stringLength"))?has_content>
*data_item = plc4c_data_create_string_data(stringLength, ${manualField.name});
<#else>
*data_item = plc4c_data_create_string_data(strlen(value), ${manualField.name});
</#if>
<#break>
<#default>
*data_item = plc4c_data_create_${case.name?lower_case}_data(${manualField.name});
Expand Down
1 change: 0 additions & 1 deletion code-generation/language-go/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
<extraArtifact>org.apache.plc4x.plugins:plc4x-maven-plugin:${plc4x-code-generation.version}</extraArtifact>
<extraArtifact>org.apache.plc4x.plugins:plc4x-code-generation-protocol-base:${plc4x-code-generation.version}</extraArtifact>
<extraArtifact>org.apache.plc4x.plugins:plc4x-code-generation-types-base:${plc4x-code-generation.version}</extraArtifact>
<extraArtifact>org.apache.plc4x.plugins:plc4x-code-generation-types-base:${plc4x-code-generation.version}</extraArtifact>
</extraArtifacts>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
Expand Down
2 changes: 2 additions & 0 deletions plc4c/drivers/plc4x/include/plc4c/driver_plc4x_static.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ extern "C" {

uint8_t plc4c_spi_evaluation_helper_str_len(char* str);

char* plc4c_plc4x_read_write_parse_string(plc4c_spi_read_buffer* io, char* encoding);

#ifdef __cplusplus
}
#endif
Expand Down
28 changes: 28 additions & 0 deletions plc4c/drivers/plc4x/src/driver_plc4x_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,31 @@ uint8_t plc4c_spi_evaluation_helper_str_len(char* str) {
return strlen(str);
}

char* plc4c_plc4x_read_write_parse_string(plc4c_spi_read_buffer* io, char* encoding) {
if (strcmp(encoding, "UTF-8") == 0) {
// Read the max length (which is not interesting for us.
uint8_t length;
plc4c_return_code res = plc4c_spi_read_unsigned_byte(io, 8, &length);
if (res != OK) {
return NULL;
}
char* result = malloc(sizeof(char) * (length + 1));
if (result == NULL) {
return NULL;
}
char* curPos = result;
for(int i = 0; i < length; i++) {
uint8_t val;
plc4c_return_code res = plc4c_spi_read_unsigned_byte(io, 8, &val);
if (res != OK) {
return NULL;
}
*curPos = (char) val;
curPos++;
}
*curPos = '\0';
return result;
} else if (strcmp(encoding, "UTF-16") == 0) {
}
return "";
}
6 changes: 6 additions & 0 deletions plc4c/drivers/s7/include/plc4c/driver_s7_encode_decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ extern "C" {
#include "plc4c/driver_s7.h"
#include "s7_var_request_parameter_item.h"

struct plc4c_s7_read_write_s7_var_request_parameter_item_field {
plc4c_s7_read_write_s7_var_request_parameter_item* parameter_item;
char* s7_address_any_encoding_of_string;
};
typedef struct plc4c_s7_read_write_s7_var_request_parameter_item_field plc4c_s7_read_write_s7_var_request_parameter_item_field;

uint16_t plc4c_driver_s7_encode_tsap_id(
plc4c_driver_s7_device_group device_group, uint8_t rack, uint8_t slot);

Expand Down
4 changes: 2 additions & 2 deletions plc4c/drivers/s7/include/plc4c/driver_s7_static.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ uint16_t plc4c_s7_read_write_s7msec_to_int(plc4c_spi_read_buffer* io);

plc4c_return_code plc4c_s7_read_write_int_to_s7msec(plc4c_spi_write_buffer* writeBuffer, uint16_t value);

char* plc4c_s7_read_write_parse_s7_string(plc4c_spi_read_buffer* io, int32_t stringLength, char* encoding);
char* plc4c_s7_read_write_parse_s7_string(plc4c_spi_read_buffer* io, int32_t stringLength, char* encoding, char* stringEncoding);

char* plc4c_s7_read_write_parse_s7_char(plc4c_spi_read_buffer* io, char* encoding);
char* plc4c_s7_read_write_parse_s7_char(plc4c_spi_read_buffer* io, char* encoding, char* stringEncoding);

time_t plc4c_s7_read_write_parse_tia_time(plc4c_spi_read_buffer* io);

Expand Down
66 changes: 61 additions & 5 deletions plc4c/drivers/s7/src/driver_s7_encode_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <stdlib.h>
#include <string.h>
#include <tpkt_packet.h>
#include "plc4c/driver_s7_encode_decode.h"

uint16_t plc4c_driver_s7_encode_tsap_id(
plc4c_driver_s7_device_group device_group, uint8_t rack, uint8_t slot) {
Expand Down Expand Up @@ -114,6 +115,7 @@ plc4c_return_code plc4c_driver_s7_encode_address(char* address, void** item) {
// Parser logic
char* cur_pos = address;
char* last_pos = address;
char* string_encoding = NULL;
// - Does it start with "%"?
if (*cur_pos == '%') {
cur_pos++;
Expand Down Expand Up @@ -247,6 +249,19 @@ plc4c_return_code plc4c_driver_s7_encode_address(char* address, void** item) {
*(num_elements + len) = '\0';
}

if (*cur_pos == '|') {
// Next comes the num_elements
cur_pos++;
last_pos = cur_pos;
while (*cur_pos != '\0') {
cur_pos++;
}
len = cur_pos - last_pos;
string_encoding = malloc(sizeof(char) * (len + 1));
strncpy(string_encoding, last_pos, len);
*(string_encoding + len) = '\0';
}

////////////////////////////////////////////////////////////////////////////
// Now parse the contents.
////////////////////////////////////////////////////////////////////////////
Expand All @@ -261,6 +276,7 @@ plc4c_return_code plc4c_driver_s7_encode_address(char* address, void** item) {
free(data_type);
free(string_length);
free(num_elements);
free(string_encoding);
free(s7_item);
return NO_MEMORY;
}
Expand All @@ -283,6 +299,7 @@ plc4c_return_code plc4c_driver_s7_encode_address(char* address, void** item) {
free(data_type);
free(string_length);
free(num_elements);
free(string_encoding);
free(any_address);
free(s7_item);
return INVALID_ADDRESS;
Expand Down Expand Up @@ -320,15 +337,50 @@ plc4c_return_code plc4c_driver_s7_encode_address(char* address, void** item) {

// TODO: THis should be moved to "driver_s7_packets.c->plc4c_return_code plc4c_driver_s7_create_s7_read_request"
if (any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_TIME ||
any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_LINT ||
any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_ULINT ||
any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_LWORD ||
any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_LREAL ||
any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_REAL ||
any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_LTIME ||
any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_DATE ||
any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_TIME_OF_DAY ||
any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_DATE_AND_TIME) {
any_address->s7_address_any_transport_size = plc4c_s7_read_write_transport_size_BYTE;
any_address->s7_address_any_number_of_elements =
plc4c_s7_read_write_transport_size_length_in_bytes(plc4x_spi_context_background(), &(any_address->s7_address_any_transport_size)) *
any_address->s7_address_any_number_of_elements;
} else if (any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_STRING) {
any_address->s7_address_any_transport_size = plc4c_s7_read_write_transport_size_BYTE;
if (string_length != NULL) {
any_address->s7_address_any_number_of_elements =
strtol(string_length, 0, 10) *
(strtol(string_length, 0, 10) +2) *
any_address->s7_address_any_number_of_elements;
} else if (any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_STRING) {
} else {
any_address->s7_address_any_number_of_elements =
256 * any_address->s7_address_any_number_of_elements;
}
} else if (any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_WSTRING) {
any_address->s7_address_any_transport_size = plc4c_s7_read_write_transport_size_BYTE;
if (string_length != NULL) {
any_address->s7_address_any_number_of_elements =
(strtol(string_length, 0, 10) +2) * 2 *
any_address->s7_address_any_number_of_elements;
} else {
any_address->s7_address_any_number_of_elements =
254 * any_address->s7_address_any_number_of_elements;
512 * any_address->s7_address_any_number_of_elements;
}
} else if (any_address->s7_address_any_transport_size ==
plc4c_s7_read_write_transport_size_TOD) {
Expand Down Expand Up @@ -396,8 +448,12 @@ plc4c_return_code plc4c_driver_s7_encode_address(char* address, void** item) {
free(read_buffer);
free(raw_data);
}
plc4c_s7_read_write_s7_var_request_parameter_item_field* s7_item_field;

*item = s7_item;
s7_item_field = malloc(sizeof(plc4c_s7_read_write_s7_var_request_parameter_item_field));
s7_item_field->parameter_item = s7_item;
s7_item_field->s7_address_any_encoding_of_string = string_encoding;
*item = s7_item_field;

return OK;
}
13 changes: 8 additions & 5 deletions plc4c/drivers/s7/src/driver_s7_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,24 +713,24 @@ plc4c_return_code plc4c_driver_s7_create_s7_read_request(
plc4c_s7_read_write_s7_var_request_parameter_item* updated_item_address;

item = cur_item->value;

plc4c_s7_read_write_s7_var_request_parameter_item_field* field = item->address;
// Get the item address from the API request.
parsed_item_address = item->address;
parsed_item_address = field->parameter_item;

// Create a copy of the request item...
updated_item_address = malloc(sizeof(plc4c_s7_read_write_s7_var_request_parameter_item));
if (updated_item_address == NULL) {
return NO_MEMORY;
}
updated_item_address->_type = parsed_item_address->_type;
updated_item_address->s7_var_request_parameter_item_address_address =
updated_item_address->s7_var_request_parameter_item_address_address =
malloc(sizeof(plc4c_s7_read_write_s7_address));
if (updated_item_address->s7_var_request_parameter_item_address_address == NULL) {
return NO_MEMORY;
}
// Memcpy inplace of fields assignment, as all fields where assigned
memcpy(updated_item_address->s7_var_request_parameter_item_address_address,
parsed_item_address->s7_var_request_parameter_item_address_address,
parsed_item_address->s7_var_request_parameter_item_address_address,
sizeof(plc4c_s7_read_write_s7_address));

// In case of TIME values, we read 4 bytes for each value instead.
Expand Down Expand Up @@ -855,7 +855,10 @@ plc4c_return_code plc4c_driver_s7_create_s7_write_request(
// Get the item address from the API request.
item = list_item->value;
parsed_item = item->item;
parsed_param = parsed_item->address;

plc4c_s7_read_write_s7_var_request_parameter_item_field* field = parsed_item->address;

parsed_param = field->parameter_item;
parsed_value = item->value;

// Make a copy of the param
Expand Down
13 changes: 8 additions & 5 deletions plc4c/drivers/s7/src/driver_s7_sm_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ plc4c_return_code plc4c_driver_s7_parse_read_response(
plc4c_data* data_item;
plc4c_response_value_item* response_value_item;
char* data_protocol_id;
char* string_encoding;
uint16_t num_elements;
int32_t string_length;
uint8_t* byte_array;
Expand All @@ -225,11 +226,13 @@ plc4c_return_code plc4c_driver_s7_parse_read_response(

// Get the protocol id for the current item from the corresponding
// request item. Also get the number of elements, if it's an array.
request_address = request_item->address;
plc4c_s7_read_write_s7_var_request_parameter_item_field* field = request_item->address;
request_address = field->parameter_item;
transport_size = request_address->s7_var_request_parameter_item_address_address->s7_address_any_transport_size;
num_elements = request_address->s7_var_request_parameter_item_address_address->s7_address_any_number_of_elements;
data_protocol_id = plc4c_s7_read_write_transport_size_get_data_protocol_id(transport_size);

string_encoding = field->s7_address_any_encoding_of_string;

if (transport_size == plc4c_s7_read_write_transport_size_STRING) {
// TODO: This needs to be changed to read arrays of strings.
string_length = num_elements;
Expand Down Expand Up @@ -257,12 +260,12 @@ plc4c_return_code plc4c_driver_s7_parse_read_response(
all_data_item = plc4c_data_create_list_data(all_list);
free(all_list);
for (idx = 0; idx < num_elements ; idx++) {
plc4c_s7_read_write_data_item_parse(plc4x_spi_context_background(), read_buffer, data_protocol_id, string_length, &data_item);
plc4c_utils_list_insert_head_value(all_data_item->data.list_value, (void*)data_item);
plc4c_s7_read_write_data_item_parse(plc4x_spi_context_background(), read_buffer, data_protocol_id, string_length, string_encoding, &data_item);
plc4c_utils_list_insert_head_value(&all_data_item->data.list_value, (void*)data_item);
}
data_item = all_data_item;
} else {
plc4c_s7_read_write_data_item_parse(plc4x_spi_context_background(), read_buffer, data_protocol_id, string_length, &data_item);
plc4c_s7_read_write_data_item_parse(plc4x_spi_context_background(), read_buffer, data_protocol_id, string_length,string_encoding, &data_item);
}

// Create a new response value-item
Expand Down
4 changes: 2 additions & 2 deletions plc4c/drivers/s7/src/driver_s7_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ uint16_t plc4c_s7_read_write_s7msec_to_int(plc4c_spi_read_buffer* io) {

char* plc4c_s7_read_write_parse_s7_string(plc4c_spi_read_buffer* io,
int32_t stringLength,
char* encoding) {
char* encoding, char* stringEncoding) {
if (strcmp(encoding, "UTF-8") == 0) {
// Read the max length (which is not interesting for us.
uint8_t maxLen;
Expand Down Expand Up @@ -89,7 +89,7 @@ char* plc4c_s7_read_write_parse_s7_string(plc4c_spi_read_buffer* io,
}

char* plc4c_s7_read_write_parse_s7_char(plc4c_spi_read_buffer* io,
char* encoding) {
char* encoding, char* stringEncoding) {
if (strcmp(encoding, "UTF-8") == 0) {
char* result = malloc(sizeof(char) * 2);
if (result == NULL) {
Expand Down
34 changes: 34 additions & 0 deletions plc4c/generated-sources/plc4x/src/plc4x_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,20 @@ plc4c_return_code plc4c_plc4x_read_write_plc4x_value_parse(plc4x_spi_context ctx

*data_item = plc4c_data_create_string_data(16, value);

} else if(valueType == plc4c_plc4x_read_write_plc4x_value_type_STRING) { /* STRING */

// Manual Field (value)
char* value = (char*) (plc4c_plc4x_read_write_parse_string(readBuffer, "UTF-8"));

*data_item = plc4c_data_create_string_data(strlen(value), value);

} else if(valueType == plc4c_plc4x_read_write_plc4x_value_type_WSTRING) { /* STRING */

// Manual Field (value)
char* value = (char*) (plc4c_plc4x_read_write_parse_string(readBuffer, "UTF-16"));

*data_item = plc4c_data_create_string_data(strlen(value), value);

} else if(valueType == plc4c_plc4x_read_write_plc4x_value_type_TIME) { /* TIME */

// Simple Field (milliseconds)
Expand Down Expand Up @@ -452,6 +466,12 @@ plc4c_return_code plc4c_plc4x_read_write_plc4x_value_serialize(plc4x_spi_context
if(_res != OK) {
return _res;
}
} else if(valueType == plc4c_plc4x_read_write_plc4x_value_type_STRING) { /* STRING */

// Manual Field (value)
} else if(valueType == plc4c_plc4x_read_write_plc4x_value_type_WSTRING) { /* STRING */

// Manual Field (value)
} else if(valueType == plc4c_plc4x_read_write_plc4x_value_type_TIME) { /* TIME */

// Simple field (milliseconds)
Expand Down Expand Up @@ -590,6 +610,20 @@ uint16_t plc4c_plc4x_read_write_plc4x_value_length_in_bits(plc4x_spi_context ctx

// Simple field (value)
lengthInBits += 16;
} else if(valueType == plc4c_plc4x_read_write_plc4x_value_type_STRING) { /* STRING */

// Manual Field (value)
{
char* _value = data_item->data.string_value;
lengthInBits += (((plc4c_spi_evaluation_helper_str_len(_value)) + (1))) * (8);
}
} else if(valueType == plc4c_plc4x_read_write_plc4x_value_type_WSTRING) { /* STRING */

// Manual Field (value)
{
char* _value = data_item->data.string_value;
lengthInBits += (((plc4c_spi_evaluation_helper_str_len(_value)) + (1))) * (16);
}
} else if(valueType == plc4c_plc4x_read_write_plc4x_value_type_TIME) { /* TIME */

// Simple field (milliseconds)
Expand Down
8 changes: 4 additions & 4 deletions plc4c/generated-sources/s7/include/data_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@

// Code generated by code-generation. DO NOT EDIT.

plc4c_return_code plc4c_s7_read_write_data_item_parse(plc4x_spi_context ctx, plc4c_spi_read_buffer* readBuffer, char* dataProtocolId, int32_t stringLength, plc4c_data** data_item);
plc4c_return_code plc4c_s7_read_write_data_item_parse(plc4x_spi_context ctx, plc4c_spi_read_buffer* readBuffer, char* dataProtocolId, int32_t stringLength, char* stringEncoding, plc4c_data** data_item);

plc4c_return_code plc4c_s7_read_write_data_item_serialize(plc4x_spi_context ctx, plc4c_spi_write_buffer* writeBuffer, char* dataProtocolId, int32_t stringLength, plc4c_data** data_item);
plc4c_return_code plc4c_s7_read_write_data_item_serialize(plc4x_spi_context ctx, plc4c_spi_write_buffer* writeBuffer, char* dataProtocolId, int32_t stringLength, char* stringEncoding, plc4c_data** data_item);

uint16_t plc4c_s7_read_write_data_item_length_in_bytes(plc4x_spi_context ctx, plc4c_data* data_item, char* dataProtocolId, int32_t stringLength);
uint16_t plc4c_s7_read_write_data_item_length_in_bytes(plc4x_spi_context ctx, plc4c_data* data_item, char* dataProtocolId, int32_t stringLength, char* stringEncoding);

uint16_t plc4c_s7_read_write_data_item_length_in_bits(plc4x_spi_context ctx, plc4c_data* data_item, char* dataProtocolId, int32_t stringLength);
uint16_t plc4c_s7_read_write_data_item_length_in_bits(plc4x_spi_context ctx, plc4c_data* data_item, char* dataProtocolId, int32_t stringLength, char* stringEncoding);

#endif // PLC4C_S7_READ_WRITE_DATA_ITEM_H_
Loading