146 fprintf(stderr, "IEC 61131-3 runtime error.\n"); |
146 fprintf(stderr, "IEC 61131-3 runtime error.\n"); |
147 /*exit(1);*/ |
147 /*exit(1);*/ |
148 } |
148 } |
149 |
149 |
150 |
150 |
151 static inline void __normalize_timespec (struct timespec *ts) { |
151 static inline void __normalize_timespec (IEC_TIMESPEC *ts) { |
152 if( ts->tv_nsec < -1000000000 || (( ts->tv_sec > 0 ) && ( ts->tv_nsec < 0 ))){ |
152 if( ts->tv_nsec < -1000000000 || (( ts->tv_sec > 0 ) && ( ts->tv_nsec < 0 ))){ |
153 ts->tv_sec--; |
153 ts->tv_sec--; |
154 ts->tv_nsec += 1000000000; |
154 ts->tv_nsec += 1000000000; |
155 } |
155 } |
156 if( ts->tv_nsec > 1000000000 || (( ts->tv_sec < 0 ) && ( ts->tv_nsec > 0 ))){ |
156 if( ts->tv_nsec > 1000000000 || (( ts->tv_sec < 0 ) && ( ts->tv_nsec > 0 ))){ |
157 ts->tv_sec++; |
157 ts->tv_sec++; |
158 ts->tv_nsec -= 1000000000; |
158 ts->tv_nsec -= 1000000000; |
159 } |
159 } |
160 } |
160 } |
161 |
161 |
162 static inline struct timespec __time_to_timespec(int sign, double mseconds, double seconds, double minutes, double hours, double days) { |
162 static inline IEC_TIMESPEC __time_to_timespec(int sign, double mseconds, double seconds, double minutes, double hours, double days) { |
163 struct timespec ts; |
163 IEC_TIMESPEC ts; |
164 |
164 |
165 /* sign is 1 for positive values, -1 for negative time... */ |
165 /* sign is 1 for positive values, -1 for negative time... */ |
166 long double total_sec = ((days*24 + hours)*60 + minutes)*60 + seconds + mseconds/1e3; |
166 long double total_sec = ((days*24 + hours)*60 + minutes)*60 + seconds + mseconds/1e3; |
167 if (sign >= 0) sign = 1; else sign = -1; |
167 if (sign >= 0) sign = 1; else sign = -1; |
168 ts.tv_sec = sign * (long int)total_sec; |
168 ts.tv_sec = sign * (long int)total_sec; |
170 |
170 |
171 return ts; |
171 return ts; |
172 } |
172 } |
173 |
173 |
174 |
174 |
175 static inline struct timespec __tod_to_timespec(double seconds, double minutes, double hours) { |
175 static inline IEC_TIMESPEC __tod_to_timespec(double seconds, double minutes, double hours) { |
176 struct timespec ts; |
176 IEC_TIMESPEC ts; |
177 |
177 |
178 long double total_sec = (hours*60 + minutes)*60 + seconds; |
178 long double total_sec = (hours*60 + minutes)*60 + seconds; |
179 ts.tv_sec = (long int)total_sec; |
179 ts.tv_sec = (long int)total_sec; |
180 ts.tv_nsec = (long int)((total_sec - ts.tv_sec)*1e9); |
180 ts.tv_nsec = (long int)((total_sec - ts.tv_sec)*1e9); |
181 |
181 |
182 return ts; |
182 return ts; |
183 } |
183 } |
184 |
184 |
185 static inline struct timespec __date_to_timespec(int day, int month, int year) { |
185 static inline IEC_TIMESPEC __date_to_timespec(int day, int month, int year) { |
186 struct timespec ts; |
186 IEC_TIMESPEC ts; |
187 struct tm broken_down_time; |
187 struct tm broken_down_time; |
188 time_t epoch_seconds; |
188 time_t epoch_seconds; |
189 |
189 |
190 broken_down_time.tm_sec = 0; |
190 broken_down_time.tm_sec = 0; |
191 broken_down_time.tm_min = 0; |
191 broken_down_time.tm_min = 0; |
203 ts.tv_nsec = 0; |
203 ts.tv_nsec = 0; |
204 |
204 |
205 return ts; |
205 return ts; |
206 } |
206 } |
207 |
207 |
208 static inline struct timespec __dt_to_timespec(double seconds, double minutes, double hours, int day, int month, int year) { |
208 static inline IEC_TIMESPEC __dt_to_timespec(double seconds, double minutes, double hours, int day, int month, int year) { |
209 struct timespec ts; |
209 IEC_TIMESPEC ts; |
210 struct tm broken_down_time; |
210 struct tm broken_down_time; |
211 time_t epoch_seconds; |
211 time_t epoch_seconds; |
212 |
212 |
213 long double total_sec = (hours*60 + minutes)*60 + seconds; |
213 long double total_sec = (hours*60 + minutes)*60 + seconds; |
214 ts.tv_sec = (long int)total_sec; |
214 ts.tv_sec = (long int)total_sec; |
789 res |= (IN % 10) << i; |
789 res |= (IN % 10) << i; |
790 } |
790 } |
791 return res; |
791 return res; |
792 } |
792 } |
793 |
793 |
794 /* workaround for va-atgs limitation on shorter that int params */ |
794 /* workaround for va-args limitation on shorter than int params */ |
795 #define VA_ARGS_REAL LREAL |
795 #define VA_ARGS_REAL LREAL |
796 #define VA_ARGS_LREAL LREAL |
796 #define VA_ARGS_LREAL LREAL |
797 #define VA_ARGS_SINT DINT |
797 #define VA_ARGS_SINT DINT |
798 #define VA_ARGS_INT DINT |
798 #define VA_ARGS_INT DINT |
799 #define VA_ARGS_DINT DINT |
799 #define VA_ARGS_DINT DINT |