modbus/mb_runtime.c
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Sat, 29 Sep 2018 17:00:01 +0300
changeset 2402 a1df3102327d
parent 2019 92f02bb17c7e
child 2480 8efa26af791d
child 2507 0f6dae98ddc5
permissions -rw-r--r--
ignore missing MotionLibrary

to fix pylint error 'E0401 # (import-error) Unable to import 'X'
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
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   283
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   284
static void *__mb_client_thread(void *_index)  {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   285
	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
   286
	struct timespec next_cycle;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   287
	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
   288
	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
   289
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   290
	// 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
   291
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
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
	// get the current time
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   294
	clock_gettime(CLOCK_MONOTONIC, &next_cycle);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   295
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   296
	// loop the communication with the client
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   297
	while (1) {
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
		struct timespec cur_time;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   300
		clock_gettime(CLOCK_MONOTONIC, &cur_time);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   301
		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
   302
		*/
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   303
		int req;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   304
		for (req=0; req < NUMBER_OF_CLIENT_REQTS; req ++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   305
			/*just do the requests belonging to the client */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   306
			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
   307
				continue;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   308
			int res_tmp = __execute_mb_request(req);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   309
			switch (res_tmp) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   310
			  case PORT_FAILURE: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   311
				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
   312
					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
   313
				client_nodes[client_node_id].prev_error = res_tmp;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   314
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   315
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   316
			  case INVALID_FRAME: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   317
				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
   318
					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
   319
				client_requests[req].prev_error = res_tmp;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   320
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   321
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   322
			  case TIMEOUT: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   323
				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
   324
					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
   325
				client_requests[req].prev_error = res_tmp;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   326
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   327
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   328
			  case MODBUS_ERROR: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   329
				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
   330
					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
   331
					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
   332
						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
   333
						fprintf(stderr, ".\n");
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   334
					}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   335
				}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   336
				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
   337
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   338
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   339
			  default: {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   340
				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
   341
					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
   342
				}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   343
				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
   344
					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
   345
				}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   346
				client_nodes[client_node_id].prev_error = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   347
				client_requests[req]        .prev_error = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   348
				break;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   349
			  }
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   350
			}
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
		// Determine absolute time instant for starting the next cycle
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   353
		// struct timespec prev_cycle;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   354
		// prev_cycle = next_cycle;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   355
		next_cycle.tv_sec  += period_sec;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   356
		next_cycle.tv_nsec += period_nsec;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   357
		if (next_cycle.tv_nsec >= 1000000000) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   358
			next_cycle.tv_sec  ++;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   359
			next_cycle.tv_nsec -= 1000000000;
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
		/* It probably does not make sense to check for overflow of timer.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   362
		 * 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
   363
		 * (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
   364
		 * 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
   365
		 * 10^11 years to overflow.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   366
		 */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   367
		/*
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   368
		if (next_cycle.tv_sec) < prev_cycle.tv_sec) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   369
			// we will lose some precision by reading the time again, 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   370
			// but it is better than the alternative...
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   371
			clock_gettime(CLOCK_MONOTONIC, &next_cycle);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   372
			next_cycle.tv_sec  += period_sec;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   373
			next_cycle.tv_nsec += period_nsec;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   374
			if (next_cycle.tv_nsec >= 1000000000) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   375
				next_cycle.tv_sec  ++;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   376
				next_cycle.tv_nsec -= 1000000000;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   377
			}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   378
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   379
		*/
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   380
		clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_cycle, NULL);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   381
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   382
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   383
	// humour the compiler.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   384
	return NULL;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   385
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   386
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   387
1913
338e2f51b685 modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents: 1912
diff changeset
   388
int __cleanup_%(locstr)s ();
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   389
int __init_%(locstr)s (int argc, char **argv){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   390
	int index;
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
	for (index=0; index < NUMBER_OF_CLIENT_NODES;index++)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   393
		client_nodes[index].mb_nd = -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   394
	for (index=0; index < NUMBER_OF_SERVER_NODES;index++)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   395
		// 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
   396
		//   -2  --> no modbus node created;  no thread  created
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   397
		//   -1  -->    modbus node created!; no thread  created
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   398
		//  >=0  -->    modbus node created!;    thread  created!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   399
		server_nodes[index].mb_nd = -2; 
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
	/* modbus library init */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   402
	/* 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
   403
	 *  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
   404
	 *  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
   405
	 *  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
   406
	 *  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
   407
	 */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   408
	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
   409
		fprintf(stderr, "Modbus plugin: Error starting modbus library\n");
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   410
		// 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
   411
		//  start the modbus library!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   412
		return -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   413
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   414
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   415
	/* init the mutex for each client request */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   416
	/* Must be done _before_ launching the client threads!! */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   417
	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   418
		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
   419
			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
   420
			goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   421
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   422
	}
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 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
   425
	/* 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
   426
	 *       generated by the modbus plugin 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   427
	 */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   428
	for (index=0; index < NUMBER_OF_CLIENT_NODES;index++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   429
		/* establish client connection */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   430
		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
   431
		if (client_nodes[index].mb_nd < 0){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   432
			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
   433
			goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   434
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   435
		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
   436
		
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   437
		/* launch a thread to handle this client node */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   438
		{
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   439
			int res = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   440
			pthread_attr_t attr;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   441
			res |= pthread_attr_init(&attr);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   442
			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
   443
			if (res !=  0) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   444
				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
   445
				goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   446
			}
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
		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
   449
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   450
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   451
	/* init each local server */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   452
	/* 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
   453
	 *       generated by the modbus plugin 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   454
	 */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   455
	for (index=0; index < NUMBER_OF_SERVER_NODES;index++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   456
		/* create the modbus server */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   457
		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
   458
		if (server_nodes[index].mb_nd < 0){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   459
			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
   460
			goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   461
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   462
		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
   463
		
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   464
		/* launch a thread to handle this server node */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   465
		{
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   466
			int res = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   467
			pthread_attr_t attr;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   468
			res |= pthread_attr_init(&attr);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   469
			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
   470
			if (res !=  0) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   471
				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
   472
				goto error_exit;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   473
			}
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
		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
   476
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   477
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   478
	return 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   479
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   480
error_exit:
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   481
	__cleanup_%(locstr)s ();
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   482
	return -1;
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
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
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
void __publish_%(locstr)s (){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   490
	int index;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   491
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   492
	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   493
		/*just do the output requests */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   494
		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
   495
			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
   496
                // 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
   497
                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
   498
                       (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
   499
                       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
   500
                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
   501
            }
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   502
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   503
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   504
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   505
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   506
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   507
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   508
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   509
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   510
void __retrieve_%(locstr)s (){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   511
	int index;
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
	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   514
		/*just do the input requests */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   515
		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
   516
			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
   517
                // 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
   518
                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
   519
                       (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
   520
                       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
   521
                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
   522
            }
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   523
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   524
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   525
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   526
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   527
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   528
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   529
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   530
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   531
int __cleanup_%(locstr)s (){
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   532
	int index, close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   533
	int res = 0;
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
	/* 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
   536
	for (index=0; index < NUMBER_OF_CLIENT_NODES; index++) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   537
		close = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   538
		if (client_nodes[index].init_state >= 2) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   539
			// thread was launched, so we try to cancel it!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   540
			close  = pthread_cancel(client_nodes[index].thread_id);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   541
			close |= pthread_join  (client_nodes[index].thread_id, NULL);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   542
			if (close < 0)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   543
				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
   544
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   545
		res |= close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   546
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   547
		close = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   548
		if (client_nodes[index].init_state >= 1) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   549
			// 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
   550
			close = mb_master_close (client_nodes[index].mb_nd);
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 modbus client node %%s\n", client_nodes[index].location);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   553
				// 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
   554
			}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   555
			client_nodes[index].mb_nd = -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   556
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   557
		res |= close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   558
		client_nodes[index].init_state = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   559
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   560
	
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   561
	/* 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
   562
	for (index=0; index < NUMBER_OF_SERVER_NODES; index++) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   563
		close = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   564
		if (server_nodes[index].init_state >= 2) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   565
			// thread was launched, so we try to cancel it!
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   566
			close  = pthread_cancel(server_nodes[index].thread_id);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   567
			close |= pthread_join  (server_nodes[index].thread_id, NULL);
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   568
			if (close < 0)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   569
				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
   570
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   571
		res |= close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   572
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   573
		close = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   574
		if (server_nodes[index].init_state >= 1) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   575
			// 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
   576
			close = mb_slave_close (server_nodes[index].mb_nd);
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 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
   579
				// 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
   580
			}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   581
			server_nodes[index].mb_nd = -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   582
		}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   583
		res |= close;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   584
		server_nodes[index].init_state = 0;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   585
	}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   586
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   587
	/* destroy the mutex of each client request */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   588
	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   589
		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
   590
			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
   591
			// 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
   592
			res |= -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   593
		}
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
	/* modbus library close */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   597
	//fprintf(stderr, "Shutting down modbus library...\n");
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   598
	if (mb_slave_and_master_done()<0) {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   599
		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
   600
		res |= -1;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   601
	}
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
	return res;
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