lib/iec_std_lib.h
changeset 57 db9cadaab2ea
parent 55 8b7a21820737
child 59 37ba6f9e51e2
equal deleted inserted replaced
56:6e87bbc2abe9 57:db9cadaab2ea
     4 
     4 
     5 #include <limits.h>
     5 #include <limits.h>
     6 #include <float.h>
     6 #include <float.h>
     7 #include <math.h>
     7 #include <math.h>
     8 #include <time.h>
     8 #include <time.h>
     9 #include <sys/types.h>
     9 #include <stdint.h>
    10 
    10 
    11 #include <stdio.h>
    11 #include <stdio.h>
    12 #include <stdlib.h>
    12 #include <stdlib.h>
    13 #include <string.h>
    13 #include <string.h>
    14 #include <stdarg.h>
    14 #include <stdarg.h>
    46 
    46 
    47 /*****************/
    47 /*****************/
    48 /*  Types defs   */
    48 /*  Types defs   */
    49 /*****************/
    49 /*****************/
    50 
    50 
    51 typedef u_int8_t  BOOL;
    51 typedef uint8_t  BOOL;
    52 
    52 
    53 #define TRUE 1
    53 #define TRUE 1
    54 #define FALSE 0
    54 #define FALSE 0
    55 
    55 
    56 typedef int8_t    SINT;
    56 typedef int8_t    SINT;
    57 typedef int16_t   INT;
    57 typedef int16_t   INT;
    58 typedef int32_t   DINT;
    58 typedef int32_t   DINT;
    59 typedef int64_t   LINT;
    59 typedef int64_t   LINT;
    60 
    60 
    61 typedef u_int8_t    USINT;
    61 typedef uint8_t    USINT;
    62 typedef u_int16_t   UINT;
    62 typedef uint16_t   UINT;
    63 typedef u_int32_t   UDINT;
    63 typedef uint32_t   UDINT;
    64 typedef u_int64_t   ULINT;
    64 typedef uint64_t   ULINT;
    65 
    65 
    66 typedef u_int8_t    BYTE;
    66 typedef uint8_t    BYTE;
    67 typedef u_int16_t   WORD;
    67 typedef uint16_t   WORD;
    68 typedef u_int32_t   DWORD;
    68 typedef uint32_t   DWORD;
    69 typedef u_int64_t   LWORD;
    69 typedef uint64_t   LWORD;
    70 
    70 
    71 typedef float    REAL;
    71 typedef float    REAL;
    72 typedef double   LREAL;
    72 typedef double   LREAL;
       
    73 
       
    74 
       
    75 #if !defined __timespec_defined
       
    76 # define __timespec_defined     1
       
    77 
       
    78 struct timespec
       
    79   {
       
    80     long int tv_sec;            /* Seconds.  */
       
    81     long int tv_nsec;           /* Nanoseconds.  */
       
    82   };
       
    83 
       
    84 #endif
    73 
    85 
    74 typedef struct timespec TIME;
    86 typedef struct timespec TIME;
    75 typedef struct timespec DATE;
    87 typedef struct timespec DATE;
    76 typedef struct timespec DT;
    88 typedef struct timespec DT;
    77 typedef struct timespec TOD;
    89 typedef struct timespec TOD;
    81 
    93 
    82 #define STR_MAX_LEN 40
    94 #define STR_MAX_LEN 40
    83 typedef int8_t __strlen_t;
    95 typedef int8_t __strlen_t;
    84 typedef struct {
    96 typedef struct {
    85     __strlen_t len;
    97     __strlen_t len;
    86     u_int8_t body[STR_MAX_LEN];
    98     uint8_t body[STR_MAX_LEN];
    87 } STRING;
    99 } STRING;
    88 
   100 
    89 #define __STR_CMP(str1, str2) memcmp((char*)&str1.body,(char*)&str2.body, str1.len < str2.len ? str1.len : str2.len)
   101 #define __STR_CMP(str1, str2) memcmp((char*)&str1.body,(char*)&str2.body, str1.len < str2.len ? str1.len : str2.len)
    90 
   102 
    91 /* TODO
   103 /* TODO
    93     __strlen_t len;
   105     __strlen_t len;
    94     u_int16_t body[STR_MAX_LEN];
   106     u_int16_t body[STR_MAX_LEN];
    95 } WSTRING;
   107 } WSTRING;
    96 */
   108 */
    97 
   109 
    98 #define __BOOL_literal(value) (BOOL)value
   110 # if __WORDSIZE == 64
    99 #define __SINT_literal(value) (SINT)value
   111 #define __32b_sufix
   100 #define __INT_literal(value) (INT)value
   112 #define __64b_sufix L
   101 #define __DINT_literal(value) (DINT)value
   113 #else
   102 #define __LINT_literal(value) (LINT)value
   114 #define __32b_sufix L
   103 #define __USINT_literal(value) (USINT)value
   115 #define __64b_sufix LL 
   104 #define __UINT_literal(value) (UINT)value
   116 #endif 
   105 #define __UDINT_literal(value) (UDINT)value
   117 
   106 #define __ULINT_literal(value) (ULINT)value
   118 #define __lit(type,value,sfx) (type)value##sfx
   107 #define __REAL_literal(value) (REAL)value
   119 // Keep this macro expention step to let sfx change into L or LL 
   108 #define __LREAL_literal(value) (LREAL)value
   120 #define __literal(type,value,sfx) __lit(type,value,sfx)
   109 #define __TIME_literal(value) (TIME)value
   121 
   110 #define __DATE_literal(value) (DATE)value
   122 #define __BOOL_LITERAL(value) __literal(BOOL,value,)
   111 #define __TOD_literal(value) (TOD)value
   123 #define __SINT_LITERAL(value) __literal(SINT,value,)
   112 #define __DT_literal(value) (DT)value
   124 #define __INT_LITERAL(value) __literal(INT,value,)
   113 #define __STRING_literal(value) (STRING)value
   125 #define __DINT_LITERAL(value) __literal(DINT,value,__32b_sufix)
   114 #define __BYTE_literal(value) (BYTE)value
   126 #define __LINT_LITERAL(value) __literal(LINT,value,__64b_sufix)
   115 #define __WORD_literal(value) (WORD)value
   127 #define __USINT_LITERAL(value) __literal(USINT,value,)
   116 #define __DWORD_literal(value) (DWORD)value
   128 #define __UINT_LITERAL(value) __literal(UINT,value,)
   117 #define __LWORD_literal(value) (LWORD)value
   129 #define __UDINT_LITERAL(value) __literal(UDINT,value,__32b_sufix)
       
   130 #define __ULINT_LITERAL(value) __literal(ULINT,value,__64b_sufix)
       
   131 #define __REAL_LITERAL(value) __literal(REAL,value,__32b_sufix)
       
   132 #define __LREAL_LITERAL(value) __literal(LREAL,value,__64b_sufix)
       
   133 #define __TIME_LITERAL(value) __literal(TIME,value,)
       
   134 #define __DATE_LITERAL(value) __literal(DATE,value,)
       
   135 #define __TOD_LITERAL(value) __literal(TOD,value,)
       
   136 #define __DT_LITERAL(value) __literal(DT,value,)
       
   137 #define __STRING_LITERAL(value) __literal(STRING,value,)
       
   138 #define __BYTE_LITERAL(value) __literal(BYTE,value,)
       
   139 #define __WORD_LITERAL(value) __literal(WORD,value,)
       
   140 #define __DWORD_LITERAL(value) __literal(DWORD,value,__32b_sufix)
       
   141 #define __LWORD_LITERAL(value) __literal(LWORD,value,__32b_sufix)
   118 
   142 
   119 
   143 
   120 
   144 
   121 #define __INIT_REAL 0
   145 #define __INIT_REAL 0
   122 #define __INIT_LREAL 0
   146 #define __INIT_LREAL 0
   640     return res;
   664     return res;
   641 }
   665 }
   642 static inline STRING __date_to_string(DATE IN){
   666 static inline STRING __date_to_string(DATE IN){
   643     /* D#1984-06-25 */
   667     /* D#1984-06-25 */
   644     STRING res = __INIT_STRING;
   668     STRING res = __INIT_STRING;
   645     struct tm broken_down_time;
   669     struct tm* broken_down_time;
   646     time_t seconds = IN.tv_sec;
   670     time_t seconds = IN.tv_sec;
   647     if (NULL == localtime_r(&seconds, &broken_down_time)){ /* get the UTC (GMT) broken down time */
   671     if (NULL == (broken_down_time = localtime(&seconds))){ /* get the UTC (GMT) broken down time */
   648         IEC_error();
   672         IEC_error();
   649         return (STRING){7,"D#ERROR"};
   673         return (STRING){7,"D#ERROR"};
   650     }
   674     }
   651     res.len = snprintf((char*)&res.body, STR_MAX_LEN, "D#%d-%2.2d-%2.2d", broken_down_time.tm_year + 1900, broken_down_time.tm_mon + 1, broken_down_time.tm_mday);
   675     res.len = snprintf((char*)&res.body, STR_MAX_LEN, "D#%d-%2.2d-%2.2d", broken_down_time->tm_year + 1900, broken_down_time->tm_mon + 1, broken_down_time->tm_mday);
   652     if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN;
   676     if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN;
   653     return res;
   677     return res;
   654 }
   678 }
   655 static inline STRING __tod_to_string(TOD IN){
   679 static inline STRING __tod_to_string(TOD IN){
   656     /* TOD#15:36:55.36 */
   680     /* TOD#15:36:55.36 */
   657     STRING res = __INIT_STRING;
   681     STRING res = __INIT_STRING;
   658     struct tm broken_down_time;
   682     struct tm* broken_down_time;
   659     time_t seconds = IN.tv_sec;
   683     time_t seconds = IN.tv_sec;
   660     if (NULL == localtime_r(&seconds, &broken_down_time)){ /* get the UTC (GMT) broken down time */
   684     if (NULL == (broken_down_time = localtime(&seconds))){ /* get the UTC (GMT) broken down time */
   661         IEC_error();
   685         IEC_error();
   662         return (STRING){9,"TOD#ERROR"};
   686         return (STRING){9,"TOD#ERROR"};
   663     }
   687     }
   664     if(IN.tv_nsec == 0){
   688     if(IN.tv_nsec == 0){
   665         res.len = snprintf((char*)&res.body, STR_MAX_LEN, "TOD#%2.2d:%2.2d:%d", broken_down_time.tm_hour, broken_down_time.tm_min, broken_down_time.tm_sec);
   689         res.len = snprintf((char*)&res.body, STR_MAX_LEN, "TOD#%2.2d:%2.2d:%d", broken_down_time->tm_hour, broken_down_time->tm_min, broken_down_time->tm_sec);
   666     }else{
   690     }else{
   667         res.len = snprintf((char*)&res.body, STR_MAX_LEN, "TOD#%2.2d:%2.2d:%g", broken_down_time.tm_hour, broken_down_time.tm_min, (LREAL)broken_down_time.tm_sec + (LREAL)IN.tv_nsec / 1e9);
   691         res.len = snprintf((char*)&res.body, STR_MAX_LEN, "TOD#%2.2d:%2.2d:%g", broken_down_time->tm_hour, broken_down_time->tm_min, (LREAL)broken_down_time->tm_sec + (LREAL)IN.tv_nsec / 1e9);
   668     }
   692     }
   669     if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN;
   693     if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN;
   670     return res;
   694     return res;
   671 }
   695 }
   672 static inline STRING __dt_to_string(DT IN){
   696 static inline STRING __dt_to_string(DT IN){
   673     /* DT#1984-06-25-15:36:55.36 */
   697     /* DT#1984-06-25-15:36:55.36 */
   674     STRING res;
   698     STRING res;
   675     struct tm broken_down_time;
   699     struct tm* broken_down_time;
   676     time_t seconds = IN.tv_sec;
   700     time_t seconds = IN.tv_sec;
   677     if (NULL == localtime_r(&seconds, &broken_down_time)){ /* get the UTC (GMT) broken down time */
   701     if (NULL == (broken_down_time = localtime(&seconds))){ /* get the UTC (GMT) broken down time */
   678         IEC_error();
   702         IEC_error();
   679         return (STRING){8,"DT#ERROR"};
   703         return (STRING){8,"DT#ERROR"};
   680     }
   704     }
   681     if(IN.tv_nsec == 0){
   705     if(IN.tv_nsec == 0){
   682         res.len = snprintf((char*)&res.body, STR_MAX_LEN, "DT#%d-%2.2d-%2.2d-%2.2d:%2.2d:%d",
   706         res.len = snprintf((char*)&res.body, STR_MAX_LEN, "DT#%d-%2.2d-%2.2d-%2.2d:%2.2d:%d",
   683                  broken_down_time.tm_year,
   707                  broken_down_time->tm_year,
   684                  broken_down_time.tm_mon,
   708                  broken_down_time->tm_mon,
   685                  broken_down_time.tm_mday,
   709                  broken_down_time->tm_mday,
   686                  broken_down_time.tm_hour,
   710                  broken_down_time->tm_hour,
   687                  broken_down_time.tm_min,
   711                  broken_down_time->tm_min,
   688                  broken_down_time.tm_sec);
   712                  broken_down_time->tm_sec);
   689     }else{
   713     }else{
   690         res.len = snprintf((char*)&res.body, STR_MAX_LEN, "DT#%d-%2.2d-%2.2d-%2.2d:%2.2d:%g",
   714         res.len = snprintf((char*)&res.body, STR_MAX_LEN, "DT#%d-%2.2d-%2.2d-%2.2d:%2.2d:%g",
   691                  broken_down_time.tm_year,
   715                  broken_down_time->tm_year,
   692                  broken_down_time.tm_mon,
   716                  broken_down_time->tm_mon,
   693                  broken_down_time.tm_mday,
   717                  broken_down_time->tm_mday,
   694                  broken_down_time.tm_hour,
   718                  broken_down_time->tm_hour,
   695                  broken_down_time.tm_min,
   719                  broken_down_time->tm_min,
   696                  (LREAL)broken_down_time.tm_sec + ((LREAL)IN.tv_nsec / 1e9));
   720                  (LREAL)broken_down_time->tm_sec + ((LREAL)IN.tv_nsec / 1e9));
   697     }
   721     }
   698     if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN;
   722     if(res.len > STR_MAX_LEN) res.len = STR_MAX_LEN;
   699     return res;
   723     return res;
   700 }
   724 }
   701     /* BCD */
   725     /* BCD */