diff --git a/.gitignore b/.gitignore index 879aa379..69695f54 100644 --- a/.gitignore +++ b/.gitignore @@ -69,6 +69,7 @@ address_cache **/out/* Obj/ Release/ +libwebsockets/ # cmake artifacts CMakeLists.txt.user @@ -76,7 +77,7 @@ CMakeCache.txt CMakeFiles/ cmake_install.cmake CTestTestfile.cmake - +# zephyr artifacts /twister-out/* /test/build/ /cmake-build-* diff --git a/CHANGELOG.md b/CHANGELOG.md index fd2026ae..fd706d22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -95,6 +95,9 @@ The git repositories are hosted at the following sites: ### Added +* Added You-Are and Who-Am-I encoding, decoding, unit tests, and command + line applications bacyouare and bacwhoami. Added You-Are handler and + integrated with apps/server example with JSON printout. (#1024) * Added object type and services supported BACnetBitString sizes for protocol revision 25-30. (#1020) * Added new BACnet text for BACnetLifeSafetyMode, BACnetLifeSafetyOperation, diff --git a/CMakeLists.txt b/CMakeLists.txt index 9335def1..45322b29 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -464,6 +464,8 @@ add_library(${PROJECT_NAME} src/bacnet/basic/service/h_ucov.h src/bacnet/basic/service/h_upt.c src/bacnet/basic/service/h_upt.h + src/bacnet/basic/service/h_whoami.c + src/bacnet/basic/service/h_whoami.h src/bacnet/basic/service/h_whohas.c src/bacnet/basic/service/h_whohas.h src/bacnet/basic/service/h_whois.c @@ -474,6 +476,8 @@ add_library(${PROJECT_NAME} src/bacnet/basic/service/h_wpm.h src/bacnet/basic/service/h_write_group.c src/bacnet/basic/service/h_write_group.h + src/bacnet/basic/service/h_youare.c + src/bacnet/basic/service/h_youare.h src/bacnet/basic/service/s_abort.c src/bacnet/basic/service/s_abort.h src/bacnet/basic/service/s_ack_alarm.c @@ -522,6 +526,8 @@ add_library(${PROJECT_NAME} src/bacnet/basic/service/s_uevent.h src/bacnet/basic/service/s_upt.c src/bacnet/basic/service/s_upt.h + src/bacnet/basic/service/s_whoami.c + src/bacnet/basic/service/s_whoami.h src/bacnet/basic/service/s_whohas.c src/bacnet/basic/service/s_whohas.h src/bacnet/basic/service/s_whois.c @@ -532,6 +538,8 @@ add_library(${PROJECT_NAME} src/bacnet/basic/service/s_wpm.h src/bacnet/basic/service/s_write_group.c src/bacnet/basic/service/s_write_group.h + src/bacnet/basic/service/s_youare.c + src/bacnet/basic/service/s_youare.h src/bacnet/basic/services.h src/bacnet/basic/sys/bigend.c src/bacnet/basic/sys/bigend.h @@ -648,6 +656,8 @@ add_library(${PROJECT_NAME} src/bacnet/timesync.c src/bacnet/timesync.h src/bacnet/version.h + src/bacnet/whoami.c + src/bacnet/whoami.h src/bacnet/whohas.c src/bacnet/whohas.h src/bacnet/whois.c @@ -658,6 +668,8 @@ add_library(${PROJECT_NAME} src/bacnet/wpm.h src/bacnet/write_group.c src/bacnet/write_group.h + src/bacnet/youare.c + src/bacnet/youare.h $<$:src/bacnet/basic/ucix/ucix.c> $<$:src/bacnet/basic/ucix/ucix.h>) target_sources( @@ -1090,6 +1102,9 @@ if(BACNET_STACK_BUILD_APPS) add_executable(uptransfer apps/uptransfer/main.c) target_link_libraries(uptransfer PRIVATE ${PROJECT_NAME}) + add_executable(whoami apps/who-am-i/main.c) + target_link_libraries(whoami PRIVATE ${PROJECT_NAME}) + add_executable(whohas apps/whohas/main.c) target_link_libraries(whohas PRIVATE ${PROJECT_NAME}) @@ -1116,6 +1131,9 @@ if(BACNET_STACK_BUILD_APPS) add_executable(writepropm apps/writepropm/main.c) target_link_libraries(writepropm PRIVATE ${PROJECT_NAME}) + + add_executable(youare apps/you-are/main.c) + target_link_libraries(youare PRIVATE ${PROJECT_NAME}) if(BACDL_BSC) add_executable(sc-hub apps/sc-hub/main.c) target_link_libraries(sc-hub PRIVATE ${PROJECT_NAME}) diff --git a/Makefile b/Makefile index cdf5bef0..33a758c1 100644 --- a/Makefile +++ b/Makefile @@ -238,6 +238,10 @@ mstpcrc: uevent: $(MAKE) -s -C apps $@ +.PHONY: who-am-i +who-am-i: + $(MAKE) -s -C apps $@ + .PHONY: whois whois: $(MAKE) -s -C apps $@ @@ -250,6 +254,10 @@ writepropm: writegroup: $(MAKE) -s -C apps $@ +.PHONY: you-are +you-are: + $(MAKE) -s -C apps $@ + .PHONY: router router: $(MAKE) -s -C apps $@ diff --git a/apps/Makefile b/apps/Makefile index f8bb1d88..aaa77a56 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -222,7 +222,8 @@ 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 server-discover apdu writegroup server-basic server-mini + who-am-i you-are apdu writegroup \ + delete-object server-discover server-basic server-mini ifeq (${BACDL_DEFINE},-DBACDL_BIP=1) SUBDIRS += whoisrouter iamrouter initrouter whatisnetnum netnumis @@ -443,6 +444,14 @@ uptransfer: $(BACNET_LIB_TARGET) whois: $(BACNET_LIB_TARGET) $(MAKE) -B -C $@ +.PHONY: who-am-i +who-am-i: $(BACNET_LIB_TARGET) + $(MAKE) -B -C $@ + +.PHONY: you-are +you-are: $(BACNET_LIB_TARGET) + $(MAKE) -B -C $@ + .PHONY: writebdt writebdt: $(BACNET_LIB_TARGET) $(MAKE) -B -C $@ diff --git a/apps/server/main.c b/apps/server/main.c index 485a6866..249997a6 100644 --- a/apps/server/main.c +++ b/apps/server/main.c @@ -174,10 +174,9 @@ static void Init_Service_Handlers(void) (unsigned)object_data.object_instance); } } - /* update structured view with this device instance */ - Structured_View_Update(); /* we need to handle who-is to support dynamic device binding */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); + apdu_set_unconfirmed_handler( + SERVICE_UNCONFIRMED_WHO_IS, handler_who_is_who_am_i_unicast); 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... */ @@ -206,6 +205,8 @@ static void Init_Service_Handlers(void) SERVICE_UNCONFIRMED_UTC_TIME_SYNCHRONIZATION, handler_timesync_utc); apdu_set_unconfirmed_handler( SERVICE_UNCONFIRMED_TIME_SYNCHRONIZATION, handler_timesync); + apdu_set_unconfirmed_handler( + SERVICE_UNCONFIRMED_YOU_ARE, handler_you_are_json_print); apdu_set_confirmed_handler( SERVICE_CONFIRMED_SUBSCRIBE_COV, handler_cov_subscribe); apdu_set_unconfirmed_handler( @@ -292,6 +293,7 @@ int main(int argc, char *argv[]) uint32_t elapsed_milliseconds = 0; uint32_t elapsed_seconds = 0; BACNET_CHARACTER_STRING DeviceName; + uint32_t device_id = 0xFFFFFFFF; #if defined(BACNET_TIME_MASTER) BACNET_DATE_TIME bdatetime; #endif @@ -375,10 +377,29 @@ int main(int argc, char *argv[]) } dlenv_init(); atexit(datalink_cleanup); - /* broadcast an I-Am on startup */ - Send_I_Am(&Handler_Transmit_Buffer[0]); +#if BACNET_PROTOCOL_REVISION >= 22 + if (Device_Object_Instance_Number() == BACNET_MAX_INSTANCE) { + apdu_set_unconfirmed_handler( + SERVICE_UNCONFIRMED_YOU_ARE, handler_you_are_device_id_set); + /* The Who-Am-I service is used by a sending BACnet-user + to indicate that it requires identity configuration + via the You-Are service. */ + Send_Who_Am_I_Broadcast( + Device_Vendor_Identifier(), Device_Model_Name(), + Device_Serial_Number()); + } +#endif /* loop forever */ for (;;) { + if (device_id != Device_Object_Instance_Number()) { + device_id = Device_Object_Instance_Number(); + /* update structured view with this device instance */ + Structured_View_Update(); + if (Device_Object_Instance_Number() != BACNET_MAX_INSTANCE) { + /* broadcast an I-Am on startup */ + Send_I_Am(&Handler_Transmit_Buffer[0]); + } + } /* input */ pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); diff --git a/apps/who-am-i/Makefile b/apps/who-am-i/Makefile new file mode 100644 index 00000000..4673fc4e --- /dev/null +++ b/apps/who-am-i/Makefile @@ -0,0 +1,39 @@ +#Makefile to build BACnet Application using GCC compiler + +# Executable file name +TARGET = bacwhoami +# BACnet objects that are used with this app +BACNET_OBJECT_DIR = $(BACNET_SRC_DIR)/bacnet/basic/object +SRC = main.c \ + $(BACNET_OBJECT_DIR)/client/device-client.c \ + $(BACNET_OBJECT_DIR)/netport.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 diff --git a/apps/who-am-i/main.c b/apps/who-am-i/main.c new file mode 100644 index 00000000..7426cc04 --- /dev/null +++ b/apps/who-am-i/main.c @@ -0,0 +1,356 @@ +/** + * @file + * @brief command line tool that sends a BACnet Who-Am-I request to devices, + * and prints any You-Are responses received. + * @author Steve Karg + * @date June 2025 + * @copyright SPDX-License-Identifier: MIT + */ +#include +#include +#include +#include +#include +#if (__STDC_VERSION__ >= 199901L) && defined(__STDC_ISO_10646__) +#include +#endif +/* BACnet Stack defines - first */ +#include "bacnet/bacdef.h" +/* BACnet Stack API */ +#include "bacnet/bactext.h" +#include "bacnet/whoami.h" +#include "bacnet/youare.h" +#include "bacnet/npdu.h" +#include "bacnet/apdu.h" +#include "bacnet/bactext.h" +#include "bacnet/version.h" +/* some demo stuff needed */ +#include "bacnet/basic/binding/address.h" +#include "bacnet/basic/sys/mstimer.h" +#include "bacnet/basic/sys/filename.h" +#include "bacnet/basic/services.h" +#include "bacnet/basic/tsm/tsm.h" +#include "bacnet/basic/object/device.h" +#include "bacnet/datalink/datalink.h" +#include "bacnet/datalink/dlenv.h" +#include "bacport.h" + +/* buffer used for receive */ +static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; + +/* global variables used in this file */ +static long Source_Vendor_ID = BACNET_VENDOR_ID; +static BACNET_CHARACTER_STRING Source_Model_Name = { 0 }; +static BACNET_CHARACTER_STRING Source_Serial_Number = { 0 }; +static bool Error_Detected = false; +/* debug info printing */ +static bool BACnet_Debug_Enabled; + +/** + * @brief Print the BACnet Abort reason + * @param src source address of the device reporting the abort + * @param invoke_id invoke ID of the message triggering the abort + * @param abort_reason abort reason given by the device reporting the abort + * @param server + */ +static void MyAbortHandler( + BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t abort_reason, bool server) +{ + (void)src; + (void)server; + fprintf( + stderr, "BACnet Abort[%u]: %s\n", (unsigned)invoke_id, + bactext_abort_reason_name(abort_reason)); + Error_Detected = true; +} + +static void +MyRejectHandler(BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t reject_reason) +{ + (void)src; + fprintf( + stderr, "BACnet Reject[%u]: %s\n", invoke_id, + bactext_reject_reason_name(reject_reason)); + Error_Detected = true; +} + +static void init_service_handlers(void) +{ + Device_Init(NULL); + /* Note: this applications doesn't need to handle who-is + it is confusing for the user! */ + /* 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); + /* we must implement read property - it's required! */ + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); + /* handle the unconfirmed request that may be sent to us */ + apdu_set_unconfirmed_handler( + SERVICE_UNCONFIRMED_YOU_ARE, handler_you_are_json_print); + /* handle any errors coming back */ + apdu_set_abort_handler(MyAbortHandler); + apdu_set_reject_handler(MyRejectHandler); +} + +static void print_usage(const char *filename) +{ + printf("Usage: %s", filename); + printf(" [vendor-id model-name serial-number]\n"); + printf(" [--dnet][--dadr][--mac]\n"); + printf(" [--version][--help]\n"); +} + +static void print_help(const char *filename) +{ + printf("Send a BACnet Who-Am-I service request to the network\n" + "and wait for any You-Are service responses.\n"); + printf("\n"); + printf("vendor-id:\n" + "the identity of the vendor of the device initiating" + "the Who-Am-I service request.\n"); + printf("\n"); + printf("model-name:\n" + "the model name of the device initiating the Who-Am-I" + "service request.\n"); + printf("\n"); + printf("serial-number:\n" + "the serial identifier of the device initiating" + "the Who-Am-I service request.\n"); + printf("\n"); + printf("--mac A\n" + "BACnet mac address." + "Valid ranges are from 00 to FF (hex) for MS/TP or ARCNET,\n" + "or an IP string with optional port number like 10.1.2.3:47808\n" + "or an Ethernet MAC in hex like 00:21:70:7e:32:bb\n"); + printf("\n"); + printf("--dnet N\n" + "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"); + printf("\n"); + printf("--dadr A\n" + "BACnet mac address on the destination BACnet network number.\n" + "Valid ranges are from 00 to FF (hex) for MS/TP or ARCNET,\n" + "or an IP string with optional port number like 10.1.2.3:47808\n" + "or an Ethernet MAC in hex like 00:21:70:7e:32:bb\n"); + printf("\n"); + printf("--repeat\n" + "Send the message repeatedly until signalled to quit.\n" + "Default is disabled, using the APDU timeout as time to quit.\n"); + printf("\n"); + printf("--retry C\n" + "Send the message C number of times\n" + "Default is retry 0, only sending one time.\n"); + printf("\n"); + printf("--timeout T\n" + "Wait T milliseconds after sending before retry\n" + "Default delay is 3000ms.\n"); + printf("\n"); + printf("--delay M\n" + "Wait M milliseconds for responses after sending\n" + "Default delay is 100ms.\n"); + printf("\n"); + printf("Example:\n"); + printf( + "Send a Who-Am-I-Request to DNET 123:\n" + "%s --dnet 123\n", + filename); + printf( + "Send a Who-Am-I-Request to MAC 10.0.0.1 DNET 123 DADR 05h:\n" + "%s --mac 10.0.0.1 --dnet 123 --dadr 05\n", + filename); + printf( + "Send a Who-Am-I-Request to MAC 10.1.2.3:47808:\n" + "%s --mac 10.1.2.3:47808\n", + filename); + printf( + "Send a Who-Am-I-Request from " + "vendor-id 123 model-name 456 serial-number 789\n" + "%s 123 456 789\n", + filename); + printf( + "Send a Who-Am-I-Request from the default " + "vendor-id, model-name, and serial-number.\n" + "%s\n", + filename); +} + +int main(int argc, char *argv[]) +{ + BACNET_ADDRESS src = { 0 }; /* address where message came from */ + uint16_t pdu_len = 0; + unsigned timeout_milliseconds = 0; + unsigned delay_milliseconds = 100; + struct mstimer apdu_timer = { 0 }; + struct mstimer datalink_timer = { 0 }; + long dnet = -1; + BACNET_MAC_ADDRESS mac = { 0 }; + BACNET_MAC_ADDRESS adr = { 0 }; + BACNET_ADDRESS dest = { 0 }; + bool global_broadcast = true; + int argi = 0; + unsigned int target_args = 0; + const char *filename = NULL; + bool repeat_forever = false; + long retry_count = 0; + + /* check for local environment settings */ + if (getenv("BACNET_DEBUG")) { + BACnet_Debug_Enabled = true; + } + /* decode any command line parameters */ + 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) 2025 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], "--mac") == 0) { + if (++argi < argc) { + if (bacnet_address_mac_from_ascii(&mac, argv[argi])) { + global_broadcast = false; + } + } + } else if (strcmp(argv[argi], "--dnet") == 0) { + if (++argi < argc) { + dnet = strtol(argv[argi], NULL, 0); + if ((dnet >= 0) && (dnet <= BACNET_BROADCAST_NETWORK)) { + global_broadcast = false; + } + } + } else if (strcmp(argv[argi], "--dadr") == 0) { + if (++argi < argc) { + if (bacnet_address_mac_from_ascii(&adr, argv[argi])) { + global_broadcast = false; + } + } + } else if (strcmp(argv[argi], "--repeat") == 0) { + repeat_forever = true; + } else if (strcmp(argv[argi], "--retry") == 0) { + if (++argi < argc) { + retry_count = strtol(argv[argi], NULL, 0); + if (retry_count < 0) { + retry_count = 0; + } + } + } else if (strcmp(argv[argi], "--timeout") == 0) { + if (++argi < argc) { + timeout_milliseconds = strtol(argv[argi], NULL, 0); + } + } else if (strcmp(argv[argi], "--delay") == 0) { + if (++argi < argc) { + delay_milliseconds = strtol(argv[argi], NULL, 0); + } + } else { + if (target_args == 0) { + Source_Vendor_ID = strtol(argv[argi], NULL, 0); + target_args++; + } else if (target_args == 1) { + characterstring_init_ansi(&Source_Model_Name, argv[argi]); + target_args++; + } else if (target_args == 2) { + characterstring_init_ansi(&Source_Serial_Number, argv[argi]); + target_args++; + } else { + print_usage(filename); + return 1; + } + } + } + if (global_broadcast) { + datalink_get_broadcast_address(&dest); + } else { + if ((dnet < 0) || (dnet > BACNET_BROADCAST_NETWORK)) { + if (adr.len && mac.len) { + dnet = BACNET_BROADCAST_NETWORK; + } else if (mac.len) { + dnet = 0; + } else { + dest.net = BACNET_BROADCAST_NETWORK; + } + } + bacnet_address_init(&dest, &mac, dnet, &adr); + } + if (Source_Vendor_ID > UINT16_MAX) { + fprintf( + stderr, "vendor-id=%ld and must not be greater than %u\n", + Source_Vendor_ID, UINT16_MAX); + return 1; + } + /* setup my info */ + Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE); + if (characterstring_length(&Source_Model_Name) == 0) { + characterstring_init_ansi(&Source_Model_Name, Device_Model_Name()); + } + if (characterstring_length(&Source_Serial_Number) == 0) { + characterstring_init_ansi( + &Source_Serial_Number, Device_Serial_Number()); + } + init_service_handlers(); + dlenv_init(); +#if (__STDC_VERSION__ >= 199901L) && defined(__STDC_ISO_10646__) + /* Internationalized programs must call setlocale() + * to initiate a specific language operation. + * This can be done by calling setlocale() as follows. + * If your native locale doesn't use UTF-8 encoding + * you need to replace the empty string with a + * locale like "en_US.utf8" which is the same as the string + * used in the enviromental variable "LANG=en_US.UTF-8". + */ + setlocale(LC_ALL, ""); +#endif + atexit(datalink_cleanup); + if (timeout_milliseconds == 0) { + timeout_milliseconds = apdu_timeout() * apdu_retries(); + } + mstimer_set(&apdu_timer, timeout_milliseconds); + mstimer_set(&datalink_timer, 1000); + /* send the request */ + Send_Who_Am_I_To_Network( + &dest, Source_Vendor_ID, &Source_Model_Name, &Source_Serial_Number); + if (retry_count > 0) { + retry_count--; + } + /* loop forever */ + for (;;) { + /* returns 0 bytes on timeout */ + pdu_len = + datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, delay_milliseconds); + /* process */ + if (pdu_len) { + npdu_handler(&src, &Rx_Buf[0], pdu_len); + } + if (Error_Detected) { + break; + } + if (mstimer_expired(&datalink_timer)) { + datalink_maintenance_timer( + mstimer_interval(&datalink_timer) / 1000); + mstimer_reset(&datalink_timer); + } + if (mstimer_expired(&apdu_timer)) { + if (repeat_forever || retry_count) { + Send_Who_Am_I_To_Network( + &dest, Source_Vendor_ID, &Source_Model_Name, + &Source_Serial_Number); + retry_count--; + } else { + break; + } + mstimer_reset(&apdu_timer); + } + } + + return 0; +} diff --git a/apps/whois/main.c b/apps/whois/main.c index d19ba2ca..ddbd5515 100644 --- a/apps/whois/main.c +++ b/apps/whois/main.c @@ -181,6 +181,9 @@ static void init_service_handlers(void) SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); /* handle the reply (request) coming back */ apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, my_i_am_handler); + /* handle any Who-Am-I requests we receive */ + apdu_set_unconfirmed_handler( + SERVICE_UNCONFIRMED_WHO_AM_I, handler_who_am_i_json_print); /* handle any errors coming back */ apdu_set_abort_handler(MyAbortHandler); apdu_set_reject_handler(MyRejectHandler); diff --git a/apps/you-are/Makefile b/apps/you-are/Makefile new file mode 100644 index 00000000..77319e23 --- /dev/null +++ b/apps/you-are/Makefile @@ -0,0 +1,39 @@ +#Makefile to build BACnet Application using GCC compiler + +# Executable file name +TARGET = bacyouare +# BACnet objects that are used with this app +BACNET_OBJECT_DIR = $(BACNET_SRC_DIR)/bacnet/basic/object +SRC = main.c \ + $(BACNET_OBJECT_DIR)/client/device-client.c \ + $(BACNET_OBJECT_DIR)/netport.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 diff --git a/apps/you-are/main.c b/apps/you-are/main.c new file mode 100644 index 00000000..8c195675 --- /dev/null +++ b/apps/you-are/main.c @@ -0,0 +1,340 @@ +/** + * @file + * @brief command line tool that sends a BACnet service to the network: + * You-Are message + * @author Steve Karg + * @date June 2025 + * @copyright SPDX-License-Identifier: MIT + */ +#include +#include +#include +#include +#include +#include /* for time */ +/* BACnet Stack defines - first */ +#include "bacnet/bacdef.h" +/* BACnet Stack API */ +#include "bacnet/bactext.h" +#include "bacnet/youare.h" +#include "bacnet/whoami.h" +#include "bacnet/npdu.h" +#include "bacnet/apdu.h" +#include "bacnet/version.h" +/* some demo stuff needed */ +#include "bacnet/basic/binding/address.h" +#include "bacnet/basic/object/device.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" + +/* buffer used for receive */ +static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; + +/* parsed command line parameters */ +static uint32_t Target_Device_ID = BACNET_MAX_INSTANCE; +static uint16_t Target_Vendor_ID; +static BACNET_OCTET_STRING Target_MAC_Address; +static BACNET_CHARACTER_STRING Target_Model_Name; +static BACNET_CHARACTER_STRING Target_Serial_Number; +static bool Target_Send_You_Are_Approved; +static BACNET_ADDRESS Target_Destination_Address; + +/* flag for signalling errors */ +static bool Error_Detected = false; + +static void MyAbortHandler( + BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t abort_reason, bool server) +{ + (void)src; + (void)server; + printf( + "BACnet Abort[%u]: %s\n", (unsigned)invoke_id, + bactext_abort_reason_name(abort_reason)); + Error_Detected = true; +} + +static void +MyRejectHandler(BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t reject_reason) +{ + (void)src; + printf( + "BACnet Reject[%u]: %s\n", (unsigned)invoke_id, + bactext_reject_reason_name(reject_reason)); + Error_Detected = true; +} + +/** + * 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. + */ +static void My_Who_Am_I_Handler( + 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 }; + + (void)src; + len = who_am_i_request_decode( + service_request, service_len, &vendor_id, &model_name, &serial_number); + if (len > 0) { + printf("Who-Am-I Requested.\n"); + if ((Target_Vendor_ID == vendor_id) && + characterstring_same(&Target_Model_Name, &model_name) && + characterstring_same(&Target_Serial_Number, &serial_number)) { + bacnet_address_copy(&Target_Destination_Address, src); + Target_Send_You_Are_Approved = true; + } + } + + return; +} + +/** + * @brief Initialize the BACnet service handlers that this application + * needs to use. + */ +static void Init_Service_Handlers(void) +{ + Device_Init(NULL); + /* we need to handle who-is + to support dynamic device binding to us */ + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); + /* 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); + /* we must implement read property - it's required! */ + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); + /* handle the reply (request) coming back */ + apdu_set_unconfirmed_handler( + SERVICE_UNCONFIRMED_WHO_AM_I, My_Who_Am_I_Handler); + /* handle any errors coming back */ + apdu_set_abort_handler(MyAbortHandler); + apdu_set_reject_handler(MyRejectHandler); +} + +static void print_usage(const char *filename) +{ + printf( + "Usage: %s [device-instance vendor-id model-name serial [MAC]]\n", + filename); + printf(" [--whois]\n"); + printf(" [--dnet][--dadr][--mac]\n"); + printf(" [--version][--help]\n"); +} + +static void print_help(const char *filename) +{ + printf("Send BACnet You-Are message to the network.\n"); + printf("--whois\n" + "Optionally send a Who-Is request to 4194303 to elicit Who-Am-I.\n"); + printf("--mac A\n" + "Optional BACnet mac address." + "Valid ranges are from 00 to FF (hex) for MS/TP or ARCNET,\n" + "or an IP string with optional port number like 10.1.2.3:47808\n" + "or an Ethernet MAC in hex like 00:21:70:7e:32:bb\n"); + printf("\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"); + printf("\n"); + printf("--dadr A\n" + "Optional BACnet mac address on the destination BACnet network " + "number.\n" + "Valid ranges are from 00 to FF (hex) for MS/TP or ARCNET,\n" + "or an IP string with optional port number like 10.1.2.3:47808\n" + "or an Ethernet MAC in hex like 00:21:70:7e:32:bb\n"); + printf("\n"); + printf("--repeat\n" + "Send the message repeatedly until signalled to quit.\n" + "Default is to not repeat, sending only a single message.\n"); + printf("\n"); + printf("--retry C\n" + "Send the message C number of times\n" + "Default is retry 0, only sending one time.\n"); + printf("\n"); + printf("--delay\n" + "Delay, in milliseconds, between repeated messages.\n" + "Default delay is 100ms.\n"); + printf("\n"); + printf("device-instance:\n" + "the Device Object_Identifier to be assigned in the qualified\n" + "device. BACnet device-ID 0..4194302\n"); + printf("\n"); + printf("vendor-id:\n" + "the identity of the vendor of the device that is qualified\n" + "to receive this You-Are service request.\n"); + printf("\n"); + printf("model-name:\n" + "the model name of the device qualified to receive the\n" + "You-Are service request.\n"); + printf("\n"); + printf("serial:\n" + "the serial number of the device qualified to receive the\n" + "You-Are service request.\n"); + printf("MAC:\n"); + printf("the device MAC address that is to be configured in the\n" + "qualified device. Either the device-instance,\n" + "or MAC, or both, shall be present. Accepts ASCII hexidecimal."); + printf("\n"); + printf( + "Example:\n" + "To send a You-Are message of instance=1234 vendor-id=260\n" + "model-name 456 serial-number 789 MAC 0F to the network:\n" + "%s 1234 260 456 789 0F\n", + filename); +} + +int main(int argc, char *argv[]) +{ + BACNET_ADDRESS src = { 0 }; /* address where message came from */ + uint16_t pdu_len = 0; + long dnet = -1; + BACNET_MAC_ADDRESS mac = { 0 }; + BACNET_MAC_ADDRESS adr = { 0 }; + BACNET_ADDRESS dest = { 0 }; + bool specific_address = false; + bool repeat_forever = false; + bool send_whois = false; + unsigned timeout = 100; /* milliseconds */ + int argi = 0; + unsigned int target_args = 0; + const char *filename = NULL; + long retry_count = 0; + struct mstimer apdu_timer = { 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) 2025 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], "--mac") == 0) { + if (++argi < argc) { + if (bacnet_address_mac_from_ascii(&mac, argv[argi])) { + specific_address = true; + } + } + } else if (strcmp(argv[argi], "--dnet") == 0) { + if (++argi < argc) { + dnet = strtol(argv[argi], NULL, 0); + if ((dnet >= 0) && (dnet <= BACNET_BROADCAST_NETWORK)) { + specific_address = true; + } + } + } else if (strcmp(argv[argi], "--dadr") == 0) { + if (++argi < argc) { + if (bacnet_address_mac_from_ascii(&adr, argv[argi])) { + specific_address = true; + } + } + } else if (strcmp(argv[argi], "--whois") == 0) { + send_whois = true; + } else if (strcmp(argv[argi], "--repeat") == 0) { + repeat_forever = true; + } else if (strcmp(argv[argi], "--retry") == 0) { + if (++argi < argc) { + retry_count = strtol(argv[argi], NULL, 0); + if (retry_count < 0) { + retry_count = 0; + } + } + } else if (strcmp(argv[argi], "--delay") == 0) { + if (++argi < argc) { + timeout = strtol(argv[argi], NULL, 0); + } + } else { + if (target_args == 0) { + Target_Device_ID = strtol(argv[argi], NULL, 0); + target_args++; + } else if (target_args == 1) { + Target_Vendor_ID = strtol(argv[argi], NULL, 0); + target_args++; + } else if (target_args == 2) { + characterstring_init_ansi(&Target_Model_Name, argv[argi]); + target_args++; + } else if (target_args == 3) { + characterstring_init_ansi(&Target_Serial_Number, argv[argi]); + target_args++; + } else if (target_args == 4) { + octetstring_init_ascii_hex(&Target_MAC_Address, argv[argi]); + target_args++; + } else { + print_usage(filename); + return 1; + } + } + } + if (specific_address) { + if ((dnet < 0) || (dnet > BACNET_BROADCAST_NETWORK)) { + if (adr.len && mac.len) { + dnet = BACNET_BROADCAST_NETWORK; + } else if (mac.len) { + dnet = 0; + } else { + dest.net = BACNET_BROADCAST_NETWORK; + } + } + bacnet_address_init(&dest, &mac, dnet, &adr); + } else { + datalink_get_broadcast_address(&dest); + } + /* setup my info */ + Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE); + Init_Service_Handlers(); + dlenv_init(); + atexit(datalink_cleanup); + /* send the request */ + if (send_whois) { + mstimer_set(&apdu_timer, apdu_timeout() * apdu_retries()); + Send_WhoIs_To_Network(&dest, BACNET_MAX_INSTANCE, BACNET_MAX_INSTANCE); + } else { + bacnet_address_copy(&Target_Destination_Address, &dest); + Target_Send_You_Are_Approved = true; + } + do { + if (Target_Send_You_Are_Approved) { + Send_You_Are_To_Network( + &dest, Target_Device_ID, Target_Vendor_ID, &Target_Model_Name, + &Target_Serial_Number, &Target_MAC_Address); + if (send_whois) { + break; + } + } + if (repeat_forever || retry_count || mstimer_interval(&apdu_timer)) { + /* returns 0 bytes on timeout */ + pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); + /* process */ + if (pdu_len) { + npdu_handler(&src, &Rx_Buf[0], pdu_len); + } + if (Error_Detected) { + break; + } + if (retry_count > 0) { + retry_count--; + } + } + } while (repeat_forever || retry_count || !mstimer_expired(&apdu_timer)); + + return 0; +} diff --git a/src/bacnet/bacapp.c b/src/bacnet/bacapp.c index e3ca8e10..5e92b7da 100644 --- a/src/bacnet/bacapp.c +++ b/src/bacnet/bacapp.c @@ -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) diff --git a/src/bacnet/bacapp.h b/src/bacnet/bacapp.h index 541bbc6d..536537bf 100644 --- a/src/bacnet/bacapp.h +++ b/src/bacnet/bacapp.h @@ -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( diff --git a/src/bacnet/basic/object/client/device-client.c b/src/bacnet/basic/object/client/device-client.c index eeed9191..3734da06 100644 --- a/src/bacnet/basic/object/client/device-client.c +++ b/src/bacnet/basic/object/client/device-client.c @@ -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; diff --git a/src/bacnet/basic/service/h_whoami.c b/src/bacnet/basic/service/h_whoami.c new file mode 100644 index 00000000..7db248d6 --- /dev/null +++ b/src/bacnet/basic/service/h_whoami.c @@ -0,0 +1,71 @@ +/** + * @file + * @brief A basic Who-Am-I service handler + * @author Steve Karg + * @date June 2025 + * @copyright SPDX-License-Identifier: MIT + */ +#include +#include +#include +#include +/* 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; +} diff --git a/src/bacnet/basic/service/h_whoami.h b/src/bacnet/basic/service/h_whoami.h new file mode 100644 index 00000000..30a23adb --- /dev/null +++ b/src/bacnet/basic/service/h_whoami.h @@ -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 +#include +#include +#include +/* 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 diff --git a/src/bacnet/basic/service/h_whois.c b/src/bacnet/basic/service/h_whois.c index 21246825..da54356c 100644 --- a/src/bacnet/basic/service/h_whois.c +++ b/src/bacnet/basic/service/h_whois.c @@ -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 diff --git a/src/bacnet/basic/service/h_whois.h b/src/bacnet/basic/service/h_whois.h index 10eb79e4..36ad40f3 100644 --- a/src/bacnet/basic/service/h_whois.h +++ b/src/bacnet/basic/service/h_whois.h @@ -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); diff --git a/src/bacnet/basic/service/h_youare.c b/src/bacnet/basic/service/h_youare.c new file mode 100644 index 00000000..17a85167 --- /dev/null +++ b/src/bacnet/basic/service/h_youare.c @@ -0,0 +1,129 @@ +/** + * @file + * @brief A basic You-Are service handler + * @author Steve Karg + * @date June 2025 + * @copyright SPDX-License-Identifier: MIT + */ +#include +#include +#include +#include +/* 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; +} diff --git a/src/bacnet/basic/service/h_youare.h b/src/bacnet/basic/service/h_youare.h new file mode 100644 index 00000000..8ab176eb --- /dev/null +++ b/src/bacnet/basic/service/h_youare.h @@ -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 +#include +#include +#include +/* 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 diff --git a/src/bacnet/basic/service/s_whoami.c b/src/bacnet/basic/service/s_whoami.c new file mode 100644 index 00000000..c6f60f01 --- /dev/null +++ b/src/bacnet/basic/service/s_whoami.c @@ -0,0 +1,92 @@ +/** + * @file + * @brief Send BACnet Who-Is request. + * @author Steve Karg + * @date June 2025 + * @copyright SPDX-License-Identifier: MIT + */ +#include +#include +#include +/* 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; +} diff --git a/src/bacnet/basic/service/s_whoami.h b/src/bacnet/basic/service/s_whoami.h new file mode 100644 index 00000000..42b4c8b3 --- /dev/null +++ b/src/bacnet/basic/service/s_whoami.h @@ -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 +#include +/* 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 diff --git a/src/bacnet/basic/service/s_youare.c b/src/bacnet/basic/service/s_youare.c new file mode 100644 index 00000000..b196faa7 --- /dev/null +++ b/src/bacnet/basic/service/s_youare.c @@ -0,0 +1,76 @@ +/** + * @file + * @brief Send BACnet You-Are request. + * @author Steve Karg + * @date June 2025 + * @copyright SPDX-License-Identifier: MIT + */ +#include +#include +#include +/* 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; +} diff --git a/src/bacnet/basic/service/s_youare.h b/src/bacnet/basic/service/s_youare.h new file mode 100644 index 00000000..e48fe245 --- /dev/null +++ b/src/bacnet/basic/service/s_youare.h @@ -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 +#include +/* 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 diff --git a/src/bacnet/basic/services.h b/src/bacnet/basic/services.h index 13140090..9ac9f60d 100644 --- a/src/bacnet/basic/services.h +++ b/src/bacnet/basic/services.h @@ -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 diff --git a/src/bacnet/whoami.c b/src/bacnet/whoami.c new file mode 100644 index 00000000..92bf35ad --- /dev/null +++ b/src/bacnet/whoami.c @@ -0,0 +1,153 @@ +/** + * @file + * @brief Encode/Decode Who-Am-I requests + * @author Steve Karg + * @date June 2025 + * @copyright SPDX-License-Identifier: GPL-2.0-or-later WITH GCC-exception-2.0 + */ +#include +/* 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; +} diff --git a/src/bacnet/whoami.h b/src/bacnet/whoami.h new file mode 100644 index 00000000..59c401f2 --- /dev/null +++ b/src/bacnet/whoami.h @@ -0,0 +1,46 @@ +/** + * @file + * @brief API for BACnet Who-Am-I service encoder and decoder + * @author Steve Karg + * @date June 2025 + * @copyright SPDX-License-Identifier: MIT + */ +#ifndef BACNET_WHO_AM_I_H +#define BACNET_WHO_AM_I_H + +#include +#include +/* 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 diff --git a/src/bacnet/youare.c b/src/bacnet/youare.c new file mode 100644 index 00000000..ab862154 --- /dev/null +++ b/src/bacnet/youare.c @@ -0,0 +1,219 @@ +/** + * @file + * @brief Encode/Decode You-Are-Request service + * @author Steve Karg + * @date June 2025 + * @copyright SPDX-License-Identifier: GPL-2.0-or-later WITH GCC-exception-2.0 + */ +#include +/* 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; +} diff --git a/src/bacnet/youare.h b/src/bacnet/youare.h new file mode 100644 index 00000000..d3b6910c --- /dev/null +++ b/src/bacnet/youare.h @@ -0,0 +1,52 @@ +/** + * @file + * @brief API for BACnet You-Are service encoder and decoder + * @author Steve Karg + * @date June 2025 + * @copyright SPDX-License-Identifier: MIT + */ +#ifndef BACNET_YOU_ARE_H +#define BACNET_YOU_ARE_H + +#include +#include +/* 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 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b6d02467..292975f1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -118,11 +118,13 @@ list(APPEND testdirs bacnet/timestamp bacnet/timesync bacnet/weeklyschedule + bacnet/whoami bacnet/whohas bacnet/whois bacnet/wp bacnet/wpm bacnet/write_group + bacnet/youare ) # bacnet/basic/* diff --git a/test/bacnet/whoami/CMakeLists.txt b/test/bacnet/whoami/CMakeLists.txt new file mode 100644 index 00000000..9987aefe --- /dev/null +++ b/test/bacnet/whoami/CMakeLists.txt @@ -0,0 +1,46 @@ +# SPDX-License-Identifier: MIT + +cmake_minimum_required(VERSION 3.10 FATAL_ERROR) + +get_filename_component(basename ${CMAKE_CURRENT_SOURCE_DIR} NAME) +project(test_${basename} + VERSION 1.0.0 + LANGUAGES C) + + +string(REGEX REPLACE + "/test/bacnet/[a-zA-Z_/-]*$" + "/src" + SRC_DIR + ${CMAKE_CURRENT_SOURCE_DIR}) +string(REGEX REPLACE + "/test/bacnet/[a-zA-Z_/-]*$" + "/test" + TST_DIR + ${CMAKE_CURRENT_SOURCE_DIR}) +set(ZTST_DIR "${TST_DIR}/ztest/src") + +add_compile_definitions( + BIG_ENDIAN=0 + CONFIG_ZTEST=1 + ) + +include_directories( + ${SRC_DIR} + ${TST_DIR}/ztest/include + ) + +add_executable(${PROJECT_NAME} + # File(s) under test + ${SRC_DIR}/bacnet/whoami.c + # Support files and stubs (pathname alphabetical) + ${SRC_DIR}/bacnet/bacdcode.c + ${SRC_DIR}/bacnet/bacint.c + ${SRC_DIR}/bacnet/bacreal.c + ${SRC_DIR}/bacnet/bacstr.c + ${SRC_DIR}/bacnet/basic/sys/bigend.c + # Test and test library files + ./src/main.c + ${ZTST_DIR}/ztest_mock.c + ${ZTST_DIR}/ztest.c + ) diff --git a/test/bacnet/whoami/src/main.c b/test/bacnet/whoami/src/main.c new file mode 100644 index 00000000..bcf446e9 --- /dev/null +++ b/test/bacnet/whoami/src/main.c @@ -0,0 +1,132 @@ +/** + * @file + * @brief Unit test for Who-Am-I-Request + * @author Steve Karg + * @date June 2025 + * @copyright SPDX-License-Identifier: MIT + */ +#include +#include +#include +#include + +/** + * @addtogroup bacnet_tests + * @{ + */ + +/** + * @brief Who-Am-I-Request service decoder + * @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 + */ +static int who_am_i_request_service_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; + + if (!apdu) { + return BACNET_STATUS_ERROR; + } + if (apdu_size < 2) { + return BACNET_STATUS_ERROR; + } + if (apdu[0] != PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST) { + return BACNET_STATUS_ERROR; + } + if (apdu[1] != SERVICE_UNCONFIRMED_WHO_AM_I) { + return BACNET_STATUS_ERROR; + } + apdu += 2; + apdu_size -= 2; + len = who_am_i_request_decode( + apdu, apdu_size, vendor_id, model_name, serial_number); + + return len; +} + +#if defined(CONFIG_ZTEST_NEW_API) +ZTEST(who_am_i_tests, test_who_am_i) +#else +static void test_who_am_i(void) +#endif +{ + uint8_t apdu[480] = { 0 }; + int len = 0, apdu_len = 0, null_len = 0; + bool status; + uint16_t vendor_id = 0, test_vendor_id = 0; + BACNET_CHARACTER_STRING model_name = { 0 }; + BACNET_CHARACTER_STRING test_model_name = { 0 }; + BACNET_CHARACTER_STRING serial_number = { 0 }; + BACNET_CHARACTER_STRING test_serial_number = { 0 }; + + vendor_id = 260; + characterstring_init_ansi(&model_name, "BDK ATXX4 MSTP"); + characterstring_init_ansi(&serial_number, "1234567890"); + + null_len = who_am_i_request_service_encode( + NULL, vendor_id, &model_name, &serial_number); + len = who_am_i_request_service_encode( + apdu, vendor_id, &model_name, &serial_number); + zassert_equal(null_len, len, NULL); + apdu_len = len; + len = who_am_i_request_service_decode( + apdu, apdu_len, &test_vendor_id, &test_model_name, &test_serial_number); + zassert_not_equal(len, BACNET_STATUS_ERROR, NULL); + zassert_equal(test_vendor_id, vendor_id, NULL); + status = characterstring_same(&test_model_name, &model_name); + zassert_true(status, NULL); + status = characterstring_same(&test_serial_number, &serial_number); + zassert_true(status, NULL); + len = who_am_i_request_service_decode( + NULL, apdu_len, &test_vendor_id, &test_model_name, &test_serial_number); + zassert_equal(len, BACNET_STATUS_ERROR, NULL); + + null_len = + who_am_i_request_encode(NULL, vendor_id, &model_name, &serial_number); + len = who_am_i_request_encode(apdu, vendor_id, &model_name, &serial_number); + zassert_equal(null_len, len, NULL); + apdu_len = len; + len = who_am_i_request_decode( + apdu, apdu_len, &test_vendor_id, &test_model_name, &test_serial_number); + zassert_not_equal(len, BACNET_STATUS_ERROR, NULL); + zassert_equal(test_vendor_id, vendor_id, NULL); + status = characterstring_same(&test_model_name, &model_name); + zassert_true(status, NULL); + status = characterstring_same(&test_serial_number, &serial_number); + zassert_true(status, NULL); + len = who_am_i_request_decode( + NULL, apdu_len, &test_vendor_id, &test_model_name, &test_serial_number); + while (apdu_len) { + apdu_len--; + len = who_am_i_request_decode( + apdu, apdu_len, &test_vendor_id, &test_model_name, + &test_serial_number); + zassert_equal( + len, BACNET_STATUS_ERROR, "apdu_len=%d len=%d", apdu_len, len); + } +} +/** + * @} + */ +#if defined(CONFIG_ZTEST_NEW_API) +ZTEST_SUITE(who_am_i_tests, NULL, NULL, NULL, NULL, NULL); +#else +void test_main(void) +{ + ztest_test_suite(who_am_i_tests, ztest_unit_test(test_who_am_i)); + + ztest_run_test_suite(who_am_i_tests); +} +#endif diff --git a/test/bacnet/youare/CMakeLists.txt b/test/bacnet/youare/CMakeLists.txt new file mode 100644 index 00000000..5e92f906 --- /dev/null +++ b/test/bacnet/youare/CMakeLists.txt @@ -0,0 +1,46 @@ +# SPDX-License-Identifier: MIT + +cmake_minimum_required(VERSION 3.10 FATAL_ERROR) + +get_filename_component(basename ${CMAKE_CURRENT_SOURCE_DIR} NAME) +project(test_${basename} + VERSION 1.0.0 + LANGUAGES C) + + +string(REGEX REPLACE + "/test/bacnet/[a-zA-Z_/-]*$" + "/src" + SRC_DIR + ${CMAKE_CURRENT_SOURCE_DIR}) +string(REGEX REPLACE + "/test/bacnet/[a-zA-Z_/-]*$" + "/test" + TST_DIR + ${CMAKE_CURRENT_SOURCE_DIR}) +set(ZTST_DIR "${TST_DIR}/ztest/src") + +add_compile_definitions( + BIG_ENDIAN=0 + CONFIG_ZTEST=1 + ) + +include_directories( + ${SRC_DIR} + ${TST_DIR}/ztest/include + ) + +add_executable(${PROJECT_NAME} + # File(s) under test + ${SRC_DIR}/bacnet/youare.c + # Support files and stubs (pathname alphabetical) + ${SRC_DIR}/bacnet/bacdcode.c + ${SRC_DIR}/bacnet/bacint.c + ${SRC_DIR}/bacnet/bacreal.c + ${SRC_DIR}/bacnet/bacstr.c + ${SRC_DIR}/bacnet/basic/sys/bigend.c + # Test and test library files + ./src/main.c + ${ZTST_DIR}/ztest_mock.c + ${ZTST_DIR}/ztest.c + ) diff --git a/test/bacnet/youare/src/main.c b/test/bacnet/youare/src/main.c new file mode 100644 index 00000000..1e1a5df4 --- /dev/null +++ b/test/bacnet/youare/src/main.c @@ -0,0 +1,173 @@ +/** + * @file + * @brief Unit test for You-Are-Request + * @author Steve Karg + * @date June 2025 + * @copyright SPDX-License-Identifier: MIT + */ +#include +#include +#include +#include + +/** + * @addtogroup bacnet_tests + * @{ + */ + +/** + * @brief You-Are-Request service decoder + * @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 + */ +static int you_are_request_service_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; + + if (!apdu) { + return BACNET_STATUS_ERROR; + } + if (apdu_size < 2) { + return BACNET_STATUS_ERROR; + } + if (apdu[0] != PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST) { + return BACNET_STATUS_ERROR; + } + if (apdu[1] != SERVICE_UNCONFIRMED_YOU_ARE) { + return BACNET_STATUS_ERROR; + } + apdu += 2; + apdu_size -= 2; + len = you_are_request_decode( + apdu, apdu_size, device_id, vendor_id, model_name, serial_number, + mac_address); + + return len; +} + +#if defined(CONFIG_ZTEST_NEW_API) +ZTEST(you_are_tests, test_you_are) +#else +static void test_you_are(void) +#endif +{ + uint8_t apdu[480] = { 0 }; + int len = 0, apdu_len = 0, null_len = 0; + bool status; + uint16_t vendor_id = 0, test_vendor_id = 0; + uint32_t device_id = 0, test_device_id = 0; + BACNET_CHARACTER_STRING model_name = { 0 }; + BACNET_CHARACTER_STRING test_model_name = { 0 }; + BACNET_CHARACTER_STRING serial_number = { 0 }; + BACNET_CHARACTER_STRING test_serial_number = { 0 }; + BACNET_OCTET_STRING mac_address = { 0 }; + BACNET_OCTET_STRING test_mac_address = { 0 }; + + device_id = 42; + vendor_id = 260; + characterstring_init_ansi(&model_name, "BDK ATXX4 MSTP"); + characterstring_init_ansi(&serial_number, "1234567890"); + octetstring_init_ascii_hex(&mac_address, "0123456789ABCDEF"); + null_len = you_are_request_service_encode( + NULL, device_id, vendor_id, &model_name, &serial_number, &mac_address); + len = you_are_request_service_encode( + apdu, device_id, vendor_id, &model_name, &serial_number, &mac_address); + zassert_equal(null_len, len, NULL); + apdu_len = len; + len = you_are_request_service_decode( + apdu, apdu_len, &test_device_id, &test_vendor_id, &test_model_name, + &test_serial_number, &test_mac_address); + zassert_not_equal(len, BACNET_STATUS_ERROR, NULL); + zassert_equal(test_device_id, device_id, NULL); + zassert_equal(test_vendor_id, vendor_id, NULL); + status = characterstring_same(&test_model_name, &model_name); + zassert_true(status, NULL); + status = characterstring_same(&test_serial_number, &serial_number); + zassert_true(status, NULL); + status = octetstring_value_same(&test_mac_address, &mac_address); + zassert_true(status, NULL); + len = you_are_request_service_decode( + NULL, apdu_len, &test_device_id, &test_vendor_id, &test_model_name, + &test_serial_number, &test_mac_address); + zassert_equal(len, BACNET_STATUS_ERROR, NULL); + + null_len = you_are_request_encode( + NULL, device_id, vendor_id, &model_name, &serial_number, &mac_address); + len = you_are_request_encode( + apdu, device_id, vendor_id, &model_name, &serial_number, &mac_address); + zassert_equal(null_len, len, NULL); + apdu_len = len; + len = you_are_request_decode( + apdu, apdu_len, &test_device_id, &test_vendor_id, &test_model_name, + &test_serial_number, &test_mac_address); + zassert_not_equal(len, BACNET_STATUS_ERROR, NULL); + zassert_equal(test_device_id, device_id, NULL); + zassert_equal(test_vendor_id, vendor_id, NULL); + status = characterstring_same(&test_model_name, &model_name); + zassert_true(status, NULL); + status = characterstring_same(&test_serial_number, &serial_number); + zassert_true(status, NULL); + status = octetstring_value_same(&test_mac_address, &mac_address); + zassert_true(status, NULL); + /* check for NULL apdu to decode */ + len = you_are_request_decode( + NULL, apdu_len, &test_device_id, &test_vendor_id, &test_model_name, + &test_serial_number, &test_mac_address); + /* check for APDU too short - use no optional parameters */ + apdu_len = you_are_request_encode( + apdu, UINT32_MAX, vendor_id, &model_name, &serial_number, NULL); + while (apdu_len) { + apdu_len--; + len = you_are_request_decode( + apdu, apdu_len, &test_device_id, &test_vendor_id, &test_model_name, + &test_serial_number, &test_mac_address); + zassert_equal( + len, BACNET_STATUS_ERROR, "apdu_len=%d len=%d", apdu_len, len); + } + /* check for optional device-id parameter */ + apdu_len = you_are_request_encode( + apdu, UINT32_MAX, vendor_id, &model_name, &serial_number, &mac_address); + len = you_are_request_decode( + apdu, apdu_len, &test_device_id, &test_vendor_id, &test_model_name, + &test_serial_number, &test_mac_address); + zassert_not_equal(len, BACNET_STATUS_ERROR, NULL); + zassert_equal(test_device_id, UINT32_MAX, NULL); + zassert_equal(test_vendor_id, vendor_id, NULL); + status = characterstring_same(&test_model_name, &model_name); + zassert_true(status, NULL); + status = characterstring_same(&test_serial_number, &serial_number); + zassert_true(status, NULL); + status = octetstring_value_same(&test_mac_address, &mac_address); + zassert_true(status, NULL); +} +/** + * @} + */ +#if defined(CONFIG_ZTEST_NEW_API) +ZTEST_SUITE(you_are_tests, NULL, NULL, NULL, NULL, NULL); +#else +void test_main(void) +{ + ztest_test_suite(you_are_tests, ztest_unit_test(test_you_are)); + + ztest_run_test_suite(you_are_tests); +} +#endif