modbus/mb_runtime.c
author Edouard Tisserant
Thu, 17 Oct 2019 15:48:09 +0200
branchsvghmi
changeset 2799 f5da343b9b63
parent 2521 48ebcbe7f19b
child 2647 990004083eb8
permissions -rw-r--r--
SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
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() */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    28
#include "mb_slave_and_master.h"
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    29
#include "MB_%(locstr)s.h"
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    30
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    31
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    32
#define MAX_MODBUS_ERROR_CODE 11
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    33
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
    34
    /* 0 */ "",                             /* un-used -> no error! */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    35
    /* 1 */ "illegal/unsuported function",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    36
    /* 2 */ "illegal data address",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    37
    /* 3 */ "illegal data value",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    38
    /* 4 */ "slave device failure",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    39
    /* 5 */ "acknowledge -> slave intends to reply later",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    40
    /* 6 */ "slave device busy",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    41
    /* 7 */ "negative acknowledge",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    42
    /* 8 */ "memory parity error",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    43
    /* 9 */ "",                             /* undefined by Modbus */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    44
    /* 10*/ "gateway path unavalilable",
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    45
    /* 11*/ "gateway target device failed to respond"
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    46
};    
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    47
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    48
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    49
/* Execute a modbus client transaction/request */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    50
static int __execute_mb_request(int request_id){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    51
	switch (client_requests[request_id].mb_function){
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
	case  1: /* read coils */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    54
		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
    55
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    56
					client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    57
					client_requests[request_id].coms_buffer,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    58
					(int) client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    59
					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
    60
					client_requests[request_id].retries,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    61
					&(client_requests[request_id].error_code),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    62
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    63
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    64
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    65
	case  2: /* read discrete inputs */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    66
		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
    67
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    68
					client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    69
					client_requests[request_id].coms_buffer,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    70
					(int) client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    71
					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
    72
					client_requests[request_id].retries,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    73
					&(client_requests[request_id].error_code),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    74
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    75
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    76
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    77
	case  3: /* read holding registers */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    78
		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
    79
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    80
					client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    81
					client_requests[request_id].coms_buffer,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    82
					(int) client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    83
					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
    84
					client_requests[request_id].retries,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    85
					&(client_requests[request_id].error_code),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    86
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    87
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    88
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    89
	case  4: /* read input registers */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    90
		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
    91
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    92
					client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    93
					client_requests[request_id].coms_buffer,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    94
					(int) client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    95
					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
    96
					client_requests[request_id].retries,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    97
					&(client_requests[request_id].error_code),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    98
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    99
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   100
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   101
	case  5: /* write single coil */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   102
		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
   103
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   104
					client_requests[request_id].coms_buffer[0],
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   105
					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
   106
					client_requests[request_id].retries,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   107
					&(client_requests[request_id].error_code),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   108
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   109
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   110
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   111
	case  6: /* write single register */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   112
		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
   113
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   114
					client_requests[request_id].coms_buffer[0],
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   115
					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
   116
					client_requests[request_id].retries,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   117
					&(client_requests[request_id].error_code),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   118
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   119
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   120
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   121
	case  7: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   122
	case  8: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   123
	case  9: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   124
	case 10: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   125
	case 11: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   126
	case 12: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   127
	case 13: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   128
	case 14: break; /* function not yet supported */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   129
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   130
	case 15: /* write multiple coils */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   131
		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
   132
					 client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   133
					 client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   134
					 client_requests[request_id].coms_buffer,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   135
					 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
   136
					 client_requests[request_id].retries,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   137
					 &(client_requests[request_id].error_code),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   138
					 &(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   139
					 &(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   140
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   141
	case 16: /* write multiple registers */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   142
		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
   143
					client_requests[request_id].address,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   144
					client_requests[request_id].count,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   145
					client_requests[request_id].coms_buffer,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   146
					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
   147
					client_requests[request_id].retries,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   148
					&(client_requests[request_id].error_code),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   149
					&(client_requests[request_id].resp_timeout),
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   150
					&(client_requests[request_id].coms_buf_mutex));
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   151
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   152
	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
   153
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   154
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   155
	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
   156
	return -1;
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
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   160
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   161
/* pack bits from unpacked_data to packed_data */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   162
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
   163
  u8 bit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   164
  u16 byte, coils_processed;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   165
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   166
  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
   167
    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
   168
  
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   169
  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
   170
    packed_data[byte] = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   171
    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
   172
      if(unpacked_data[start_addr + coils_processed])
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   173
            packed_data[byte] |=  bit; /*   set bit */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   174
      else  packed_data[byte] &= ~bit; /* reset bit */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   175
    }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   176
  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   177
  return 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   178
}
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
/* unpack bits from packed_data to unpacked_data */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   182
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
   183
  u8  temp, bit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   184
  u16 byte, coils_processed;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   185
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   186
  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
   187
    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
   188
  
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   189
  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
   190
    temp = packed_data[byte] ;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   191
    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
   192
      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
   193
    }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   194
  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   195
  return 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   196
}
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
static int __read_inbits   (void *mem_map, u16 start_addr, u16 bit_count, u8  *data_bytes)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   200
  {return   __pack_bits(((server_mem_t *)mem_map)->ro_bits, start_addr, bit_count, data_bytes);}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   201
static int __read_outbits  (void *mem_map, u16 start_addr, u16 bit_count, u8  *data_bytes)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   202
  {return   __pack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes);}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   203
static int __write_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8  *data_bytes)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   204
  {return __unpack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes); }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   205
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   206
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   207
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   208
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
   209
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   210
  if ((start_addr + word_count) > MEM_AREA_SIZE)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   211
    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
   212
338e2f51b685 modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents: 1912
diff changeset
   213
  /* 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
   214
  memcpy(/* dest */ (void *)data_words,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   215
         /* 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
   216
         /* size */ word_count * 2);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   217
  return 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   218
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   219
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   220
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   221
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   222
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
   223
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   224
  if ((start_addr + word_count) > MEM_AREA_SIZE)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   225
    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
   226
338e2f51b685 modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents: 1912
diff changeset
   227
  /* 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
   228
  memcpy(/* dest */ (void *)data_words,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   229
         /* 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
   230
         /* size */ word_count * 2);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   231
  return 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   232
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   233
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   234
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   235
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   236
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   237
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
   238
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   239
  if ((start_addr + word_count) > MEM_AREA_SIZE)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   240
    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
   241
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   242
  /* 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
   243
   *           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
   244
   *           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
   245
   */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   246
  /*
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   247
  for (count = 0; count < word_count ; count++)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   248
    ((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
   249
  */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   250
  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
   251
         /* src  */ (void *)data_words,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   252
         /* size */ word_count * 2);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   253
  return 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   254
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   255
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
#include <pthread.h>
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   260
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   261
static void *__mb_server_thread(void *_server_node)  {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   262
	server_node_t *server_node = _server_node;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   263
	mb_slave_callback_t callbacks = { 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   264
			&__read_inbits,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   265
			&__read_outbits,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   266
			&__write_outbits,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   267
			&__read_inwords,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   268
			&__read_outwords,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   269
			&__write_outwords,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   270
			(void *)&(server_node->mem_area)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   271
			};  
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   272
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   273
	// 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
   274
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
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
	// mb_slave_run() should never return!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   277
	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
   278
	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
   279
	return NULL;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   280
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   281
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   282
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
   283
#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
   284
	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
   285
	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
   286
	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
   287
		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
   288
		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
   289
	}					\
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
   290
}
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
   291
1912
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
static void *__mb_client_thread(void *_index)  {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   294
	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
   295
	struct timespec next_cycle;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   296
	int period_sec  =  client_nodes[client_node_id].comm_period / 1000;          /* comm_period is in ms */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   297
	int period_nsec = (client_nodes[client_node_id].comm_period %%1000)*1000000; /* comm_period is in ms */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   298
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   299
	// 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
   300
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
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
	// get the current time
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   303
	clock_gettime(CLOCK_MONOTONIC, &next_cycle);
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
	// loop the communication with the client
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   306
	while (1) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   307
		/*
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   308
		struct timespec cur_time;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   309
		clock_gettime(CLOCK_MONOTONIC, &cur_time);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   310
		fprintf(stderr, "Modbus client thread - new cycle (%%ld:%%ld)!\n", cur_time.tv_sec, cur_time.tv_nsec);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   311
		*/
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   312
		int req;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   313
		for (req=0; req < NUMBER_OF_CLIENT_REQTS; req ++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   314
			/*just do the requests belonging to the client */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   315
			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
   316
				continue;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   317
			int res_tmp = __execute_mb_request(req);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   318
			switch (res_tmp) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   319
			  case PORT_FAILURE: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   320
				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
   321
					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
   322
				client_nodes[client_node_id].prev_error = res_tmp;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   323
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   324
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   325
			  case INVALID_FRAME: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   326
				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
   327
					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
   328
				client_requests[req].prev_error = res_tmp;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   329
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   330
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   331
			  case TIMEOUT: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   332
				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
   333
					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
   334
				client_requests[req].prev_error = res_tmp;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   335
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   336
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   337
			  case MODBUS_ERROR: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   338
				if (client_requests[req].prev_error != client_requests[req].error_code) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   339
					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].error_code);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   340
					if (client_requests[req].error_code <= MAX_MODBUS_ERROR_CODE ) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   341
						fprintf(stderr, "(%%s)", modbus_error_messages[client_requests[req].error_code]);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   342
						fprintf(stderr, ".\n");
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   343
					}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   344
				}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   345
				client_requests[req].prev_error = client_requests[req].error_code;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   346
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   347
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   348
			  default: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   349
				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
   350
					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
   351
				}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   352
				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
   353
					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
   354
				}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   355
				client_nodes[client_node_id].prev_error = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   356
				client_requests[req]        .prev_error = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   357
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   358
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   359
			}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   360
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   361
		// Determine absolute time instant for starting the next cycle
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
   362
		struct timespec prev_cycle, now;
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
   363
		prev_cycle = next_cycle;
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
   364
		timespec_add(next_cycle, period_sec, period_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
   365
		/* NOTE A:
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
   366
		 * When we have difficulty communicating with remote client and/or server, then the communications get delayed and we will
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
   367
		 * fall behind in the period. This means that when communication is re-established we may end up running this loop continuously
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
   368
		 * for some time until we catch up.
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
   369
		 * This is undesirable, so we detect it by making sure the next_cycle will start in the future.
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
   370
		 * When this happens we will switch from a purely periodic task _activation_ sequence, to a fixed task suspension interval.
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
   371
		 * 
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
   372
		 * NOTE B:
2521
48ebcbe7f19b Long due merge
Edouard Tisserant
parents: 2480 2507
diff changeset
   373
		 * It probably does not make sense to check for overflow of timer - so we don't do it for now!
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   374
		 * Even in 32 bit systems this will take at least 68 years since the computer booted
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   375
		 * (remember, we are using CLOCK_MONOTONIC, which should start counting from 0
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   376
		 * every time the system boots). On 64 bit systems, it will take over 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   377
		 * 10^11 years to overflow.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   378
		 */
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
   379
		clock_gettime(CLOCK_MONOTONIC, &now);
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
   380
		if (  ((now.tv_sec > next_cycle.tv_sec) || ((now.tv_sec == next_cycle.tv_sec) && (now.tv_nsec > next_cycle.tv_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
   381
		   /* We are falling behind. See NOTE A above */
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
   382
		   || (next_cycle.tv_sec < prev_cycle.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
   383
		   /* Timer overflow. See NOTE B above */
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
   384
		   ) {
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
   385
			next_cycle = now;
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
   386
			timespec_add(next_cycle, period_sec, period_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
   387
		}
2521
48ebcbe7f19b Long due merge
Edouard Tisserant
parents: 2480 2507
diff changeset
   388
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   389
		clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_cycle, NULL);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   390
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   391
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   392
	// humour the compiler.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   393
	return NULL;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   394
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   395
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   396
1913
338e2f51b685 modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents: 1912
diff changeset
   397
int __cleanup_%(locstr)s ();
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   398
int __init_%(locstr)s (int argc, char **argv){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   399
	int index;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   400
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   401
	for (index=0; index < NUMBER_OF_CLIENT_NODES;index++)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   402
		client_nodes[index].mb_nd = -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   403
	for (index=0; index < NUMBER_OF_SERVER_NODES;index++)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   404
		// 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
   405
		//   -2  --> no modbus node created;  no thread  created
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   406
		//   -1  -->    modbus node created!; no thread  created
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   407
		//  >=0  -->    modbus node created!;    thread  created!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   408
		server_nodes[index].mb_nd = -2; 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   409
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   410
	/* modbus library init */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   411
	/* 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
   412
	 *  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
   413
	 *  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
   414
	 *  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
   415
	 *  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
   416
	 */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   417
	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
   418
		fprintf(stderr, "Modbus plugin: Error starting modbus library\n");
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   419
		// 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
   420
		//  start the modbus library!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   421
		return -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   422
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   423
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   424
	/* init the mutex for each client request */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   425
	/* Must be done _before_ launching the client threads!! */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   426
	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   427
		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
   428
			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
   429
			goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   430
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   431
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   432
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   433
	/* 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
   434
	/* 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
   435
	 *       generated by the modbus plugin 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   436
	 */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   437
	for (index=0; index < NUMBER_OF_CLIENT_NODES;index++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   438
		/* establish client connection */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   439
		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
   440
		if (client_nodes[index].mb_nd < 0){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   441
			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
   442
			goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   443
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   444
		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
   445
		
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   446
		/* launch a thread to handle this client node */
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
			int res = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   449
			pthread_attr_t attr;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   450
			res |= pthread_attr_init(&attr);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   451
			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
   452
			if (res !=  0) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   453
				fprintf(stderr, "Modbus plugin: Error starting modbus client thread for node %%s\n", client_nodes[index].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   454
				goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   455
			}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   456
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   457
		client_nodes[index].init_state = 2; // we have created the node and a thread
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
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   460
	/* init each local server */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   461
	/* 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
   462
	 *       generated by the modbus plugin 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   463
	 */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   464
	for (index=0; index < NUMBER_OF_SERVER_NODES;index++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   465
		/* create the modbus server */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   466
		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
   467
		if (server_nodes[index].mb_nd < 0){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   468
			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
   469
			goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   470
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   471
		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
   472
		
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   473
		/* launch a thread to handle this server node */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   474
		{
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   475
			int res = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   476
			pthread_attr_t attr;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   477
			res |= pthread_attr_init(&attr);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   478
			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
   479
			if (res !=  0) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   480
				fprintf(stderr, "Modbus plugin: Error starting modbus server thread for node %%s\n", server_nodes[index].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   481
				goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   482
			}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   483
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   484
		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
   485
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   486
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   487
	return 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   488
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   489
error_exit:
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   490
	__cleanup_%(locstr)s ();
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   491
	return -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   492
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   493
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   494
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   495
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   496
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   497
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   498
void __publish_%(locstr)s (){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   499
	int index;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   500
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   501
	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   502
		/*just do the output requests */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   503
		if (client_requests[index].req_type == req_output){
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
   504
			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
   505
                // 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
   506
                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
   507
                       (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
   508
                       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
   509
                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
   510
            }
1912
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
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   514
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   515
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   516
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   517
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   518
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   519
void __retrieve_%(locstr)s (){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   520
	int index;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   521
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   522
	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   523
		/*just do the input requests */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   524
		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
   525
			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
   526
                // 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
   527
                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
   528
                       (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
   529
                       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
   530
                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
   531
            }
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   532
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   533
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   534
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   535
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   536
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   537
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   538
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   539
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   540
int __cleanup_%(locstr)s (){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   541
	int index, close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   542
	int res = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   543
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   544
	/* 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
   545
	for (index=0; index < NUMBER_OF_CLIENT_NODES; index++) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   546
		close = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   547
		if (client_nodes[index].init_state >= 2) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   548
			// thread was launched, so we try to cancel it!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   549
			close  = pthread_cancel(client_nodes[index].thread_id);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   550
			close |= pthread_join  (client_nodes[index].thread_id, NULL);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   551
			if (close < 0)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   552
				fprintf(stderr, "Modbus plugin: Error closing thread for modbus client %%s\n", client_nodes[index].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   553
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   554
		res |= close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   555
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   556
		close = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   557
		if (client_nodes[index].init_state >= 1) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   558
			// 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
   559
			close = mb_master_close (client_nodes[index].mb_nd);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   560
			if (close < 0){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   561
				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
   562
				// 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
   563
			}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   564
			client_nodes[index].mb_nd = -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   565
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   566
		res |= close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   567
		client_nodes[index].init_state = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   568
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   569
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   570
	/* 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
   571
	for (index=0; index < NUMBER_OF_SERVER_NODES; index++) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   572
		close = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   573
		if (server_nodes[index].init_state >= 2) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   574
			// thread was launched, so we try to cancel it!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   575
			close  = pthread_cancel(server_nodes[index].thread_id);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   576
			close |= pthread_join  (server_nodes[index].thread_id, NULL);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   577
			if (close < 0)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   578
				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
   579
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   580
		res |= close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   581
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   582
		close = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   583
		if (server_nodes[index].init_state >= 1) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   584
			// 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
   585
			close = mb_slave_close (server_nodes[index].mb_nd);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   586
			if (close < 0) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   587
				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
   588
				// 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
   589
			}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   590
			server_nodes[index].mb_nd = -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   591
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   592
		res |= close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   593
		server_nodes[index].init_state = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   594
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   595
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   596
	/* destroy the mutex of each client request */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   597
	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   598
		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
   599
			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
   600
			// 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
   601
			res |= -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   602
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   603
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   604
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   605
	/* modbus library close */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   606
	//fprintf(stderr, "Shutting down modbus library...\n");
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   607
	if (mb_slave_and_master_done()<0) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   608
		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
   609
		res |= -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   610
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   611
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   612
	return res;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   613
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   614