py_ext/plc_python.c
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Fri, 15 Jun 2018 16:12:50 +0300
changeset 2184 bbd2364fbf71
parent 1470 f65ab5ff91d1
child 2272 28b0a783975e
permissions -rw-r--r--
Add argument to set list of source files to check

This is necessary, in case you want to use docker run in pretxncommit
Mercurial hook. Unfortunately for some reason if hg running in
container can't find just created commit defined by $HG_NODE. And it
throws error 'unknown revision'.

Here is example for hgrc.

[hooks]
pretxncommit.linter = hg status -m -n -a -n -I '**.py' --change $HG_NODE > files.lst && docker run --volume=$PWD:/beremiz --workdir="/beremiz" --volume=$PWD/../CanFestival-3:/CanFestival-3 --memory=1g --entrypoint=/beremiz/tests/tools/check_source.sh skvorl/beremiz-requirements --files-to-check files.lst
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     1
/*
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     2
 * Python Asynchronous execution code
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     3
 *
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     4
 * PLC put python commands in a fifo, respecting execution order
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     5
 * with the help of C pragmas inserted in python_eval FB code
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     6
 *
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     7
 * Buffer content is read asynchronously, (from non real time part),
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     8
 * commands are executed and result stored for later use by PLC.
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     9
 *
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    10
 * In this implementation, fifo is a list of pointer to python_eval
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    11
 * function blocks structures. Some local variables have been added in
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    12
 * python_eval interface. We use those local variables as buffer and state
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    13
 * flags.
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    14
 *
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    15
 * */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    16
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    17
#include "iec_types_all.h"
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    18
#include "POUS.h"
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    19
#include <string.h>
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    20
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    21
/* The fifo (fixed size, as number of FB is fixed) */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    22
static PYTHON_EVAL* EvalFBs[%(python_eval_fb_count)d];
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    23
/* Producer and consumer cursors */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    24
static int Current_PLC_EvalFB;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    25
static int Current_Python_EvalFB;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    26
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    27
/* A global IEC-Python gateway state, for use inside python_eval FBs*/
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    28
static int PythonState;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    29
#define PYTHON_LOCKED_BY_PYTHON 0
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    30
#define PYTHON_LOCKED_BY_PLC 1
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    31
#define PYTHON_MUSTWAKEUP 2
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    32
#define PYTHON_FINISHED 4
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    33
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    34
/* Each python_eval FunctionBlock have it own state */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    35
#define PYTHON_FB_FREE 0
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    36
#define PYTHON_FB_REQUESTED 1
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    37
#define PYTHON_FB_PROCESSING 2
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    38
#define PYTHON_FB_ANSWERED 3
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    39
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    40
int WaitPythonCommands(void);
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    41
void UnBlockPythonCommands(void);
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    42
int TryLockPython(void);
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    43
void UnLockPython(void);
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    44
void LockPython(void);
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    45
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 721
diff changeset
    46
int __init_py_ext()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    47
{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    48
	int i;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    49
	/* Initialize cursors */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    50
	Current_Python_EvalFB = 0;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    51
	Current_PLC_EvalFB = 0;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    52
	PythonState = PYTHON_LOCKED_BY_PYTHON;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    53
	for(i = 0; i < %(python_eval_fb_count)d; i++)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    54
		EvalFBs[i] = NULL;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    55
  return 0;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    56
}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    57
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 721
diff changeset
    58
void __cleanup_py_ext()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    59
{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    60
	PythonState = PYTHON_FINISHED;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    61
	UnBlockPythonCommands();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    62
}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    63
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 721
diff changeset
    64
void __retrieve_py_ext()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    65
{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    66
	/* Check Python thread is not being
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    67
	 * modifying internal python_eval data */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    68
	PythonState = TryLockPython() ?
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    69
	                PYTHON_LOCKED_BY_PLC :
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    70
	                PYTHON_LOCKED_BY_PYTHON;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    71
	/* If python thread _is_ in, then PythonState remains PYTHON_LOCKED_BY_PYTHON
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    72
	 * and python_eval will no do anything */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    73
}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    74
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 721
diff changeset
    75
void __publish_py_ext()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    76
{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    77
	if(PythonState & PYTHON_LOCKED_BY_PLC){
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    78
		/* If runnig PLC did push something in the fifo*/
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    79
		if(PythonState & PYTHON_MUSTWAKEUP){
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    80
			/* WakeUp python thread */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    81
			UnBlockPythonCommands();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    82
		}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    83
		UnLockPython();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    84
	}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    85
}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    86
/**
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    87
 * Called by the PLC, each time a python_eval
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    88
 * FB instance is executed
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    89
 */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    90
void __PythonEvalFB(int poll, PYTHON_EVAL* data__)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    91
{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    92
	/* detect rising edge on TRIG to trigger evaluation */
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
    93
	if(((__GET_VAR(data__->TRIG) && !__GET_VAR(data__->TRIGM1)) ||
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    94
	   /* polling is equivalent to trig on value rather than on rising edge*/
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
    95
	    (poll && __GET_VAR(data__->TRIG) )) &&
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    96
	    /* trig only if not already trigged */
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
    97
	    __GET_VAR(data__->TRIGGED) == 0){
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    98
		/* mark as trigged */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
    99
	    __SET_VAR(data__->, TRIGGED,, 1);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   100
		/* make a safe copy of the code */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   101
		__SET_VAR(data__->, PREBUFFER,, __GET_VAR(data__->CODE));
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   102
	}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   103
	/* retain value for next rising edge detection */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   104
	__SET_VAR(data__->, TRIGM1,, __GET_VAR(data__->TRIG));
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   105
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   106
	/* python thread is not in ? */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   107
	if( PythonState & PYTHON_LOCKED_BY_PLC){
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   108
		/* if some answer are waiting, publish*/
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
   109
		if(__GET_VAR(data__->STATE) == PYTHON_FB_ANSWERED){
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   110
			/* Copy buffer content into result*/
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   111
			__SET_VAR(data__->, RESULT,, __GET_VAR(data__->BUFFER));
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   112
			/* signal result presece to PLC*/
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   113
			__SET_VAR(data__->, ACK,, 1);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   114
			/* Mark as free */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   115
			__SET_VAR(data__->, STATE,, PYTHON_FB_FREE);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   116
			/* mark as not trigged */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   117
			if(!poll)
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   118
			    __SET_VAR(data__->, TRIGGED,, 0);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   119
			/*printf("__PythonEvalFB pop %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);*/
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   120
		}else if(poll){
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   121
			/* when in polling, no answer == ack down */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   122
		    __SET_VAR(data__->, ACK,, 0);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   123
		}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   124
		/* got the order to act ?*/
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
   125
		if(__GET_VAR(data__->TRIGGED) == 1 &&
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   126
		   /* and not already being processed */
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
   127
		   __GET_VAR(data__->STATE) == PYTHON_FB_FREE)
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   128
		{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   129
			/* Enter the block in the fifo
613
be0ceea3b6dd fixed warnings in plc_python.c
Edouard Tisserant
parents: 483
diff changeset
   130
			 * Don't have to check if fifo cell is free
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   131
			 * as fifo size == FB count, and a FB cannot
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   132
			 * be requested twice */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   133
			EvalFBs[Current_PLC_EvalFB] = data__;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   134
			/* copy into BUFFER local*/
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   135
			__SET_VAR(data__->, BUFFER,, __GET_VAR(data__->PREBUFFER));
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   136
			/* Set ACK pin to low so that we can set a rising edge on result */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   137
			if(!poll){
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   138
				/* when not polling, a new answer imply reseting ack*/
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   139
			    __SET_VAR(data__->, ACK,, 0);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   140
			}else{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   141
				/* when in polling, acting reset trigger */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   142
			    __SET_VAR(data__->, TRIGGED,, 0);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   143
			}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   144
			/* Mark FB busy */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   145
			__SET_VAR(data__->, STATE,, PYTHON_FB_REQUESTED);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   146
			/* Have to wakeup python thread in case he was asleep */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   147
			PythonState |= PYTHON_MUSTWAKEUP;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   148
			/*printf("__PythonEvalFB push %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);*/
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   149
			/* Get a new line */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   150
			Current_PLC_EvalFB = (Current_PLC_EvalFB + 1) %% %(python_eval_fb_count)d;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   151
		}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   152
	}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   153
}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   154
851
666f5bdad301 Added FBID variable to PY_EVAL evaluation context. FBID does identify uniquely py_eval block instance triggering execution
Edouard Tisserant
parents: 728
diff changeset
   155
char* PythonIterator(char* result, void** id)
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   156
{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   157
	char* next_command;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   158
	PYTHON_EVAL* data__;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   159
	//printf("PythonIterator result %%s\n", result);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 454
diff changeset
   160
    /*emergency exit*/
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 454
diff changeset
   161
    if(PythonState & PYTHON_FINISHED) return NULL;
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   162
	/* take python mutex to prevent changing PLC data while PLC running */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   163
	LockPython();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   164
	/* Get current FB */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   165
	data__ = EvalFBs[Current_Python_EvalFB];
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   166
	if(data__ && /* may be null at first run */
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
   167
	    __GET_VAR(data__->STATE) == PYTHON_FB_PROCESSING){ /* some answer awaited*/
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   168
	   	/* If result not None */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   169
	   	if(result){
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   170
			/* Get results len */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   171
	   	    __SET_VAR(data__->, BUFFER, .len, strlen(result));
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   172
			/* prevent results overrun */
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
   173
			if(__GET_VAR(data__->BUFFER, .len) > STR_MAX_LEN)
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   174
			{
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   175
			    __SET_VAR(data__->, BUFFER, .len, STR_MAX_LEN);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   176
				/* TODO : signal error */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   177
			}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   178
			/* Copy results to buffer */
613
be0ceea3b6dd fixed warnings in plc_python.c
Edouard Tisserant
parents: 483
diff changeset
   179
			strncpy((char*)__GET_VAR(data__->BUFFER, .body), result, __GET_VAR(data__->BUFFER,.len));
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   180
	   	}else{
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   181
	   	    __SET_VAR(data__->, BUFFER, .len, 0);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   182
	   	}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   183
		/* remove block from fifo*/
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   184
		EvalFBs[Current_Python_EvalFB] = NULL;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   185
		/* Mark block as answered */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   186
		__SET_VAR(data__->, STATE,, PYTHON_FB_ANSWERED);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   187
		/* Get a new line */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   188
		Current_Python_EvalFB = (Current_Python_EvalFB + 1) %% %(python_eval_fb_count)d;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   189
		//printf("PythonIterator ++ Current_Python_EvalFB %%d\n", Current_Python_EvalFB);
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   190
	}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   191
	/* while next slot is empty */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   192
	while(((data__ = EvalFBs[Current_Python_EvalFB]) == NULL) ||
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   193
	 	  /* or doesn't contain command */
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
   194
	      __GET_VAR(data__->STATE) != PYTHON_FB_REQUESTED)
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   195
	{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   196
		UnLockPython();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   197
		/* wait next FB to eval */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   198
		//printf("PythonIterator wait\n");
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   199
		if(WaitPythonCommands()) return NULL;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   200
		/*emergency exit*/
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   201
		if(PythonState & PYTHON_FINISHED) return NULL;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   202
		LockPython();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   203
	}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   204
	/* Mark block as processing */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   205
	__SET_VAR(data__->, STATE,, PYTHON_FB_PROCESSING);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   206
	//printf("PythonIterator\n");
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   207
	/* make BUFFER a null terminated string */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   208
	__SET_VAR(data__->, BUFFER, .body[__GET_VAR(data__->BUFFER, .len)], 0);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   209
	/* next command is BUFFER */
613
be0ceea3b6dd fixed warnings in plc_python.c
Edouard Tisserant
parents: 483
diff changeset
   210
	next_command = (char*)__GET_VAR(data__->BUFFER, .body);
1288
adc79fc44079 Fixed two typos in py_ext : FBID was not current but previous py_eval block FBID, and compiled AST cache was filled buy never used.
Edouard Tisserant
parents: 851
diff changeset
   211
	*id=data__;
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   212
	/* free python mutex */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   213
	UnLockPython();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   214
	/* return the next command to eval */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   215
	return next_command;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   216
}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   217