Feature/add who am i and you are services (#1024)

* Added Who-Am-I-Request encoding, decoding, unit tests, and command line application bacwhoami.

* Added You-Are-Request encoding, decoding, unit tests, and command line application bacyouare.

* Added Who-Am-I with Who-Is and You-Are handling into the example server application when run as device 4194303.
This commit is contained in:
Steve Karg
2025-06-23 10:49:59 -05:00
committed by GitHub
parent 9e626e599b
commit 5072fb5913
34 changed files with 2411 additions and 80 deletions
+106 -70
View File
@@ -2184,6 +2184,106 @@ static int bacapp_snprintf_double(char *str, size_t str_len, double value)
}
#endif
/**
* @brief Print an octet string value to a string for EPICS
* @param str - destination string, or NULL for length only
* @param str_len - length of the destination string, or 0 for length only
* @param value - octet string value to print
* @return number of characters written
*/
int bacapp_snprintf_octet_string(
char *str, size_t str_len, const BACNET_OCTET_STRING *value)
{
int ret_val = 0;
int len = 0;
int slen = 0;
int i = 0;
len = octetstring_length(value);
if (len > 0) {
const uint8_t *octet_str;
octet_str = octetstring_value((BACNET_OCTET_STRING *)value);
for (i = 0; i < len; i++) {
slen = bacapp_snprintf(str, str_len, "%02X", *octet_str);
ret_val += bacapp_snprintf_shift(slen, &str, &str_len);
octet_str++;
}
}
return ret_val;
}
/**
* @brief Print a character string value to a string for EPICS
* @param str - destination string, or NULL for length only
* @param str_len - length of the destination string, or 0 for length only
* @param value - character string value to print
* @return number of characters written
*/
int bacapp_snprintf_character_string(
char *str, size_t str_len, const BACNET_CHARACTER_STRING *value)
{
int ret_val = 0;
int len = 0;
int slen = 0;
int i = 0;
const char *char_str;
#if (__STDC_VERSION__ >= 199901L) && defined(__STDC_ISO_10646__)
/* Wide character (decoded from multi-byte character). */
wchar_t wc;
/* Wide character length in bytes. */
int wclen;
#endif
len = characterstring_length(value);
char_str = characterstring_value(value);
slen = bacapp_snprintf(str, str_len, "\"");
ret_val += bacapp_snprintf_shift(slen, &str, &str_len);
#if (__STDC_VERSION__ >= 199901L) && defined(__STDC_ISO_10646__)
if (characterstring_encoding(value) == CHARACTER_UTF8) {
while (len > 0) {
wclen = mbtowc(&wc, char_str, MB_CUR_MAX);
if (wclen == -1) {
/* Encoding error, reset state: */
mbtowc(NULL, NULL, MB_CUR_MAX);
/* After handling an invalid byte,
retry with the next one. */
wclen = 1;
wc = L'?';
} else {
if (!iswprint(wc)) {
wc = L'.';
}
}
/* For portability, cast wchar_t to wint_t */
slen = bacapp_snprintf(str, str_len, "%lc", (wint_t)wc);
ret_val += bacapp_snprintf_shift(slen, &str, &str_len);
if (len > wclen) {
len -= wclen;
char_str += wclen;
} else {
len = 0;
}
}
} else
#endif
{
for (i = 0; i < len; i++) {
if (isprint(*((const unsigned char *)char_str))) {
slen = bacapp_snprintf(str, str_len, "%c", *char_str);
} else {
slen = bacapp_snprintf(str, str_len, "%c", '.');
}
ret_val += bacapp_snprintf_shift(slen, &str, &str_len);
char_str++;
}
}
slen = bacapp_snprintf(str, str_len, "\"");
ret_val += slen;
return ret_val;
}
#if defined(BACAPP_BIT_STRING)
/**
* @brief Print a bit string value to a string for EPICS
@@ -3601,20 +3701,12 @@ static int bacapp_snprintf_action_command(
int bacapp_snprintf_value(
char *str, size_t str_len, const BACNET_OBJECT_PROPERTY_VALUE *object_value)
{
size_t len = 0, i = 0;
const BACNET_APPLICATION_DATA_VALUE *value;
BACNET_PROPERTY_ID property = PROP_ALL;
BACNET_OBJECT_TYPE object_type = MAX_BACNET_OBJECT_TYPE;
int ret_val = 0;
#if defined(BACAPP_BDT_ENTRY) || defined(BACAPP_FDT_ENTRY)
int slen = 0;
#if defined(BACAPP_CHARACTER_STRING)
const char *char_str;
#if (__STDC_VERSION__ >= 199901L) && defined(__STDC_ISO_10646__)
/* Wide character (decoded from multi-byte character). */
wchar_t wc;
/* Wide character length in bytes. */
int wclen;
#endif
#endif
if (object_value && object_value->value) {
@@ -3658,69 +3750,14 @@ int bacapp_snprintf_value(
#endif
#if defined(BACAPP_OCTET_STRING)
case BACNET_APPLICATION_TAG_OCTET_STRING:
len = octetstring_length(&value->type.Octet_String);
if (len > 0) {
const uint8_t *octet_str;
octet_str = octetstring_value(
(BACNET_OCTET_STRING *)&value->type.Octet_String);
for (i = 0; i < len; i++) {
slen =
bacapp_snprintf(str, str_len, "%02X", *octet_str);
ret_val += bacapp_snprintf_shift(slen, &str, &str_len);
octet_str++;
}
}
ret_val = bacapp_snprintf_octet_string(
str, str_len, &value->type.Octet_String);
break;
#endif
#if defined(BACAPP_CHARACTER_STRING)
case BACNET_APPLICATION_TAG_CHARACTER_STRING:
len = characterstring_length(&value->type.Character_String);
char_str = characterstring_value(&value->type.Character_String);
slen = bacapp_snprintf(str, str_len, "\"");
ret_val += bacapp_snprintf_shift(slen, &str, &str_len);
#if (__STDC_VERSION__ >= 199901L) && defined(__STDC_ISO_10646__)
if (characterstring_encoding(&value->type.Character_String) ==
CHARACTER_UTF8) {
while (len > 0) {
wclen = mbtowc(&wc, char_str, MB_CUR_MAX);
if (wclen == -1) {
/* Encoding error, reset state: */
mbtowc(NULL, NULL, MB_CUR_MAX);
/* After handling an invalid byte,
retry with the next one. */
wclen = 1;
wc = L'?';
} else {
if (!iswprint(wc)) {
wc = L'.';
}
}
/* For portability, cast wchar_t to wint_t */
slen = bacapp_snprintf(str, str_len, "%lc", (wint_t)wc);
ret_val += bacapp_snprintf_shift(slen, &str, &str_len);
if (len > wclen) {
len -= wclen;
char_str += wclen;
} else {
len = 0;
}
}
} else
#endif
{
for (i = 0; i < len; i++) {
if (isprint(*((const unsigned char *)char_str))) {
slen =
bacapp_snprintf(str, str_len, "%c", *char_str);
} else {
slen = bacapp_snprintf(str, str_len, "%c", '.');
}
ret_val += bacapp_snprintf_shift(slen, &str, &str_len);
char_str++;
}
}
slen = bacapp_snprintf(str, str_len, "\"");
ret_val += slen;
ret_val = bacapp_snprintf_character_string(
str, str_len, &value->type.Character_String);
break;
#endif
#if defined(BACAPP_BIT_STRING)
@@ -3754,9 +3791,8 @@ int bacapp_snprintf_value(
#endif
#if defined(BACAPP_TIMESTAMP)
case BACNET_APPLICATION_TAG_TIMESTAMP:
slen = bacapp_timestamp_to_ascii(
ret_val = bacapp_timestamp_to_ascii(
str, str_len, &value->type.Time_Stamp);
ret_val += slen;
break;
#endif
#if defined(BACAPP_DATETIME)
+4
View File
@@ -376,6 +376,10 @@ int bacapp_snprintf_value(
char *str,
size_t str_len,
const BACNET_OBJECT_PROPERTY_VALUE *object_value);
int bacapp_snprintf_octet_string(
char *str, size_t str_len, const BACNET_OCTET_STRING *value);
int bacapp_snprintf_character_string(
char *str, size_t str_len, const BACNET_CHARACTER_STRING *value);
BACNET_STACK_EXPORT
bool bacapp_channel_value_copy(
+22 -2
View File
@@ -51,6 +51,7 @@ static char *Application_Software_Version = "1.0";
static const char *BACnet_Version = BACNET_VERSION_TEXT;
static char *Location = "USA";
static char *Description = "command line client";
static char *Serial_Number = "BACnetc64b8511f0a5bab73ca11c2d9a";
/* static uint8_t Protocol_Version = 1; - constant, not settable */
/* static uint8_t Protocol_Revision = 4; - constant, not settable */
/* Protocol_Services_Supported - dynamically generated */
@@ -330,9 +331,14 @@ static const int Device_Properties_Required[] = {
static const int Device_Properties_Optional[] = {
#if defined(BACDL_MSTP)
PROP_MAX_MASTER, PROP_MAX_INFO_FRAMES,
PROP_MAX_MASTER,
PROP_MAX_INFO_FRAMES,
#endif
PROP_DESCRIPTION, PROP_LOCATION, PROP_ACTIVE_COV_SUBSCRIPTIONS, -1
PROP_DESCRIPTION,
PROP_LOCATION,
PROP_SERIAL_NUMBER,
PROP_ACTIVE_COV_SUBSCRIPTIONS,
-1
};
static const int Device_Properties_Proprietary[] = { -1 };
@@ -670,6 +676,15 @@ bool Device_Set_Location(const char *name, size_t length)
return status;
}
/**
* @brief Get the UUID device serial-number property value.
* @return The device serial-number, as a character string.
*/
const char *Device_Serial_Number(void)
{
return Serial_Number;
}
uint8_t Device_Protocol_Version(void)
{
return BACNET_PROTOCOL_VERSION;
@@ -1162,6 +1177,11 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata)
#endif
case PROP_ACTIVE_COV_SUBSCRIPTIONS:
break;
case PROP_SERIAL_NUMBER:
characterstring_init_ansi(&char_string, Serial_Number);
apdu_len =
encode_application_character_string(&apdu[0], &char_string);
break;
default:
rpdata->error_class = ERROR_CLASS_PROPERTY;
rpdata->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
+71
View File
@@ -0,0 +1,71 @@
/**
* @file
* @brief A basic Who-Am-I service handler
* @author Steve Karg <skarg@users.sourceforge.net>
* @date June 2025
* @copyright SPDX-License-Identifier: MIT
*/
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
/* BACnet Stack defines - first */
#include "bacnet/bacdef.h"
/* BACnet Stack API */
#include "bacnet/bacdcode.h"
#include "bacnet/bacapp.h"
#include "bacnet/whoami.h"
#include "bacnet/basic/services.h"
#include "bacnet/basic/sys/debug.h"
/**
* A basic handler for Who-Am-I responses.
* @param service_request [in] The received message to be handled.
* @param service_len [in] Length of the service_request message.
* @param src [in] The BACNET_ADDRESS of the message's source.
*/
void handler_who_am_i_json_print(
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src)
{
int len = 0;
uint16_t vendor_id = 0;
BACNET_CHARACTER_STRING model_name = { 0 };
BACNET_CHARACTER_STRING serial_number = { 0 };
char *model_name_string = NULL;
char *serial_number_string = NULL;
(void)src;
len = who_am_i_request_decode(
service_request, service_len, &vendor_id, &model_name, &serial_number);
if (len > 0) {
debug_printf_stdout("{\n\"Who-Am-I-Request\": {\n");
debug_printf_stdout(" \"vendor-id\" : %u,\n", (unsigned)vendor_id);
len = bacapp_snprintf_character_string(NULL, 0, &model_name);
if (len > 0) {
model_name_string = calloc(sizeof(char), len + 1);
if (model_name_string) {
bacapp_snprintf_character_string(
model_name_string, len + 1, &model_name);
}
}
debug_printf_stdout(
" \"model-name\" : %s,\n",
model_name_string ? model_name_string : "");
len = bacapp_snprintf_character_string(NULL, 0, &serial_number);
if (len > 0) {
serial_number_string = calloc(sizeof(char), len + 1);
if (serial_number_string) {
bacapp_snprintf_character_string(
serial_number_string, len + 1, &serial_number);
}
}
debug_printf_stdout(
" \"serial-number\" : %s",
serial_number_string ? serial_number_string : "");
debug_printf_stdout("\n }\n}\n");
free(model_name_string);
free(serial_number_string);
}
return;
}
+31
View File
@@ -0,0 +1,31 @@
/**
* @file
* @brief Header file for a basic Who-Am-I-Request service handler
* @author Steve Karg
* @date June 2025
* @copyright SPDX-License-Identifier: MIT
*/
#ifndef HANDLER_WHO_AM_I_H
#define HANDLER_WHO_AM_I_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
/* BACnet Stack defines - first */
#include "bacnet/bacdef.h"
/* BACnet Stack API */
#include "bacnet/apdu.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
BACNET_STACK_EXPORT
void handler_who_am_i_json_print(
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+51 -1
View File
@@ -67,8 +67,8 @@ void handler_who_is_unicast(
len = whois_decode_service_request(
service_request, service_len, &low_limit, &high_limit);
/* If no limits, then always respond */
if (len == 0) {
/* If no limits, then always respond */
Send_I_Am_Unicast(&Handler_Transmit_Buffer[0], src);
} else if (len != BACNET_STATUS_ERROR) {
/* is my device id within the limits? */
@@ -81,6 +81,56 @@ void handler_who_is_unicast(
return;
}
/** Handler for Who-Is requests, with broadcast I-Am or Who-Am-I response.
* @ingroup DMDDB
* @param service_request [in] The received message to be handled.
* @param service_len [in] Length of the service_request message.
* @param src [in] The BACNET_ADDRESS of the message's source (ignored).
*/
void handler_who_is_who_am_i_unicast(
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src)
{
int len = 0;
int32_t low_limit = 0;
int32_t high_limit = 0;
BACNET_CHARACTER_STRING model_name = { 0 }, serial_number = { 0 };
len = whois_decode_service_request(
service_request, service_len, &low_limit, &high_limit);
if (len == 0) {
if (Device_Object_Instance_Number() == BACNET_MAX_INSTANCE) {
/* The Who-Am-I service is also used to respond to a Who-Is
service request that uses the Device Object_Identifier
instance number of 4194303. */
characterstring_init_ansi(&model_name, Device_Model_Name());
characterstring_init_ansi(&serial_number, Device_Serial_Number());
Send_Who_Am_I_To_Network(
src, Device_Vendor_Identifier(), &model_name, &serial_number);
} else {
/* If no limits, then always respond */
Send_I_Am_Unicast(&Handler_Transmit_Buffer[0], src);
}
} else if (len != BACNET_STATUS_ERROR) {
/* is my device id within the limits? */
if ((Device_Object_Instance_Number() >= (uint32_t)low_limit) &&
(Device_Object_Instance_Number() <= (uint32_t)high_limit)) {
if (Device_Object_Instance_Number() == BACNET_MAX_INSTANCE) {
/* The Who-Am-I service is also used to respond to a Who-Is
service request that uses the Device Object_Identifier
instance number of 4194303. */
characterstring_init_ansi(&model_name, Device_Model_Name());
characterstring_init_ansi(
&serial_number, Device_Serial_Number());
Send_Who_Am_I_To_Network(
src, Device_Vendor_Identifier(), &model_name,
&serial_number);
} else {
Send_I_Am_Unicast(&Handler_Transmit_Buffer[0], src);
}
}
}
}
#ifdef BAC_ROUTING /* was for BAC_ROUTING - delete in 2/2012 if still unused \
*/
/* EKH: I restored this to BAC_ROUTING (from DEPRECATED) because I found that
+4
View File
@@ -32,6 +32,10 @@ BACNET_STACK_EXPORT
void handler_who_is_unicast(
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src);
BACNET_STACK_EXPORT
void handler_who_is_who_am_i_unicast(
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src);
BACNET_STACK_EXPORT
void handler_who_is_bcast_for_routing(
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src);
+129
View File
@@ -0,0 +1,129 @@
/**
* @file
* @brief A basic You-Are service handler
* @author Steve Karg <skarg@users.sourceforge.net>
* @date June 2025
* @copyright SPDX-License-Identifier: MIT
*/
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
/* BACnet Stack defines - first */
#include "bacnet/bacdef.h"
/* BACnet Stack API */
#include "bacnet/bacdcode.h"
#include "bacnet/bacapp.h"
#include "bacnet/youare.h"
#include "bacnet/basic/services.h"
#include "bacnet/basic/sys/debug.h"
#include "bacnet/basic/object/device.h"
/**
* A basic handler for You-Are responses.
* @param service_request [in] The received message to be handled.
* @param service_len [in] Length of the service_request message.
* @param src [in] The BACNET_ADDRESS of the message's source.
*/
void handler_you_are_json_print(
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src)
{
int len = 0;
uint32_t device_id = 0;
uint16_t vendor_id = 0;
BACNET_CHARACTER_STRING model_name = { 0 };
BACNET_CHARACTER_STRING serial_number = { 0 };
BACNET_OCTET_STRING mac_address = { 0 };
char *model_name_string = NULL;
char *serial_number_string = NULL;
char *mac_address_string = NULL;
(void)src;
len = you_are_request_decode(
service_request, service_len, &device_id, &vendor_id, &model_name,
&serial_number, &mac_address);
if (len > 0) {
debug_printf_stdout("{\n\"You-Are-Request\": {\n");
debug_printf_stdout(" \"vendor-id\" : %u,\n", (unsigned)vendor_id);
len = bacapp_snprintf_character_string(NULL, 0, &model_name);
if (len > 0) {
model_name_string = calloc(sizeof(char), len + 1);
if (model_name_string) {
bacapp_snprintf_character_string(
model_name_string, len + 1, &model_name);
}
}
debug_printf_stdout(
" \"model-name\" : %s,\n",
model_name_string ? model_name_string : "");
len = bacapp_snprintf_character_string(NULL, 0, &serial_number);
if (len > 0) {
serial_number_string = calloc(sizeof(char), len + 1);
if (serial_number_string) {
bacapp_snprintf_character_string(
serial_number_string, len + 1, &serial_number);
}
}
debug_printf_stdout(
" \"serial-number\" : %s",
serial_number_string ? serial_number_string : "");
if (device_id <= BACNET_MAX_INSTANCE) {
debug_printf_stdout(",\n");
debug_printf_stdout(
" \"device-identifier\" : %lu", (unsigned long)device_id);
}
if (mac_address.length > 0) {
debug_printf_stdout(",\n");
len = bacapp_snprintf_octet_string(NULL, 0, &mac_address);
if (len > 0) {
mac_address_string = calloc(sizeof(char), len + 1);
if (mac_address_string) {
bacapp_snprintf_octet_string(
mac_address_string, len + 1, &mac_address);
}
}
debug_printf_stdout(
" \"device-mac-address\" : \"%s\"",
mac_address_string ? mac_address_string : "");
}
debug_printf_stdout("\n }\n}\n");
free(model_name_string);
free(serial_number_string);
free(mac_address_string);
}
return;
}
/**
* A basic handler for You-Are-Request
* @param service_request [in] The received message to be handled.
* @param service_len [in] Length of the service_request message.
* @param src [in] The BACNET_ADDRESS of the message's source.
*/
void handler_you_are_device_id_set(
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src)
{
int len = 0;
uint32_t device_id = 0;
uint16_t vendor_id = 0;
BACNET_CHARACTER_STRING model_name = { 0 }, device_model_name = { 0 };
BACNET_CHARACTER_STRING serial_number = { 0 }, device_serial_number = { 0 };
(void)src;
len = you_are_request_decode(
service_request, service_len, &device_id, &vendor_id, &model_name,
&serial_number, NULL);
if (len > 0) {
characterstring_init_ansi(&device_model_name, Device_Model_Name());
characterstring_init_ansi(
&device_serial_number, Device_Serial_Number());
if ((Device_Vendor_Identifier() == vendor_id) &&
characterstring_same(&device_model_name, &model_name) &&
characterstring_same(&device_serial_number, &serial_number)) {
Device_Set_Object_Instance_Number(device_id);
}
}
return;
}
+35
View File
@@ -0,0 +1,35 @@
/**
* @file
* @brief Header file for a basic You-Are-Request service handler
* @author Steve Karg
* @date June 2025
* @copyright SPDX-License-Identifier: MIT
*/
#ifndef HANDLER_YOU_ARE_H
#define HANDLER_YOU_ARE_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
/* BACnet Stack defines - first */
#include "bacnet/bacdef.h"
/* BACnet Stack API */
#include "bacnet/apdu.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
BACNET_STACK_EXPORT
void handler_you_are_json_print(
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src);
BACNET_STACK_EXPORT
void handler_you_are_device_id_set(
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+92
View File
@@ -0,0 +1,92 @@
/**
* @file
* @brief Send BACnet Who-Is request.
* @author Steve Karg <skarg@users.sourceforge.net>
* @date June 2025
* @copyright SPDX-License-Identifier: MIT
*/
#include <stddef.h>
#include <stdint.h>
#include <string.h>
/* BACnet Stack defines - first */
#include "bacnet/bacdef.h"
/* BACnet Stack API */
#include "bacnet/bacdcode.h"
#include "bacnet/npdu.h"
#include "bacnet/apdu.h"
#include "bacnet/dcc.h"
#include "bacnet/whoami.h"
#include "bacnet/bacenum.h"
/* some demo stuff needed */
#include "bacnet/basic/binding/address.h"
#include "bacnet/basic/object/device.h"
#include "bacnet/basic/services.h"
#include "bacnet/basic/sys/debug.h"
#include "bacnet/basic/tsm/tsm.h"
#include "bacnet/datalink/datalink.h"
/**
* @brief Send a Who-Am-I service request to a remote network
* @param target_address [in] BACnet address of the target network
* @param vendor_id the identity of the vendor of the device initiating
* the Who-Am-I service request.
* @param model_name the model name of the device initiating the Who-Am-I
* service request.
* @param serial_number the serial identifier of the device initiating
* the Who-Am-I service request.
* @return number of bytes sent to the network
*/
int Send_Who_Am_I_To_Network(
BACNET_ADDRESS *target_address,
uint16_t vendor_id,
const BACNET_CHARACTER_STRING *model_name,
const BACNET_CHARACTER_STRING *serial_number)
{
int len = 0;
int pdu_len = 0;
int bytes_sent = 0;
BACNET_NPDU_DATA npdu_data;
BACNET_ADDRESS my_address;
datalink_get_my_address(&my_address);
/* encode the NPDU portion of the packet */
npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL);
pdu_len = npdu_encode_pdu(
&Handler_Transmit_Buffer[0], target_address, &my_address, &npdu_data);
/* encode the APDU portion of the packet */
len = who_am_i_request_service_encode(
&Handler_Transmit_Buffer[pdu_len], vendor_id, model_name,
serial_number);
pdu_len += len;
bytes_sent = datalink_send_pdu(
target_address, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len);
if (bytes_sent <= 0) {
debug_perror("Failed to Send Who-Am-I-Request");
}
return bytes_sent;
}
/**
* @brief Send an I-Am broadcast message in response to Who-Is message
* @param buffer [in] The buffer to use for building and sending the message.
* @return The number of bytes sent to the network.
*/
int Send_Who_Am_I_Broadcast(
uint16_t device_vendor_id,
const char *device_model_name,
const char *device_serial_number)
{
int bytes_sent = 0;
BACNET_CHARACTER_STRING model_name = { 0 }, serial_number = { 0 };
BACNET_ADDRESS dest = { 0 };
datalink_get_broadcast_address(&dest);
characterstring_init_ansi(&model_name, device_model_name);
characterstring_init_ansi(&serial_number, device_serial_number);
bytes_sent = Send_Who_Am_I_To_Network(
&dest, device_vendor_id, &model_name, &serial_number);
return bytes_sent;
}
+38
View File
@@ -0,0 +1,38 @@
/**
* @file
* @brief Header file for a basic Who-Am-I service request
* @author Steve Karg
* @date June 2025
* @copyright SPDX-License-Identifier: MIT
*/
#ifndef SEND_WHO_AM_I_H
#define SEND_WHO_AM_I_H
#include <stddef.h>
#include <stdint.h>
/* BACnet Stack defines - first */
#include "bacnet/bacdef.h"
/* BACnet Stack API */
#include "bacnet/bacstr.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
BACNET_STACK_EXPORT
int Send_Who_Am_I_To_Network(
BACNET_ADDRESS *target_address,
uint16_t vendor_id,
const BACNET_CHARACTER_STRING *model_name,
const BACNET_CHARACTER_STRING *serial_number);
BACNET_STACK_EXPORT
int Send_Who_Am_I_Broadcast(
uint16_t device_vendor_id,
const char *device_model_name,
const char *device_serial_number);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+76
View File
@@ -0,0 +1,76 @@
/**
* @file
* @brief Send BACnet You-Are request.
* @author Steve Karg <skarg@users.sourceforge.net>
* @date June 2025
* @copyright SPDX-License-Identifier: MIT
*/
#include <stddef.h>
#include <stdint.h>
#include <string.h>
/* BACnet Stack defines - first */
#include "bacnet/bacdef.h"
/* BACnet Stack API */
#include "bacnet/bacdcode.h"
#include "bacnet/npdu.h"
#include "bacnet/apdu.h"
#include "bacnet/dcc.h"
#include "bacnet/youare.h"
#include "bacnet/bacenum.h"
/* some demo stuff needed */
#include "bacnet/basic/binding/address.h"
#include "bacnet/basic/object/device.h"
#include "bacnet/basic/services.h"
#include "bacnet/basic/sys/debug.h"
#include "bacnet/basic/tsm/tsm.h"
#include "bacnet/datalink/datalink.h"
/**
* @brief Send a You-Are service request to a remote network
* @param target_address [in] BACnet address of the target network
* @param device_id the Device Object_Identifier to be assigned
* in the qualified device.
* @param vendor_id the identity of the vendor of the device that
* is qualified to receive this You-Are service request.
* @param model_name the model name of the device qualified to receive
* the You-Are service request.
* @param serial_number the serial identifier of the device qualified
* to receive the You-Are service request.
* @param mac_address the device MAC address that is to be configured
* in the qualified device.
* @return number of bytes sent to the network
*/
int Send_You_Are_To_Network(
BACNET_ADDRESS *target_address,
uint32_t device_id,
uint16_t vendor_id,
const BACNET_CHARACTER_STRING *model_name,
const BACNET_CHARACTER_STRING *serial_number,
const BACNET_OCTET_STRING *mac_address)
{
int len = 0;
int pdu_len = 0;
int bytes_sent = 0;
bool data_expecting_reply = false;
BACNET_NPDU_DATA npdu_data;
BACNET_ADDRESS my_address;
datalink_get_my_address(&my_address);
/* encode the NPDU portion of the packet */
npdu_encode_npdu_data(
&npdu_data, data_expecting_reply, MESSAGE_PRIORITY_NORMAL);
pdu_len = npdu_encode_pdu(
&Handler_Transmit_Buffer[0], target_address, &my_address, &npdu_data);
/* encode the APDU portion of the packet */
len = you_are_request_service_encode(
&Handler_Transmit_Buffer[pdu_len], device_id, vendor_id, model_name,
serial_number, mac_address);
pdu_len += len;
bytes_sent = datalink_send_pdu(
target_address, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len);
if (bytes_sent <= 0) {
debug_perror("Failed to Send You-Are-Request");
}
return bytes_sent;
}
+34
View File
@@ -0,0 +1,34 @@
/**
* @file
* @brief Header file for a basic You-Are service request
* @author Steve Karg
* @date June 2025
* @copyright SPDX-License-Identifier: MIT
*/
#ifndef SEND_YOU_ARE_H
#define SEND_YOU_ARE_H
#include <stddef.h>
#include <stdint.h>
/* BACnet Stack defines - first */
#include "bacnet/bacdef.h"
/* BACnet Stack API */
#include "bacnet/bacstr.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
BACNET_STACK_EXPORT
int Send_You_Are_To_Network(
BACNET_ADDRESS *target_address,
uint32_t device_id,
uint16_t vendor_id,
const BACNET_CHARACTER_STRING *model_name,
const BACNET_CHARACTER_STRING *serial_number,
const BACNET_OCTET_STRING *mac_address);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+5
View File
@@ -48,11 +48,13 @@
#include "bacnet/basic/service/h_ts.h"
#include "bacnet/basic/service/h_ucov.h"
#include "bacnet/basic/service/h_upt.h"
#include "bacnet/basic/service/h_whoami.h"
#include "bacnet/basic/service/h_whohas.h"
#include "bacnet/basic/service/h_whois.h"
#include "bacnet/basic/service/h_wp.h"
#include "bacnet/basic/service/h_wpm.h"
#include "bacnet/basic/service/h_write_group.h"
#include "bacnet/basic/service/h_youare.h"
/* application layer service send helpers */
#include "bacnet/basic/service/s_abort.h"
@@ -79,11 +81,14 @@
#include "bacnet/basic/service/s_ts.h"
#include "bacnet/basic/service/s_uevent.h"
#include "bacnet/basic/service/s_upt.h"
#include "bacnet/basic/service/s_whoami.h"
#include "bacnet/basic/service/s_whohas.h"
#include "bacnet/basic/service/s_whois.h"
#include "bacnet/basic/service/s_wp.h"
#include "bacnet/basic/service/s_wpm.h"
#include "bacnet/basic/service/s_write_group.h"
#include "bacnet/basic/service/s_youare.h"
#include "bacnet/basic/service/s_youare.h"
/** @defgroup MISCHNDLR Miscellaneous Service Handlers
* Various utilities and functions to support the service handlers
+153
View File
@@ -0,0 +1,153 @@
/**
* @file
* @brief Encode/Decode Who-Am-I requests
* @author Steve Karg <skarg@users.sourceforge.net>
* @date June 2025
* @copyright SPDX-License-Identifier: GPL-2.0-or-later WITH GCC-exception-2.0
*/
#include <stdint.h>
/* BACnet Stack defines - first */
#include "bacnet/bacdef.h"
/* BACnet Stack API */
#include "bacnet/bacdcode.h"
#include "bacnet/whoami.h"
/**
* @brief Encode a Who-Am-I-Request APDU
*
* Who-Am-I-Request ::= SEQUENCE {
* vendor-id Unsigned16,
* model-name CharacterString,
* serial-number CharacterString
* }
*
* @param apdu [out] Buffer in which the APDU contents are built, or NULL for
* length determination
* @param vendor_id the identity of the vendor of the device initiating
* the Who-Am-I service request.
* @param model_name the model name of the device initiating the Who-Am-I
* service request.
* @param serial_number the serial identifier of the device initiating
* the Who-Am-I service request.
* @return The length of the apdu encoded
*/
int who_am_i_request_encode(
uint8_t *apdu,
uint16_t vendor_id,
const BACNET_CHARACTER_STRING *model_name,
const BACNET_CHARACTER_STRING *serial_number)
{
int len = 0;
int apdu_len = 0;
len = encode_application_unsigned(apdu, vendor_id);
apdu_len += len;
if (apdu) {
apdu += len;
}
len = encode_application_character_string(apdu, model_name);
apdu_len += len;
if (apdu) {
apdu += len;
}
len = encode_application_character_string(apdu, serial_number);
apdu_len += len;
return apdu_len;
}
/**
* @brief Encode a Who-Am-I-Request unconfirmed service APDU
* @param apdu [out] Buffer in which the APDU contents are built, or NULL for
* length determination
* @param vendor_id the identity of the vendor of the device initiating
* the Who-Am-I service request.
* @param model_name the model name of the device initiating the Who-Am-I
* service request.
* @param serial_number the serial identifier of the device initiating
* the Who-Am-I service request.
* @return The length of the apdu encoded
*/
int who_am_i_request_service_encode(
uint8_t *apdu,
uint16_t vendor_id,
const BACNET_CHARACTER_STRING *model_name,
const BACNET_CHARACTER_STRING *serial_number)
{
int len = 0; /* length of each encoding */
int apdu_len = 0; /* total length of the apdu, return value */
if (apdu) {
apdu[0] = PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST;
apdu[1] = SERVICE_UNCONFIRMED_WHO_AM_I; /* service choice */
}
len = 2;
apdu_len += len;
if (apdu) {
apdu += len;
}
len = who_am_i_request_encode(apdu, vendor_id, model_name, serial_number);
apdu_len += len;
return apdu_len;
}
/**
* @brief Decode a Who-Is-Request APDU
* @param apdu [in] Buffer containing the APDU
* @param apdu_size [in] The length of the APDU
* @param vendor_id the identity of the vendor of the device initiating
* the Who-Am-I service request.
* @param model_name the model name of the device initiating the Who-Am-I
* service request.
* @param serial_number the serial identifier of the device initiating
* the Who-Am-I service request.
* @return The number of bytes decoded , or #BACNET_STATUS_ERROR on error
*/
int who_am_i_request_decode(
const uint8_t *apdu,
size_t apdu_size,
uint16_t *vendor_id,
BACNET_CHARACTER_STRING *model_name,
BACNET_CHARACTER_STRING *serial_number)
{
int len = 0, apdu_len = 0;
BACNET_UNSIGNED_INTEGER unsigned_value = 0;
if (!apdu) {
return BACNET_STATUS_ERROR;
}
if (apdu_size == 0) {
return BACNET_STATUS_ERROR;
}
len = bacnet_unsigned_application_decode(
&apdu[apdu_len], apdu_size - apdu_len, &unsigned_value);
if (len > 0) {
if (unsigned_value <= UINT16_MAX) {
if (vendor_id) {
*vendor_id = (uint16_t)unsigned_value;
}
} else {
return BACNET_STATUS_ERROR;
}
} else {
return BACNET_STATUS_ERROR;
}
apdu_len += len;
len = bacnet_character_string_application_decode(
&apdu[apdu_len], apdu_size - apdu_len, model_name);
if (len > 0) {
apdu_len += len;
} else {
return BACNET_STATUS_ERROR;
}
len = bacnet_character_string_application_decode(
&apdu[apdu_len], apdu_size - apdu_len, serial_number);
if (len > 0) {
apdu_len += len;
} else {
return BACNET_STATUS_ERROR;
}
return apdu_len;
}
+46
View File
@@ -0,0 +1,46 @@
/**
* @file
* @brief API for BACnet Who-Am-I service encoder and decoder
* @author Steve Karg <skarg@users.sourceforge.net>
* @date June 2025
* @copyright SPDX-License-Identifier: MIT
*/
#ifndef BACNET_WHO_AM_I_H
#define BACNET_WHO_AM_I_H
#include <stdint.h>
#include <stdbool.h>
/* BACnet Stack defines - first */
#include "bacnet/bacdef.h"
#include "bacnet/bacstr.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
BACNET_STACK_EXPORT
int who_am_i_request_encode(
uint8_t *apdu,
uint16_t vendor_id,
const BACNET_CHARACTER_STRING *Model_Name,
const BACNET_CHARACTER_STRING *Serial_Number);
BACNET_STACK_EXPORT
int who_am_i_request_decode(
const uint8_t *apdu,
size_t apdu_size,
uint16_t *vendor_id,
BACNET_CHARACTER_STRING *Model_Name,
BACNET_CHARACTER_STRING *Serial_Number);
BACNET_STACK_EXPORT
int who_am_i_request_service_encode(
uint8_t *apdu,
uint16_t vendor_id,
const BACNET_CHARACTER_STRING *model_name,
const BACNET_CHARACTER_STRING *serial_number);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+219
View File
@@ -0,0 +1,219 @@
/**
* @file
* @brief Encode/Decode You-Are-Request service
* @author Steve Karg <skarg@users.sourceforge.net>
* @date June 2025
* @copyright SPDX-License-Identifier: GPL-2.0-or-later WITH GCC-exception-2.0
*/
#include <stdint.h>
/* BACnet Stack defines - first */
#include "bacnet/bacdef.h"
/* BACnet Stack API */
#include "bacnet/bacdcode.h"
#include "bacnet/whoami.h"
/**
* @brief Encode a You-Are-Request APDU
*
* You-Are-Request ::= SEQUENCE {
* vendor-id Unsigned16,
* model-name CharacterString,
* serial-number CharacterString,
* device-identifier BACnetObjectIdentifier OPTIONAL,
* device-mac-address OctetString OPTIONAL
* }
*
* @param apdu [out] Buffer in which the APDU contents are built, or NULL for
* length determination
* @param device_id the Device Object_Identifier to be assigned
* in the qualified device.
* @param vendor_id the identity of the vendor of the device that
* is qualified to receive this You-Are service request.
* @param model_name the model name of the device qualified to receive
* the You-Are service request.
* @param serial_number the serial identifier of the device qualified
* to receive the You-Are service request.
* @param mac_address the device MAC address that is to be configured
* in the qualified device.
* @return The length of the apdu encoded
*/
int you_are_request_encode(
uint8_t *apdu,
uint32_t device_id,
uint16_t vendor_id,
const BACNET_CHARACTER_STRING *model_name,
const BACNET_CHARACTER_STRING *serial_number,
const BACNET_OCTET_STRING *mac_address)
{
int len = 0;
int apdu_len = 0;
len = encode_application_unsigned(apdu, vendor_id);
apdu_len += len;
if (apdu) {
apdu += len;
}
len = encode_application_character_string(apdu, model_name);
apdu_len += len;
if (apdu) {
apdu += len;
}
len = encode_application_character_string(apdu, serial_number);
apdu_len += len;
if (apdu) {
apdu += len;
}
if (device_id < BACNET_MAX_INSTANCE) {
len = encode_application_unsigned(apdu, device_id);
apdu_len += len;
if (apdu) {
apdu += len;
}
}
if (mac_address && mac_address->length) {
len = encode_application_octet_string(apdu, mac_address);
apdu_len += len;
}
return apdu_len;
}
/**
* @brief Encode a You-Are-Request unconfirmed service APDU
* @param apdu [out] Buffer in which the APDU contents are built, or NULL for
* length determination
* @param device_id the Device Object_Identifier to be assigned
* in the qualified device.
* @param vendor_id the identity of the vendor of the device that
* is qualified to receive this You-Are service request.
* @param model_name the model name of the device qualified to receive
* the You-Are service request.
* @param serial_number the serial identifier of the device qualified
* to receive the You-Are service request.
* @param mac_address the device MAC address that is to be configured
* in the qualified device.
* @return The length of the apdu encoded
*/
int you_are_request_service_encode(
uint8_t *apdu,
uint32_t device_id,
uint16_t vendor_id,
const BACNET_CHARACTER_STRING *model_name,
const BACNET_CHARACTER_STRING *serial_number,
const BACNET_OCTET_STRING *mac_address)
{
int len = 0; /* length of each encoding */
int apdu_len = 0; /* total length of the apdu, return value */
if (apdu) {
apdu[0] = PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST;
apdu[1] = SERVICE_UNCONFIRMED_YOU_ARE;
}
len = 2;
apdu_len += len;
if (apdu) {
apdu += len;
}
len = you_are_request_encode(
apdu, device_id, vendor_id, model_name, serial_number, mac_address);
apdu_len += len;
return apdu_len;
}
/**
* @brief Decode a You-Are-Request APDU
* @param apdu [in] Buffer containing the APDU
* @param apdu_size [in] The length of the APDU
* @param device_id the Device Object_Identifier to be assigned
* in the qualified device.
* @param vendor_id the identity of the vendor of the device that
* is qualified to receive this You-Are service request.
* @param model_name the model name of the device qualified to receive
* the You-Are service request.
* @param serial_number the serial identifier of the device qualified
* to receive the You-Are service request.
* @param mac_address the device MAC address that is to be configured
* in the qualified device.
* @return The number of bytes decoded , or #BACNET_STATUS_ERROR on error
*/
int you_are_request_decode(
const uint8_t *apdu,
size_t apdu_size,
uint32_t *device_id,
uint16_t *vendor_id,
BACNET_CHARACTER_STRING *model_name,
BACNET_CHARACTER_STRING *serial_number,
BACNET_OCTET_STRING *mac_address)
{
int len = 0, apdu_len = 0;
BACNET_UNSIGNED_INTEGER unsigned_value = 0;
if (!apdu) {
return BACNET_STATUS_ERROR;
}
if (apdu_size == 0) {
return BACNET_STATUS_ERROR;
}
len = bacnet_unsigned_application_decode(
&apdu[apdu_len], apdu_size - apdu_len, &unsigned_value);
if (len > 0) {
if (unsigned_value <= UINT16_MAX) {
if (vendor_id) {
*vendor_id = (uint16_t)unsigned_value;
}
} else {
return BACNET_STATUS_ERROR;
}
} else {
return BACNET_STATUS_ERROR;
}
apdu_len += len;
len = bacnet_character_string_application_decode(
&apdu[apdu_len], apdu_size - apdu_len, model_name);
if (len > 0) {
apdu_len += len;
} else {
return BACNET_STATUS_ERROR;
}
len = bacnet_character_string_application_decode(
&apdu[apdu_len], apdu_size - apdu_len, serial_number);
if (len > 0) {
apdu_len += len;
} else {
return BACNET_STATUS_ERROR;
}
len = bacnet_unsigned_application_decode(
&apdu[apdu_len], apdu_size - apdu_len, &unsigned_value);
if (len > 0) {
apdu_len += len;
if (unsigned_value <= UINT32_MAX) {
if (device_id) {
*device_id = (uint32_t)unsigned_value;
}
} else {
return BACNET_STATUS_ERROR;
}
} else if (len == 0) {
/* optional - skip apdu_len increment */
if (device_id) {
*device_id = UINT32_MAX;
}
} else {
return BACNET_STATUS_ERROR;
}
len = bacnet_octet_string_application_decode(
&apdu[apdu_len], apdu_size - apdu_len, mac_address);
if (len > 0) {
apdu_len += len;
} else if (len == 0) {
/* optional - skip apdu_len increment */
if (mac_address) {
mac_address->length = 0;
}
} else {
return BACNET_STATUS_ERROR;
}
return apdu_len;
}
+52
View File
@@ -0,0 +1,52 @@
/**
* @file
* @brief API for BACnet You-Are service encoder and decoder
* @author Steve Karg <skarg@users.sourceforge.net>
* @date June 2025
* @copyright SPDX-License-Identifier: MIT
*/
#ifndef BACNET_YOU_ARE_H
#define BACNET_YOU_ARE_H
#include <stdint.h>
#include <stdbool.h>
/* BACnet Stack defines - first */
#include "bacnet/bacdef.h"
#include "bacnet/bacstr.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
BACNET_STACK_EXPORT
int you_are_request_encode(
uint8_t *apdu,
uint32_t device_id,
uint16_t vendor_id,
const BACNET_CHARACTER_STRING *model_name,
const BACNET_CHARACTER_STRING *serial_number,
const BACNET_OCTET_STRING *mac_address);
BACNET_STACK_EXPORT
int you_are_request_decode(
const uint8_t *apdu,
size_t apdu_size,
uint32_t *device_id,
uint16_t *vendor_id,
BACNET_CHARACTER_STRING *model_name,
BACNET_CHARACTER_STRING *serial_number,
BACNET_OCTET_STRING *mac_address);
BACNET_STACK_EXPORT
int you_are_request_service_encode(
uint8_t *apdu,
uint32_t device_id,
uint16_t vendor_id,
const BACNET_CHARACTER_STRING *model_name,
const BACNET_CHARACTER_STRING *serial_number,
const BACNET_OCTET_STRING *mac_address);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif