From 9c72572692acc25662025961d89d30d7278c8d84 Mon Sep 17 00:00:00 2001 From: Steve Karg Date: Thu, 20 Jan 2022 16:42:47 -0600 Subject: [PATCH] Feature/bacnet time sync override (#215) * add --time and --date options to command line time sync for override * remove PRINT_ENABLED compile overreach * fix bacstr.c for non-PRINT_ENABLED compile Co-authored-by: Steve Karg --- apps/timesync/main.c | 19 ++++++++++++++++++- src/bacnet/bacapp.c | 4 ---- src/bacnet/bacstr.c | 12 +++--------- src/bacnet/bacstr.h | 2 -- src/bacnet/datetime.c | 4 ---- src/bacnet/timestamp.c | 2 -- 6 files changed, 21 insertions(+), 22 deletions(-) diff --git a/apps/timesync/main.c b/apps/timesync/main.c index e772077e..2cadaff3 100644 --- a/apps/timesync/main.c +++ b/apps/timesync/main.c @@ -156,6 +156,8 @@ int main(int argc, char *argv[]) const unsigned timeout = 100; /* milliseconds */ BACNET_DATE bdate; BACNET_TIME btime; + bool override_date = false; + bool override_time = false; struct mstimer apdu_timer; long dnet = -1; BACNET_MAC_ADDRESS mac = { 0 }; @@ -204,6 +206,20 @@ int main(int argc, char *argv[]) } } } + if (strcmp(argv[argi], "--date") == 0) { + if (++argi < argc) { + if (datetime_date_init_ascii(&bdate, argv[argi])) { + override_date = true; + } + } + } + if (strcmp(argv[argi], "--time") == 0) { + if (++argi < argc) { + if (datetime_time_init_ascii(&btime, argv[argi])) { + override_time = true; + } + } + } } if (global_broadcast) { datalink_get_broadcast_address(&dest); @@ -244,7 +260,8 @@ int main(int argc, char *argv[]) atexit(datalink_cleanup); mstimer_init(); /* send the request */ - datetime_local(&bdate, &btime, NULL, NULL); + datetime_local(override_date?NULL:&bdate,override_time?NULL:&btime, + NULL, NULL); Send_TimeSync_Remote(&dest, &bdate, &btime); mstimer_set(&apdu_timer, apdu_timeout()); /* loop forever - not necessary for time sync, but we can watch */ diff --git a/src/bacnet/bacapp.c b/src/bacnet/bacapp.c index 2d1fa0a9..2db1a938 100644 --- a/src/bacnet/bacapp.c +++ b/src/bacnet/bacapp.c @@ -1543,10 +1543,8 @@ bool bacapp_parse_application_data(BACNET_APPLICATION_TAG tag_number, #endif #if defined(BACAPP_OCTET_STRING) case BACNET_APPLICATION_TAG_OCTET_STRING: -#if PRINT_ENABLED /* Apparently ain't necessarily so. */ status = octetstring_init_ascii_hex(&value->type.Octet_String, argv); -#endif break; #endif #if defined(BACAPP_CHARACTER_STRING) @@ -1557,9 +1555,7 @@ bool bacapp_parse_application_data(BACNET_APPLICATION_TAG tag_number, #endif #if defined(BACAPP_BIT_STRING) case BACNET_APPLICATION_TAG_BIT_STRING: -#if PRINT_ENABLED status = bitstring_init_ascii(&value->type.Bit_String, argv); -#endif break; #endif #if defined(BACAPP_ENUMERATED) diff --git a/src/bacnet/bacstr.c b/src/bacnet/bacstr.c index 7f96826c..b04a75d7 100644 --- a/src/bacnet/bacstr.c +++ b/src/bacnet/bacstr.c @@ -34,14 +34,12 @@ #include #include -#include /* for strlen */ +#include +#include +#include #include "bacnet/config.h" #include "bacnet/bacstr.h" #include "bacnet/bits.h" -#if PRINT_ENABLED -#include /* for strtol */ -#include /* for isalnum */ -#endif /* TODO: For some reason my Zephyr build for non-native targets does not * see a definition for strnlen(), but it is visible in when @@ -319,7 +317,6 @@ bool bitstring_same( return false; } -#if PRINT_ENABLED /** * Converts an null terminated ASCII string to an bitstring. * @@ -371,7 +368,6 @@ bool bitstring_init_ascii(BACNET_BIT_STRING *bit_string, const char *ascii) return status; } -#endif #define CHARACTER_STRING_CAPACITY (MAX_CHARACTER_STRING_BYTES - 1) /** @@ -981,7 +977,6 @@ bool octetstring_init( return status; } -#if PRINT_ENABLED /** @brief Converts an null terminated ASCII Hex string to an octet string. * * @param octet_string Pointer to the octed string. @@ -1031,7 +1026,6 @@ bool octetstring_init_ascii_hex( return status; } -#endif /** * Copy an octed string from source to destination. diff --git a/src/bacnet/bacstr.h b/src/bacnet/bacstr.h index 7a5de136..376eaee3 100644 --- a/src/bacnet/bacstr.h +++ b/src/bacnet/bacstr.h @@ -190,14 +190,12 @@ extern "C" { BACNET_OCTET_STRING * octet_string, uint8_t * value, size_t length); -#ifdef PRINT_ENABLED /* converts an null terminated ASCII Hex string to an octet string. returns true if successfully converted and fits; false if too long */ BACNET_STACK_EXPORT bool octetstring_init_ascii_hex( BACNET_OCTET_STRING * octet_string, const char *ascii_hex); -#endif BACNET_STACK_EXPORT bool octetstring_copy( BACNET_OCTET_STRING * dest, diff --git a/src/bacnet/datetime.c b/src/bacnet/datetime.c index f8cea54b..e00eb6fb 100644 --- a/src/bacnet/datetime.c +++ b/src/bacnet/datetime.c @@ -1113,7 +1113,6 @@ int bacapp_decode_context_datetime( return apdu_len; } -#if PRINT_ENABLED /** * @brief Parse an ascii string for the date 2021/12/31 or 2021/12/31:1 * @param bdate - #BACNET_DATE structure @@ -1141,9 +1140,7 @@ bool datetime_date_init_ascii(BACNET_DATE *bdate, const char *ascii) return status; } -#endif -#if PRINT_ENABLED /** * @brief Parse an ascii string for the time formatted 23:59:59.99 * @param btime - #BACNET_TIME structure @@ -1180,7 +1177,6 @@ bool datetime_time_init_ascii(BACNET_TIME *btime, const char *ascii) return status; } -#endif #ifdef BAC_TEST #include diff --git a/src/bacnet/timestamp.c b/src/bacnet/timestamp.c index 2283bfe5..549a4ee0 100644 --- a/src/bacnet/timestamp.c +++ b/src/bacnet/timestamp.c @@ -266,7 +266,6 @@ int bacapp_decode_context_timestamp( return len; } -#if PRINT_ENABLED /** * @brief Parse an ascii string for the timestamp * @param btime - #BACNET_TIME structure @@ -347,7 +346,6 @@ bool bacapp_timestamp_init_ascii(BACNET_TIMESTAMP *timestamp, const char *ascii) return status; } -#endif #ifdef BAC_TEST