Skip to content
Open
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
2 changes: 2 additions & 0 deletions include/neuron/tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ typedef enum {
NEU_DATATAG_ENDIAN_LB64 = 9,
NEU_DATATAG_ENDIAN_BB64 = 10,
NEU_DATATAG_ENDIAN_BL64 = 11,
NEU_DATATAG_ENDIAN_WS64 = 12, // #WS 3,4,1,2,7,8,5,6
NEU_DATATAG_ENDIAN_WR64 = 13, // #WR 5,6,7,8,1,2,3,4
} neu_datatag_endian_e;

typedef enum {
Expand Down
36 changes: 36 additions & 0 deletions include/neuron/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,42 @@ static inline uint64_t neu_htonbl(uint64_t u)
return out;
}

static inline uint64_t neu_htonws(uint64_t u)
{
uint8_t *in = (uint8_t *) &u;
uint64_t out = 0;
uint8_t *out_bytes = (uint8_t *) &out;

out_bytes[0] = in[2];
out_bytes[1] = in[3];
out_bytes[2] = in[0];
out_bytes[3] = in[1];
out_bytes[4] = in[6];
out_bytes[5] = in[7];
out_bytes[6] = in[4];
out_bytes[7] = in[5];

return out;
}

static inline uint64_t neu_htonwr(uint64_t u)
{
uint8_t *in = (uint8_t *) &u;
uint64_t out = 0;
uint8_t *out_bytes = (uint8_t *) &out;

out_bytes[0] = in[4];
out_bytes[1] = in[5];
out_bytes[2] = in[6];
out_bytes[3] = in[7];
out_bytes[4] = in[0];
out_bytes[5] = in[1];
out_bytes[6] = in[2];
out_bytes[7] = in[3];

return out;
}

static inline uint64_t neu_ntohll(uint64_t u)
{
return neu_htonll(u);
Expand Down
14 changes: 11 additions & 3 deletions plugins/modbus/modbus-rtu.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
},
{
"type": 7,
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|)$"
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|#WS|#WR|)$"
},
{
"type": 8,
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|)$"
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|#WS|#WR|)$"
},
{
"type": 9,
"regex": "^[0-9]+![3-4][0-9]+(#BB|#BL|#LL|#LB|)$"
},
{
"type": 10,
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|)$"
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|#WS|#WR|)$"
},
{
"type": 11,
Expand Down Expand Up @@ -211,6 +211,14 @@
{
"key": "78 56 34 12",
"value": 4
},
{
"key": "34 12 78 56",
"value": 5
},
{
"key": "56 78 12 34",
"value": 6
}
]
}
Expand Down
14 changes: 11 additions & 3 deletions plugins/modbus/modbus-tcp.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
},
{
"type": 7,
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|)$"
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|#WS|#WR|)$"
},
{
"type": 8,
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|)$"
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|#WS|#WR|)$"
},
{
"type": 9,
"regex": "^[0-9]+![3-4][0-9]+(#BB|#BL|#LL|#LB|)$"
},
{
"type": 10,
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|)$"
"regex": "^[0-9]+![3-4][0-9]+(#B|#L|#BB|#BL|#LL|#LB|#WS|#WR|)$"
},
{
"type": 11,
Expand Down Expand Up @@ -219,6 +219,14 @@
{
"key": "78 56 34 12",
"value": 4
},
{
"key": "34 12 78 56",
"value": 5
},
{
"key": "56 78 12 34",
"value": 6
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions plugins/modbus/modbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ typedef enum modbus_endianess_64 {
MODBUS_LB = 2, // 21 43 65 87
MODBUS_BB = 3, // 87 65 43 21
MODBUS_BL = 4, // 78 56 34 12
MODBUS_WS = 5, // 34 12 78 56
MODBUS_WR = 6, // 56 78 12 34
} modbus_endianess_64;

typedef enum modbus_address_base {
Expand Down
6 changes: 6 additions & 0 deletions plugins/modbus/modbus_point.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,12 @@ void modbus_convert_endianess_64(neu_value_u * value,
case MODBUS_BL:
value->u64 = neu_htonbl(value->u64);
break;
case MODBUS_WS:
value->u64 = neu_htonws(value->u64);
break;
case MODBUS_WR:
value->u64 = neu_htonwr(value->u64);
break;
default:
break;
}
Expand Down
52 changes: 51 additions & 1 deletion plugins/modbus/modbus_req.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,41 @@ static int process_protocol_buf_test(neu_plugin_t *plugin, void *req,
modbus_point_t *point,
uint16_t response_size);

static void
convert_point_endianess_64_for_test(neu_value_u *value, modbus_point_t *point,
modbus_endianess_64 default_endianess)
{
modbus_endianess_64 endianess = default_endianess;

if (!point->option.value64.is_default) {
switch (point->option.value64.endian) {
case NEU_DATATAG_ENDIAN_B64:
case NEU_DATATAG_ENDIAN_BB64:
endianess = MODBUS_BB;
break;
case NEU_DATATAG_ENDIAN_LB64:
endianess = MODBUS_LB;
break;
case NEU_DATATAG_ENDIAN_BL64:
endianess = MODBUS_BL;
break;
case NEU_DATATAG_ENDIAN_WS64:
endianess = MODBUS_WS;
break;
case NEU_DATATAG_ENDIAN_WR64:
endianess = MODBUS_WR;
break;
case NEU_DATATAG_ENDIAN_L64:
case NEU_DATATAG_ENDIAN_LL64:
default:
endianess = MODBUS_LL;
break;
}
}

modbus_convert_endianess_64(value, endianess);
}

void modbus_conn_connected(void *data, int fd)
{
struct neu_plugin *plugin = (struct neu_plugin *) data;
Expand Down Expand Up @@ -595,6 +630,11 @@ int modbus_value_handle_test(neu_plugin_t *plugin, void *req,
case NEU_TYPE_DOUBLE: {
uint64_t tmp_vald;
memcpy(&tmp_vald, recv_bytes, sizeof(uint64_t));
neu_value_u value = { 0 };
value.u64 = tmp_vald;
convert_point_endianess_64_for_test(&value, point,
plugin->endianess_64);
tmp_vald = value.u64;
jtype = NEU_JSON_DOUBLE;
jvalue.val_int = neu_ntohll(tmp_vald);
break;
Expand All @@ -603,13 +643,23 @@ int modbus_value_handle_test(neu_plugin_t *plugin, void *req,
jtype = NEU_JSON_INT;
int64_t tmp_val64;
memcpy(&tmp_val64, recv_bytes, sizeof(int64_t));
neu_value_u value = { 0 };
value.u64 = (uint64_t) tmp_val64;
convert_point_endianess_64_for_test(&value, point,
plugin->endianess_64);
tmp_val64 = (int64_t) value.u64;
jvalue.val_int = (int64_t) neu_ntohll(tmp_val64);
break;
}
case NEU_TYPE_UINT64: {
jtype = NEU_JSON_INT;
uint64_t tmp_val64;
memcpy(&tmp_val64, recv_bytes, sizeof(uint64_t));
neu_value_u value = { 0 };
value.u64 = tmp_val64;
convert_point_endianess_64_for_test(&value, point,
plugin->endianess_64);
tmp_val64 = value.u64;
jvalue.val_int = neu_ntohll(tmp_val64);
break;
}
Expand Down Expand Up @@ -1025,4 +1075,4 @@ static int process_protocol_buf_test(neu_plugin_t *plugin, void *req,

free(recv_buf);
return ret;
}
}
24 changes: 24 additions & 0 deletions src/adapter/driver/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,12 @@ static void fix_value(neu_datatag_t *tag, neu_type_e value_type,
case NEU_DATATAG_ENDIAN_BL64:
value->value.u64 = neu_htonbl(value->value.u64);
break;
case NEU_DATATAG_ENDIAN_WS64:
value->value.u64 = neu_htonws(value->value.u64);
break;
case NEU_DATATAG_ENDIAN_WR64:
value->value.u64 = neu_htonwr(value->value.u64);
break;
case NEU_DATATAG_ENDIAN_L64:
case NEU_DATATAG_ENDIAN_LL64:
default:
Expand Down Expand Up @@ -2896,6 +2902,12 @@ static void read_report_group(int64_t timestamp, int64_t timeout,
case NEU_DATATAG_ENDIAN_BL64:
value.value.value.u64 = neu_htonbl(value.value.value.u64);
break;
case NEU_DATATAG_ENDIAN_WS64:
value.value.value.u64 = neu_htonws(value.value.value.u64);
break;
case NEU_DATATAG_ENDIAN_WR64:
value.value.value.u64 = neu_htonwr(value.value.value.u64);
break;
case NEU_DATATAG_ENDIAN_L64:
case NEU_DATATAG_ENDIAN_LL64:
default:
Expand Down Expand Up @@ -3071,6 +3083,12 @@ static void read_group(int64_t timestamp, int64_t timeout,
case NEU_DATATAG_ENDIAN_BL64:
value.value.value.u64 = neu_htonbl(value.value.value.u64);
break;
case NEU_DATATAG_ENDIAN_WS64:
value.value.value.u64 = neu_htonws(value.value.value.u64);
break;
case NEU_DATATAG_ENDIAN_WR64:
value.value.value.u64 = neu_htonwr(value.value.value.u64);
break;
case NEU_DATATAG_ENDIAN_L64:
case NEU_DATATAG_ENDIAN_LL64:
default:
Expand Down Expand Up @@ -3260,6 +3278,12 @@ static void read_group_paginate(int64_t timestamp, int64_t timeout,
case NEU_DATATAG_ENDIAN_BL64:
value.value.value.u64 = neu_htonbl(value.value.value.u64);
break;
case NEU_DATATAG_ENDIAN_WS64:
value.value.value.u64 = neu_htonws(value.value.value.u64);
break;
case NEU_DATATAG_ENDIAN_WR64:
value.value.value.u64 = neu_htonwr(value.value.value.u64);
break;
case NEU_DATATAG_ENDIAN_L64:
case NEU_DATATAG_ENDIAN_LL64:
default:
Expand Down
6 changes: 6 additions & 0 deletions src/base/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ int neu_datatag_parse_addr_option(const neu_datatag_t * datatag,
} else if (e1 == 'L' && e2 == 'B') {
option->value64.endian = NEU_DATATAG_ENDIAN_LB64;
option->value64.is_default = false;
} else if (e1 == 'W' && e2 == 'S') {
option->value64.endian = NEU_DATATAG_ENDIAN_WS64;
option->value64.is_default = false;
} else if (e1 == 'W' && e2 == 'R') {
option->value64.endian = NEU_DATATAG_ENDIAN_WR64;
option->value64.is_default = false;
}
} else if (n == 1) {
if (e1 == 'B') {
Expand Down
Loading
Loading