tests/main.c
author Mario de Sousa <msousa@fe.up.pt>
Mon, 04 Apr 2011 10:42:03 +0100
changeset 267 0a1204bcc9af
parent 150 398cfd6e8e4a
child 279 c0453b7f99df
permissions -rwxr-xr-x
starting to add proper header file with declaration of standard functions
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
     1
#ifdef __WIN32__
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
     2
#include <stdio.h>
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
     3
#include <sys/timeb.h>
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
     4
#include <time.h>
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
     5
#include <windows.h>
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
     6
#else
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
     7
#include <stdio.h>
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
     8
#include <string.h>
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
     9
#include <time.h>
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    10
#include <signal.h>
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    11
#endif
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    12
150
398cfd6e8e4a Bug with DEBUG mode in SFC fixed
lbessard
parents: 57
diff changeset
    13
#include "iec_types.h"
398cfd6e8e4a Bug with DEBUG mode in SFC fixed
lbessard
parents: 57
diff changeset
    14
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    15
/*
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    16
 * Functions and variables provied by generated C softPLC
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    17
 **/ 
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    18
extern int common_ticktime__;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    19
150
398cfd6e8e4a Bug with DEBUG mode in SFC fixed
lbessard
parents: 57
diff changeset
    20
IEC_BOOL __DEBUG;
398cfd6e8e4a Bug with DEBUG mode in SFC fixed
lbessard
parents: 57
diff changeset
    21
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    22
/*
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    23
 * Functions and variables provied by plc.c
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    24
 **/ 
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    25
void run(long int tv_sec, long int tv_nsec);
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    26
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    27
#define maxval(a,b) ((a>b)?a:b)
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    28
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    29
#ifdef __WIN32__
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    30
void timer_notify()
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    31
{
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    32
   struct _timeb timebuffer;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    33
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    34
   _ftime( &timebuffer );
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    35
   run(timebuffer.time, timebuffer.millitm * 1000000);
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    36
}
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    37
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    38
int main(int argc,char **argv)
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    39
{
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    40
    HANDLE hTimer = NULL;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    41
    LARGE_INTEGER liDueTime;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    42
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    43
    liDueTime.QuadPart = -10000 * maxval(common_ticktime__,1);;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    44
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    45
    // Create a waitable timer.
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    46
    hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    47
    if (NULL == hTimer)
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    48
    {
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    49
        printf("CreateWaitableTimer failed (%d)\n", GetLastError());
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    50
        return 1;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    51
    }
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    52
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    53
    config_init__();
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    54
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    55
    // Set a timer to wait for 10 seconds.
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    56
    if (!SetWaitableTimer(hTimer, &liDueTime, common_ticktime__, NULL, NULL, 0))
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    57
    {
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    58
        printf("SetWaitableTimer failed (%d)\n", GetLastError());
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    59
        return 2;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    60
    }
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    61
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    62
    while(1){
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    63
    // Wait for the timer.
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    64
        if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    65
        {
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    66
            printf("WaitForSingleObject failed (%d)\n", GetLastError());
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    67
            break;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    68
        }
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    69
        timer_notify();
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    70
    }
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    71
    return 0;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    72
}
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    73
#else
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    74
void timer_notify(sigval_t val)
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    75
{
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    76
    struct timespec CURRENT_TIME;
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    77
    clock_gettime(CLOCK_REALTIME, &CURRENT_TIME);
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
    78
    run(CURRENT_TIME.tv_sec, CURRENT_TIME.tv_nsec);
41
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    79
}
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    80
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    81
void catch_signal(int sig)
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    82
{
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    83
  signal(SIGTERM, catch_signal);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    84
  signal(SIGINT, catch_signal);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    85
  printf("Got Signal %d\n",sig);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    86
}
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    87
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    88
int main(int argc,char **argv)
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    89
{
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    90
    timer_t timer;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    91
    struct sigevent sigev;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    92
    long tv_nsec = 1000000 * (maxval(common_ticktime__,1)%1000);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    93
    time_t tv_sec = common_ticktime__/1000;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    94
    struct itimerspec timerValues;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    95
    
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    96
    memset (&sigev, 0, sizeof (struct sigevent));
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    97
    memset (&timerValues, 0, sizeof (struct itimerspec));
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    98
    sigev.sigev_value.sival_int = 0;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
    99
    sigev.sigev_notify = SIGEV_THREAD;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   100
    sigev.sigev_notify_attributes = NULL;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   101
    sigev.sigev_notify_function = timer_notify;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   102
    timerValues.it_value.tv_sec = tv_sec;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   103
    timerValues.it_value.tv_nsec = tv_nsec;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   104
    timerValues.it_interval.tv_sec = tv_sec;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   105
    timerValues.it_interval.tv_nsec = tv_nsec;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   106
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   107
    config_init__();
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   108
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   109
    timer_create (CLOCK_REALTIME, &sigev, &timer);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   110
    timer_settime (timer, 0, &timerValues, NULL);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   111
    
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   112
    /* install signal handler for manual break */
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   113
    signal(SIGTERM, catch_signal);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   114
    signal(SIGINT, catch_signal);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   115
    
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   116
    pause();
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   117
    
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   118
    timer_delete (timer);
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   119
    
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   120
    return 0;
8998c8b24b60 First working IEC std lib test, actually test from string and to_string functions.
etisserant
parents:
diff changeset
   121
}
57
db9cadaab2ea Now generated SoftPLC compiles with mingw32 and run windows.
etisserant
parents: 43
diff changeset
   122
#endif