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
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
}
+17 -8
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,13 +137,19 @@ int rpm_ack_decode_service_request(
* OK. */
if (len < 0) {
/* problem decoding */
PERROR("RPM Ack: unable to decode! %s:%s\n",
bactext_object_type_name(
rpm_object->object_type),
bactext_property_name(
rpm_property->propertyIdentifier));
/* note: caller will free the memory */
return BACNET_STATUS_ERROR;
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),
bactext_property_name(
rpm_property->propertyIdentifier));
/* note: caller will free the memory */
return BACNET_STATUS_ERROR;
}
}
decoded_len += len;
apdu_len -= 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
}