po_hi_lua.c 3.77 KB
Newer Older
jdelange's avatar
jdelange committed
1
2
3
4
5
6
7
8
9
10
11
/*
 * 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.
 *
 * For more informations, please visit http://www.assert-online.net/taste
 *
 * Copyright (C) 2012, European Space Agency (ESA)
 */

#include <po_hi_lua.h>
jdelange's avatar
jdelange committed
12
#include <po_hi_debug.h>
jdelange's avatar
jdelange committed
13
14
#include <po_hi_time.h>

jdelange's avatar
jdelange committed
15
16

#ifdef __PO_HI_USE_LUA
jdelange's avatar
jdelange committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int __po_hi_lua_time_wait (lua_State* state)
{
   __po_hi_time_t now;
   __po_hi_time_t delay;
   __po_hi_time_t deadline;

   int msec;

   msec = lua_tonumber (state, 1);

   __po_hi_get_time (&now);
   __po_hi_milliseconds (&delay, msec);
   __po_hi_add_times (&deadline, &now, &delay);
   __po_hi_delay_until (&deadline);
   return 0;
}
jdelange's avatar
jdelange committed
33

jdelange's avatar
jdelange committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
int __po_hi_lua_time_get (lua_State* state)
{
   int ret;
   __po_hi_time_t now;

   __po_hi_get_time (&now);

   ret = now.sec * 1000 + now.nsec / 1000000;

   lua_pushnumber (state, ret);
   return 1;
}


jdelange's avatar
jdelange committed
48
int __po_hi_lua_load (__po_hi_lua_context_t* context, const char* filename)
jdelange's avatar
jdelange committed
49
{
jdelange's avatar
jdelange committed
50
51
52
53
54
55
56
57
58
59
   if (context == NULL)
   {
      return __PO_HI_INVALID;
   }

   if (filename == NULL)
   {
      return __PO_HI_INVALID;
   }

jdelange's avatar
jdelange committed
60
#ifdef __PO_HI_USE_LUA
jdelange's avatar
jdelange committed
61
   context->state = lua_open();
jdelange's avatar
jdelange committed
62

jdelange's avatar
jdelange committed
63
   luaL_openlibs (context->state);
jdelange's avatar
jdelange committed
64
65

   lua_register (context->state, "time_wait", __po_hi_lua_time_wait);
jdelange's avatar
jdelange committed
66
   lua_register (context->state, "time_get", __po_hi_lua_time_get);
jdelange's avatar
jdelange committed
67

jdelange's avatar
jdelange committed
68
   if (luaL_dofile (context->state,filename) != 0)
jdelange's avatar
jdelange committed
69
   {
jdelange's avatar
jdelange committed
70
      __PO_HI_DEBUG_DEBUG ("[LUA] Fail to load LUA file %s !", filename);
jdelange's avatar
jdelange committed
71
72
73
74
75
      return __PO_HI_INVALID;
   }
#endif 
   return __PO_HI_SUCCESS;
}
jdelange's avatar
jdelange committed
76
77
78

int __po_hi_lua_init_function_call (__po_hi_lua_context_t* ctx, const char* fctname)
{
jdelange's avatar
jdelange committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
   int len; 

   if (ctx == NULL)
   {
      return __PO_HI_INVALID;
   }

   if (fctname == NULL)
   {
      return __PO_HI_INVALID;
   }

   len = strlen (fctname);

jdelange's avatar
jdelange committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
   if ( len >= __PO_HI_LUA_FUNCTION_NAME_MAX_SIZE)
   {
      return __PO_HI_INVALID;
   }

   memset (ctx->function_name, '\0', __PO_HI_LUA_FUNCTION_NAME_MAX_SIZE);

   memcpy (ctx->function_name, fctname, len);

   ctx->nb_args = 0;

   lua_getglobal(ctx->state, fctname);

   if (!lua_isfunction(ctx->state,-1))
   {
jdelange's avatar
jdelange committed
108
109
      __PO_HI_DEBUG_DEBUG ("[LUA] Function %s does not exists !", fctname);

jdelange's avatar
jdelange committed
110
      lua_pop(ctx->state,1);
jdelange's avatar
jdelange committed
111

jdelange's avatar
jdelange committed
112
113
114
115
116
117
118
119
      return __PO_HI_INVALID;
   }

   return __PO_HI_SUCCESS;
}

int __po_hi_lua_perform_function_call (__po_hi_lua_context_t* ctx)
{
jdelange's avatar
jdelange committed
120
121
122
123
124
   if (ctx == NULL)
   {
      return __PO_HI_INVALID;
   }

jdelange's avatar
jdelange committed
125
   lua_call (ctx->state, ctx->nb_args, 0);
jdelange's avatar
jdelange committed
126
   return __PO_HI_SUCCESS;
jdelange's avatar
jdelange committed
127
128
129
130
131
132
133
134
}


int __po_hi_lua_push_number (__po_hi_lua_context_t* ctx, int val)
{
   ctx->nb_args = ctx->nb_args + 1;
   lua_pushnumber (ctx->state, val);

jdelange's avatar
jdelange committed
135
   return __PO_HI_SUCCESS;
jdelange's avatar
jdelange committed
136
137
138
139
140
}


int __po_hi_lua_push_boolean (__po_hi_lua_context_t* ctx, int val)
{
jdelange's avatar
jdelange committed
141
142
143
144
145
   if (ctx == NULL)
   {
      return __PO_HI_INVALID;
   }

jdelange's avatar
jdelange committed
146
147
148
   ctx->nb_args = ctx->nb_args + 1;

   lua_pushboolean (ctx->state, val);
jdelange's avatar
jdelange committed
149
150

   return __PO_HI_SUCCESS;
jdelange's avatar
jdelange committed
151
152
153
154
155
}


int __po_hi_lua_push_string (__po_hi_lua_context_t* ctx, char* val)
{
jdelange's avatar
jdelange committed
156
157
158
159
160
   if (ctx == NULL)
   {
      return __PO_HI_INVALID;
   }

jdelange's avatar
jdelange committed
161
162
163
   ctx->nb_args = ctx->nb_args + 1;

   lua_pushstring (ctx->state, val);
jdelange's avatar
jdelange committed
164
   return __PO_HI_SUCCESS;
jdelange's avatar
jdelange committed
165
166
167
168
169
}


int __po_hi_lua_get_number (__po_hi_lua_context_t* ctx, char* varname, int* val)
{
jdelange's avatar
jdelange committed
170
171
172
173
174
   if (ctx == NULL)
   {
      return __PO_HI_INVALID;
   }

jdelange's avatar
jdelange committed
175
176
177
178
   lua_settop (ctx->state,0);
   lua_getglobal (ctx->state,varname);
   *val = lua_tonumber (ctx->state,1);
   lua_pop (ctx->state,1);
jdelange's avatar
jdelange committed
179
   return __PO_HI_SUCCESS;
jdelange's avatar
jdelange committed
180
181
182
183
184
}


int __po_hi_lua_get_boolean (__po_hi_lua_context_t* ctx, char* varname, int* val)
{
jdelange's avatar
jdelange committed
185
186
187
188
189
190
   if (ctx == NULL)
   {
      return __PO_HI_INVALID;
   }

   return __PO_HI_SUCCESS;
jdelange's avatar
jdelange committed
191
192
193
194
195
}


int __po_hi_lua_get_string (__po_hi_lua_context_t* ctx, char* varname, char* val)
{
jdelange's avatar
jdelange committed
196
197
198
199
200
201
   if (ctx == NULL)
   {
      return __PO_HI_INVALID;
   }

   return __PO_HI_SUCCESS;
jdelange's avatar
jdelange committed
202
203
}

jdelange's avatar
jdelange committed
204
205
#endif

jdelange's avatar
jdelange committed
206