From de4abea3c505710c3585dbf113762dcc5ab0774f Mon Sep 17 00:00:00 2001 From: skarg Date: Tue, 21 Feb 2006 20:22:18 +0000 Subject: [PATCH] Added BACnet UTC Time Sync service encoding and decoding. --- bacnet-stack/timesync.c | 65 +++++++++++++++++++++++++++++++++++++---- bacnet-stack/timesync.h | 35 ++++++++++++---------- 2 files changed, 79 insertions(+), 21 deletions(-) diff --git a/bacnet-stack/timesync.c b/bacnet-stack/timesync.c index 714c420d..ddf958a4 100644 --- a/bacnet-stack/timesync.c +++ b/bacnet-stack/timesync.c @@ -38,9 +38,9 @@ #include "bacapp.h" #include "timesync.h" -/* encode service - use -1 for limit for unlimited */ - -int timesync_encode_apdu(uint8_t * apdu, +/* encode service */ +int timesync_encode_apdu_service(uint8_t * apdu, + BACNET_UNCONFIRMED_SERVICE service, BACNET_DATE *my_date, BACNET_TIME *my_time) { @@ -49,7 +49,7 @@ int timesync_encode_apdu(uint8_t * apdu, if (apdu && my_date && my_time) { apdu[0] = PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST; - apdu[1] = SERVICE_UNCONFIRMED_TIME_SYNCHRONIZATION; + apdu[1] = service; apdu_len = 2; len = encode_tagged_date(&apdu[apdu_len], my_date); apdu_len += len; @@ -60,6 +60,26 @@ int timesync_encode_apdu(uint8_t * apdu, return apdu_len; } +int timesync_utc_encode_apdu(uint8_t * apdu, + BACNET_DATE *my_date, + BACNET_TIME *my_time) +{ + return timesync_encode_apdu_service(apdu, + SERVICE_UNCONFIRMED_UTC_TIME_SYNCHRONIZATION, + my_date, + my_time); +} + +int timesync_encode_apdu(uint8_t * apdu, + BACNET_DATE *my_date, + BACNET_TIME *my_time) +{ + return timesync_encode_apdu_service(apdu, + SERVICE_UNCONFIRMED_TIME_SYNCHRONIZATION, + my_date, + my_time); +} + /* decode the service request only */ int timesync_decode_service_request(uint8_t * apdu, unsigned apdu_len, @@ -92,7 +112,8 @@ int timesync_decode_service_request(uint8_t * apdu, return len; } -int timesync_decode_apdu(uint8_t * apdu, +int timesync_decode_apdu_service(uint8_t * apdu, + BACNET_UNCONFIRMED_SERVICE service, unsigned apdu_len, BACNET_DATE *my_date, BACNET_TIME *my_time) @@ -104,7 +125,7 @@ int timesync_decode_apdu(uint8_t * apdu, /* optional checking - most likely was already done prior to this call */ if (apdu[0] != PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST) return -1; - if (apdu[1] != SERVICE_UNCONFIRMED_TIME_SYNCHRONIZATION) + if (apdu[1] != service) return -1; /* optional limits - must be used as a pair */ if (apdu_len > 2) { @@ -115,6 +136,30 @@ int timesync_decode_apdu(uint8_t * apdu, return len; } +int timesync_utc_decode_apdu(uint8_t * apdu, + unsigned apdu_len, + BACNET_DATE *my_date, + BACNET_TIME *my_time) +{ + return timesync_decode_apdu_service(apdu, + SERVICE_UNCONFIRMED_UTC_TIME_SYNCHRONIZATION, + apdu_len, + my_date, + my_time); +} + +int timesync_decode_apdu(uint8_t * apdu, + unsigned apdu_len, + BACNET_DATE *my_date, + BACNET_TIME *my_time) +{ + return timesync_decode_apdu_service(apdu, + SERVICE_UNCONFIRMED_TIME_SYNCHRONIZATION, + apdu_len, + my_date, + my_time); +} + #ifdef TEST #include #include @@ -137,6 +182,14 @@ void testTimeSyncData(Test * pTest, ct_test(pTest, len != -1); ct_test(pTest, bacapp_same_time(my_time, &test_time)); ct_test(pTest, bacapp_same_date(my_date, &test_date)); + + len = timesync_utc_encode_apdu(&apdu[0], my_date, my_time); + ct_test(pTest, len != 0); + apdu_len = len; + len = timesync_utc_decode_apdu(&apdu[0], apdu_len, &test_date, &test_time); + ct_test(pTest, len != -1); + ct_test(pTest, bacapp_same_time(my_time, &test_time)); + ct_test(pTest, bacapp_same_date(my_date, &test_date)); } void testTimeSync(Test * pTest) diff --git a/bacnet-stack/timesync.h b/bacnet-stack/timesync.h index 415e65f4..334aca7e 100644 --- a/bacnet-stack/timesync.h +++ b/bacnet-stack/timesync.h @@ -31,37 +31,42 @@ License. ------------------------------------------- ####COPYRIGHTEND####*/ -#ifndef REINITIALIZE_DEVICE_H -#define REINITIALIZE_DEVICE_H +#ifndef TIMESYNC_H +#define TIMESYNC_H #include #include +#include "bacdef.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ /* encode service */ - int rd_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, - BACNET_REINITIALIZED_STATE state, - BACNET_CHARACTER_STRING * password); - + int timesync_utc_encode_apdu(uint8_t * apdu, + BACNET_DATE *my_date, + BACNET_TIME *my_time); + int timesync_encode_apdu(uint8_t * apdu, + BACNET_DATE *my_date, + BACNET_TIME *my_time); /* decode the service request only */ - int rd_decode_service_request(uint8_t * apdu, + int timesync_decode_service_request(uint8_t * apdu, unsigned apdu_len, - BACNET_REINITIALIZED_STATE * state, - BACNET_CHARACTER_STRING * password); + BACNET_DATE *my_date, + BACNET_TIME *my_time); - int rd_decode_apdu(uint8_t * apdu, + int timesync_utc_decode_apdu(uint8_t * apdu, unsigned apdu_len, - uint8_t * invoke_id, - BACNET_REINITIALIZED_STATE * state, - BACNET_CHARACTER_STRING * password); + BACNET_DATE *my_date, + BACNET_TIME *my_time); + int timesync_decode_apdu(uint8_t * apdu, + unsigned apdu_len, + BACNET_DATE *my_date, + BACNET_TIME *my_time); #ifdef TEST #include "ctest.h" - void test_ReinitializeDevice(Test * pTest); + void testTimeSync(Test * pTest); #endif #ifdef __cplusplus