From ae3930920f45fc6f5572caaf6c8afaa54944dabd Mon Sep 17 00:00:00 2001 From: Steve Karg Date: Mon, 20 May 2024 09:13:06 -0500 Subject: [PATCH] Minor code cleanup of BACnetWeeklySchedule (#646) * Minor code cleanup of BACnetWeeklySchedule --- src/bacnet/weeklyschedule.c | 97 +++++++++++++++---------------------- src/bacnet/weeklyschedule.h | 40 +++++---------- 2 files changed, 50 insertions(+), 87 deletions(-) diff --git a/src/bacnet/weeklyschedule.c b/src/bacnet/weeklyschedule.c index 01b469aa..74d09f53 100644 --- a/src/bacnet/weeklyschedule.c +++ b/src/bacnet/weeklyschedule.c @@ -1,37 +1,11 @@ -/*####COPYRIGHTBEGIN#### -------------------------------------------- -Copyright (C) 2022 Steve Karg - -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####*/ - +/** + * @file + * @brief BACnetWeeklySchedule complex data type encode and decode + * @author Ondřej Hruška + * @author Steve Karg + * @date September 2022 + * @copyright SPDX-License-Identifier: GPL-2.0-or-later WITH GCC-exception-2.0 + */ #include #include "bacnet/weeklyschedule.h" #include "bacnet/bacdcode.h" @@ -40,12 +14,12 @@ License. /** * @brief decode a BACnetWeeklySchedule * @param apdu - buffer of data to be decoded - * @param max_apdu_len - number of bytes in the buffer + * @param apdu_size - number of bytes in the buffer * @param value - stores the decoded property value * @return number of bytes decoded, or BACNET_STATUS_ERROR if errors occur */ int bacnet_weeklyschedule_decode( - uint8_t *apdu, int max_apdu_len, BACNET_WEEKLY_SCHEDULE *value) + uint8_t *apdu, int apdu_size, BACNET_WEEKLY_SCHEDULE *value) { int len = 0; int apdu_len = 0; @@ -54,13 +28,14 @@ int bacnet_weeklyschedule_decode( if (!apdu) { return BACNET_STATUS_ERROR; } - if (max_apdu_len <= 0) { + if (apdu_size <= 0) { return BACNET_STATUS_ERROR; } value->singleDay = false; for (wi = 0; wi < 7; wi++) { - len = bacnet_dailyschedule_context_decode(&apdu[apdu_len], - max_apdu_len - apdu_len, 0, &value->weeklySchedule[wi]); + len = bacnet_dailyschedule_context_decode( + &apdu[apdu_len], apdu_size - apdu_len, 0, + &value->weeklySchedule[wi]); if (len < 0) { if (wi == 1) { value->singleDay = true; @@ -82,26 +57,25 @@ int bacnet_weeklyschedule_decode( * @param apdu - buffer of data to be encoded, or NULL for length * @param tag_number - context tag number to be encoded * @param value - value to be encoded - * @return the number of apdu bytes encoded, or BACNET_STATUS_ERROR + * @return the number of apdu bytes encoded, or BACNET_STATUS_ERROR if value + * was inconsistent. */ 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_context_encode( - apdu_offset, 0, &value->weeklySchedule[wi]); + apdu, 0, &value->weeklySchedule[wi]); if (len < 0) { return BACNET_STATUS_ERROR; } apdu_len += len; + if (apdu) { + apdu += len; + } } return apdu_len; @@ -119,24 +93,22 @@ int bacnet_weeklyschedule_context_encode( { 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); + len = encode_opening_tag(apdu, tag_number); apdu_len += len; if (apdu) { - apdu_offset = &apdu[apdu_len]; + apdu += len; } - len = bacnet_weeklyschedule_encode(apdu_offset, value); + len = bacnet_weeklyschedule_encode(apdu, value); if (len == BACNET_STATUS_ERROR) { return 0; } apdu_len += len; if (apdu) { - apdu_offset = &apdu[apdu_len]; + apdu += len; } - len = encode_closing_tag(apdu_offset, tag_number); + len = encode_closing_tag(apdu, tag_number); apdu_len += len; } @@ -146,34 +118,41 @@ int bacnet_weeklyschedule_context_encode( /** * @brief decode a context encoded Weekly_Schedule property * @param apdu - buffer of data to be decoded - * @param max_apdu_len - number of bytes in the buffer + * @param apdu_size - number of bytes in the buffer * @param tag_number - context tag number to match * @param value - stores the decoded property value * @return number of bytes decoded, or BACNET_STATUS_ERROR if an error occurs */ -int bacnet_weeklyschedule_context_decode(uint8_t *apdu, - int max_apdu_len, +int bacnet_weeklyschedule_context_decode( + uint8_t *apdu, + int apdu_size, uint8_t tag_number, BACNET_WEEKLY_SCHEDULE *value) { int apdu_len = 0; int len; + if (!apdu) { + return BACNET_STATUS_ERROR; + } + if (apdu_size <= 0) { + return BACNET_STATUS_ERROR; + } if (bacnet_is_opening_tag_number( - &apdu[apdu_len], max_apdu_len - apdu_len, tag_number, &len)) { + &apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) { apdu_len += len; } else { return BACNET_STATUS_ERROR; } len = bacnet_weeklyschedule_decode( - &apdu[apdu_len], max_apdu_len - apdu_len, value); + &apdu[apdu_len], apdu_size - apdu_len, value); if (len > 0) { apdu_len += len; } else { return BACNET_STATUS_ERROR; } if (bacnet_is_closing_tag_number( - &apdu[apdu_len], max_apdu_len - apdu_len, tag_number, &len)) { + &apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) { apdu_len += len; } else { return BACNET_STATUS_ERROR; diff --git a/src/bacnet/weeklyschedule.h b/src/bacnet/weeklyschedule.h index 75c5fa30..7de58b11 100644 --- a/src/bacnet/weeklyschedule.h +++ b/src/bacnet/weeklyschedule.h @@ -1,28 +1,12 @@ -/************************************************************************** - * - * Copyright (C) 2012 Steve Karg - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - *********************************************************************/ -#ifndef WEEKLYSCHEDULE_H -#define WEEKLYSCHEDULE_H +/** + * @file + * @brief BACnetWeeklySchedule complex data type encode and decode + * @author Ondřej Hruška + * @date September 2022 + * @copyright SPDX-License-Identifier: MIT + */ +#ifndef BACNET_WEEKLY_SCHEDULE_H +#define BACNET_WEEKLY_SCHEDULE_H #include #include @@ -45,7 +29,7 @@ extern "C" { BACNET_STACK_EXPORT int bacnet_weeklyschedule_decode( uint8_t * apdu, - int max_apdu_len, + int apdu_size, BACNET_WEEKLY_SCHEDULE * value); /** Encode WeeklySchedule */ @@ -60,7 +44,7 @@ extern "C" { BACNET_STACK_EXPORT int bacnet_weeklyschedule_context_decode( - uint8_t *apdu, int max_apdu_len, uint8_t tag_number, + uint8_t *apdu, int apdu_size, uint8_t tag_number, BACNET_WEEKLY_SCHEDULE *value); BACNET_STACK_EXPORT @@ -70,4 +54,4 @@ extern "C" { #ifdef __cplusplus } #endif /* __cplusplus */ -#endif /* WEEKLYSCHEDULE_H */ +#endif