forked from tuxd3v/ats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathats.c
586 lines (508 loc) · 18 KB
/
ats.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/* I/O */
#include <stdio.h>
/* for sleep/usleep*/
#include <unistd.h>
/* for symlink checks */
#include <sys/stat.h>
#include <sys/types.h>
/* atoi()*/
#include <stdlib.h>
/* for ceil() */
#include <math.h>
/* LuaC */
#include <lua5.3/lua.h>
#include <lua5.3/lauxlib.h>
#include <lua5.3/lualib.h>
#include "../include/debug.h"
#include "../include/ats.h"
/* Structure to hold all relevant information about ATS
*
* Set ATS Constants..
*
* Datasheet has limits ] -20°C, ~80°C [
* CPU should be a emergency shutdown close to 85°C..
* For Safety Reasons we operate bellow, shutting down at 70°C
*/
ats_t ats;
/*** Variables used for Temperature/PWM/Timers..
**/
/** Temperature Value( CPU or GPU ), which one is greater... */
signed char temp;
/** Initial Fan Pwm Value set in ats Lua file.. */
unsigned char pwm = 190;
/* Timer Pointers to all pre-calculated values... */
unsigned char *Pratio = ats.profile.pwm_ratio + 30;
unsigned short int *Qtimer = ats.profile.quiet_timers +30;
unsigned short int *Rtimer = ats.profile.run_timers + 30;
/*** CTL Variables..
**/
char * thermal_ctl[ 2 ] = { NULL };
char * pwm_ctl = NULL;
/*** Thermal values readed..
**/
/** CPU Thermal Zone / GPU Thermal Zone, variables used by this backend */
signed char thermal_[2];
/** File Descriptors for stdout **/
FILE * fstdout = NULL;
/* Function to set quiet,run timers and pwm ratios */
static void setTriggers( ats_t *self ){
fprintf( fstdout, "info:'Pratio' timers\n" );
signed char i;
/* to get pwm float char*/
char number[11];
/* Temperature limits */
const signed char max = self->ABSOLUTE_MAX_THERMAL_TEMP;
const signed char min = self->ABSOLUTE_MIN_THERMAL_TEMP;
const signed char max_plus = ( max + 10 );
const signed char min_plus = ( min - 10 );
const signed char max_continuous_thermal_temp = self->profile.MAX_CONTINUOUS_THERMAL_TEMP;
const signed char min_continuous_thermal_temp = self->profile.MIN_CONTINUOUS_THERMAL_TEMP;
fprintf( fstdout, "info: 'Pratio[ %d - %d [' = %d\n", min, min_continuous_thermal_temp, self->ABSOLUTE_MIN_PWM );
for ( i = min_plus; i <= max_plus; ++i ){
if( i < min_continuous_thermal_temp ){
Pratio[ i ] = self->ABSOLUTE_MIN_PWM;
//Bellow -10°C, heat up the CPU[ -20°C, +80°C ]
if( i <= -10 ){
Qtimer[ i ] = 360;
Rtimer[ i ] = 0;
} else if( i <= 0 ){
Qtimer[ i ] = 300;
Rtimer[ i ] = 1;
} else if( i <= 10 ){
Qtimer[ i ] = 250;
Rtimer[ i ] = 2;
} else if( i <= 35 ){
Qtimer[ i ] = 190;
Rtimer[ i ] = 3;
} else {
Qtimer[ i ] = 120;
Rtimer[ i ] = 5;
}
} else if( i >= min_continuous_thermal_temp && i <= max_continuous_thermal_temp ){
/* Calculate PWM values based on functions with diferent pwm 'Response Curves' */
/* *( pwm_ratio0 + i ) = self->profile.getProfile_pwm( self, i ); */
/* Get float to String, then convert String to integer*/
snprintf( number, 6, "%3.2f", ( float ) ( self->profile.MIN_PWM + ( ( ( self->profile.MAX_PWM - self->profile.MIN_PWM ) * 1.0 ) / ( self->profile.MAX_CONTINUOUS_THERMAL_TEMP - self->profile.MIN_CONTINUOUS_THERMAL_TEMP ) ) * ( i - self->profile.MIN_CONTINUOUS_THERMAL_TEMP ) ) );
Pratio[ i ] = ( unsigned char ) atoi( number );
fprintf( fstdout, "info: 'Pratio[ %d ]' = %d\n", i, Pratio[ i ] );
if ( i <= 45 ){
Qtimer[ i ] = 90;
Rtimer[ i ] = 10;
} else if( i <= 50 ){
Qtimer[ i ] = 50;
Rtimer[ i ] = 20;
}else if( i <= 55 ){
Qtimer[ i ] = 40;
Rtimer[ i ] = 30;
} else if( i <= 60 ){
Qtimer[ i ] = 10;
Rtimer[ i ] = 60;
}
} else if( i > max_continuous_thermal_temp ){
Pratio[ i ] = self->ABSOLUTE_MAX_PWM;
if( i < ( max - 5 ) ){
Qtimer[ i ] = 6;
Rtimer[ i ] = 120;
}else{
Qtimer[ i ] = 3;
Rtimer[ i ] = 160;
}
}
}
fprintf( fstdout, "info: 'Pratio[ %d - %d [' = %d\n", max_continuous_thermal_temp, max, self->ABSOLUTE_MAX_PWM );
}
/**
* Function to initialize ATS Backend
*/
static int initCore_c( lua_State *L ){
double number;
/* Get Lua Frontend STDout descriptor.. */
lua_getglobal( L, "io" );
lua_pushstring(L, "stdout");
lua_gettable(L, -2);
/*fstdout = *(FILE **)lua_touserdata(L, -1);*/
fstdout = ((luaL_Stream *)lua_touserdata(L, -1))->f;
lua_pop(L, 2);
/* Theoretically speaking, we received a Table...is this a table? */
if ( lua_istable( L, -1 ) ) {
fprintf( fstdout, "info:'SYSTEM' Table\n" );
/* Looking up based on the key */
/* Add key we're interested in to the stack*/
lua_pushstring( L, "BOARD" );
lua_gettable( L, -2 );
if ( lua_istable( L, -1 ) ) {
fprintf( fstdout, "info: 'BOARD' Table\n" );
/* Put on top, key NAME*/
lua_pushstring( L, "NAME" );
/* Get on top, value pair for key NAME*/
lua_gettable( L, -2 );
/* Get NAME value */
ats.NAME = lua_tostring( L, -1 );
fprintf( fstdout, "info: 'NAME' = %s\n", ats.NAME );
/* Free Stack NAME Value*/
lua_pop( L, 1 );
/* Put on top, key CPU*/
lua_pushstring(L,"CPU");
/* Get on top, value pair for key NAME*/
lua_gettable( L, -2 );
/* Get CPU value */
ats.CPU = lua_tostring( L, -1 );
fprintf( fstdout, "info: 'CPU' = %s\n", ats.CPU );
/* Free BOARD TABLE from Stack top*/
lua_pop( L, 2 );
} else {
/* Free BOARD TABLE key from Stack top*/
lua_pop( L, 1 );
fprintf( fstdout, "warn: UPS.. there are problems with SYSTEM config table.\n Expecting a BOARD table..\n Check your /etc/ats.conf file..\n A trace follows bellow:\n" );
stackTrace( fstdout, L );
/* push false on stack( return false to lua ) */
lua_pushboolean ( L, 0 );
return 1;
}
/* Put on top, key THERMAL0_CTL */
lua_pushstring( L,"THERMAL0_CTL" );
/* Get on top, value pair for key THERMAL0_CTL*/
lua_gettable( L, -2 );
/* Get THERMAL0_CTL value */
ats.THERMAL0_CTL = lua_tostring( L, -1 );
fprintf( fstdout, "info: 'THERMAL0_CTL' = %s\n", ats.THERMAL0_CTL );
/* Free Stack THERMAL0_CTL Value*/
lua_pop( L, 1 );
/* Put on top, key THERMAL1_CTL */
lua_pushstring( L,"THERMAL1_CTL" );
/* Get on top, value pair for key THERMAL1_CTL*/
lua_gettable( L, -2 );
/* Get THERMAL1_CTL value */
ats.THERMAL1_CTL = lua_tostring( L, -1 );
fprintf( fstdout, "info: 'THERMAL1_CTL' = %s\n", ats.THERMAL1_CTL );
/* Free Stack THERMAL1_CTL Value*/
lua_pop( L, 1 );
/* Put on top, key PWM_CTL */
lua_pushstring( L, "PWM_CTL" );
/* Get on top, value pair for key PWM_CTL*/
lua_gettable( L, -2 );
/* Get PWM_CTL value */
ats.PWM_CTL = lua_tostring( L, -1 );
fprintf( fstdout, "info: 'PWM_CTL' = %s\n", ats.PWM_CTL );
/* Free Stack PWM_CTL Value*/
lua_pop( L, 1 );
/* ATS Limits */
ats.ABSOLUTE_MAX_THERMAL_TEMP = 70;
ats.ABSOLUTE_MIN_THERMAL_TEMP = -20;
ats.ABSOLUTE_MAX_PWM = 255;
ats.ABSOLUTE_MIN_PWM = 0;
/* Put on top, key MAX_CONTINUOUS_THERMAL_TEMP */
lua_pushstring( L, "MAX_CONTINUOUS_THERMAL_TEMP" );
/* Get on top, value pair for key MAX_CONTINUOUS_THERMAL_TEMP*/
lua_gettable( L, -2 );
/* Get MAX_CONTINUOUS_THERMAL_TEMP value */
number = lua_tonumber( L, -1 );
if( number > ats.ABSOLUTE_MIN_THERMAL_TEMP && number < ats.ABSOLUTE_MAX_THERMAL_TEMP ){
ats.profile.MAX_CONTINUOUS_THERMAL_TEMP = ( signed char ) number;
fprintf( fstdout, "info: 'MAX_CONTINUOUS_THERMAL_TEMP' = %d\n", ats.profile.MAX_CONTINUOUS_THERMAL_TEMP );
} else {
fprintf( fstdout, "warn: 'MAX_CONTINUOUS_THERMAL_TEMP' outside range] %d, %d [\n 'MAX_CONTINUOUS_THERMAL_TEMP' = %d\n",
ats.ABSOLUTE_MIN_THERMAL_TEMP,
ats.ABSOLUTE_MAX_THERMAL_TEMP, 60 );
ats.profile.MAX_CONTINUOUS_THERMAL_TEMP = 60;
}
/* Free Stack MAX_CONTINUOUS_THERMAL_TEMP Value*/
lua_pop(L,1);
/* Put on top, key MIN_CONTINUOUS_THERMAL_TEMP */
lua_pushstring( L, "MIN_CONTINUOUS_THERMAL_TEMP" );
/* Get on top, value pair for key MIN_CONTINUOUS_THERMAL_TEMP*/
lua_gettable( L, -2 );
/* Get MIN_CONTINUOUS_THERMAL_TEMP value */
number = lua_tonumber( L, -1 );
if( number > ats.ABSOLUTE_MIN_THERMAL_TEMP && number < ats.ABSOLUTE_MAX_THERMAL_TEMP ){
ats.profile.MIN_CONTINUOUS_THERMAL_TEMP = ( signed char ) number;
fprintf( fstdout, "info: 'MIN_CONTINUOUS_THERMAL_TEMP' = %d\n", ats.profile.MIN_CONTINUOUS_THERMAL_TEMP );
} else {
fprintf( fstdout, "warn: 'MIN_CONTINUOUS_THERMAL_TEMP' outside range] %d, %d [\n 'MIN_CONTINUOUS_THERMAL_TEMP' = %d\n",
ats.ABSOLUTE_MIN_THERMAL_TEMP,
ats.ABSOLUTE_MAX_THERMAL_TEMP, 40 );
ats.profile.MIN_CONTINUOUS_THERMAL_TEMP = 40;
}
/* Free Stack MIN_CONTINUOUS_THERMAL_TEMP Value*/
lua_pop( L, 1 );
/* Put on top, key MAX_PWM */
lua_pushstring( L, "MAX_PWM" );
/* Get on top, value pair for key MAX_PWM*/
lua_gettable( L, -2 );
/* Get MAX_PWM value */
number = lua_tonumber( L, -1 );
if( number > ats.ABSOLUTE_MIN_PWM && number <= ats.ABSOLUTE_MAX_PWM ){
ats.profile.MAX_PWM = ( unsigned char ) number;
fprintf( fstdout, "info: 'MAX_PWM' = %d\n", ats.profile.MAX_PWM );
} else {
fprintf( fstdout, "warn: 'MAX_PWM' outside range] %d, %d ]\n 'MAX_PWM' = %d\n",ats.ABSOLUTE_MIN_PWM, ats.ABSOLUTE_MAX_PWM,
ats.ABSOLUTE_MAX_PWM );
ats.profile.MAX_PWM = ats.ABSOLUTE_MAX_PWM;
}
/* Free Stack MAX_PWM Value*/
lua_pop( L, 1 );
/* Put on top, key MIN_PWM */
lua_pushstring( L, "MIN_PWM" );
/* Get on top, value pair for key MIN_PWM*/
lua_gettable( L, -2 );
/* Get MIN_PWM value */
number = lua_tonumber( L, -1 );
if( number > ats.ABSOLUTE_MIN_PWM && number < ats.ABSOLUTE_MAX_PWM ){
ats.profile.MIN_PWM = ( unsigned char ) number;
fprintf( fstdout, "info: 'MIN_PWM' = %d\n", ats.profile.MIN_PWM );
} else {
fprintf( fstdout, "warn: 'MIN_PWM' outside range] %d, %d [\n 'MIN_PWM' = %d\n", ats.ABSOLUTE_MIN_PWM, ats.ABSOLUTE_MAX_PWM, 40 );
ats.profile.MIN_PWM = 40;
}
/* Free Stack MIN_PWM Value*/
lua_pop( L, 1 );
/* Put on top, key ALWAYS_ON */
lua_pushstring( L, "ALWAYS_ON" );
/* Get on top, value pair for key ALWAYS_ON*/
lua_gettable( L, -2 );
/* Get ALWAYS_ON value on ats structure */
ats.profile.ALWAYS_ON = ( unsigned char ) lua_toboolean( L, -1 );
if( ! ats.profile.ALWAYS_ON ){
fprintf( fstdout, "info: 'ALWAYS_ON' = false\n");
}else {
fprintf( fstdout, "info: 'ALWAYS_ON' = true\n");
}
/* Free Stack ALWAYS_ON Value*/
lua_pop( L, 1 );
/* Put on top, key PROFILE_NAME */
lua_pushstring( L, "PROFILE_NAME" );
/* Get on top, value pair for key PROFILE_NAME*/
lua_gettable( L, -2 );
/* Get PROFILE_NAME value */
ats.profile.name = lua_tostring( L, -1 );
fprintf( fstdout, "info: 'PROFILE_NAME' = %s\n", ats.profile.name );
/* Free Stack PROFILE_NAME Value*/
lua_pop( L, 1 );
/* Put on top, key PROFILE_NR */
lua_pushstring( L, "PROFILE_NR" );
/* Get on top, value pair for key PROFILE_NR*/
lua_gettable( L, -2 );
/* Get PROFILE_NR value */
number = lua_tonumber( L, -1 );
if( number >= 0 && number < 3 ){
ats.profile.nr = ( unsigned char ) number;
fprintf( fstdout, "info: 'PROFILE' = %d\n", ats.profile.nr );
} else {
fprintf( fstdout, "warn: 'PROFILE' outside range[ %d, %d ]\n 'MIN_PWM' = %d\n", 0, 2, 0 );
ats.profile.nr = 0;
}
/* Free Stack PROFILE Value*/
lua_pop( L, 1 );
ats.setTriggers = setTriggers;
ats.setTriggers( &ats );
/* Return true on stack */
lua_pushboolean ( L, 1 );
} else {
fprintf( fstdout, "warn:UPS.. there are problems with SYSTEM config table.\n Expecting a SYSTEM table..\n Check your /etc/ats.conf file..\n A trace follows bellow:\n" );
stackTrace( fstdout, L );
/* push false on stack( return false to lua ) */
lua_pushboolean ( L, 0 );
}
/* return one value on the stack */
return 1;
}
/* Get Thernal Values[ integer ] */
static void getThermal(){
signed int value;
/* 2 thermal zones */
unsigned char i;
FILE * fthermal= NULL;
for ( i = 0; i < 2; i++ ){
fthermal = fopen( thermal_ctl[ i ], "r" );
if( fthermal != NULL ){
/* Read thermal_{ 0, 1 } 5 chars has a integer.. TODO: compare speed has reading 2 chars to a buffer and then atoi()..*/
if ( fscanf( fthermal, "%d", &value ) != 0 )
thermal_[ i ] = ( signed char ) ceil( value / 1000 );
if( fclose( fthermal ) != 0 )
thermal_[ i ] = ats.profile.MAX_CONTINUOUS_THERMAL_TEMP;
}else{
thermal_[ i ] = ats.profile.MAX_CONTINUOUS_THERMAL_TEMP;
}
}
/* Use Biggest Thermal Value from THERMAL_{0,1} for temp var*/
if( thermal_[ 0 ] > thermal_[ 1 ] )
temp = thermal_[ 0 ];
else
temp = thermal_[ 1 ];
}
/*** Set Fan PWM value[ unsigned char ]
**/
static void setPwm( unsigned char value ){
FILE * pwm1 = NULL;
if( ! pwm ){
/* When stopped, it needs more power to start...give him 0.2 seconds to rotate poles a bit, so that would be better for aplying bigger push,
* In This Way, initial peak current needed to start fan is lower..
*/
/* to force recursion, and update PWM, in case of fail PWM is set to zero bellow, so that it will try again, in second call .. */
pwm = 1;
setPwm( 130 );
usleep( 200000 );
setPwm( 190 );
sleep( 1 );
}
pwm1 = fopen( pwm_ctl, "w" );
if ( pwm1 != NULL ){
if( fprintf( pwm1, "%d", value ) != 0 )
pwm = value;
/* could be dangerous...if not able to close the file open descriptor...will loop recursivelly until resources exausted.. FIXME */
if ( fclose( pwm1 ) != 0 )
pwm = 0;
}else{
pwm = 0;
}
}
/* pooling loop */
static int loop_c( lua_State *L ){
unsigned char instant_ratio;
signed char absolute_max_thermal_temp = ats.ABSOLUTE_MAX_THERMAL_TEMP;
signed char absolute_min_thermal_temp = ats.ABSOLUTE_MIN_THERMAL_TEMP;
unsigned int verbose = lua_toboolean ( L, -1 );
/* Free Stack verbose boolean Value*/
lua_pop( L, 1 );
/* CTL Variables */
thermal_ctl[ 0 ] = ( char * ) ats.THERMAL0_CTL;
thermal_ctl[ 1 ] = ( char * ) ats.THERMAL1_CTL;
pwm_ctl = ( char * ) ats.PWM_CTL;
/* At beguining force timers for max thermal temp, so that, ATS will start check quickly the real temps.. */
temp = absolute_max_thermal_temp;
/* Looping cycle.. */
if( ! ats.profile.ALWAYS_ON ){
for(;;){
if( verbose )
printf( "Stopping for[ seconds ]............... %d\nCPU Temperature[ max 70 °C ].......... %d\nGPU Temperature[ max 70 °C ].......... %d\nFan PWM Duty Cycle value[ 0 - 255 ]... %d\n--------------------\n",
Qtimer[ temp ],
thermal_[ 0 ],
thermal_[ 1 ],
pwm );
/* Sleeping with Fan OFF, until next cicle */
sleep( Qtimer[ temp ] );
/* Aquire { CPU, GPU } -> THERMAL_{ 0, 1 } values */
getThermal();
instant_ratio = Pratio[ temp ];
/* If temp doesn't change...don't update it..*/
if( instant_ratio != pwm )
setPwm( instant_ratio );
/* Temp Above Threshold to ShutDown.. */
if( temp <= absolute_min_thermal_temp || temp >= absolute_max_thermal_temp ){
/* Temp is Critically Above 'ABSOLUTE_MAX_THERMAL_TEMP' */
/* push false on stack( return false to lua ) */
lua_pushboolean ( L, 0 );
break;
}
if( verbose )
printf( "Running for[ seconds ]................ %d\nCPU Temperature[ max 70 °C ].......... %d\nGPU Temperature[ max 70 °C ].......... %d\nFan PWM Duty Cycle value[ 0 - 255 ]... %d\n--------------------\n",
Rtimer[ temp ],
thermal_[ 0 ],
thermal_[ 1 ],
pwm );
/* Sleeping with Fan ON until next cycle */
sleep( Rtimer[ temp ] );
/* Stop Fan */
setPwm( 0 );
}
}else{
for(;;){
/* Aquire { CPU, GPU } -> THERMAL_{ 0, 1 } values */
getThermal();
instant_ratio = Pratio[ temp ];
/* If temp doesn't change...don't update it..*/
if( instant_ratio != pwm )
setPwm( instant_ratio );
/* Temp Above Threshold to ShutDown.. */
if( temp <= absolute_min_thermal_temp || temp >= absolute_max_thermal_temp ){
/* Temp is Critically Above 'ABSOLUTE_MAX_THERMAL_TEMP' */
/* push false on stack( return false to lua ) */
lua_pushboolean ( L, 0 );
break;
}
if( verbose )
printf( "Running for[ seconds ]................ %d\nCPU Temperature[ max 70 °C ].......... %d\nGPU Temperature[ max 70 °C ].......... %d\nFan PWM Duty Cycle value[ 0 - 255 ]... %d\n--------------------\n",
Rtimer[ temp ],
thermal_[ 0 ],
thermal_[ 1 ],
pwm );
/* Sleeping with Fan ON until next cycle */
sleep( Rtimer[ temp ] );
}
}
return 1;
}
/*** Sleep Functions
**/
/*
* unistd.h don't provide a msleep function( provide usleep ), but still,
* Avoid to name convenctions from unistd.h, providing a termination "_c" on function name
*/
static int mSleep_c( lua_State *L ){
long msecs = lua_tointeger( L, -1 );
usleep( 1000 * msecs );
return 0;
}
/*
* unistd.h provide a sleep function,
* Avoid to name convenctions from unistd.h, providing a termination "_c" on function name
*/
static int sSleep_c( lua_State *L ){
long secs = lua_tointeger( L, -1 );
sleep( secs );
return 0;
}
/*
* Check if a a SymLink exists,
* add "_c" on declaration
*/
static int chkSymlink_c( lua_State *L ){
struct stat buffer;
if ( lstat( lua_tostring( L, -1 ), &buffer ) == 0 ) {
lua_pushboolean ( L, 1 );
}else{
lua_pushnil( L );
}
return 1;
}
/* Register the functions
* checkSymlink
* msleep
* sleep
*/
int luaopen_ats( lua_State *L ){
lua_register( L, "initCore", initCore_c );
lua_register( L, "loop", loop_c );
lua_register( L, "chkSymlink", chkSymlink_c );
lua_register( L, "mSleep", mSleep_c );
lua_register( L, "sSleep", sSleep_c );
return 0;
}
/*
static const luaL_Reg Map [] = {
{ "initCore", initCore_c },
{ "chkSymlink", chkSymlink_c },
{ "mSleep", mSleep_c },
{ "sSleep", sSleep_c },
{ NULL, NULL }
};
*/
/* Register the Functions in Lua State
int luaopen_ats ( lua_State *L ) {
luaL_register( L, "LUA_ATS", Map );
void luaL_newlib ( L, const luaL_Reg l[]
return 1;
}
*/
/*********************** LUA C API Test.c Frontend **************************
**/
/**
* Function to initialize ATS Backend
*/
int tinitCore( lua_State *L ){
return initCore_c( L );
}
/* pooling loop */
int tloop( lua_State *L ){
return loop_c( L );
}