Feature/bacnet discover dnet devices (#583)

* Added an example application to discover devices and their objects and properties on a specific destination network. The application uses a BACnet Discovery FSM module along with the BACnet R/W FSM.  The BACnet Discovery module stores the binary property data in Keylists and includes device object property queries and iterators.

* Added callback from BACnet R/W FSM module for I-Am messages.

* Removed dependency in BACnet R/W FSM module on rpm_ack_decode_service_request() which uses calloc/free value lists.  Created an alternate RPM-ACK to RP-ACK processing function.

* Changed RPM handler to skip over unknown property values
This commit is contained in:
Steve Karg
2024-02-28 12:56:09 -06:00
committed by GitHub
parent dfef5208d1
commit 1176b0d966
15 changed files with 1945 additions and 131 deletions
+25
View File
@@ -34,6 +34,11 @@ option(
"compile the bacpoll app"
ON)
option(
BACNET_BUILD_BACDISCOVER_APP
"compile the bacdiscover app"
ON)
option(
BACDL_ETHERNET
"compile with ethernet support"
@@ -70,6 +75,18 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
# Build types
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
message(STATUS "Maximum optimization for speed")
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
message(STATUS "Maximum optimization for speed, debug info included")
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
message(STATUS "Maximum optimization for size")
else ()
message(STATUS "Minimal optimization, debug info included")
add_compile_definitions(DEBUG_ENABLED=1)
endif ()
option(
BACNET_STACK_DEPRECATED_DISABLE
"Disable deprecation compile warnings"
@@ -671,6 +688,14 @@ if(BACNET_STACK_BUILD_APPS)
target_link_libraries(bacpoll PRIVATE ${PROJECT_NAME})
endif(BACNET_BUILD_BACPOLL_APP)
if(BACNET_BUILD_BACDISCOVER_APP)
add_executable(bacdiscover
apps/server-discover/main.c
src/bacnet/basic/client/bac-discover.c
src/bacnet/basic/client/bac-rw.c)
target_link_libraries(bacdiscover PRIVATE ${PROJECT_NAME})
endif(BACNET_BUILD_BACDISCOVER_APP)
if(NOT BACDL_ETHERNET)
add_executable(readbdt apps/readbdt/main.c)
target_link_libraries(readbdt PRIVATE ${PROJECT_NAME})
+8
View File
@@ -157,6 +157,14 @@ netnumis:
server:
$(MAKE) -s -C apps $@
.PHONY: server-client
server-client:
$(MAKE) LEGACY=true -s -C apps $@
.PHONY: server-discover
server-discover:
$(MAKE) LEGACY=true -s -C apps $@
.PHONY: mstpcap
mstpcap:
$(MAKE) -s -C apps $@
+5 -1
View File
@@ -193,7 +193,7 @@ SUBDIRS = lib readprop writeprop readfile writefile reinit server dcc \
whohas whois iam ucov scov timesync epics readpropm readrange \
writepropm uptransfer getevent uevent abort error event ack-alarm \
server-client add-list-element remove-list-element create-object \
delete-object apdu
delete-object server-discover apdu
ifeq (${BACDL_DEFINE},-DBACDL_BIP=1)
SUBDIRS += whoisrouter iamrouter initrouter whatisnetnum netnumis
@@ -374,6 +374,10 @@ server: $(BACNET_LIB_TARGET)
server-client: $(BACNET_LIB_TARGET)
$(MAKE) -B -C $@
.PHONY: server-discover
server-discover: $(BACNET_LIB_TARGET)
$(MAKE) -B -C $@
.PHONY: timesync
timesync: $(BACNET_LIB_TARGET)
$(MAKE) -B -C $@
+45
View File
@@ -0,0 +1,45 @@
#Makefile to build BACnet Application using GCC compiler
# Executable file name - BACnet Server-Discover Application
TARGET = bacdiscover
# BACnet objects that are used with this app
# BACnet objects that are used with this app
BACNET_OBJECT_DIR = $(BACNET_SRC_DIR)/bacnet/basic/object
BACNET_CLIENT_DIR = $(BACNET_SRC_DIR)/bacnet/basic/client
SRC = main.c \
$(BACNET_OBJECT_DIR)/client/device-client.c \
$(BACNET_OBJECT_DIR)/netport.c \
$(BACNET_CLIENT_DIR)/bac-discover.c \
$(BACNET_CLIENT_DIR)/bac-rw.c
# TARGET_EXT is defined in apps/Makefile as .exe or nothing
TARGET_BIN = ${TARGET}$(TARGET_EXT)
OBJS += ${SRC:.c=.o}
all: ${BACNET_LIB_TARGET} Makefile ${TARGET_BIN}
${TARGET_BIN}: ${OBJS} Makefile ${BACNET_LIB_TARGET}
${CC} ${PFLAGS} ${OBJS} ${LFLAGS} -o $@
size $@
cp $@ ../../bin
${BACNET_LIB_TARGET}:
( cd ${BACNET_LIB_DIR} ; $(MAKE) clean ; $(MAKE) -s )
.c.o:
${CC} -c ${CFLAGS} $*.c -o $@
.PHONY: depend
depend:
rm -f .depend
${CC} -MM ${CFLAGS} *.c >> .depend
.PHONY: clean
clean:
rm -f core ${TARGET_BIN} ${OBJS} $(TARGET).map ${BACNET_LIB_TARGET}
.PHONY: include
include: .depend
+291
View File
@@ -0,0 +1,291 @@
/**
* @file
* @author Steve Karg
* @date 2022
* @brief Application to acquire devices and their object list from a BACnet
* network.
*
* SPDX-License-Identifier: MIT
*/
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
/* core library */
#include "bacnet/config.h"
#include "bacnet/apdu.h"
#include "bacnet/bacdef.h"
#include "bacnet/bacdcode.h"
#include "bacnet/bacerror.h"
#include "bacnet/bacstr.h"
#include "bacnet/bactext.h"
#include "bacnet/dcc.h"
#include "bacnet/iam.h"
#include "bacnet/npdu.h"
#include "bacnet/version.h"
#include "bacnet/whois.h"
/* basic services */
#include "bacnet/basic/binding/address.h"
#include "bacnet/basic/client/bac-discover.h"
#include "bacnet/basic/object/device.h"
#include "bacnet/basic/sys/debug.h"
#include "bacnet/basic/sys/filename.h"
#include "bacnet/basic/sys/mstimer.h"
#include "bacnet/basic/services.h"
#include "bacnet/basic/tsm/tsm.h"
#include "bacnet/datalink/datalink.h"
#include "bacnet/datalink/dlenv.h"
#if BACNET_SVC_SERVER
#error "App requires server-only features disabled! Set BACNET_SVC_SERVER=0"
#endif
/* current version of the BACnet stack */
static const char *BACnet_Version = BACNET_VERSION_TEXT;
/** Buffer used for receiving */
static uint8_t Rx_Buf[MAX_MPDU];
/* task timer for various BACnet timeouts */
static struct mstimer BACnet_Task_Timer;
/* task timer for TSM timeouts */
static struct mstimer BACnet_TSM_Timer;
/* task timer for printing data */
static struct mstimer BACnet_Print_Timer;
/* flag to determine if devices or both devices and objects are printed */
static bool Print_Summary = false;
/**
* @brief Print the list of discovered devices and their objects
*/
void print_discovered_devices(void)
{
unsigned int device_index = 0;
unsigned int device_count = 0;
unsigned int object_count = 0;
unsigned int object_index = 0;
unsigned int property_count = 0;
uint32_t device_id = 0;
BACNET_OBJECT_ID object_id = { 0 };
unsigned long milliseconds = 0;
size_t heap_ram = 0;
char model_name[MAX_CHARACTER_STRING_BYTES] = { 0 };
char object_name[MAX_CHARACTER_STRING_BYTES] = { 0 };
device_count = bacnet_discover_device_count();
printf("----list of %u devices ----\n", device_count);
for (device_index = 0; device_index < device_count; device_index++) {
device_id = bacnet_discover_device_instance(device_index);
object_count = bacnet_discover_device_object_count(device_id);
milliseconds = bacnet_discover_device_elapsed_milliseconds(device_id);
heap_ram = bacnet_discover_device_memory(device_id);
/* convert to KB next highest value */
bacnet_discover_property_name(device_id, OBJECT_DEVICE, device_id,
PROP_MODEL_NAME, model_name, sizeof(model_name), "");
printf(
"device[%u] %7u \"%s\" object_list[%d] in %lums using %lu bytes\n",
device_index, device_id, model_name, object_count, milliseconds,
(unsigned long)heap_ram);
if (Print_Summary) {
continue;
}
for (object_index = 0; object_index < object_count; object_index++) {
if (bacnet_discover_device_object_identifier(
device_id, object_index, &object_id)) {
property_count = bacnet_discover_object_property_count(
device_id, object_id.type, object_id.instance);
bacnet_discover_property_name(device_id, object_id.type,
object_id.instance, PROP_OBJECT_NAME, object_name,
sizeof(object_name), "");
printf(" object_list[%d] %s %u \"%s\" has %u properties\n",
object_index, bactext_object_type_name(object_id.type),
object_id.instance, object_name, property_count);
}
}
}
}
/**
* @brief Non-blocking task for running BACnet server tasks
*/
static void bacnet_server_task(void)
{
static bool initialized = false;
BACNET_ADDRESS src = { 0 }; /* address where message came from */
uint16_t pdu_len = 0;
const unsigned timeout_ms = 5;
if (!initialized) {
initialized = true;
/* broadcast an I-Am on startup */
Send_I_Am(&Handler_Transmit_Buffer[0]);
}
/* input */
/* returns 0 bytes on timeout */
pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout_ms);
/* process */
if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len);
}
/* 1 second tasks */
if (mstimer_expired(&BACnet_Task_Timer)) {
mstimer_reset(&BACnet_Task_Timer);
dcc_timer_seconds(1);
datalink_maintenance_timer(1);
dlenv_maintenance_timer(1);
}
if (mstimer_expired(&BACnet_TSM_Timer)) {
mstimer_reset(&BACnet_TSM_Timer);
tsm_timer_milliseconds(mstimer_interval(&BACnet_TSM_Timer));
}
}
/**
* @brief Initialize the handlers for this server device
*/
static void bacnet_server_init(void)
{
Device_Init(NULL);
/* we need to handle who-is to support dynamic device binding */
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is);
/* we need to handle who-has to support dynamic object binding */
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_HAS, handler_who_has);
/* set the handler for all the services we don't implement */
/* It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler(handler_unrecognized_service);
/* Set the handlers for any confirmed services that we support. */
/* We must implement read property - it's required! */
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property);
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_READ_PROP_MULTIPLE, handler_read_property_multiple);
/* handle communication so we can shutup when asked */
apdu_set_confirmed_handler(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
handler_device_communication_control);
mstimer_set(&BACnet_Task_Timer, 1000);
mstimer_set(&BACnet_TSM_Timer, 50);
}
/**
* @brief Print the usage information for this application
*/
static void print_usage(const char *filename)
{
printf("Usage: %s [--dnet]\n", filename);
printf(" [--discover-seconds][--print-seconds][--print-summary]\n");
printf(" [--version][--help]\n");
}
/**
* @brief Print the help information for this application
*/
static void print_help(const char *filename)
{
printf("Simulate a BACnet server-discovery device.\n");
printf("--discover-seconds:\n"
"Number of seconds to wait before initiating the next discovery.\n");
printf("--print-seconds:\n"
"Number of seconds to wait before printing list of devices.\n");
printf("--print-summary:\n"
"Print only the list of devices.\n");
printf("--dnet N\n"
"Optional BACnet network number N for directed requests.\n"
"Valid range is from 0 to 65535 where 0 is the local connection\n"
"and 65535 is network broadcast.\n");
(void)filename;
}
/**
* @brief Main function of server-client demo.
* @param argc [in] Arg count.
* @param argv [in] Takes one argument: the Device Instance #.
* @return 0 on success.
*/
int main(int argc, char *argv[])
{
int argi = 0;
unsigned int target_args = 0;
const char *filename = NULL;
uint32_t device_id = BACNET_MAX_INSTANCE;
long long_value = -1;
/* data from the command line */
unsigned long print_seconds = 60;
unsigned long discover_seconds = 60;
uint16_t dnet = 0;
filename = filename_remove_path(argv[0]);
for (argi = 1; argi < argc; argi++) {
if (strcmp(argv[argi], "--help") == 0) {
print_usage(filename);
print_help(filename);
return 0;
}
if (strcmp(argv[argi], "--version") == 0) {
printf("%s %s\n", filename, BACNET_VERSION_TEXT);
printf("Copyright (C) 2024 by Steve Karg and others.\n"
"This is free software; see the source for copying "
"conditions.\n"
"There is NO warranty; not even for MERCHANTABILITY or\n"
"FITNESS FOR A PARTICULAR PURPOSE.\n");
return 0;
}
if (strcmp(argv[argi], "--discover-seconds") == 0) {
if (++argi < argc) {
discover_seconds = strtol(argv[argi], NULL, 0);
}
} else if (strcmp(argv[argi], "--print-seconds") == 0) {
if (++argi < argc) {
print_seconds = strtol(argv[argi], NULL, 0);
}
} else if (strcmp(argv[argi], "--print-summary") == 0) {
Print_Summary = true;
} else if (strcmp(argv[argi], "--dnet") == 0) {
if (++argi < argc) {
long_value = strtol(argv[argi], NULL, 0);
if ((long_value >= 0) && (long_value <= UINT16_MAX)) {
dnet = (uint16_t)long_value;
}
}
} else {
if (target_args == 0) {
device_id = strtol(argv[argi], NULL, 0);
target_args++;
}
}
}
if (device_id > BACNET_MAX_INSTANCE) {
debug_perror("device-instance=%u - it must be less than %u\n",
device_id, BACNET_MAX_INSTANCE);
return 1;
}
Device_Set_Object_Instance_Number(device_id);
debug_aprintf("BACnet Server-Discovery Demo\n"
"BACnet Stack Version %s\n"
"BACnet Device ID: %u\n"
"DNET: %u every %lu seconds\n"
"Print Devices: every %lu seconds (0=none)\n"
"Max APDU: %d\n",
BACnet_Version, Device_Object_Instance_Number(), dnet, discover_seconds,
print_seconds, MAX_APDU);
dlenv_init();
atexit(datalink_cleanup);
bacnet_server_init();
/* configure the discovery module */
bacnet_discover_dnet_set(dnet);
bacnet_discover_seconds_set(discover_seconds);
bacnet_discover_init();
atexit(bacnet_discover_cleanup);
mstimer_set(&BACnet_Print_Timer, print_seconds * 1000UL);
/* loop forever */
for (;;) {
bacnet_server_task();
bacnet_discover_task();
if (mstimer_expired(&BACnet_Print_Timer)) {
mstimer_reset(&BACnet_Print_Timer);
print_discovered_devices();
}
}
return 0;
}
-1
View File
@@ -45,7 +45,6 @@
#include "bacnet/basic/sys/mstimer.h"
#include "bacnet/basic/sys/filename.h"
#include "bacnet/basic/services.h"
#include "bacnet/basic/services.h"
#include "bacnet/basic/tsm/tsm.h"
#if defined(BACDL_MSTP)
#include "rs485.h"
File diff suppressed because it is too large Load Diff
+136
View File
@@ -0,0 +1,136 @@
/**
* @file
* @author Steve Karg <skarg@users.sourceforge.net>
* @date 2024
*
* SPDX-License-Identifier: MIT
*/
#ifndef BAC_DISCOVER_H
#define BAC_DISCOVER_H
#include <stdbool.h>
#include <stdint.h>
#include "bacnet/bacdef.h"
#include "bacnet/bacenum.h"
#include "bacnet/bacapp.h"
#include "bacnet/rp.h"
#include "bacnet/bacnet_stack_exports.h"
/**
* @brief Callback function for iterating the results of the device discovery.
* @param device_id [in] The device ID of the data
* @param device_index [in] The index of the device in the list of discovered devices
* @param object_index [in] The index of the object in the list of discovered objects in the device
* @param property_index [in] The index of the property in the list of discovered properties in the object in the device
* @param rp_data [in] The contents of the device object property
* @param context_data [in] The context data passed to the discover function
* @return true if the iteration should continue, false if it should stop
*/
typedef bool (*bacnet_discover_device_callback) (
uint32_t device_id,
unsigned device_index,
unsigned object_index,
unsigned property_index,
BACNET_READ_PROPERTY_DATA * rp_data,
void *context_data);
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
BACNET_STACK_EXPORT
void bacnet_discover_cleanup(void);
BACNET_STACK_EXPORT
int bacnet_discover_device_count(void);
BACNET_STACK_EXPORT
uint32_t bacnet_discover_device_instance(unsigned index);
BACNET_STACK_EXPORT
int bacnet_discover_device_object_count(uint32_t device_id);
BACNET_STACK_EXPORT
bool bacnet_discover_device_object_identifier(uint32_t device_id,
unsigned index, BACNET_OBJECT_ID *object_id);
BACNET_STACK_EXPORT
unsigned long bacnet_discover_device_elapsed_milliseconds(
uint32_t device_id);
BACNET_STACK_EXPORT
size_t bacnet_discover_device_memory(
uint32_t device_id);
BACNET_STACK_EXPORT
unsigned int bacnet_discover_object_property_count(
uint32_t device_id,
BACNET_OBJECT_TYPE object_type,
uint32_t object_instance);
BACNET_STACK_EXPORT
bool bacnet_discover_object_property_identifier(
uint32_t device_id,
BACNET_OBJECT_TYPE object_type,
uint32_t object_instance,
unsigned index,
uint32_t *property_id);
BACNET_STACK_EXPORT
bool bacnet_discover_property_value(uint32_t device_id,
BACNET_OBJECT_TYPE object_type,
uint32_t object_instance,
BACNET_PROPERTY_ID object_property,
BACNET_APPLICATION_DATA_VALUE *value);
BACNET_STACK_EXPORT
bool bacnet_discover_property_name(uint32_t device_id,
BACNET_OBJECT_TYPE object_type,
uint32_t object_instance,
BACNET_PROPERTY_ID object_property,
char *buffer,
size_t buffer_len,
const char *default_string);
BACNET_STACK_EXPORT
bool bacnet_discover_device_object_property_iterate(
uint32_t device_id,
BACNET_OBJECT_TYPE object_type,
uint32_t object_instance,
bacnet_discover_device_callback callback, void *context);
BACNET_STACK_EXPORT
bool bacnet_discover_device_object_iterate(
uint32_t device_id,
bacnet_discover_device_callback callback, void *context);
BACNET_STACK_EXPORT
bool bacnet_discover_device_iterate(
bacnet_discover_device_callback callback, void *context);
BACNET_STACK_EXPORT
void bacnet_discover_task(void);
BACNET_STACK_EXPORT
void bacnet_discover_dnet_set(uint16_t dnet);
BACNET_STACK_EXPORT
uint16_t bacnet_discover_dnet(void);
BACNET_STACK_EXPORT
void bacnet_discover_vendor_id_set(uint16_t vendor_id);
BACNET_STACK_EXPORT
uint16_t bacnet_discover_vendor_id(void);
BACNET_STACK_EXPORT
void bacnet_discover_seconds_set(unsigned int seconds);
BACNET_STACK_EXPORT
unsigned int bacnet_discover_seconds(void);
BACNET_STACK_EXPORT
void bacnet_discover_read_process_milliseconds_set(unsigned long milliseconds);
BACNET_STACK_EXPORT
unsigned long bacnet_discover_read_process_milliseconds(void);
BACNET_STACK_EXPORT
void bacnet_discover_device_add(
uint32_t device_instance,
unsigned max_apdu,
int segmentation,
uint16_t vendor_id);
BACNET_STACK_EXPORT
void bacnet_discover_init(void);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+94 -119
View File
@@ -32,6 +32,8 @@ static struct mstimer Cache_Timer;
static struct mstimer Read_Write_Timer;
/* where the data from the read is stored */
static bacnet_read_write_value_callback_t bacnet_read_write_value_callback;
/* where the data from the I-Am is called */
static bacnet_read_write_device_callback_t bacnet_read_write_device_callback;
/* states for client task */
typedef enum {
@@ -170,6 +172,10 @@ static void My_I_Am_Bind(
}
if (bind) {
address_add_binding(device_id, max_apdu, src);
if (bacnet_read_write_device_callback) {
bacnet_read_write_device_callback(
device_id, max_apdu, segmentation, vendor_id);
}
}
}
}
@@ -191,6 +197,64 @@ static void MyWritePropertySimpleAckHandler(
}
}
/**
* @brief Process a ReadProperty-ACK message
* @param device_id [in] The device ID of the source of the message
* @param rp_data [in] The contents of the service request.
*/
static void bacnet_read_property_ack_process(
uint32_t device_id, BACNET_READ_PROPERTY_DATA *rp_data)
{
BACNET_APPLICATION_DATA_VALUE *value;
uint8_t *apdu;
int apdu_len, len;
BACNET_ARRAY_INDEX array_index = 0;
if (rp_data) {
apdu = rp_data->application_data;
apdu_len = rp_data->application_data_len;
while (apdu_len) {
value = &Target_Decoded_Property_Value;
len = bacapp_decode_known_property(apdu, (unsigned)apdu_len, value,
rp_data->object_type, rp_data->object_property);
if (len > 0) {
if ((len < apdu_len) &&
(rp_data->array_index == BACNET_ARRAY_ALL)) {
/* assume that since there is more data that this
is an array and split full array of elements
into separate RP Acks */
array_index = 1;
}
rp_data->error_class = ERROR_CLASS_SERVICES;
rp_data->error_code = ERROR_CODE_SUCCESS;
if (array_index) {
rp_data->array_index = array_index;
}
if (bacnet_read_write_value_callback) {
bacnet_read_write_value_callback(device_id, rp_data, value);
}
/* see if there is any more data */
if (len < apdu_len) {
apdu += len;
apdu_len -= len;
if (array_index) {
array_index++;
}
} else {
break;
}
} else {
rp_data->error_class = ERROR_CLASS_SERVICES;
rp_data->error_code = ERROR_CODE_SUCCESS;
if (bacnet_read_write_value_callback) {
bacnet_read_write_value_callback(device_id, rp_data, NULL);
}
break;
}
}
}
}
/** Handler for a ReadProperty ACK.
* Saves the data from a matching read-property request
*
@@ -207,13 +271,11 @@ static void My_Read_Property_Ack_Handler(uint8_t *service_request,
{
int len = 0;
BACNET_READ_PROPERTY_DATA rp_data;
uint8_t *application_data;
int application_data_len;
BACNET_APPLICATION_DATA_VALUE *value;
uint32_t device_id = 0;
if (address_match(&Target_Address, src) &&
(service_data->invoke_id == Request_Invoke_ID)) {
address_get_device_id(src, &device_id);
len = rp_ack_decode_service_request(
service_request, service_len, &rp_data);
if (len < 0) {
@@ -222,68 +284,7 @@ static void My_Read_Property_Ack_Handler(uint8_t *service_request,
Error_Class = ERROR_CLASS_SERVICES;
Error_Code = ERROR_CODE_INTERNAL_ERROR;
} else {
address_get_device_id(src, &device_id);
application_data = rp_data.application_data;
application_data_len = rp_data.application_data_len;
/* value? need to loop until all of the len is gone... */
for (;;) {
value = &Target_Decoded_Property_Value;
len = bacapp_decode_application_data(
application_data, (uint8_t)application_data_len, value);
if (len > 0) {
/* handle the data */
rp_data.error_class = ERROR_CLASS_SERVICES;
rp_data.error_code = ERROR_CODE_SUCCESS;
if (bacnet_read_write_value_callback) {
bacnet_read_write_value_callback(
device_id, &rp_data, value);
}
/* see if there is any more data */
if (len < application_data_len) {
application_data += len;
application_data_len -= len;
} else {
break;
}
} else {
break;
}
}
}
}
}
static void bacnet_rpm_process(
uint32_t device_id, BACNET_READ_ACCESS_DATA *rpm_data)
{
BACNET_PROPERTY_REFERENCE *listOfProperties;
BACNET_APPLICATION_DATA_VALUE *value;
BACNET_READ_PROPERTY_DATA rp_data;
if (rpm_data) {
rp_data.error_class = ERROR_CLASS_SERVICES;
rp_data.error_code = ERROR_CODE_SUCCESS;
rp_data.object_type = rpm_data->object_type;
rp_data.object_instance = rpm_data->object_instance;
listOfProperties = rpm_data->listOfProperties;
while (listOfProperties) {
rp_data.object_property = listOfProperties->propertyIdentifier;
rp_data.array_index = listOfProperties->propertyArrayIndex;
if (listOfProperties->propertyArrayIndex == BACNET_ARRAY_ALL) {
rp_data.array_index = 1;
}
value = listOfProperties->value;
while (value) {
if (bacnet_read_write_value_callback) {
bacnet_read_write_value_callback(
device_id, &rp_data, value);
}
value = value->next;
if (listOfProperties->propertyArrayIndex == BACNET_ARRAY_ALL) {
rp_data.array_index++;
}
}
listOfProperties = listOfProperties->next;
bacnet_read_property_ack_process(device_id, &rp_data);
}
}
}
@@ -297,67 +298,20 @@ static void bacnet_rpm_process(
* @param service_data [in] The BACNET_CONFIRMED_SERVICE_DATA information
* decoded from the APDU header of this message.
*/
static void My_Read_Property_Multiple_Ack_Handler(uint8_t *service_request,
uint16_t service_len,
static void My_Read_Property_Multiple_Ack_Handler(uint8_t *apdu,
uint16_t apdu_len,
BACNET_ADDRESS *src,
BACNET_CONFIRMED_SERVICE_ACK_DATA *service_data)
{
int len = 0;
BACNET_READ_ACCESS_DATA *rpm_data;
BACNET_READ_ACCESS_DATA *old_rpm_data;
BACNET_PROPERTY_REFERENCE *rpm_property;
BACNET_PROPERTY_REFERENCE *old_rpm_property;
BACNET_APPLICATION_DATA_VALUE *value;
BACNET_APPLICATION_DATA_VALUE *old_value;
BACNET_READ_PROPERTY_DATA rp_data = { 0 };
uint32_t device_id = 0;
address_get_device_id(src, &device_id);
if (address_match(&Target_Address, src) &&
(service_data->invoke_id == Request_Invoke_ID)) {
rpm_data = calloc(1, sizeof(BACNET_READ_ACCESS_DATA));
if (rpm_data) {
len = rpm_ack_decode_service_request(
service_request, service_len, rpm_data);
}
if (len > 0) {
address_get_device_id(src, &device_id);
while (rpm_data) {
rpm_ack_print_data(rpm_data);
bacnet_rpm_process(device_id, rpm_data);
rpm_property = rpm_data->listOfProperties;
while (rpm_property) {
value = rpm_property->value;
while (value) {
old_value = value;
value = value->next;
free(old_value);
}
old_rpm_property = rpm_property;
rpm_property = rpm_property->next;
free(old_rpm_property);
}
old_rpm_data = rpm_data;
rpm_data = rpm_data->next;
free(old_rpm_data);
}
} else {
while (rpm_data) {
rpm_property = rpm_data->listOfProperties;
while (rpm_property) {
value = rpm_property->value;
while (value) {
old_value = value;
value = value->next;
free(old_value);
}
old_rpm_property = rpm_property;
rpm_property = rpm_property->next;
free(old_rpm_property);
}
old_rpm_data = rpm_data;
rpm_data = rpm_data->next;
free(old_rpm_data);
}
}
rpm_ack_object_property_process(apdu, apdu_len,
device_id, &rp_data,
bacnet_read_property_ack_process);
}
}
@@ -546,6 +500,17 @@ void bacnet_read_write_value_callback_set(
bacnet_read_write_value_callback = callback;
}
/**
* @brief Sets the callback for when an I-Am returns device data
*
* @param callback - function for callback
*/
void bacnet_read_write_device_callback_set(
bacnet_read_write_device_callback_t callback)
{
bacnet_read_write_device_callback = callback;
}
/**
* @brief Handles the ReadProperty repetitive task
*/
@@ -879,6 +844,16 @@ void bacnet_read_write_vendor_id_filter_set(uint16_t vendor_id)
Target_Vendor_ID = vendor_id;
}
/**
* @brief Gets a Vendor ID filter on I-Am bindings to limit the address
* cache usage when we are only reading/writing to a specific vendor ID
* @return vendor_id - vendor ID to filter, 0=no filter
*/
uint16_t bacnet_read_write_vendor_id_filter(void)
{
return Target_Vendor_ID;
}
/**
* @brief Initializes the ReadProperty module
*/
+18
View File
@@ -28,6 +28,19 @@ typedef void (*bacnet_read_write_value_callback_t)(uint32_t device_instance,
BACNET_READ_PROPERTY_DATA *rp_data,
BACNET_APPLICATION_DATA_VALUE *value);
/**
* Save the I-Am service data to a data store
*
* @param device_instance [in] device instance number where data originated
* @param max_apdu [in] maximum APDU size
* @param segmentation [in] segmentation flag
* @param vendor_id [in] vendor identifier
*/
typedef void (*bacnet_read_write_device_callback_t)(uint32_t device_instance,
unsigned max_apdu,
int segmentation,
uint16_t vendor_id);
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
@@ -97,7 +110,12 @@ BACNET_STACK_EXPORT
void bacnet_read_write_value_callback_set(
bacnet_read_write_value_callback_t callback);
BACNET_STACK_EXPORT
void bacnet_read_write_device_callback_set(
bacnet_read_write_device_callback_t callback);
BACNET_STACK_EXPORT
void bacnet_read_write_vendor_id_filter_set(uint16_t vendor_id);
BACNET_STACK_EXPORT
uint16_t bacnet_read_write_vendor_id_filter(void);
#ifdef __cplusplus
}
+10 -1
View File
@@ -62,6 +62,7 @@ int rpm_ack_decode_service_request(
int len = 0; /* number of bytes returned from decoding */
uint8_t tag_number = 0; /* decoded tag number */
uint32_t len_value = 0; /* decoded length value */
int data_len = 0; /* data blob length */
BACNET_READ_ACCESS_DATA *rpm_object;
BACNET_READ_ACCESS_DATA *old_rpm_object;
BACNET_PROPERTY_REFERENCE *rpm_property;
@@ -110,6 +111,8 @@ int rpm_ack_decode_service_request(
apdu_len -= len;
apdu += len;
if (apdu_len && decode_is_opening_tag_number(apdu, 4)) {
data_len = bacapp_data_len(apdu, (unsigned)apdu_len,
rpm_property->propertyIdentifier);
/* propertyValue */
decoded_len++;
apdu_len--;
@@ -121,7 +124,7 @@ int rpm_ack_decode_service_request(
/* Special case for an empty array - we decode it as null */
if (apdu_len && decode_is_closing_tag_number(apdu, 4)) {
/* NULL value has tag 0, that was already set by calloc */
bacapp_value_list_init(value, 1);
decoded_len++;
apdu_len--;
apdu++;
@@ -134,6 +137,11 @@ int rpm_ack_decode_service_request(
* OK. */
if (len < 0) {
/* problem decoding */
if (data_len >= 0) {
/* valid data that we'll skip over */
len = data_len;
bacapp_value_list_init(value, 1);
} else {
PERROR("RPM Ack: unable to decode! %s:%s\n",
bactext_object_type_name(
rpm_object->object_type),
@@ -142,6 +150,7 @@ int rpm_ack_decode_service_request(
/* note: caller will free the memory */
return BACNET_STATUS_ERROR;
}
}
decoded_len += len;
apdu_len -= len;
apdu += len;
+2 -2
View File
@@ -1535,7 +1535,7 @@ BACNET_PROPERTY_ID property_list_special_property(
BACNET_PROPERTY_ID special_property,
unsigned index)
{
int property = -1; /* return value */
BACNET_PROPERTY_ID property = UINT32_MAX; /* return value */
unsigned required, optional, proprietary;
struct special_property_list_t PropertyList = { 0 };
@@ -1573,7 +1573,7 @@ BACNET_PROPERTY_ID property_list_special_property(
}
}
return (BACNET_PROPERTY_ID)property;
return property;
}
unsigned property_list_special_count(
+9
View File
@@ -57,6 +57,15 @@ typedef int (
*read_property_function) (
BACNET_READ_PROPERTY_DATA * rp_data);
/**
* @brief Process a ReadProperty-ACK message
* @param device_id [in] The device ID of the source of the message
* @param rp_data [in] The contents of the ReadProperty-ACK message
*/
typedef void (
*read_property_ack_process) (
uint32_t device_id, BACNET_READ_PROPERTY_DATA *rp_data);
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
+133
View File
@@ -587,4 +587,137 @@ int rpm_ack_decode_object_property(uint8_t *apdu,
return (int)len;
}
/**
* @brief Decode the RPM Ack and call the ReadProperty-ACK function to
* process each property value of the reply.
*
* ReadAccessResult ::= SEQUENCE {
* object-identifier [0] BACnetObjectIdentifier,
* list-of-results [1] SEQUENCE OF SEQUENCE {
* property-identifier [2] BACnetPropertyIdentifier,
* property-array-index [3] Unsigned OPTIONAL,
* -- used only with array datatype
* -- if omitted with an array the entire
* -- array is referenced
* read-result CHOICE {
* property-value [4] ABSTRACT-SYNTAX.&Type,
* property-access-error [5] Error
* }
* }
* }
*
* @param apdu [in] Buffer of bytes received.
* @param apdu_len [in] Count of valid bytes in the buffer.
* @param device_id [in] The device ID of the device that replied.
* @param rp_data [in] The data structure to be filled.
* @param callback [in] The function to call for each property value.
*/
void rpm_ack_object_property_process(
uint8_t *apdu,
unsigned apdu_len,
uint32_t device_id,
BACNET_READ_PROPERTY_DATA *rp_data,
read_property_ack_process callback)
{
int len = 0;
uint16_t application_data_len;
uint32_t error_value = 0; /* decoded error value */
if (!apdu) {
return;
}
if (!rp_data) {
return;
}
while (apdu_len) {
/* object-identifier [0] BACnetObjectIdentifier */
/* list-of-results [1] SEQUENCE OF SEQUENCE */
len = rpm_ack_decode_object_id(
apdu, apdu_len, &rp_data->object_type, &rp_data->object_instance);
if (len <= 0) {
/* malformed */
return;
}
apdu_len -= len;
apdu += len;
while (apdu_len) {
len = rpm_ack_decode_object_property(apdu, apdu_len,
&rp_data->object_property, &rp_data->array_index);
if (len <= 0) {
/* malformed */
return;
}
apdu_len -= len;
apdu += len;
if (bacnet_is_opening_tag_number(apdu, apdu_len, 4, &len)) {
application_data_len = bacapp_data_len(
apdu, apdu_len, rp_data->object_property);
/* propertyValue */
apdu_len -= len;
apdu += len;
if (application_data_len) {
rp_data->application_data_len = application_data_len;
rp_data->application_data = apdu;
apdu_len -= application_data_len;
apdu += application_data_len;
}
if (bacnet_is_closing_tag_number(apdu, apdu_len, 4, &len)) {
apdu_len -= len;
apdu += len;
} else {
/* malformed */
return;
}
rp_data->error_class = ERROR_CLASS_PROPERTY;
rp_data->error_code = ERROR_CODE_SUCCESS;
if (callback) {
callback(device_id, rp_data);
}
} else if (bacnet_is_opening_tag_number(
apdu, apdu_len, 5, &len)) {
apdu_len -= len;
apdu += len;
/* property-access-error */
len = bacnet_enumerated_application_decode(
apdu, apdu_len, &error_value);
if (len > 0) {
rp_data->error_class = (BACNET_ERROR_CLASS)error_value;
apdu_len -= len;
apdu += len;
} else {
/* malformed */
return;
}
len = bacnet_enumerated_application_decode(
apdu, apdu_len, &error_value);
if (len > 0) {
rp_data->error_code = (BACNET_ERROR_CODE)error_value;
apdu_len -= len;
apdu += len;
} else {
/* malformed */
return;
}
if (bacnet_is_closing_tag_number(apdu, apdu_len, 5, &len)) {
apdu_len -= len;
apdu += len;
} else {
/* malformed */
return;
}
if (callback) {
callback(device_id, rp_data);
}
}
}
len = rpm_decode_object_end(apdu, apdu_len);
if (len) {
apdu_len -= len;
apdu += len;
}
}
return;
}
#endif
+7
View File
@@ -31,6 +31,7 @@
#include "bacnet/bacdef.h"
#include "bacnet/bacapp.h"
#include "bacnet/proplist.h"
#include "bacnet/rp.h"
/*
* Bundle together commonly used data items for convenience when calling
* rpm helper functions.
@@ -184,6 +185,12 @@ extern "C" {
unsigned apdu_len,
BACNET_PROPERTY_ID * object_property,
BACNET_ARRAY_INDEX * array_index);
void rpm_ack_object_property_process(
uint8_t *apdu,
unsigned apdu_len,
uint32_t device_id,
BACNET_READ_PROPERTY_DATA *rp_data,
read_property_ack_process callback);
#ifdef __cplusplus
}