void Send_TimeSync( BACNET_DATE * bdate, BACNET_TIME * btime) { int len = 0; int pdu_len = 0; BACNET_ADDRESS dest; int bytes_sent = 0; BACNET_NPDU_DATA npdu_data; if (!dcc_communication_enabled()) return; /* we could use unicast or broadcast */ datalink_get_broadcast_address(&dest); /* encode the NPDU portion of the packet */ npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); pdu_len = npdu_encode_pdu(&Handler_Transmit_Buffer[0], &dest, NULL, &npdu_data); /* encode the APDU portion of the packet */ len = timesync_encode_apdu(&Handler_Transmit_Buffer[pdu_len], bdate, btime); pdu_len += len; /* send it out the datalink */ bytes_sent = datalink_send_pdu(&dest, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); #if PRINT_ENABLED if (bytes_sent <= 0) fprintf(stderr, "Failed to Send Time-Synchronization Request (%s)!\n", strerror(errno)); #endif }
void testTimeSyncData( Test * pTest, BACNET_DATE * my_date, BACNET_TIME * my_time) { uint8_t apdu[480] = { 0 }; int len = 0; int apdu_len = 0; BACNET_DATE test_date; BACNET_TIME test_time; len = timesync_encode_apdu(&apdu[0], my_date, my_time); ct_test(pTest, len != 0); apdu_len = len; len = timesync_decode_apdu(&apdu[0], apdu_len, &test_date, &test_time); ct_test(pTest, len != -1); ct_test(pTest, datetime_compare_time(my_time, &test_time) == 0); ct_test(pTest, datetime_compare_date(my_date, &test_date) == 0); 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, datetime_compare_time(my_time, &test_time) == 0); ct_test(pTest, datetime_compare_date(my_date, &test_date) == 0); }
int BacnetTimeSync( int deviceInstanceNumber, int year, int month, int day, int hour, int minute, int second, int isUTC, int UTCOffset) { BACNET_DATE bdate; BACNET_TIME btime; struct tm my_time; time_t aTime; struct tm *newTime; my_time.tm_sec = second; my_time.tm_min = minute; my_time.tm_hour = hour; my_time.tm_mday = day; my_time.tm_mon = month - 1; my_time.tm_year = year - 1900; my_time.tm_wday = 0; /* does not matter */ my_time.tm_yday = 0; /* does not matter */ my_time.tm_isdst = 0; /* does not matter */ aTime = mktime(&my_time); newTime = localtime(&aTime); bdate.year = newTime->tm_year; bdate.month = newTime->tm_mon + 1; bdate.day = newTime->tm_mday; bdate.wday = newTime->tm_wday ? newTime->tm_wday : 7; btime.hour = newTime->tm_hour; btime.min = newTime->tm_min; btime.sec = newTime->tm_sec; btime.hundredths = 0; int len = 0; int pdu_len = 0; int bytes_sent = 0; BACNET_NPDU_DATA npdu_data; BACNET_ADDRESS my_address; uint8_t Handler_Transmit_Buffer[MAX_PDU] = { 0 }; /* Loop for eary exit */ do { if (!dcc_communication_enabled()) { LogError("DCC communicaiton is not enabled"); break; } /* encode the NPDU portion of the packet */ npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); datalink_get_my_address(&my_address); pdu_len = npdu_encode_pdu(&Handler_Transmit_Buffer[0], &Target_Address, &my_address, &npdu_data); /* encode the APDU portion of the packet */ len = timesync_encode_apdu(&Handler_Transmit_Buffer[pdu_len], &bdate, &btime); pdu_len += len; /* send it out the datalink */ bytes_sent = datalink_send_pdu(&Target_Address, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); if (bytes_sent <= 0) { char errorMsg[64]; sprintf(errorMsg, "Failed to Send Time-Synchronization Request (%s)!", strerror(errno)); LogError(errorMsg); break; } Wait_For_Answer_Or_Timeout(100, waitAnswer); } while (false); int isFailure = Error_Detected; Error_Detected = 0; return isFailure; }