Schedule encoding/decoding (#319)

* schedule: add decode_daily_schedule() and encode_daily_schedule()

* schedule: encode/decode implemented + add to bacapp

* add safe encode/decode functions for timevalue, schedule function renaming

* fix unit tests build failing

* add IDEA and test temporary files to .gitignore

* try to make "deprecated" work in MSVC

* add WeeklySchedule compare function

* add bacnet_weeklyschedule_context_decode()

* Add basic test for WeeklySchedule

* Fix WeeklySchedule parsing and snprintf, decoder verified with real hardware

* try to fix windows build

* improve boolean parsing in 'bacapp_parse_application_data'

* add parse function for weekly schedule

* allow types > 16 in bacwp, show the decoded value before sending

* add bacapp binaries to gitignore

* remove bacwp logging

* Add error checking to bacapp_parse_application_data

* try to fix windows build

* fix avr build

* Fix error handling in RP Ack

* add singleDay flag

* show day name in single day weeklyschedule snprintf

* show weeklyschedule inner tag in snprintf

* improve weeklyschedule parsing and printing, supports type names now

* add weekly schedule to bacapp_decode_data

* move bacnet/bacnet_plat_compat.h to bacnet/basic/sys/platform.h

* disable tag limit also in bacwpm

* add ifdef's around strtoX helper functions in bacapp

* move strtox to BACAPP_PRINT_ENABLED ifdef in bacapp

* fix stm32 makefiles

* fix at91sam7s build

* use BACNET_UNSIGNED_INTEGER in BACnet_Short_Application_Data_Value

* fix capitalization in BACnet_Daily_Schedule

* add name to BACNET_TIME_VALUE struct

* change bacwp bacwpm to use bacapp_known_property_tag()

* fix some macros in bacdcode missing parentheses

* Remove dummy fields from BACNET_SHORT_APPLICATION_DATA_VALUE, replace remaining uses of upcasting (adds extra overhead but is maybe safer), rename short DV to Primitive

* fix new ci warnings

* more fixes for ancient C

* fix tests no longer building

* primitive value renamed to shorter name
This commit is contained in:
Ondřej Hruška
2022-09-05 16:27:15 +02:00
committed by GitHub
parent bd40c51e19
commit 7cdab61d72
66 changed files with 1800 additions and 108 deletions
+187
View File
@@ -0,0 +1,187 @@
/*####COPYRIGHTBEGIN####
-------------------------------------------
Copyright (C) 2022 Steve Karg <skarg@users.sourceforge.net>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
The Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA.
As a special exception, if other files instantiate templates or
use macros or inline functions from this file, or you compile
this file and link it with other works to produce a work based
on this file, this file does not by itself cause the resulting
work to be covered by the GNU General Public License. However
the source code for this file must still be made available in
accordance with section (3) of the GNU General Public License.
This exception does not invalidate any other reasons why a work
based on this file might be covered by the GNU General Public
License.
-------------------------------------------
####COPYRIGHTEND####*/
#include <stdint.h>
#include "bacnet/weeklyschedule.h"
#include "bacnet/bacdcode.h"
#include "bacapp.h"
int bacnet_weeklyschedule_decode(
uint8_t * apdu,
int max_apdu_len,
BACNET_WEEKLY_SCHEDULE *value)
{
int len = 0;
int apdu_len = 0;
int wi;
value->singleDay = false;
for (wi = 0; wi < 7; wi++) {
len = bacnet_dailyschedule_decode(
&apdu[apdu_len], max_apdu_len - apdu_len, &value->weeklySchedule[wi]);
if (len < 0) {
if (wi == 1) {
value->singleDay = true;
break;
}
return -1;
}
apdu_len += len;
}
return apdu_len;
}
int bacnet_weeklyschedule_encode(
uint8_t * apdu,
BACNET_WEEKLY_SCHEDULE *value)
{
int apdu_len = 0;
int len = 0;
int wi;
uint8_t *apdu_offset = NULL;
for (wi = 0; wi < (value->singleDay ? 1 : 7); wi++) {
/* not enough room to write data */
if (apdu) {
apdu_offset = &apdu[apdu_len];
}
len =
bacnet_dailyschedule_encode(apdu_offset, &value->weeklySchedule[wi]);
if (len < 0)
return -1;
apdu_len += len;
}
return apdu_len;
}
/**
* @brief Encode a context tagged WeeklySchedule complex data type
* @param apdu - the APDU buffer
* @param tag_number - the APDU buffer size
* @param value - WeeklySchedule structure
* @return length of the APDU buffer, or 0 if not able to encode
*/
int bacnet_weeklyschedule_context_encode(
uint8_t *apdu, uint8_t tag_number, BACNET_WEEKLY_SCHEDULE *value)
{
int len = 0;
int apdu_len = 0;
uint8_t *apdu_offset = NULL;
if (value) {
apdu_offset = apdu;
len = encode_opening_tag(apdu_offset, tag_number);
apdu_len += len;
if (apdu) {
apdu_offset = &apdu[apdu_len];
}
len = bacnet_weeklyschedule_encode(apdu_offset, value);
apdu_len += len;
if (apdu) {
apdu_offset = &apdu[apdu_len];
}
len = encode_closing_tag(apdu_offset, tag_number);
apdu_len += len;
}
return apdu_len;
}
int bacnet_weeklyschedule_context_decode(
uint8_t *apdu, int max_apdu_len, uint8_t tag_number,
BACNET_WEEKLY_SCHEDULE *value)
{
int apdu_len = 0;
int len;
if ((max_apdu_len - apdu_len) >= 1 && decode_is_opening_tag_number(&apdu[apdu_len], tag_number)) {
apdu_len += 1;
} else {
return -1;
}
if (-1 == (len = bacnet_weeklyschedule_decode(&apdu[apdu_len], max_apdu_len - apdu_len, value))) {
return -1;
} else {
apdu_len += len;
}
if ((max_apdu_len - apdu_len) >= 1 && decode_is_closing_tag_number(&apdu[apdu_len], tag_number)) {
apdu_len += 1;
} else {
return -1;
}
return apdu_len;
}
/**
* @brief Compare the BACnetWeeklySchedule complex data
* @param value1 - BACNET_COLOR_COMMAND structure
* @param value2 - BACNET_COLOR_COMMAND structure
* @return true if the same
*/
bool bacnet_weeklyschedule_same(
BACNET_WEEKLY_SCHEDULE *value1, BACNET_WEEKLY_SCHEDULE *value2)
{
int wi, ti;
for (wi = 0; wi < 7; wi++) {
BACNET_DAILY_SCHEDULE *ds1 = &value1->weeklySchedule[wi];
BACNET_DAILY_SCHEDULE *ds2 = &value2->weeklySchedule[wi];
if (ds1->TV_Count != ds2->TV_Count) {
return false;
}
for (ti = 0; ti < ds1->TV_Count; ti++) {
BACNET_TIME_VALUE *tv1 = &ds1->Time_Values[ti];
BACNET_TIME_VALUE *tv2 = &ds2->Time_Values[ti];
if (0 != datetime_compare_time(&tv1->Time, &tv2->Time)) {
return false;
}
// TODO the conversion can be avoided by adding a "primitive" variant of bacapp_same_value(),
// at the cost of some code duplication
BACNET_APPLICATION_DATA_VALUE adv1, adv2;
bacnet_primitive_to_application_data_value(&adv1, &tv1->Value);
bacnet_primitive_to_application_data_value(&adv2, &tv2->Value);
if (!bacapp_same_value(&adv1, &adv2))
{
return false;
}
}
}
return true;
}