modbus/mb_runtime.c
author Edouard Tisserant <edouard@beremiz.fr>
Mon, 30 Sep 2024 16:21:11 +0200
changeset 4023 b344393859df
parent 3859 098875cff39f
child 4049 80fc760d4a4a
permissions -rw-r--r--
MQTT: Add status global variable MQTT_STATUS_n, one per MQTT client
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     1
/* File generated by Beremiz (PlugGenerate_C method of Modbus plugin) */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     2
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     3
/*
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     4
 * Copyright (c) 2016 Mario de Sousa (msousa@fe.up.pt)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     5
 *
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     6
 * This file is part of the Modbus library for Beremiz and matiec.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     7
 *
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     8
 * This Modbus library is free software: you can redistribute it and/or modify
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     9
 * it under the terms of the GNU Lesser General Public License as published by
2019
92f02bb17c7e Make Modbus extension license compatible with the rest of Beremiz IDE python code : GPLv2.
Edouard Tisserant
parents: 2011
diff changeset
    10
 * the Free Software Foundation, either version 2 of the License, or
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    11
 * (at your option) any later version.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    12
 *
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    13
 * This program is distributed in the hope that it will be useful, but
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    16
 * General Public License for more details.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    17
 *
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    18
 * You should have received a copy of the GNU Lesser General Public License
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    19
 * along with this Modbus library.  If not, see <http://www.gnu.org/licenses/>.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    20
 *
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    21
 * This code is made available on the understanding that it will not be
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    22
 * used in safety-critical situations without a full and competent review.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    23
 */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    24
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    25
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    26
#include <stdio.h>
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    27
#include <string.h>  /* required for memcpy() */
2683
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
    28
#include <errno.h>
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
    29
#include <time.h>
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
    30
#include <signal.h>
2687
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
    31
#include <unistd.h>  /* required for pause() */
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    32
#include "mb_slave_and_master.h"
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    33
#include "MB_%(locstr)s.h"
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    34
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    35
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    36
#define MAX_MODBUS_ERROR_CODE 11
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    37
static const char *modbus_error_messages[MAX_MODBUS_ERROR_CODE+1] = {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    38
    /* 0 */ "",                             /* un-used -> no error! */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    39
    /* 1 */ "illegal/unsuported function",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    40
    /* 2 */ "illegal data address",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    41
    /* 3 */ "illegal data value",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    42
    /* 4 */ "slave device failure",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    43
    /* 5 */ "acknowledge -> slave intends to reply later",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    44
    /* 6 */ "slave device busy",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    45
    /* 7 */ "negative acknowledge",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    46
    /* 8 */ "memory parity error",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    47
    /* 9 */ "",                             /* undefined by Modbus */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    48
    /* 10*/ "gateway path unavalilable",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    49
    /* 11*/ "gateway target device failed to respond"
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    50
};    
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    51
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    52
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    53
/* Execute a modbus client transaction/request */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    54
static int __execute_mb_request(int request_id){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    55
	switch (client_requests[request_id].mb_function){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    56
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    57
	case  1: /* read coils */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    58
		return read_output_bits(client_requests[request_id].slave_id,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    59
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    60
					client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    61
					client_requests[request_id].coms_buffer,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    62
					(int) client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    63
					client_nodes[client_requests[request_id].client_node_id].mb_nd,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    64
					client_requests[request_id].retries,
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
    65
					&(client_requests[request_id].mb_error_code),
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    66
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    67
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    68
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    69
	case  2: /* read discrete inputs */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    70
		return read_input_bits( client_requests[request_id].slave_id,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    71
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    72
					client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    73
					client_requests[request_id].coms_buffer,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    74
					(int) client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    75
					client_nodes[client_requests[request_id].client_node_id].mb_nd,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    76
					client_requests[request_id].retries,
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
    77
					&(client_requests[request_id].mb_error_code),
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    78
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    79
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    80
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    81
	case  3: /* read holding registers */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    82
		return read_output_words(client_requests[request_id].slave_id,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    83
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    84
					client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    85
					client_requests[request_id].coms_buffer,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    86
					(int) client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    87
					client_nodes[client_requests[request_id].client_node_id].mb_nd,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    88
					client_requests[request_id].retries,
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
    89
					&(client_requests[request_id].mb_error_code),
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    90
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    91
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    92
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    93
	case  4: /* read input registers */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    94
		return read_input_words(client_requests[request_id].slave_id,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    95
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    96
					client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    97
					client_requests[request_id].coms_buffer,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    98
					(int) client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    99
					client_nodes[client_requests[request_id].client_node_id].mb_nd,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   100
					client_requests[request_id].retries,
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   101
					&(client_requests[request_id].mb_error_code),
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   102
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   103
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   104
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   105
	case  5: /* write single coil */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   106
		return write_output_bit(client_requests[request_id].slave_id,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   107
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   108
					client_requests[request_id].coms_buffer[0],
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   109
					client_nodes[client_requests[request_id].client_node_id].mb_nd,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   110
					client_requests[request_id].retries,
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   111
					&(client_requests[request_id].mb_error_code),
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   112
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   113
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   114
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   115
	case  6: /* write single register */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   116
		return write_output_word(client_requests[request_id].slave_id,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   117
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   118
					client_requests[request_id].coms_buffer[0],
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   119
					client_nodes[client_requests[request_id].client_node_id].mb_nd,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   120
					client_requests[request_id].retries,
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   121
					&(client_requests[request_id].mb_error_code),
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   122
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   123
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   124
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   125
	case  7: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   126
	case  8: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   127
	case  9: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   128
	case 10: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   129
	case 11: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   130
	case 12: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   131
	case 13: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   132
	case 14: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   133
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   134
	case 15: /* write multiple coils */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   135
		return write_output_bits(client_requests[request_id].slave_id,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   136
					 client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   137
					 client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   138
					 client_requests[request_id].coms_buffer,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   139
					 client_nodes[client_requests[request_id].client_node_id].mb_nd,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   140
					 client_requests[request_id].retries,
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   141
					 &(client_requests[request_id].mb_error_code),
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   142
					 &(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   143
					 &(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   144
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   145
	case 16: /* write multiple registers */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   146
		return write_output_words(client_requests[request_id].slave_id,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   147
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   148
					client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   149
					client_requests[request_id].coms_buffer,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   150
					client_nodes[client_requests[request_id].client_node_id].mb_nd,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   151
					client_requests[request_id].retries,
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   152
					&(client_requests[request_id].mb_error_code),
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   153
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   154
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   155
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   156
	default: break;  /* should never occur, if file generation is correct */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   157
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   158
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   159
	fprintf(stderr, "Modbus plugin: Modbus function %%d not supported\n", request_id); /* should never occur, if file generation is correct */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   160
	return -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   161
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   162
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   163
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   164
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   165
/* pack bits from unpacked_data to packed_data */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   166
static inline int __pack_bits(u16 *unpacked_data, u16 start_addr, u16 bit_count,  u8  *packed_data) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   167
  u8 bit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   168
  u16 byte, coils_processed;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   169
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   170
  if ((0 == bit_count) || (65535-start_addr < bit_count-1))
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   171
    return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   172
  
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   173
  for( byte = 0, coils_processed = 0; coils_processed < bit_count; byte++) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   174
    packed_data[byte] = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   175
    for( bit = 0x01; (bit & 0xFF) && (coils_processed < bit_count); bit <<= 1, coils_processed++ ) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   176
      if(unpacked_data[start_addr + coils_processed])
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   177
            packed_data[byte] |=  bit; /*   set bit */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   178
      else  packed_data[byte] &= ~bit; /* reset bit */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   179
    }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   180
  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   181
  return 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   182
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   183
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   184
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   185
/* unpack bits from packed_data to unpacked_data */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   186
static inline int __unpack_bits(u16 *unpacked_data, u16 start_addr, u16 bit_count,  u8  *packed_data) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   187
  u8  temp, bit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   188
  u16 byte, coils_processed;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   189
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   190
  if ((0 == bit_count) || (65535-start_addr < bit_count-1))
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   191
    return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   192
  
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   193
  for(byte = 0, coils_processed = 0; coils_processed < bit_count; byte++) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   194
    temp = packed_data[byte] ;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   195
    for(bit = 0x01; (bit & 0xff) && (coils_processed < bit_count); bit <<= 1, coils_processed++) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   196
      unpacked_data[start_addr + coils_processed] = (temp & bit)?1:0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   197
    }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   198
  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   199
  return 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   200
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   201
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   202
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   203
static int __read_inbits   (void *mem_map, u16 start_addr, u16 bit_count, u8  *data_bytes) {
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   204
  int res = __pack_bits(((server_mem_t *)mem_map)->ro_bits, start_addr, bit_count, data_bytes);
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   205
  
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   206
  if (res >= 0) {
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   207
    /* update the flag and counter of Modbus requests we have processed. */
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   208
    ((server_mem_t *)mem_map)->flag_read_req_counter++;
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   209
    ((server_mem_t *)mem_map)->flag_read_req_flag = 1;
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   210
  }
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   211
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   212
  return res;
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   213
}
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   214
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   215
static int __read_outbits  (void *mem_map, u16 start_addr, u16 bit_count, u8  *data_bytes) {
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   216
  int res = __pack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes);
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   217
  
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   218
  if (res >= 0) {
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   219
    /* update the flag and counter of Modbus requests we have processed. */
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   220
    ((server_mem_t *)mem_map)->flag_read_req_counter++;
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   221
    ((server_mem_t *)mem_map)->flag_read_req_flag = 1;
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   222
  }
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   223
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   224
  return res;
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   225
}
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   226
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   227
static int __write_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8  *data_bytes) {
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   228
  int res = __unpack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes);
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   229
  
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   230
  if (res >= 0) {
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   231
    /* update the flag and counter of Modbus requests we have processed. */
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   232
    ((server_mem_t *)mem_map)->flag_write_req_counter++;
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   233
    ((server_mem_t *)mem_map)->flag_write_req_flag = 1;
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   234
  }
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   235
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   236
  return res;
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   237
}
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   238
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   239
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   240
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   241
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   242
static int __read_inwords  (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
1913
338e2f51b685 modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents: 1912
diff changeset
   243
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   244
  if ((start_addr + word_count) > MEM_AREA_SIZE)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   245
    return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
1913
338e2f51b685 modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents: 1912
diff changeset
   246
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   247
  /* update the flag and counter of Modbus requests we have processed. */
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   248
  ((server_mem_t *)mem_map)->flag_read_req_counter++;
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   249
  ((server_mem_t *)mem_map)->flag_read_req_flag = 1;
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   250
1913
338e2f51b685 modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents: 1912
diff changeset
   251
  /* use memcpy() because loop with pointers (u16 *) caused alignment problems */
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   252
  memcpy(/* dest */ (void *)data_words,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   253
         /* src  */ (void *)&(((server_mem_t *)mem_map)->ro_words[start_addr]),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   254
         /* size */ word_count * 2);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   255
  return 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   256
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   257
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   258
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   259
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   260
static int __read_outwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
1913
338e2f51b685 modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents: 1912
diff changeset
   261
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   262
  if ((start_addr + word_count) > MEM_AREA_SIZE)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   263
    return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
1913
338e2f51b685 modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents: 1912
diff changeset
   264
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   265
  /* update the flag and counter of Modbus requests we have processed. */
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   266
  ((server_mem_t *)mem_map)->flag_read_req_counter++;
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   267
  ((server_mem_t *)mem_map)->flag_read_req_flag = 1;
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   268
1913
338e2f51b685 modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents: 1912
diff changeset
   269
  /* use memcpy() because loop with pointers (u16 *) caused alignment problems */
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   270
  memcpy(/* dest */ (void *)data_words,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   271
         /* src  */ (void *)&(((server_mem_t *)mem_map)->rw_words[start_addr]),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   272
         /* size */ word_count * 2);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   273
  return 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   274
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   275
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   276
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   277
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   278
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   279
static int __write_outwords(void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
1913
338e2f51b685 modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents: 1912
diff changeset
   280
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   281
  if ((start_addr + word_count) > MEM_AREA_SIZE)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   282
    return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   283
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   284
  /* update the flag and counter of Modbus requests we have processed. */
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   285
  ((server_mem_t *)mem_map)->flag_write_req_counter++;
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   286
  ((server_mem_t *)mem_map)->flag_write_req_flag = 1;
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   287
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   288
  /* WARNING: The data returned in the data_words[] array is not guaranteed to be 16 bit aligned.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   289
   *           It is not therefore safe to cast it to an u16 data type.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   290
   *           The following code cannot be used. memcpy() is used instead.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   291
   */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   292
  /*
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   293
  for (count = 0; count < word_count ; count++)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   294
    ((server_mem_t *)mem_map)->rw_words[count + start_addr] = data_words[count];
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   295
  */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   296
  memcpy(/* dest */ (void *)&(((server_mem_t *)mem_map)->rw_words[start_addr]),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   297
         /* src  */ (void *)data_words,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   298
         /* size */ word_count * 2);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   299
  return 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   300
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   301
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   302
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   303
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   304
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   305
#include <pthread.h>
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   306
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   307
static void *__mb_server_thread(void *_server_node)  {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   308
	server_node_t *server_node = _server_node;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   309
	mb_slave_callback_t callbacks = { 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   310
			&__read_inbits,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   311
			&__read_outbits,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   312
			&__write_outbits,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   313
			&__read_inwords,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   314
			&__read_outwords,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   315
			&__write_outwords,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   316
			(void *)&(server_node->mem_area)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   317
			};  
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   318
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   319
	// Enable thread cancelation. Enabled is default, but set it anyway to be safe.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   320
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   321
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   322
	// mb_slave_run() should never return!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   323
	mb_slave_run(server_node->mb_nd /* nd */, callbacks, server_node->slave_id);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   324
	fprintf(stderr, "Modbus plugin: Modbus server for node %%s died unexpectedly!\n", server_node->location); /* should never occur */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   325
	return NULL;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   326
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   327
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   328
2480
8efa26af791d Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
parents: 2019 2479
diff changeset
   329
#define timespec_add(ts, sec, nsec) {		\
8efa26af791d Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
parents: 2019 2479
diff changeset
   330
	ts.tv_sec  +=  sec;			\
8efa26af791d Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
parents: 2019 2479
diff changeset
   331
	ts.tv_nsec += nsec;			\
8efa26af791d Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
parents: 2019 2479
diff changeset
   332
	if (ts.tv_nsec >= 1000000000) {		\
8efa26af791d Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
parents: 2019 2479
diff changeset
   333
		ts.tv_sec  ++;			\
8efa26af791d Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
parents: 2019 2479
diff changeset
   334
		ts.tv_nsec -= 1000000000;	\
8efa26af791d Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
parents: 2019 2479
diff changeset
   335
	}					\
8efa26af791d Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
parents: 2019 2479
diff changeset
   336
}
8efa26af791d Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
parents: 2019 2479
diff changeset
   337
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   338
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   339
static void *__mb_client_thread(void *_index)  {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   340
	int client_node_id = (char *)_index - (char *)NULL; // Use pointer arithmetic (more portable than cast)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   341
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   342
	// Enable thread cancelation. Enabled is default, but set it anyway to be safe.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   343
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   344
	
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   345
    /* loop the communication with the client
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   346
     * 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   347
         * When the client thread has difficulty communicating with remote client and/or server (network issues, for example),
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   348
         * then the communications get delayed and we will fall behind in the period. 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   349
         * 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   350
         * This is OK. Note that if the condition variable were to be signaled multiple times while the client thread is inside the same
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   351
         * Modbus transaction, then all those signals would be ignored.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   352
         * However, and since we keep the mutex locked during the communication cycle, it is not possible to signal the condition variable
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   353
         * during that time (it is only possible while the thread is blocked during the call to pthread_cond_wait().
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   354
         * 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   355
         * This means that when network issues eventually get resolved, we will NOT have a bunch of delayed activations to handle
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   356
         * in quick succession (which would goble up CPU time). 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   357
         * 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   358
         * Notice that the above property is valid whether the communication cycle is run with the mutex locked, or unlocked.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   359
         * Since it makes it easier to implement the correct semantics for the other activation methods if the communication cycle
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   360
         * is run with the mutex locked, then that is what we do.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   361
         * 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   362
     * Note that during all the communication cycle we will keep locked the mutex 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   363
     * (i.e. the mutex used together with the condition variable that will activate a new communication cycle)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   364
     * 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   365
     * Note that we never get to explicitly unlock this mutex. It will only be unlocked by the pthread_cond_wait()
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   366
     * call at the end of the cycle.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   367
     */
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   368
    pthread_mutex_lock(&(client_nodes[client_node_id].mutex));
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   369
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   370
	while (1) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   371
		/*
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   372
		struct timespec cur_time;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   373
		clock_gettime(CLOCK_MONOTONIC, &cur_time);
2687
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   374
		fprintf(stderr, "Modbus client thread (%%d) - new cycle (%%ld:%%ld)!\n", client_node_id, cur_time.tv_sec, cur_time.tv_nsec);
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   375
		*/
3733
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   376
		
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   377
		/* Variable use to specify delay to introduce between any two consecutive requests we send out to the same client
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   378
		 * Initially set to 0 since we don't want to introduce a delay before the very first request.
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   379
		 */
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   380
		struct timespec inter_request_delay;
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   381
		inter_request_delay.tv_sec  = 0;
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   382
		inter_request_delay.tv_nsec = 0;
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   383
		
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   384
		int req;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   385
		for (req=0; req < NUMBER_OF_CLIENT_REQTS; req ++){
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   386
			/* just do the requests belonging to the client */
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   387
			if (client_requests[req].client_node_id != client_node_id)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   388
				continue;
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   389
            
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   390
            /* only do the request if:
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   391
             *   - this request was explictly asked to be executed by the client program
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   392
             *  OR
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   393
             *   - the client thread was activated periodically
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   394
             *     (in which case we execute all the requests belonging to the client node)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   395
             */
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   396
            if ((client_requests[req].flag_exec_req == 0) && (client_nodes[client_requests[req].client_node_id].periodic_act == 0))
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   397
                continue;
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   398
            
2687
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   399
            /*
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   400
            fprintf(stderr, "Modbus client thread (%%d): RUNNING Modbus request %%d  (periodic = %%d  flag_exec_req = %%d)\n", 
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   401
                    client_node_id, req, client_nodes[client_requests[req].client_node_id].periodic_act, client_requests[req].flag_exec_req );
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   402
            */
3733
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   403
			
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   404
			/* Insert a delay between any two consecutive requests to the same client
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   405
			 * Needed because some clients will ignore our requests if we send them out too fast.
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   406
			 *
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   407
			 * Note that since we don't want to insert a delay before the very first request we will send, the inter_request_delay variable
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   408
			 * is first initialised to 0. It will be set to the correct delay after the first (and second, third, etc..) request has completed.
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   409
			 */
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   410
			clock_nanosleep(CLOCK_MONOTONIC, 0 /* relative sleep */, &inter_request_delay, NULL);
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   411
            
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   412
			int res_tmp = __execute_mb_request(req);
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   413
			client_requests[req].tn_error_code = 0; // assume success
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   414
			switch (res_tmp) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   415
			  case PORT_FAILURE: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   416
				if (res_tmp != client_nodes[client_node_id].prev_error)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   417
					fprintf(stderr, "Modbus plugin: Error connecting Modbus client %%s to remote server.\n", client_nodes[client_node_id].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   418
				client_nodes[client_node_id].prev_error = res_tmp;
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   419
				client_requests[req].tn_error_code = 1; // error accessing IP network, or serial interface
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   420
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   421
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   422
			  case INVALID_FRAME: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   423
				if ((res_tmp != client_requests[req].prev_error) && (0 == client_nodes[client_node_id].prev_error))
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   424
					fprintf(stderr, "Modbus plugin: Modbus client request configured at location %%s was unsuccesful. Server/slave returned an invalid/corrupted frame.\n", client_requests[req].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   425
				client_requests[req].prev_error = res_tmp;
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   426
				client_requests[req].tn_error_code = 2; // reply received from server was an invalid frame
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   427
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   428
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   429
			  case TIMEOUT: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   430
				if ((res_tmp != client_requests[req].prev_error) && (0 == client_nodes[client_node_id].prev_error))
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   431
					fprintf(stderr, "Modbus plugin: Modbus client request configured at location %%s timed out waiting for reply from server.\n", client_requests[req].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   432
				client_requests[req].prev_error = res_tmp;
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   433
				client_requests[req].tn_error_code = 3; // server did not reply before timeout expired
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   434
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   435
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   436
			  case MODBUS_ERROR: {
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   437
				if (client_requests[req].prev_error != client_requests[req].mb_error_code) {
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   438
					fprintf(stderr, "Modbus plugin: Modbus client request configured at location %%s was unsuccesful. Server/slave returned error code 0x%%2x", client_requests[req].location, client_requests[req].mb_error_code);
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   439
					if (client_requests[req].mb_error_code <= MAX_MODBUS_ERROR_CODE ) {
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   440
						fprintf(stderr, "(%%s)", modbus_error_messages[client_requests[req].mb_error_code]);
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   441
						fprintf(stderr, ".\n");
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   442
					}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   443
				}
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   444
				client_requests[req].prev_error = client_requests[req].mb_error_code;
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   445
				client_requests[req].tn_error_code = 4; // server returned a valid Modbus error frame
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   446
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   447
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   448
			  default: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   449
				if ((res_tmp >= 0) && (client_nodes[client_node_id].prev_error != 0)) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   450
					fprintf(stderr, "Modbus plugin: Modbus client %%s has reconnected to server/slave.\n", client_nodes[client_node_id].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   451
				}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   452
				if ((res_tmp >= 0) && (client_requests[req]        .prev_error != 0)) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   453
					fprintf(stderr, "Modbus plugin: Modbus client request configured at location %%s has succesfully resumed comunication.\n", client_requests[req].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   454
				}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   455
				client_nodes[client_node_id].prev_error = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   456
				client_requests[req]        .prev_error = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   457
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   458
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   459
			}
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   460
2714
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   461
			/* Set the flag_tn_error_code and flag_mb_error_code that are mapped onto
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   462
             * located BYTE variables, so the user program
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   463
             * knows how the communication is going.
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   464
             */
2714
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   465
            client_requests[req].flag_mb_error_code = client_requests[req].mb_error_code;
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   466
            client_requests[req].flag_tn_error_code = client_requests[req].tn_error_code;
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   467
            
3733
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   468
            /* We have just finished executing a client transaction request.
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   469
             * If the current cycle was activated by user request we reset the flag used to ask to run it
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   470
             */
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   471
            if (0 != client_requests[req].flag_exec_req) {
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   472
                client_requests[req].flag_exec_req     = 0;
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   473
                client_requests[req].flag_exec_started = 0;   
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   474
            }
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   475
            
3733
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   476
            /* We have just finished executing a client transaction request.
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   477
             * Set the inter request delay before we send the next request. Value of delay is set by user in beremiz GUI
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   478
             */
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   479
            inter_request_delay.tv_sec  =  client_nodes[client_node_id].req_delay / 1000; /* ms to seconds */
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   480
            inter_request_delay.tv_nsec = (client_nodes[client_node_id].req_delay %% 1000) * 1000 * 1000; /* ms to ns */
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   481
            
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   482
            //fprintf(stderr, "Modbus plugin: RUNNING<---> of Modbus request %%d  (periodic = %%d  flag_exec_req = %%d)\n", 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   483
            //        req, client_nodes[client_requests[req].client_node_id].periodic_act, client_requests[req].flag_exec_req );
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   484
        }
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   485
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   486
        // Wait for signal (from timer or explicit request from user program) before starting the next cycle
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   487
        {
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   488
            // No need to lock the mutex. Is is already locked just before the while(1) loop.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   489
            // Read the comment there to understand why.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   490
            // pthread_mutex_lock(&(client_nodes[client_node_id].mutex));
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   491
            
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   492
            /* the client thread has just finished a cycle, so all the flags used to signal an activation
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   493
             * and specify the activation source (periodic, user request, ...)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   494
             * get reset here, before waiting for a new activation.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   495
             */
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   496
            client_nodes[client_node_id].periodic_act = 0;            
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   497
            client_nodes[client_node_id].execute_req  = 0;
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   498
            
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   499
            while (client_nodes[client_node_id].execute_req == 0)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   500
                pthread_cond_wait(&(client_nodes[client_node_id].condv),
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   501
                                &(client_nodes[client_node_id].mutex)); 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   502
                            
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   503
            // We run the communication cycle with the mutex locked.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   504
            // Read the comment just above the while(1) to understand why.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   505
            // pthread_mutex_unlock(&(client_nodes[client_node_id].mutex));
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   506
        }
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   507
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   508
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   509
	// humour the compiler.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   510
	return NULL;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   511
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   512
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   513
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   514
2683
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   515
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   516
2687
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   517
/* Thread that simply implements a periodic 'timer',
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   518
 *  i.e. periodically sends signal to the  thread running __mb_client_thread()
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   519
 * 
2687
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   520
 * Note that we do not use a posix timer (timer_create() ) because there doesn't seem to be a way
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   521
 * of having the timer notify the thread that is portable across Xenomai and POSIX.
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   522
 * - SIGEV_THREAD    : not supported by Xenomai
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   523
 * - SIGEV_THREAD_ID : Linux specific (i.e. non POSIX)
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   524
 *                     Even so, I did not get it to work under Linux (issues with the header files)
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   525
 * - SIGEV_SIGNAL    : Will not work, as signal is sent to random thread in process!
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   526
 */
2683
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   527
static void *__mb_client_timer_thread(void *_index) {
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   528
	int client_node_id = (char *)_index - (char *)NULL; // Use pointer arithmetic (more portable than cast)
2687
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   529
	struct timespec next_cycle;
2683
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   530
2684
da8de4ef0449 Modbus: fix client timer invalid arguments.
Edouard Tisserant
parents: 2683
diff changeset
   531
	int period_sec  =  client_nodes[client_node_id].comm_period / 1000;          /* comm_period is in ms */
da8de4ef0449 Modbus: fix client timer invalid arguments.
Edouard Tisserant
parents: 2683
diff changeset
   532
	int period_nsec = (client_nodes[client_node_id].comm_period %%1000)*1000000; /* comm_period is in ms */
da8de4ef0449 Modbus: fix client timer invalid arguments.
Edouard Tisserant
parents: 2683
diff changeset
   533
2687
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   534
	// Enable thread cancelation. Enabled is default, but set it anyway to be safe.
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   535
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   536
    
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   537
    if (client_nodes[client_node_id].comm_period <= 0) {
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   538
        // No periodic activation required => nothing to do! 
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   539
        while (1) pause(); // wait to be canceled when program terminates (shutdown() is called)
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   540
        return NULL;  // not really necessary, just makes it easier to understand the code.
2684
da8de4ef0449 Modbus: fix client timer invalid arguments.
Edouard Tisserant
parents: 2683
diff changeset
   541
    }
da8de4ef0449 Modbus: fix client timer invalid arguments.
Edouard Tisserant
parents: 2683
diff changeset
   542
2687
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   543
	// get the current time
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   544
	clock_gettime(CLOCK_MONOTONIC, &next_cycle);
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   545
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   546
    while(1) {
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   547
        // Determine absolute time instant for starting the next cycle
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   548
        struct timespec prev_cycle, now;
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   549
        prev_cycle = next_cycle;
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   550
        timespec_add(next_cycle, period_sec, period_nsec);
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   551
                
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   552
        /* NOTE:
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   553
         * It is probably un-necessary to check for overflow of timer!
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   554
         * Even in 32 bit systems this will take at least 68 years since the computer booted
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   555
         * (remember, we are using CLOCK_MONOTONIC, which should start counting from 0
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   556
         * every time the system boots). On 64 bit systems, it will take over 
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   557
         * 10^11 years to overflow.
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   558
         */
2687
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   559
        clock_gettime(CLOCK_MONOTONIC, &now);
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   560
        if (next_cycle.tv_sec < prev_cycle.tv_sec) {
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   561
           /* Timer overflow. See NOTE B above */
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   562
            next_cycle = now;
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   563
            timespec_add(next_cycle, period_sec, period_nsec);
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   564
        }
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   565
        
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   566
        while (0 != clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_cycle, NULL));
2688
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   567
        
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   568
        /* signal the client node's condition variable on which the client node's thread should be waiting... */
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   569
        /* Since the communication cycle is run with the mutex locked, we use trylock() instead of lock() */
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   570
        if (pthread_mutex_trylock (&(client_nodes[client_node_id].mutex)) == 0) {
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   571
            client_nodes[client_node_id].execute_req  = 1; // tell the thread to execute
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   572
            client_nodes[client_node_id].periodic_act = 1; // tell the thread the activation was done by periodic timer   
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   573
            pthread_cond_signal (&(client_nodes[client_node_id].condv));
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   574
            pthread_mutex_unlock(&(client_nodes[client_node_id].mutex));
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   575
        } else {
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   576
            /* We never get to signal the thread for activation. But that is OK.
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   577
             * If it still in the communication cycle (during which the mutex is kept locked)
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   578
             * then that means that the communication cycle is falling behing in the periodic 
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   579
             * communication cycle, and we therefore need to skip a period.
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   580
             */
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   581
        }
2683
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   582
    }
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   583
2688
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   584
    return NULL; // humour the compiler -> will never be executed!
2683
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   585
}
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   586
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   587
1913
338e2f51b685 modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents: 1912
diff changeset
   588
int __cleanup_%(locstr)s ();
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   589
int __init_%(locstr)s (int argc, char **argv){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   590
	int index;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   591
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   592
	for (index=0; index < NUMBER_OF_CLIENT_NODES;index++) {
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   593
		client_nodes[index].mb_nd = -1;
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   594
        /* see comment in mb_runtime.h to understad why we need to initialize these entries */
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   595
        switch (client_nodes[index].node_address.naf) {
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   596
            case naf_tcp:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   597
                client_nodes[index].node_address.addr.tcp.host    = client_nodes[index].str1;
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   598
                client_nodes[index].node_address.addr.tcp.service = client_nodes[index].str2;
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   599
                break;
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   600
            case naf_rtu:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   601
                client_nodes[index].node_address.addr.rtu.device  = client_nodes[index].str1;
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   602
                break;
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   603
        }
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   604
    }
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   605
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   606
	for (index=0; index < NUMBER_OF_SERVER_NODES;index++) {
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   607
		// mb_nd with negative numbers indicate how far it has been initialised (or not)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   608
		//   -2  --> no modbus node created;  no thread  created
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   609
		//   -1  -->    modbus node created!; no thread  created
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   610
		//  >=0  -->    modbus node created!;    thread  created!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   611
		server_nodes[index].mb_nd = -2; 
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   612
		server_nodes[index].mem_area.flag_write_req_flag    = 0; 
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   613
		server_nodes[index].mem_area.flag_write_req_counter = 0; 
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
   614
		server_nodes[index].mem_area.flag_read_req_counter  = 0; 
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   615
		server_nodes[index].mem_area.flag_read_req_flag     = 0; 
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   616
        /* see comment in mb_runtime.h to understad why we need to initialize these entries */
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   617
        switch (server_nodes[index].node_address.naf) {
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   618
            case naf_tcp:
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   619
                server_nodes[index].node_address.addr.tcp.host    = server_nodes[index].str1;
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   620
                server_nodes[index].node_address.addr.tcp.service = server_nodes[index].str2;
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   621
                break;
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   622
            case naf_rtu:
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   623
                server_nodes[index].node_address.addr.rtu.device  = server_nodes[index].str1;
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   624
                break;
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   625
        }
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   626
	}
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   627
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   628
	/* modbus library init */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   629
	/* Note that TOTAL_xxxNODE_COUNT are the nodes required by _ALL_ the instances of the modbus
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   630
	 *  extension currently in the user's project. This file (MB_xx.c) is handling only one instance,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   631
	 *  but must initialize the library for all instances. Only the first call to mb_slave_and_master_init()
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   632
	 *  will result in memory being allocated. All subsequent calls (by other MB_xx,c files) will be ignored
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   633
	 *  by the mb_slave_and_master_init() funtion, as long as they are called with the same arguments.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   634
	 */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   635
	if (mb_slave_and_master_init(TOTAL_TCPNODE_COUNT, TOTAL_RTUNODE_COUNT, TOTAL_ASCNODE_COUNT) <0) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   636
		fprintf(stderr, "Modbus plugin: Error starting modbus library\n");
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   637
		// return imediately. Do NOT goto error_exit, as we did not get to
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   638
		//  start the modbus library!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   639
		return -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   640
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   641
	
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   642
	/* init each client request */
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   643
	/* Must be done _before_ launching the client threads!! */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   644
	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   645
        /* make sure flags connected to user program MB transaction start request are all reset */
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   646
        client_requests[index].flag_exec_req     = 0;
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   647
        client_requests[index].flag_exec_started = 0;        
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   648
        /* init the mutex for each client request */
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   649
        /* Must be done _before_ launching the client threads!! */
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   650
		if (pthread_mutex_init(&(client_requests[index].coms_buf_mutex), NULL)) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   651
			fprintf(stderr, "Modbus plugin: Error initializing request for modbus client node %%s\n", client_nodes[client_requests[index].client_node_id].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   652
			goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   653
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   654
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   655
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   656
	/* init each client connection to remote modbus server, and launch thread */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   657
	/* NOTE: All client_nodes[].init_state are initialised to 0 in the code 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   658
	 *       generated by the modbus plugin 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   659
	 */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   660
	for (index=0; index < NUMBER_OF_CLIENT_NODES;index++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   661
		/* establish client connection */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   662
		client_nodes[index].mb_nd = mb_master_connect (client_nodes[index].node_address);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   663
		if (client_nodes[index].mb_nd < 0){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   664
			fprintf(stderr, "Modbus plugin: Error creating modbus client node %%s\n", client_nodes[index].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   665
			goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   666
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   667
		client_nodes[index].init_state = 1; // we have created the node 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   668
		
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   669
		/* initialize the mutex variable that will be used by the thread handling the client node */
2685
f71c22b2ca25 Modbus: fix strange side effect of re-use of condition variables or mutex with Xenomai : without bzero of the associated types, mutex keeps locked.
Edouard Tisserant
parents: 2684
diff changeset
   670
        bzero(&(client_nodes[index].mutex), sizeof(pthread_mutex_t));
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   671
        if (pthread_mutex_init(&(client_nodes[index].mutex), NULL) < 0) {
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   672
			fprintf(stderr, "Modbus plugin: Error creating mutex for modbus client node %%s\n", client_nodes[index].location);
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   673
			goto error_exit;                
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   674
        }
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   675
		client_nodes[index].init_state = 2; // we have created the mutex
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   676
		
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   677
		/* initialize the condition variable that will be used by the thread handling the client node */
2685
f71c22b2ca25 Modbus: fix strange side effect of re-use of condition variables or mutex with Xenomai : without bzero of the associated types, mutex keeps locked.
Edouard Tisserant
parents: 2684
diff changeset
   678
        bzero(&(client_nodes[index].condv), sizeof(pthread_cond_t));
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   679
        if (pthread_cond_init(&(client_nodes[index].condv), NULL) < 0) {
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   680
			fprintf(stderr, "Modbus plugin: Error creating condition variable for modbus client node %%s\n", client_nodes[index].location);
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   681
			goto error_exit;                
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   682
        }
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   683
        client_nodes[index].execute_req = 0; //variable associated with condition variable
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   684
		client_nodes[index].init_state = 3; // we have created the condition variable
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   685
		
2683
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   686
		/* launch a thread to handle this client node timer */
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   687
		{
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   688
			int res = 0;
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   689
			pthread_attr_t attr;
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   690
			res |= pthread_attr_init(&attr);
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   691
			res |= pthread_create(&(client_nodes[index].timer_thread_id), &attr, &__mb_client_timer_thread, (void *)((char *)NULL + index));
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   692
			if (res !=  0) {
3859
098875cff39f Modbus: More verbose error message in case of thread creation problem
Edouard Tisserant
parents: 3733
diff changeset
   693
				fprintf(stderr, "Modbus plugin: Error (%%d) starting timer thread for modbus client node %%s\n", res, client_nodes[index].location);
2683
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   694
				goto error_exit;
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   695
			}
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   696
		}
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   697
        client_nodes[index].init_state = 4; // we have created the timer
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   698
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   699
		/* launch a thread to handle this client node */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   700
		{
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   701
			int res = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   702
			pthread_attr_t attr;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   703
			res |= pthread_attr_init(&attr);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   704
			res |= pthread_create(&(client_nodes[index].thread_id), &attr, &__mb_client_thread, (void *)((char *)NULL + index));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   705
			if (res !=  0) {
3859
098875cff39f Modbus: More verbose error message in case of thread creation problem
Edouard Tisserant
parents: 3733
diff changeset
   706
				fprintf(stderr, "Modbus plugin: Error (%%d) starting thread for modbus client node %%s\n", res, client_nodes[index].location);
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   707
				goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   708
			}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   709
		}
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   710
		client_nodes[index].init_state = 5; // we have created the thread
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   711
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   712
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   713
	/* init each local server */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   714
	/* NOTE: All server_nodes[].init_state are initialised to 0 in the code 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   715
	 *       generated by the modbus plugin 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   716
	 */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   717
	for (index=0; index < NUMBER_OF_SERVER_NODES;index++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   718
		/* create the modbus server */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   719
		server_nodes[index].mb_nd = mb_slave_new (server_nodes[index].node_address);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   720
		if (server_nodes[index].mb_nd < 0){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   721
			fprintf(stderr, "Modbus plugin: Error creating modbus server node %%s\n", server_nodes[index].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   722
			goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   723
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   724
		server_nodes[index].init_state = 1; // we have created the node
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   725
		
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   726
		/* launch a thread to handle this server node */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   727
		{
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   728
			int res = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   729
			pthread_attr_t attr;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   730
			res |= pthread_attr_init(&attr);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   731
			res |= pthread_create(&(server_nodes[index].thread_id), &attr, &__mb_server_thread, (void *)&(server_nodes[index]));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   732
			if (res !=  0) {
3859
098875cff39f Modbus: More verbose error message in case of thread creation problem
Edouard Tisserant
parents: 3733
diff changeset
   733
				fprintf(stderr, "Modbus plugin: Error (%%d) starting modbus server thread for node %%s\n", res, server_nodes[index].location);
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   734
				goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   735
			}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   736
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   737
		server_nodes[index].init_state = 2; // we have created the node and thread
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   738
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   739
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   740
	return 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   741
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   742
error_exit:
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   743
	__cleanup_%(locstr)s ();
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   744
	return -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   745
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   746
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   747
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   748
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   749
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   750
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   751
void __publish_%(locstr)s (){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   752
	int index;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   753
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   754
	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   755
		/* synchronize the PLC and MB buffers only for the output requests */
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   756
		if (client_requests[index].req_type == req_output){
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   757
            
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   758
            // lock the mutex brefore copying the data
2011
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   759
			if(pthread_mutex_trylock(&(client_requests[index].coms_buf_mutex)) == 0){
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   760
                
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   761
                // Check if user configured this MB request to be activated whenever the data to be written changes
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   762
                if (client_requests[index].write_on_change) {
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   763
                    // Let's check if the data did change...
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   764
                    // compare the data in plcv_buffer to coms_buffer
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   765
                    int res;
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   766
                    res = memcmp((void *)client_requests[index].coms_buffer /* buf 1 */,
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   767
                                 (void *)client_requests[index].plcv_buffer /* buf 2*/,
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   768
                                 REQ_BUF_SIZE * sizeof(u16) /* size in bytes */);
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   769
                    
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   770
                    // if data changed, activate execution request 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   771
                    if (0 != res)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   772
                        client_requests[index].flag_exec_req = 1;
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   773
                }
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   774
                
2011
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   775
                // copy from plcv_buffer to coms_buffer
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   776
                memcpy((void *)client_requests[index].coms_buffer /* destination */,
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   777
                       (void *)client_requests[index].plcv_buffer /* source */,
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   778
                       REQ_BUF_SIZE * sizeof(u16) /* size in bytes */);
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   779
                pthread_mutex_unlock(&(client_requests[index].coms_buf_mutex));
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   780
            }
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   781
		}
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   782
        /* if the user program set the execution request flag, then activate the thread
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   783
         *  that handles this Modbus client transaction so it gets a chance to be executed
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   784
         *  (but don't activate the thread if it has already been activated!)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   785
         * 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   786
         * NOTE that we do this, for both the IN and OUT mapped location, under this
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   787
         *  __publish_() function. The scan cycle of the PLC works as follows:
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   788
         *   - call __retrieve_()
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   789
         *   - execute user programs
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   790
         *   - call __publish_()
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   791
         *   - insert <delay> until time to start next periodic/cyclic scan cycle
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   792
         * 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   793
         *  In an attempt to be able to run the MB transactions during the <delay>
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   794
         *  interval in which not much is going on, we handle the user program
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   795
         *  requests to execute a specific MB transaction in this __publish_()
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   796
         *  function.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   797
         */
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   798
        if ((client_requests[index].flag_exec_req != 0) && (0 == client_requests[index].flag_exec_started)) {
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   799
            int client_node_id = client_requests[index].client_node_id;
2688
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   800
            
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   801
             /* We TRY to signal the client thread.
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   802
              * We do this because this function can be called at the end of the PLC scan cycle
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   803
              * and we don't want it to block at that time.
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   804
              */
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   805
             if (pthread_mutex_trylock(&(client_nodes[client_node_id].mutex)) == 0) {
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   806
                 client_nodes[client_node_id].execute_req = 1; // tell the thread to execute
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   807
                 pthread_cond_signal (&(client_nodes[client_node_id].condv));
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   808
                 pthread_mutex_unlock(&(client_nodes[client_node_id].mutex));
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   809
                 /* - upon success, set flag_exec_started
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   810
                  * - both flags (flag_exec_req and flag_exec_started) will be reset
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   811
                  *   once the transaction has completed.
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   812
                  */
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   813
                 client_requests[index].flag_exec_started = 1;    
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   814
             } else {
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   815
                 /* The mutex is locked => the client thread is currently executing MB transactions.
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   816
                  * We will try to activate it in the next PLC cycle...
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   817
                  * For now, do nothing.
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   818
                  */
4dd67aa45855 Modbus plugin: reformat code, and add comment.
Mario de Sousa <msousa@fe.up.pt>
parents: 2687
diff changeset
   819
             }
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   820
        }                    
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   821
    }
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   822
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   823
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   824
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   825
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   826
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   827
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   828
void __retrieve_%(locstr)s (){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   829
	int index;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   830
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   831
	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   832
		/*just do the input requests */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   833
		if (client_requests[index].req_type == req_input){
2011
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   834
			if(pthread_mutex_trylock(&(client_requests[index].coms_buf_mutex)) == 0){
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   835
                // copy from coms_buffer to plcv_buffer
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   836
                memcpy((void *)client_requests[index].plcv_buffer /* destination */,
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   837
                       (void *)client_requests[index].coms_buffer /* source */,
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   838
                       REQ_BUF_SIZE * sizeof(u16) /* size in bytes */);
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   839
                pthread_mutex_unlock(&(client_requests[index].coms_buf_mutex));
64268e87613e Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents: 1913
diff changeset
   840
            }
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   841
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   842
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   843
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   844
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   845
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   846
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   847
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   848
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   849
int __cleanup_%(locstr)s (){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   850
	int index, close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   851
	int res = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   852
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   853
	/* kill thread and close connections of each modbus client node */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   854
	for (index=0; index < NUMBER_OF_CLIENT_NODES; index++) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   855
		close = 0;
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   856
		if (client_nodes[index].init_state >= 5) {
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   857
			// thread was launched, so we try to cancel it!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   858
			close  = pthread_cancel(client_nodes[index].thread_id);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   859
			close |= pthread_join  (client_nodes[index].thread_id, NULL);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   860
			if (close < 0)
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   861
				fprintf(stderr, "Modbus plugin: Error closing thread for modbus client node %%s\n", client_nodes[index].location);
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   862
		}
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   863
		res |= close;
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   864
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   865
		close = 0;
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   866
		if (client_nodes[index].init_state >= 4) {
2687
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   867
			// timer thread was launched, so we try to cancel it!
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   868
			close  = pthread_cancel(client_nodes[index].timer_thread_id);
2683
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   869
			close |= pthread_join  (client_nodes[index].timer_thread_id, NULL);
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   870
			if (close < 0)
2683
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   871
				fprintf(stderr, "Modbus plugin: Error closing timer thread for modbus client node %%s\n", client_nodes[index].location);
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2665
diff changeset
   872
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   873
		}
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   874
		res |= close;
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   875
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   876
		close = 0;
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   877
		if (client_nodes[index].init_state >= 3) {
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   878
			// condition variable was created, so we try to destroy it!
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   879
			close  = pthread_cond_destroy(&(client_nodes[index].condv));
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   880
			if (close < 0)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   881
				fprintf(stderr, "Modbus plugin: Error destroying condition variable for modbus client node %%s\n", client_nodes[index].location);
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   882
		}
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   883
		res |= close;
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   884
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   885
		close = 0;
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   886
		if (client_nodes[index].init_state >= 2) {
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   887
			// mutex was created, so we try to destroy it!
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   888
			close  = pthread_mutex_destroy(&(client_nodes[index].mutex));
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   889
			if (close < 0)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2521
diff changeset
   890
				fprintf(stderr, "Modbus plugin: Error destroying mutex for modbus client node %%s\n", client_nodes[index].location);
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   891
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   892
		res |= close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   893
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   894
		close = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   895
		if (client_nodes[index].init_state >= 1) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   896
			// modbus client node was created, so we try to close it!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   897
			close = mb_master_close (client_nodes[index].mb_nd);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   898
			if (close < 0){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   899
				fprintf(stderr, "Modbus plugin: Error closing modbus client node %%s\n", client_nodes[index].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   900
				// We try to shut down as much as possible, so we do not return noW!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   901
			}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   902
			client_nodes[index].mb_nd = -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   903
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   904
		res |= close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   905
		client_nodes[index].init_state = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   906
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   907
	
2687
c79c5d49ba34 Modbus plugin: fix runtime periodic timer (don't use POSIX timer)
Mario de Sousa <msousa@fe.up.pt>
parents: 2685
diff changeset
   908
//fprintf(stderr, "Modbus plugin: __cleanup_%%s()  5  close=%%d   res=%%d\n", client_nodes[index].location, close, res);
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   909
	/* kill thread and close connections of each modbus server node */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   910
	for (index=0; index < NUMBER_OF_SERVER_NODES; index++) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   911
		close = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   912
		if (server_nodes[index].init_state >= 2) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   913
			// thread was launched, so we try to cancel it!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   914
			close  = pthread_cancel(server_nodes[index].thread_id);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   915
			close |= pthread_join  (server_nodes[index].thread_id, NULL);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   916
			if (close < 0)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   917
				fprintf(stderr, "Modbus plugin: Error closing thread for modbus server %%s\n", server_nodes[index].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   918
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   919
		res |= close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   920
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   921
		close = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   922
		if (server_nodes[index].init_state >= 1) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   923
			// modbus server node was created, so we try to close it!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   924
			close = mb_slave_close (server_nodes[index].mb_nd);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   925
			if (close < 0) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   926
				fprintf(stderr, "Modbus plugin: Error closing node for modbus server %%s (%%d)\n", server_nodes[index].location, server_nodes[index].mb_nd);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   927
				// We try to shut down as much as possible, so we do not return noW!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   928
			}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   929
			server_nodes[index].mb_nd = -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   930
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   931
		res |= close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   932
		server_nodes[index].init_state = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   933
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   934
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   935
	/* destroy the mutex of each client request */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   936
	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   937
		if (pthread_mutex_destroy(&(client_requests[index].coms_buf_mutex))) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   938
			fprintf(stderr, "Modbus plugin: Error destroying request for modbus client node %%s\n", client_nodes[client_requests[index].client_node_id].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   939
			// We try to shut down as much as possible, so we do not return noW!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   940
			res |= -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   941
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   942
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   943
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   944
	/* modbus library close */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   945
	//fprintf(stderr, "Shutting down modbus library...\n");
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   946
	if (mb_slave_and_master_done()<0) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   947
		fprintf(stderr, "Modbus plugin: Error shutting down modbus library\n");
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   948
		res |= -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   949
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   950
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   951
	return res;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   952
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   953
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   954
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   955
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   956
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   957
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   958
/**********************************************/
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   959
/** Functions for Beremiz web interface.     **/
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   960
/**********************************************/
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   961
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   962
/*
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   963
 * Beremiz has a program to run on the PLC (Beremiz_service.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   964
 * to handle downloading of compiled programs, start/stop of PLC, etc.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   965
 * (see runtime/PLCObject.py for start/stop, loading, ...)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   966
 * 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   967
 * This service also includes a web server to access PLC state (start/stop)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   968
 * and to change some basic confiuration parameters.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   969
 * (see runtime/NevowServer.py for the web server)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   970
 * 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   971
 * The web server allows for extensions, where additional configuration
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   972
 * parameters may be changed on the running/downloaded PLC.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   973
 * Modbus plugin also comes with an extension to the web server, through
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   974
 * which the basic Modbus plugin configuration parameters may be changed
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   975
 * 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   976
 * These parameters are changed _after_ the code (.so file) is loaded into 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   977
 * memmory. These changes may be applied before (or after) the code starts
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   978
 * running (i.e. before or after __init_() ets called)! 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   979
 * 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   980
 * The following functions are never called from other C code. They are 
3733
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   981
 * called instead from the python code in modbus/web_settings.py, that
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   982
 * implements the web server extension for configuring Modbus parameters.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   983
 */
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   984
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   985
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   986
/* The number of Cient nodes (i.e. the number of entries in the client_nodes array)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   987
 * The number of Server nodes (i.e. the numb. of entries in the server_nodes array)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   988
 * 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   989
 * These variables are also used by the Modbus web config code to determine 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   990
 * whether the current loaded PLC includes the Modbus plugin
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   991
 * (so it should make the Modbus parameter web interface visible to the user).
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   992
 */
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   993
const int __modbus_plugin_client_node_count = NUMBER_OF_CLIENT_NODES;
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   994
const int __modbus_plugin_server_node_count = NUMBER_OF_SERVER_NODES;
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   995
const int __modbus_plugin_param_string_size = MODBUS_PARAM_STRING_SIZE;
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   996
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   997
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   998
3733
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
   999
/* NOTE: We could have the python code in modbus/web_settings.py
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1000
 *       directly access the server_node_t and client_node_t structures,
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1001
 *       however this would create a tight coupling between these two
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1002
 *       disjoint pieces of code.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1003
 *       Any change to the server_node_t or client_node_t structures would
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1004
 *       require the python code to be changed accordingly. I have therefore 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1005
 *       opted to create get/set functions, one for each parameter.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1006
 * 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1007
 *       We also convert the enumerated constants naf_ascii, etc...
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1008
 *       (from node_addr_family_t in modbus/mb_addr.h)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1009
 *       into strings so as to decouple the python code that will be calling
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1010
 *       these functions from the Modbus library code definitions.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1011
 */
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1012
const char *addr_type_str[] = {
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1013
        [naf_ascii] = "ascii",
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1014
        [naf_rtu  ] = "rtu",
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1015
        [naf_tcp  ] = "tcp"    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1016
};
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1017
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1018
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1019
#define __safe_strcnpy(str_dest, str_orig, max_size) {  \
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1020
    strncpy(str_dest, str_orig, max_size);              \
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1021
    str_dest[max_size - 1] = '\0';                      \
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1022
}
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1023
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1024
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1025
/* NOTE: The host, port and device parameters are strings that may be changed 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1026
 *       (by calling the following functions) after loading the compiled code 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1027
 *       (.so file) into memory, but before the code starts running
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1028
 *       (i.e. before __init_() gets called).
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1029
 *       This means that the host, port and device parameters may be changed
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1030
 *       _before_ they get mapped onto the str1 and str2 variables by __init_(),
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1031
 *       which is why the following functions must access the str1 and str2 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1032
 *       parameters directly.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1033
 */
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1034
const char *       __modbus_get_ClientNode_config_name(int nodeid)  {return client_nodes[nodeid].config_name;                    }
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1035
const char *       __modbus_get_ClientNode_host       (int nodeid)  {return client_nodes[nodeid].str1;                           }
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1036
const char *       __modbus_get_ClientNode_port       (int nodeid)  {return client_nodes[nodeid].str2;                           }
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1037
const char *       __modbus_get_ClientNode_device     (int nodeid)  {return client_nodes[nodeid].str1;                           }
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1038
int                __modbus_get_ClientNode_baud       (int nodeid)  {return client_nodes[nodeid].node_address.addr.rtu.baud;     }
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1039
int                __modbus_get_ClientNode_parity     (int nodeid)  {return client_nodes[nodeid].node_address.addr.rtu.parity;   }
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1040
int                __modbus_get_ClientNode_stop_bits  (int nodeid)  {return client_nodes[nodeid].node_address.addr.rtu.stop_bits;}
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1041
u64                __modbus_get_ClientNode_comm_period(int nodeid)  {return client_nodes[nodeid].comm_period;                    }
3733
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
  1042
u64                __modbus_get_ClientNode_req_delay  (int nodeid)  {return client_nodes[nodeid].req_delay;                      }
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1043
const char *       __modbus_get_ClientNode_addr_type  (int nodeid)  {return addr_type_str[client_nodes[nodeid].node_address.naf];}
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1044
                                                                                                                        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1045
const char *       __modbus_get_ServerNode_config_name(int nodeid)  {return server_nodes[nodeid].config_name;                    }
2665
978cf239c689 Modbus plugin web interface: show the keyword "#ANY#" on TCP servers
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
  1046
const char *       __modbus_get_ServerNode_host       (int nodeid)  {char*x=server_nodes[nodeid].str1; return (x[0]=='\0'?"#ANY#":x); }
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
  1047
const char *       __modbus_get_ServerNode_port       (int nodeid)  {return server_nodes[nodeid].str2;                           }
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
  1048
const char *       __modbus_get_ServerNode_device     (int nodeid)  {return server_nodes[nodeid].str1;                           }
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1049
int                __modbus_get_ServerNode_baud       (int nodeid)  {return server_nodes[nodeid].node_address.addr.rtu.baud;     }
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1050
int                __modbus_get_ServerNode_parity     (int nodeid)  {return server_nodes[nodeid].node_address.addr.rtu.parity;   }
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1051
int                __modbus_get_ServerNode_stop_bits  (int nodeid)  {return server_nodes[nodeid].node_address.addr.rtu.stop_bits;}
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
  1052
u8                 __modbus_get_ServerNode_slave_id   (int nodeid)  {return server_nodes[nodeid].slave_id;                       }
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1053
const char *       __modbus_get_ServerNode_addr_type  (int nodeid)  {return addr_type_str[server_nodes[nodeid].node_address.naf];}
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1054
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1055
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1056
void __modbus_set_ClientNode_host       (int nodeid, const char * value)  {__safe_strcnpy(client_nodes[nodeid].str1, value, MODBUS_PARAM_STRING_SIZE);}
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1057
void __modbus_set_ClientNode_port       (int nodeid, const char * value)  {__safe_strcnpy(client_nodes[nodeid].str2, value, MODBUS_PARAM_STRING_SIZE);}
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1058
void __modbus_set_ClientNode_device     (int nodeid, const char * value)  {__safe_strcnpy(client_nodes[nodeid].str1, value, MODBUS_PARAM_STRING_SIZE);}
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1059
void __modbus_set_ClientNode_baud       (int nodeid, int          value)  {client_nodes[nodeid].node_address.addr.rtu.baud      = value;}
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1060
void __modbus_set_ClientNode_parity     (int nodeid, int          value)  {client_nodes[nodeid].node_address.addr.rtu.parity    = value;}
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1061
void __modbus_set_ClientNode_stop_bits  (int nodeid, int          value)  {client_nodes[nodeid].node_address.addr.rtu.stop_bits = value;}
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1062
void __modbus_set_ClientNode_comm_period(int nodeid, u64          value)  {client_nodes[nodeid].comm_period                     = value;}
3733
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2723
diff changeset
  1063
void __modbus_set_ClientNode_req_delay  (int nodeid, u64          value)  {client_nodes[nodeid].req_delay                       = value;}
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1064
                                                                                                                        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
  1065
2665
978cf239c689 Modbus plugin web interface: show the keyword "#ANY#" on TCP servers
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
  1066
void __modbus_set_ServerNode_host       (int nodeid, const char * value)  {if (strcmp(value,"#ANY#")==0) value = "";
978cf239c689 Modbus plugin web interface: show the keyword "#ANY#" on TCP servers
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
  1067
                                                                           __safe_strcnpy(server_nodes[nodeid].str1, value, MODBUS_PARAM_STRING_SIZE);}
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
  1068
void __modbus_set_ServerNode_port       (int nodeid, const char * value)  {__safe_strcnpy(server_nodes[nodeid].str2, value, MODBUS_PARAM_STRING_SIZE);}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
  1069
void __modbus_set_ServerNode_device     (int nodeid, const char * value)  {__safe_strcnpy(server_nodes[nodeid].str1, value, MODBUS_PARAM_STRING_SIZE);}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
  1070
void __modbus_set_ServerNode_baud       (int nodeid, int          value)  {server_nodes[nodeid].node_address.addr.rtu.baud      = value;}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
  1071
void __modbus_set_ServerNode_parity     (int nodeid, int          value)  {server_nodes[nodeid].node_address.addr.rtu.parity    = value;}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
  1072
void __modbus_set_ServerNode_stop_bits  (int nodeid, int          value)  {server_nodes[nodeid].node_address.addr.rtu.stop_bits = value;}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
  1073
void __modbus_set_ServerNode_slave_id   (int nodeid, u8           value)  {server_nodes[nodeid].slave_id                        = value;}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
  1074