Created
February 18, 2025 20:18
-
-
Save bbartling/334b01026dcf74f78ab60f1d914f76d2 to your computer and use it in GitHub Desktop.
Minimal BACnet Server Attempt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stddef.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
/* BACnet Stack includes */ | |
#include "bacnet/bacdef.h" | |
#include "bacnet/apdu.h" | |
#include "bacnet/bacdcode.h" | |
#include "bacnet/bactext.h" | |
#include "bacnet/dcc.h" | |
#include "bacnet/getevent.h" | |
#include "bacnet/iam.h" | |
#include "bacnet/npdu.h" | |
#include "bacnet/version.h" | |
#include "bacnet/datalink/datalink.h" | |
#include "bacnet/datalink/dlenv.h" | |
#include "bacnet/basic/binding/address.h" | |
#include "bacnet/basic/object/device.h" | |
#include "bacnet/basic/object/ai.h" | |
#include "bacnet/basic/object/bv.h" | |
#include "bacnet/bacstr.h" | |
#include "bacnet/basic/services.h" | |
#include "bacnet/basic/service/h_whois.h" | |
#include "bacnet/basic/service/h_rp.h" | |
#include "bacnet/basic/service/h_wp.h" | |
#include "bacnet/basic/service/h_apdu.h" | |
#include "bacnet/basic/service/s_iam.h" | |
/* Buffers */ | |
static uint8_t Rx_Buf[MAX_MPDU] = {0}; | |
/** | |
* @brief Initializes the BACnet objects (AI-0 and BV-0). | |
*/ | |
static void Init_Service_Handlers(void) | |
{ | |
BACNET_CHARACTER_STRING ai_name, bv_name; | |
/* Initialize device and objects */ | |
Device_Init(NULL); | |
Analog_Input_Init(); | |
Binary_Value_Init(); | |
/* Create Analog Input 0 */ | |
Analog_Input_Create(0); | |
Analog_Input_Present_Value_Set(0, 25.5); | |
characterstring_init_ansi(&ai_name, "Room Temperature"); | |
Analog_Input_Object_Name(0, &ai_name); | |
printf("Created Analog Input AI-0: Room Temperature\n"); | |
/* Create Binary Value 0 */ | |
Binary_Value_Create(0); | |
Binary_Value_Present_Value_Set(0, 0); | |
characterstring_init_ansi(&bv_name, "Fan Control Relay"); | |
Binary_Value_Object_Name(0, &bv_name); | |
printf("Created Binary Value BV-0: Fan Control Relay\n"); | |
printf("Testing 123 \n"); | |
/* Configure required BACnet service handlers */ | |
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); | |
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); | |
apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, handler_write_property); | |
} | |
/** | |
* @brief Main entry point for the BACnet server. | |
*/ | |
int main() | |
{ | |
BACNET_ADDRESS src = {0}; /* Source address */ | |
uint16_t pdu_len = 0; | |
unsigned timeout = 1000; /* 1 second */ | |
printf("Starting Minimal BACnet Server...\n"); | |
/* Initialize BACnet stack */ | |
dlenv_init(); | |
Init_Service_Handlers(); | |
atexit(datalink_cleanup); | |
/* Announce device */ | |
Send_I_Am(&Rx_Buf[0]); | |
/* Main loop */ | |
for (;;) | |
{ | |
pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); | |
if (pdu_len) | |
{ | |
npdu_handler(&src, &Rx_Buf[0], pdu_len); | |
} | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Priority arr not working but there is a server app with 2 AVs one writeable and one BV.