Minor code cleanup of BACnetWeeklySchedule (#646)
* Minor code cleanup of BACnetWeeklySchedule
This commit is contained in:
+38
-59
@@ -1,37 +1,11 @@
|
|||||||
/*####COPYRIGHTBEGIN####
|
/**
|
||||||
-------------------------------------------
|
* @file
|
||||||
Copyright (C) 2022 Steve Karg <skarg@users.sourceforge.net>
|
* @brief BACnetWeeklySchedule complex data type encode and decode
|
||||||
|
* @author Ondřej Hruška <ondra@ondrovo.com>
|
||||||
This program is free software; you can redistribute it and/or
|
* @author Steve Karg <skarg@users.sourceforge.net>
|
||||||
modify it under the terms of the GNU General Public License
|
* @date September 2022
|
||||||
as published by the Free Software Foundation; either version 2
|
* @copyright SPDX-License-Identifier: GPL-2.0-or-later WITH GCC-exception-2.0
|
||||||
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 <stdint.h>
|
||||||
#include "bacnet/weeklyschedule.h"
|
#include "bacnet/weeklyschedule.h"
|
||||||
#include "bacnet/bacdcode.h"
|
#include "bacnet/bacdcode.h"
|
||||||
@@ -40,12 +14,12 @@ License.
|
|||||||
/**
|
/**
|
||||||
* @brief decode a BACnetWeeklySchedule
|
* @brief decode a BACnetWeeklySchedule
|
||||||
* @param apdu - buffer of data to be decoded
|
* @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
|
* @param value - stores the decoded property value
|
||||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR if errors occur
|
* @return number of bytes decoded, or BACNET_STATUS_ERROR if errors occur
|
||||||
*/
|
*/
|
||||||
int bacnet_weeklyschedule_decode(
|
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 len = 0;
|
||||||
int apdu_len = 0;
|
int apdu_len = 0;
|
||||||
@@ -54,13 +28,14 @@ int bacnet_weeklyschedule_decode(
|
|||||||
if (!apdu) {
|
if (!apdu) {
|
||||||
return BACNET_STATUS_ERROR;
|
return BACNET_STATUS_ERROR;
|
||||||
}
|
}
|
||||||
if (max_apdu_len <= 0) {
|
if (apdu_size <= 0) {
|
||||||
return BACNET_STATUS_ERROR;
|
return BACNET_STATUS_ERROR;
|
||||||
}
|
}
|
||||||
value->singleDay = false;
|
value->singleDay = false;
|
||||||
for (wi = 0; wi < 7; wi++) {
|
for (wi = 0; wi < 7; wi++) {
|
||||||
len = bacnet_dailyschedule_context_decode(&apdu[apdu_len],
|
len = bacnet_dailyschedule_context_decode(
|
||||||
max_apdu_len - apdu_len, 0, &value->weeklySchedule[wi]);
|
&apdu[apdu_len], apdu_size - apdu_len, 0,
|
||||||
|
&value->weeklySchedule[wi]);
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
if (wi == 1) {
|
if (wi == 1) {
|
||||||
value->singleDay = true;
|
value->singleDay = true;
|
||||||
@@ -82,26 +57,25 @@ int bacnet_weeklyschedule_decode(
|
|||||||
* @param apdu - buffer of data to be encoded, or NULL for length
|
* @param apdu - buffer of data to be encoded, or NULL for length
|
||||||
* @param tag_number - context tag number to be encoded
|
* @param tag_number - context tag number to be encoded
|
||||||
* @param value - value 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 bacnet_weeklyschedule_encode(uint8_t *apdu, BACNET_WEEKLY_SCHEDULE *value)
|
||||||
{
|
{
|
||||||
int apdu_len = 0;
|
int apdu_len = 0;
|
||||||
int len = 0;
|
int len = 0;
|
||||||
int wi;
|
int wi;
|
||||||
uint8_t *apdu_offset = NULL;
|
|
||||||
|
|
||||||
for (wi = 0; wi < (value->singleDay ? 1 : 7); wi++) {
|
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(
|
len = bacnet_dailyschedule_context_encode(
|
||||||
apdu_offset, 0, &value->weeklySchedule[wi]);
|
apdu, 0, &value->weeklySchedule[wi]);
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
return BACNET_STATUS_ERROR;
|
return BACNET_STATUS_ERROR;
|
||||||
}
|
}
|
||||||
apdu_len += len;
|
apdu_len += len;
|
||||||
|
if (apdu) {
|
||||||
|
apdu += len;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return apdu_len;
|
return apdu_len;
|
||||||
@@ -119,24 +93,22 @@ int bacnet_weeklyschedule_context_encode(
|
|||||||
{
|
{
|
||||||
int len = 0;
|
int len = 0;
|
||||||
int apdu_len = 0;
|
int apdu_len = 0;
|
||||||
uint8_t *apdu_offset = NULL;
|
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
apdu_offset = apdu;
|
len = encode_opening_tag(apdu, tag_number);
|
||||||
len = encode_opening_tag(apdu_offset, tag_number);
|
|
||||||
apdu_len += len;
|
apdu_len += len;
|
||||||
if (apdu) {
|
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) {
|
if (len == BACNET_STATUS_ERROR) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
apdu_len += len;
|
apdu_len += len;
|
||||||
if (apdu) {
|
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;
|
apdu_len += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,34 +118,41 @@ int bacnet_weeklyschedule_context_encode(
|
|||||||
/**
|
/**
|
||||||
* @brief decode a context encoded Weekly_Schedule property
|
* @brief decode a context encoded Weekly_Schedule property
|
||||||
* @param apdu - buffer of data to be decoded
|
* @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 tag_number - context tag number to match
|
||||||
* @param value - stores the decoded property value
|
* @param value - stores the decoded property value
|
||||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR if an error occurs
|
* @return number of bytes decoded, or BACNET_STATUS_ERROR if an error occurs
|
||||||
*/
|
*/
|
||||||
int bacnet_weeklyschedule_context_decode(uint8_t *apdu,
|
int bacnet_weeklyschedule_context_decode(
|
||||||
int max_apdu_len,
|
uint8_t *apdu,
|
||||||
|
int apdu_size,
|
||||||
uint8_t tag_number,
|
uint8_t tag_number,
|
||||||
BACNET_WEEKLY_SCHEDULE *value)
|
BACNET_WEEKLY_SCHEDULE *value)
|
||||||
{
|
{
|
||||||
int apdu_len = 0;
|
int apdu_len = 0;
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
|
if (!apdu) {
|
||||||
|
return BACNET_STATUS_ERROR;
|
||||||
|
}
|
||||||
|
if (apdu_size <= 0) {
|
||||||
|
return BACNET_STATUS_ERROR;
|
||||||
|
}
|
||||||
if (bacnet_is_opening_tag_number(
|
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;
|
apdu_len += len;
|
||||||
} else {
|
} else {
|
||||||
return BACNET_STATUS_ERROR;
|
return BACNET_STATUS_ERROR;
|
||||||
}
|
}
|
||||||
len = bacnet_weeklyschedule_decode(
|
len = bacnet_weeklyschedule_decode(
|
||||||
&apdu[apdu_len], max_apdu_len - apdu_len, value);
|
&apdu[apdu_len], apdu_size - apdu_len, value);
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
apdu_len += len;
|
apdu_len += len;
|
||||||
} else {
|
} else {
|
||||||
return BACNET_STATUS_ERROR;
|
return BACNET_STATUS_ERROR;
|
||||||
}
|
}
|
||||||
if (bacnet_is_closing_tag_number(
|
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;
|
apdu_len += len;
|
||||||
} else {
|
} else {
|
||||||
return BACNET_STATUS_ERROR;
|
return BACNET_STATUS_ERROR;
|
||||||
|
|||||||
+12
-28
@@ -1,28 +1,12 @@
|
|||||||
/**************************************************************************
|
/**
|
||||||
*
|
* @file
|
||||||
* Copyright (C) 2012 Steve Karg <skarg@users.sourceforge.net>
|
* @brief BACnetWeeklySchedule complex data type encode and decode
|
||||||
*
|
* @author Ondřej Hruška <ondra@ondrovo.com>
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* @date September 2022
|
||||||
* a copy of this software and associated documentation files (the
|
* @copyright SPDX-License-Identifier: MIT
|
||||||
* "Software"), to deal in the Software without restriction, including
|
*/
|
||||||
* without limitation the rights to use, copy, modify, merge, publish,
|
#ifndef BACNET_WEEKLY_SCHEDULE_H
|
||||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
#define BACNET_WEEKLY_SCHEDULE_H
|
||||||
* 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
|
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@@ -45,7 +29,7 @@ extern "C" {
|
|||||||
BACNET_STACK_EXPORT
|
BACNET_STACK_EXPORT
|
||||||
int bacnet_weeklyschedule_decode(
|
int bacnet_weeklyschedule_decode(
|
||||||
uint8_t * apdu,
|
uint8_t * apdu,
|
||||||
int max_apdu_len,
|
int apdu_size,
|
||||||
BACNET_WEEKLY_SCHEDULE * value);
|
BACNET_WEEKLY_SCHEDULE * value);
|
||||||
|
|
||||||
/** Encode WeeklySchedule */
|
/** Encode WeeklySchedule */
|
||||||
@@ -60,7 +44,7 @@ extern "C" {
|
|||||||
|
|
||||||
BACNET_STACK_EXPORT
|
BACNET_STACK_EXPORT
|
||||||
int bacnet_weeklyschedule_context_decode(
|
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_WEEKLY_SCHEDULE *value);
|
||||||
|
|
||||||
BACNET_STACK_EXPORT
|
BACNET_STACK_EXPORT
|
||||||
@@ -70,4 +54,4 @@ extern "C" {
|
|||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif /* __cplusplus */
|
#endif /* __cplusplus */
|
||||||
#endif /* WEEKLYSCHEDULE_H */
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user