po_hi_lua.h 7.2 KB
Newer Older
jdelange's avatar
jdelange committed
1
2
3
4
5
/*
 * This is a part of PolyORB-HI-C distribution, a minimal
 * middleware written for generated code from AADL models.
 * You should use it with the Ocarina toolsuite.
 *
yoogx's avatar
yoogx committed
6
 * For more informations, please visit http://taste.tuxfamily.org/wiki
jdelange's avatar
jdelange committed
7
 *
yoogx's avatar
yoogx committed
8
 * Copyright (C) 2012-2014 ESA & ISAE.
jdelange's avatar
jdelange committed
9
10
11
12
13
14
 */


#ifndef __PO_HI_LUA_H__
#define __PO_HI_LUA_H__

jdelange's avatar
jdelange committed
15
16
#include <po_hi_config.h>

jdelange's avatar
jdelange committed
17
18
19
20
21
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <po_hi_returns.h>

jdelange's avatar
jdelange committed
22
23
24
25
#ifdef POSIX
#include <string.h>
#endif

jdelange's avatar
jdelange committed
26
27
#define __PO_HI_LUA_FUNCTION_NAME_MAX_SIZE 100

jdelange's avatar
jdelange committed
28
#ifdef __PO_HI_USE_LUA
jdelange's avatar
jdelange committed
29
30
31
#include <lua/lua.h>
#include <lua/lualib.h>
#include <lua/lauxlib.h>
jdelange's avatar
jdelange committed
32
33
34
35
36
37
38

typedef struct
{
   lua_State*  state;
   int         nb_args;
   char        function_name[__PO_HI_LUA_FUNCTION_NAME_MAX_SIZE];
}__po_hi_lua_context_t;
jdelange's avatar
jdelange committed
39

jdelange's avatar
jdelange committed
40
41
/*!
 * \fn __po_hi_lua_load (__po_hi_lua_context_t*, const char*);
jdelange's avatar
jdelange committed
42
 * \brief load a lua script and initialize a lua context
jdelange's avatar
jdelange committed
43
 *
jdelange's avatar
jdelange committed
44
45
46
 * this function takes the following arguments:
 *   - 1st arg: a lua context that will contain the execution of the script
 *   - 2nd arg: the name of the script
jdelange's avatar
jdelange committed
47
 *
jdelange's avatar
jdelange committed
48
49
50
51
 *  it returns the potential values:
 *    - __po_hi_failure: fails to load the script
 *    - __po_hi_invalid: invalid lua context
 *    - __po_hi_success: successfully load the script
jdelange's avatar
jdelange committed
52
 */
jdelange's avatar
jdelange committed
53
54
int __po_hi_lua_load (__po_hi_lua_context_t*, const char*);

jdelange's avatar
jdelange committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*!
 * \fn __po_hi_lua_load (__po_hi_lua_context_t*, const char*);
 * \brief load a lua script and initialize a lua context
 *
 * this function takes the following arguments:
 *   - 1st arg: a lua context that will contain the execution of the script
 *   - 2nd arg: the name of the script
 *
 *  it returns the potential values:
 *    - __po_hi_failure: fails to load the script
 *    - __po_hi_invalid: invalid lua context
 *    - __po_hi_success: successfully load the script
 */
int __po_hi_lua_load_str (__po_hi_lua_context_t*, const char*);

jdelange's avatar
jdelange committed
70
71
72
73
74
75
76
77
78
79
80
81
/*!
 * \fn __po_hi_lua_init_function_call (__po_hi_lua_context_t*, const char*);
 * \brief Initialize a function-call within a LUA execution context
 *
 * This function takes the following arguments:
 *   - 1st arg: A LUA context that contains the function to be called.
 *   - 2nd arg: The name of the function to be called.
 *
 *  It returns the potential values:
 *    - __PO_HI_INVALID: invalid LUA context or invalid function name
 *    - __PO_HI_SUCCESS: successful operation
 */
jdelange's avatar
jdelange committed
82
83
int __po_hi_lua_init_function_call (__po_hi_lua_context_t*, const char*);

jdelange's avatar
jdelange committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*!
 * \fn __po_hi_lua_perform_function_call (__po_hi_lua_context_t*);
 * \brief Perform a function call previously initalized.
 *
 * In fact, this function really performs the function calls. When
 * you want to make a call to a function from a LUA script, you have
 * to perform the following function calls:
 * __po_hi_lua_load                    : load the script
 * __po_hi_lua_init_function_call      : prepare the function call
 * __po_hi_lua_push_XXXX               : push the arguments of the function
 * __po_hi_lua_perform_function_call   : finally make the effective call
 *
 * This function takes the following arguments:
 *   - 1st arg: A LUA context that contains the function to be called and was
 *              used during the previous __po_hi_lua_init_function_call call
 *
 *  It returns the potential values:
 *    - __PO_HI_ERROR:   error while calling the LUA function
 *    - __PO_HI_SUCCESS: successful operation
 */

jdelange's avatar
jdelange committed
105
106
int __po_hi_lua_perform_function_call (__po_hi_lua_context_t*);

jdelange's avatar
jdelange committed
107
108
109
110
111
112
113
114
115
116
117
118
119
/*!
 * \fn __po_hi_lua_push_number (__po_hi_lua_context_t*, int);
 * \brief Push a number value on the stack before calling a LUA function.
 *
 * This function takes the following arguments:
 *   - 1st arg: A LUA context that executed a script
 *   - 2nd arg: The value to put on the stack
 *
 *  It returns the potential values:
 *    - __PO_HI_FAILURE: fails to put the value on the stack
 *    - __PO_HI_INVALID: invalid LUA context
 *    - __PO_HI_SUCCESS: successfully push the value on the LUA stack
 */
jdelange's avatar
jdelange committed
120
121
int __po_hi_lua_push_number (__po_hi_lua_context_t*, int);

jdelange's avatar
jdelange committed
122
123
124
125
126
127
128
129
130
131
132
133
134
/*!
 * \fn __po_hi_lua_push_boolean (__po_hi_lua_context_t*, int);
 * \brief Push a boolean value on the stack before calling a LUA function.
 *
 * This function takes the following arguments:
 *   - 1st arg: A LUA context that executed a script
 *   - 2nd arg: The value to put on the stack
 *
 *  It returns the potential values:
 *    - __PO_HI_FAILURE: fails to put the value on the stack
 *    - __PO_HI_INVALID: invalid LUA context
 *    - __PO_HI_SUCCESS: successfully push the value on the LUA stack
 */
jdelange's avatar
jdelange committed
135
136
int __po_hi_lua_push_boolean (__po_hi_lua_context_t*, int);

jdelange's avatar
jdelange committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*!
 * \fn __po_hi_lua_push_string (__po_hi_lua_context_t*, char*);
 * \brief Push a string value on the stack before calling a LUA function.
 *
 * This function takes the following arguments:
 *   - 1st arg: A LUA context that executed a script
 *   - 2nd arg: The value to put on the stack (a string)
 *
 *  It returns the potential values:
 *    - __PO_HI_FAILURE: fails to put the value on the stack
 *    - __PO_HI_INVALID: invalid LUA context
 *    - __PO_HI_SUCCESS: successfully push the value on the LUA stack
 */

jdelange's avatar
jdelange committed
151
152
int __po_hi_lua_push_string (__po_hi_lua_context_t*, char*);

jdelange's avatar
jdelange committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*!
 * \fn __po_hi_lua_get_boolean (__po_hi_lua_context_t*, char*, int*);
 * \brief Get a number value from a global variable from a LUA script
 *        and inject it in C code.
 *
 * This function takes the following arguments:
 *   - 1st arg: A LUA context that executed a script
 *   - 2nd arg: The global variable name in the LUA script
 *   - 2rd arg: Pointer to the integer value to be filled.
 *
 *  It returns the potential values:
 *    - __PO_HI_FAILURE: fails to convert the variable to a number
 *                       or non-existent variable
 *    - __PO_HI_SUCCESS: successfully convert the variable from LUA to C
 *
 */
jdelange's avatar
jdelange committed
169
170
int __po_hi_lua_get_number (__po_hi_lua_context_t*, char*, int*);

jdelange's avatar
jdelange committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*!
 * \fn __po_hi_lua_get_boolean (__po_hi_lua_context_t*, char*, int*);
 * \brief Get a boolean value from the global variable from a LUA script
 *
 * This function takes the following arguments:
 *   - 1st arg: A LUA context that executed a script
 *   - 2nd arg: The global variable name in the LUA script
 *   - 2rd arg: Pointer to the boolean/integer value to be filled.
 *
 *  It returns the potential values:
 *    - __PO_HI_FAILURE: fails to convert the variable to a boolean
 *                       or non-existent variable
 *    - __PO_HI_SUCCESS: successfully convert the variable from LUA to C
 *
 */
jdelange's avatar
jdelange committed
186
187
int __po_hi_lua_get_boolean (__po_hi_lua_context_t*, char*, int*);

jdelange's avatar
jdelange committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*!
 * \fn __po_hi_lua_get_string (__po_hi_lua_context_t*, char*, char*);
 * \brief Get a string value from the global variable from a LUA script
 *
 * This function takes the following arguments:
 *   - 1st arg: A LUA context that executed a script
 *   - 2nd arg: The global variable name in the LUA script
 *   - 2rd arg: Pointer to the string value to be filled.
 *
 *  It returns the potential values:
 *    - __PO_HI_FAILURE: fails to convert the variable to a string
 *                       or non-existent variable
 *    - __PO_HI_SUCCESS: successfully convert the variable from LUA to C
 *
 */
jdelange's avatar
jdelange committed
203
204
int __po_hi_lua_get_string (__po_hi_lua_context_t*, char*, char*);

jdelange's avatar
jdelange committed
205
206


jdelange's avatar
jdelange committed
207
#else
jdelange's avatar
jdelange committed
208
#include <po_hi_debug.h>
jdelange's avatar
jdelange committed
209
typedef int __po_hi_lua_context_t;
jdelange's avatar
jdelange committed
210
#endif /* ifdef __PO_HI_USE_LUA */
jdelange's avatar
jdelange committed
211

jdelange's avatar
jdelange committed
212
#endif /* __PO_HI_LUA_H__ */