123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668 |
- #include "iec_include.h"
- #include "mmodbus.h"
- typedef uint64_t time_t;
- struct tm {
- int tm_sec;
- int tm_min;
- int tm_hour;
- int tm_mday;
- int tm_mon;
- int tm_year;
- int tm_wday;
- int tm_yday;
- int tm_isdst;
- };
-
- bool
- CP16Time2a_getFromBuffer (CP16Time2a self, const uint8_t* msg, int msgSize, int startIndex)
- {
- if (msgSize < startIndex + 2)
- return false;
- int i;
- for (i = 0; i < 2; i++)
- self->encodedValue[i] = msg[startIndex + i];
- return true;
- }
- int
- CP16Time2a_getEplapsedTimeInMs(const CP16Time2a self)
- {
- return (self->encodedValue[0] + (self->encodedValue[1] * 0x100));
- }
- void
- CP16Time2a_setEplapsedTimeInMs(CP16Time2a self, int value)
- {
- self->encodedValue[0] = (uint8_t) (value % 0x100);
- self->encodedValue[1] = (uint8_t) (value / 0x100);
- }
- uint8_t*
- CP16Time2a_getEncodedValue(CP16Time2a self)
- {
- return self->encodedValue;
- }
- /************************************
- * CP24Time2a and CP26Time2a common
- ************************************/
- static int
- getMillisecond(const uint8_t* encodedValue)
- {
- return (encodedValue[0] + (encodedValue[1] * 0x100)) % 1000;
- }
- static void
- setMillisecond(uint8_t* encodedValue, int value)
- {
- int millies = encodedValue[0] + (encodedValue[1] * 0x100);
- /* erase sub-second part */
- millies = millies - (millies % 1000);
- millies = millies + value;
- encodedValue[0] = (uint8_t) (millies & 0xff);
- encodedValue[1] = (uint8_t) ((millies / 0x100) & 0xff);
- }
- static int
- getSecond(const uint8_t* encodedValue)
- {
- return (encodedValue[0] + (encodedValue[1] * 0x100)) / 1000;
- }
- static void
- setSecond(uint8_t* encodedValue, int value)
- {
- int millies = encodedValue[0] + (encodedValue[1] * 0x100);
- int msPart = millies % 1000;
- millies = (value * 1000) + msPart;
- encodedValue[0] = (uint8_t) (millies & 0xff);
- encodedValue[1] = (uint8_t) ((millies / 0x100) & 0xff);
- }
- static int
- getMinute(const uint8_t* encodedValue)
- {
- return (encodedValue[2] & 0x3f);
- }
- static void
- setMinute(uint8_t* encodedValue, int value)
- {
- encodedValue[2] = (uint8_t) ((encodedValue[2] & 0xc0) | (value & 0x3f));
- }
- static bool
- isInvalid(const uint8_t* encodedValue)
- {
- return ((encodedValue[2] & 0x80) != 0);
- }
- static void
- setInvalid(uint8_t* encodedValue, bool value)
- {
- if (value)
- encodedValue[2] |= 0x80;
- else
- encodedValue[2] &= 0x7f;
- }
- static bool
- isSubstituted(const uint8_t* encodedValue)
- {
- return ((encodedValue[2] & 0x40) == 0x40);
- }
- static void
- setSubstituted(uint8_t* encodedValue, bool value)
- {
- if (value)
- encodedValue[2] |= 0x40;
- else
- encodedValue[2] &= 0xbf;
- }
- /**********************************
- * CP24Time2a type
- **********************************/
- bool
- CP24Time2a_getFromBuffer (CP24Time2a self, const uint8_t* msg, int msgSize, int startIndex)
- {
- if (msgSize < startIndex + 3)
- return false;
- int i;
- for (i = 0; i < 3; i++)
- self->encodedValue[i] = msg[startIndex + i];
- return true;
- }
- int
- CP24Time2a_getMillisecond(const CP24Time2a self)
- {
- return getMillisecond(self->encodedValue);
- }
- void
- CP24Time2a_setMillisecond(CP24Time2a self, int value)
- {
- setMillisecond(self->encodedValue, value);
- }
- int
- CP24Time2a_getSecond(const CP24Time2a self)
- {
- return getSecond(self->encodedValue);
- }
- void
- CP24Time2a_setSecond(CP24Time2a self, int value)
- {
- setSecond(self->encodedValue, value);
- }
- int
- CP24Time2a_getMinute(const CP24Time2a self)
- {
- return getMinute(self->encodedValue);
- }
- void
- CP24Time2a_setMinute(CP24Time2a self, int value)
- {
- setMinute(self->encodedValue, value);
- }
- bool
- CP24Time2a_isInvalid(const CP24Time2a self)
- {
- return isInvalid(self->encodedValue);
- }
- void
- CP24Time2a_setInvalid(CP24Time2a self, bool value)
- {
- setInvalid(self->encodedValue, value);
- }
- bool
- CP24Time2a_isSubstituted(const CP24Time2a self)
- {
- return isSubstituted(self->encodedValue);
- }
- void
- CP24Time2a_setSubstituted(CP24Time2a self, bool value)
- {
- setSubstituted(self->encodedValue, value);
- }
- #if 0
- /* function found here: http://stackoverflow.com/questions/530519/stdmktime-and-timezone-info */
- time_t my_timegm(register struct tm * t)
- /* struct tm to seconds since Unix epoch */
- {
- register long year;
- register time_t result;
- #define MONTHSPERYEAR 12 /* months per calendar year */
- static const int cumdays[MONTHSPERYEAR] =
- { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
- /*@ +matchanyintegral @*/
- year = 1900 + t->tm_year + t->tm_mon / MONTHSPERYEAR;
- result = (year - 1970) * 365 + cumdays[t->tm_mon % MONTHSPERYEAR];
- result += (year - 1968) / 4;
- result -= (year - 1900) / 100;
- result += (year - 1600) / 400;
- if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0) &&
- (t->tm_mon % MONTHSPERYEAR) < 2)
- result--;
- result += t->tm_mday - 1;
- result *= 24;
- result += t->tm_hour;
- result *= 60;
- result += t->tm_min;
- result *= 60;
- result += t->tm_sec;
- if (t->tm_isdst == 1)
- result -= 3600;
- /*@ -matchanyintegral @*/
- return (result);
- }
- #endif
- /* Conversion from UTC date to second, unsigned 32-bit Unix epoch version.
- * Written by François Grieu, 2015-07-21; public domain.
- *
- * my_mktime converts from struct tm UTC to non-leap seconds since
- * 00:00:00 on the first UTC day of year 1970 (fixed).
- * It works from 1970 to 2105 inclusive. It strives to be compatible
- * with C compilers supporting // comments and claiming C89 conformance.
- *
- * input: Pointer to a struct tm with field tm_year, tm_mon, tm_mday,
- * tm_hour, tm_min, tm_sec set per mktime convention; thus
- * - tm_year is year minus 1900
- * - tm_mon is [0..11] for January to December, but [-2..14]
- * works for November of previous year to February of next year
- * - tm_mday, tm_hour, tm_min, tm_sec similarly can be offset to
- * the full range [-32767 to 32768], as long as the combination
- * with tm_year gives a result within years [1970..2105], and
- * tm_year>0.
- * output: Number of non-leap seconds since beginning of the first UTC
- * day of year 1970, as an unsigned at-least-32-bit integer.
- * The input is not changed (in particular, fields tm_wday,
- * tm_yday, and tm_isdst are unchanged and ignored).
- */
- static time_t
- my_mktime(const struct tm * ptm)
- {
- int m, y = ptm->tm_year;
- if ((m = ptm->tm_mon) < 2) {
- m += 12;
- --y;
- }
- return ((((time_t) (y - 69) * 365u + y / 4 - y / 100 * 3 / 4 + (m + 2) * 153 / 5 - 446 +
- ptm->tm_mday) * 24u + ptm->tm_hour) * 60u + ptm->tm_min) * 60u + ptm->tm_sec;
- }
- /**********************************
- * CP32Time2a type
- **********************************/
- CP32Time2a
- CP32Time2a_create(CP32Time2a self)
- {
- if (self == NULL)
- self = (CP32Time2a) GLOBAL_CALLOC(1, sizeof(struct sCP32Time2a));
- else
- memset (self, 0, sizeof(struct sCP32Time2a));
- return self;
- }
- bool
- CP32Time2a_getFromBuffer (CP32Time2a self, const uint8_t* msg, int msgSize, int startIndex)
- {
- if (msgSize < startIndex + 4)
- return false;
- int i;
- for (i = 0; i < 4; i++)
- self->encodedValue[i] = msg[startIndex + i];
- return true;
- }
- int
- CP32Time2a_getMillisecond(const CP32Time2a self)
- {
- return (self->encodedValue[0] + (self->encodedValue[1] * 0x100)) % 1000;
- }
- void
- CP32Time2a_setMillisecond(CP32Time2a self, int value)
- {
- int millies = (CP32Time2a_getSecond(self) * 1000) + value;
- self->encodedValue[0] = (uint8_t) (millies & 0xff);
- self->encodedValue[1] = (uint8_t) ((millies / 0x100) & 0xff);
- }
- int
- CP32Time2a_getSecond(const CP32Time2a self)
- {
- return getSecond(self->encodedValue);
- }
- void
- CP32Time2a_setSecond(CP32Time2a self, int value)
- {
- setSecond(self->encodedValue, value);
- }
- int
- CP32Time2a_getMinute(const CP32Time2a self)
- {
- return getMinute(self->encodedValue);
- }
- void
- CP32Time2a_setMinute(CP32Time2a self, int value)
- {
- setMinute(self->encodedValue, value);
- }
- bool
- CP32Time2a_isInvalid(const CP32Time2a self)
- {
- return isInvalid(self->encodedValue);
- }
- void
- CP32Time2a_setInvalid(CP32Time2a self, bool value)
- {
- setInvalid(self->encodedValue, value);
- }
- bool
- CP32Time2a_isSubstituted(const CP32Time2a self)
- {
- return isSubstituted(self->encodedValue);
- }
- void
- CP32Time2a_setSubstituted(CP32Time2a self, bool value)
- {
- setSubstituted(self->encodedValue, value);
- }
- int
- CP32Time2a_getHour(const CP32Time2a self)
- {
- return (self->encodedValue[3] & 0x1f);
- }
- void
- CP32Time2a_setHour(CP32Time2a self, int value)
- {
- self->encodedValue[3] = (uint8_t) ((self->encodedValue[3] & 0xe0) | (value & 0x1f));
- }
- bool
- CP32Time2a_isSummerTime(const CP32Time2a self)
- {
- return ((self->encodedValue[3] & 0x80) != 0);
- }
- void
- CP32Time2a_setSummerTime(CP32Time2a self, bool value)
- {
- if (value)
- self->encodedValue[3] |= 0x80;
- else
- self->encodedValue[3] &= 0x7f;
- }
- void
- CP32Time2a_setFromMsTimestamp(CP32Time2a self, uint64_t timestamp)
- {
- memset(self->encodedValue, 0, 4);
- time_t timeVal = timestamp / 1000;
- int msPart = timestamp % 1000;
- struct tm tmTime;
- //#ifdef _WIN32
- // gmtime_s(&tmTime, &timeVal);
- //#else
- // gmtime_r(&timeVal, &tmTime);
- //#endif
- CP32Time2a_setSecond(self, tmTime.tm_sec);
- CP32Time2a_setMillisecond(self, msPart);
- CP32Time2a_setMinute(self, tmTime.tm_min);
- CP32Time2a_setHour(self, tmTime.tm_hour);
- }
- uint8_t*
- CP32Time2a_getEncodedValue(CP32Time2a self)
- {
- return self->encodedValue;
- }
- /**********************************
- * CP56Time2a type
- **********************************/
- CP56Time2a
- CP56Time2a_createFromMsTimestamp(CP56Time2a self, uint64_t timestamp)
- {
- if (self == NULL)
- self = (CP56Time2a) GLOBAL_CALLOC(1, sizeof(struct sCP56Time2a));
- else
- memset (self, 0, sizeof(struct sCP56Time2a));
- if (self != NULL)
- CP56Time2a_setFromMsTimestamp(self, timestamp);
- return self;
- }
- void
- CP56Time2a_setFromMsTimestamp(CP56Time2a self, uint64_t timestamp)
- {
- memset(self->encodedValue, 0, 7);
- time_t timeVal = timestamp / 1000;
- int msPart = timestamp % 1000;
- struct tm tmTime;
- /* TODO replace with portable implementation */
- //#ifdef _WIN32
- //gmtime_s(&tmTime, &timeVal);
- //#else
- // gmtime_r(&timeVal, &tmTime);
- //#endif
- CP56Time2a_setMillisecond(self, msPart);
- CP56Time2a_setSecond(self, tmTime.tm_sec);
- CP56Time2a_setMinute(self, tmTime.tm_min);
- CP56Time2a_setHour(self, tmTime.tm_hour);
- CP56Time2a_setDayOfMonth(self, tmTime.tm_mday);
- /* set day of week to 0 = not present */
- CP56Time2a_setDayOfWeek(self, 0);
- CP56Time2a_setMonth(self, tmTime.tm_mon + 1);
- CP56Time2a_setYear(self, tmTime.tm_year);
- }
- uint64_t
- CP56Time2a_toMsTimestamp(const CP56Time2a self)
- {
- struct tm tmTime;
- tmTime.tm_sec = CP56Time2a_getSecond(self);
- tmTime.tm_min = CP56Time2a_getMinute(self);
- tmTime.tm_hour = CP56Time2a_getHour(self);
- tmTime.tm_mday = CP56Time2a_getDayOfMonth(self);
- tmTime.tm_mon = CP56Time2a_getMonth(self) - 1;
- tmTime.tm_year = CP56Time2a_getYear(self) + 100;
- time_t timestamp = my_mktime(&tmTime);
- uint64_t msTimestamp = ((uint64_t) (timestamp * (uint64_t) 1000)) + CP56Time2a_getMillisecond(self);
- return msTimestamp;
- }
- /* private */ bool
- CP56Time2a_getFromBuffer(CP56Time2a self, const uint8_t* msg, int msgSize, int startIndex)
- {
- if (msgSize < startIndex + 7)
- return false;
- int i;
- for (i = 0; i < 7; i++)
- self->encodedValue[i] = msg[startIndex + i];
- return true;
- }
- int
- CP56Time2a_getMillisecond(const CP56Time2a self)
- {
- return getMillisecond(self->encodedValue);
- }
- void
- CP56Time2a_setMillisecond(CP56Time2a self, int value)
- {
- setMillisecond(self->encodedValue, value);
- }
- int
- CP56Time2a_getSecond(const CP56Time2a self)
- {
- return getSecond(self->encodedValue);
- }
- void
- CP56Time2a_setSecond(CP56Time2a self, int value)
- {
- setSecond(self->encodedValue, value);
- }
- int
- CP56Time2a_getMinute(const CP56Time2a self)
- {
- return getMinute(self->encodedValue);
- }
- void
- CP56Time2a_setMinute(CP56Time2a self, int value)
- {
- setMinute(self->encodedValue, value);
- }
- int
- CP56Time2a_getHour(const CP56Time2a self)
- {
- return (self->encodedValue[3] & 0x1f);
- }
- void
- CP56Time2a_setHour(CP56Time2a self, int value)
- {
- self->encodedValue[3] = (uint8_t) ((self->encodedValue[3] & 0xe0) | (value & 0x1f));
- }
- int
- CP56Time2a_getDayOfWeek(const CP56Time2a self)
- {
- return ((self->encodedValue[4] & 0xe0) >> 5);
- }
- void
- CP56Time2a_setDayOfWeek(CP56Time2a self, int value)
- {
- self->encodedValue[4] = (uint8_t) ((self->encodedValue[4] & 0x1f) | ((value & 0x07) << 5));
- }
- int
- CP56Time2a_getDayOfMonth(const CP56Time2a self)
- {
- return (self->encodedValue[4] & 0x1f);
- }
- void
- CP56Time2a_setDayOfMonth(CP56Time2a self, int value)
- {
- self->encodedValue[4] = (uint8_t) ((self->encodedValue[4] & 0xe0) + (value & 0x1f));
- }
- int
- CP56Time2a_getMonth(const CP56Time2a self)
- {
- return (self->encodedValue[5] & 0x0f);
- }
- void
- CP56Time2a_setMonth(CP56Time2a self, int value)
- {
- self->encodedValue[5] = (uint8_t) ((self->encodedValue[5] & 0xf0) + (value & 0x0f));
- }
- int
- CP56Time2a_getYear(const CP56Time2a self)
- {
- return (self->encodedValue[6] & 0x7f);
- }
- void
- CP56Time2a_setYear(CP56Time2a self, int value)
- {
- value = value % 100;
- self->encodedValue[6] = (uint8_t) ((self->encodedValue[6] & 0x80) + (value & 0x7f));
- }
- bool
- CP56Time2a_isSummerTime(const CP56Time2a self)
- {
- return ((self->encodedValue[3] & 0x80) != 0);
- }
- void
- CP56Time2a_setSummerTime(CP56Time2a self, bool value)
- {
- if (value)
- self->encodedValue[3] |= 0x80;
- else
- self->encodedValue[3] &= 0x7f;
- }
- bool
- CP56Time2a_isInvalid(const CP56Time2a self)
- {
- return isInvalid(self->encodedValue);
- }
- void
- CP56Time2a_setInvalid(CP56Time2a self, bool value)
- {
- setInvalid(self->encodedValue, value);
- }
- bool
- CP56Time2a_isSubstituted(const CP56Time2a self)
- {
- return isSubstituted(self->encodedValue);
- }
- void
- CP56Time2a_setSubstituted(CP56Time2a self, bool value)
- {
- setSubstituted(self->encodedValue, value);
- }
- uint8_t*
- CP56Time2a_getEncodedValue(CP56Time2a self)
- {
- return self->encodedValue;
- }
|