diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index eeec6e9..0413e7d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -36,11 +36,11 @@ x86_64-linux: script: - sed -i 's/deb.debian.org/mirror.yandex.ru/' /etc/apt/sources.list - sed -i 's/security.debian.org/mirror.yandex.ru/' /etc/apt/sources.list - - apt update -y && apt install -y ninja-build cmake git gcc g++ + - apt update -y && apt install -y ninja-build cmake git gcc - mkdir build - cd build - - cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS_RELEASE="-O3 -DNDEBUG -Wall -Wextra -pedantic -Werror -Wno-unused-parameter -std=c99" -DCMAKE_FIND_ROOT_PATH=/tmp/output .. + - cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS_RELEASE="-O3 -DNDEBUG -Wall -Wextra -pedantic -Werror -Wno-unused-parameter -std=c99" .. - ninja - ninja test tags: @@ -52,7 +52,7 @@ x86_64-linux-memsan: script: - sed -i 's/deb.debian.org/mirror.yandex.ru/' /etc/apt/sources.list - sed -i 's/security.debian.org/mirror.yandex.ru/' /etc/apt/sources.list - - apt update -y && apt install -y ninja-build cmake git gcc g++ + - apt update -y && apt install -y ninja-build cmake git gcc - mkdir build - cd build @@ -62,17 +62,33 @@ x86_64-linux-memsan: tags: - docker +x86_64-linux-valgrind: + image: debian:9 + stage: build + script: + - sed -i 's/deb.debian.org/mirror.yandex.ru/' /etc/apt/sources.list + - sed -i 's/security.debian.org/mirror.yandex.ru/' /etc/apt/sources.list + - apt update -y && apt install -y ninja-build cmake git gcc valgrind + + - mkdir build + - cd build + - cmake -G "Ninja" -DCMAKE_C_FLAGS="-g -O0" .. + - ninja + - find -executable -type f -name 'unit_*' -exec valgrind '{}' \; + tags: + - docker + x86_64-linux-coverage: image: debian:9 stage: build script: - sed -i 's/deb.debian.org/mirror.yandex.ru/' /etc/apt/sources.list - sed -i 's/security.debian.org/mirror.yandex.ru/' /etc/apt/sources.list - - apt update -y && apt install -y ninja-build cmake git gcc g++ gcovr + - apt update -y && apt install -y ninja-build cmake git gcc gcovr - mkdir build - cd build - - cmake -G "Ninja" -DTEKON_TESTS_COVERAGE=1 -DCMAKE_FIND_ROOT_PATH=/tmp/output .. + - cmake -G "Ninja" -DTEKON_TESTS_COVERAGE=1 .. - ninja - ninja test - ../test/suite_msr.sh $(pwd) @@ -89,12 +105,14 @@ armhf-linux: script: - sed -i 's/deb.debian.org/mirror.yandex.ru/' /etc/apt/sources.list - sed -i 's/security.debian.org/mirror.yandex.ru/' /etc/apt/sources.list - - apt update -y && apt install -y ninja-build cmake git gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf + - apt update -y && apt install -y ninja-build cmake git gcc-arm-linux-gnueabihf qemu - mkdir build - cd build - - cmake -G "Ninja" -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc -DCMAKE_FIND_ROOT_PATH=/usr/arm-linux-gnueabihf -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS_RELEASE="-O3 -DNDEBUG -Wall -Wextra -Wno-unused-parameter -std=c99" -DCMAKE_FIND_ROOT_PATH=/tmp/output .. + - cmake -G "Ninja" -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS_RELEASE="-O3 -DNDEBUG -Wall -Wextra -Wno-unused-parameter -std=c99" .. - ninja + + - find -executable -type f -name 'unit_*' -exec qemu-arm -L /usr/arm-linux-gnueabihf '{}' \; tags: - docker @@ -105,12 +123,16 @@ x86_64-mingw: script: - sed -i 's/deb.debian.org/mirror.yandex.ru/' /etc/apt/sources.list - sed -i 's/security.debian.org/mirror.yandex.ru/' /etc/apt/sources.list - - apt update && apt install -y ninja-build cmake git gcc-mingw-w64-x86-64 g++-mingw-w64-x86-64 build-essential autoconf automake libtool + - apt update && apt install -y ninja-build cmake git gcc-mingw-w64-x86-64 build-essential autoconf automake libtool wine - mkdir build - cd build - cmake -G "Ninja" -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -DCMAKE_LINKER=/usr/x86_64-w64-mingw32/bin/ld -DCMAKE_C_STANDARD_LIBRARIES="-lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -lws2_32 -liphlpapi -lpsapi -luserenv" -DCMAKE_EXE_LINKER_FLAGS="-Wl,-Bstatic -lwinpthread" .. - ninja + - find -executable -type f -name 'unit_*' -exec wine '{}' \; + tags: - docker + + diff --git a/README.md b/README.md index a2060b6..5ead771 100644 --- a/README.md +++ b/README.md @@ -4,35 +4,31 @@ # TEKON-UTILS -Набор утилит для работы со счетчиками [Тэкон](https://kreit.ru/) через Ethernet -контроллер [К-104](https://kreit.ru/products/communication/k-104.html). +Набор утилит для работы со счетчиками [Тэкон](https://kreit.ru/) через Ethernet контроллер [К-104](https://kreit.ru/products/communication/k-104.html). Позволяют выполнять чтение значений, чтение архивов, синхронизацию времени. -Для работы используется протокол описанный в [Т10.06.59РД](https://kreit.ru/files/prot17_6.pdf) -и [Т10.06.59РД-Д1](https://kreit.ru/files/prot_d1.pdf). - +Для работы используется протокол описанный в [Т10.06.59РД](https://kreit.ru/files/prot17_6.pdf) и [Т10.06.59РД-Д1](https://kreit.ru/files/prot_d1.pdf). ## Быстрый старт ### Чтение параметров - ```console -tekon_msr -a udp:10.0.0.3:51960@9 -p'3:0xF001:0:H 3:0xF017:0:D 3:0xF018:0:T' -9:3:0xf001 H 0x3840 OK 1557896484 18000 -9:3:0xf017 D 2019-05-15 OK 1557896484 18000 -9:3:0xf018 T 10:01:22 OK 1557896484 18000 +tekon_msr -a udp:10.0.0.3:51960@9 -p'3:0xF001:0:H 3:0xF017:0:D 3:0xF018:0:T 3:0x801c:2:f' +9:3:0xf001:0 H 0x3840 OK 1558082153 18000 +9:3:0xf017:0 D 2019-05-17 OK 1558082153 18000 +9:3:0xf018:0 T 13:35:49 OK 1558082153 18000 +9:3:0x801c:2 F 38.151833 OK 1558082153 18000 ``` -### Чтение архива +### Чтение архива ```console tekon_arch -a udp:10.0.0.3:51960@9 -p 3:0x801C:0:12:F -i m:12 -d 3:0xF017:0xF018 -9:3:0x801c F -nan OK 1546282800 18000 0 -9:3:0x801c F -nan OK 1548961200 18000 1 -9:3:0x801c F 38.151833 OK 1551380400 18000 2 +9:3:0x801c:0 F -nan OK 1546282800 18000 +9:3:0x801c:1 F -nan OK 1548961200 18000 +9:3:0x801c:2 F 38.151833 OK 1551380400 18000 ... ``` ### Синхронизация времени - ```console tekon_sync -a udp:10.0.0.3:51960@9 -d 3:0xF017:0xF018 -p 00000001 ``` @@ -45,8 +41,7 @@ tekon_sync -a udp:10.0.0.3:51960@9 -d 3:0xF017:0xF018 -p 00000001 * cmake * make -### Пример сборки для Debian x86_64 - +### Debian x86_64 ```console apt install git cmake make gcc git clone @@ -58,8 +53,7 @@ make make test ``` -### Пример кросс-компиляции для Debian ARMv7 - +### Debian ARM ```console apt install git cmake make gcc-arm-linux-gnueabihf git clone @@ -70,8 +64,7 @@ cmake -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc -DCMAKE_FIND_ROOT_PATH=/usr/arm make ``` -### Пример кросс-компиляции для Windows XP x86 - +### Windows XP x86 ```console apt install git cmake make gcc-mingw-w64-i686 git clone @@ -82,6 +75,11 @@ cmake -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_C_COMPILER=i686-w64-mingw32-gcc -DCMAK make ``` +Все примеры выполнены в ОС Debian 9 x86_64. + +git clone - вместо параметра следует подставить адрес репозитория, +например, https://github.com/alexs-sh/tekon-utils.git + ## Применение Утилиты для чтения получают данные с устройства и выводят их в stdout. Чтение @@ -102,22 +100,21 @@ make ### Чтение значений Формат вывода данных: -шлюз:адрес:параметр тип значение качество UTC TZ +шлюз:адрес:параметр:индекс тип значение качество UTC TZ ```console -9:3:0xf001 H 0x3840 OK 1557900053 18000 +9:3:0xf001:0 H 0x3840 OK 1557900053 18000 ``` ### Чтение архива Формат вывода данных: -шлюз:адрес:параметр тип значение качество UTC TZ индекс +шлюз:адрес:параметр:индекс тип значение качество UTC TZ ```console -9:3:0x801c F -nan OK 1546282800 18000 0 +9:3:0x801c:0 F -nan OK 1546282800 18000 ``` - Чтение архива может выполнятся в 2-х вариантах: * без метки времени @@ -127,9 +124,9 @@ make Чтение без меток времени. Все значения UTC равны -1. ```console tekon_arch -a udp:10.0.0.3:51960@9 -p 3:0x801C:0:12:F -9:3:0x801c F -nan OK -1 18000 0 -9:3:0x801c F -nan OK -1 18000 1 -9:3:0x801c F 38.151833 OK -1 18000 2 +9:3:0x801c:0 F -nan OK -1 18000 +9:3:0x801c:1 F -nan OK -1 18000 +9:3:0x801c:2 F 38.151833 OK -1 18000 ... ``` @@ -137,12 +134,18 @@ tekon_arch -a udp:10.0.0.3:51960@9 -p 3:0x801C:0:12:F интервала и адресов даты / времени. Переводит индексы Тэкон в UTC время. ```console tekon_arch -a udp:10.0.0.3:51960@9 -p 3:0x801C:0:12:F -i m:12 -d 3:0xF017:0xF018 -9:3:0x801c F -nan OK 1546282800 18000 0 -9:3:0x801c F -nan OK 1548961200 18000 1 -9:3:0x801c F 38.151833 OK 1551380400 18000 2 +9:3:0x801c:0 F -nan OK 1546282800 18000 +9:3:0x801c:1 F -nan OK 1548961200 18000 +9:3:0x801c:2 F 38.151833 OK 1551380400 18000 ... ``` +Для месячных, суточных, часовых архивов метка времени относится к началу архива. + +Для интервальных архивов метка времени относится к концу архива. + +Более подробно см. [Т10.06.59РД-Д1](https://kreit.ru/files/prot_d1.pdf) стр. +31-33 ### Синхронизация времени diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index cd1910d..acd6bb5 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -20,8 +20,7 @@ pipelines: - step: name: build-x86_64-linux script: - - echo "Skip" - - apt update -y && apt install -y ninja-build cmake git gcc g++ + - apt update -y && apt install -y ninja-build cmake git gcc - mkdir build - cd build @@ -32,8 +31,7 @@ pipelines: - step: name: build-x86_64-linux-memsan script: - - echo "Skip" - - apt update -y && apt install -y ninja-build cmake git gcc g++ + - apt update -y && apt install -y ninja-build cmake git gcc - mkdir build - cd build @@ -41,11 +39,22 @@ pipelines: - ninja - ninja test + - step: + name: build-x86_64-linux-valgrind + script: + - apt update -y && apt install -y ninja-build cmake git gcc valgrind + + - mkdir build + - cd build + - cmake -G "Ninja" -DCMAKE_C_FLAGS="-g -O0" .. + - ninja + + - find -executable -type f -name 'unit_*' -exec valgrind '{}' \; + - step: name: build-x86_64-linux-coverage script: - - echo "Skip" - - apt update -y && apt install -y ninja-build cmake git gcc g++ gcovr curl + - apt update -y && apt install -y ninja-build cmake git gcc gcovr curl - mkdir build - cd build @@ -63,32 +72,26 @@ pipelines: - step: name: build-armhf-linux script: - - apt update -y && apt install -y ninja-build cmake git gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf + - apt update -y && apt install -y ninja-build cmake git gcc-arm-linux-gnueabihf qemu - mkdir build - cd build - - cmake -G "Ninja" -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc -DCMAKE_FIND_ROOT_PATH=/usr/arm-linux-gnueabihf -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS_RELEASE="-O3 -DNDEBUG -Wall -Wextra -Wno-unused-parameter -std=c99" -DCMAKE_FIND_ROOT_PATH=/tmp/output .. + - cmake -G "Ninja" -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS_RELEASE="-O3 -DNDEBUG -Wall -Wextra -Wno-unused-parameter -std=c99" .. - ninja + - find -executable -type f -name 'unit_*' -exec qemu-arm -L /usr/arm-linux-gnueabihf '{}' \; + - step: name: build-x86_64-mingw script: - - apt update && apt install -y ninja-build cmake git gcc-mingw-w64-x86-64 g++-mingw-w64-x86-64 build-essential autoconf automake libtool + - apt update && apt install -y ninja-build cmake git gcc-mingw-w64-x86-64 build-essential autoconf automake libtool wine - mkdir build - cd build - cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -DCMAKE_LINKER=/usr/x86_64-w64-mingw32/bin/ld -DCMAKE_C_STANDARD_LIBRARIES="-lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -lws2_32 -liphlpapi -lpsapi -luserenv" -DCMAKE_C_FLAGS_RELEASE="-O3 -DNDEBUG -Wall -Wextra -Wno-unused-parameter -std=c99 -DWINVER=0x0501" -DCMAKE_EXE_LINKER_FLAGS="-Wl,-Bstatic -lwinpthread" .. - ninja - - step: - name: build-i686-mingw - script: - - apt update && apt install -y ninja-build cmake git gcc-mingw-w64-i686 g++-mingw-w64-i686 build-essential autoconf automake libtool - - - mkdir build - - cd build - - cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_C_COMPILER=i686-w64-mingw32-gcc -DCMAKE_LINKER=/usr/i686-w64-mingw32/bin/ld -DCMAKE_C_STANDARD_LIBRARIES="-lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -lws2_32 -liphlpapi -lpsapi -luserenv" -DCMAKE_C_FLAGS_RELEASE="-O3 -DNDEBUG -Wall -Wextra -Wno-unused-parameter -std=c99 -DWINVER=0x0501" -DCMAKE_EXE_LINKER_FLAGS="-Wl,-Bstatic -lwinpthread" .. - - ninja + - find -executable -type f -name 'unit_*' -exec wine '{}' \; options: docker: true diff --git a/tekon/message.c b/tekon/message.c index 49273e7..1d8eaf4 100644 --- a/tekon/message.c +++ b/tekon/message.c @@ -9,7 +9,6 @@ extern "C" { #include "tekon/message.h" #include -#include #include int tekon_req_11(struct message * self, uint8_t gateway, uint8_t device, uint16_t address) diff --git a/tekon/pack.c b/tekon/pack.c index ac1442f..c7b009f 100644 --- a/tekon/pack.c +++ b/tekon/pack.c @@ -10,7 +10,6 @@ extern "C" { #include "tekon/pack.h" #include #include -#include struct buffer_writer { size_t avail; diff --git a/test/suite_arch.sh b/test/suite_arch.sh index 32493c3..c40e031 100755 --- a/test/suite_arch.sh +++ b/test/suite_arch.sh @@ -24,13 +24,13 @@ test_output() # Проверка кол-ва слов WD=$(wc -w ${OUT} | cut -f 1 -d' ') - EXPECT=84 + EXPECT=72 if [ "$WD" -ne "$EXPECT" ]; then fail "Invalid output. Got ${WD} words insted of ${EXPECT}" fi # Проверка по шаблону строки - PAT='2:3:0x801c F 0.000000 COM -1' - grep "${PAT}" /tmp/out > /dev/null && echo "Done" || fail "Invalid output. Can't find ${PAT}" + PAT='2:3:0x801c:([0-9])+ F 0.000000 COM' + grep -E "${PAT}" /tmp/out > /dev/null && echo "Done" || fail "Invalid output. Can't find ${PAT}" } echo "<-- ARCHIVE TEST SUITE -->" diff --git a/test/suite_msr.sh b/test/suite_msr.sh index ddf4b87..47a1d02 100755 --- a/test/suite_msr.sh +++ b/test/suite_msr.sh @@ -29,22 +29,22 @@ test_output() fi # Проверка по шаблону строки - PAT='2:3:0x8001 F 0.000000 COM' + PAT='2:3:0x8001:0 F 0.000000 COM' grep "${PAT}" /tmp/out > /dev/null || fail "Invalid output. Can't find ${PAT}" - PAT='2:3:0x8001 R 0x00000000 COM' + PAT='2:3:0x8001:1 R 0x00000000 COM' grep "${PAT}" /tmp/out > /dev/null || fail "Invalid output. Can't find ${PAT}" - PAT='2:3:0x8001 U 0 COM' + PAT='2:3:100:0 U 0 COM' grep "${PAT}" /tmp/out > /dev/null || fail "Invalid output. Can't find ${PAT}" - PAT='2:3:0x8001 D 2000-00-00 COM' + PAT='2:3:0x8001:0 D 2000-00-00 COM' grep "${PAT}" /tmp/out > /dev/null || fail "Invalid output. Can't find ${PAT}" - PAT='2:3:0x8001 T 00:00:00 COM' + PAT='2:3:0x8001:0 T 00:00:00 COM' grep "${PAT}" /tmp/out > /dev/null || fail "Invalid output. Can't find ${PAT}" - PAT='2:3:0x8001 H 0x0 COM' + PAT='2:3:0x8001:0 H 0x0 COM' grep "${PAT}" /tmp/out > /dev/null || fail "Invalid output. Can't find ${PAT}" echo "Done" @@ -53,11 +53,11 @@ test_output() echo "<-- MEASURMENT TEST SUITE -->" echo "Binary dir: ${BINDIR}" -${BINDIR}/utils/msr/tekon_msr -a udp:127.0.0.1:59160@2 -p'3:0x8001:0:F 3:0x8001:0:R 3:0x8001:0:U 3:0x8001:0:H 3:0x8001:0:D 3:0x8001:0:T' -t 100 > ${OUT} 2>/dev/null +${BINDIR}/utils/msr/tekon_msr -a udp:127.0.0.1:59160@2 -p'3:0x8001:0:F 3:0x8001:1:R 3:100:0:U 3:0x8001:0:H 3:0x8001:0:D 3:0x8001:0:T' -t 100 > ${OUT} 2>/dev/null test_output "UDP" rm ${OUT} -${BINDIR}/utils/msr/tekon_msr -a tcp:127.0.0.1:59160@2 -p'3:0x8001:0:F 3:0x8001:0:R 3:0x8001:0:U 3:0x8001:0:H 3:0x8001:0:D 3:0x8001:0:T' -t 100 > ${OUT} 2>/dev/null +${BINDIR}/utils/msr/tekon_msr -a tcp:127.0.0.1:59160@2 -p'3:0x8001:0:F 3:0x8001:1:R 3:100:0:U 3:0x8001:0:H 3:0x8001:0:D 3:0x8001:0:T' -t 100 > ${OUT} 2>/dev/null test_output "TCP" rm ${OUT} diff --git a/utils/arch/main.c b/utils/arch/main.c index 87d4602..8a6cb61 100644 --- a/utils/arch/main.c +++ b/utils/arch/main.c @@ -11,6 +11,7 @@ extern "C" { #include #include #include +#include #include #include @@ -21,6 +22,7 @@ extern "C" { #define APP_NAME "tekon_arch" #define APP_ERR LOG_ERR APP_NAME " : ERR" #define APP_WARN LOG_WARN APP_NAME " : WARN" +#define APP_INFO LOG_INFO APP_NAME " : INFO" struct app { @@ -52,7 +54,7 @@ static void usage() printf(" F - 32-bit float\n"); printf(" U - 32-bit unsigned integer\n"); printf(" H - 32-bit unsigned integer (HEX)\n"); - printf(" B - boolean\n\n"); + printf(" B - boolean\n"); printf(" R - raw\n\n"); printf(" -d date/time addresses in [device:dateaddr:timeaddr] format.\n\n"); printf(" -i interval description in [type:depth:interval] format.\n"); @@ -464,12 +466,17 @@ static void print(struct rec * self, void * data ) // Добавить адрес параметра result = addr->hex ? - snprintf(ptr, remain, "0x%x ", addr->address) : - snprintf(ptr, remain, "%"PRIu16" ", addr->address); + snprintf(ptr, remain, "0x%x:", addr->address) : + snprintf(ptr, remain, "%"PRIu16":", addr->address); assert(result > 0); ptr += result; remain -= result; + // Добавить индекс параметра + result = snprintf(ptr, remain, "%"PRIu16" ", self->index); + assert(result > 0); + ptr += result; + remain -= result; // Добавить тип и значение switch(app->archive.address.type) { @@ -525,17 +532,16 @@ static void print(struct rec * self, void * data ) // Добавить сдвиг часового пояса result = snprintf(ptr, remain, "%"PRIi32" ", app->tzoffset); assert(result>0); - ptr += result; - remain -= result; - - // Добавить индекс - result = snprintf(ptr, remain, "%" PRIu16,self->index); - assert(result > 0); // Вывести printf("%s\n", buffer); } +static void sigint(int sig) +{ + log_print(APP_INFO " : stop\n"); +} + int main(int argc, char * argv[]) { @@ -543,6 +549,8 @@ int main(int argc, char * argv[]) init(&app); + signal(SIGINT, sigint); + //прочитать аргменты командной строки if(!read_args(&app, argc, argv)) { usage(); diff --git a/utils/base/test/unit_tstamp.c b/utils/base/test/unit_tstamp.c index 5f03832..96cb941 100644 --- a/utils/base/test/unit_tstamp.c +++ b/utils/base/test/unit_tstamp.c @@ -66,6 +66,7 @@ MU_TEST(test_month_cv) struct tekon_date date = {.year = 18, .month = 10, .day = 1}; struct tm dt; + memset(&dt, 0, sizeof(dt)); dt.tm_year = date.year + 100; //tm_year: год - 1900 dt.tm_mon = date.month - 2; //tm_mon [0,11] + начало архива на 1 месяц раньше @@ -144,6 +145,7 @@ MU_TEST(test_day_cv) struct tekon_date date = {.year = 18, .month = 5, .day = 9}; struct tm dt; + memset(&dt, 0, sizeof(dt)); dt.tm_year = date.year + 100; //tm_year: год - 1900 dt.tm_mon = date.month - 1; //tm_mon [0,11] @@ -236,6 +238,7 @@ MU_TEST(test_hour_cv) struct tekon_time time = {.hour = 15, .minute = 0, .second = 0}; struct tm dt; + memset(&dt, 0, sizeof(dt)); dt.tm_year = date.year + 100; //tm_year: год - 1900 dt.tm_mon = date.month - 1; //tm_mon [0,11] @@ -290,6 +293,7 @@ MU_TEST(test_interval_cv) struct tekon_time time = {.hour = 15, .minute = 45, .second = 0}; struct tm dt; + memset(&dt, 0, sizeof(dt)); dt.tm_year = date.year + 100; //tm_year: год - 1900 dt.tm_mon = date.month - 1; //tm_mon [0,11] diff --git a/utils/base/types.c b/utils/base/types.c index c13542d..0d45b96 100644 --- a/utils/base/types.c +++ b/utils/base/types.c @@ -89,16 +89,14 @@ int netaddr_from_string(struct netaddr * self, const char * str) // прочитать IP ptr = string_next(ptr); for(cnt = 0; cnt < iplen && !string_is_term(ptr) && *ptr != ':'; cnt++, ptr++) { - buffer[cnt] = *ptr; + result.ip[cnt] = *ptr; } - buffer[cnt] = '\0'; + result.ip[cnt] = '\0'; - if(!validate_ip(buffer)) + if(!validate_ip(result.ip)) return 0; - strncpy(result.ip, buffer, cnt); - // прочитать порт ptr = string_next(ptr); for(cnt = 0; cnt < portlen && !string_is_term(ptr) && *ptr != '@'; cnt++, ptr++) { diff --git a/utils/msr/main.c b/utils/msr/main.c index 0ebd6e5..7ed3c8e 100644 --- a/utils/msr/main.c +++ b/utils/msr/main.c @@ -11,6 +11,7 @@ extern "C" { #include #include #include +#include #include #include @@ -21,6 +22,7 @@ extern "C" { #define APP_NAME "tekon_msr" #define APP_ERR LOG_ERR APP_NAME " : ERR" #define APP_WARN LOG_WARN APP_NAME " : WARN" +#define APP_INFO LOG_INFO APP_NAME " : INFO" struct app { @@ -54,15 +56,15 @@ static void usage() { printf("Usage: %s -a address -p parameters [-t timeout] [-v verbosity]\n\n", APP_NAME); printf(" -a gateway's address in [type:ip:port@gateway] format.\n\n"); - printf(" -p list of parameters in device:parameter:index:type format.\n"); + printf(" -p list of parameters in [device:parameter:index:type] format.\n"); printf(" type: \n"); printf(" F - 32-bit float\n"); printf(" U - 32-bit unsigned integer\n"); printf(" H - 32-bit unsigned integer (HEX)\n"); - printf(" B - boolean\n\n"); - printf(" R - raw\n\n"); + printf(" B - boolean\n"); + printf(" R - raw\n"); printf(" D - date\n"); - printf(" T - time\n"); + printf(" T - time\n\n"); printf(" -t response timeout in milliseconds.\n\n"); printf(" -v set verbose:\n"); printf(" 0 - silent \n"); @@ -318,12 +320,19 @@ void print(struct msr * self, void * data) // Добавить адрес параметра result = self->hex ? - snprintf(ptr, remain, "0x%x ", self->address) : - snprintf(ptr, remain, "%"PRIu16" ", self->address); + snprintf(ptr, remain, "0x%x:", self->address) : + snprintf(ptr, remain, "%"PRIu16":", self->address); assert(result > 0); ptr += result; remain -= result; + // Добавить индекс параметра + result = snprintf(ptr, remain, "%"PRIu16" ", self->index); + assert(result > 0); + ptr += result; + remain -= result; + + // Добавить значение switch(self->type) { case TEKON_PARAM_RAW: @@ -391,12 +400,20 @@ void print(struct msr * self, void * data) printf("%s\n", buffer); } +static void sigint(int sig) +{ + log_print(APP_INFO " : stop\n"); +} + int main(int argc, char * argv[]) { struct app app; + init(&app); + signal(SIGINT, sigint); + if(!read_args(&app, argc, argv)) { usage(); return 1; diff --git a/utils/sync/check.c b/utils/sync/check.c index 25a01f2..47e8191 100644 --- a/utils/sync/check.c +++ b/utils/sync/check.c @@ -124,7 +124,7 @@ int checks_from_string(struct checks * self, const char * str) if(arglen == buflen) return 0; - strncpy(buffer, str, buflen); + strncpy(buffer, str, buflen - 1); char * ptr = buffer; struct checks result; @@ -138,16 +138,18 @@ int checks_from_string(struct checks * self, const char * str) else break; } else if(strncmp(token, "difference", 10) == 0) { - strtok_r(token, ":", &saveptr); - result.diff = atoi(saveptr); + char * value = NULL; + strtok_r(token, ":", &value); + result.diff = atoi(value); if(result.diff <= 0) return 0; result.avail |= TIME_CHECK_DIFF; } else if(strncmp(token, "minutes", 7) == 0) { - strtok_r(token, ":", &saveptr); - result.minutes = atoi(saveptr); + char * value = NULL; + strtok_r(token, ":", &value); + result.minutes = atoi(value); if(result.minutes <= 0) return 0; @@ -184,7 +186,7 @@ int checks_run(struct checks * self, const struct tm * newtime, const struct tm if(result != 0) { self->fail |= TIME_CHECK_DIFF; snprintf(self->message, sizeof(self->message), - "Difference check failed"); + "difference check failed"); return 0; } } @@ -193,7 +195,7 @@ int checks_run(struct checks * self, const struct tm * newtime, const struct tm if(result != 0) { self->fail |= TIME_CHECK_MINUTES; snprintf(self->message, sizeof(self->message), - "Minutes check failed"); + "minutes check failed"); return 0; } } @@ -202,7 +204,7 @@ int checks_run(struct checks * self, const struct tm * newtime, const struct tm if(result != 0) { self->fail |= TIME_CHECK_INDEX; snprintf(self->message, sizeof(self->message), - "Indexes check failed"); + "indexes check failed"); return 0; } } diff --git a/utils/sync/main.c b/utils/sync/main.c index 9865510..e76942d 100644 --- a/utils/sync/main.c +++ b/utils/sync/main.c @@ -11,6 +11,7 @@ extern "C" { #include #include #include +#include #include #include @@ -22,8 +23,6 @@ extern "C" { #define APP_ERR LOG_ERR APP_NAME " : ERR" #define APP_WARN LOG_WARN APP_NAME " : WARN" #define APP_INFO LOG_WARN APP_NAME " : INFO" -#define APP_MAX_DIFF 60*1000 -#define APP_DEF_DIFF 20*1000 struct app { struct netaddr netcfg; @@ -289,7 +288,7 @@ static int sync(struct app * app) return 0; } - log_print(APP_INFO " : device UTC %ld\n", time_utc_from_local(&devtime)); + log_print(APP_INFO " : device UTC %"PRIi64"\n", time_utc_from_local(&devtime)); log_print(APP_INFO " : device time %d-%02d-%02d %02d:%02d:%02d\n", devtime.tm_year + 1900, devtime.tm_mon + 1, @@ -298,7 +297,7 @@ static int sync(struct app * app) devtime.tm_min, devtime.tm_sec); - log_print(APP_INFO " : new UTC %ld\n", app->newtime); + log_print(APP_INFO " : new UTC %"PRIi64"\n", app->newtime); log_print(APP_INFO " : new time %d-%02d-%02d %02d:%02d:%02d\n", newtime.tm_year + 1900, newtime.tm_mon + 1, @@ -436,12 +435,19 @@ static int read_args(struct app * app, int argc, char * const argv[]) return 1; } +static void sigint(int sig) +{ + log_print(APP_INFO " : stop\n"); +} int main(int argc, char * argv[]) { struct app app; + init(&app); + signal(SIGINT, sigint); + if(!read_args(&app, argc, argv)) { usage(); return 1;